Modul:Wikidata utilities: Unterschied zwischen den Versionen
(Datum) |
(fw -> wu; +function wu.addProperty( p )) |
||
| Zeile 1: | Zeile 1: | ||
-- Wikidata convenience utilities | |||
-- documentation | -- documentation | ||
local FastWikidata = { | local FastWikidata = { | ||
suite = 'FastWikidata', | suite = 'FastWikidata', | ||
serial = '2020- | serial = '2020-09-26', | ||
item = 65439025 | item = 65439025 | ||
} | } | ||
-- module variable | -- module variable | ||
local | local wu = {} | ||
-- table storing property ids used | -- table storing property ids used | ||
| Zeile 14: | Zeile 16: | ||
} | } | ||
function | function wu.getEntity( id ) | ||
local wrongQualifier = false | local wrongQualifier = false | ||
local entity = nil | local entity = nil | ||
| Zeile 34: | Zeile 36: | ||
end | end | ||
function | function wu.getEntityId( id ) | ||
local wrongQualifier = false | local wrongQualifier = false | ||
local entity = nil | local entity = nil | ||
| Zeile 54: | Zeile 56: | ||
end | end | ||
function | function wu.getLabel( entity, lang ) | ||
local isString = type( entity ) == 'string' | local isString = type( entity ) == 'string' | ||
if not entity or ( isString and entity == '' ) then | if not entity or ( isString and entity == '' ) then | ||
| Zeile 74: | Zeile 76: | ||
end | end | ||
function | function wu.getSitelink( entity, globalSiteId ) | ||
local isString = type( entity ) == 'string' | local isString = type( entity ) == 'string' | ||
if not entity or ( isString and entity == '' ) then | if not entity or ( isString and entity == '' ) then | ||
| Zeile 128: | Zeile 130: | ||
end | end | ||
function | function wu.getBestStatements( entity, p ) | ||
if type( entity ) == 'string' then | if type( entity ) == 'string' then | ||
return mw.wikibase.getBestStatements( entity, p ) | return mw.wikibase.getBestStatements( entity, p ) | ||
| Zeile 136: | Zeile 138: | ||
end | end | ||
function | function wu.getStatements( entity, p, count ) | ||
local ar = {} | local ar = {} | ||
if not entity or entity == '' then | if not entity or entity == '' then | ||
| Zeile 142: | Zeile 144: | ||
end | end | ||
local statements = | local statements = wu.getBestStatements( entity, p ) | ||
count = count or #statements | count = count or #statements | ||
| Zeile 163: | Zeile 165: | ||
end | end | ||
function | function wu.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( | value = getFirstValue( wu.getBestStatements( entity, p ) ) | ||
if value then | if value then | ||
catTable[ p ] = '' | catTable[ p ] = '' | ||
| Zeile 176: | Zeile 178: | ||
end | end | ||
function | function wu.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( | value = getFirstValue( wu.getBestStatements( entity, p ) ) | ||
if value then | if value then | ||
catTable[ p ] = '' | catTable[ p ] = '' | ||
| Zeile 190: | Zeile 192: | ||
end | end | ||
function | function wu.getValues( entity, p, count ) | ||
local values = '' | local values = '' | ||
if entity and entity ~= '' and p and p ~= '' then | if entity and entity ~= '' and p and p ~= '' then | ||
local statements = | local statements = wu.getBestStatements( entity, p ) | ||
values = getNValues( statements, count or #statements ) | values = getNValues( statements, count or #statements ) | ||
if #values > 0 then | if #values > 0 then | ||
| Zeile 202: | Zeile 204: | ||
end | end | ||
function | function wu.getValuesByLang( entity, p, count, lang ) | ||
local ar = '' | local ar = '' | ||
if entity and entity ~= '' and p and p ~= '' then | if entity and entity ~= '' and p and p ~= '' then | ||
local statements = | local statements = wu.getBestStatements( entity, p ) | ||
ar = {} | ar = {} | ||
count = count or #statements | count = count or #statements | ||
| Zeile 228: | Zeile 230: | ||
end | end | ||
function | function wu.getValuesWithQualifierIds( entity, p, qualifierP, defaultId ) | ||
local result = {} | 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 | ||
local statements = | local statements = wu.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 260: | Zeile 262: | ||
-- get values array for monolingual text | -- get values array for monolingual text | ||
function | function wu.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 | ||
local statements = | local statements = wu.getStatements( entity, p, nil ) | ||
local hyphen, lng, value | local hyphen, lng, value | ||
if #statements > 0 and statements[ 1 ].mainsnak.datatype == 'monolingualtext' then | if #statements > 0 and statements[ 1 ].mainsnak.datatype == 'monolingualtext' then | ||
| Zeile 304: | Zeile 306: | ||
-- The following function is an experimental one, not for extensive use | -- The following function is an experimental one, not for extensive use | ||
function | function wu.getValuesWithQualifiers( entity, p, qualifiers, count ) | ||
local result = {} | local result = {} | ||
local statements = | local statements = wu.getStatements( entity, p, count ) | ||
if #statements == 0 then | if #statements == 0 then | ||
return result | return result | ||
| Zeile 352: | Zeile 354: | ||
end | end | ||
function | function wu.getAliases( entity, lang ) | ||
if type( entity ) == 'string' then -- is Q id | if type( entity ) == 'string' then -- is Q id | ||
entity = mw.wikibase.getEntity( entity ) | entity = mw.wikibase.getEntity( entity ) | ||
| Zeile 368: | Zeile 370: | ||
end | end | ||
function | -- maintenance utilities | ||
function wu.addProperty( p ) | |||
catTable[ p ] = '' | |||
end | |||
function wu.getCategories( formatStr ) | |||
local result = '' | local result = '' | ||
| Zeile 382: | Zeile 389: | ||
end | end | ||
return | return wu | ||
Version vom 26. September 2020, 07:42 Uhr
Die Dokumentation für dieses Modul kann unter Modul:Wikidata utilities/doc erstellt werden
-- Wikidata convenience utilities
-- documentation
local FastWikidata = {
suite = 'FastWikidata',
serial = '2020-09-26',
item = 65439025
}
-- module variable
local wu = {}
-- table storing property ids used
local catTable = {
P0 = ''
}
function wu.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 wu.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 wu.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 wu.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 wu.getBestStatements( entity, p )
if type( entity ) == 'string' then
return mw.wikibase.getBestStatements( entity, p )
else
return entity:getBestStatements( p )
end
end
function wu.getStatements( entity, p, count )
local ar = {}
if not entity or entity == '' then
return ar
end
local statements = wu.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 wu.getValue( entity, p )
local value = ''
if entity and entity ~= '' and p and p ~= '' then
value = getFirstValue( wu.getBestStatements( entity, p ) )
if value then
catTable[ p ] = ''
else
value = ''
end
end
return value
end
function wu.getId( entity, p )
local value = ''
if entity and entity ~= '' and p and p ~= '' then
value = getFirstValue( wu.getBestStatements( entity, p ) )
if value then
catTable[ p ] = ''
value = value.id
else
value = ''
end
end
return value
end
function wu.getValues( entity, p, count )
local values = ''
if entity and entity ~= '' and p and p ~= '' then
local statements = wu.getBestStatements( entity, p )
values = getNValues( statements, count or #statements )
if #values > 0 then
catTable[ p ] = ''
end
end
return values
end
function wu.getValuesByLang( entity, p, count, lang )
local ar = ''
if entity and entity ~= '' and p and p ~= '' then
local statements = wu.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 wu.getValuesWithQualifierIds( entity, p, qualifierP, defaultId )
local result = {}
if entity and entity ~= '' and p and p ~= '' and qualifierP and qualifierP ~= '' then
local statements = wu.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 wu.getValuesWithLanguages( entity, p )
local result = {}
if entity and entity ~= '' and p and p ~= '' then
local statements = wu.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 == 'quantity' then
if v.unit == '1' then
v = tonumber( v.amount ) or 1
else
v = v.amount:gsub( '^+', '' ) .. ' ' .. v.unit
end
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 wu.getValuesWithQualifiers( entity, p, qualifiers, count )
local result = {}
local statements = wu.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 = getValueFromDatavalue( statements[ i ].mainsnak.datavalue ),
[ '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' ] =
getValueFromDatavalue( statements[ i ].qualifiers[ value ][ j ].datavalue )
break
end
end
end
end
end
end
table.insert( result, array )
end
return result
end
function wu.getAliases( entity, lang )
if type( entity ) == 'string' then -- is Q id
entity = mw.wikibase.getEntity( entity )
end
if not lang then
lang = mw.getContentLanguage():getCode()
end
local aliases = {}
if entity and entity.aliases and entity.aliases[ lang ] then
for _, alias in ipairs( entity.aliases[ lang ] ) do
table.insert( aliases, alias.value )
end
end
return aliases
end
-- maintenance utilities
function wu.addProperty( p )
catTable[ p ] = ''
end
function wu.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 wu