<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.expertiza.ncsu.edu/index.php?action=history&amp;feed=atom&amp;title=Module%3ANamespace_detect</id>
	<title>Module:Namespace detect - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.expertiza.ncsu.edu/index.php?action=history&amp;feed=atom&amp;title=Module%3ANamespace_detect"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Module:Namespace_detect&amp;action=history"/>
	<updated>2026-05-10T09:55:41Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Module:Namespace_detect&amp;diff=78393&amp;oldid=prev</id>
		<title>Admin: 1 revision</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Module:Namespace_detect&amp;diff=78393&amp;oldid=prev"/>
		<updated>2013-09-19T03:34:40Z</updated>

		<summary type="html">&lt;p&gt;1 revision&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;
--                        NAMESPACE DETECT                          --&lt;br /&gt;
--                                                                  --&lt;br /&gt;
--      This module implements the {{namespace detect}} template    --&lt;br /&gt;
--      in Lua, with a few improvements: all namespaces and all     --&lt;br /&gt;
--      namespace aliases are supported, and namespace names are    --&lt;br /&gt;
--      detected automatically for the local wiki. Function names   --&lt;br /&gt;
--      can be configured for different wikis by altering the       --&lt;br /&gt;
--      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 can be set here.          --&lt;br /&gt;
----------------------------------------------------------------------&lt;br /&gt;
&lt;br /&gt;
local cfg = {}&lt;br /&gt;
&lt;br /&gt;
-- The name for the parameter to display content for the main namespace:&lt;br /&gt;
cfg.main = 'main'&lt;br /&gt;
&lt;br /&gt;
-- The name for the parameter to display content in talk namespaces:&lt;br /&gt;
cfg.talk = 'talk'&lt;br /&gt;
&lt;br /&gt;
-- The name for the parameter to display content for &amp;quot;other&amp;quot; namespaces&lt;br /&gt;
-- (namespaces for which parameters have not been specified, or for when&lt;br /&gt;
-- cfg.demospace is set to cfg.other):&lt;br /&gt;
cfg.other = 'other'&lt;br /&gt;
&lt;br /&gt;
-- The name for the parameter to set a demonstration namespace:&lt;br /&gt;
cfg.demospace = 'demospace'&lt;br /&gt;
&lt;br /&gt;
-- The name for the parameter to set a specific page to compare:&lt;br /&gt;
cfg.page = 'page'&lt;br /&gt;
&lt;br /&gt;
-- The header for the namespace column in the wikitable containing the &lt;br /&gt;
-- list of possible subject-space parameters.&lt;br /&gt;
cfg.wikitableNamespaceHeader = 'Namespace'&lt;br /&gt;
&lt;br /&gt;
-- The header for the wikitable containing the list of possible&lt;br /&gt;
-- subject-space parameters.&lt;br /&gt;
cfg.wikitableAliasesHeader = 'Aliases'&lt;br /&gt;
&lt;br /&gt;
----------------------------------------------------------------------&lt;br /&gt;
--                     End configuration data                       --&lt;br /&gt;
----------------------------------------------------------------------&lt;br /&gt;
&lt;br /&gt;
----------------------------------------------------------------------&lt;br /&gt;
--                        Global functions                          --&lt;br /&gt;
--      The following functions are global, because we want them    --&lt;br /&gt;
--      to be accessible from other Lua modules called using        --&lt;br /&gt;
--      require().                                                  --&lt;br /&gt;
----------------------------------------------------------------------&lt;br /&gt;
&lt;br /&gt;
-- Declare the table of functions to return.&lt;br /&gt;
local p = {}&lt;br /&gt;
&lt;br /&gt;
-- Get the page object. This will return the page object for the page&lt;br /&gt;
-- specified, or nil if there are errors in the title or if the&lt;br /&gt;
-- expensive function count has been exceeded.&lt;br /&gt;
function p.getPageObject( page )&lt;br /&gt;
    -- Get the title object for args.page if it is specified. Otherwise&lt;br /&gt;
    -- get the title object for the current page.&lt;br /&gt;
    if page then&lt;br /&gt;
        -- Get the page object, passing the function through pcall &lt;br /&gt;
        -- in case we are over the expensive function count limit.&lt;br /&gt;
        local noError, pageObject = pcall(mw.title.new, page)&lt;br /&gt;
        if not noError then&lt;br /&gt;
            return nil&lt;br /&gt;
        else&lt;br /&gt;
            return pageObject&lt;br /&gt;
        end&lt;br /&gt;
    else&lt;br /&gt;
        return mw.title.getCurrentTitle()&lt;br /&gt;
    end    &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
--[[ Returns a table of how parameter names map to namespace names.&lt;br /&gt;
The keys are the actual namespace names, in lower case, and the &lt;br /&gt;
values are the possible parameter names for that namespace, also&lt;br /&gt;
in lower case. The table entries are structured like this:&lt;br /&gt;
    [''] = {&lt;br /&gt;
        {'main'},&lt;br /&gt;
    },&lt;br /&gt;
    ['wikipedia'] = {&lt;br /&gt;
        {'wikipedia', 'project', 'wp' }&lt;br /&gt;
    }&lt;br /&gt;
]] &lt;br /&gt;
function p.getParamMappings()&lt;br /&gt;
    local mappings = {}&lt;br /&gt;
    mappings[mw.ustring.lower( mw.site.namespaces[0].name )] = { cfg.main }&lt;br /&gt;
    mappings[cfg.talk] = { cfg.talk }&lt;br /&gt;
    for nsid, ns in pairs( mw.site.subjectNamespaces ) do&lt;br /&gt;
        if nsid ~= 0 then -- Exclude main namespace.&lt;br /&gt;
            local nsname = mw.ustring.lower( ns.name )&lt;br /&gt;
            local canonicalName = mw.ustring.lower( ns.canonicalName )&lt;br /&gt;
            mappings[nsname] = { nsname }&lt;br /&gt;
            if canonicalName ~= nsname then&lt;br /&gt;
                table.insert( mappings[nsname], canonicalName )&lt;br /&gt;
            end&lt;br /&gt;
            for _, alias in ipairs( ns.aliases ) do&lt;br /&gt;
                table.insert( mappings[nsname], mw.ustring.lower( alias ) )&lt;br /&gt;
            end&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
    return mappings&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
--[[ Create a wikitable of all subject namespace parameters, for documentation&lt;br /&gt;
  purposes. Talk is excluded, as it should usually be treated separately in&lt;br /&gt;
  the documentation.&lt;br /&gt;
]]&lt;br /&gt;
function p.table()&lt;br /&gt;
    -- Get the parameter mappings.&lt;br /&gt;
    local mappings = p.getParamMappings()&lt;br /&gt;
    &lt;br /&gt;
    -- Start the wikitable.&lt;br /&gt;
    local ret = '{| class=&amp;quot;wikitable&amp;quot;'&lt;br /&gt;
        .. '\n|-'&lt;br /&gt;
        .. '\n! ' .. cfg.wikitableNamespaceHeader&lt;br /&gt;
        .. '\n! ' .. cfg.wikitableAliasesHeader&lt;br /&gt;
    &lt;br /&gt;
    -- Generate the row for the main namespace, as we want this&lt;br /&gt;
    -- to be first in the list.&lt;br /&gt;
    ret = ret .. '\n|-'&lt;br /&gt;
        .. '\n| &amp;lt;code&amp;gt;' .. cfg.main .. '&amp;lt;/code&amp;gt;'&lt;br /&gt;
        .. '\n|'&lt;br /&gt;
        &lt;br /&gt;
    -- Enclose all parameter names in &amp;lt;code&amp;gt; tags.&lt;br /&gt;
    for ns, params in pairs( mappings ) do&lt;br /&gt;
        if ns ~= mw.site.namespaces[0].name then&lt;br /&gt;
            for i, param in ipairs( params ) do&lt;br /&gt;
                mappings[ns][i] = '&amp;lt;code&amp;gt;' .. param .. '&amp;lt;/code&amp;gt;'&lt;br /&gt;
            end&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    -- Generate the other wikitable rows.&lt;br /&gt;
    for ns, params in pairs( mappings ) do&lt;br /&gt;
        if ns ~= mw.site.namespaces[0].name then -- Ignore the main namespace.&lt;br /&gt;
            ret = ret .. '\n|-'&lt;br /&gt;
                .. '\n| ' .. params[1]&lt;br /&gt;
                .. '\n| ' .. table.concat( params, ', ', 2 )&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    -- End the wikitable.&lt;br /&gt;
    ret = ret .. '\n|-'&lt;br /&gt;
        .. '\n|}'&lt;br /&gt;
    &lt;br /&gt;
    return ret&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
----------------------------------------------------------------------&lt;br /&gt;
--                         Local functions                          --&lt;br /&gt;
--      The following are internal functions, which we do not want  --&lt;br /&gt;
--      to be accessible from other modules.                        --&lt;br /&gt;
----------------------------------------------------------------------&lt;br /&gt;
&lt;br /&gt;
-- Gets the namespace name to compare to the arguments. The returned value&lt;br /&gt;
-- is lower-case.&lt;br /&gt;
local function getNamespace( page, demospace )&lt;br /&gt;
    local ret&lt;br /&gt;
    if demospace then&lt;br /&gt;
        -- Handle &amp;quot;demospace = main&amp;quot; properly.&lt;br /&gt;
        if mw.ustring.lower( demospace ) == cfg.main then&lt;br /&gt;
            ret = mw.site.namespaces[0].name&lt;br /&gt;
        else&lt;br /&gt;
            ret = demospace&lt;br /&gt;
        end&lt;br /&gt;
    else&lt;br /&gt;
        local pageObject = p.getPageObject( page )&lt;br /&gt;
        if pageObject then&lt;br /&gt;
            if pageObject.isTalkPage then&lt;br /&gt;
                -- {{namespace detect}} uses the same value for all talk&lt;br /&gt;
                -- namespaces, so that's what the module should do too.&lt;br /&gt;
                ret = cfg.talk&lt;br /&gt;
            else&lt;br /&gt;
                ret = pageObject.nsText&lt;br /&gt;
            end&lt;br /&gt;
        else&lt;br /&gt;
            return nil -- return nil if the page object doesn't exist.&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
    return mw.ustring.lower(ret)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- Compare the namespace found with the parameters that have been&lt;br /&gt;
-- specified, and return content of the appropriate parameter.&lt;br /&gt;
local function compare( args )&lt;br /&gt;
    -- Get the namespace to compare the parameters to, and the parameter&lt;br /&gt;
    -- mapping table.&lt;br /&gt;
    local namespace = getNamespace( args[cfg.page], args[cfg.demospace] )&lt;br /&gt;
    local mappings = p.getParamMappings()&lt;br /&gt;
    &lt;br /&gt;
    -- Check for any matches in the namespace arguments. The order we check&lt;br /&gt;
    -- them doesn't matter, as there can only be one match.&lt;br /&gt;
    for ns, params in pairs( mappings ) do&lt;br /&gt;
        if ns == namespace then&lt;br /&gt;
            -- Check all aliases for matches. The default local namespace is&lt;br /&gt;
            -- checked first, as {{namespace detect}} checked these before&lt;br /&gt;
            -- alias names.&lt;br /&gt;
            for _, param in ipairs( params ) do&lt;br /&gt;
                if args[param] then&lt;br /&gt;
                    return args[param]&lt;br /&gt;
                end&lt;br /&gt;
            end&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    -- If there were no matches, return parameters for other namespaces. &lt;br /&gt;
    -- This happens if there was no text specified for the namespace that&lt;br /&gt;
    -- was detected or if the demospace parameter is not a valid namespace.&lt;br /&gt;
    -- Note that the parameter for the detected namespace must be completely&lt;br /&gt;
    -- absent for this to happen, not merely blank.&lt;br /&gt;
    if args[cfg.other] then&lt;br /&gt;
        return args[cfg.other]&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
----------------------------------------------------------------------&lt;br /&gt;
--                             Main function                        --&lt;br /&gt;
--      This is the function that will be most used. It processes   --&lt;br /&gt;
--      the arguments and calls the compare() function. It is       --&lt;br /&gt;
--      global, but is put down here as it depends on the other     --&lt;br /&gt;
--      local in order for it to work.                              --&lt;br /&gt;
----------------------------------------------------------------------&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 demospace and &lt;br /&gt;
    -- page parameters.&lt;br /&gt;
    local args = {}&lt;br /&gt;
    for k, v in pairs(origArgs) do&lt;br /&gt;
        v = mw.text.trim(v) -- Trim whitespace.&lt;br /&gt;
        if k == cfg.demospace or k == cfg.page 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;
    return compare(args)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
return p&lt;/div&gt;</summary>
		<author><name>Admin</name></author>
	</entry>
</feed>