local WG_ECHO_MAX_MENTIONS_COUNT = 50
local getArgs = require('Module:Arguments').getArgs
-- 引入[[Module:Error]]
local mod_error = require('Module:Error')
--[[
创建一个由于不当使用模组而产生的错误消息。
`msg`:错误消息的内容。
]]
local function errormsg(msg)
return mod_error.error{ [1] = '[[Module:Reply to]]錯誤:' .. msg }
end
--[[
创建一个由于使用者不当使用模版、完全不提供任何使用者名称而产生的错误消息。
`template`:使用者不当使用的模版名称。
]]
local function nouser(template)
return mod_error.error{ [1] = '使用[[' .. template .. ']]時出現錯誤:並無提供-{zh-hant:使用者名稱;zh-hans:用户名;}-。模板用法見於[[' .. template .. ']]。' }
end
--[[
创建一个由于使用者不当使用模版、提供了多于最大数量的使用者名称而产生的错误消息。
`template`:使用者不当使用的模版名称。
`count`:模版最多允许加入的使用者个数。
]]
local function maxuser(template, count)
return mod_error.error{ [1] = '[[' .. template .. ']]最多-{zh-hans:支持;zh-hant:支援}-提及' .. (count or WG_ECHO_MAX_MENTIONS_COUNT) .. '个-{zh-hant:使用者;zh-hans:用户;}-。如果需要提及超过' .. (count or WG_ECHO_MAX_MENTIONS_COUNT) .. '个-{zh-hant:使用者;zh-hans:用户;}-,必须多次留言。模板用法見於[[' .. template .. ']]。' }
end
--[[
创建一个由于使用者不当使用模版、试图以更大数值绕过设定的最大提及数量而产生的错误消息。
`template`:使用者不当使用的模版名称。
`max`:试图设定的最大值。
]]
local function invalidmaxoverride(template, max)
return mod_error.error{ [1] = '试图让[[' .. template .. ']]允许一次性提及' .. max .. '位-{zh-hant:使用者;zh-hans:用户;}-。MediaWiki限制一次性最多只能提及' .. WG_ECHO_MAX_MENTIONS_COUNT .. '位-{zh-hant:使用者;zh-hans:用户;}-。' }
end
--[[
创建一个由于使用者不当使用模版、试图提及非法使用者名称而产生的错误消息。
`username`:非法的使用者名称。
]]
local function invalidusername(username)
return mod_error.error{ [1] = '试图提及的-{zh-hant:使用者名稱;zh-hans:用户名;}-「' .. username ..'」在技术上不合法。' }
end
--[[
模块核心内部调用代码。输出字符串编码的提及内容,以及错误消息。
`echo`:是否触发MediaWiki的echo机制。默认为true。
`args`:参数列表。允许包含的内容有:
`1`, `2`, ...:需要提及的使用者名称。只应当从模版参数间接获取。
]]
local function reply_core(args, echo)
local ret = {} -- 返回内容
local error = {} -- 捕获的错误文字
local template = args.template or 'Template:Reply to'
if echo == nil then echo = true end
-- 设定最大提及数量的override。
local max = WG_ECHO_MAX_MENTIONS_COUNT
if max > WG_ECHO_MAX_MENTIONS_COUNT then
error[#error + 1] = invalidmaxoverride(template, max)
max = WG_ECHO_MAX_MENTIONS_COUNT
end
-- 生成每一位使用者的wikilink,以table/array方式存储,暂存至ret当中。
local i = 0
while true do
username = args[i + 1] -- Lua的数组下标从1开始
if (username ~= nil and username ~= '') then
-- 检查是否超出数量
if i >= max then
error[#error + 1] = maxuser(template, max)
break
end
-- 检查用户名是否合法
local success, title = pcall(mw.title.new, username)
-- 註:因為username不該包含命名空間,因此經過解析後必須在條目命名空間
if not success or not title or title.namespace ~= 0 then
error[#error + 1] = invalidusername(username)
else
if echo then
ret[#ret + 1] = '-{[[User:' .. username ..'|' .. username .. ']]}-'
else
local url = mw.uri.fullUrl(mw.site.namespaces.User.name .. ':' .. username)
url = tostring(url)
url = string.format('-{[%s %s]}-', url, username)
ret[#ret + 1] = url
end
end
i = i + 1
else
break
end
end
local ret_text = ''
if #ret <= 0 then
if i <= 0 then
-- 完全没有提供使用者名称。抛出错误消息。
error[#error + 1] = nouser(template)
else
-- 有提及使用者,但是没有一个使用者的名称是合法的。由于不合法使用者名称已经抛出了消息,所以这里不抛出错误消息。
end
else
ret_text = mw.text.listToText(ret, '、', '、')
ret_text = '@' .. ret_text
end
local ret_err = mw.text.listToText(error, '', '')
return {
['body'] = ret_text,
['error'] = ret_err,
}
end
----------- 导出方法 -----------
local p = {}
--[[
{{Reply to}}的主入口。
]]
function p.replyto(frame)
local args = getArgs(frame)
args['template'] = args['template'] or (frame:getParent() and frame:getParent():getTitle()) or 'Template:Reply to'
local data = reply_core(args, true)
return mw.text.tag('span', {['class']='template-ping'}, data['body']) .. data['error']
end
--[[
{{Mute}}的主入口。
]]
function p.hidden_ping(frame)
local args = getArgs(frame)
args['template'] = args['template'] or (frame:getParent() and frame:getParent():getTitle()) or 'Template:Mute'
local data = reply_core(args, true)
return mw.text.tag('span', {['style']='display:none'}, data['body']) .. data['error']
end
return p