Mô đun:Csdcheck

Bách khoa toàn thư mở Wikipedia
Tài liệu mô đun[tạo]
--
-- This module checks whether any of a given set of input criteria are valid CSD criteria.
-- It is also possible to specify pre-defined or custom sets of CSD criteria to check against.
--

local p = {}

function critMatch(s,test_values) -- returns true if s matches one of the table of test_values
    if type(test_values) == "table" then
        for n,value in ipairs(test_values) do
           if s == value then
               return true
           end
        end
    else
        error("Tham số thứ hai được thông qua để critMatch() phải là một bảng",2)
    end
end

function p.check(frame) -- the main CSD check function

    -- get arguments
    local args;
    if frame == mw.getCurrentFrame() then
        -- We're being called via #invoke. If the invoking template passed any args, use
        -- them. Otherwise, use the args that were passed into the template.
        args = frame:getParent().args;
        for k, v in pairs(frame.args) do
            args = frame.args;
            break
        end
    else
        -- We're being called from another module or from the debug console, so assume
        -- the args are passed in directly.
        args = frame;
    end

    -- define variables
    local input_values = {};
    local test_criteria = {};
    local all_criteria = { -- tất cả tiêu chí CSD hợp lệ
        "C1" , "C2" , "C3" , "C4" , "C5" , "C6" , "C7" , "C8" , "C9" , "C10" , "C11" , "C12" , "C13" ,
        "BV1" , "BV2" , "BV3" , "BV4" ,
        "TT1" , "TT2" , "TT3" , "TT4" , "TT5" , "TT6" , "TT7" , "TT8" , "TT9" , "TT10" , "TT11" ,
        "TL1" , "TL2" ,
        "TV1" , "TV2" , "TV3" ,
        "ĐH1" , "ĐH2" , "ĐH3" , "ĐH4" ,
        "BM1" , "BM2" , "BM3" ,
        "CTT1" , "CTT2"
    };
    local tag_criteria = { -- tất cả tiêu chí CSD được sử dụng bởi [[Bản mẫu:Db-multiple]]
        "C1" , "C2" , "C3" , "C4" , "C5" , "C6" , "C7" , "C8" , "C9" , "C10" , "C11" , "C12" , "C13" ,
        "BV1" , "BV2" , "BV3" , "BV4" ,
        "TT1" , "TT2" , "TT3" , "TT7" , "TT8" , "TT9" , "TT10" ,
        "TL1" , "TL2" ,
        "TV1" , "TV2" , "TV3" ,
        "ĐH1" , "ĐH2" , "ĐH3" , "ĐH4" ,
        "BM1" , "BM2" , "BM3" ,
        "CTT1" , "CTT2"
    };
    local notice_criteria = { -- tất cả tiêu chí CSD được sử dụng bởi [[Bản mẫu:Db-notice-multiple]]
        "C1" , "C2" , "C3" , "C4" , "C9" , "C10" , "C11" , "C12" , "C13" ,
        "BV1" , "BV2" , "BV3" , "BV4" ,
        "TT1" , "TT2" , "TT3" , "TT7" , "TT9" , "TT10" ,
        "TL1" , "TL2" ,
        "TV3" ,
        "ĐH1" , "ĐH2" , "ĐH3" , "ĐH4" ,
        "CTT1" , "CTT2"
    };

    -- build tables of input values and test criteria
    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
            if critMatch(v,all_criteria) then -- check to make sure the criteria are valid
                table.insert(test_criteria,v)
            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
    for i,v in ipairs(input_values) do
        if critMatch(v,criteria_set) then
            return "yes"
        end
    end
end

return p