A modult a Modul:Wikibase/doc lapon tudod dokumentálni

require"strict"

local p = {}
local getArgs = require"Modul:Arguments".getArgs

-- Return the item ID of the item linked to the current page.
function p.id(frame)
	local entity = mw.wikibase.getEntity()

	if entity == nil then
		return "(nincs kapcsolt elem)"
	end

	return entity.id
end

-- Return the label of a given data item.
function p.label(frame)
	local id
	if frame.args[1] == nil then
		local entity = mw.wikibase.getEntity()
		if not entity then
			return nil
		end

		id = entity.id
	else
		id = frame.args[1]
	end

	return mw.wikibase.label(id)
end

-- Return the local page about a given data item.
function p.page(frame)
	local id
	if frame.args[1] == nil then
		local entity = mw.wikibase.getEntity()
		if not entity then
			return nil
		end

		id = entity.id
	else
		id = frame.args[1]
	end

	return mw.wikibase.sitelink(id)
end

function p.renderSnaks(frame)
	local entity = mw.wikibase.getEntity()
	local property = frame.args[1]
	if entity and entity.claims and entity.claims[property] and entity.claims[property][1].qualifiers then
		return mw.wikibase.renderSnaks(entity.claims[property][1].qualifiers)
	end
	return nil
end

function p.renderReference(frame)
	local entity = mw.wikibase.getEntity()
	local property = frame.args[1]
	local statementIndex = tonumber(frame.args[2]) or 1
	local referenceIndex = tonumber(frame.args[3]) or 1
	if entity and entity.claims and entity.claims[property] and entity.claims[property][statementIndex] and 
	entity.claims[property][statementIndex].references and entity.claims[property][statementIndex].references[referenceIndex] then
		return mw.wikibase.renderSnaks(entity.claims[property][statementIndex].references[referenceIndex].snaks)
	end
	return nil
end

-- Return the sitelink on globalSiteId wiki (connected to entityId entity)
-- Burkolósablonok: Projektlink
function p.sitelink(frame)
	local args = getArgs(frame)
	local globalSiteId = args.globalSiteId or args[1]
	local entityId = args.entityId or args[2]
	local entity = mw.wikibase.getEntity(entityId)
	return entity and entity:getSitelink(globalSiteId)
end

local function processLocalLink(localLinks, lang, title)
	localLinks[lang] = localLinks[lang] or title
end

local function processEditionLink(editionLinks, lang, title)
	if not editionLinks[lang] then
		editionLinks[lang] = { title = title, conflict = false }
	else
		editionLinks[lang].conflict = true
	end
end

local function processSiteLinks(entity, outputTable, processLink)
	if entity.sitelinks then
		for _, v in pairs(entity.sitelinks) do
			if string.sub(v.site, -10) == 'wikisource' then
				local lang = string.sub(v.site, 1, -11)
				processLink(outputTable, lang, v.title)
			end
		end
	end
end

function p._wikisourceLangLinks(entity)
	if not entity then
		return {}
	end
	local localLinks = {}
	processSiteLinks(entity, localLinks, processLocalLink)
	local editionEntity
	local editionOf = entity:getBestStatements('P629')
	if #editionOf == 1 and editionOf[1].mainsnak.snaktype == 'value' then
		editionEntity = mw.wikibase.getEntity(editionOf[1].mainsnak.datavalue.value.id)
		processSiteLinks(editionEntity, localLinks, processLocalLink)
	end
	local editionLinks = {}
	local editions = entity:getBestStatements('P747')
	for _, editionStatement in ipairs(editions) do
		if editionStatement.mainsnak.snaktype == 'value' then
			processSiteLinks(mw.wikibase.getEntity(editionStatement.mainsnak.datavalue.value.id), editionLinks, processEditionLink)
		end
	end
	if editionEntity then
		editions = editionEntity:getBestStatements('P747')
		for _, editionStatement in ipairs(editions) do
			if editionStatement.mainsnak.snaktype == 'value' then
				processSiteLinks(mw.wikibase.getEntity(editionStatement.mainsnak.datavalue.value.id), editionLinks, processEditionLink)
			end
		end
	end
	for lang, v in pairs(editionLinks) do
		if not v.conflict and not localLinks[lang] then
			localLinks[lang] = v.title
		end
	end
	local finalLinks = {}
	for lang, title in pairs(localLinks) do
		if lang ~= 'hu' then -- skip this very wiki
			table.insert(finalLinks, {lang = lang:gsub('%_', '-'), title = title})
		end
	end
	table.sort(finalLinks, function (a, b) return a.lang < b.lang end)
	return finalLinks
end

function p.wikisourceLangLinks(frame)
	local args = getArgs(frame)
	local entity = args.entity
	if not entity then
		entity = mw.wikibase.getEntity(args.entityId or args[1])
	end
	local finalLinks = p._wikisourceLangLinks(entity)
	local ret = {}
	for _, v in ipairs(finalLinks) do
		table.insert(ret, string.format('[[%s:%s]]', v.lang, v.title))
	end
	return table.concat(ret, '\n')
end

return p