Mô đun:Vandal-m

Bách khoa toàn thư mở Wikipedia
Tài liệu mô đun[tạo]
-- This module implements {{vandal-m}}.

local p = {}

local su = require('Mô đun:Su')._main

local function makeWikilink(link, display)
	if display then
		return string.format('[[%s|%s]]', link, display)
	else
		return string.format('[[%s]]', link)
	end
end

local function makeUrlLink(data, display)
	local url = mw.uri.new(data)
	url = tostring(url)
	return string.format('[%s %s]', url, display)
end

local function makeFullUrlLink(page, query, display)
	local url = mw.uri.fullUrl(page, query)
	url = tostring(url)
	return string.format('[%s %s]', url, display)
end

local function getTitle(page)
	local success, title = pcall(mw.title.new, page)
	if success then
		return title
	else
		return nil
	end
end

local function getLinkIfExists(pagePrefix, username, display)
	local title = getTitle(pagePrefix .. username)
	if title and title.exists then
		return makeWikilink(title.prefixedText, display)
	end
end

function p.main(frame)
	local args = require('Mô đun:Arguments').getArgs(frame, {parentOnly = true})
	return p._main(args)
end

function p._main(args)
	local username, usernameEncoded
	do
		local lang = mw.language.getContentLanguage()
		username = args.User or args[1] or 'Ví dụ'
		username = lang:ucfirst(username)
		usernameEncoded = mw.uri.encode(username)
	end

	local links = {}

	-- Talk
	links[#links + 1] = makeWikilink('Thảo luận thành viên:' .. username, 'thảo luận')

	-- Contribs
	links[#links + 1] = makeWikilink(
		'Đặc biệt:Đóng_ góp/' .. username,
		'<span title="Các đóng góp của ' .. username .. '">đóng góp</span>'
	)

	-- Block log and autoblocks
	do
		local blockLogLink = makeFullUrlLink(
			'Đặc biệt:Nhật trình/block',
			{page = 'User:' .. username},
			'<span title="Nhật trình cấm của '
				.. username
				.. '" style="color:#002bb8">nhật&nbsp;trình</span>'
		)
		local autoblocksLink = makeUrlLink(
			{
				host = 'tools.wmflabs.org',
				path = '/xtools/autoblock/',
				query = {user = username}
			},
			'<sup title="Nhật trình tự động cấm của '
				.. username
				.. '" style="color:#002bb8">tự động cấm</sup>'
		)
		links[#links + 1] = blockLogLink .. autoblocksLink
	end
	-- Sockpuppet categories
	-- There isn't a seperator between these and the suspected sockpuppets link,
	-- so we will add them later instead of adding them to the links table.
	local sockCategoryLinks
	do
		local confirmed = getTitle(
			'Thể loại:Tài khoản con rối của ' .. username
		)
		local suspected = getTitle(
			'Thể loại:Tài khoản bị nghi là con rối của ' .. username
		)
		if confirmed and confirmed.exists or suspected and suspected.exists then
			local sup, sub
			if confirmed and confirmed.exists then
				sup = '&ensp;' .. makeWikilink(
					':' .. confirmed.prefixedText,
					'Rối&nbsp;đã&nbsp;xác&nbsp;nhận'
				)
			end
			if suspected and suspected.exists then
				sub = '&ensp;' .. makeWikilink(
					':' .. suspected.prefixedText,
					'Rối&nbsp;bị&nbsp;nghi&nbsp;ngờ'
				)
			end
			sockCategoryLinks = su(sup, sub)
		end
	end

	-- Add the user link and the outer span tags.
	return string.format(
		'<span id="%s" class="plainlinks">%s (%s%s)</span>',
		username,
		makeWikilink('Thành viên:' .. username, username),
		table.concat(links, ' &bull; '),
		sockCategoryLinks or ''
	)
end

return p