Module:DYK nompage links

MyWikiBiz, Author Your Legacy — Sunday November 03, 2024
Revision as of 21:15, 15 July 2021 by Zoran (talk | contribs) (Pywikibot 6.4.0)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

This module produces the list of links used on Wikipedia:Did you know nomination pages. It implements {{DYK nompage links}}.

Usage from wikitext

Usually, this module should be used via the {{DYK nompage links}} template. However, it can also be used with the syntax {{#invoke:DYK nompage links|main|arguments}}. See Template:DYK nompage links for a list of arguments.

Usage from other Lua modules

To use this module from other Lua modules, first load the module:

<syntaxhighlight lang="lua"> local mDYKlinks = require('Module:DYK nompage links') </syntaxhighlight>

You can then use the _main function like this:

<syntaxhighlight lang="lua"> mDYKlinks._main(nominationPage, historyPages) </syntaxhighlight>

nominationPage is the nomination page name, as a string. historyPages is an array of page names to display (also strings). Both parameters are required, and historyPages must have a length of at least one.



-- This module implements {{DYK nompage links}}.


local formatStringSingle = [[
<div class="plainlinks hlist">
* ( %s
* %s )
</div>]]

local formatStringMulti = [[
<div class="plainlinks hlist">
* ( %s )
</div>
<div class="plainlinks hlist">
* ( Article history links: %s )
</div>]]

local p = {}

local function makeWikitextError(msg)
	return string.format('<strong class="error">Error: %s</strong>', msg)
end

local function makeFullUrl(page, query, display)
	local url = mw.uri.fullUrl(page, query)
	if not url then
		url = makeWikitextError(string.format(
			'"%s" is not a valid page name',
			tostring(page)
		))
	end
	return string.format(
		'[%s %s]',
		tostring(url),
		display
	)
end

function p.main(frame)
	local mArguments = require('Module:Arguments')
	local mTableTools = require('Module:TableTools')
	local args = mArguments.getArgs(frame, {
		wrappers = 'Template:DYK nompage links'
	})
	local nominationPage = args.nompage
	local historyPages = mTableTools.compressSparseArray(args)
	return p._main(nominationPage, historyPages)
end

function p._main(nominationPage, historyPages)
	-- Deal with bad input.
	if not nominationPage then
		return makeWikitextError('no nomination page specified')
	end
	if not historyPages or not historyPages[1] then
		return makeWikitextError('no articles specified')
	end

	-- Find out whether we are dealing with multiple history pages.
	local isMulti = #historyPages > 1

	-- Make the nompage link.
	local nominationLink
	do
		local currentPage = mw.title.getCurrentTitle().prefixedText
		local dykSubpage = 'Template:Did you know nominations/' .. nominationPage
		if currentPage == dykSubpage then
			nominationLink = string.format(
				'[[Template talk:Did you know#%s|Back to T:TDYK]]',
				nominationPage
			)
		else
			nominationLink = makeFullUrl(
				'Template:Did you know nominations/' .. nominationPage,
				{action = 'edit'},
				'Review or comment'
			)
		end
	end

	-- Make the history links.
	local historyLinks
	do
		if isMulti then
			local links = {}
			for i, page in ipairs(historyPages) do
				links[#links + 1] = makeFullUrl(
					page,
					{action = 'history'},
					page
				)
			end
			historyLinks = table.concat(links, '\n* ')
		else
			historyLinks = makeFullUrl(
				historyPages[1],
				{action = 'history'},
				'Article history'
			)
		end
	end

	-- Assemble the output.
	local stringToFormat = isMulti and formatStringMulti or formatStringSingle
	return string.format(stringToFormat, nominationLink, historyLinks)
end

return p