Documentation for this module may be created at Module:Wikibase/τεκμηρίωση

-- Module:Wikibase
local p = {}

-- Return the item ID of the item linked to the current page.
-- mw.wikibase.getEntityIdForCurrentPage
function p.id(frame)
	return mw.wikibase.getEntityIdForCurrentPage()
end

-- Return the label of a given data item, optionally in a given language.
-- mw.wikibase.getLabel, mw.wikibase.getLabelByLang
function p.label(frame)
	local id
	if frame.args[1] == nil then
		id = mw.wikibase.getEntityIdForCurrentPage()
		if not id then return nil end
	else
		id = mw.text.trim(frame.args[1])
	end
	if frame.args[2] then
		return mw.wikibase.getLabelByLang(id, mw.text.trim(frame.args[2]))
	end
	return mw.wikibase.getLabel(id)
end

-- Return the language code of the label of a given data item.
-- mw.wikibase.getLabelWithLang
function p.label_lang(frame)
	local id
	if frame.args[1] == nil then
		id = mw.wikibase.getEntityIdForCurrentPage()
		if not id then return nil end
	else
		id = mw.text.trim(frame.args[1])
	end
	local _, lang = mw.wikibase.getLabelWithLang(id)
	return lang
end

-- Return the description of a given data entity, optionally in a given language.
-- mw.wikibase.getDescription
function p.description(frame)
	local id = frame.args[1] and mw.text.trim(frame.args[1]) or nil
	local lang = frame.args[2] and mw.text.trim(frame.args[2]) or nil
	local entity = mw.wikibase.getEntity(id)
	if not entity then return nil end
	return entity:getDescription(lang)
end

-- Return the local page about a given data item, optionary in a given wiki.
-- mw.wikibase.getSitelink
function p.page(frame) -- alias
	return p.getSiteLink(frame)
end

function p.getSiteLink(frame)
	local id = frame.args[1] and mw.text.trim(frame.args[1])
	if id == nil or id == '' then
		id = mw.wikibase.getEntityIdForCurrentPage()
		if not id then return nil end
	end
	return mw.wikibase.getSitelink(id, mw.text.trim(frame.args[2] or ''))
end

-- Expansion of {{#if:{{#property:...}} }}
-- returns the first value of given property or nil if not found, not isValidEntityId or novalue/somevalue
-- mw.wikibase.isValidEntityId
function p.validproperty(frame)
	local property = mw.text.trim(frame.args[1])
	local item = frame.args.item or frame.args.from; if item == '' then item = nil end
	local type = frame.args.type or "id"
	local entity = mw.wikibase.getEntity(item)
	if not entity then return end
	if not entity.claims then return end
	local hasProp = entity.claims[property]
	if not hasProp then return end
	if not hasProp[1].mainsnak.datavalue then return end
	if type == "value" then return hasProp[1].mainsnak.datavalue.value end
	if not hasProp[1].mainsnak.datavalue.value.id then return end
	if not mw.wikibase.isValidEntityId(hasProp[1].mainsnak.datavalue.value.id) then return end
	return hasProp[1].mainsnak.datavalue.value.id
end

return p