Jump to content

Module:Ns has subpages

Permanently protected module
From Wikipedia, the free encyclopedia

-- This module implements [[Template:Ns has subpages]].
-- While the template is fairly simple, this information is made available to
-- Lua directly, so using a module means that we don't have to update the
-- template as new namespaces are added.

localp={}

functionp._main(ns,frame)
-- Get the current namespace if we were not passed one.
ifnotnsthen
ns=mw.title.getCurrentTitle().namespace
end

-- Look up the namespace table from mw.site.namespaces. This should work
-- for a majority of cases.
localnsTable=mw.site.namespaces[ns]

-- Try using string matching to get the namespace from page names.
-- Do a quick and dirty bad title check to try and make sure we do the same
-- thing as {{NAMESPACE}} in most cases.
ifnotnsTableandtype(ns)=='string'andnotns:find('[<>|%[%]{}]')then
localnsStripped=ns:gsub('^[_%s]*:','')
nsStripped=nsStripped:gsub(':.*$','')
nsTable=mw.site.namespaces[nsStripped]
end

-- If we still have no match then try the {{NAMESPACE}} parser function,
-- which should catch the remainder of cases. Don't use a mw.title object,
-- as this would increment the expensive function count for each new page
-- tested.
ifnotnsTablethen
frame=frameormw.getCurrentFrame()
localnsProcessed=frame:callParserFunction('NAMESPACE',ns)
nsTable=nsProcessedandmw.site.namespaces[nsProcessed]
end

returnnsTableandnsTable.hasSubpages
end

functionp.main(frame)
localns=frame:getParent().args[1]
ifnsthen
ns=ns:match('^%s*(.-)%s*$')-- trim whitespace
ns=tonumber(ns)orns
end
localhasSubpages=p._main(ns,frame)
returnhasSubpagesand'yes'or''
end

returnp