Acest modul este destinat construirii de formate utilizate în legături externe care trimit la baze de date sau fișiere de autoritate, eventual cu preluarea datelor de la Wikidata.

{{#invoke:ExtId|print
| pattern = <!-- formatul adresei URL, unde $1 se înlocuiește cu o valoare, de ex. "https://cs.wikipedia.org/wiki/$1" -->
| property = <!-- ID-ul proprietății de Wikidata, de ex. "P345" -->
| value = <!-- valoarea transmisă din parametrii formatului -->
| regex = <!-- expresie regulată (sintaxa Lua) pentru a valida valoarea completată local -->
| text = <!-- descriere a legăturii (opțional); de regulă și ea provine din parametrii formatului -->
| italic = <!-- "da", dacă se dorește formatarea cu italice a descrierii -->
| before = <!-- text opțional de afișat la stânga linkului -->
| after = <!-- text opțional de afișat la dreapta linkului -->
}}

require( 'strict' )
local getArgs = require('Modul:Arguments').getArgs
local Wikidata = require('Modul:Wikidata')

local p = {}

local function getWikidataValue(property)
	return Wikidata.findOneValueNoRef(property)
end

local function isValueTrue(val) 
	if type(val) == 'boolean' then return val end
	if type(val) == 'string' then return val == 'da' or val == 'yes' or val == 'true' or val == '1' end
	return false
end

function p.print(frame)
	local args = getArgs(frame, {
		removeBlanks = true,
		frameOnly = true,
		readOnly = true,
	})
	local parent = frame:getParent()
	local parent_args = getArgs(parent, {
		frameOnly = true,
		readOnly = true,
	})

	local template_name = mw.title.new(parent:getTitle()).text
	local title = mw.title.getCurrentTitle()
	local is_template_page = parent:getTitle() == title.fullText

	local is_local = isValueTrue(parent_args['local'])
	local property = args.property
	local formatterURL = args.pattern

	local regex = args.regex
	if regex and is_template_page then
		mw.ustring.find('', regex) -- dummy for error propagation
	end

	local categories = {}
	local good_format = true
	local fromWikidata = false
	local value = args.value
	if not value then
		if not is_local and property then
			value = getWikidataValue(property)
			if value then
				fromWikidata = true
			end
		end
		if not value then
			if not is_template_page then
				error('Nu a fost specificată sau găsită nicio valoare pentru identificatorul extern „' .. template_name .. '“')
			end

			value = ''
		end
	else
		if regex then
			local ok, arg, _ = pcall(mw.ustring.find, value, '^' .. regex .. '$')
			if not ok then
				mw.log(arg)
			elseif arg == nil then
				good_format = false
			end
		end
	end
	local text = args.text or mw.wikibase.getLabel() or title.text
	local link
	if formatterURL then
		local escaped = mw.ustring.gsub(value, '%%', '%%%%')
		local formattedURL = mw.ustring.gsub(formatterURL, '$1', escaped)
		link = '[' .. formattedURL .. ' ' .. text .. ']'
	else
		link = Wikidata.formatExternalLink(property, mw.wikibase.getEntityIdForCurrentPage(), text)
	end
	if isValueTrue(args['italic']) then
		link = "''" .. link .. "''"
	end
	local before, after, fail = '', '', ''
	if args.before then
		before = args.before .. ' '
	end
	if args.after then
		after = ' ' .. args.after
	end
	local ret = mw.ustring.format('%s%s%s%s%s', before, link, after, fail, table.concat(categories))
	return ret
end

return p