Module:Wikibase

La bibliothèque libre.
Sauter à la navigation Sauter à la recherche
Documentation du module [voir] [modifier] [purger]
La documentation de ce module Scribunto écrit en Lua est incluse depuis sa sous-page de documentation.

Le module Wikibase (un mot-valise formé sur la technologie wiki) permet de lire depuis un module Lua des données provenant d'une base de données, venant de Wikidata.

Utilisation[modifier]

local export = {}

local _upper = string.upper;
local _sub   = string.sub;

local _t = mw and mw.title;
local _t_getCurrentTitle = _t and _t.getCurrentTitle;

local _wb = mw.wikibase; --assert( _wb, 'no mw.wikibase' ); // ce test n'est pas activé!
local _wb_getEntity                 = _wb and _wb.getEntity;
local _wb_getEntityObject           = _wb and _wb.getEntityObject;
local _wb_getEntityIdForCurrentPage = _wb and _wb.getEntityIdForCurrentPage;
local _wb_label                     = _wb and _wb.label;
local _wb_description               = _wb and _wb.description;
local _wb_sitelink                  = _wb and _wb.sitelink;
local _wb_getSitelink               = _wb and _wb.getSitelink;

-- Return the item ID of the item linked to the current page.
-- frame: unused (implicit)
function _id( frame )
	-- return _wb and _wb.getEntityIdForCurrentPage()
	local object = _wb_getEntityObject and _wb_getEntityObject();
	return object and object.id;
end
export.id = _id;

-- Returns the title of the Wikidata page for the entity
function _title( frame )
	local args = frame.args;
	local id = args.id or args[1] or _id( frame )
	if id then
		id = _upper(id)
		local id1 = _sub(id, 1, 1)
		if id1 == 'Q' then
			return 'd:' .. id
		elseif id1 == 'P' then
			return 'd:Property:' .. id
		else
			return '<span class="error">Unknown entity id: ' .. id .. '</span>'
		end
	end
end
export.title = _title;

-- Return the label of a given data item, or of connected page
-- if no argument is provided to this method.
function _label( frame )
	local args = frame.args;
	local id = args.id or args[1] or _id( frame );
	return _wb_label and _wb_label( id );
end
export.label = _label;

-- Return the description of a given data item, or of connected page
-- if no argument is provided to this method.
function _description( frame )
	local args = frame.args;
	local id = args.id or args[1] or _id( frame );
	return _wb_description and _wb_description( id );
end
export.description = _description;

-- Return the local page about a given data item, or of connected page
-- if id is not specified.
function _page( frame )
	local args = frame.args;
	local id = args.id or args[1] or _id( frame );
	return _wb_sitelink and _wb_sitelink( id );
end
export.page = _page;

-- Returns a sitelink of a given data item.
-- @param string|nil id the item id (default: the item linked to the current page)
-- @param string|nil site the site (default: the current site)
function _sitelink(frame)
	local args = frame.args;
	local id   = args.id   or args[1] or _id( frame );
	local site = args.site or args[2];
	if site then
		assert( _id );
		local  entity = _wb_getEntity and _wb_getEntity( id );
		return entity and _wb_getSitelink and _wb_getSitelink( entity, site );
	elseif id then
		return _wb_sitelink and _wb_sitelink( id );
	else
		return _t_getCurrentTitle and _t_getCurrentTitle().fullText;
	end
end
export.sitelink = _sitelink;

return export