Modul:Wikidata utilities: Unterschied zwischen den Versionen

maintain categories table
(+getEntityId)
(maintain categories table)
Zeile 1: Zeile 1:
-- documentation
local FastWikidata = {
suite  = 'FastWikidata',
serial = '2020-05-13',
-- item  = 58187612
}
-- module variable
local fw = {}
local fw = {}
-- table storing property ids used
local catTable = {
P0 = ''
}


function fw.getEntity( id )
function fw.getEntity( id )
Zeile 97: Zeile 110:
local function getNValues( statements, count )
local function getNValues( statements, count )
local ar = {}
local ar = {}
if count > #statements then count = #statements end
if count > #statements then
count = #statements
end
if ( #statements == 0 ) or ( count <= 0 ) then
if ( #statements == 0 ) or ( count <= 0 ) then
return ar
return ar
Zeile 130: Zeile 145:


count = count or #statements
count = count or #statements
if count > #statements then count = #statements end
if count > #statements then
count = #statements
end
if ( #statements == 0 ) or ( count <= 0 ) then
if ( #statements == 0 ) or ( count <= 0 ) then
return ar
return ar
Zeile 146: Zeile 163:
end
end


function fw.getValue( entity, p, catArray )
function fw.getValue( entity, p )
local value = ''
local value = ''
if entity and entity ~= '' and p and p ~= '' then
if entity and entity ~= '' and p and p ~= '' then
value = getFirstValue( fw.getBestStatements( entity, p ) )
value = getFirstValue( fw.getBestStatements( entity, p ) )
if value and catArray then
if value then
catArray[ p ] = ''
catTable[ p ] = ''
end
end
value = value or ''
value = value or ''
end
end
if catArray then
return value
return value, catArray
else
return value
end
end
end


function fw.getId( entity, p, catArray )
function fw.getId( entity, p )
local value = ''
local value = ''
if entity and entity ~= '' and p and p ~= '' then
if entity and entity ~= '' and p and p ~= '' then
value = getFirstValue( fw.getBestStatements( entity, p ) )
value = getFirstValue( fw.getBestStatements( entity, p ) )
if value then
if value then
if catArray then catArray[ p ] = '' end
catTable[ p ] = ''
value = value.id
value = value.id
else
else
Zeile 173: Zeile 186:
end
end
end
end
if catArray then
return value
return value, catArray
else
return value
end
end
end


function fw.getValues( entity, p, count, catArray )
function fw.getValues( entity, p, count )
local values = '', statements
local values = ''
if entity and entity ~= '' and p and p ~= '' then
if entity and entity ~= '' and p and p ~= '' then
statements = fw.getBestStatements( entity, p )
local statements = fw.getBestStatements( entity, p )
values = getNValues( statements, count or #statements )
values = getNValues( statements, count or #statements )
if catArray and #values > 0 then catArray[ p ] = '' end
if #values > 0 then
end
catTable[ p ] = ''
if catArray then
end
return values, catArray
else
return values
end
end
return values
end
end


function fw.getValuesByLang( entity, p, count, lang, catArray )
function fw.getValuesByLang( entity, p, count, lang )
local ar = '', statements, i, value
local ar = ''
if entity and entity ~= '' and p and p ~= '' then
if entity and entity ~= '' and p and p ~= '' then
statements = fw.getBestStatements( entity, p )
local statements = fw.getBestStatements( entity, p )
ar = {}
ar = {}
count = count or #statements
count = count or #statements
if #statements > 0 and count > 0 then
if #statements > 0 and count > 0 then
i = 0
local i = 0
local value
repeat
repeat
i = i + 1
i = i + 1
Zeile 212: Zeile 220:
until ( i >= #statements ) or ( #ar >= count )
until ( i >= #statements ) or ( #ar >= count )
end
end
if catArray and #ar > 0 then catArray[ p ] = '' end
if #ar > 0 then
end
catTable[ p ] = ''
if catArray then
end
return ar, catArray
else
return ar
end
end
return ar
end
end


function fw.getValuesWithQualifierIds( entity, p, qualifierP, defaultId, catArray )
function fw.getValuesWithQualifierIds( entity, p, qualifierP, defaultId )
local result = {}, statements, value, id, i, j
local result = {}
if entity and entity ~= '' and p and p ~= '' and qualifierP and qualifierP ~= '' then
if entity and entity ~= '' and p and p ~= '' and qualifierP and qualifierP ~= '' then
statements = fw.getStatements( entity, p, nil )
local statements = fw.getStatements( entity, p, nil )
if #statements > 0 then
if #statements > 0 then
-- defaultId is used if a qualifier is missing
-- defaultId is used if a qualifier is missing
Zeile 231: Zeile 237:
end
end


if catArray then catArray[ p ] = '' end
catTable[ p ] = ''
local id, value
for i = 1, #statements, 1 do
for i = 1, #statements, 1 do
value = statements[ i ].mainsnak.datavalue.value
value = statements[ i ].mainsnak.datavalue.value
Zeile 248: Zeile 255:
end
end
end
end
if catArray then
return result
return result, catArray
else
return result
end
end
end


-- get values array for monolingual text
-- get values array for monolingual text
function fw.getValuesWithLanguages( entity, p, catArray )
function fw.getValuesWithLanguages( entity, p )
local result = {}, statements, hyphen, i, lng, value
local result = {}, statements, hyphen, i, lng, value
if entity and entity ~= '' and p and p ~= '' then
if entity and entity ~= '' and p and p ~= '' then
statements = fw.getStatements( entity, p, nil )
statements = fw.getStatements( entity, p, nil )
if #statements > 0 and statements[ 1 ].mainsnak.datatype == 'monolingualtext' then
if #statements > 0 and statements[ 1 ].mainsnak.datatype == 'monolingualtext' then
if catArray then catArray[ p ] = '' end
catTable[ p ] = ''
for i = 1, #statements, 1 do
for i = 1, #statements, 1 do
value = statements[i].mainsnak.datavalue.value
value = statements[i].mainsnak.datavalue.value
Zeile 269: Zeile 272:
lng = lng:sub( 1, hyphen - 1 )
lng = lng:sub( 1, hyphen - 1 )
end
end
if not result[ lng ] then result[ lng ] = value.text end
if not result[ lng ] then
result[ lng ] = value.text
end
end
end
end
end
end
end
if catArray then
return result
return result, catArray
else
return result
end
end
end


Zeile 285: Zeile 286:
if type( v ) == 'table' then
if type( v ) == 'table' then
-- items which can be reduced to a string
-- items which can be reduced to a string
if t == 'wikibase-entityid' then v = v.id
if t == 'wikibase-entityid' then
elseif t == 'time' then v = v.time
v = v.id
elseif t == 'time' then
v = v.time
end
end
end
end
Zeile 296: Zeile 299:
local result = {}
local result = {}
local statements = fw.getStatements( entity, p, count )
local statements = fw.getStatements( entity, p, count )
if #statements == 0 then return result end
if #statements == 0 then
return result
end


if qualifiers and ( type( qualifiers ) == 'string' ) then
if qualifiers and ( type( qualifiers ) == 'string' ) then
Zeile 302: Zeile 307:
end
end


local array, key, value, i, j
local array
for i = 1, #statements, 1 do
for i = 1, #statements, 1 do
array = { value = statements[i].mainsnak.datavalue.value,
array = { value = statements[i].mainsnak.datavalue.value,
['value-type'] = statements[i].mainsnak.datavalue.type }
['value-type'] = statements[i].mainsnak.datavalue.type }
if statements[i].qualifiers then
if statements[ i ].qualifiers then
if not qualifiers then -- all qualifier properties
if not qualifiers then -- all qualifier properties
for key, value in pairs( statements[i].qualifiers ) do
for key, value in pairs( statements[i].qualifiers ) do
Zeile 322: Zeile 327:
for key, value in pairs( qualifiers ) do
for key, value in pairs( qualifiers ) do
if statements[i].qualifiers[ value ] and
if statements[i].qualifiers[ value ] and
( #statements[i].qualifiers[ value ] > 0 ) then
( #statements[ i ].qualifiers[ value ] > 0 ) then
for j = 1, #statements[i].qualifiers[ value ], 1 do
for j = 1, #statements[i].qualifiers[ value ], 1 do
if statements[i].qualifiers[ value ][ j ].snaktype == 'value' then
if statements[i].qualifiers[ value ][ j ].snaktype == 'value' then
Zeile 339: Zeile 344:
end
end


function fw.typeSearch( p31, list, limit, catArray )
function fw.typeSearch( p31, list, limit )
-- p31: array of Wikidata values
-- p31: array of Wikidata values
-- list: indexed array of q id - types relations
-- list: indexed array of q id - types relations
-- limit: maximum levels to analyse
-- limit: maximum levels to analyse
if not list or not p31 or #p31 == 0 then
if not list or not p31 or #p31 == 0 then
return 'error', catArray
return 'error'
end
end


local function compareIds( ar )
local function compareIds( ar )
local i, t
local t
for i = 1, #ar, 1 do
for i = 1, #ar, 1 do
t = list[ ar[ i ].id ]
t = list[ ar[ i ].id ]
Zeile 357: Zeile 362:
return nil
return nil
end
end
local aType, i, id, ids, j


aType = compareIds( p31 ) -- check p31 ids first, maybe step 2 is not nessary
local aType = compareIds( p31 ) -- check p31 ids first, maybe step 2 is not nessary
if aType then
if aType then
return aType, catArray
return aType
end
end


-- now functions becomes expensive because of multiple fw.getValues calls
-- 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
for i = 1, #p31, 1 do -- step 2: analyse P279 chains of first ids
id = p31[ i ].id -- start id
id = p31[ i ].id -- start id
j = 0
local j = 0
repeat
repeat
ids, catArray = fw.getValues( id, 'P279', nil, catArray )
ids = fw.getValues( id, 'P279', nil )
if #ids > 0 then
if #ids > 0 then
id = ids[ 1 ].id
id = ids[ 1 ].id
aType = compareIds( ids )
aType = compareIds( ids )
if aType then
if aType then
return aType, catArray
return aType
end
end
end
end
Zeile 381: Zeile 386:
end
end


return 'error', catArray
return 'error'
end
end


function fw.getCategories( catArray, formatStr )
function fw.getCategories( catArray, formatStr )
result = ''
local result = ''
if not catArray then return result end
if type( catArray ) == 'string' then
formatStr = catArray
end
if not formatStr or formatStr == '' then
if not formatStr or formatStr == '' then
formatStr = '[[Category:%s]]'
formatStr = '[[Category:%s]]'
end
end
for key, value in pairs( catArray ) do
 
catTable.P0 = nil
for key, value in pairs( catTable ) do
result = result .. string.format( formatStr, key )
result = result .. string.format( formatStr, key )
end
end
Anonymer Benutzer