Dokumentaci tohoto modulu lze vytvořit na stránce Modul:LinkEntry/Dokumentace

require 'strict'

local p = {}

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

local function getDescription(id)
	local desc, lang = mw.wikibase.getDescriptionWithLang(id)
	if lang == 'cs' then
		return lib.addWdClass(mw.text.nowiki(desc))
	end
	return nil
end

local function buildEntry(args)
	local out = args.link
	if args.parenthesis then
		out = out .. ' (' .. args.parenthesis .. ')'
	end
	if args.description then
		out = out .. ' – ' .. args.description
	end
	return out .. table.concat(args.categories, '')
end

local function checkEntity(id)
	if not mw.wikibase.isValidEntityId(id) then
		error(mw.ustring.format('„%s“ není platný identifikátor entity', id))
	end
end

local function wrapText(title, left, right)
	local repl, n = mw.ustring.gsub(
		title,
		'^(.-)%s+(%b())$',
		left .. '%1' .. right .. ' %2',
		1
	)
	if n ~= 0 then
		return repl
	else
		return left .. title .. right
	end
end

local function wrapByType(title, e_type)
	if e_type == 'dílo' or e_type == 'loď' then
		return wrapText(title, "''", "''")
	elseif e_type == 'píseň' then
		return wrapText(title, '„', '“')
	else
		return title
	end
end

local function buildLink(title, e_type)
	local text = wrapByType(title, e_type)
	if text ~= title then
		return mw.ustring.format('[[%s|%s]]', title, text)
	else
		return '[[' .. title .. ']]'
	end
end

local function getData(args)
	local title, link, id
	local categories = {}
	if args.title then
		title = args.title
		link = buildLink(title, args.type)
		id = mw.wikibase.getEntityIdForTitle(title)
		if not id and args.id then
			checkEntity(args.id)
			id = args.id
		elseif args.id and args.id ~= id then
			checkEntity(args.id)
			--table.insert(categories)
		end
	elseif args.id then
		id = args.id
		checkEntity(id)
		title = mw.wikibase.getSitelink(id)
		if title then
			link = lib.addWdClass(buildLink(title, args.type))
		else
			link = mw.wikibase.getLabel(id)
			if link then
				link = wrapByType(link, args.type)
			else
				link = lib.getLinkWhenNonexistingLabel(id)
			end
		end
	else
		error('Nebyl zadán název stránky ani položka na Wikidatech')
	end
	local description = args.description
	if not description and id then
		description = getDescription(id)
		--if not description then table.insert(categories) end
	end
	return { link = link, id = id, description = description, categories = categories }
end

local function getDateSnak(id, property)
	local WD = require 'Modul:Wikidata'
	local statements = WD.getStatements{
		id = id,
		property = property,
		rank = 'best',
		somevalue = true
	}
	if #statements > 0 then
		return statements[1].mainsnak
	else
		return nil
	end
end

function p.formatEntry(frame)
	local args = getArgs(frame, {
		frameOnly = true,
		trim = true,
		removeBlanks = true,
		readOnly = true,
	})
	local data = getData(args)
	if data.id and args.type == 'člověk' then
		local lifespan = lib.formatDateRange(
			{
				begin = getDateSnak(data.id, 'P569'),
				ending = getDateSnak(data.id, 'P570')
			},
			{
				precision = 9, -- year
				nolink = true,
				somevalue = '?',
				['begin-format'] = '* %s',
				['end-format'] = '† %s',
			}
		)
		if lifespan and lifespan ~= '' then
			data.parenthesis = lib.addWdClass(lifespan)
		end
	end
	return buildEntry(data)
end

return p