被永久保护的模块

模組:Reply to

维基百科,自由的百科全书
跳到导航 跳到搜索
文档图示 模块文档[创建]
local p = {}

local function makeError(msg)
	msg ='[[Module:Reply_to]]錯誤:' .. msg
	return mw.text.tag('strong', {['class']='error'}, msg)
end

local function errormsg(msg)
	return makeError(msg)
end

local function nouser(template)
	return errormsg( '使用[[' ..  template .. ']]時出現錯誤:並無提供-{zh:使用者名稱;zh-tw:使用者名稱;zh-cn:用户名;}-。模板用法見於[[' ..  template .. ']]。' )
end

local function maxuser(template, count)
	return errormsg( '[[' ..  template .. ']]最多支持' .. (count or '50') .. '个-{zh-hant:用戶;zh-tw:使用者;zh-hans:用户;}-。如果需要超过' .. (count or '50') .. '个,必须使用多个模板。模板用法見於[[' ..  template .. ']]。' )
end

function p.replyto(frame)
	local origArgs = frame:getParent().args
	local args = {}
	local maxArg = 1
	local usernames = 0
	for k, v in pairs(origArgs) do
		if type(k) == 'number' then
			if mw.ustring.match(v,'%S') then
				if k > maxArg then maxArg = k end
				usernames = usernames + 1
				local title = mw.title.new(v)
				if not title then return makeError('輸入內容含有被禁止輸入的字元。') end
				args[k] = title.rootText
			end
		elseif v == '' and k:sub(0,5) == 'label' then
			args[k] = '​'
		else
			args[k] = v
		end
	end

	if usernames > (tonumber(frame.args.max) or 50) then
		return makeError(string.format(
			'受MediaWiki系統限制,最多只能同時提及%s人。',
			tostring(frame.args.max or 50)
		))
	else
		if usernames < 1 then
			if frame.args.example then args[1] = frame.args.example else return makeError('未提供-{zh:使用者名稱;zh-hk:用戶名;zh-tw:使用者名稱;zh-cn:用户名;}-。') end
		end
		args['label1'] = args['label1'] or args['label']
		local isfirst = true
		local outStr = args['prefix'] or '@'
		for i = 1, maxArg do
			if args[i] then
				if isfirst then
					isfirst = false
				else
					if ( (usernames > 2) or ((usernames == 2) and (args['c'] == '')) ) then outStr = outStr..'、' end
				end
				outStr = string.format(
					'%s[[User:%s|%s]]',
					outStr,
					args[i],
					args['label'..tostring(i)] or args[i]
				)
			end
		end
		outStr = outStr..(args['p'] or ':')
		return mw.text.tag('span', {['class']='template-ping'}, outStr)
	end
end

function p.ping(frame)
	return p.replyto(frame)
end

function p.replyto2(frame)
	local origArgs = frame:getParent().args
	local args = {}
	local maxArg = 1
	local usernames = 0
	for k, v in pairs(origArgs) do
		if type(k) == 'number' then
			if mw.ustring.match(v,'%S') then
				if k > maxArg then maxArg = k end
				usernames = usernames + 1
				local title = mw.title.new(v)
				if not title then return makeError('輸入內容含有被禁止輸入的字元。') end
				args[k] = title.rootText
			end
		elseif v == '' and k:sub(0,5) == 'label' then
			args[k] = '&#x200B;'
		else
			args[k] = v
		end
	end

	if usernames > (tonumber(frame.args.max) or 50) then
		return makeError(string.format(
			'受MediaWiki系統限制,最多只能同時提及%s人。',
			tostring(frame.args.max or 50)
		))
	else
		if usernames < 1 then
			if frame.args.example then args[1] = frame.args.example else return makeError('未提供-{zh:使用者名稱;zh-hk:用戶名;zh-tw:使用者名稱;zh-cn:用户名;}-。') end
		end
		args['label1'] = args['label1'] or args['label']
		local isfirst = true
		local outStr = args['prefix'] or '@'
		for i = 1, maxArg do
			if args[i] then
				if isfirst then
					isfirst = false
				else
					if ( (usernames > 2) or ((usernames == 2) and (args['c'] == '')) ) then outStr = outStr..'、' end
				end
				outStr = string.format(
					'%s[[User:%s|%s]]',
					outStr,
					args[i],
					args['label'..tostring(i)] or args[i]
				)
			end
		end
		outStr = outStr..(args['p'] or '')
		return mw.text.tag('span', {['class']='template-ping'}, outStr)
	end
end

function p.ping2(frame)
	return p.replyto(frame)
end

function p._unping(args)
	local ret = {}
	local error = ''
	local format = string.format
	local i = 1
	while true do
        username = args[i]
        if (username ~= nil) then
        	if username == '' then else
        		ret[#ret + 1] = '[[:cmn:User:' .. username .. '|' ..  (args['label' .. i] or username) .. ']]'
        	end
			if #ret >= 50 then
				error = maxuser('Template:Unping')
				break
			end
			i = i +1
		else
			break
        end
	end
	ret = mw.text.listToText(ret,'、','及')
	if ret == '' then
		return nouser('Template:Unping')
	end
	ret = (args['@'] or '') .. '-{' .. ret .. '}-' .. (args['p'] or '')
	return mw.text.tag('span', {['class']='template-unping'}, ret) .. error
end

function p.unping(frame)
	local args = frame:getParent().args
	return p._unping(args)
end

function p._hidden_ping(args)
	local ret = {}
	local error = ''
	local format = string.format
	local i = 1
	while true do
        username = args[i]
        if (username ~= nil) then
        	if username == '' then else
        		ret[#ret + 1] = '[[User:' .. username .. '|' .. (args['label' .. i] or username)  .. ']]'
        	end
			if #ret >= 50 then
				error = maxuser('Template:Noping')
				break
			end
			i = i +1
		else
			break
        end
	end
	ret = mw.text.listToText(ret)
	if ret == '' then
		return  nouser('Template:Noping')
	end
	ret = '<span style="display:none">' .. ret .. '</span>' .. error
	return ret
end

function p.hidden_ping(frame)
	local args = frame:getParent().args
	return p._hidden_ping(args)
end

return p