Importer, Bürokraten, Moderatoren (CommentStreams), flow-bot, Oberflächenadministratoren, Push-Abonnementverwalter, Oversighter, Administratoren, Kampagnenbearbeiter (Hochladeassistent)
853
Bearbeitungen
(moduleInterface) |
K (89 Versionen von wikivoyage:Modul:Wikidata_utilities importiert) |
||
(15 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 | Gregorian = 'Q12138', -- calendar models | ||
prolepticGregorian = 'Q1985727' | prolepticGregorian = 'Q1985727' | ||
Zeile 67: | 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 83: | Zeile 88: | ||
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 102: | Zeile 110: | ||
end | end | ||
end | end | ||
-- convert from url to Q id | -- convert from url to Q id | ||
Zeile 131: | Zeile 140: | ||
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 203: | 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 210: | 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 252: | 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 311: | 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 317: | Zeile 333: | ||
tab[ key ] = {} | tab[ key ] = {} | ||
end | end | ||
for | 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 332: | Zeile 349: | ||
end | end | ||
local function hasValue( tab, val ) | local function hasValue( tab, val ) | ||
for | for i = 1, #tab do | ||
if | if tab[ i ] == val then | ||
return true | return true | ||
end | end | ||
Zeile 369: | Zeile 386: | ||
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 379: | Zeile 398: | ||
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( array, key, statement.qualifiers[ key ], labelFct, ... ) | toQualifierTable( array, key, statement.qualifiers[ key ], labelFct, ... ) | ||
Zeile 389: | Zeile 409: | ||
array.references = {} | array.references = {} | ||
if statement.references then | if statement.references then | ||
for | 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 398: | Zeile 419: | ||
end | end | ||
else -- table of selected references | else -- table of selected references | ||
for | 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 416: | 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 437: | 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 | ||
Bearbeitungen