Modul:Wikidata utilities: Unterschied zwischen den Versionen
(erweitert) |
(Anfang vereinfachung) |
||
| Zeile 2: | Zeile 2: | ||
function fw.checkId( id ) | function fw.checkId( id ) | ||
if (not id) or (id == '') then return '' end | if ( not id ) 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 44: | Zeile 44: | ||
return ar | 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 | end | ||
| Zeile 96: | Zeile 132: | ||
return '' | 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 | end | ||
| Zeile 116: | Zeile 167: | ||
end | end | ||
function fw.typeSearch( p31, | function fw.typeSearch( p31, list, limit ) | ||
-- p31: array of Wikidata values | -- p31: array of Wikidata values | ||
-- | -- list: array of q id - types relations | ||
-- limit: maximum levels to analyse | -- limit: maximum levels to analyse | ||
if (not p31) or (#p31 == 0) then return 'error' end | if (not p31) or (#p31 == 0) then return 'error' end | ||
| Zeile 125: | Zeile 176: | ||
for i = 1, #p31, 1 do | for i = 1, #p31, 1 do | ||
id = p31[i].id | id = p31[i].id | ||
aType = | aType = list[id] | ||
if aType then | if aType then | ||
| Zeile 134: | Zeile 185: | ||
id = fw.getWDid( id, 'P279' ) | id = fw.getWDid( id, 'P279' ) | ||
if id ~= '' then | if id ~= '' then | ||
aType = | aType = list[id] | ||
if aType then return aType end | if aType then return aType end | ||
end | end | ||
Version vom 18. März 2018, 08:16 Uhr
Die Dokumentation für dieses Modul kann unter Modul:Wikidata utilities/doc erstellt werden
local fw = {}
function fw.checkId( id )
if ( not id ) 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
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.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.getEntWDid( anEntity, p )
if anEntity == nil then
return ''
end
local value = getFirstValue( anEntity:getBestStatements( p ) )
if value then
return value.id
end
return ''
end
function fw.getEntWDvalue( anEntity, p )
if anEntity == nil then
return ''
end
local value = getFirstValue( anEntity:getBestStatements( p ) )
if value then
return value
end
return ''
end
function fw.getWDid( id, p )
if not id or id == '' then
return ''
end
local value = getFirstValue( mw.wikibase.getBestStatements( id, p ) )
if value then
return value.id
end
return ''
end
function fw.getWDvalue( id, p )
if not id or id == '' then
return ''
end
local value = getFirstValue( mw.wikibase.getBestStatements( id, p ) )
if value then
return value
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.getEntWDvalues( anEntity, p, count )
if anEntity == nil then
return {}
end
local statements = anEntity:getBestStatements( p )
return getNValues( statements, count or #statements )
end
function fw.getWDvalues( id, p, count )
if not id or id == '' then
return {}
end
local statements = mw.wikibase.getBestStatements( id, p )
return getNValues( statements, count or #statements )
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.getWDid( 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