Difference between revisions of "Module:Ns has subpages/sandbox"

From blackwiki
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)
-- Get the current namespace if we were not passed one.
+
local nsData -- The subtable of mw.site.namespaces for the target namespace
if not ns then
+
 
ns = mw.title.getCurrentTitle().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
 
end
  
-- Look up the namespace table from mw.site.namespaces. This should work
+
-- If the namespace wasn't valid, assume we were passed a page name. We find
-- for a majority of cases.
+
-- the namespace from the page's title object, or use the current title if
local nsTable = mw.site.namespaces[ns]
+
-- we weren't passed a namespace.
 
+
if not nsData then
-- Try using string matching to get the namespace from page names.
+
local title
-- Do a quick and dirty bad title check to try and make sure we do the same
+
if ns then
-- thing as {{NAMESPACE}} in most cases.
+
title = mw.title.new(ns)
if not nsTable and type(ns) == 'string' and not ns:find('[<>|%[%]{}]') then
+
else
local nsStripped = ns:gsub('^[_%s]*:', '')
+
title = mw.title.getCurrentTitle()
nsStripped = nsStripped:gsub(':.*$', '')
+
end
nsTable = mw.site.namespaces[nsStripped]
+
if title then
 +
nsData = mw.site.namespaces[title.namespace]
 +
end
 
end
 
end
  
-- If we still have no match then try the {{NAMESPACE}} parser function,
+
-- If we found a valid namespace, return a boolean, otherwise return nil.
-- which should catch the remainder of cases. Don't use a mw.title object,
+
if nsData then
-- as this would increment the expensive function count for each new page
+
return nsData.hasSubpages
-- tested.
+
else
if not nsTable then
+
return nil
frame = frame or mw.getCurrentFrame()
 
local nsProcessed = frame:callParserFunction('NAMESPACE', ns)
 
nsTable = nsProcessed and mw.site.namespaces[nsProcessed]
 
 
end
 
end
 
return nsTable and nsTable.hasSubpages
 
 
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