Difference between revisions of "Module:Ns has subpages/sandbox"
Jump to navigation
Jump to search
blackwiki>Mr. Stradivarius (Create sandbox version of Module:Ns has subpages) |
blackwiki>Mr. Stradivarius (try simplifying this by using title objects) |
||
| Line 7: | Line 7: | ||
function p._main(ns, frame) | function p._main(ns, frame) | ||
| − | -- | + | local nsData -- The subtable of mw.site.namespaces for the target namespace |
| − | if | + | |
| − | + | -- First check to see if the namespace we were passed is a valid namespace | |
| + | -- number or namespace name. | ||
| + | if ns then | ||
| + | nsData = mw.site.namespaces[ns] | ||
end | end | ||
| − | -- | + | -- If the namespace wasn't valid, assume we were passed a page name. We find |
| − | + | -- the namespace from the page's title object, or use the current title if | |
| − | + | -- we weren't passed a namespace. | |
| − | + | if not nsData then | |
| − | -- | + | local title |
| − | + | if ns then | |
| − | -- | + | title = mw.title.new(ns) |
| − | if not | + | else |
| − | local | + | title = mw.title.getCurrentTitle() |
| − | + | end | |
| − | + | if title then | |
| + | nsData = mw.site.namespaces[title.namespace] | ||
| + | end | ||
end | end | ||
| − | -- If we | + | -- If we found a valid namespace, return a boolean, otherwise return nil. |
| − | + | if nsData then | |
| − | + | return nsData.hasSubpages | |
| − | + | else | |
| − | if | + | return nil |
| − | |||
| − | |||
| − | |||
end | end | ||
| − | |||
| − | |||
end | end | ||
Revision as of 00:45, 17 June 2016
Documentation for this module may be created at Module:Ns has subpages/sandbox/doc
-- 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.
local p = {}
function p._main(ns, frame)
local nsData -- The subtable of mw.site.namespaces for the target namespace
-- First check to see if the namespace we were passed is a valid namespace
-- number or namespace name.
if ns then
nsData = mw.site.namespaces[ns]
end
-- If the namespace wasn't valid, assume we were passed a page name. We find
-- the namespace from the page's title object, or use the current title if
-- we weren't passed a namespace.
if not nsData then
local title
if ns then
title = mw.title.new(ns)
else
title = mw.title.getCurrentTitle()
end
if title then
nsData = mw.site.namespaces[title.namespace]
end
end
-- If we found a valid namespace, return a boolean, otherwise return nil.
if nsData then
return nsData.hasSubpages
else
return nil
end
end
function p.main(frame)
local ns = frame:getParent().args[1]
if ns then
ns = ns:match('^%s*(.-)%s*$') -- trim whitespace
ns = tonumber(ns) or ns
end
local hasSubpages = p._main(ns, frame)
return hasSubpages and 'yes' or ''
end
return p