Modul:Wikidata utilities: Unterschied zwischen den Versionen

stronger type checks
Keine Bearbeitungszusammenfassung
(stronger type checks)
Zeile 4: Zeile 4:
local WikidataUtilities = {
local WikidataUtilities = {
suite  = 'WikidataUtilities',
suite  = 'WikidataUtilities',
serial = '2021-10-18',
serial = '2021-10-29',
item  = 65439025
item  = 65439025
}
}
Zeile 72: Zeile 72:
return nil
return nil
end
end
if type( entity ) == 'string' then -- entity is id
local tp = type( entity )
if tp == 'string' and mw.wikibase.isValidEntityId( entity ) then
return isSet( lang ) and mw.wikibase.getLabelByLang( entity, lang )
return isSet( lang ) and mw.wikibase.getLabelByLang( entity, lang )
or mw.wikibase.getLabel( entity )
or mw.wikibase.getLabel( entity )
else -- entity is table
elseif tp == 'table' and entity.labels then
return isSet( lang ) and entity:getLabel( lang ) or entity:getLabel()
return isSet( lang ) and entity:getLabel( lang ) or entity:getLabel()
else
return nil
end
end
end
end
Zeile 84: Zeile 87:
return nil
return nil
end
end
if type( entity ) == 'string' then -- entity is id
local tp = type( entity )
if tp == 'string' and mw.wikibase.isValidEntityId( entity ) then
return mw.wikibase.getSitelink( entity, globalSiteId )
return mw.wikibase.getSitelink( entity, globalSiteId )
else -- entity is table
elseif tp == 'table' and entity.labels then
return entity:getSitelink( globalSiteId )
return entity:getSitelink( globalSiteId )
else
return nil
end
end
end
end
Zeile 103: Zeile 109:
end
end
end
end


-- convert from url to Q id
-- convert from url to Q id
Zeile 132: Zeile 139:


function wu.getBestStatements( entity, p )
function wu.getBestStatements( entity, p )
if type( entity ) == 'string' then
local tp = type( entity )
if tp == 'string' and mw.wikibase.isValidEntityId( entity ) then
return mw.wikibase.getBestStatements( entity, p )
return mw.wikibase.getBestStatements( entity, p )
elseif tp == 'table' and entity.labels then
return entity:getBestStatements( p )
else
else
return entity:getBestStatements( p )
return {}
end
end
end
end
Anonymer Benutzer