Importer, Bürokraten, Moderatoren (CommentStreams), flow-bot, Oberflächenadministratoren, Push-Abonnementverwalter, Oversighter, Administratoren, Kampagnenbearbeiter (Hochladeassistent)
853
Bearbeitungen
(analyzeDatavalue modifiziert; +wu.isGregorian( t ); +wu.getDateFromTime( t )) |
K (89 Versionen von wikivoyage:Modul:Wikidata_utilities importiert) |
||
(23 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' | ||
} | } | ||
-- module variable | -- module variable and administration | ||
local wu = {} | local wu = { | ||
moduleInterface = WikidataUtilities | |||
} | |||
-- table storing property ids used | -- table storing property ids used | ||
Zeile 65: | 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 81: | 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 ) | |||
elseif tp == 'table' and entity.labels then | |||
return | return 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 118: | 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 190: | 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 197: | 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 239: | 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 249: | 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 295: | Zeile 325: | ||
-- {} : using no qualifiers/references | -- {} : using no qualifiers/references | ||
-- nil : using all qualifiers/references | -- nil : using all qualifiers/references | ||
function wu.getValuesWithQualifiers( entity, p, qualifiers, references, count, | function wu.getValuesWithQualifiers( entity, p, values, qualifiers, references, | ||
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 303: | 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 316: | 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 322: | 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 332: | 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 342: | 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 352: | 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 361: | 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 379: | 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 400: | 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 439: | Zeile 528: | ||
end | end | ||
return result | return result | ||
end | end | ||
return wu | return wu |
Bearbeitungen