Přeskočit na obsah

Modul:LinkEntry

Z Wikipedie, otevřené encyklopedie

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

require 'Modul:No globals'

local p = {}

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

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.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 getData(args)
	local title, link, id
	local categories = {}
	if args.title then
		title = args.title
		link = '[[' .. title .. ']]'
		id = mw.wikibase.getEntityIdForTitle(title)
		if not 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('[[' .. title .. ']]')
		else
			link = mw.wikibase.getLabel(id) or lib.getLinkWhenNonexistingLabel(id)
		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

function p.formatEntry(frame)
	local args = getArgs(frame, {
		frameOnly = true,
		trim = true,
		removeBlanks = true,
		readOnly = true,
	})
	local data = getData(args)
	return buildEntry(data)
end

function p.formatHumanEntry(frame)
	local args = getArgs(frame, {
		frameOnly = true,
		trim = true,
		removeBlanks = true,
		readOnly = true,
	})
	local data = getData(args)
	if data.id then
		-- TODO: datum narození/úmrtí
	end
	return buildEntry(data)
end

return p