Module:Wikidata/Outils
Apparence
La documentation pour ce module peut être créée à Module:Wikidata/Outils/doc
--Fonccions èlèmentères de maneyance des snaks Wikidata
local p = {}
p.i18n = require "Module:Wikidata/I18n"
local defaultlang = mw.getContentLanguage():getCode()
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) then
return 'Q' .. 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)
return snak.datavalue.value
end
function p.formatError( key )
return error(p.i18n[key] or key)
end
function p.addcat(cat, sortkey)
if sortkey then
return '[[Category:' .. cat .. '|' .. (sortkey or '') .. ']]'
end
return '[[Category:' .. cat .. ']]'
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) -- transfôrme en table les chênes que vegnont de Vouiquitèxto qu’emplèye de virgules de sèparacion
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
function p.alreadyHere(searchset, val) -- dèpassâ
return p.isHere(searchset, val)
end
function p.getEntityPagename(entity)
if (not entity) then
return nil -- ou ben chouèx de maneyance de les fôtes ?
end
local name ="d:"
if entity["type"] == "property" then
name = "d:Property:"
end
return name .. entity.id
end
function p.getEntityPagename(entity)
if (not entity) then
return nil -- ou ben chouèx de maneyance de les fôtes ?
end
local name ="d:"
if entity["type"] == "property" then
name = "d:Property:"
end
return name .. entity.id
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)
-- returns 3 values: a sitelink (with the relevant prefix) a project name and a language
lang = lang or defaultlang
if (type(project) ~= 'string') then
project = 'wiki'
end
project = project:lower()
if project == 'wikipedia' then
project = 'wiki'
end
if type(entity) == 'string' and (project == 'wiki') and ( (not lang or lang == defaultlang) ) then -- èvite de chargiér la piéce entiére
return mw.wikibase.sitelink(entity), 'wiki', defaultlang
end
if project == 'wikidata' then
return wikidataLink(entity), 'wikidata'
end
local projects = {
-- nom = {prèfixo dessus Wikidata, prèfixo por los lims dessus Vouiquipèdia, apondre prèfixo de lengoua}
wiki = {'wiki', nil, true}, -- wikipedia
commons = {'commonswiki', 'commons', false},
commonswiki = {'commonswiki', 'commons', false},
wikiquote = {'wikiquote', 'wikiquote', true},
wikivoyage = {'wikivoyage', 'wikivoyage', true},
wikibooks = {'wikibooks', 'wikibooks', true},
wikinews = {'wikinews', 'wikinews', true},
-- meta
-- mediawiki
}
entity = p.getEntity(entity)
if not entity then
return nil
end
local projectdata = projects[project:lower()]
if not projectdata then -- ôtrament, pôvont avêr de lims du tipo "enwiki" pletout que "en" et lang = "wikipedia'
for k, v in pairs(projects) do
if project:match( k .. '$' )
and mw.language.isKnownLanguageTag(project:sub(1, #project-#k))
then
lang = project:sub(1, #project-#k)
projectdata = k
break
end
end
if not mw.language.isKnownLanguageTag(lang) then
return p.formatError('invalid project code: ' .. (project or '?'))
end
end
if not projectdata then
return p.formatError('invalid project code: ' .. (project or '?'))
end
local linkcode = projectdata[1]
local prefix = projectdata[2]
local multiversion = projectdata[3]
if multiversion then
linkcode = lang .. linkcode
end
local link = entity:getSitelink(linkcode)
if not link then
return nil
end
if prefix then
link = prefix .. ':' .. link
end
if multiversion then
link = ':' .. lang .. ':' .. link
end
return link, project, lang
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