模組:Checkparams

維基詞典,自由的多語言詞典


local export = {}

local rsplit = mw.text.split
local rfind = mw.ustring.find

function get_parent_title(frame)
	return frame:getParent():getTitle()
end

function get_parent_content(frame)
	local title = get_parent_title(frame)
	return mw.title.new( title ):getContent()
end

function get_template_code(text)
    -- returns the transcludable code on a template page
    res = ""
    matched = false
    for part in string.gfind(text, "<onlyinclude>(.-)</onlyinclude>") do
        res = res .. part
        matched = true
    end
    code = matched and res or text

    code = string.gsub(code, "<noinclude>(.-)</noinclude>", "\1")
    code = string.gsub(code, "<noinclude>(.-)</noinclude>", "")

    return code
end

function clean_name(param)
    param = mw.text.trim(param)

    -- convert numeric to numbers
    if string.find(param, "^[0-9]+$") then
        return tonumber(param)
    end

    return param
end

function get_params(content)
    -- returns a table of all params used in the template text
    -- "used" params are any params referenced inside triple braces like {{{param}}}

	local MAGIC_WORD = {
		["FULLPAGENAME"] = true,
		["PAGENAME"] = true,
		["BASEPAGENAME"] = true,
		["NAMESPACE"] = true,
		["SUBPAGENAME"] = true,
		["SUBJECTSPACE"] = true,
		["TALKPAGENAME"] = true,
		["!"] = true,
	}

	local params = {}

	-- count any string between {{{ and | or } (minus MAGIC_WORDS) as a parameter name
	for param in string.gfind(content, "{{{([^=|{}<>]-)[|}]") do
        param = clean_name(param)
		if not MAGIC_WORD[param] then
			params[param] = {}
		end  
	end

	return params
end

function get_parent_params(frame)
	local parent_content = get_parent_content(frame)
    parent_content = get_template_code(parent_content)
    return get_params(parent_content)
end

function export.process(frame, allowed_params, nocat, noattn)
   -- This is desgined to be called by other Lua modules instead of calling Module:parameters.process()
   -- frame - the frame containing the arguments to be checked
   -- params - a table of valid arguments
   -- nocat - if specified, will not included category in warning_text
   -- noattn - if specified, will not include attention seeking span in in warning_text
   -- returns valid_args, invalid_args, warning_text

    local template_name = frame:getTitle()
	local valid_args, invalid_args = require("Module:parameters").process(frame.args, allowed_params, "return unknown")

	local msg = ""
	for k, v in pairs(invalid_args) do
		if msg == "" then
            msg = "Invalid params in call to " .. template_name .. ": "
        else
            msg = msg .. "; "
        end
		msg = msg .. k .. "=" .. v
	end

	if msg == "" then
        return valid_args, invalid_args, ""
    end

    -- show warning in previewer
    warn = '<sup class="error previewonly"><small>' .. msg .. '</small></sup>'

    -- add attentionseeking message
    attn = noattn and "" or '<span class="attentionseeking">' .. msg .. '</span>'

    -- categorize
    cat = nocat and "" or "[[Category:Pages using bad params when calling " .. template_name .. "]]"

    result = warn .. attn .. cat
    return valid_args, invalid_args, result

end

function export.warn(frame)
   -- This is designed to be called by non-Lua templates using "{{#invoke:checkparams|warn}}"
   -- 1= (optional) a comma separated list of allowed parameters
   -- if 1= is not specified, allows all parameters referenced by the template

	local params
	if frame.args[1] then
		params = {}
		for _, param in ipairs(rsplit(allowed_params, "%s*,%s*")) do
            param = clean_name(param)
			params[param] = {}
		end
	else
		params = get_parent_params(frame)
	end
    local nocat = frame.args["nocat"] or false
    local noattn = frame.args["noattn"] or false

    template_name = get_parent_title(frame)
    valid_args, invalid_args, invalid_args_warning = export.process(frame:getParent(), params, nocat, noattn)

    return invalid_args_warning
end

return export