Module:Csdcheck
--
 -- 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) == &quot;table&quot; then
         for n,value in ipairs(test_values) do
            if s == value then
                return true
            end
         end
     else
         error(&quot;the second parameter passed to critMatch() must be a table&quot;,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 = { -- all valid CSD criteria
         &quot;G1&quot; , &quot;G2&quot; , &quot;G3&quot; , &quot;G4&quot; , &quot;G5&quot; , &quot;G6&quot; , &quot;G7&quot; , &quot;G8&quot; , &quot;G9&quot; , &quot;G10&quot; , &quot;G11&quot; , &quot;G12&quot; , &quot;G13&quot; ,
         &quot;A1&quot; , &quot;A2&quot; , &quot;A3&quot; , &quot;A5&quot; , &quot;A7&quot; , &quot;A9&quot; , &quot;A10&quot; , &quot;A11&quot;,
         &quot;F1&quot; , &quot;F2&quot; , &quot;F3&quot; , &quot;F4&quot; , &quot;F5&quot; , &quot;F6&quot; , &quot;F7&quot; , &quot;F8&quot; , &quot;F9&quot; , &quot;F10&quot; , &quot;F11&quot; ,
         &quot;C1&quot; , &quot;C2&quot; ,
         &quot;U1&quot; , &quot;U2&quot; , &quot;U3&quot; , &quot;U5&quot; ,
         &quot;R2&quot; , &quot;R3&quot; ,
         &quot;T2&quot; , &quot;T3&quot; ,
         &quot;P1&quot; , &quot;P2&quot;
     };
     local tag_criteria = { -- all CSD criteria used by [[Template:Db-multiple]]
         &quot;G1&quot; , &quot;G2&quot; , &quot;G3&quot; , &quot;G4&quot; , &quot;G5&quot; , &quot;G6&quot; , &quot;G7&quot; , &quot;G8&quot; , &quot;G10&quot; , &quot;G11&quot; , &quot;G12&quot; , &quot;G13&quot; ,
         &quot;A1&quot; , &quot;A2&quot; , &quot;A3&quot; , &quot;A5&quot; , &quot;A7&quot; , &quot;A9&quot; , &quot;A10&quot; , &quot;A11&quot;,
         &quot;F1&quot; , &quot;F2&quot; , &quot;F3&quot; , &quot;F7&quot; , &quot;F8&quot; , &quot;F9&quot; , &quot;F10&quot; ,
         &quot;C1&quot; ,
         &quot;U1&quot; , &quot;U2&quot; , &quot;U3&quot; , &quot;U5&quot; ,
         &quot;R2&quot; , &quot;R3&quot; ,
         &quot;T2&quot; ,
         &quot;P1&quot; , &quot;P2&quot;
     };
     local notice_criteria = { -- all CSD criteria used by [[Template:Db-notice-multiple]]
         &quot;G1&quot; , &quot;G2&quot; , &quot;G3&quot; , &quot;G4&quot; , &quot;G10&quot; , &quot;G11&quot; , &quot;G12&quot; , &quot;G13&quot; ,
         &quot;A1&quot; , &quot;A2&quot; , &quot;A3&quot; , &quot;A5&quot; , &quot;A7&quot; , &quot;A9&quot; , &quot;A10&quot; , &quot;A11&quot;,
         &quot;F1&quot; , &quot;F2&quot; , &quot;F3&quot; , &quot;F7&quot; , &quot;F9&quot; , &quot;F10&quot; ,
         &quot;C1&quot; ,
         &quot;U3&quot; , &quot;U5&quot; ,
         &quot;R2&quot; , &quot;R3&quot; ,
         &quot;T2&quot; ,
         &quot;P1&quot; , &quot;P2&quot;
     };
 
     -- 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) == &quot;number&quot; then
             v = mw.ustring.gsub(v,&quot;^%s*(.-)%s*$&quot;,&quot;%1&quot;); -- strip whitespace from positional parameters
             table.insert(input_values,v)
 
         -- insert critn parameter values into test_criteria
         elseif mw.ustring.match(k,&quot;^crit[1-9]%d*$&quot;) 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 &quot;set&quot; parameter
         criteria_set = test_criteria;
     elseif args[&quot;set&quot;] == &quot;tag&quot; then
         criteria_set = tag_criteria;
     elseif args[&quot;set&quot;] == &quot;notice&quot; then
         criteria_set = notice_criteria;
     else
         criteria_set = all_criteria;
     end
 
     -- check the input values against the criteria set and output &quot;yes&quot; if there is a match
     for i,v in ipairs(input_values) do
         if critMatch(v,criteria_set) then
             return &quot;yes&quot;
         end
     end
 end