Jump to content

Module:Ns has subpages/sandbox

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Mr. Stradivarius (talk | contribs) at 00:45, 17 June 2016 (try simplifying this by using title objects). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
-- 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