Zum Inhalt springen

Modul:Wikidata utilities: Unterschied zwischen den Versionen

- typeSearch (-> MarkerBase), + getAliases
(code check)
(- typeSearch (-> MarkerBase), + getAliases)
Zeile 2: Zeile 2:
local FastWikidata = {
local FastWikidata = {
suite  = 'FastWikidata',
suite  = 'FastWikidata',
serial = '2020-05-13',
serial = '2020-06-26',
item  = 65439025
item  = 65439025
}
}
Zeile 346: Zeile 346:
end
end


function fw.typeSearch( p31, list, limit )
function fw.getAliases( entity, lang )
-- p31: array of Wikidata values
if type( entity ) == 'string' then -- is Q id
-- list: indexed array of q id - types relations
entity = mw.wikibase.getEntity( entity )
-- limit: maximum levels to analyse
if not list or not p31 or #p31 == 0 then
return 'error'
end
end
 
local aliases = {}
local function compareIds( ar )
if entity and entity.aliases and entity.aliases[ lang ] then
local t
for _, alias in pairs( entity.aliases[ lang ] ) do
for i = 1, #ar, 1 do
table.insert( aliases, alias.value )
t = list[ ar[ i ].id ]
if t then
return t
end
end
end
return nil
end
local aType = compareIds( p31 ) -- check p31 ids first, maybe step 2 is not nessary
if aType then
return aType
end
end
 
return aliases
-- now functions becomes expensive because of multiple fw.getValues calls
local id, ids
for i = 1, #p31, 1 do -- step 2: analyse P279 chains of first ids
id = p31[ i ].id -- start id
local j = 0
repeat
ids = fw.getValues( id, 'P279', nil )
if #ids > 0 then
id = ids[ 1 ].id
aType = compareIds( ids )
if aType then
return aType
end
end
j = j + 1
until j >= limit or #ids == 0
end
 
return 'error'
end
end


Anonymer Benutzer