跳转到内容

模組:Reply to/sandbox

维基百科,自由的百科全书

这是本页的一个历史版本,由MilkyDefer留言 | 贡献2023年7月27日 (四) 16:04编辑。这可能和当前版本存在着巨大的差异。

-- MediaWiki后台配置项$wgEchoMaxMentionsCount。这个配置项决定一笔带签名的编辑最多可以同时提及多少使用者。
-- 维基媒体旗下的wiki站点将这个值设定为50。若未来该配置出现变更,或将此模块使用在其他wiki站点,直接修改此值即可。
local WG_ECHO_MAX_MENTIONS_COUNT = 50

-- 引入[[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

--[[
	模块核心内部调用代码。输出字符串编码的提及内容,以及错误消息。

	`max`:用作重新设定默认最大提及数量的值。只允许重新设定为更小的数字,否则该参数无效。
	`template`:使用者调用该方法所使用的模版名称。用作错误消息输出。原则上调用此方法者都需要指定该值,但是该方法也提供一个绝对正确的默认值:「Template:Reply to」。
	`echo`:是否触发MediaWiki的echo机制。默认为true。
	`args`:参数列表。允许包含的内容有:
		`1`, `2`, ...:需要提及的使用者名称。
		`label1`, `label2`, ...:使用者管道链接显示的标签。
		`c`、`c2`:用作连接各个被提及的使用者的连接符号。如果只有一个使用者,没有连接符号;如果只有两个使用者,使用`c2`;如果有三个或更多使用者,最后一对使用者之间使用`c2`连接,其他使用`c`连接。默认`c`和`c2`都是「、」。
			`c2`有fallback机制。如果没有指定`c2`,但是指定了`c`,那么`c2`将会使用`c`的值。
		`@`:用作替代输出结果前缀「@」字样的文字。如不指定则为「@」。
		`p`:用作替代输出结果后缀「:」字样的文字。如不指定则为「:」。
]]
local function reply_core(args, echo, max, template)
	local ret = {}			-- 返回内容
	local error = {}		-- 捕获的错误文字

	local template = template or 'Template:Reply to'
	local echo = echo or true

	-- 设定最大提及数量的override。
	local max = max or 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) then
			-- 检查是否超出数量
			if i >= max then
				error[#error + 1] = maxuser(template, max)
				break
			end

			-- 检查用户名是否合法
			local title = mw.title.new(username)
			if not title then
				error[#error + 1] = invalidusername(username)
			else
				if echo then
					ret[#ret + 1] = '[[User:' .. username .. '|' .. (args['label' .. (i + 1)] or username)  .. ']]'
				else
					ret[#ret + 1] = '[[:zh:User:' .. username .. '|' .. (args['label' .. (i + 1)] or username)  .. ']]'
				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, (args['c'] or '、'), (args['c2'] or args['c'] or '、'))
		ret_text = (args['@'] or '@') .. '-{' .. ret_text .. '}-' .. (args['p'] or ':') 
	end

	local ret_err = mw.text.listToText(error, '', '')

	return {
		['body'] = ret_text,
		['error'] = ret_err,
	}
end

----------- 导出方法 -----------

local p = {}

--[[
	考量到下方的 `p.replyto`、`p.unping`和`p.hidden_ping` 存在几乎一样的代码块。
	特此将其提取出来,减少代码冗余,以便维护。
]]
local function get_data(frame, echo)
	local args = frame:getParent().args
	local echo = echo or true
	local max = tonumber(frame.args.max)
	local template = frame.args.template or frame:getParent():getTitle()

	return reply_core(args, echo, max, template)
end

--[[
	{{Reply to}}的主入口。
	约定:直接在页面中使用{{#invoke|Reply to|replyto|user1|user2|user3}}将不会有任何结果。
	需要将{{#invoke|Reply to|replyto}}写入模版{{Reply to}}中,然后在页面中使用{{Reply to|user1|user2}}才会生效。
]]
function p.replyto(frame)
	local data = get_data(frame, true)
	return mw.text.tag('span', {['class']='template-ping'}, data['body']) .. data['error']
end

--[[
	p.replyto的兼容用别名。以函数指针实现,因而在调用此方法时可以少一层嵌套。
]]
p.ping = p.replyto

--[[
	{{Unping}}的主入口。
]]
function p.unping(frame)
	local data = get_data(frame, false)
	return mw.text.tag('span', {['class']='template-unping'}, data['body']) .. data['error']
end

--[[
	{{Noping}}的主入口。
]]
function p.hidden_ping(frame)
	local data = get_data(frame, true)
	return mw.text.tag('span', {['style']='display:none'}, data['body']) .. data['error']
end

return p