Modul:Wikidata utilities: Unterschied zwischen den Versionen

K
89 Versionen von wikivoyage:Modul:Wikidata_utilities importiert
(+references)
K (89 Versionen von wikivoyage:Modul:Wikidata_utilities importiert)
 
(24 dazwischenliegende Versionen von 2 Benutzern werden nicht angezeigt)
Zeile 4: Zeile 4:
local WikidataUtilities = {
local WikidataUtilities = {
suite  = 'WikidataUtilities',
suite  = 'WikidataUtilities',
serial = '2020-10-10',
serial = '2022-09-04',
item  = 65439025
item  = 65439025
}
}
Zeile 10: Zeile 10:
-- i18n
-- i18n
local wd = {
local wd = {
version = 'P348'
version   = 'P348',
retrieved = 'P813',
Gregorian = 'Q12138', -- calendar models
prolepticGregorian = 'Q1985727'
}
}


-- module variable
-- module variable and administration
local wu = {}
local wu = {
moduleInterface = WikidataUtilities
}


-- table storing property ids used
-- table storing property ids used
Zeile 63: 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 79: Zeile 88:
return nil
return nil
end
end
if type( entity ) == 'string' then -- entity is id
local tp = type( entity )
return isSet( globalSiteId ) and mw.wikibase.getSitelink( entity, globalSiteId )
if tp == 'string' and mw.wikibase.isValidEntityId( entity ) then
or mw.wikibase.getSitelink( entity )
return mw.wikibase.getSitelink( entity, globalSiteId )
else -- entity is table
elseif tp == 'table' and entity.labels then
return isSet( globalSiteId ) and entity:getSitelink( globalSiteId )
return entity:getSitelink( globalSiteId )
or entity:getSitelink( globalSiteId )
else
return nil
end
end
end
end
function wu.getSitelinkTable( entity, globalSiteId )
if not isSet( entity ) or not isSet( globalSiteId ) then
return nil
elseif type( entity ) == 'string' then -- entity is id
entity = mw.wikibase.getEntity( entity )
end
if entity and entity.sitelinks then
return entity.sitelinks[ globalSiteId ]
else
return nil
end
end


-- convert from url to Q id
-- convert from url to Q id
Zeile 95: Zeile 119:
return ''
return ''
end
end
end
-- is calendar model a Gregorian calendar?
function wu.isGregorian( t )
return type( t ) == 'string' or t.calendarmodel == wd.Gregorian
or t.calendarmodel == wd.prolepticGregorian
end
-- extract date from time
function wu.getDateFromTime( t )
local model = wd.prolepticGregorian
if type( t ) == 'table' then
model = t.calendarmodel
t = t.time
end
t = t:gsub( '^+', '' ):gsub( 'T.+Z$', '' ):gsub( '-00$', '' )
:gsub( '-00$', '' )
return t, model
end
end


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
Zeile 170: 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 177: 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 219: 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 229: Zeile 278:
id = qualifier.datavalue.value.id
id = qualifier.datavalue.value.id
if id then
if id then
catTable[ qualifierP ] = ''
break
break
end
end
Zeile 251: Zeile 301:
end
end
elseif t == 'quantity' then
elseif t == 'quantity' then
v.amount = v.amount:gsub( '^+', '' )
if tonumber( v.amount ) == 0 then
v.amount = '0'
end
if v.unit == '1' then
if v.unit == '1' then
v = tonumber( v.amount ) or 1
v = tonumber( v.amount ) or 1
else
v.unit = wu.getUnit( v.unit )
end
end
elseif t == 'time' then
elseif t == 'time' then
v = v.time
v.calendarmodel = wu.getUnit( v.calendarmodel )
if wu.isGregorian( v ) then
v = v.time
end
end
end
end
end
Zeile 262: Zeile 321:


-- The following function is an experimental one, not for extensive use
-- The following function is an experimental one, not for extensive use
function wu.getValuesWithQualifiers( entity, p, qualifiers, references, count,
-- for qualifiers, references
labelFct, ... )
--  { item1, item2, ... } : using named qualifiers/references
local array
--  {} : using no qualifiers/references
--  nil : using all qualifiers/references
function wu.getValuesWithQualifiers( entity, p, values, qualifiers, references,
count, labelFct, ... )
local array, qual
local function toQualifierTable( tab, key, qualTab, labelFct, ... )
local function toQualifierTable( tab, key, qualTab, labelFct, ... )
local v
local v
Zeile 270: 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 283: Zeile 347:
catTable[ key ] = ''
catTable[ key ] = ''
end
end
end
local function hasValue( tab, val )
for i = 1, #tab do
if tab[ i ] == val then
return true
end
end
return false
end
end


Zeile 289: Zeile 361:
if #statements == 0 then
if #statements == 0 then
return results
return results
end
local i, v
if type( values ) == 'table' and #values > 0 then
for i = #statements, 1, -1 do
v = statements[ i ].mainsnak.datavalue.value
if type( v ) ~= 'string' then
v = v.id
end
if not isSet( v ) or not hasValue( values, v ) then
table.remove( statements, i )
end
end
if #statements == 0 then
return results
end
end
end
catTable[ p ] = ''
catTable[ p ] = ''
Zeile 299: 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 309: 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 319: 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 328: 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 346: 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
Zeile 367: Zeile 461:


return results
return results
end
-- get lastEdit from reference retrieve date
function wu.getLastedit( lastEdit, statements )
local isBoolean = type( lastEdit ) == 'boolean'
if isBoolean and lastEdit == false then
return lastEdit
end
local le = ''
for _, statement in ipairs( statements ) do
if statement.references then
for _, reference in ipairs( statement.references ) do
if reference[ wd.retrieved ] then
for _, retrieved in ipairs( reference[ wd.retrieved ] ) do
retrieved = wu.getDateFromTime( retrieved )
if retrieved > le then
le = retrieved
end
end
end
end
end
end
if isBoolean then
return ( le ~= '' ) and le or lastEdit
else
return ( le > lastEdit ) and le or lastEdit
end
end
end


Zeile 406: Zeile 528:
end
end
return result
return result
end
-- similar function like Modul:Failsafe
function wu._failsafe( version, moduleInterface )
if type( moduleInterface ) ~= 'table' then
return false
end
local i, parent, v
-- check if version is a frame table
if type( version ) == 'table' and type( version.args ) == 'table' then
v = version.args[ 1 ]
if not v and type( version.getParent ) == 'function' then
parent = version:getParent()
if parent then
v = parent.args[ 1 ]
end
end
version = v
end
-- version should be a string
if type( version ) == 'string' then
-- check if empty string
version = mw.text.trim( version )
if version == '' then
version = nil
end
else
version = nil
end
-- getting version from Wikidata
if version == 'wikidata' then
version = nil
i = moduleInterface.item
if type( i ) == 'number' and i > 0 then
v = wu.getValue( 'Q' .. i, wd.version )
if v ~= '' then
return v
end
end
end
if not moduleInterface.serial then
return false
elseif not version or version <= moduleInterface.serial then
return moduleInterface.serial
else
return false
end
end
-- module administration
function wu.getModuleInterface()
return WikidataUtilities
end
function wu.failsafe( version )
return wu._failsafe( version, WikidataUtilities ) or ''
end
end


return wu
return wu