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.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 wrapItalics(title)
	local repl, n = mw.ustring.gsub(title, '^(.-)%s+(%b())$', "''%1'' %2", 1)
	if n ~= 0 then
		return repl
	else
		return "''" .. title .. "''"
	end
end

local function buildLink(title, italic)
	if italic then
		return mw.ustring.format('[[%s|%s]]', title, wrapItalics(title))
	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 == 'dílo')
		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 == 'dílo'))
		else
			link = mw.wikibase.getLabel(id)
			if link then
				if args.type == 'dílo' then
					link = wrapItalics(link)
				end
			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/sandbox'
	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 WD = require 'Modul:Wikidata/sandbox'
		local lifespan = WD.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