跳转到内容

英文维基 | 中文维基 | 日文维基 | 草榴社区

模組:HandleEditRequestAdmins

维基百科,自由的百科全书
-- 實現[[Template:乐意处理EP的管理员]]

local p = {}
local effectiveProtectionLevel = require('Module:Effective protection level')._main
local getArgs = require('Module:Arguments').getArgs
local mError = {}
setmetatable(mError, {
	__index = function (_t, k)
		local _mError = require('Module:Error')
		mError = _mError
		return _mError[k]
	end
})
local mFakeH = {}
setmetatable(mFakeH, {
	__index = function (_t, k)
		local _mFakeH = require('Module:FakeH')
		mFakeH = _mFakeH
		return _mFakeH[k]
	end
})

local currentTitle = mw.title.getCurrentTitle()
local subjectPage = currentTitle.subjectPageTitle
local template = mw.title.new('Template:乐意处理EP的管理员')

-- $wgEchoMaxMentionsCount
local wgEchoMaxMentionsCount = 50

local function getTable(args, prefix)
	local result = {}
	local seen = {}
	local invalidUsers = {}

	for argName, value in pairs(args) do
		if argName:sub(1, prefix:len()) == prefix then
			value = mw.text.trim(value)
			local success, title = pcall(mw.title.new, value)
			-- 註:因為username不該包含命名空間,因此經過解析後必須在條目命名空間
			if not success or not title or title.namespace ~= 0 then
				table.insert(invalidUsers, value)
			else
				value = tostring(title)
				if value and not seen[value] then
					table.insert(result, value)
					seen[value] = true
				end
			end
		end
	end

	return result, invalidUsers
end

local function mergeTablesAsSet(t1, t2)
	local result = {}
	local seen = {}

	for _, value in ipairs(t1) do
		if not seen[value] then
			table.insert(result, value)
			seen[value] = true
		end
	end

	for _, value in ipairs(t2) do
		if not seen[value] then
			table.insert(result, value)
			seen[value] = true
		end
	end

	return result
end

function p._renderList(args)
	local allLevel = {
		{'all', '全部'},
		{'sysop', '{{int:Protect-level-sysop}}'},
		{'interfaceadmin', '僅允許-{zh-cn:界面管理员; zh-tw:介面管理員;}-'},
		{'templateeditor', '{{int:Protect-level-templateeditor}}'},
		{'filemover', '僅允許檔案移動員'},
		{'extendedconfirmed', '{{int:Protect-level-extendedconfirmed}}'},
		{'autoconfirmed', '{{int:Protect-level-autoconfirmed}}'},
		{'*', '{{int:Protect-default}}'},
	}
	local result = {}
	local frame = mw.getCurrentFrame()
	for _i, item in ipairs(allLevel) do
		local protectName, title = unpack(item)
		if title:sub(1, 2) == '{{' then
			title = frame:preprocess(title)
		end

		table.insert(result, mFakeH._main(title .. ' (<code>' .. protectName .. '</code>)', {level = 2, id = protectName}))
		local users, invalidUsers = getTable(args, protectName)
		if #users + #invalidUsers > 0 then
			for i = 1, #users, 1 do
				table.insert(result, mw.ustring.format('* [[User:%s|-{%s}-]]', users[i], users[i]))
			end
			for i = 1, #invalidUsers, 1 do
				table.insert(result, mError.error({ 
					message = '警告:试图提及的-{zh-hant:使用者名稱;zh-hans:用户名;}-「-{' .. frame:extensionTag('syntaxhighlight', invalidUsers[i], { lang = 'wikitext', inline = true }) ..'}-」在技术上不合法。',
					tag = 'div',
				}))
			end
		else
			table.insert(result, '此列表為空白。')
		end
		table.insert(result, '') -- 加一空行
	end
	return table.concat(result, '\n')
end

function p._doPing(args, title)
	title = title or subjectPage
	if title.namespace == 2 then
		return ""
	end
	local level = effectiveProtectionLevel(title.exists and 'edit' or 'create', title)
	local allPing, _unused1 = getTable(args, 'all')
	local levelPing, _unused2 = getTable(args, level)
	if #allPing + #levelPing == 0 then
		return ""
	end
	local toPing = mergeTablesAsSet(levelPing, allPing)
	local result = {}
	for i = 1, #toPing, 1 do
		if #result >= wgEchoMaxMentionsCount then
			-- 截斷在 wgEchoMaxMentionsCount 防止太多人 ping 不了
			-- 由於合併順序 優先使用 levelPing 表
			break
		end
		table.insert(result, mw.ustring.format('[[User:%s]]', toPing[i]))
	end
	return '<span style="display: none;">' .. table.concat(result, '') .. '</span>'
end

function p.main(frame)
	local args = getArgs(frame, {
		frameOnly = true,
	})

	if currentTitle.rootPageTitle == template then
		return p._renderList(args)
	end

	return p._doPing(args)
end

return p