« Module:Wikibase » : différence entre les versions

Contenu supprimé Contenu ajouté
Aucun résumé des modifications
restauration : merci de faire les tests dans un module de test, et ne changer le module principal que quand tout est fonctionnel, et de ne pas oublier de migrer les modèles qui dépendent du module
Ligne 1 :
--Utility functions
local export = {}
function getEntity(frame)
 
if frame.args.id then
local _upper = string.upper;
return mw.wikibase.getEntity(frame.args.id)
local _sub = string.sub;
else
return mw.wikibase.getEntity()
end
end
 
local _tp = mw and mw.title;{}
local _t_getCurrentTitle = _t and _t.getCurrentTitle;
 
-- Returns the item ID of the item linked to the current page.
local _wb = mw.wikibase; --assert( _wb, 'no mw.wikibase' ); // ce test n'est pas activé!
function p.id(frame)
local _wb_getEntity = _wb and _wb.getEntity;
return mw.wikibase.getEntityIdForCurrentPage()
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 thea titlesitelink of thea Wikidatagiven pagedata for the entityitem.
-- @param string|nil id the item id (default: the item linked to the current page)
function _title( frame )
-- @param string|nil site the site (default: the current site)
local args = frame.arg;
function p.sitelink(frame)
local id = args.id or args[1] or _id( frame )
if idframe.args.site then
idlocal entity = _uppergetEntity(idframe)
if entity then
local id1 = _sub(id, 1, 1)
return entity:getSitelink(frame.args.site)
if id1 == 'Q' then
end
return 'd:' .. id
else
elseif id1 == 'P' then
if frame.args.id then
return 'd:Property:' .. id
return mw.wikibase.sitelink(frame.args.id)
else
return mw.title.getCurrentTitle().fullText
return '<span class="error">Unknown entity id: ' .. id .. '</span>'
end
end
end
export.title = _title;
 
-- Returns a description for the current page or from a Wikibase ID
-- Return the label of a given data item, or of connected page
function p.label(frame)
-- if no argument is provided to this method.
if frame.args.id then
function _label( frame )
local args = return mw.wikibase.label(frame.args;.id)
else
local id = args.id or args[1] or _id( frame );
return _wb_label and _wb_labelmw.wikibase.label( id );
end
end
export.label = _label;
 
-- ReturnReturns thea description offor athe givencurrent data item,page or offrom connecteda pageWikibase ID
function p.description(frame)
-- if no argument is provided to this method.
if frame.args.id then
function _description( frame )
local args = return mw.wikibase.description(frame.args;.id)
else
local id = args.id or args[1] or _id( frame );
return mw.wikibase.description()
return _wb_description and _wb_description( id );
end
end
export.description = _description;
 
-- ReturnReturns the localtitle pageof aboutthe awiki given data item, orpage of connectedthe pageentity
function p.title(frame)
-- if id is not specified.
id = frame.args.id or mw.wikibase.getEntityIdForCurrentPage()
function _page( frame )
if not id then
local args = frame.args;
return nil
local id = args.id or args[1] or _id( frame );
end
return _wb_sitelink and _wb_sitelink( id );
id = string.upper(id)
end
if string.sub(id, 1, 1) == 'Q' then
export.page = _page;
return 'd:' .. id
 
elseif string.sub(id, 1, 1) == 'P' then
-- Returns a sitelink of a given data item.
return 'd:Property:' .. id
-- @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 '<span class="error">Unknown entity id: ' .. id .. '</span>'
return _t_getCurrentTitle and _t_getCurrentTitle().fullText;
end
end
export.sitelink = _sitelink;
 
return exportp