Difference between revisions 51775 and 51776 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')
local libraryUtil = require('libraryUtil')

-- Get the config table.
local cfg = mw.loadData('Module:Documentation/config')

local p = {}

-- Constants.
local currentTitle = mw.title.getCurrentTitle()
local subjectSpace = mw.site.namespaces[currentTitle.namespace].subject.id -- The number of the current subject namespace.

-- Often-used functions.
local gsub = mw.ustring.gsub
local checkType = libraryUtil.checkType

----------------------------------------------------------------------------
-- Helper functions
----------------------------------------------------------------------------

local function formatMessage(msgcfgKey, valArray)
	--[[
	-- Formats a message, usually from the cfg table.
	-- Values from valArray can be specified in the message by using $1 for [1], $2 for [2], etc.
	-- So formatMessage('Foo $2 bar $1.For example, if the message cfg.fooMessage had the value 'Foo $2 bar $1.',
	-- formatMessage('fooMessage', {'baz', 'qux'}) willould return "Foo qux bar baz."
	--]]
	checkType('formatMessage', 1, msgcfgKey, 'string')
	checkType('formatMessage', 2, valArray, 'table')
	local msg = cfg[cfgKey] or error('formatMessage: no message found for cfg key "' .. cfgKey .. '"', 2)

	local function getMessageVal(match)
		match = tonumber(match)
		return valArray[match] or error('formatMessage: Nno value found for key $' .. match .. '. M in message was "' .. msg .. '"'cfg.' .. cfgKey, 42)
	end

	local ret = gsub(msg, '$([1-9][0-9]*)', getMessageVal)
	return ret
end

local function makeWikilink(page, display)
(contracted; show full)		text = text .. linkBox
	else
		if docExist then
			-- /doc exists; link to it.
			local docLink = makeWikilink(docpage)
			local editLink = makeUrlLink(docTitle:fullUrl{action = 'edit'}, cfg.editLinkDisplay)
			local historyLink = makeUrlLink(docTitle:fullUrl{action = 'history'}, cfg.historyLinkDisplay)
			text = text .. formatMessage(
cfg.'transcludedFromBlurb', {docLink}) .. ' ' .. makeToolbar(editLink, historyLink) .. '<br />'
		elseif subjectSpace == 828 then
			-- /doc does not exist; ask to create it.
			local createLink = makeUrlLink(docTitle:fullUrl{action = 'edit', preload = cfg.modulePreload}, cfg.createLinkDisplay)
			text = text .. formatMessage(cfg.'createModuleDocBlurb', {createLink}) .. '<br />'
		end
		-- Add links to /sandbox and /testcases when appropriate.
		if subjectSpace == 2 or subjectSpace == 828 or subjectSpace == 10 then
			-- We are in the user, module or template namespaces. 
			local sandboxLinks, testcasesLinks
			local pagePossessive = subjectSpace == 828 and cfg.modulePossessive or cfg.templatePossessive
			local sandboxTitle = mw.title.new(sandbox)
			if sandboxTitle.exists then
				local sandboxLink = makeWikilink(sandbox, cfg.sandboxLinkDisplay)
				local sandboxEditLink = makeUrlLink(sandboxTitle:fullUrl{action = 'edit'}, cfg.sandboxEditLinkDisplay)
				local compareLink = makeUrlLink(mw.title.new('Special:ComparePages'):fullUrl{page1 = templatePage, page2 = sandbox}, cfg.compareLinkDisplay)
				sandboxLinks = sandboxLink .. ' ' .. makeToolbar(sandboxEditLink, compareLink)
			else
				local sandboxPreload = subjectSpace == 828 and cfg.moduleSandboxPreload or cfg.templateSandboxPreload
				local sandboxCreateLink = makeUrlLink(sandboxTitle:fullUrl{action = 'edit', preload = sandboxPreload}, cfg.sandboxCreateLinkDisplay)
				local mirrorSummary = formatMessage(cfg.'mirrorEditSummary', {makeWikilink(templatePage)})
				local mirrorLink = makeUrlLink(sandboxTitle:fullUrl{action = 'edit', preload = templatePage, summary = mirrorSummary}, cfg.mirrorLinkDisplay)
				sandboxLinks = cfg.sandboxLinkDisplay .. ' ' .. makeToolbar(sandboxCreateLink, mirrorLink)
			end
			local testcaseTitle = mw.title.new(testcases)
			if testcaseTitle.exists then
				local testcasesLink = makeWikilink(testcases, cfg.testcasesLinkDisplay)
				local testcasesEditLink = makeUrlLink(testcaseTitle:fullUrl{action = 'edit'}, cfg.testcasesEditLinkDisplay)
				testcasesLinks = testcasesLink .. ' ' .. makeToolbar(testcasesEditLink)
			else
				local testcasesPreload = subjectSpace == 828 and cfg.moduleTestcasesPreload or cfg.templateTestcasesPreload
				local testcasesCreateLink = makeUrlLink(testcaseTitle:fullUrl{action = 'edit', preload = testcasesPreload}, cfg.testcasesCreateLinkDisplay)
				testcasesLinks = cfg.testcasesLinkDisplay .. ' ' .. makeToolbar(testcasesCreateLink)
			end
			text = text .. formatMessage(cfg.'experimentBlurb', {pagePossessive, sandboxLinks, testcasesLinks}) .. '<br />'
			-- Show the categories text, but not if "content" fed or "docname fed" since then it is unclear where to add the categories.
			if not content and not docnameFed then
				local docPathLink = makeWikilink(docpage, cfg.docLinkDisplay)
				text = text .. formatMessage(cfg.'addCategoriesBlurb', {docPathLink})
			end
			-- Show the "subpages" link.
			if subjectSpace ~= 6 then -- Don't show the link in file space.
				local pagetype
				if subjectSpace == 10 then
					pagetype = cfg.templatePagetype
				elseif subjectSpace == 828 then
					pagetype = cfg.modulePagetype
				else
					pagetype = cfg.defaultPagetype
				end
				text = text .. ' ' .. makeWikilink('Special:PrefixIndex/' .. templatePage .. '/', formatMessage(cfg.'subpagesLinkDisplay', {pagetype}))
			end
			-- Show the "print" link if it exists.
			local printPage = templatePage .. '/' .. cfg.printSubpage
			local printTitle = mw.title.new(printPage)
			if printTitle.exists then
				local printLink = makeWikilink(printPage, cfg.printLinkDisplay)
				text = text .. '<br />' .. formatMessage(cfg.'printBlurb', {printLink})
					.. (cfg.displayPrintCategory and makeCategoryLink(cfg.printCategory) or '')
			end
		end
	end
	fmargs.text = text

	-- Return the fmbox output.
	return messageBox.main('fmbox', fmargs)
end

function p.addTrackingCategories()
	-- Check if {{documentation}} is transcluded on a /doc or /testcases page.
	local ret = ''
	local subpage = currentTitle.subpageText
	if cfg.displayStrangeUsageCategory and (subpage == cfg.docSubpage or subpage == cfg.testcasesSubpage) then
		local sort = (currentTitle.namespace == 0 and cfg.strangeUsageCategoryMainspaceSort or '') .. currentTitle.prefixedText -- Sort on namespace.
		ret = ret .. makeCategoryLink(cfg.strangeUsageCategory, sort)
	end
	return ret
end

function p.docspace()
	-- Determines the namespace of the documentation.
	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 currentTitle.subjectNsText
	end
end

function p.templatePage()
	-- Determines the template page. No namespace or interwiki prefixes are included.
	local subpage = currentTitle.subpageText
	if subpage == cfg.sandboxSubpage or subpage == cfg.testcasesSubpage then
		return currentTitle.baseText
	else
		return currentTitle.text
	end
end

return p