Module:Wikidata/Tools
Jump to navigation
Jump to search
Lua
CodeDiscussionEditHistoryLinksLink count Subpages:DocumentationTestsResultsSandboxLive code All modules
Elementary functions that can be used in other Wikidata-related modules
Code
--Fonctions élémentaires de gestion des snaks Wikidata
local p = {}
p.i18n = require "Module:I18n/wikidata"
local defaultlang = mw.getCurrentFrame():callParserFunction("int", "lang")
function p.translate(str, rep1, rep2)
str = p.i18n[str] or str
if rep1 then
str = str:gsub('$1', rep1)
end
if rep2 then
str = str:gsub('$2', rep2)
end
return str
end
function p.snaktype(snak)
return snak.snaktype
end
function p.isSpecial(snak)
return (snak.snaktype ~= 'value')
end
function p.isValue(snak)
return (snak.snaktype == 'value')
end
function p.getId(snak)
if p.isValue(snak) and (snak.datatype == 'wikibase-item') then
return 'Q' .. snak.datavalue.value['numeric-id']
elseif p.isValue(snak) and (snak.datatype == 'wikibase-property') then
return 'P' .. snak.datavalue.value['numeric-id']
end
end
function p.getNumericId(snak)
if p.isValue(snak) then
return snak.datavalue.value['numeric-id']
end
end
function p.getMainId(claim)
return p.getId(claim.mainsnak)
end
function p.EntityId(entity)
if type(entity) == 'string' then
return entity
end
return entity.id
end
function p.getValue(snak)
if (snak.snaktype ~= "value") then
return nil
end
return snak.datavalue.value
end
function p.formatError( key )
return error(p.i18n[key] or key)
end
function p.getEntity( val )
if type(val) == 'table' then
return val
end
if val == '-' then
return nil
end
return mw.wikibase.getEntityObject(val)
end
function p.splitStr(val) -- transforme en table les chaînes venant du Wikitexte qui utilisent des virgules de séparatin
if type(val) == 'string' then
val = mw.text.split(val, ",")
end
return val
end
function p.isHere(searchset, val)
for i, j in pairs(searchset) do
if val == j then
return true
end
end
return false
end
local function wikidataLink(entity)
local name =':d:'
if type(entity) == 'string' then
if entity:match("P[0-9+]") then
entity = "Property:" .. entity
end
return name .. entity
elseif type(entity) == 'table' then
if entity["type"] == "property" then
name = ":d:Property:"
end
return name .. entity.id
elseif type(entity) == nil then
return formatError('entity-not-found')
end
end
function p.siteLink(entity, project, lang)
local id = entity
if type(id) == "table" then
id = id.id
end
if (not id) then
return nil
end
-- default: directly return link to the current wiki
if (not project) or (mw.text.trim(project) == "") then
return mw.wikibase.sitelink(id)
end
-- cleanup up paramaters
project = project:lower()
lang = lang or defaultlang
-- special case: link to wikidata
if project == 'wikidata' then
return ":d:" .. id
end
-- link to other projects
local projects = {
-- parametername = {prefix in wikidata, prefix in wikipedia, is multilingual}
wikipedia = {'wiki', nil, true}, -- wikipedia
commons = {'commonswiki', 'c', false},
commonswiki = {'commonswiki', 'c', false},
wikiquote = {'wikiquote', 'wikiquote', true},
wikivoyage = {'wikivoyage', 'wikivoyage', true},
wikibooks = {'wikibooks', 'wikibooks', true},
wikinews = {'wikinews', 'wikinews', true},
-- meta
-- mediawiki
}
local projectdata = projects[project]
if not projectdata then
return p.formatError('invalid project code: ' .. (project or '?'))
end
local linkcode = projectdata[1]
local prefix = projectdata[2]
local multilang = projectdata[3]
if multilang then
linkcode = lang .. linkcode
end
entity = p.getEntity(entity)
local link = entity:getSitelink(linkcode)
if not link then
return nil
end
link = ':' .. link
if prefix then
link = ':' .. prefix .. link
end
if multilang then
link = ':' .. lang .. link
end
return link
end
function p.hasqualifier(claim, acceptedqualifs, acceptedvals, excludequalifiervalues)
local claimqualifs = claim.qualifiers
if (not claimqualifs) then
return false
end
acceptedqualifs = p.splitStr(acceptedqualifs)
acceptedvals = p.splitStr( acceptedvals)
local function ok(qualif) -- vérification pour un qualificatif individuel
if not claimqualifs[qualif] then
return false
end
if not (acceptedvals) then -- si aucune valeur spécifique n'est demandée, OK
return true
end
for i, wanted in pairs(acceptedvals) do
for j, actual in pairs(claimqualifs[qualif]) do
if p.getId(actual) == wanted then
return true
end
end
end
end
for i, qualif in pairs(acceptedqualifs) do
if ok(qualif) then
return true
end
end
return false
end
-- add new values to a list, avoiding duplicates
function p.addnewvalues(old, new)
if not new then
return old
end
for _, j in pairs(new) do
if not p.isHere(old, j) then
table.insert(old, j)
end
end
return old
end
return p