Importer, Bürokraten, Moderatoren (CommentStreams), flow-bot, Oberflächenadministratoren, Push-Abonnementverwalter, Oversighter, Administratoren, Kampagnenbearbeiter (Hochladeassistent)
853
Bearbeitungen
(Vorbereitung Erweiterung getValuesWithQualifiers) |
K (89 Versionen von wikivoyage:Modul:Wikidata_utilities importiert) |
||
(25 dazwischenliegende Versionen von 2 Benutzern werden nicht angezeigt) | |||
Zeile 4: | Zeile 4: | ||
local WikidataUtilities = { | local WikidataUtilities = { | ||
suite = 'WikidataUtilities', | suite = 'WikidataUtilities', | ||
serial = ' | 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 | ||
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 ) ) | ||
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 | ||
local tp = type( entity ) | |||
return | if tp == 'string' and mw.wikibase.isValidEntityId( entity ) then | ||
return mw.wikibase.getSitelink( entity, globalSiteId ) | |||
else | elseif tp == 'table' and entity.labels then | ||
return | return entity:getSitelink( globalSiteId ) | ||
else | |||
return nil | |||
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 | ||
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 ) | ||
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 | ||
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 | 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 | 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 | ||
-- { item1, item2, ... } : using named qualifiers/references | |||
local array | -- {} : using no qualifiers/references | ||
local function toQualifierTable( key, qualTab, labelFct, ... ) | -- 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 v | local v | ||
if not | if not tab[ key ] then | ||
tab[ key ] = {} | |||
end | end | ||
for | for i = 1, #qualTab do | ||
qual = qualTab[ i ] | |||
if qual.snaktype == 'value' then | if qual.snaktype == 'value' then | ||
v, | v, tab[ key .. '-type' ] = | ||
analyzeDatavalue( qual.datavalue, labelFct, ... ) | analyzeDatavalue( qual.datavalue, labelFct, ... ) | ||
table.insert( | table.insert( tab[ key ], v ) | ||
end | end | ||
end | end | ||
if # | if #tab[ key ] == 0 then | ||
tab[ key ] = nil | |||
tab[ key .. '-type' ] = nil | |||
else | else | ||
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 296: | Zeile 383: | ||
end | end | ||
if type( references ) == 'string' then | if type( references ) == 'string' then | ||
references = { references } | |||
end | end | ||
for | 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 306: | Zeile 395: | ||
if not qualifiers then -- all qualifier properties | if not qualifiers then -- all qualifier properties | ||
for key, qualTab in pairs( statement.qualifiers ) do | for key, qualTab in pairs( statement.qualifiers ) do | ||
toQualifierTable( key, qualTab, labelFct, ... ) | toQualifierTable( array, key, qualTab, labelFct, ... ) | ||
end | end | ||
else -- table of selected qualifier properties | else -- table of selected qualifier properties | ||
for | for j = 1, #qualifiers do | ||
key = qualifiers[ j ] | |||
if statement.qualifiers[ key ] then | if statement.qualifiers[ key ] then | ||
toQualifierTable( key, statement.qualifiers[ key ], labelFct, ... ) | toQualifierTable( array, key, statement.qualifiers[ key ], labelFct, ... ) | ||
end | end | ||
end | end | ||
end | end | ||
end | end | ||
array.references = {} | |||
if statement.references then | if statement.references then | ||
if not references then -- all references | for k = 1, #statement.references do | ||
reference = statement.references[ k ] | |||
if reference and reference.snaks then | |||
table.insert( array.references, {} ) | |||
if not references then -- all references | |||
for key, refTab in pairs( reference.snaks ) do | |||
toQualifierTable( array.references[ #array.references ], | |||
key, refTab ) | |||
end | |||
else -- table of selected references | |||
for j = 1, #references do | |||
key = references[ j ] | |||
if reference.snaks[ key ] then | |||
toQualifierTable( array.references[ #array.references ], | |||
key, reference.snaks[ key ] ) | |||
end | |||
end | |||
end | |||
end | |||
end | end | ||
end | end | ||
table.insert( results, array ) | |||
end | end | ||
Zeile 332: | Zeile 438: | ||
local sort1 = 0 | local sort1 = 0 | ||
local mult = false | local mult = false | ||
for | 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 353: | 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 392: | Zeile 528: | ||
end | end | ||
return result | return result | ||
end | end | ||
return wu | return wu |
Bearbeitungen