پودمان:شماره کیو

از ویکی‌پدیا، دانشنامهٔ آزاد
توضیحات پودمان[ایجاد] [پاکسازی]
-- 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('عنوان «' .. input .. '» معتبر نیست')
	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("عنوان «"  .. input .. "» تغییر مسیری است که به آیتم ویکی‌داده پیوند نشده")
		else
			error("جستاری با عنوان «"  .. input .. "» وجود ندارد یا به آیتم ویکی‌داده پیوند نشده")
		end
	end
end

return {
	numero = function (frame)
		if frame.args[1] == '' then
			error("عنوان جستار در الگو (احتمالا « شماره کیو ») یافت نشد، این پارامتر اجباری است")
		end
		local erreur_si_non_trouve = true
		if frame.args["erreur si non trouvé"] == "no" then
			erreur_si_non_trouve = false
		end
		local test_redirection = true
		if frame.args["test redirection"] == "no" then
			test_redirection = false
		end
		return qid_from_any(frame.args[1], erreur_si_non_trouve, test_redirection) or ''
	end,
	_numero = qid_from_any
}