Module:LSV liens nompage

 Documentation[créer] [purger]
-- This module implements {{LSV liens nompage}}.


local formatStringSingle = [[
<div class="plainlinks liste-horizontale">
* ( %s | %s )
</div>]]

local formatStringMulti = [[
<div class="plainlinks liste-horizontale">
* ( %s )
</div>
<div class="plainlinks liste-horizontale">
* ( Historiques des articles : %s )
</div>]]

local p = {}

local function makeWikitextError(msg)
	return string.format('<strong class="error">Erreur : %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" n\'est pas un nom de page valide',
			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 = 'Modèle:LSV liens nompage'
	})
	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(string.format('"%s" pas de proposition d\'anecdote',	tostring(nominationPage)))
	end
	if not historyPages or not historyPages[1] then
		return makeWikitextError('pas d\'articles spécifié')
	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 = 'Wikipédia:Le saviez-vous%3F/' .. nominationPage
		if currentPage == dykSubpage then
			nominationLink = string.format(
				'[[WP:Le saviez-vous%3F/Anecdotes proposées|Revenir à WP:LSV]]',
				nominationPage
			)
		else
			nominationLink = makeFullUrl(
				'WP:LSV/' .. nominationPage,
				{action = 'edit'},
				'Relecture ou commentaire'
			)
		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'},
				'historique'
			)
		end
	end

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

return p