<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://www.limswiki.org/index.php?action=history&amp;feed=atom&amp;title=Module%3ACategory_handler</id>
	<title>Module:Category handler - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://www.limswiki.org/index.php?action=history&amp;feed=atom&amp;title=Module%3ACategory_handler"/>
	<link rel="alternate" type="text/html" href="https://www.limswiki.org/index.php?title=Module:Category_handler&amp;action=history"/>
	<updated>2026-04-04T09:14:10Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.36.1</generator>
	<entry>
		<id>https://www.limswiki.org/index.php?title=Module:Category_handler&amp;diff=16545&amp;oldid=prev</id>
		<title>Shawndouglas: Updated code</title>
		<link rel="alternate" type="text/html" href="https://www.limswiki.org/index.php?title=Module:Category_handler&amp;diff=16545&amp;oldid=prev"/>
		<updated>2014-11-22T16:15:27Z</updated>

		<summary type="html">&lt;p&gt;Updated code&lt;/p&gt;
&lt;a href=&quot;https://www.limswiki.org/index.php?title=Module:Category_handler&amp;amp;diff=16545&amp;amp;oldid=13162&quot;&gt;Show changes&lt;/a&gt;</summary>
		<author><name>Shawndouglas</name></author>
	</entry>
	<entry>
		<id>https://www.limswiki.org/index.php?title=Module:Category_handler&amp;diff=13162&amp;oldid=prev</id>
		<title>Shawndouglas: Created as needed.</title>
		<link rel="alternate" type="text/html" href="https://www.limswiki.org/index.php?title=Module:Category_handler&amp;diff=13162&amp;oldid=prev"/>
		<updated>2013-10-30T23:44:25Z</updated>

		<summary type="html">&lt;p&gt;Created as needed.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;----------------------------------------------------------------------------------------------------------&lt;br /&gt;
--                                                                                                      --&lt;br /&gt;
--                                          CATEGORY HANDLER                                            --&lt;br /&gt;
--                                                                                                      --&lt;br /&gt;
--      This module implements the {{category handler}} template in Lua, with a few improvements: all   --&lt;br /&gt;
--      namespaces and all namespace aliases are supported, and namespace names are detected            --&lt;br /&gt;
--      automatically for the local wiki. This module requires [[Module:Namespace detect]] and          --&lt;br /&gt;
--      [[Module:Yesno]] to be available on the local wiki. It can be configured for different wikis    --&lt;br /&gt;
--      by altering the values in the &amp;quot;cfg&amp;quot; table.                                                      --&lt;br /&gt;
--                                                                                                      --&lt;br /&gt;
----------------------------------------------------------------------------------------------------------&lt;br /&gt;
&lt;br /&gt;
----------------------------------------------------------------------------------------------------------&lt;br /&gt;
--                                          Configuration data                                          --&lt;br /&gt;
--                      Language-specific parameter names and values can be set here.                   --&lt;br /&gt;
----------------------------------------------------------------------------------------------------------&lt;br /&gt;
&lt;br /&gt;
local cfg = {}&lt;br /&gt;
&lt;br /&gt;
-- The following config values set the names of parameters that suppress categorisation. They are used&lt;br /&gt;
-- with Module:Yesno, and work as follows:&lt;br /&gt;
--&lt;br /&gt;
-- cfg.nocat:&lt;br /&gt;
-- Result of yesno(args[cfg.nocat])         Effect&lt;br /&gt;
-- true                                     Categorisation is suppressed&lt;br /&gt;
-- false                                    Categorisation is allowed, and the blacklist check is skipped&lt;br /&gt;
-- nil                                      Categorisation is allowed&lt;br /&gt;
--&lt;br /&gt;
-- cfg.categories:&lt;br /&gt;
-- Result of yesno(args[cfg.categories])    Effect&lt;br /&gt;
-- true                                     Categorisation is allowed, and the blacklist check is skipped&lt;br /&gt;
-- false                                    Categorisation is suppressed&lt;br /&gt;
-- nil                                      Categorisation is allowed&lt;br /&gt;
cfg.nocat = 'nocat'    &lt;br /&gt;
cfg.categories = 'categories'&lt;br /&gt;
&lt;br /&gt;
-- The parameter name for the legacy &amp;quot;category2&amp;quot; parameter. This skips the blacklist if set to the&lt;br /&gt;
-- cfg.category2Yes value, and suppresses categorisation if present but equal to anything other than&lt;br /&gt;
-- cfg.category2Yes or cfg.category2Negative.&lt;br /&gt;
cfg.category2 = 'category2'&lt;br /&gt;
cfg.category2Yes = 'yes'&lt;br /&gt;
cfg.category2Negative = '¬'&lt;br /&gt;
&lt;br /&gt;
-- cfg.subpage is the parameter name to specify how to behave on subpages. cfg.subpageNo is the value to&lt;br /&gt;
-- specify to not categorise on subpages; cfg.only is the value to specify to only categorise on subpages.&lt;br /&gt;
cfg.subpage = 'subpage'&lt;br /&gt;
cfg.subpageNo = 'no'&lt;br /&gt;
cfg.subpageOnly = 'only'&lt;br /&gt;
&lt;br /&gt;
-- The parameter for data to return in all namespaces.&lt;br /&gt;
cfg.all = 'all'&lt;br /&gt;
&lt;br /&gt;
-- The parameter name for data to return if no data is specified for the namespace that is detected. This&lt;br /&gt;
-- must be the same as the cfg.other parameter in [[Module:Namespace detect]].&lt;br /&gt;
cfg.other = 'other'&lt;br /&gt;
&lt;br /&gt;
-- The parameter name used to specify a page other than the current page; used for testing and&lt;br /&gt;
-- demonstration. This must be the same as the cfg.page parameter in [[Module:Namespace detect]].&lt;br /&gt;
cfg.page = 'page'&lt;br /&gt;
&lt;br /&gt;
-- The categorisation blacklist. Pages that match Lua patterns in this list will not be categorised.&lt;br /&gt;
-- (However, see the explanation of cfg.nocat, cfg.categories and cfg.category2 for some exceptions.)&lt;br /&gt;
-- If the namespace name has a space in, it must be written with an underscore, e.g. &amp;quot;Wikipedia_talk&amp;quot;.&lt;br /&gt;
-- Other parts of the title can have either underscores or spaces.&lt;br /&gt;
cfg.blacklist = {&lt;br /&gt;
    '^Main Page$', -- don't categorise the main page.&lt;br /&gt;
    &lt;br /&gt;
    -- Don't categorise the following pages or their subpages.&lt;br /&gt;
    '^Wikipedia:Cascade%-protected items$',&lt;br /&gt;
    '^Wikipedia:Cascade%-protected items/.*$',&lt;br /&gt;
    '^User:UBX$', -- The userbox &amp;quot;template&amp;quot; space.&lt;br /&gt;
    '^User:UBX/.*$',&lt;br /&gt;
    '^User_talk:UBX$',&lt;br /&gt;
    '^User_talk:UBX/.*$',&lt;br /&gt;
    &lt;br /&gt;
    -- Don't categorise subpages of these pages, but allow&lt;br /&gt;
    -- categorisation of the base page.&lt;br /&gt;
    '^Wikipedia:Template messages/.+$',&lt;br /&gt;
    &lt;br /&gt;
    '/[aA]rchive' -- Don't categorise archives.&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
-- This is a table of namespaces to categorise by default. They should be in the format of parameter&lt;br /&gt;
-- names accepted by [[Module:Namespace detect]].&lt;br /&gt;
cfg.defaultNamespaces = {&lt;br /&gt;
    'main',&lt;br /&gt;
    'file',&lt;br /&gt;
    'help',&lt;br /&gt;
    'category'&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
----------------------------------------------------------------------------------------------------------&lt;br /&gt;
--                                          End configuration data                                      --&lt;br /&gt;
----------------------------------------------------------------------------------------------------------&lt;br /&gt;
&lt;br /&gt;
-- Get dependent modules&lt;br /&gt;
local nsDetect = require('Module:Namespace detect')&lt;br /&gt;
local yesno = require('Module:Yesno')&lt;br /&gt;
&lt;br /&gt;
----------------------------------------------------------------------------------------------------------&lt;br /&gt;
--                                          Local functions                                             --&lt;br /&gt;
--      The following are internal functions, which we do not want to be accessible from other modules. --&lt;br /&gt;
----------------------------------------------------------------------------------------------------------&lt;br /&gt;
&lt;br /&gt;
-- Find whether we need to return a category or not.&lt;br /&gt;
local function needsCategory(pageObject, args)&lt;br /&gt;
    -- Don't categorise if the relevant options are set.&lt;br /&gt;
    if yesno(args[cfg.nocat])&lt;br /&gt;
        or yesno(args[cfg.categories]) == false&lt;br /&gt;
        or (&lt;br /&gt;
            args[cfg.category2] &lt;br /&gt;
            and args[cfg.category2] ~= cfg.category2Yes &lt;br /&gt;
            and args[cfg.category2] ~= cfg.category2Negative&lt;br /&gt;
        )&lt;br /&gt;
    then&lt;br /&gt;
        return false&lt;br /&gt;
    end&lt;br /&gt;
    -- If there is no pageObject available, then that either means that we are over&lt;br /&gt;
    -- the expensive function limit or that the title specified was invalid. Invalid&lt;br /&gt;
    -- titles will probably only be a problem during testing, so we choose the best&lt;br /&gt;
    -- fallback for being over the expensive function limit. The fallback behaviour&lt;br /&gt;
    -- of the old template was to assume the page was not a subpage, so we will do&lt;br /&gt;
    -- the same here.&lt;br /&gt;
    if args[cfg.subpage] == cfg.subpageNo and pageObject and pageObject.isSubpage then&lt;br /&gt;
        return false&lt;br /&gt;
    end&lt;br /&gt;
    if args[cfg.subpage] == cfg.subpageOnly &lt;br /&gt;
        and (not pageObject or (pageObject and not pageObject.isSubpage))&lt;br /&gt;
    then&lt;br /&gt;
        return false&lt;br /&gt;
    end&lt;br /&gt;
    return true&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- Find whether we need to check the blacklist or not.&lt;br /&gt;
local function needsBlacklistCheck(args)&lt;br /&gt;
    if yesno(args[cfg.nocat]) == false&lt;br /&gt;
        or yesno(args[cfg.categories]) == true&lt;br /&gt;
        or args[cfg.category2] == cfg.category2Yes&lt;br /&gt;
    then&lt;br /&gt;
        return false&lt;br /&gt;
    else&lt;br /&gt;
        return true&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- Find whether any namespace parameters have been specified.&lt;br /&gt;
-- Mappings is the table of parameter mappings taken from&lt;br /&gt;
-- [[Module:Namespace detect]].&lt;br /&gt;
local function nsParamsExist(mappings, args)&lt;br /&gt;
    if args[cfg.all] or args[cfg.other] then&lt;br /&gt;
        return true&lt;br /&gt;
    end&lt;br /&gt;
    for ns, params in pairs(mappings) do&lt;br /&gt;
        for i, param in ipairs(params) do&lt;br /&gt;
            if args[param] then&lt;br /&gt;
                return true&lt;br /&gt;
            end&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
    return false&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
----------------------------------------------------------------------------------------------------------&lt;br /&gt;
--                                          Global functions                                            --&lt;br /&gt;
--      The following functions are global, because we want them to be accessible from #invoke and      --&lt;br /&gt;
--      from other Lua modules.                                                                         --&lt;br /&gt;
----------------------------------------------------------------------------------------------------------&lt;br /&gt;
&lt;br /&gt;
local p = {}&lt;br /&gt;
&lt;br /&gt;
-- Find if a string matches the blacklist. Returns the match if one is found, or nil otherwise.&lt;br /&gt;
-- Input should be a page title with a namespace prefix, e.g. &amp;quot;Wikipedia talk:Articles for deletion&amp;quot;.&lt;br /&gt;
function p.matchesBlacklist(page)&lt;br /&gt;
    if type(page) ~= 'string' then return end&lt;br /&gt;
    for i, pattern in ipairs(cfg.blacklist) do&lt;br /&gt;
        local match = mw.ustring.match(page, pattern)&lt;br /&gt;
        if match then&lt;br /&gt;
            return match&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- The main structure of the module. Checks whether we need to categorise,&lt;br /&gt;
-- and then passes the relevant arguments to [[Module:Namespace detect]].&lt;br /&gt;
function p._main(args)&lt;br /&gt;
    -- Get the page object and argument mappings from&lt;br /&gt;
    -- [[Module:Namespace detect]], to save us from having to rewrite the&lt;br /&gt;
    -- code.&lt;br /&gt;
    local pageObject = nsDetect.getPageObject(args[cfg.page])&lt;br /&gt;
    local mappings = nsDetect.getParamMappings()&lt;br /&gt;
    &lt;br /&gt;
    if not needsCategory(pageObject, args) then return end&lt;br /&gt;
    &lt;br /&gt;
    local ret = ''&lt;br /&gt;
    -- Check blacklist if necessary.&lt;br /&gt;
    if not needsBlacklistCheck(args) or not p.matchesBlacklist(pageObject.prefixedText) then&lt;br /&gt;
        if not nsParamsExist(mappings, args) then&lt;br /&gt;
            -- No namespace parameters exist; basic usage. Pass args[1] to&lt;br /&gt;
            -- [[Module:Namespace detect]] using the default namespace&lt;br /&gt;
            -- parameters, and return the result.&lt;br /&gt;
            local ndargs = {}&lt;br /&gt;
            for _, ndarg in ipairs(cfg.defaultNamespaces) do&lt;br /&gt;
                ndargs[ndarg] = args[1]&lt;br /&gt;
            end&lt;br /&gt;
            ndargs.page = args.page&lt;br /&gt;
            ndargs.demospace = args.demospace&lt;br /&gt;
            local ndresult = nsDetect._main(ndargs)&lt;br /&gt;
            if ndresult then&lt;br /&gt;
                ret = ret .. ndresult&lt;br /&gt;
            end&lt;br /&gt;
        else&lt;br /&gt;
            -- Namespace parameters exist; advanced usage.&lt;br /&gt;
            -- If the all parameter is specified, return it.&lt;br /&gt;
            local all = args.all&lt;br /&gt;
            if type(all) == 'string' then&lt;br /&gt;
                ret = ret .. all&lt;br /&gt;
            end&lt;br /&gt;
            &lt;br /&gt;
            -- Get the arguments to pass to [[Module:Namespace detect]].&lt;br /&gt;
            local ndargs = {}&lt;br /&gt;
            for ns, params in pairs(mappings) do&lt;br /&gt;
                for _, param in ipairs(params) do&lt;br /&gt;
                    ndargs[param] = args[param] or args[cfg.other] or nil&lt;br /&gt;
                end&lt;br /&gt;
            end&lt;br /&gt;
            ndargs.other = args.other&lt;br /&gt;
            ndargs.page = args.page&lt;br /&gt;
            ndargs.demospace = args.demospace&lt;br /&gt;
            &lt;br /&gt;
            local data = nsDetect._main(ndargs)&lt;br /&gt;
            &lt;br /&gt;
            -- Work out what to return based on the result of the namespace detect call.&lt;br /&gt;
            local datanum = tonumber(data)&lt;br /&gt;
            if type(datanum) == 'number' then&lt;br /&gt;
                -- &amp;quot;data&amp;quot; is a number, so return that positional parameter.&lt;br /&gt;
                -- Remove non-positive integer values, as only positive integers&lt;br /&gt;
                -- from 1-10 were used with the old template.&lt;br /&gt;
                if datanum &amp;gt; 0 and math.floor(datanum) == datanum then&lt;br /&gt;
                    local dataArg = args[datanum]&lt;br /&gt;
                    if type(dataArg) == 'string' then&lt;br /&gt;
                        ret = ret .. dataArg&lt;br /&gt;
                    end&lt;br /&gt;
                end&lt;br /&gt;
            else&lt;br /&gt;
                -- &amp;quot;data&amp;quot; is not a number, so return it as it is.&lt;br /&gt;
                if type(data) == 'string' then&lt;br /&gt;
                    ret = ret .. data&lt;br /&gt;
                end&lt;br /&gt;
            end&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
    return ret&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function p.main(frame)&lt;br /&gt;
    -- If called via #invoke, use the args passed into the invoking&lt;br /&gt;
    -- template, or the args passed to #invoke if any exist. Otherwise&lt;br /&gt;
    -- assume args are being passed directly in.&lt;br /&gt;
    local origArgs&lt;br /&gt;
    if frame == mw.getCurrentFrame() then&lt;br /&gt;
        origArgs = frame:getParent().args&lt;br /&gt;
        for k, v in pairs(frame.args) do&lt;br /&gt;
            origArgs = frame.args&lt;br /&gt;
            break&lt;br /&gt;
        end&lt;br /&gt;
    else&lt;br /&gt;
        origArgs = frame&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    -- Trim whitespace and remove blank arguments for the following args:&lt;br /&gt;
    -- 1, 2, 3 etc., &amp;quot;nocat&amp;quot;, &amp;quot;categories&amp;quot;, &amp;quot;subpage&amp;quot;, and &amp;quot;page&amp;quot;.&lt;br /&gt;
    local args = {}&lt;br /&gt;
    for k, v in pairs(origArgs) do&lt;br /&gt;
        if type(v) == 'string' then&lt;br /&gt;
            v = mw.text.trim(v) -- Trim whitespace.&lt;br /&gt;
        end&lt;br /&gt;
        if type(k) == 'number'&lt;br /&gt;
            or k == cfg.nocat&lt;br /&gt;
            or k == cfg.categories&lt;br /&gt;
            or k == cfg.subpage&lt;br /&gt;
            or k == cfg.page&lt;br /&gt;
        then&lt;br /&gt;
            if v ~= '' then&lt;br /&gt;
                args[k] = v&lt;br /&gt;
            end&lt;br /&gt;
        else&lt;br /&gt;
            args[k] = v&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    -- Lower-case &amp;quot;nocat&amp;quot;, &amp;quot;categories&amp;quot;, &amp;quot;category2&amp;quot;, and &amp;quot;subpage&amp;quot;. These&lt;br /&gt;
    -- parameters are put in lower case whenever they appear in the old&lt;br /&gt;
    -- template, so we can just do it once here and save ourselves some work.&lt;br /&gt;
    local lowercase = {cfg.nocat, cfg.categories, cfg.category2, cfg.subpage}&lt;br /&gt;
    for _, v in ipairs(lowercase) do&lt;br /&gt;
        local argVal = args[v]&lt;br /&gt;
        if type(argVal) == 'string' then&lt;br /&gt;
            args[v] = mw.ustring.lower(argVal)&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    return p._main(args)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
return p&lt;/div&gt;</summary>
		<author><name>Shawndouglas</name></author>
	</entry>
</feed>