模組:VandalSubmitForm

维基百科,自由的百科全书
文档图示 模块文档[创建]
local p = {}
local yesno = require('Module:Yesno')

local function mightGetTitle(pageName, overrideNS)
	if not pageName then
		return ''
	end
	pageName = mw.ustring.gsub(pageName, '^%[%[(.*)%]%]$', '%1')
	pageName = mw.ustring.gsub(pageName, '‎', '') -- 不可見字元
	local success, title = pcall(mw.title.new, mw.text.trim(pageName), overrideNS)
	if success then
		return title
	end
	return nil
end

function p._cleanUserName(userName)
	local title = mightGetTitle(userName, 2)
	if title then
		return title.rootText
	end
	return nil
end

function p._cleanPageName(pageName)
	local title = mightGetTitle(pageName)
	if title then
		return title.prefixedText
	end
	return nil
end

local form = mw.text.trim([=[{{safesubst:/form
| 用户名 = 
<!-- 若要提報多個用戶,請分多次提交,切勿複製本表單 -->
| 受影响的条目1 = 
<!-- 请勿在此栏填写“见贡献”、“很多”之类。这些请写在说明处 -->
| 说明 = 
}}]=])

function p._makeReportContent(args)
	local userName = p._cleanUserName(args['用户名'])
	if not userName then -- 輸入錯了也當作沒有要提報
		return ''
	end
	local output = mw.ustring.format('=== %s ===\n* \'\'\'{{vandal|1=%s}}\'\'\'', userName, userName)
	-- 遞迴獲取受影響的條目
	local i = 1
	while true do
		local pageName = p._cleanPageName(args['受影响的条目' .. i])
		if pageName then
			output = output .. mw.ustring.format('\n* {{pagelinks|1=%s}}', pageName)
		else
			break
		end
		i = i + 1
	end
	if args['说明'] and mw.text.trim(args['说明']) ~= '' then
		output = output .. mw.ustring.format('\n* %s', mw.text.trim(args['说明']))
	end
	output = output .. '\n* 发现人:~~~~\n* 处理:<!-- 非管理員僅可標記已執行的封禁,針對提報的意見請放在下一行 -->'
	return output
end

function p.main(frame)
	local pFrame = frame:getParent()
	if not pFrame then
		return ''
	end
	local tArgs = frame.args
	local pArgs = pFrame.args
	if mw.isSubsting() then
		return mw.text.trim((yesno(pArgs.norebuild) and '' or form .. '\n') .. p._makeReportContent(pArgs))
	else
		return frame:expandTemplate{ title = tArgs.root .. '/header' }
	end
end

return p