Importer, Bürokraten, Moderatoren (CommentStreams), Strukturierte-Diskussionen-Bots, Oberflächenadministratoren, Push-Abonnementverwalter, Oversighter, Administratoren, Kampagnenbearbeiter (Hochladeassistent)
855
Bearbeitungen
(+labelFct) |
K (89 Versionen von wikivoyage:Modul:Wikidata_utilities importiert) |
||
| (29 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 ) | |||
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 | |||
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 | |||
function wu.getUnit( unit ) | |||
if isSet( unit ) and type( unit ) == 'string' then | |||
return unit:gsub( 'https?://www.wikidata.org/entity/', '' ) | |||
else | |||
return '' | |||
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 112: | Zeile 166: | ||
i = i + 1 | i = i + 1 | ||
if statements[ i ].mainsnak.snaktype == 'value' then | if statements[ i ].mainsnak.snaktype == 'value' then | ||
if statements[ i ].mainsnak.datatype == 'quantity' then | |||
statements[ i ].mainsnak.datavalue.value.amount = | |||
statements[ i ].mainsnak.datavalue.value.amount:gsub( '^+', '' ) | |||
statements[ i ].mainsnak.datavalue.value.unit = wu.getUnit( | |||
statements[ i ].mainsnak.datavalue.value.unit ) | |||
end | |||
table.insert( ar, statements[ i ] ) | table.insert( ar, statements[ i ] ) | ||
end | end | ||
| Zeile 155: | 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 162: | 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 204: | 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 214: | 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 236: | 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 | else | ||
v. | 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 250: | 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, count, labelFct, ... ) | -- for qualifiers, references | ||
local array | -- { item1, item2, ... } : using named qualifiers/references | ||
local function toQualifierTable( key, qualTab, labelFct, ... ) | -- {} : 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 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 276: | 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 281: | Zeile 381: | ||
if type( qualifiers ) == 'string' then | if type( qualifiers ) == 'string' then | ||
qualifiers = { qualifiers } | qualifiers = { qualifiers } | ||
end | |||
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 } | ||
if statement.qualifiers then | if statement.qualifiers then | ||
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 | |||
array.references = {} | |||
if statement.references then | |||
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 | ||
end | end | ||
table.insert( results, array ) | table.insert( results, array ) | ||
end | end | ||
| Zeile 305: | Zeile 437: | ||
local helper = {} | local helper = {} | ||
local sort1 = 0 | local sort1 = 0 | ||
for | local mult = false | ||
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 | ||
mult = true | |||
else | else | ||
sort1 = sort1 + 1 | sort1 = sort1 + 1 | ||
| Zeile 315: | Zeile 451: | ||
result.sort2 = helper[ result.value ].sort2 | result.sort2 = helper[ result.value ].sort2 | ||
end | end | ||
if #results > 2 then | if sort1 > 1 and mult and #results > 2 then | ||
table.sort( results, | table.sort( results, | ||
function( a, b ) | function( a, b ) | ||
| Zeile 325: | 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 346: | Zeile 510: | ||
function wu.addProperty( p ) | function wu.addProperty( p ) | ||
catTable[ p ] = '' | catTable[ p ] = '' | ||
end | |||
function wu.removeProperty( p ) | |||
catTable[ p ] = nil | |||
end | end | ||
| Zeile 360: | Zeile 528: | ||
end | end | ||
return result | return result | ||
end | end | ||
return wu | return wu | ||