https://nordiclarp.org/w/index.php?title=Module:Namespace_detect&feed=atom&action=historyModule:Namespace detect - Revision history2024-03-28T09:18:17ZRevision history for this page on the wikiMediaWiki 1.34.2https://nordiclarp.org/w/index.php?title=Module:Namespace_detect&diff=7846&oldid=prevJohannes Axner: 1 revision imported2018-12-29T02:22:08Z<p>1 revision imported</p>
<table class="diff diff-contentalign-left" data-mw="interface">
<tr class="diff-title" lang="en">
<td colspan="1" style="background-color: #fff; color: #222; text-align: center;">← Older revision</td>
<td colspan="1" style="background-color: #fff; color: #222; text-align: center;">Revision as of 02:22, 29 December 2018</td>
</tr><tr><td colspan="2" class="diff-notice" lang="en"><div class="mw-diff-empty">(No difference)</div>
</td></tr></table>Johannes Axnerhttps://nordiclarp.org/w/index.php?title=Module:Namespace_detect&diff=7845&oldid=preven>Mr. Stradivarius: use demopage instead of page as the main "page" parameter2014-04-05T17:01:23Z<p>use demopage instead of page as the main "page" parameter</p>
<p><b>New page</b></p><div>--[[<br />
--------------------------------------------------------------------------------<br />
-- --<br />
-- NAMESPACE DETECT --<br />
-- --<br />
-- This module implements the {{namespace detect}} template in Lua, with a --<br />
-- few improvements: all namespaces and all namespace aliases are supported, --<br />
-- and namespace names are detected automatically for the local wiki. The --<br />
-- module can also use the corresponding subject namespace value if it is --<br />
-- used on a talk page. Parameter names can be configured for different wikis --<br />
-- by altering the values in the "cfg" table in --<br />
-- Module:Namespace detect/config. --<br />
-- --<br />
--------------------------------------------------------------------------------<br />
--]]<br />
<br />
local data = mw.loadData('Module:Namespace detect/data')<br />
local argKeys = data.argKeys<br />
local cfg = data.cfg<br />
local mappings = data.mappings<br />
<br />
local yesno = require('Module:Yesno')<br />
local mArguments -- Lazily initialise Module:Arguments<br />
local mTableTools -- Lazily initilalise Module:TableTools<br />
local ustringLower = mw.ustring.lower<br />
<br />
local p = {}<br />
<br />
local function fetchValue(t1, t2)<br />
-- Fetches a value from the table t1 for the first key in array t2 where<br />
-- a non-nil value of t1 exists.<br />
for i, key in ipairs(t2) do<br />
local value = t1[key]<br />
if value ~= nil then<br />
return value<br />
end<br />
end<br />
return nil<br />
end<br />
<br />
local function equalsArrayValue(t, value)<br />
-- Returns true if value equals a value in the array t. Otherwise<br />
-- returns false.<br />
for i, arrayValue in ipairs(t) do<br />
if value == arrayValue then<br />
return true<br />
end<br />
end<br />
return false<br />
end<br />
<br />
function p.getPageObject(page)<br />
-- Get the page object, passing the function through pcall in case of<br />
-- errors, e.g. being over the expensive function count limit.<br />
if page then<br />
local success, pageObject = pcall(mw.title.new, page)<br />
if success then<br />
return pageObject<br />
else<br />
return nil<br />
end<br />
else<br />
return mw.title.getCurrentTitle()<br />
end<br />
end<br />
<br />
-- Provided for backward compatibility with other modules<br />
function p.getParamMappings()<br />
return mappings<br />
end<br />
<br />
local function getNamespace(args)<br />
-- This function gets the namespace name from the page object.<br />
local page = fetchValue(args, argKeys.demopage)<br />
if page == '' then<br />
page = nil<br />
end<br />
local demospace = fetchValue(args, argKeys.demospace)<br />
if demospace == '' then<br />
demospace = nil<br />
end<br />
local subjectns = fetchValue(args, argKeys.subjectns)<br />
local ret<br />
if demospace then<br />
-- Handle "demospace = main" properly.<br />
if equalsArrayValue(argKeys.main, ustringLower(demospace)) then<br />
ret = mw.site.namespaces[0].name<br />
else<br />
ret = demospace<br />
end<br />
else<br />
local pageObject = p.getPageObject(page)<br />
if pageObject then<br />
if pageObject.isTalkPage then<br />
-- Get the subject namespace if the option is set,<br />
-- otherwise use "talk".<br />
if yesno(subjectns) then<br />
ret = mw.site.namespaces[pageObject.namespace].subject.name<br />
else<br />
ret = 'talk'<br />
end<br />
else<br />
ret = pageObject.nsText<br />
end<br />
else<br />
return nil -- return nil if the page object doesn't exist.<br />
end<br />
end<br />
ret = ret:gsub('_', ' ')<br />
return ustringLower(ret)<br />
end<br />
<br />
function p._main(args)<br />
-- Check the parameters stored in the mappings table for any matches.<br />
local namespace = getNamespace(args) or 'other' -- "other" avoids nil table keys<br />
local params = mappings[namespace] or {}<br />
local ret = fetchValue(args, params)<br />
--[[<br />
-- If there were no matches, return parameters for other namespaces.<br />
-- This happens if there was no text specified for the namespace that<br />
-- was detected or if the demospace parameter is not a valid<br />
-- namespace. Note that the parameter for the detected namespace must be<br />
-- completely absent for this to happen, not merely blank.<br />
--]]<br />
if ret == nil then<br />
ret = fetchValue(args, argKeys.other)<br />
end<br />
return ret<br />
end<br />
<br />
function p.main(frame)<br />
mArguments = require('Module:Arguments')<br />
local args = mArguments.getArgs(frame, {removeBlanks = false})<br />
local ret = p._main(args)<br />
return ret or ''<br />
end<br />
<br />
function p.table(frame)<br />
--[[<br />
-- Create a wikitable of all subject namespace parameters, for<br />
-- documentation purposes. The talk parameter is optional, in case it<br />
-- needs to be excluded in the documentation.<br />
--]]<br />
<br />
-- Load modules and initialise variables.<br />
mTableTools = require('Module:TableTools')<br />
local namespaces = mw.site.namespaces<br />
local cfg = data.cfg<br />
local useTalk = type(frame) == 'table' <br />
and type(frame.args) == 'table' <br />
and yesno(frame.args.talk) -- Whether to use the talk parameter.<br />
<br />
-- Get the header names.<br />
local function checkValue(value, default)<br />
if type(value) == 'string' then<br />
return value<br />
else<br />
return default<br />
end<br />
end<br />
local nsHeader = checkValue(cfg.wikitableNamespaceHeader, 'Namespace')<br />
local aliasesHeader = checkValue(cfg.wikitableAliasesHeader, 'Aliases')<br />
<br />
-- Put the namespaces in order.<br />
local mappingsOrdered = {}<br />
for nsname, params in pairs(mappings) do<br />
if useTalk or nsname ~= 'talk' then<br />
local nsid = namespaces[nsname].id<br />
-- Add 1, as the array must start with 1; nsid 0 would be lost otherwise.<br />
nsid = nsid + 1 <br />
mappingsOrdered[nsid] = params<br />
end<br />
end<br />
mappingsOrdered = mTableTools.compressSparseArray(mappingsOrdered)<br />
<br />
-- Build the table.<br />
local ret = '{| class="wikitable"'<br />
.. '\n|-'<br />
.. '\n! ' .. nsHeader<br />
.. '\n! ' .. aliasesHeader<br />
for i, params in ipairs(mappingsOrdered) do<br />
for j, param in ipairs(params) do<br />
if j == 1 then<br />
ret = ret .. '\n|-'<br />
.. '\n| <code>' .. param .. '</code>'<br />
.. '\n| '<br />
elseif j == 2 then<br />
ret = ret .. '<code>' .. param .. '</code>'<br />
else<br />
ret = ret .. ', <code>' .. param .. '</code>'<br />
end<br />
end<br />
end<br />
ret = ret .. '\n|-'<br />
.. '\n|}'<br />
return ret<br />
end<br />
<br />
return p</div>en>Mr. Stradivarius