Module:Numéro Q

Útá Wikipedia.

La documentation pour ce module peut être créée à Module:Numéro Q/doc

-- extracting Q titles from wikilinks like [[d:Q1]], [[:d:Q1]], [[wikidata:Q1]], [[d:Q1|Q1]] or [[d:Q1|plop]]
local function qid_from_wd_wikilink(link)
	local attempt = link
		:gsub("^%[%[:?d:", "")
		:gsub("^%[%[:?wikidata:", "")
		:gsub("%|.*%]%]$", "")
		:gsub("%]%]$", "")
	if attempt:find("^Q[0-9]+$") then
		return attempt
	end
end

local function qid_from_any(input, erreur_si_non_trouve, test_redirection)
	local attempt

	attempt = qid_from_wd_wikilink(input)
	if attempt then
		return attempt
	end

	local decodedInput = mw.text.decode(input) -- in case template family like {{PAGENAME}} with its bug are used
	local title = mw.title.new(decodedInput)
	if not title then
		error('le titre « ' .. input .. ' » est invalide')
	end

	attempt = mw.wikibase.getEntityIdForTitle(title.prefixedText)
	if attempt then
		return attempt
	end

	if erreur_si_non_trouve then
		if test_redirection and title.isRedirect then
			error("le titre « "  .. input .. " » est une redirection qui n’est pas reliée à un élément Wikidata")
		else
			error("l’article de titre « "  .. input .. " » n’existe pas ou n’est pas lié à un élément Wikidata")
		end
	end
end

return {
	numero = function (frame)
		if frame.args[1] == '' then
			error("pas de titre d’article fourni au modèle (probablement « Numéro Q »), c’est un paramètre requis")
		end
		local erreur_si_non_trouve = true
		if frame.args["erreur si non trouvé"] == "non" then
			erreur_si_non_trouve = false
		end
		local test_redirection = true
		if frame.args["test redirection"] == "non" then
			test_redirection = false
		end
		return qid_from_any(frame.args[1], erreur_si_non_trouve, test_redirection) or ''
	end,
	_numero = qid_from_any
}