Difference between revisions 51816 and 51817 on wikimaniawiki

-- This module implements {{documentation}}.

-- Get required modules.
local getArgs = require('Module:Arguments').getArgs
local htmlBuilder = require('Module:HtmlBuilder')
local messageBox = require('Module:Message box')

-- Get the config table.
(contracted; show full)end

----------------------------------------------------------------------------
-- Environment settings
----------------------------------------------------------------------------

function p.getEnvironment(args)
	--
 Returns a table with information about the environment, including the title to use, the subject namespace, etc.
	-- This is called from p._main using pcall in case we get any errors from exceeding the expensive function count
	-- limit, or other perils unknown.
	--
	-- Data includes:
	-- env.title - the title object of the page we are making documentation for (usually the current title)
	-- env.subjectSpace - the number of the title's subject namespace.
	-- env.docspace - the name of the namespace the title puts its documentation in.
	-- env.templatePage - the name of the template page with no namespace or interwiki prefixes.
	local env, envFuncs = {}, {}

	-- Set up the metatable. If a nil value is called, we call that function in the envFuncs table and memoize it
	-- in the env table so we don't have to call any of the functions more than once.
	setmetatable(env, {
		__index = function (t, key)
			local envFunc = envFuncs[key]
			if envFunc then
				local success, val = pcall(envFunc)
				if success then
					env[key] = val -- Memoise the value.
					return val
				end
			end
			return nil
		end
	})	

	function envFuncs.title()
		-- The title object for the current page, or a test page passed with args.page.
		local title
		local titleArg = args.page
		if titleArg then
			title = mw.title.new(titleArg)
			if not title then
				error(message('titleArgError', 'string', {titleArg}))
			end
		else
			title = mw.title.getCurrentTitle()
		end
		return title
	end

	function envFuncs.subjectSpace()
		-- The subject namespace number.
		return mw.site.namespaces[env.title.namespace].subject.id
	end

	function envFuncs.docspace()
		-- The name of the documentation namespace.
		local subjectSpace = env.subjectSpace
		if subjectSpace == 0 or subjectSpace == 6 or subjectSpace == 8 or subjectSpace == 14 then
			-- Pages in the Article, File, MediaWiki or Category namespaces must have their
			-- /doc, /sandbox and /testcases pages in talk space.
			return mw.site.namespaces[subjectSpace].talk.name 
		else
			return env.title.subjectNsText
		end
	end

	function envFuncs.templatePage()
		-- The template page with no namespace or interwiki prefixes.
		local title = env.title
		local subpage = title.subpageText
		if subpage == message('sandboxSubpage', 'string') or subpage == message('testcasesSubpage', 'string') then
			return title.baseText
		else
			return title.text
		end[[
	-- Returns a table with information about the environment, including title objects and other namespace- or
	-- path-related data.
	--
	-- Title objects include:
	-- env.title - the page we are making documentation for (usually the current title)
	-- env.templateTitle - the template (or module, file, etc.)
	-- env.docTitle - the /doc subpage.
	-- env.sandboxTitle - the /sandbox subpage.
	-- env.testcasesTitle - the /testcases subpage.
	-- env.printTitle - the print version of the template, located at the /Print subpage.
	--
	-- Data includes:
	-- env.subjectSpace - the number of the title's subject namespace.
	-- env.docSpace - the number of the namespace the title puts its documentation in.
	-- env.docpageRoot - the text of the base page of the /doc, /sandbox and /testcases pages, with namespace.
	-- env.compareLink - a URL link of the Special:ComparePages page comparing the sandbox with the template.
	-- 
	-- All table lookups are passed through pcall so that errors are caught. If an error occurs, the value
	-- returned will be nil.
	--]]
	
	local env, envFuncs = {}, {}

	-- Set up the metatable. If triggered we call the corresponding function in the envFuncs table. The value
	-- returned by that function is memoized in the env table so that we don't call any of the functions
	-- more than once. (Nils won't be memoized.)
	setmetatable(env, {
		__index = function (t, key)
			local envFunc = envFuncs[key]
			if envFunc then
				local success, val = pcall(envFunc)
				if success then
					env[key] = val -- Memoise the value.
					return val
				end
			end
			return nil
		end
	})	

	function envFuncs.title()
		-- The title object for the current page, or a test page passed with args.page.
		local title
		local titleArg = args.page
		if titleArg then
			title = mw.title.new(titleArg)
			if not title then
				error(message('titleArgError', 'string', {titleArg}))
			end
		else
			title = mw.title.getCurrentTitle()
		end
		return title
	end

	function envFuncs.templateTitle()
		-- The template (or module, etc.) title object.
		local title = env.title
		local subpage = title.subpageText
		if subpage == message('sandboxSubpage', 'string') or subpage == message('testcasesSubpage', 'string') then
			return title.basePageTitle
		else
			return title
		end
	end

	function envFuncs.docTitle()
		-- Title object of the /doc subpage.
		local title = env.title
		local docname = args[1] -- User-specified doc page.
		local docpage
		if docname then
			docpage = docname
		else
			docpage = env.docpageRoot .. '/' .. message('docSubpage', 'string')
		end
		return mw.title.new(docpage)
	end
	
	function envFuncs.docpageRoot()
		-- The base page of the /doc, /sandbox, and /testcases subpages.
		-- For some namespaces this is the talk page, rather than the template page.
		local title = env.title
		return (env.docspace or title.nsText) .. ':' .. (env.templatePage or title.text)
	end
	
	function envFuncs.sandboxTitle()
		-- Title object for the /sandbox subpage.
		return mw.title.new(env.docpageRoot .. '/' .. message('sandboxSubpage', 'string'))
	end
	
	function envFuncs.testcasesTitle()
		-- Title object for the /testcases subpage.
		return mw.title.new(env.docpageRoot .. '/' .. message('testcasesSubpage', 'string'))
	end
	
	function envFuncs.printTitle()
		-- Title object for the /Print subpage.
		return mw.title.new(env.templatePage .. '/' .. message('printSubpage', 'string'))env.templateTitle:subPageTitle(message('printSubpage', 'string'))
	end

	function envFuncs.subjectSpace()
		-- The subject namespace number.
		return mw.site.namespaces[env.title.namespace].subject.id
	end

	function envFuncs.docSpace()
		-- The documentation namespace number. For most namespaces this is the same as the
		-- subject namespace. However, pages in the Article, File, MediaWiki or Category
		-- namespaces must have their /doc, /sandbox and /testcases pages in talk space.
		local subjectSpace = env.subjectSpace
		if subjectSpace == 0 or subjectSpace == 6 or subjectSpace == 8 or subjectSpace == 14 then
			return subjectSpace + 1
		else
			return subjectSpace
		end
	end

	function envFuncs.docpageRoot()
		-- The base page of the /doc, /sandbox, and /testcases subpages.
		-- For some namespaces this is the talk page, rather than the template page.
		local templateTitle = env.templateTitle
		local docSpace = env.docSpace
		local docSpaceText = mw.site.namespaces[docSpace].name
		-- Assemble the link. docSpace is never the main namespace, so we can hardcode the colon.
		return docSpaceText .. ':' .. templateTitle.text
	end
	
	function envFuncs.compareLink()
		-- Diff link between the sandbox and the main template using [[Special:ComparePages]].
		local templateTitle = env.templateTitle
		local sandboxTitle = env.sandboxTitle
		local compareUrl = mw.uri.fullUrl(
(contracted; show full)			.. '<br />'
	end
	return ret
end

function p.makeEndBoxExperimentBlurb(args, env)
	-- Renders the text "Editors can experiment in this template's sandbox (edit | diff) and testcases (edit) pages."
	
local subjectSpace = env.subjectSpace
	local templatePage = env.templatePage
	-- Get title objects.
	local templateTitle = env.templateTitle
	local sandboxTitle = env.sandboxTitle
	local testcasesTitle = env.testcasesTitle
	if-- Get environment data.
	local subjectSpace = env.subjectSpace
	local templateTitle = env.templateTitle
	local sandboxTitle = env.sandboxTitle
	local testcasesTitle = env.testcasesTitle
	local templatePage = templateTitle.prefixedText
	if not subjectSpace or not templateTitle or not sandboxTitle or not testcasesTitle then
		return nil
	end
	-- Make links.
	local sandboxLinks, testcasesLinks
	if sandboxTitle.exists then
		local sandboxPage = sandboxTitle.prefixedText
		local sandboxDisplay = message('sandboxLinkDisplay', 'string')
(contracted; show full)
		local sort = (title.namespace == 0 and message('strangeUsageCategoryMainspaceSort', 'string') or '') .. title.prefixedText -- Sort on namespace.
		ret = ret .. makeCategoryLink(message('strangeUsageCategory', 'string'), sort)
	end
	return ret
end

return p