Modul:Wikidata utilities: Unterschied zwischen den Versionen
Modul:Wikidata utilities (Quelltext anzeigen)
Version vom 29. Oktober 2021, 15:29 Uhr
, vor 2 Jahrenstronger type checks
Keine Bearbeitungszusammenfassung |
(stronger type checks) |
||
Zeile 4: | Zeile 4: | ||
local WikidataUtilities = { | local WikidataUtilities = { | ||
suite = 'WikidataUtilities', | suite = 'WikidataUtilities', | ||
serial = '2021-10- | serial = '2021-10-29', | ||
item = 65439025 | item = 65439025 | ||
} | } | ||
Zeile 72: | Zeile 72: | ||
return nil | return nil | ||
end | end | ||
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 ) | ||
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 | ||
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 ) | ||
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 ) | ||
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 | return {} | ||
end | end | ||
end | end |