Modul:Wikidata utilities: Unterschied zwischen den Versionen

(Anfang vereinfachung)
Keine Bearbeitungszusammenfassung
Zeile 1: Zeile 1:
local fw = {}
local fw = {}


function fw.checkId( id )
function fw.checkId( id ) -- only syntax check
if ( not id ) or ( id == '' ) then return '' end
if ( not id ) or ( type( id ) ~= 'string' ) or ( id == '' ) then
return ''
end
local i = id:upper()
local i = id:upper()
if mw.ustring.match( i, '^Q[%d]+$') == nil then
if mw.ustring.match( i, '^Q[%d]+$') == nil then
Zeile 12: Zeile 14:
end
end
return i
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
end


Zeile 31: Zeile 51:
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
end
end
Zeile 40: Zeile 60:
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
until ( i >= #statements ) or ( #ar >= count )
return ar
end
function fw.getNStatements( 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
local ar = {}
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
end
until ( i >= #statements ) or ( #ar >= count )
until ( i >= #statements ) or ( #ar >= count )
Zeile 183: Zeile 233:
j = 0
j = 0
repeat
repeat
id = fw.getWDid( id, 'P279' )
id = fw.getId( id, 'P279' )
if id ~= '' then
if id ~= '' then
aType = list[id]
aType = list[id]