--[[
--------------------------------------------------------------------------------
-- --
-- NAMESPACE DETECT --
-- --
-- This module implements the {{namespace detect}} template in Lua, with a --
-- few improvements: all namespaces and all namespace aliases are supported, --
-- and namespace names are detected automatically for the local wiki. The --
-- module can also use the corresponding subject namespace value if it is --
-- used on a talk page. Parameter names can be configured for different wikis --
-- by altering the values in the "cfg" table in --
-- Module:Namespace detect/config. --
-- --
--------------------------------------------------------------------------------
--]]
local data = mw.loadData('Module:Namespace detect/data')
local argKeys = data.argKeys
local cfg = data.cfg
local mappings = data.mappings
local yesno = require('Module:Yesno')
local mArguments
local mTableTools
local ustringLower = mw.ustring.lower
local p = {}
local function fetchValue(t1, t2)
for i, key in ipairs(t2) do
local value = t1[key]
if value ~= nil then
return value
end
end
return nil
end
local function equalsArrayValue(t, value)
for i, arrayValue in ipairs(t) do
if value == arrayValue then
return true
end
end
return false
end
function p.getPageObject(page)
if page then
local success, pageObject = pcall(mw.title.new, page)
if success then
return pageObject
else
return nil
end
else
return mw.title.getCurrentTitle()
end
end
function p.getParamMappings()
return mappings
end
local function getNamespace(args)
local page = fetchValue(args, argKeys.demopage)
if page == '' then page = nil end
local demospace = fetchValue(args, argKeys.demospace)
if demospace == '' then demospace = nil end
local subjectns = fetchValue(args, argKeys.subjectns)
local ret
if demospace then
if equalsArrayValue(argKeys.main, ustringLower(demospace)) then
ret = mw.site.namespaces[0].name
else
ret = demospace
end
else
local pageObject = p.getPageObject(page)
if pageObject then
if pageObject.isTalkPage then
if yesno(subjectns) then
ret = mw.site.namespaces[pageObject.namespace].subject.name
else
ret = 'talk'
end
else
ret = pageObject.nsText
end
else
return nil
end
end
ret = ret:gsub('_', ' ')
return ustringLower(ret)
end
function p._main(args)
local namespace = getNamespace(args) or 'other'
local params = mappings[namespace] or {}
local ret = fetchValue(args, params)
if ret == nil then
ret = fetchValue(args, argKeys.other)
end
return ret
end
function p.main(frame)
mArguments = require('Module:Arguments')
local args = mArguments.getArgs(frame, {removeBlanks = false})
local ret = p._main(args)
return ret or ''
end
return p