Jump to content

Module:Sandbox/Certes

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Certes (talk | contribs) at 17:56, 17 February 2018 (Returns an arbitrary Wikidata property of an arbitrary page of the local Wikipedia). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
local p = {}

-- Entry point for Lua callers
-- Returns an arbitrary Wikidata property of an arbitrary page of the local Wikipedia
function p._page_property(pagename, property)

	-- bypass redirect
	local mRedirect = require('Module:Redirect')
	realpage = mRedirect.getTarget(pagename) or pagename

	-- get entity id for page, e.g. Q123
	local entity_id = mw.wikibase.getEntityIdForTitle(pagename)
	if not entity_id then return nil end

	-- entity data for this id
	local entity = mw.wikibase.getEntity(entity_id)
	if not entity then return nil end

	-- get relevant property as a table { label=..., value=... }
	local values = entity:formatPropertyValues(property)
	if not values then return nil end

	-- return relevant property value
	return values.value
end

-- Entry point for template callers using #invoke:
function p.page_property(frame)
	-- args = { 1 = page name, 2 = property }
	-- 2 is a label e.g. "father" or an id e.g. "P42"
	local args = frame.args -- from calling module
	local pargs = frame:getParent().args -- from template
	return p._page_property(args[1] or pargs[1], args[2] or pargs[2])
end

return p