Modul:Wikidata utilities: Unterschied zwischen den Versionen
(+ function fw.getValuesWithQualifiers( entity, p, qualifiers, count )) |
Keine Bearbeitungszusammenfassung |
||
Zeile 179: | Zeile 179: | ||
elseif t == 'globecoordinate' then v = v.latitude .. ',' .. v.longitude | elseif t == 'globecoordinate' then v = v.latitude .. ',' .. v.longitude | ||
elseif t == 'quantity' then v = v.amount .. ',' .. v.unit | elseif t == 'quantity' then v = v.amount .. ',' .. v.unit | ||
elseif t == 'monolingualtext' then v = v.text .. ',' .. v.language | |||
end | end | ||
end | end | ||
Zeile 195: | Zeile 196: | ||
local array, key, value, i, j | local array, key, value, i, j | ||
for i = 1, #statements, 1 do | for i = 1, #statements, 1 do | ||
array = { | array = { value = statements[i].mainsnak.datavalue.value, | ||
['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 |
Version vom 21. März 2018, 08:31 Uhr
Die Dokumentation für dieses Modul kann unter Modul:Wikidata utilities/doc erstellt werden
local fw = {} function fw.checkId( id ) -- only syntax check if ( not id ) or ( type( id ) ~= 'string' ) or ( id == '' ) then return '' end local i = id:upper() if mw.ustring.match( i, '^Q[%d]+$') == nil then if mw.ustring.match( i, '^[%d]+$') ~= nil then -- only number return 'Q' .. i else -- invalid id return '' end end return i end function fw.getEntity( id ) local wrongQualifier = false local entity = nil local i = fw.checkId( id ) if i ~= '' then -- expensive function call entity = mw.wikibase.getEntity( i ) if not entity then i = '' wrongQualifier = true end else if id ~= '' then wrongQualifier = true end end return i, entity, wrongQualifier 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.getStatements( entity, p, count ) local ar = {} if ( not entity ) or ( entity == '' ) then return ar end local statements if type( entity ) == 'string' then statements = mw.wikibase.getBestStatements( entity, p ) else statements = entity:getBestStatements( p ) end 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 ) if ( not entity ) or ( entity == '' ) then return '' end local value if type( entity ) == 'string' then value = getFirstValue( mw.wikibase.getBestStatements( entity, p ) ) else value = getFirstValue( entity:getBestStatements( p ) ) end if value then return value end return '' end function fw.getId( entity, p ) if ( not entity ) or ( entity == '' ) then return '' end local value if type( entity ) == 'string' then value = getFirstValue( mw.wikibase.getBestStatements( entity, p ) ) else value = getFirstValue( entity:getBestStatements( p ) ) end if value then return value.id end return '' end function fw.getValues( entity, p, count ) if ( not entity ) or ( entity == '' ) then return '' end local statements if type( entity ) == 'string' then statements = mw.wikibase.getBestStatements( entity, p ) else statements = entity:getBestStatements( p ) end return getNValues( statements, count or #statements ) end function fw.getValuesWithQualifierIds( entity, p, qualifierP ) local result = {} local statements = fw.getStatements( entity, p, nil ) if #statements == 0 then return result end local value, id, i, j for i = 1, #statements, 1 do value = statements[i].mainsnak.datavalue.value id = 'unknown' 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 return result end local function getValueFromDatavalue( datavalue ) local v = datavalue.value local t = datavalue.type if type( v ) == 'table' then if t == 'wikibase-entityid' then v = v.id elseif t == 'time' then v = v.time elseif t == 'globecoordinate' then v = v.latitude .. ',' .. v.longitude elseif t == 'quantity' then v = v.amount .. ',' .. v.unit elseif t == 'monolingualtext' then v = v.text .. ',' .. v.language end end return v, t end 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, key, value, i, j 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: array of q id - types relations -- limit: maximum levels to analyse if (not p31) or (#p31 == 0) then return 'error' end local aType, i, id, j for i = 1, #p31, 1 do id = p31[i].id aType = list[id] if aType then return aType else j = 0 repeat id = fw.getId( id, 'P279' ) if id ~= '' then aType = list[id] if aType then return aType end end j = j + 1 until (j >= limit) or (id == '') end end return 'error' end return fw