Modul:Wikidata utilities: Unterschied zwischen den Versionen
Modul:Wikidata utilities (Quelltext anzeigen)
Version vom 30. September 2020, 19:13 Uhr
, vor 4 Jahrenvereinfacht
(Funktion umbenannt) |
(vereinfacht) |
||
Zeile 4: | Zeile 4: | ||
local WikidataUtilities = { | local WikidataUtilities = { | ||
suite = 'WikidataUtilities', | suite = 'WikidataUtilities', | ||
serial = '2020-09- | serial = '2020-09-30', | ||
item = 65439025 | item = 65439025 | ||
} | } | ||
Zeile 15: | Zeile 15: | ||
P0 = '' | P0 = '' | ||
} | } | ||
local function isSet( arg ) | |||
return arg and arg ~= '' | |||
end | |||
function wu.getEntity( id ) | function wu.getEntity( id ) | ||
Zeile 20: | Zeile 24: | ||
local entity = nil | local entity = nil | ||
if not id | if not isSet( id ) then | ||
return '', entity, wrongQualifier | return '', entity, wrongQualifier | ||
end | end | ||
Zeile 40: | Zeile 44: | ||
local entity = nil | local entity = nil | ||
if not id | if not isSet( id ) then | ||
id = '' | |||
elseif mw.wikibase.isValidEntityId( id ) and mw.wikibase.entityExists( id ) then | |||
-- expensive function call | -- expensive function call | ||
-- redirect ids marked false, too | -- redirect ids marked false, too | ||
entity = id | entity = id | ||
else | |||
id = '' | id = '' | ||
wrongQualifier = true | wrongQualifier = true | ||
Zeile 57: | Zeile 59: | ||
function wu.getLabel( entity, lang ) | function wu.getLabel( entity, lang ) | ||
if not isSet( entity ) then | |||
if not | |||
return nil | return nil | ||
end | end | ||
if | if type( entity ) == 'string' then -- entity is id | ||
return isSet( lang ) and mw.wikibase.getLabelByLang( entity, lang ) | |||
or mw.wikibase.getLabel( entity ) | |||
else -- entity is table | else -- entity is table | ||
return isSet( lang ) and entity:getLabel( lang ) or entity:getLabel() | |||
end | end | ||
end | end | ||
function wu.getSitelink( entity, globalSiteId ) | function wu.getSitelink( entity, globalSiteId ) | ||
if not isSet( entity ) then | |||
if not | |||
return nil | return nil | ||
end | end | ||
if | if type( entity ) == 'string' then -- entity is id | ||
return isSet( globalSiteId ) and mw.wikibase.getSitelink( entity, globalSiteId ) | |||
or mw.wikibase.getSitelink( entity ) | |||
else -- entity is table | else -- entity is table | ||
return isSet( globalSiteId ) and entity:getSitelink( globalSiteId ) | |||
or entity:getSitelink( globalSiteId ) | |||
end | end | ||
end | end | ||
Zeile 140: | Zeile 93: | ||
function wu.getStatements( entity, p, count ) | function wu.getStatements( entity, p, count ) | ||
local ar = {} | local ar = {} | ||
if not entity | if not ( isSet( entity ) and isSet( p ) ) then | ||
return ar | return ar | ||
end | end | ||
local statements = wu.getBestStatements( entity, p ) | local statements = wu.getBestStatements( entity, p ) | ||
count = math.min( count or #statements, #statements ) | |||
count = count or #statements | if count <= 0 then | ||
if | |||
return ar | return ar | ||
end | end | ||
Zeile 166: | Zeile 115: | ||
function wu.getValue( entity, p ) | function wu.getValue( entity, p ) | ||
local | local statements = wu.getStatements( entity, p, 1 ) | ||
if #statements > 0 then | |||
catTable[ p ] = '' | |||
return statements[ 1 ].mainsnak.datavalue.value | |||
else | |||
else | return '' | ||
end | end | ||
end | end | ||
function wu.getId( entity, p ) | function wu.getId( entity, p ) | ||
local value = '' | local value = '' | ||
local statements = wu.getStatements( entity, p, 1 ) | |||
value = | if #statements > 0 then | ||
if value then | value = statements[ 1 ].mainsnak.datavalue.value | ||
value = value.id or '' | |||
if value ~= '' then | |||
catTable[ p ] = '' | catTable[ p ] = '' | ||
end | end | ||
end | end | ||
Zeile 193: | Zeile 138: | ||
function wu.getValues( entity, p, count ) | function wu.getValues( entity, p, count ) | ||
local statements = wu.getStatements( entity, p, count ) | |||
if #statements > 0 then | |||
catTable[ p ] = '' | |||
for i = 1, #statements, 1 do | |||
statements[ i ] = statements[ i ].mainsnak.datavalue.value | |||
end | end | ||
end | end | ||
return | return statements | ||
end | end | ||
function wu.getValuesByLang( entity, p, count, lang ) | function wu.getValuesByLang( entity, p, count, lang ) | ||
local ar = | local ar = {} | ||
local statements = wu.getStatements( entity, p, count ) | |||
if #statements > 0 then | |||
local value | |||
for i = 1, #statements, 1 do | |||
value = statements[ i ].mainsnak.datavalue.value | |||
if value.language and lang == value.language then | |||
table.insert( ar, value.text ) | |||
end | |||
end | end | ||
end | |||
if #ar > 0 then | |||
catTable[ p ] = '' | |||
end | end | ||
return ar | return ar | ||
end | end | ||
function wu. | -- get values array for monolingual text | ||
function wu.getMonolingualValues( entity, p ) | |||
local result = {} | local result = {} | ||
local statements = wu.getStatements( entity, p, nil ) | |||
if #statements > 0 and statements[ 1 ].mainsnak.datatype == 'monolingualtext' then | |||
local hyphen, lng, value | |||
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 | end | ||
if not result[ lng ] then | |||
result[ lng ] = value.text | |||
result[ | |||
end | end | ||
end | end | ||
Zeile 261: | Zeile 188: | ||
end | end | ||
function wu.getValuesByQualifier( entity, p, qualifierP, defaultId ) | |||
function wu. | |||
local result = {} | local result = {} | ||
if | if not isSet( qualifierP ) then | ||
local statements = wu.getStatements( entity, p, nil ) | return result | ||
elseif type( defaultId ) ~= 'string' or defaultId == '' then | |||
defaultId = 'unknown' | |||
end | |||
local statements = wu.getStatements( entity, p, nil ) | |||
if #statements > 0 then | |||
catTable[ p ] = '' | |||
local id, value | |||
for _, statement in ipairs( statements ) do | |||
-- defaultId is used if a qualifier is missing | |||
id = defaultId | |||
value = statement.mainsnak.datavalue.value | |||
if statement.qualifiers and statement.qualifiers[ qualifierP ] then | |||
for _, qualifier in ipairs( statement.qualifiers[ qualifierP ] ) do | |||
if qualifier.snaktype == 'value' then | |||
id = qualifier.datavalue.value.id | |||
if id then | |||
break | |||
end | |||
end | |||
end | end | ||
end | end | ||
result[ id ] = value | |||
end | end | ||
end | end | ||
Zeile 313: | Zeile 248: | ||
end | end | ||
if | if type( qualifiers ) == 'string' then | ||
qualifiers = { qualifiers } | qualifiers = { qualifiers } | ||
end | end | ||
local array | local array | ||
for | for _, statement in ipairs( statements ) do | ||
array = { value = getValueFromDatavalue( | array = { value = getValueFromDatavalue( statement.mainsnak.datavalue ), | ||
[ 'value-type' ] = | [ 'value-type' ] = statement.mainsnak.datavalue.type } | ||
if | if statement.qualifiers then | ||
if not qualifiers then -- all qualifier properties | if not qualifiers then -- all qualifier properties | ||
for key, | for key, qualTab in pairs( statement.qualifiers ) do | ||
for _, qual in ipairs( qualTab ) do | |||
if qual.snaktype == 'value' then | |||
array[ key ], array[ key .. '-type' ] = | |||
getValueFromDatavalue( qual.datavalue ) | |||
break | |||
end | end | ||
end | end | ||
end | end | ||
else -- table of selected qualifier properties | else -- table of selected qualifier properties | ||
for key, value in | for key, value in ipairs( qualifiers ) do | ||
if | if statement.qualifiers[ value ] then | ||
for _, qual in ipairs( statement.qualifiers[ value ] ) do | |||
for | if qual.snaktype == 'value' then | ||
if | |||
array[ value ], array[ value .. '-type' ] = | array[ value ], array[ value .. '-type' ] = | ||
getValueFromDatavalue( | getValueFromDatavalue( qual.datavalue ) | ||
break | break | ||
end | end | ||
Zeile 378: | Zeile 310: | ||
local result = '' | local result = '' | ||
if not formatStr | if not isSet( formatStr ) then | ||
formatStr = '[[Category:%s]]' | formatStr = '[[Category:%s]]' | ||
end | end |