Jump to content

Module:Csdcheck

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Mr. Stradivarius (talk | contribs) at 15:53, 26 March 2013 (Simplify/optimise, format the criteria tables for readability, fix the whitespace-trimming function). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

local p = {}

function p.check(frame)

    -- get arguments
    local pframe = frame:getParent();
    local args = pframe.args; -- the arguments passed to [[Template:Csdcheck]]. Arguments passed directly to #invoke: are ignored.

    -- define variables
    local input_values = {};
    local test_criteria = {};
    local all_criteria = {
        "G1" , "G2" , "G3" , "G4" , "G5" , "G6" , "G7" , "G8" , "G9" , "G10" , "G11" , "G12" ,
        "A1" , "A2" , "A3" , "A5" , "A7" , "A9" , "A10" ,
        "F1" , "F2" , "F3" , "F4" , "F5" , "F6" , "F7" , "F8" , "F9" , "F10" , "F11" ,
        "C1" , "C2" ,
        "U1" , "U2" , "U3" ,
        "R2" , "R3" ,
        "T2" , "T3" ,
        "P1" , "P2"
    };
    local tag_criteria = {
        "G1" , "G2" , "G3" , "G4" , "G5" , "G6" , "G7" , "G8" , "G10" , "G11" , "G12" ,
        "A1" , "A2" , "A3" , "A5" , "A7" , "A9" , "A10" ,
        "F1" , "F2" , "F3" , "F7" , "F8" , "F9" , "F10" ,
        "C1" ,
        "U1" , "U2" , "U3" ,
        "R2" , "R3" ,
        "T2" ,
        "P1" , "P2"
    };
    local notice_criteria = {
        "G1" , "G2" , "G3" , "G4" , "G10" , "G11" , "G12" ,
        "A1" , "A2" , "A3" , "A5" , "A7" , "A9" , "A10" ,
        "F1" , "F2" , "F3" , "F7" , "F9" , "F10" ,
        "C1" ,
        "U3" ,
        "R2" , "R3" ,
        "T2" ,
        "P1" , "P2"
    };

    -- build tables of input values and test critieria
    for k,v in pairs(args) do
        v = mw.ustring.upper(v);

        -- insert positional parameter values into input_values
        if type(k) == "number" then
            v = mw.ustring.gsub(v,"^%s*(.-)%s*$","%1"); -- strip whitespace from positional parameters
            table.insert(input_values,v)

        -- insert critn parameter values into test_criteria
        elseif mw.ustring.match(k,"^crit[1-9]%d*$") then 
            for a,b in ipairs(all_criteria) do
                if b == v then
                    table.insert(test_criteria,v)
                end
            end
        end
    end

    -- work out which set of CSD criteria to check against
    local criteria_set = {}
    if next(test_criteria) then -- if any test criteria are specified, use those regardless of the "set" parameter
        criteria_set = test_criteria;
    elseif args["set"] == "tag" then
        criteria_set = tag_criteria;
    elseif args["set"] == "notice" then
        criteria_set = notice_criteria;
    else
        criteria_set = all_criteria;
    end

    -- check the input values against the criteria set and output "yes" if there is a match
    local result = ""
    for i,v in ipairs(input_values) do
        for a,b in ipairs(criteria_set) do
            if v == b then
                result = "yes";
            end
        end
    end
    return result
end

return p