Modul:Wikidata utilities: Unterschied zwischen den Versionen

K
89 Versionen von wikivoyage:Modul:Wikidata_utilities importiert
Keine Bearbeitungszusammenfassung
K (89 Versionen von wikivoyage:Modul:Wikidata_utilities importiert)
 
(11 dazwischenliegende Versionen von 2 Benutzern werden nicht angezeigt)
Zeile 4: Zeile 4:
local WikidataUtilities = {
local WikidataUtilities = {
suite  = 'WikidataUtilities',
suite  = 'WikidataUtilities',
serial = '2021-10-18',
serial = '2022-09-04',
item  = 65439025
item  = 65439025
}
}
Zeile 68: Zeile 68:
end
end


function wu.getLabel( entity, lang )
function wu.getLabel( entity, lang, noFallback )
if not isSet( entity ) then
if not isSet( entity ) then
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 ( not noFallback and 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 ( not noFallback and entity:getLabel() )
else
return nil
end
end
end
end
Zeile 84: Zeile 88:
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 110:
end
end
end
end


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


function wu.getBestStatements( entity, p )
function wu.getBestStatements( entity, p )
if not entity or not isSet( p ) then
local tp = type( entity )
return {}
if tp == 'string' and mw.wikibase.isValidEntityId( entity ) then
elseif type( entity ) == 'string' 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
Zeile 206: Zeile 215:
function wu.getValuesByLang( entity, p, count, lang )
function wu.getValuesByLang( entity, p, count, lang )
local ar = {}
local ar = {}
local statements = wu.getStatements( entity, p, count )
local statements = wu.getStatements( entity, p )
if #statements > 0 then
if #statements > 0 then
local value
local value
Zeile 213: Zeile 222:
if value.language and lang == value.language then
if value.language and lang == value.language then
table.insert( ar, value.text )
table.insert( ar, value.text )
end
if count and #ar >= count then
break
end
end
end
end
Zeile 255: Zeile 267:
if #statements > 0 then
if #statements > 0 then
catTable[ p ] = ''
catTable[ p ] = ''
local id, value
local id, statement, value
for _, statement in ipairs( statements ) do
for i = 1, #statements do
statement = statements[ i ]
-- defaultId is used if a qualifier is missing
-- defaultId is used if a qualifier is missing
id = defaultId
id = defaultId
Zeile 314: Zeile 327:
function wu.getValuesWithQualifiers( entity, p, values, qualifiers, references,
function wu.getValuesWithQualifiers( entity, p, values, qualifiers, references,
count, labelFct, ... )
count, labelFct, ... )
local array
local array, qual
local function toQualifierTable( tab, key, qualTab, labelFct, ... )
local function toQualifierTable( tab, key, qualTab, labelFct, ... )
local v
local v
Zeile 320: Zeile 333:
tab[ key ] = {}
tab[ key ] = {}
end
end
for _, qual in ipairs( qualTab ) do
for i = 1, #qualTab do
qual = qualTab[ i ]
if qual.snaktype == 'value' then
if qual.snaktype == 'value' then
v, tab[ key .. '-type' ] =
v, tab[ key .. '-type' ] =
Zeile 335: Zeile 349:
end
end
local function hasValue( tab, val )
local function hasValue( tab, val )
for _, value in ipairs( tab ) do
for i = 1, #tab do
if value == val then
if tab[ i ] == val then
return true
return true
end
end
Zeile 372: Zeile 386:
end
end


for _, statement in ipairs( statements ) do
local key, reference, statement
for i = 1, #statements do
statement = statements[ i ]
array = { value = analyzeDatavalue( statement.mainsnak.datavalue, labelFct, ... ),
array = { value = analyzeDatavalue( statement.mainsnak.datavalue, labelFct, ... ),
[ 'value-type' ] = statement.mainsnak.datavalue.type }
[ 'value-type' ] = statement.mainsnak.datavalue.type }
Zeile 382: Zeile 398:
end
end
else -- table of selected qualifier properties
else -- table of selected qualifier properties
for _, key in ipairs( qualifiers ) do
for j = 1, #qualifiers do
key = qualifiers[ j ]
if statement.qualifiers[ key ] then
if statement.qualifiers[ key ] then
toQualifierTable( array, key, statement.qualifiers[ key ], labelFct, ... )
toQualifierTable( array, key, statement.qualifiers[ key ], labelFct, ... )
Zeile 392: Zeile 409:
array.references = {}
array.references = {}
if statement.references then
if statement.references then
for i, reference in ipairs( statement.references ) do
for k = 1, #statement.references do
reference = statement.references[ k ]
if reference and reference.snaks then
if reference and reference.snaks then
table.insert( array.references, {} )
table.insert( array.references, {} )
Zeile 401: Zeile 419:
end
end
else -- table of selected references
else -- table of selected references
for _, key in ipairs( references ) do
for j = 1, #references do
key = references[ j ]
if reference.snaks[ key ] then
if reference.snaks[ key ] then
toQualifierTable( array.references[ #array.references ],
toQualifierTable( array.references[ #array.references ],
Zeile 419: Zeile 438:
local sort1 = 0
local sort1 = 0
local mult = false
local mult = false
for _, result in ipairs( results ) do
local result
for i = 1, #results do
result = results[ i ]
if helper[ result.value ] then
if helper[ result.value ] then
helper[ result.value ].sort2 = helper[ result.value ].sort2 + 1
helper[ result.value ].sort2 = helper[ result.value ].sort2 + 1