« Module:Wikidata/Outils » : différence entre les versions

De Lagny-sur-Marne Wiki
Aller à la navigation Aller à la recherche
0x010D (discussion | contributions)
Aucun résumé des modifications
0x010D (discussion | contributions)
Aucun résumé des modifications
Ligne 3 : Ligne 3 :
p.i18n = require "Module:Wikidata/I18n"
p.i18n = require "Module:Wikidata/I18n"
local defaultlang = mw.getContentLanguage():getCode()
local defaultlang = mw.getContentLanguage():getCode()
function p.translate(str, rep1, rep2)
str = p.i18n[str] or str
if rep1 then
str:gsub('$1', rep1)
end
if rep2 then
str:gsub('$2', rep2)
end
end


function p.snaktype(snak)
function p.snaktype(snak)
Ligne 94 : Ligne 104 :
end
end


function p.getLink(entity, linktype, lang)
function p.getLink(entity, project, lang)
lang = lang or defaultlang
lang = lang or defaultlang
linktype = linktype or 'wikipedia'
if (not project) or (project == 'wikipedia') then
if linktype == '-' then
project = 'wiki'
end
if type(entity) == 'string' and (project == 'wiki') and ( (not lang or lang == defaultlang) ) then -- évite de charger l'élément entier
return  mw.wikibase.sitelink(entity)
end
if project == 'wikidata' then
return wikidataLink(entity)
end
local projects = {
-- nom = {préfixe sur Wikidata, préfix pour les liens sur Wikipédia, ajouter préfixe de langue}
wiki = {'wiki', nil, true}, -- wikipedia
commons = {'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
return nil
end
end
if linktype == 'wikipedia' then
 
return wikipediaLink(entity, lang)
 
elseif linktype == 'wikidata' then
local projectdata = projects[project]
return wikidataLink(entity)
if not projectdata then -- sinon, on peut avoir des liens du type "enwiki" plutôt que "en" et lang = "wikipedia'
local i = 2
lang = string.sub(project, 1, i)
while not mw.language.isValidCode(lang) do
lang = string.sub(project, 1, i)
i = i +1
end
if not mw.language.isValidCode(lang) then
return p.formatError('invalid project code: ' .. (project or '?'))
end
project = string.sub(project, #lang +1)
projectdata = projects[project]
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
end
-- analyse les valeurs du type 'enwiki' pour linktype = 'wikipedia', lang = 'en'
if prefix then
local endstr = string.sub(linktype, #linktype - 3, #linktype)
link = prefix .. ':' .. link
if endstr == 'wiki' then
lang = string.sub(linktype, 1, #linktype - 4)
return wikipediaLink(entity, lang)
end
end
--[[
if multiversion then
local errormsg = i18n['invalid-linktype']
link = ':' .. lang .. ':' .. link
if type(linktype) ~= 'string' then
errormsg = errormsg .. ': ' .. linktype
else
errormsg = errormsg .. ':' .. type(linktype)
end
end
return formatError(errormsg)
return link, lang, project
]]--
end
end



Version du 25 septembre 2015 à 10:40

--Fonctions élémentaires de gestion 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:gsub('$1', rep1) end if rep2 then str:gsub('$2', rep2) end 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.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 end return 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.alreadyHere(searchset, val) for i, j in pairs(searchset) do if val == j then return true end end return false end

local function wikipediaLink(entity, lang) lang = lang or defaultlang if (type(entity) == 'string') and (lang == defaultlang) then -- le plus économique return mw.wikibase.sitelink(entity) end if type(entity) == 'string' then entity = mw.wikibase.getEntityObject(entity) end if (not entity) or type(entity) ~= 'table' then return formatError('entity-not-found') end local link = entity:getSitelink(lang .. 'wiki') if link then return ':' .. lang .. ':' .. link end end

local function wikidataLink(entity) if type(entity) == 'string' then return ':d:' .. entity elseif type(entity) == 'table' then return ':d:' .. entity.id elseif type(entity) == nil then return formatError('entity-not-found') end end

function p.getLink(entity, project, lang) lang = lang or defaultlang if (not project) or (project == 'wikipedia') then project = 'wiki' end if type(entity) == 'string' and (project == 'wiki') and ( (not lang or lang == defaultlang) ) then -- évite de charger l'élément entier return mw.wikibase.sitelink(entity) end if project == 'wikidata' then return wikidataLink(entity) end local projects = { -- nom = {préfixe sur Wikidata, préfix pour les liens sur Wikipédia, ajouter préfixe de langue} wiki = {'wiki', nil, true}, -- wikipedia commons = {'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] if not projectdata then -- sinon, on peut avoir des liens du type "enwiki" plutôt que "en" et lang = "wikipedia' local i = 2 lang = string.sub(project, 1, i) while not mw.language.isValidCode(lang) do lang = string.sub(project, 1, i) i = i +1 end if not mw.language.isValidCode(lang) then return p.formatError('invalid project code: ' .. (project or '?')) end project = string.sub(project, #lang +1) projectdata = projects[project] 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, lang, project 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.alreadyHere(old, j) then table.insert(old, j) end end return old end

return p