Modul:Wikidata utilities: Unterschied zwischen den Versionen
(maintain categories table) |
(code check) |
||
| Zeile 3: | Zeile 3: | ||
suite = 'FastWikidata', | suite = 'FastWikidata', | ||
serial = '2020-05-13', | serial = '2020-05-13', | ||
item = 65439025 | |||
} | } | ||
| Zeile 100: | Zeile 100: | ||
for i = 1, #statements, 1 do | for i = 1, #statements, 1 do | ||
if statements[i].mainsnak.snaktype == 'value' then | if statements[ i ].mainsnak.snaktype == 'value' then | ||
return statements[i].mainsnak.datavalue.value | return statements[ i ].mainsnak.datavalue.value | ||
end | end | ||
end | end | ||
| Zeile 113: | Zeile 113: | ||
count = #statements | count = #statements | ||
end | end | ||
if | if #statements == 0 or count <= 0 then | ||
return ar | return ar | ||
end | end | ||
| Zeile 120: | Zeile 120: | ||
repeat | repeat | ||
i = i + 1 | i = i + 1 | ||
if statements[i].mainsnak.snaktype == 'value' then | if statements[ i ].mainsnak.snaktype == 'value' then | ||
table.insert( ar, statements[i].mainsnak.datavalue.value ) | table.insert( ar, statements[ i ].mainsnak.datavalue.value ) | ||
end | end | ||
until | until i >= #statements or #ar >= count | ||
return ar | return ar | ||
| Zeile 138: | Zeile 138: | ||
function fw.getStatements( entity, p, count ) | function fw.getStatements( entity, p, count ) | ||
local ar = {} | local ar = {} | ||
if | if not entity or entity == '' then | ||
return ar | return ar | ||
end | end | ||
| Zeile 148: | Zeile 148: | ||
count = #statements | count = #statements | ||
end | end | ||
if | if #statements == 0 or count <= 0 then | ||
return ar | return ar | ||
end | end | ||
| Zeile 155: | Zeile 155: | ||
repeat | repeat | ||
i = i + 1 | i = i + 1 | ||
if statements[i].mainsnak.snaktype == 'value' then | if statements[ i ].mainsnak.snaktype == 'value' then | ||
table.insert( ar, statements[i] ) | table.insert( ar, statements[ i ] ) | ||
end | end | ||
until | until i >= #statements or #ar >= count | ||
return ar | return ar | ||
| Zeile 169: | Zeile 169: | ||
if value then | if value then | ||
catTable[ p ] = '' | catTable[ p ] = '' | ||
else | |||
value = '' | |||
end | end | ||
end | end | ||
return value | return value | ||
| Zeile 218: | Zeile 219: | ||
end | end | ||
end | end | ||
until | until i >= #statements or #ar >= count | ||
end | end | ||
if #ar > 0 then | if #ar > 0 then | ||
| Zeile 260: | Zeile 261: | ||
-- get values array for monolingual text | -- get values array for monolingual text | ||
function fw.getValuesWithLanguages( entity, p ) | function fw.getValuesWithLanguages( entity, p ) | ||
local result = {} | local result = {} | ||
if entity and entity ~= '' and p and p ~= '' then | if entity and entity ~= '' and p and p ~= '' then | ||
statements = fw.getStatements( entity, p, nil ) | local statements = fw.getStatements( entity, p, nil ) | ||
local hyphen, lng, value | |||
if #statements > 0 and statements[ 1 ].mainsnak.datatype == 'monolingualtext' then | if #statements > 0 and statements[ 1 ].mainsnak.datatype == 'monolingualtext' then | ||
catTable[ p ] = '' | 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 | ||
lng = value.language | lng = value.language | ||
hyphen = lng:find( '-' ) | hyphen = lng:find( '-' ) | ||
| Zeile 303: | Zeile 305: | ||
end | end | ||
if qualifiers and | if qualifiers and type( qualifiers ) == 'string' then | ||
qualifiers = { qualifiers } | qualifiers = { qualifiers } | ||
end | end | ||
| Zeile 309: | Zeile 311: | ||
local array | 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 | ||
if #value > 0 then | if #value > 0 then | ||
for j = 1, #value, 1 do | for j = 1, #value, 1 do | ||
| Zeile 326: | Zeile 328: | ||
else -- table of selected qualifier properties | else -- table of selected qualifier properties | ||
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 | |||
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 | ||
array[ value ], array[ value .. '-type' ] = | array[ value ], array[ value .. '-type' ] = | ||
getValueFromgetValueFromDatavalue( statements[i].qualifiers[ value ][ j ].datavalue ) | getValueFromgetValueFromDatavalue( statements[ i ].qualifiers[ value ][ j ].datavalue ) | ||
break | break | ||
end | end | ||
| Zeile 389: | Zeile 391: | ||
end | end | ||
function fw.getCategories( | function fw.getCategories( formatStr ) | ||
local result = '' | local result = '' | ||
if not formatStr or formatStr == '' then | if not formatStr or formatStr == '' then | ||
formatStr = '[[Category:%s]]' | formatStr = '[[Category:%s]]' | ||
Version vom 13. Mai 2020, 11:05 Uhr
Die Dokumentation für dieses Modul kann unter Modul:Wikidata utilities/doc erstellt werden
-- documentation
local FastWikidata = {
suite = 'FastWikidata',
serial = '2020-05-13',
item = 65439025
}
-- module variable
local fw = {}
-- table storing property ids used
local catTable = {
P0 = ''
}
function fw.getEntity( id )
local wrongQualifier = false
local entity = nil
if not id or id == '' then
return '', entity, wrongQualifier
end
if mw.wikibase.isValidEntityId( id ) then
-- expensive function call
-- redirect ids marked false, too
entity = mw.wikibase.getEntity( id )
end
if not entity then
id = ''
wrongQualifier = true
end
return id, entity, wrongQualifier
end
function fw.getEntityId( id )
local wrongQualifier = false
local entity = nil
if not id or id == '' then
return '', entity, wrongQualifier
end
if mw.wikibase.isValidEntityId( id ) and mw.wikibase.entityExists( id ) then
-- expensive function call
-- redirect ids marked false, too
entity = id
end
if not entity then
id = ''
wrongQualifier = true
end
return id, entity, wrongQualifier
end
function fw.getLabel( entity, lang )
local isString = type( entity ) == 'string'
if not entity or ( isString and entity == '' ) then
return nil
end
if isString then -- entity is id
if lang and lang ~= '' then
return mw.wikibase.getLabelByLang( entity, lang )
else
return mw.wikibase.getLabel( entity )
end
else -- entity is table
if lang and lang ~= '' then
return entity:getLabel( lang )
else
return entity:getLabel()
end
end
end
function fw.getSitelink( entity, globalSiteId )
local isString = type( entity ) == 'string'
if not entity or ( isString and entity == '' ) then
return nil
end
if isString then -- entity is id
if globalSiteId and globalSiteId ~= '' then
return mw.wikibase.getSitelink( entity, globalSiteId )
else
return mw.wikibase.getSitelink( entity )
end
else -- entity is table
if globalSiteId and globalSiteId ~= '' then
return entity:getSitelink( globalSiteId )
else
return entity:getSitelink( )
end
end
end
local function getFirstValue( statements )
if #statements == 0 then
return nil
end
for i = 1, #statements, 1 do
if statements[ i ].mainsnak.snaktype == 'value' then
return statements[ i ].mainsnak.datavalue.value
end
end
return nil
end
local function getNValues( statements, count )
local ar = {}
if count > #statements then
count = #statements
end
if #statements == 0 or count <= 0 then
return ar
end
local i = 0
repeat
i = i + 1
if statements[ i ].mainsnak.snaktype == 'value' then
table.insert( ar, statements[ i ].mainsnak.datavalue.value )
end
until i >= #statements or #ar >= count
return ar
end
function fw.getBestStatements( entity, p )
if type( entity ) == 'string' then
return mw.wikibase.getBestStatements( entity, p )
else
return entity:getBestStatements( p )
end
end
function fw.getStatements( entity, p, count )
local ar = {}
if not entity or entity == '' then
return ar
end
local statements = fw.getBestStatements( entity, p )
count = count or #statements
if count > #statements then
count = #statements
end
if #statements == 0 or count <= 0 then
return ar
end
local i = 0
repeat
i = i + 1
if statements[ i ].mainsnak.snaktype == 'value' then
table.insert( ar, statements[ i ] )
end
until i >= #statements or #ar >= count
return ar
end
function fw.getValue( entity, p )
local value = ''
if entity and entity ~= '' and p and p ~= '' then
value = getFirstValue( fw.getBestStatements( entity, p ) )
if value then
catTable[ p ] = ''
else
value = ''
end
end
return value
end
function fw.getId( entity, p )
local value = ''
if entity and entity ~= '' and p and p ~= '' then
value = getFirstValue( fw.getBestStatements( entity, p ) )
if value then
catTable[ p ] = ''
value = value.id
else
value = ''
end
end
return value
end
function fw.getValues( entity, p, count )
local values = ''
if entity and entity ~= '' and p and p ~= '' then
local statements = fw.getBestStatements( entity, p )
values = getNValues( statements, count or #statements )
if #values > 0 then
catTable[ p ] = ''
end
end
return values
end
function fw.getValuesByLang( entity, p, count, lang )
local ar = ''
if entity and entity ~= '' and p and p ~= '' then
local statements = fw.getBestStatements( entity, p )
ar = {}
count = count or #statements
if #statements > 0 and count > 0 then
local i = 0
local value
repeat
i = i + 1
if statements[ i ].mainsnak.snaktype == 'value' then
value = statements[ i ].mainsnak.datavalue.value
if value.language and lang == value.language then
table.insert( ar, statements[ i ].mainsnak.datavalue.value.text )
end
end
until i >= #statements or #ar >= count
end
if #ar > 0 then
catTable[ p ] = ''
end
end
return ar
end
function fw.getValuesWithQualifierIds( entity, p, qualifierP, defaultId )
local result = {}
if entity and entity ~= '' and p and p ~= '' and qualifierP and qualifierP ~= '' then
local statements = fw.getStatements( entity, p, nil )
if #statements > 0 then
-- defaultId is used if a qualifier is missing
if not defaultId or defaultId == '' or type( defaultId ) ~= 'string' then
defaultId = 'unknown'
end
catTable[ p ] = ''
local id, value
for i = 1, #statements, 1 do
value = statements[ i ].mainsnak.datavalue.value
id = defaultId
if statements[ i ].qualifiers and statements[ i ].qualifiers[ qualifierP ]
and ( #statements[ i ].qualifiers[ qualifierP ] > 0 ) then
for j = 1, #statements[ i ].qualifiers[ qualifierP ], 1 do
if statements[ i ].qualifiers[ qualifierP ][ j ].snaktype == 'value' then
id = statements[ i ].qualifiers[ qualifierP ][ j ].datavalue.value.id
break
end
end
end
result[ id ] = value
end
end
end
return result
end
-- get values array for monolingual text
function fw.getValuesWithLanguages( entity, p )
local result = {}
if entity and entity ~= '' and p and p ~= '' then
local statements = fw.getStatements( entity, p, nil )
local hyphen, lng, value
if #statements > 0 and statements[ 1 ].mainsnak.datatype == 'monolingualtext' then
catTable[ p ] = ''
for i = 1, #statements, 1 do
value = statements[ i ].mainsnak.datavalue.value
lng = value.language
hyphen = lng:find( '-' )
if hyphen then
lng = lng:sub( 1, hyphen - 1 )
end
if not result[ lng ] then
result[ lng ] = value.text
end
end
end
end
return result
end
local function getValueFromDatavalue( datavalue )
local v = datavalue.value
local t = datavalue.type
if type( v ) == 'table' then
-- items which can be reduced to a string
if t == 'wikibase-entityid' then
v = v.id
elseif t == 'time' then
v = v.time
end
end
return v, t
end
-- The following function is an experimental one, not for extensive use
function fw.getValuesWithQualifiers( entity, p, qualifiers, count )
local result = {}
local statements = fw.getStatements( entity, p, count )
if #statements == 0 then
return result
end
if qualifiers and type( qualifiers ) == 'string' then
qualifiers = { qualifiers }
end
local array
for i = 1, #statements, 1 do
array = { value = statements[ i ].mainsnak.datavalue.value,
[ 'value-type' ] = statements[ i ].mainsnak.datavalue.type }
if statements[ i ].qualifiers then
if not qualifiers then -- all qualifier properties
for key, value in pairs( statements[ i ].qualifiers ) do
if #value > 0 then
for j = 1, #value, 1 do
if value[ j ].snaktype == 'value' then
array[ key ], array[ key .. '-type' ] =
getValueFromDatavalue( value[ j ].datavalue )
break
end
end
end
end
else -- table of selected qualifier properties
for key, value in pairs( qualifiers ) do
if statements[ i ].qualifiers[ value ] and
#statements[ i ].qualifiers[ value ] > 0 then
for j = 1, #statements[ i ].qualifiers[ value ], 1 do
if statements[ i ].qualifiers[ value ][ j ].snaktype == 'value' then
array[ value ], array[ value .. '-type' ] =
getValueFromgetValueFromDatavalue( statements[ i ].qualifiers[ value ][ j ].datavalue )
break
end
end
end
end
end
end
table.insert( result, array )
end
return result
end
function fw.typeSearch( p31, list, limit )
-- p31: array of Wikidata values
-- list: indexed array of q id - types relations
-- limit: maximum levels to analyse
if not list or not p31 or #p31 == 0 then
return 'error'
end
local function compareIds( ar )
local t
for i = 1, #ar, 1 do
t = list[ ar[ i ].id ]
if t then
return t
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
-- 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
function fw.getCategories( formatStr )
local result = ''
if not formatStr or formatStr == '' then
formatStr = '[[Category:%s]]'
end
catTable.P0 = nil
for key, value in pairs( catTable ) do
result = result .. string.format( formatStr, key )
end
return result
end
return fw