Aller au contenu

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

De Wreck
Modèle:Infobox>GrandEscogriffe
intégration de la catégorisation par langue à traduire dans getLabel ; création de la catégorisation comme page à traduire de l'anglais
m 541 versions importées
 
(67 versions intermédiaires par 9 utilisateurs non affichées)
Ligne 1 : Ligne 1 :
--script that retrieves basic data stored in Wikidata, for the datamodel, see https://www.mediawiki.org/wiki/Extension:Wikibase_Client/Lua
--script that retrieves basic data stored in Wikidata, for the datamodel, see https://www.mediawiki.org/wiki/Extension:Wikibase_Client/Lua
-- luacheck: globals mw


local wd = {}
local wd = {}
Ligne 8 : Ligne 9 :
wd.compare = {}
wd.compare = {}


local databases = { }
local modules = { }
local modules = { }
local databasesNames = { -- modules de données statiques pouvant être appelés avec mw.loadData(), ne nécessitant pas require()
i18n = 'Module:Wikidata/I18n',
globes = 'Module:Wikidata/Globes',
langhierarchy = 'Module:Wikidata/Hiérarchie des langues',
langcodes = 'Module:Dictionnaire Wikidata/Codes langue', -- big, infrequently used
invertedlangcodes = 'Module:Dictionnaire Wikidata/Codes langue/inversé'
}
local modulesNames = {
local modulesNames = {
reference = 'Module:Wikidata/Références',
reference = 'Module:Wikidata/Références',
i18n = 'Module:Wikidata/I18n',
globes = 'Module:Wikidata/Globes',
langhierarchy = 'Module:Wikidata/Hiérarchie des langues/Bac à sable',
langcodes = 'Module:Dictionnaire Wikidata/Codes langue', -- big, infrequently useda
invertedlangcodes = 'Module:Dictionnaire Wikidata/Codes langue/inversé',
linguistic = 'Module:Linguistique',
linguistic = 'Module:Linguistique',
datemodule = 'Module:Date',
datemodule = 'Module:Date',
Ligne 26 : Ligne 29 :
}
}


local function loadDatabase( t, key )
if databasesNames[key] then
local m = mw.loadData( databasesNames[key] )
t[key] = m
return m
end
end
local function loadModule( t, key )
local function loadModule( t, key )
if modulesNames[key] then
if modulesNames[key] then
Ligne 33 : Ligne 43 :
end
end
end
end
setmetatable( modules, { __index = loadModule } )
setmetatable( databases, { __index = loadDatabase } )
setmetatable( modules, { __index = loadModule } ) -- ainsi le require() sera opéré seulement si nécessaire par modules.(nom du module)


local datequalifiers = {'P585', 'P571', 'P580', 'P582', 'P1319', 'P1326'}
local datequalifiers = {'P585', 'P571', 'P580', 'P582', 'P1319', 'P1326'}
Ligne 41 : Ligne 52 :


function wd.translate(str, rep1, rep2)
function wd.translate(str, rep1, rep2)
str = modules.i18n[str] or str
str = databases.i18n[str] or str
if rep1 and (type (rep1) == 'string') then
if rep1 and (type (rep1) == 'string') then
str = str:gsub('$1', rep1)
str = str:gsub('$1', rep1)
Ligne 53 : Ligne 64 :
local function addCat(cat, sortkey)
local function addCat(cat, sortkey)
if sortkey then
if sortkey then
return '[[Category:' .. cat .. '|' .. sortkey .. ']]'
return '[[Category:' .. cat .. '|' .. sortkey .. ']]'
end
end
return '[[Category:' .. cat .. ']]'
return '[[Category:' .. cat .. ']]'
end
end


local function formatError( key , category, debug)
local function formatError( key , category, debug)
     if debug then
     if debug then
         return error(modules.i18n[key] or key)
         return error(databases.i18n[key] or key)
     end
     end
     if category then
     if category then
         return addCat(category, key)
         return addCat(category, key)
     else
     else
         return addCat('cat-unsorted-issue', key)
         return addCat(databases.i18n['cat-unsorted-issue'], key)
     end
     end
end
end


--  
--


function wd.isSpecial(snak)
function wd.isSpecial(snak)
Ligne 103 : Ligne 114 :
end
end


-- function that returns true if the "qid" parameter is the qid  
-- function that returns true if the "qid" parameter is the qid
-- of the item that is linked to the calling page
-- of the item that is linked to the calling page
function wd.isPageOfQId(qid)  
function wd.isPageOfQId(qid)
local self_id = mw.wikibase.getEntityIdForCurrentPage()  
local self_id = mw.wikibase.getEntityIdForCurrentPage()
return self_id ~= nil and qid == self_id  
return self_id ~= nil and qid == self_id
end
end


function wd.getEntity( val )  
function wd.getEntity( val )
if type(val) == 'table' then
if type(val) == 'table' then
return val
return val
Ligne 130 : Ligne 141 :
end
end


function wd.isHere(searchset, val)
function wd.isHere(searchset, val, matchfunction)
for i, j in pairs(searchset) do
for i, j in pairs(searchset) do
if val == j then
if matchfunction then
return true
if matchfunction(val,j) then
return true
end
else
if val == j then
return true
end
end
end
end
end
Ligne 142 : Ligne 159 :
local function wikidataLink(entity)
local function wikidataLink(entity)
local name =':d:'
local name =':d:'
 
if type(entity) == 'string' then
if type(entity) == 'string' then
if entity:match("P[0-9+]") then
if entity:match("P[0-9]+") then
entity = "Property:" .. entity
entity = "Property:" .. entity
end
end
Ligne 169 : Ligne 186 :
end
end
if type(entity) == 'string' and (project == 'wiki') and ( (not lang or lang == defaultlang) ) then -- évite de charger l'élément entier
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), 'wiki', defaultlang
local link = mw.wikibase.getSitelink(entity)
if link then
local test_redirect = mw.title.new(link) -- remplacement des redirections (retirer si trop coûteux)
if test_redirect.isRedirect and test_redirect.redirectTarget then
link = test_redirect.redirectTarget.fullText
if link == mw.title.getCurrentTitle().text then return nil end --redirection vers l'article de départ : pas de lien plutôt qu'une mise en gras
end
end
return link, 'wiki', defaultlang
end
end
if project == 'wikidata' then
if project == 'wikidata' then
Ligne 198 : Ligne 223 :
if not projectdata then -- defaultlink might be in the form "dewiki" rather than "project: 'wiki', lang: 'de' "
if not projectdata then -- defaultlink might be in the form "dewiki" rather than "project: 'wiki', lang: 'de' "
for k, v in pairs(projects) do
for k, v in pairs(projects) do
if project:match( k .. '$' )  
if project:match( k .. '$' )
and mw.language.isKnownLanguageTag(project:sub(1, #project-#k))
and mw.language.isKnownLanguageTag(project:sub(1, #project-#k))
then
then
Ligne 214 : Ligne 239 :
return -- formatError('invalid-project-code', projet or 'nil')
return -- formatError('invalid-project-code', projet or 'nil')
end
end
 
local linkcode = projectdata[1]
local linkcode = projectdata[1]
local prefix = projectdata[2]
local prefix = projectdata[2]
Ligne 225 : Ligne 250 :
return nil
return nil
end
end
 
if prefix then
if prefix then
link = prefix .. ':' .. link
link = prefix .. ':' .. link
Ligne 259 : Ligne 284 :
local function notSpecial(claim)
local function notSpecial(claim)
local type
local type
 
if claim.mainsnak ~= nil then
if claim.mainsnak ~= nil then
type = claim.mainsnak.snaktype
type = claim.mainsnak.snaktype
Ligne 267 : Ligne 292 :
type = claim.snaktype
type = claim.snaktype
end
end
 
return type == 'value'
return type == 'value'
end
end
Ligne 280 : Ligne 305 :
return wd.isSpecial(claim.mainsnak) or not ( hasTargetValue(claim, values) )
return wd.isSpecial(claim.mainsnak) or not ( hasTargetValue(claim, values) )
end
end
local function hasTargetClass(claim, targets, maxdepth) -- retourne true si la valeur est une instance d'une classe dans la liste des target, ou si c'est une valeur spéciale filtrée séparément par excludespecial
local id = wd.getMainId(claim)
local targets = wd.splitStr(targets)
local maxdepth = maxdepth or 10
local matchfunction = function(value, target) return wd.isInstance(target, value, maxdepth) end
return wd.isHere(targets, id, matchfunction) or wd.isSpecial(claim.mainsnak)
end
local function excludeClasses(claim, classes, maxdepth) -- true si la valeur n'est pas une instance d'une classe dans la liste, ou si c'est une valeur spéciale (filtrée à part par excludespecial)
return wd.isSpecial(claim.mainsnak) or not ( hasTargetClass(claim, classes, maxdepth) )
end
local function hasTargetSuperclass(claim, targets, maxdepth) -- retourne true si la valeur est une sous-classe d'une classe dans la liste des target, ou si c'est une valeur spéciale filtrée séparément par excludespecial
local id = wd.getMainId(claim)
local targets = wd.splitStr(targets)
local maxdepth = maxdepth or 10
local matchfunction = function(value, target) return wd.isSubclass(target, value, maxdepth) end
return wd.isHere(targets, id, matchfunction) or wd.isSpecial(claim.mainsnak)
end
local function excludeSuperclasses(claim, classes, maxdepth) -- true si la valeur n'est pas une sous-classe d'une classe dans la liste, ou si c'est une valeur spéciale (filtrée à part par excludespecial)
return wd.isSpecial(claim.mainsnak) or not ( hasTargetSuperclass(claim, classes, maxdepth) )
end


local function bestRanked(claims)
local function bestRanked(claims)
Ligne 305 : Ligne 355 :
end
end
local newclaims = {}
local newclaims = {}
for pos, claim in pairs(claims) do
for pos, claim in pairs(claims) do
if target == 'valid' then
if target == 'valid' then
if claim.rank ~= 'deprecated' then
if claim.rank ~= 'deprecated' then
Ligne 319 : Ligne 369 :
function wd.hasQualifier(claim, acceptedqualifs, acceptedvals, excludequalifiervalues)
function wd.hasQualifier(claim, acceptedqualifs, acceptedvals, excludequalifiervalues)
local claimqualifs = claim.qualifiers
local claimqualifs = claim.qualifiers
 
if (not claimqualifs) then
if (not claimqualifs) then
return false
return false
Ligne 332 : Ligne 382 :
return false
return false
end
end
if not (acceptedvals) then -- si aucune valeur spécifique n'est demandée, OK
if not (acceptedvals) then -- si aucune valeur spécifique n'est demandée, OK
return true
return true
end
end
Ligne 354 : Ligne 404 :
function wd.hasQualifierNumber(claim, acceptedqualifs, acceptedvals, excludequalifiervalues)
function wd.hasQualifierNumber(claim, acceptedqualifs, acceptedvals, excludequalifiervalues)
local claimqualifs = claim.qualifiers
local claimqualifs = claim.qualifiers
 
if (not claimqualifs) then
if (not claimqualifs) then
return false
return false
Ligne 367 : Ligne 417 :
return false
return false
end
end
if not (acceptedvals) then -- si aucune valeur spécifique n'est demandée, OK
if not (acceptedvals) then -- si aucune valeur spécifique n'est demandée, OK
return true
return true
end
end
Ligne 392 : Ligne 442 :
return true
return true
end
end
if (not claim.references) then return
if (not claim.references) then
false
return false
end
end
local candidates = claim.references[1].snaks[sourceproperty] -- les snaks utilisant la propriété demandée
local candidates = claim.references[1].snaks[sourceproperty] -- les snaks utilisant la propriété demandée
Ligne 437 : Ligne 487 :
end
end


local function isInLanguage(claim, lang) -- ne fonctionne que pour les monolingualtext / étendre aux autres types en utilisant les qualifiers ?
local function isInLanguage(claim, lang)
if type(lang) == 'table' then -- si c'est une table de language séparées par des virgules, on les accepte toutes
if type(lang) == 'table' then -- si c'est une table de language séparées par des virgules, on les accepte toutes
for i, l in pairs(lang) do
for i, l in pairs(lang) do
Ligne 454 : Ligne 504 :
end
end
if (lang == 'locallang') then
if (lang == 'locallang') then
lang = mw.getContentLanguage():getCode()
lang = mw.getContentLanguage():getCode()
end
end
 
-- pour les monolingual text
-- pour les monolingual text
local snak = claim.mainsnak or claim
local snak = claim.mainsnak or claim
Ligne 472 : Ligne 522 :
lang = 'Q1860'
lang = 'Q1860'
else
else
lang = invertedlangcodes[lang]
lang = databases.invertedlangcodes[lang]
end
end
if claim.qualifiers and claim.qualifiers.P407 then
if claim.qualifiers and claim.qualifiers.P407 then
Ligne 528 : Ligne 578 :


for _, claim in ipairs( claims ) do
for _, claim in ipairs( claims ) do
local snack = claim.mainsnak or claim
local snak = claim.mainsnak or claim
if (snack.snaktype == 'value') and (snack.datatype == 'time') and snack.datavalue.value.precision >= 11 then -- s'il s'agit d'un time et que la précision est au moins l'année
if (snak.snaktype == 'value') and (snak.datatype == 'time') and snak.datavalue.value.precision >= 11 then -- s'il s'agit d'un time et que la précision est au moins l'année
local iso = snack.datavalue.value.time
local iso = snak.datavalue.value.time
_, _, iso = string.find(iso, "(+%d+-%d+-%d+T)")
_, _, iso = string.find(iso, "(+%d+-%d+-%d+T)")
local deleteIfDuplicate = false
local deleteIfDuplicate = false
if snack.datavalue.value.calendarmodel == 'http://www.wikidata.org/entity/Q1985727' then -- si la date est grégorienne
if snak.datavalue.value.calendarmodel == 'http://www.wikidata.org/entity/Q1985727' then -- si la date est grégorienne
if modules.formatDate.before('+1582', iso) then -- si avant 1582 on calcule la date julienne
if modules.formatDate.before('+1582', iso) then -- si avant 1582 on calcule la date julienne
_, _, y, m, d = string.find(iso, "+(%d+)-(%d+)-(%d+)T")
_, _, y, m, d = string.find(iso, "+(%d+)-(%d+)-(%d+)T")
Ligne 555 : Ligne 605 :
table.insert(newClaims, claim)
table.insert(newClaims, claim)
end
end
elseif snack.datavalue.value.calendarmodel == 'http://www.wikidata.org/entity/Q1985786' then -- si date julienne
elseif snak.datavalue.value.calendarmodel == 'http://www.wikidata.org/entity/Q1985786' then -- si date julienne
if not modules.formatDate.before('+1582', iso) then -- si après 1582 on calcule la date grégorienne
if not modules.formatDate.before('+1582', iso) then -- si après 1582 on calcule la date grégorienne
_, _, y, m, d = string.find(iso, "+(%d+)-(%d+)-(%d+)T")
_, _, y, m, d = string.find(iso, "+(%d+)-(%d+)-(%d+)T")
Ligne 594 : Ligne 644 :
return nil
return nil
end
end
for i, qualif in pairs(qualifs or timequalifiers) do
for i, qualif in ipairs(qualifs or datequalifiers) do
local vals = claimqualifs[qualif]
local vals = claimqualifs[qualif]
if vals and (vals[1].snaktype == 'value') then
if vals and (vals[1].snaktype == 'value') then
Ligne 602 : Ligne 652 :
end
end


local function atDate(claim, mydate)  
local function atDate(claim, mydate)
if mydate == "today" then
if mydate == "today" then
mydate = os.date("!%Y-%m-%dT%TZ")
mydate = os.date("!%Y-%m-%dT%TZ")
Ligne 611 : Ligne 661 :
local myprecision
local myprecision
if d[3] then
if d[3] then
myprecision = 11 -- day
myprecision = 11 -- day
elseif d[2] then
elseif d[2] then
myprecision = 10 -- month
myprecision = 10 -- month
Ligne 623 : Ligne 673 :
return modules.formatDate.equal(mydate, d, math.min(myprecision, storedprecision))
return modules.formatDate.equal(mydate, d, math.min(myprecision, storedprecision))
end
end
-- with start or end date -- TODO: precision  
-- with start or end date -- TODO: precision
local mindate = timeFromQualifs(claim, {'P580'})  
local mindate = timeFromQualifs(claim, {'P580'})
local maxdate = timeFromQualifs(claim, {'P582'})
local maxdate = timeFromQualifs(claim, {'P582'})
if modules.formatDate.before(mydate, mindate) and modules.formatDate.before(maxdate, mydate) then
if modules.formatDate.before(mydate, mindate) and modules.formatDate.before(maxdate, mydate) then
return true
return true
end
end
Ligne 636 : Ligne 686 :
return condition(claim)
return condition(claim)
end
end
return formatError('invalid type', 'function', type(condition))  
return formatError('invalid type', 'function', type(condition))
end
end


local function minPrecision(claim, minprecision)
local function minPrecision(claim, minprecision)
local snack
local snak
if claim.qualifiers then -- si une date est donnée en qualificatif, c'est elle qu'on utilise de préférence au mainsnak
if claim.qualifiers then -- si une date est donnée en qualificatif, c'est elle qu'on utilise de préférence au mainsnak
for i, j in ipairs(datequalifiers) do
for i, j in ipairs(datequalifiers) do
if claim.qualifiers[j] then
if claim.qualifiers[j] then
snack = claim.qualifiers[j][1]  
snak = claim.qualifiers[j][1]
break
break
end
end
end
end
end
end
if not snack then
if not snak then
snack = claim.mainsnak or claim
snak = claim.mainsnak or claim
end
end
if (snack.snaktype == 'value') and (snack.datatype == 'time') and (snack.datavalue.value.precision < minprecision) then
if (snak.snaktype == 'value') and (snak.datatype == 'time') and (snak.datavalue.value.precision < minprecision) then
return false
return false
end
end
Ligne 662 : Ligne 712 :
return nil
return nil
end
end
if sorttype == 'chronological' then
if wd.isHere({'chronological', 'order', 'inverted', 'age', 'ageinverted'}, sorttype) then
return wd.chronoSort(claims)
return wd.chronoSort(claims, sorttype)
elseif sorttype == 'inverted' then
return wd.chronoSort(claims, true)
elseif sorttype == 'order' then
return wd.chronoSort(claims)
elseif sorttype == 'ascending' then
elseif sorttype == 'ascending' then
return wd.quantitySort(claims)
return wd.quantitySort(claims)
elseif sorttype == 'descending' then
elseif sorttype == 'descending' then
return wd.quantitySort(claims, true)
return wd.quantitySort(claims, true)
elseif sorttype == 'alphabetical' then
return wd.alphabetSort(claims)
elseif sorttype == "language" then
return wd.sortByLanguage(claims)
elseif type(sorttype) == 'function' then
elseif type(sorttype) == 'function' then
table.sort(claims, sorttype)
table.sort(claims, sorttype)
Ligne 697 : Ligne 747 :
filter('condition', check, {'condition'} )
filter('condition', check, {'condition'} )
if claims[1] and claims[1].mainsnak then
if claims[1] and claims[1].mainsnak then
filter('targetvalue', hasTargetValue, {'targetvalue'} )
filter('targetvalue', hasTargetValue, {'targetvalue','maxdepth'} )
filter('targetclass', hasTargetClass, {'targetclass','maxdepth'} )
filter('targetsuperclass', hasTargetSuperclass, {'targetsuperclass'} )
filter('atdate', atDate, {'atdate'} )
filter('atdate', atDate, {'atdate'} )
filter('qualifier', wd.hasQualifier, {'qualifier', 'qualifiervalue'} )
filter('qualifier', wd.hasQualifier, {'qualifier', 'qualifiervalue'} )
Ligne 705 : Ligne 757 :
filter('withdate', wd.hasDate, {} )
filter('withdate', wd.hasDate, {} )
filter('excludevalues', excludeValues, {'excludevalues'})
filter('excludevalues', excludeValues, {'excludevalues'})
filter('excludeclasses', excludeClasses, {'excludeclasses','maxdepth'})
filter('excludesuperclasses', excludeSuperclasses, {'excludesuperclasses','maxdepth'})
filter('withlink', hasLink, {'withlink', 'linklang'} )
filter('withlink', hasLink, {'withlink', 'linklang'} )
filter('minprecision', minPrecision, {'minprecision'} )
filter('minprecision', minPrecision, {'minprecision'} )
Ligne 728 : Ligne 782 :
if type(entity) ~= 'table' then
if type(entity) ~= 'table' then
if cache then
if cache then
if not cache[entity] then  
if not cache[entity] then
cache[entity] = mw.wikibase.getEntity(entity)
cache[entity] = mw.wikibase.getEntity(entity)
mw.log("cached")
mw.log("cached")
Ligne 739 : Ligne 793 :
         return mw.wikibase.getEntity(entity)
         return mw.wikibase.getEntity(entity)
         end
         end
     else  
     else
     return entity
     return entity
     end
     end
Ligne 769 : Ligne 823 :
             entity = mw.wikibase.getEntityIdForCurrentPage()
             entity = mw.wikibase.getEntityIdForCurrentPage()
         end
         end
if (not entity) or (entity == '-') then
if (not entity) or (entity == '-') or (entity == wd.translate('somevalue')) or (entity == modules.linguistic.ucfirst(wd.translate('somevalue'))) then
return nil
return nil
end
if args.labelformat and args.labelformat == 'gendered' then
args.labelformat = wd.getgender(entity)
end
end
local claims = {}
local claims = {}
Ligne 778 : Ligne 835 :
else
else
for i, prop in ipairs(properties) do
for i, prop in ipairs(properties) do
local newclaims = mw.wikibase.getAllStatements(entity, prop)  
local newclaims = mw.wikibase.getAllStatements(entity, prop)
if newclaims and #newclaims > 0 then
if newclaims and #newclaims > 0 then
for j, claim in ipairs(newclaims) do
for j, claim in ipairs(newclaims) do
Ligne 797 : Ligne 854 :


function wd.getLabel(entity, lang1, lang2)
function wd.getLabel(entity, lang1, lang2)
 
if (not entity) then
if (not entity) then
return nil -- ou option de gestion des erreurs ?
return nil -- ou option de gestion des erreurs ?
Ligne 803 : Ligne 860 :
entity = entity.id or ( type(entity) == "string" and entity)
entity = entity.id or ( type(entity) == "string" and entity)
if not(type(entity) == 'string') then return nil end
if not(type(entity) == 'string') then return nil end
 
lang1 = lang1 or defaultlang
lang1 = lang1 or defaultlang
 
local str, lang --str : texte rendu, lang : langue de celui-ci
local str, lang --str : texte rendu, lang : langue de celui-ci
if lang1 == defaultlang then -- le plus économique
if lang1 == defaultlang then -- le plus économique
Ligne 813 : Ligne 870 :
if str then lang = lang1 end
if str then lang = lang1 end
end
end
if str and (lang == lang1) then --pas de catégorie "à traduire" si on a obtenu un texte dans la langue désirée (normalement fr)
if str and (lang == lang1 or lang == "mul") then --pas de catégorie "à traduire" si on a obtenu un texte dans la langue désirée (normalement fr) ou multilingue
return str
return str
end
end
Ligne 823 : Ligne 880 :
end
end
end
end
if not str then --si ni lang1, ni lang2 ni l'anglais ne sont présents, parcours de la hiérarchie des langues
if not str then --si ni lang1, ni lang2 ni l'anglais ne sont présents, parcours de la hiérarchie des langues
for _, trylang in ipairs(modules.langhierarchy.codes) do
for _, trylang in ipairs(databases.langhierarchy.codes) do
str = mw.wikibase.getLabelByLang(entity, trylang)
str = mw.wikibase.getLabelByLang(entity, trylang)
if str then
if str then
Ligne 832 : Ligne 889 :
end
end
end
end
 
if str then
if str then
local translationCat = modules.i18n['to translate']
local translationCat = databases.i18n['to translate']
translationCat = translationCat .. (modules.langhierarchy.cattext[lang] or '')
translationCat = translationCat .. (databases.langhierarchy.cattext[lang] or '')
translationCat = addCat(translationCat)
translationCat = addCat(translationCat)
return str, translationCat
return str, translationCat
Ligne 856 : Ligne 913 :
local displayformat = params.displayformat
local displayformat = params.displayformat
local labelformat = params.labelformat
local labelformat = params.labelformat
local labelformat2 = params.labelformat2
local defaultlabel = params.defaultlabel or id
local defaultlabel = params.defaultlabel or id
local linktype = params.link
local linktype = params.link
Ligne 866 : Ligne 924 :
if params.displayformat == 'raw' then
if params.displayformat == 'raw' then
return id
return id
end
if wd.isGender[params.labelformat] then
labelformat = function(objectid) return wd.genderedlabel(objectid, params.labelformat) end
end
end


local link, label, translationCat
local label, translationCat
 
if type(labelformat) == 'function' then -- sert aux cas particuliers
if type(labelformat) == 'function' then -- sert à des cas particuliers
label, translationCat = labelformat(entity)
label, translationCat = labelformat(entity)
end
end
 
if not label then label, translationCat = wd.getLabel(entity, lang, params.wikidatalang) end
if not label then
label, translationCat = wd.getLabel(entity, lang, params.wikidatalang)
end
 
if labelformat == 'bold' or labelformat2 == 'bold' then --mises en forme typographiques simples accessibles depius le wikicode
labelformat2 = function(str) if label then return "'''" .. str .. "'''" end end
end
if labelformat == 'italic' or labelformat2 == 'italic' then
labelformat2 = function(str) if label then return "''" .. str .. "''" end end
end
if type(labelformat2) == 'function' and label then
label = labelformat2(label)
end


translationCat = translationCat or "" -- sera toujours ajoutée au résultat mais sera vide si la catégorie de maintenance n'est pas nécessaire
translationCat = translationCat or "" -- sera toujours ajoutée au résultat mais sera vide si la catégorie de maintenance n'est pas nécessaire
 
-- détermination du fait qu'on soit ou non en train de rendre l'élément sur la page de son article
local rendering_entity_on_its_page = wd.isPageOfQId(id)
if not label then
if not label then
if (defaultlabel == '-') then  
if (defaultlabel == '-') then
return nil
return nil
end
end
link = wd.siteLink(id, 'wikidata')
local link = wd.siteLink(id, 'wikidata')
return '[[' .. link .. '|' .. id .. ']]' .. translationCat
return '[[' .. link .. '|' .. id .. ']]' .. translationCat
-- si pas de libellé, on met un lien vers Wikidata pour qu'on comprenne à quoi ça fait référence
-- si pas de libellé, on met un lien vers Wikidata pour qu'on comprenne à quoi ça fait référence
end
end
if params.ucfirst_entity then
label =  modules.linguistic.ucfirst(label)
end
-- détermination du fait qu'on soit ou non en train de rendre l'élément sur la page de son article
local rendering_entity_on_its_page = wd.isPageOfQId(id)


if (linktype == '-') or rendering_entity_on_its_page then
if (linktype == '-') or rendering_entity_on_its_page then
Ligne 912 : Ligne 989 :
end
end
end
end
end
end
end
end


if link then  
if link then
if link:match('^Category:') or link:match('^Catégorie:') then -- attention, le « é » est multibyte
-- lier vers une catégorie au lieu de catégoriser
link = ':' .. link
end
return '[[' .. link .. '|' .. label .. ']]' .. translationCat
return '[[' .. link .. '|' .. label .. ']]' .. translationCat
end
end
Ligne 926 : Ligne 1 007 :
local linktype
local linktype
local sidelink, site, langcode
local sidelink, site, langcode
 
if type(defaultlink) == 'string' then
if type(defaultlink) == 'string' then
defaultlink = {defaultlink}
defaultlink = {defaultlink}
Ligne 939 : Ligne 1 020 :
sidelink, site = wd.siteLink(entity, 'wikidata')
sidelink, site = wd.siteLink(entity, 'wikidata')
end
end
 
local icon, class, title = site, nil, nil -- le texte affiché du lien
local icon, class, title = site, nil, nil -- le texte affiché du lien
if site == 'wiki' then
if site == 'wiki' then
icon, class, title = langcode, "indicateur-langue", wd.translate('see-another-language', mw.language.fetchLanguageName(langcode, defaultlang))
icon, class, title = langcode, "indicateur-langue", wd.translate('see-another-language', mw.language.fetchLanguageName(langcode, defaultlang))
elseif site == 'wikidata' then
elseif site == 'wikidata' then
icon, class, title = 'd', "indicateur-langue", wd.translate('see-wikidata')
icon, class, title = 'd', "indicateur-langue", wd.translate('see-wikidata')
else
else
title = wd.translate('see-another-project', site)
title = wd.translate('see-another-project', site)
Ligne 950 : Ligne 1 031 :
local val = '[[' .. sidelink .. '|' .. '<span class = "' .. (class or '').. '" title = "' .. (title or '') .. '">' .. icon .. '</span>]]'
local val = '[[' .. sidelink .. '|' .. '<span class = "' .. (class or '').. '" title = "' .. (title or '') .. '">' .. icon .. '</span>]]'
return label .. ' <small>(' .. val .. ')</small>' .. translationCat
return label .. ' <small>(' .. val .. ')</small>' .. translationCat
end  
end
return label .. translationCat
return label .. translationCat
end
end


function wd.addTrackingCat(prop, cat) -- doit parfois être appelé par d'autres modules
function wd.addTrackingCat(prop, cat) -- doit parfois être appelé par d'autres modules
if type(prop) == 'table' then
if type(prop) == 'table' then
prop = prop[1] -- devrait logiquement toutes les ajouter
local catTable = {}
for i, v in ipairs(prop) do
    catTable[i] = wd.addTrackingCat(v,cat)
end
return table.concat(catTable)
end
end
if not prop and not cat then
if not prop and not cat then
Ligne 965 : Ligne 1 049 :
cat = wd.translate('trackingcat', prop or 'P??')
cat = wd.translate('trackingcat', prop or 'P??')
end
end
return addCat(cat )
return addCat(cat)
end
end


Ligne 1 000 : Ligne 1 084 :


local function getLangCode(entityid)
local function getLangCode(entityid)
return modules.langcodes[tonumber(entityid:sub(2))]
return databases.langcodes[tonumber(entityid:sub(2))]
end
end


local function showLang(statement, maxLang) -- retourne le code langue entre paranthèse avant la valeur (par exemple pour les biblios et liens externes)
local function showLang(statement) -- retourne le code langue entre parenthèses avant la valeur (par exemple pour les biblios et liens externes)
local mainsnak = statement.mainsnak
local mainsnak = statement.mainsnak
if mainsnak.snaktype ~= 'value' then
if mainsnak.snaktype ~= 'value' then
Ligne 1 015 : Ligne 1 099 :
else
else
for i, j in pairs( statement.qualifiers.P407 ) do
for i, j in pairs( statement.qualifiers.P407 ) do
if j.snaktype == 'value' then
if j.snaktype == 'value' then
local langentity = wd.getId(j)
local langentity = wd.getId(j)
local langcode = getLangCode(langentity)
local langcode = getLangCode(langentity)
table.insert(langlist, langcode)
table.insert(langlist, langcode)
end
end
Ligne 1 023 : Ligne 1 107 :
end
end
if (#langlist > 1) or (#langlist == 1 and langlist[1] ~= defaultlang) then -- si c'est en français, pas besoin de le dire
if (#langlist > 1) or (#langlist == 1 and langlist[1] ~= defaultlang) then -- si c'est en français, pas besoin de le dire
langlist.maxLang = maxLang
langlist.maxLang = 3
return modules.langmodule.indicationMultilingue(langlist)
return modules.langmodule.indicationMultilingue(langlist)
end
end
Ligne 1 031 : Ligne 1 115 :
-- === DATE HANDLING ===
-- === DATE HANDLING ===


function wd.addStandardQualifs(str, statement)
local function fuzzydate(str, precision) -- ajoute le qualificatif "vers" à une date
if not str then
return nil
end
if (precision >= 11) or (precision == 7) or (precision == 6) then --dates avec jour, siècles, millénaires
return "vers le " .. str
end
if (precision == 8) then --décennies ("années ...")
return "vers les " .. str
end
return "vers " .. str
end
 
function wd.addStandardQualifs(str, statement, onlygeneral)
-- qualificateurs de date ou de lieu approximatif ou d'info globalement incertaine ; onlygenereal=true pour rerstreindre à ces derniers
if (not statement) or (not statement.qualifiers) then
if (not statement) or (not statement.qualifiers) then
return str
return str
Ligne 1 042 : Ligne 1 140 :
for i, j in pairs(statement.qualifiers.P1480) do
for i, j in pairs(statement.qualifiers.P1480) do
local v = wd.getId(j)
local v = wd.getId(j)
if (v == "Q21818619") then
if (v == "Q21818619") and not onlygeneral then --"à proximité de"
str = wd.translate('approximate-place', str)
str = wd.translate('approximate-place', str)
elseif (v == "Q18122778") or (v == "Q18912752") then
elseif (v == "Q18122778") or (v == "Q18912752") or (v == "Q56644435") or (v == "Q30230067") then --"présumé", "controversé", "probablement", "possible"
str = wd.translate('uncertain-information', str)
str = wd.translate('uncertain-information', str)
elseif (v == "Q5727902") then
elseif (v == "Q5727902") and not onlygeneral and statement.mainsnak.datatype == 'time' then --date approximative
if (statement.mainsnak.datatype == 'time') then
local datevalue = statement.mainsnak.datavalue
str = modules.formatDate.fuzzydate(str)
if datevalue then str = fuzzydate(str, datevalue.value.precision) end
else
end
str = wd.translate('approximate-value', str)
end
end
end
end
end
end
return str
return str
end
local function rangeObject(begin, ending, params)
--[[
objet comportant un timestamp pour le classement chronologique et deux dateobject (begin et ending)
]]--
local timestamp
if begin then
timestamp = begin.timestamp
else
timestamp = ending.timestamp
end
return {begin = begin, ending = ending, timestamp = timestamp, type = 'rangeobject'}
end
local function dateObject(orig, params)
--[[ transforme un snak en un nouvel objet utilisable par Module:Date complexe
{type = 'dateobject', timestamp = str, era = '+' ou '-', year = number, month = number, day = number, calendar = calendar}
]]--
if not params then
params = {}
end
local newobj = modules.formatDate.splitDate(orig.time, orig.calendarmodel)
newobj.precision = params.precision or orig.precision
newobj.type = 'dateobject'
return newobj
end
local function objectToText(obj, params)
if obj.type == 'dateobject' then
return modules.formatDate.simplestring(obj, params)
elseif obj.type == 'rangeobject' then
return modules.formatDate.daterange(obj.begin, obj.ending, params)
end
end
end


Ligne 1 102 : Ligne 1 161 :
return nil
return nil
end
end
return dateObject(v.datavalue.value)
return modules.formatDate.dateObject(v.datavalue.value)
end
end


Ligne 1 110 : Ligne 1 169 :
return period
return period
end
end
local begin, ending = wd.getDateFromQualif(statement, 'P580'), wd.getDateFromQualif(statement, 'P582')
local begin, ending = wd.getDateFromQualif(statement, 'P580'), wd.getDateFromQualif(statement, 'P582')
if begin or ending then
if begin or ending then
return rangeObject(begin, ending) -- retourne un rangeobject fait de deux dateobject
return modules.formatDate.rangeObject(begin, ending) -- retourne un rangeobject fait de deux dateobject
end
end
return nil
return nil
Ligne 1 122 : Ligne 1 181 :
end
end
local str
local str


--cherche la date avec les qualifs P580/P582
--cherche la date avec les qualifs P580/P582
local datetable = wd.getDate(statement)
local datetable = wd.getDate(statement)
if datetable then
if datetable then
str = objectToText(datetable, params)
str = modules.formatDate.objectToText(datetable, params)
end
end
 
-- puis limite intérieur / supérieur
-- puis limite intérieur / supérieur
if not str then
if not str then
Ligne 1 135 : Ligne 1 193 :
str = modules.formatDate.between(start, ending, params)
str = modules.formatDate.between(start, ending, params)
end
end
local fromqualif = false
if str then fromqualif = true end --si la date est tirée des qualificateurs, on n'y ajoute pas l'éventuel "vers ..."


-- sinon, le mainsnak, pour les données de type time
-- sinon, le mainsnak, pour les données de type time
Ligne 1 145 : Ligne 1 206 :


if str and params and (params.addstandardqualifs ~= '-') then
if str and params and (params.addstandardqualifs ~= '-') then
str = wd.addStandardQualifs(str, statement)
str = wd.addStandardQualifs(str, statement, fromqualif)
end
end
return str
return str
end
end
Ligne 1 152 : Ligne 1 214 :
wd.compare.by_quantity = function(c1, c2)
wd.compare.by_quantity = function(c1, c2)
local v1 = wd.getDataValue(c1.mainsnak)
local v1 = wd.getDataValue(c1.mainsnak)
local v2 = wd.getDataValue(c2.mainsnak)  
local v2 = wd.getDataValue(c2.mainsnak)
if not (v1 and v2) then
if not (v1 and v2) then
return true
return true
Ligne 1 159 : Ligne 1 221 :
end
end


--[[ tri chronologique générique :  
--[[ tri chronologique générique :
             retourne une fonction de tri de liste de déclaration
             retourne une fonction de tri de liste de déclaration
             en fonction d’une fonction qui calcule la clé de tri
             en fonction d’une fonction qui calcule la clé de tri
             et d’une fonction qui compare les clés de tri
             et d’une fonction qui compare les clés de tri
 
 
   paramètres nommés: (appel type wikidata.compare.chrono_key_sort{sortKey="nom clé"})
   paramètres nommés: (appel type wikidata.compare.chrono_key_sort{sortKey="nom clé"})
         sortKey (optionnel)  : chaine, le nom de la clé utilisée pour un tri  
         sortKey (optionnel)  : chaine, le nom de la clé utilisée pour un tri
                                 (pour éviter de rentrer en collision avec "dateSortKey"  
                                 (pour éviter de rentrer en collision avec "dateSortKey"
                                 utilisé par chronoSort au besoin)
                                 utilisé par chronoSort au besoin)
         snak_key_get_function : fonction qui calcule la valeur de la clé à partir d’un snak ou d’une déclaration,
         snak_key_get_function : fonction qui calcule la valeur de la clé à partir d’un snak ou d’une déclaration,
Ligne 1 172 : Ligne 1 234 :
         key_compare_function  : fonction de comparaison des clés calculées par snak_key_get_function
         key_compare_function  : fonction de comparaison des clés calculées par snak_key_get_function
         (optionnel)
         (optionnel)
--]]  
--]]


function wd.chrono_key_sort(arg)
function wd.chrono_key_sort(arg)
 
local snak_key_get_function = arg.snak_key_get_function
local snak_key_get_function = arg.snak_key_get_function
local sortKey = arg.sortKey or "dateSortKey"
local sortKey = arg.sortKey or "dateSortKey"
local key_compare_function = arg.key_compare_function or  
local key_compare_function = arg.key_compare_function or
function(c1, c2) return c1 < c2 end
function(c1, c2) return c1 < c2 end
return function(claims)
return function(claims)
Ligne 1 205 : Ligne 1 267 :
local function sort(c1, c2)
local function sort(c1, c2)
local v1 = wd.getDataValue(c1.mainsnak)
local v1 = wd.getDataValue(c1.mainsnak)
local v2 = wd.getDataValue(c2.mainsnak)  
local v2 = wd.getDataValue(c2.mainsnak)
if not (v1 and v2) then
if not (v1 and v2) then
return true
return true
Ligne 1 218 : Ligne 1 280 :
end
end


function wd.compare.get_claim_date(claim)
function wd.alphabetSort(claims)
local snack = claim.mainsnak or claim
local function sort(c1, c2)
local iso
local v1 = wd.getDataValue(c1.mainsnak, {link = '-', displayformat = {monolingualtext = 'raw'}})
if (snack.snaktype == 'value') and (snack.datatype == 'time') then
local v2 = wd.getDataValue(c2.mainsnak, {link = '-', displayformat = {monolingualtext = 'raw'}})
iso = snack.datavalue.value.time
if not (v1 and v2) then
return true
end
return modules.linguistic.makeSortkey(v1) < modules.linguistic.makeSortkey(v2)
end
table.sort(claims, sort )
return claims
end
 
function wd.compare.get_claim_date(claim, datetype) -- rend une date au format numérique pour faire des comparaisons
local snak = claim.mainsnak or claim
 
if datetype and datetype == 'personbirthdate' then -- fonctionne avec un claim dont la valeur est une personne dont on va rendre la date de naissance
if (snak.snaktype == 'value') and (snak.datatype == 'wikibase-item') then
local personid = wd.getId(snak)
local birthclaims = wd.getClaims({ entity = personid, property = 'P569', numval = 1})
if birthclaims then
return wd.compare.get_claim_date(birthclaims[1] or birthclaims)
else return math.huge end
else return math.huge end -- en cas de donnée manquante, valeur infinie qui entraîne le classement en fin de liste
end
 
local iso, datequalif, isonumber
if (snak.snaktype == 'value') and (snak.datatype == 'time') then
iso = snak.datavalue.value.time
else
else
iso = timeFromQualifs(claim, datequalifiers) or '0'
for i, dqualif in ipairs(datequalifiers) do
iso = timeFromQualifs(claim, {dqualif})
if iso then
datequalif = dqualif
break
end
end
if not iso then return math.huge end
end
end
-- transformation en nombre (indication de la base car gsub retourne deux valeurs)
-- transformation en nombre (indication de la base car gsub retourne deux valeurs)
return tonumber( iso:gsub( '(%d)%D', '%1' ), 10 )
isonumber = tonumber( iso:gsub( '(%d)%D', '%1' ), 10 )
 
-- ajustement de la date tenant compte du qualificatif dont elle est issue : un fait se terminant à une date est antérieur à un autre commençant à cette date
if datequalif == 'P582' then --date de fin
isonumber = isonumber - 2
elseif datequalif == 'P1326' then -- date au plus tard
isonumber = isonumber - 1
elseif datequalif == 'P1319' then -- date au plus tôt
isonumber = isonumber + 1
elseif datequalif == 'P571' or datequalif == 'P580' then -- date de début et date de création
isonumber = isonumber + 2
end
 
return isonumber
end
end


Ligne 1 247 : Ligne 1 353 :
-- Si des clés de tri de ce nom existent déjà, elles sont utilisées sans modification.
-- Si des clés de tri de ce nom existent déjà, elles sont utilisées sans modification.


function wd.chronoSort( claims, inverted )
function wd.chronoSort( claims, sorttype )
for _, claim in ipairs( claims ) do
for _, claim in ipairs( claims ) do
if not claim.dateSortKey then
if not claim.dateSortKey then
claim.dateSortKey = wd.compare.get_claim_date(claim)
if sorttype and (sorttype == 'age' or sorttype == 'ageinverted') then
claim.dateSortKey = wd.compare.get_claim_date(claim, 'personbirthdate')
else
claim.dateSortKey = wd.compare.get_claim_date(claim)
end
if sorttype and (sorttype == 'inverted' or sorttype == 'ageinverted') and claim.dateSortKey == math.huge then
claim.dateSortKey = -math.huge -- quand la donnée est manquante on lui assigne la valeur qui entraîne le classement en fin de liste
end
end
end
end
end
table.sort(  
table.sort(
claims,
claims,
function ( c1, c2 )
function ( c1, c2 )
if inverted then
if sorttype and (sorttype == 'inverted' or sorttype == 'ageinverted') then
return c2.dateSortKey < c1.dateSortKey
return c2.dateSortKey < c1.dateSortKey
end
end
Ligne 1 265 : Ligne 1 378 :
end
end


local function get_numeric_claim_value(claim, propertySort)
-- Function to prioritize certain languages in a multilingual label context
local val
-- prioritize french, mul, roman languages, english if not all values needed
function wd.sortByLanguage(claims)
-- Arbitrary order for now, ask to make it a parameter if needed
local priority = {
fr=1, mul=2, ["en-gb"]=3, en=4, es=5, it=6, de=7
}
-- maybe something to generalize a bit :
-- local wikilang = mw.getContentLanguage()
-- local priority = wikilang:getFallbackLanguages()
-- priority.insert(priority, 1, wikilang.code)
local function get_priority(lang)
local attempt = priority[lang]
if attempt ~= nil then
return attempt
else
return math.huge
end
end
local function get_snak_value(mainsnak)
if mainsnak.datavalue and
mainsnak.datavalue.value then
return mainsnak.datavalue.value
end
end
-- get the main val as an object
local function get_main_value(claim)
if claim.mainsnak then
return get_snak_value(claim.mainsnak)
end
end
local function sortfunction(v1, v2)
-- get a value if they are statements or qualifiers as well
local str1 = get_main_value(v1) or get_snak_value(v1)
local str2 = get_main_value(v2) or get_snak_value(v2)
 
if str1 ~= nil and str2 ~= nil then
local prio1 = get_priority(str1.language)
local prio2 = get_priority(str2.language)
return prio1 < prio2
elseif str1 then
return true
else
return false
end
end
 
table.sort(claims, sortfunction)
return claims
end
 
local function get_numeric_claim_value(claim, propertySort)
local val
local claimqualifs = claim.qualifiers
local claimqualifs = claim.qualifiers
if claimqualifs then
if claimqualifs then
Ligne 1 304 : Ligne 1 472 :


--[[
--[[
test possible en console pour la fonction précédente :  
test possible en console pour la fonction précédente :
= p.formatStatements{entity = "Q375946", property = 'P50', sorttype = 'P1545', linkback = "true"}
= p.formatStatements{entity = "Q375946", property = 'P50', sorttype = 'P1545', linkback = "true"}
--]]
--]]
Ligne 1 324 : Ligne 1 492 :
end
end
return false
return false
end
end


if ref.snaks.P248 then -- cas lorsque P248 (affirmé dans) est utilisé  
if ref.snaks.P248 then -- cas lorsque P248 (affirmé dans) est utilisé
for j, source in pairs(ref.snaks.P248) do
for j, source in pairs(ref.snaks.P248) do
if source.snaktype == 'value' then
if source.snaktype == 'value' then
local page, accessdate, quotation
local page, accessdate, quotation
if hasValue('P304') then -- page  
if hasValue('P304') then -- page
page = wd.formatSnak(ref.snaks.P304[1])
page = wd.formatSnak(ref.snaks.P304[1])
end
end
Ligne 1 345 : Ligne 1 513 :
end
end
end
end
 
elseif hasValue('P8091') or hasValue('P854') then -- cas lorsque P8091 (Archival Resource Key) ou P854 (URL de la référence)est utilisé
elseif hasValue('P8091') or hasValue('P854') then -- cas lorsque P8091 (Archival Resource Key) ou P854 (URL de la référence)est utilisé
local arkKey, url, title, author, publisher, accessdate, publishdate, publishlang, quotation, description
local arkKey, url, title, author, publisher, accessdate, publishdate, publishlang, quotation, description
Ligne 1 373 : Ligne 1 541 :
accessdate = wd.formatSnak(ref.snaks.P813[1])
accessdate = wd.formatSnak(ref.snaks.P813[1])
end
end
if hasValue('P50') then -- author (item type)
if hasValue('P50') then -- author (item type)
author = wd.formatSnak(ref.snaks.P50[1])
author = wd.formatSnak(ref.snaks.P50[1])
elseif hasValue('P2093') then -- author (string type)
elseif hasValue('P2093') then -- author (string type)
Ligne 1 386 : Ligne 1 554 :
if hasValue('P577') then -- date de publication
if hasValue('P577') then -- date de publication
publishdate = wd.formatSnak(ref.snaks.P577[1])
publishdate = wd.formatSnak(ref.snaks.P577[1])
end  
end
if hasValue('P407') then -- langue de l'œuvre
if hasValue('P407') then -- langue de l'œuvre
local id = wd.getId(ref.snaks.P407[1])
local id = wd.getId(ref.snaks.P407[1])
Ligne 1 436 : Ligne 1 604 :
local datatype = snak.datatype
local datatype = snak.datatype
local value = snak.datavalue.value
local value = snak.datavalue.value
 
local displayformat = params.displayformat
local displayformat = params.displayformat
if type(displayformat) == 'table' then
displayformat = displayformat[datatype]
end
if type(displayformat) == 'function' then
if type(displayformat) == 'function' then
return displayformat(snak, params)
return displayformat(snak, params)
Ligne 1 459 : Ligne 1 630 :


if datatype == 'tabular-data' then
if datatype == 'tabular-data' then
return mw.ustring.sub(value, 6, 100) -- returns the name of the file, without the "Data:" prefix
return mw.ustring.sub(value, 6, 100) -- returns the name of the file, without the "Data:" prefix
end
end


Ligne 1 476 : Ligne 1 647 :
return value
return value
end
end
 
if datatype == 'time' then -- format example: +00000001809-02-12T00:00:00Z
if datatype == 'time' then -- format example: +00000001809-02-12T00:00:00Z
if displayformat == 'raw' then
if displayformat == 'raw' then
return value.time
return value.time
else
else
local dateobject = dateObject(value, {precision = params.precision})
local dateobject = modules.formatDate.dateObject(value, {precision = params.precision})
return objectToText(dateobject, params)
return modules.formatDate.objectToText(dateobject, params)
end
end
end
end
Ligne 1 494 : Ligne 1 665 :
else
else
local coordvalue = mw.clone( value )
local coordvalue = mw.clone( value )
coordvalue.globe = modules.globes[value.globe] -- transforme l'ID du globe en nom anglais utilisable par geohack
coordvalue.globe = databases.globes[value.globe] -- transforme l'ID du globe en nom anglais utilisable par geohack
return coordvalue -- note : les coordonnées Wikidata peuvent être utilisée depuis Module:Coordinates. Faut-il aussi autoriser à appeler Module:Coordiantes ici ?
return coordvalue -- note : les coordonnées Wikidata peuvent être utilisée depuis Module:Coordinates. Faut-il aussi autoriser à appeler Module:Coordiantes ici ?
end
end
Ligne 1 505 : Ligne 1 676 :
unit = unit:match('Q%d+')
unit = unit:match('Q%d+')
end
end
 
if not unit then
if not unit then
unit = 'dimensionless'
unit = 'dimensionless'
end
end
 
local raw
local raw
if displayformat == "raw" then
if displayformat == "raw" then
raw = true
raw = true
Ligne 1 519 : Ligne 1 690 :
end
end
if datatype == 'monolingualtext' then
if datatype == 'monolingualtext' then
if value.language == defaultlang then
if value.language == defaultlang or displayformat == 'raw' then
return value.text
return value.text
else
else
return modules.langmodule.langue({value.language, value.text, nocat=true})
return modules.langmodule.langue({value.language, value.text, nocat=true})
end
end
end
end
return formatError('unknown-datavalue-type' )
return formatError('unknown-datavalue-type' )


Ligne 1 580 : Ligne 1 751 :
return nil
return nil
end
end
qualiftable = wd.filterClaims(qualiftable, params) or {}
qualiftable = wd.filterClaims(qualiftable, params) or {}
-- Sorting to prioritize languages
if(params.showonlyqualifier ~= nil) then
qualiftable = wd.sortClaims(qualiftable, params.sorttype or params.qualifsorttype)
end
for i, j in pairs(qualiftable) do
for i, j in pairs(qualiftable) do
qualiftable[i] = wd.formatSnak(j, params)
qualiftable[i] = wd.formatSnak(j, params)
Ligne 1 588 : Ligne 1 766 :


function wd.showQualifiers(str, statement, args)
function wd.showQualifiers(str, statement, args)
local qualifs = args.showqualifiers
local qualifs = args.showqualifiers
if not qualifs then
if not qualifs then
return str -- or error ?
return str -- or error ?
Ligne 1 599 : Ligne 1 777 :
qualifargs.displayformat = args.qualifdisplayformat or args.displayformat
qualifargs.displayformat = args.qualifdisplayformat or args.displayformat
qualifargs.labelformat = args.qualiflabelformat or args.labelformat
qualifargs.labelformat = args.qualiflabelformat or args.labelformat
qualifargs.labelformat2 = args.qualiflabelformat2 or args.labelformat2
qualifargs.link = args.qualiflink or args.link
qualifargs.link = args.qualiflink or args.link
qualifargs.linktopic = args.qualiflinktopic or args.linktopic
qualifargs.linktopic = args.qualiflinktopic or args.linktopic
Ligne 1 607 : Ligne 1 786 :
qualifargs.defaultlinkquery = args.qualifdefaultlinkquery or args.defaultlinkquery
qualifargs.defaultlinkquery = args.qualifdefaultlinkquery or args.defaultlinkquery
if args.qualiflabelformat == 'objectgender' then
local objectid = wd.getId(statement.mainsnak)
qualifargs.labelformat = wd.getgender(objectid)
end
local formattedqualifs
local formattedqualifs
if args.qualifformat and type (args.qualifformat) == 'function' then
if args.qualifformat and type (args.qualifformat) == 'function' then
formattedqualifs = args.qualifformat(statement, qualifs, qualifargs)
formattedqualifs = args.qualifformat(statement, qualifs, qualifargs)
else
else
formattedqualifs = wd.getFormattedQualifiers(statement, qualifs, qualifargs)
formattedqualifs = wd.getFormattedQualifiers(statement, qualifs, qualifargs)
end
end
Ligne 1 637 : Ligne 1 821 :
args = {}
args = {}
end
end
if not statement.type or statement.type ~= 'statement' then
if not statement.type or statement.type ~= 'statement' then
return formatError( 'unknown-claim-type' )
return formatError( 'unknown-claim-type' )
Ligne 1 659 : Ligne 1 844 :
end
end
if args.addstandardqualifs ~= '-' then
if args.addstandardqualifs ~= '-' then
str = wd.addStandardQualifs(str, statement)
str = wd.addStandardQualifs(str, statement, true)
end
end
else
else
Ligne 1 670 : Ligne 1 855 :
-- ajouts divers
-- ajouts divers
if args.showlang == true then
if args.showlang == true then
local indicateur = showLang(statement, args.maxLang)
local indicateur = showLang(statement)
if indicateur then
if indicateur then
str = indicateur .. '&nbsp;' .. str
str = indicateur .. '&nbsp;' .. str
end
end
end
end
Ligne 1 680 : Ligne 1 865 :


if args.showdate then -- when "showdate and chronosort are both set, date retrieval is performed twice
if args.showdate then -- when "showdate and chronosort are both set, date retrieval is performed twice
local period = wd.getFormattedDate(statement, args, "-") -- 3 arguments indicate the we should not use additional qualifiers, alrady added by wd.formatStatement
local period = wd.getFormattedDate(statement, args, "-") -- 3 arguments indicate the we should not use additional qualifiers, already added by wd.formatStatement
if period then
if period then
str = str .. " <small>(" .. period .. ")</small>"
str = str .. " <small>(" .. period .. ")</small>"
Ligne 1 686 : Ligne 1 871 :
end
end


if args.showsource and args.showsource ~= '-' then
if args.showsource and args.showsource ~= '-' and args.showsource ~= "false" then
if args.showsource == "only" then str="" end -- si showsource="only", alors ne montrer que la (les) source(s),
                                            -- sans la valeur qui, auparavant, était enregistrée dans str
                                            -- Utilisé par le modèle {{PH census}}
local sources, hashes = wd.getReferences(statement)
local sources, hashes = wd.getReferences(statement)
if sources then
if sources then
Ligne 1 709 : Ligne 1 897 :
property = property[1]
property = property[1]
end
end
 
id = wd.entityId(id)
id = mw.text.trim(wd.entityId(id))


local class = ''
local class = ''
Ligne 1 725 : Ligne 1 913 :
:wikitext(str)
:wikitext(str)
:tag('span')
:tag('span')
:addClass('noprint wikidata-linkback')
:addClass('noprint wikidata-linkback skin-invert')
:wikitext(icon:format(title, url))
:wikitext(icon:format(title, url))
:allDone()
:allDone()
Ligne 1 758 : Ligne 1 946 :
local function addClaim(claim)
local function addClaim(claim)
local id = wd.getMainId(claim)
local id = wd.getMainId(claim)
for i, j in pairs(groupedClaims) do  
for i, j in pairs(groupedClaims) do
if (j.id == id) then
if (j.id == id) then
table.insert(groupedClaims[i].claims, claim)
table.insert(groupedClaims[i].claims, claim)
Ligne 1 766 : Ligne 1 954 :
table.insert(groupedClaims, {id = id, claims = {claim}})
table.insert(groupedClaims, {id = id, claims = {claim}})
end
end
for i, claim in pairs(claims) do
for i, claim in pairs(claims) do
addClaim(claim)
addClaim(claim)
end
end
Ligne 1 808 : Ligne 1 996 :
local sources = {}
local sources = {}
local hashes = {}
local hashes = {}
 
local function dupeRef(old, new)
local function dupeRef(old, new)
for i, j in pairs(old) do
for i, j in pairs(old) do
Ligne 1 845 : Ligne 2 033 :
table.insert(stringTable, str)
table.insert(stringTable, str)
end
end
 
args.valuetable = stringTable
args.valuetable = stringTable
return wd.formatStatements(args)
return wd.formatStatements(args)
Ligne 1 872 : Ligne 2 060 :
return formatStatementsGrouped(args)
return formatStatementsGrouped(args)
end
end
local valuetable = args.valuetable -- dans le cas où les valeurs sont déjà formtées
local valuetable = args.valuetable -- dans le cas où les valeurs sont déjà formatées
local props -- les prorpriétés réellement utilisées (dans certainse cas, ce ne sont pas toutes celles de ags.property
local props -- les propriétés réellement utilisées (dans certains cas, ce ne sont pas toutes celles de args.property
local cat = ''
local cat = ''
if not valuetable then -- cas le plus courant
if not valuetable then -- cas le plus courant
Ligne 1 879 : Ligne 2 067 :
end
end


if args.ucfirst == '-' and args.conjtype == 'new line' then args.conjtype = 'lowercase new line' end
local str = modules.linguistic.conj(valuetable, args.conjtype)
local str = modules.linguistic.conj(valuetable, args.conjtype)
if not str then
if not str then
Ligne 1 915 : Ligne 2 104 :
val = wd.addLinkBack(val, args.entity, args.property)
val = wd.addLinkBack(val, args.entity, args.property)
return val
return val
end  
end
return wd.formatStatements( args )
return wd.formatStatements( args )
end
end
Ligne 1 961 : Ligne 2 150 :
params.addcat = params.addcat or true
params.addcat = params.addcat or true
return wd.getTheDate(params)
return wd.getTheDate(params)
elseif string.sub(event, 1, 1) == 'P' then -- on demande une propriété
elseif string.sub(event, 1, 1) == 'P' then -- on demande une propriété
params.property = event
params.property = event
return wd.formatAndCat(params)
return wd.formatAndCat(params)
Ligne 1 969 : Ligne 2 158 :
end
end


function wd.mainDate(entity)
function wd.mainDate(entity)
-- essaye P580/P582
-- essaye P580/P582
local args = {entity = entity, addcat = true}
local args = {entity = entity, addcat = true}
 
args.property = 'P580'
args.property = 'P580'
local startpoint = wd.formatStatements(args)
local startpoint = wd.formatStatements(args)
Ligne 1 990 : Ligne 2 179 :
return wd.formatStatements(args)
return wd.formatStatements(args)
end
end
-- ==== Fonctions sur le genre ====
function wd.getgender(id)
local vals = {
['Q6581072'] = 'f', -- féminin
['Q6581097'] = 'm', -- masculin
['Q1052281'] = 'f', -- femme transgenre
['Q2449503'] = 'm', -- homme transgenre
['Q17148251'] = 'f', -- en:Travesti (gender identity)
['Q43445'] = 'f', -- femelle
['Q44148'] = 'm', -- mâle
default      = '?'
}
local gender = wd.formatStatements{entity = id, property = 'P21', displayformat = 'raw', numval = 1}
return vals[gender] or vals.default
end
wd.isGender = {m = true, male = true, f = true, female = true} --reconnaissance des chaînes de caractères désignant un genre
-- catégories de genre/nombre
function wd.getgendernum(claims)
local personid, gender
local anym = false
local anyf = false
local anyunknown = false
for i, claim in pairs(claims) do
local snak = claim.mainsnak or claim
if(snak.snaktype == 'value') and (snak.datatype == 'wikibase-item') then
personid = wd.getId(snak)
gender = wd.getgender(personid)
anym = anym or (gender == 'm')
anyf = anyf or (gender == 'f')
anyunknown = anyunknown or (gender == '?')
else
anyunknown = true
end
end
local gendernum
if #claims > 1 then
if anyunknown then
gendernum = 'p'
else
if anym and not anyf then gendernum = 'mp' end
if anyf and not anym then gendernum = 'fp' end
if anym and anyf then gendernum = 'mixtep' end
end
else
gendernum = 's'
if anym then gendernum = 'ms' end
if anyf then gendernum = 'fs' end
end
return gendernum
end
-- récupération des libellés genrés de Wikidata
function wd.genderedlabel(id, labelgender)
local label
if not labelgender then return nil end
if labelgender == 'f' or labelgender == 'female' then -- femme : chercher le libellé dans P2521 (libellé féminin)
label = wd.formatStatements{entity = id, property = 'P2521', isinlang = 'fr', numval = 1, ucfirst = '-'}
elseif labelgender == 'm' or labelgender == 'male' then -- homme : chercher le libellé dans P3321 (libellé masculin)
label = wd.formatStatements{entity = id, property = 'P3321', isinlang = 'fr', numval = 1, ucfirst = '-'}
end
if not label then
label = wd.getLabel(id)
end
return label
end


-- === FUNCTIONS FOR TRANSITIVE PROPERTIES ===
-- === FUNCTIONS FOR TRANSITIVE PROPERTIES ===
Ligne 2 014 : Ligne 2 277 :
local origsize = #list
local origsize = #list
for i = 1, origsize do
for i = 1, origsize do
-- tried a "checkpos" param instead of starting to 1 each time, but no impact on performance
-- tried a "checkpos" param instead of starting to 1 each time, but no impact on performance
local candidates = wd.getIds(list[i], query)
local candidates = wd.getIds(list[i], query)
list = wd.addNewValues(list, candidates, maxnodes, stopval)
list = wd.addNewValues(list, candidates, maxnodes, stopval)
Ligne 2 024 : Ligne 2 287 :
end
end
end
end
 
if (#list == origsize) then
if (#list == origsize) then
return list
return list
Ligne 2 044 : Ligne 2 307 :
return nil
return nil
end
end
local v = wd.addVals(vals, query, maxdepth - 1, maxnodes, stopval)  
local v = wd.addVals(vals, query, maxdepth - 1, maxnodes, stopval)
if not v then
if not v then
return nil
return nil
Ligne 2 064 : Ligne 2 327 :
function wd.inTransitiveVals(searchedval, sourceval, query, maxdepth, maxnodes )
function wd.inTransitiveVals(searchedval, sourceval, query, maxdepth, maxnodes )
local vals = wd.transitiveVals(sourceval, query, maxdepth, maxnodes, searchedval )
local vals = wd.transitiveVals(sourceval, query, maxdepth, maxnodes, searchedval )
if (not vals) then  
if (not vals) then
return false
return false
end
end
Ligne 2 094 : Ligne 2 357 :
if not directclasses then
if not directclasses then
return false
return false
end  
end
for i, class in pairs(directclasses) do
for i, class in pairs(directclasses) do
if wd.isSubclass(targetclass, class, maxdepth - 1) then
if wd.isSubclass(targetclass, class, maxdepth - 1) then
Ligne 2 111 : Ligne 2 374 :
if candidates then
if candidates then
for i, j in pairs(candidates) do
for i, j in pairs(candidates) do
if wd.isInstance(targetclass, j, instancedepth) then
if wd.isInstance(targetclass, j, instancedepth) then
return j
return j
end
end
Ligne 2 136 : Ligne 2 399 :
local description
local description
if lang == defaultlang then
if lang == defaultlang then
return mw.wikibase.description(qid)
return mw.wikibase.description(qid)
end
end
if not entity.descriptions then
if not entity.descriptions then

Dernière version du 22 février 2026 à 00:16

La documentation pour ce module peut être créée à Module:Wikidata/doc

--script that retrieves basic data stored in Wikidata, for the datamodel, see https://www.mediawiki.org/wiki/Extension:Wikibase_Client/Lua
-- luacheck: globals mw

local wd = {}

-- creation of a subobject to store comparison funtions, used for sorting claims
-- to be able to build more complex sorts like topological sorts

wd.compare = {}

local databases = { }
local modules = { }
local databasesNames = { -- modules de données statiques pouvant être appelés avec mw.loadData(), ne nécessitant pas require()
	i18n = 'Module:Wikidata/I18n',
	globes = 'Module:Wikidata/Globes',
	langhierarchy = 'Module:Wikidata/Hiérarchie des langues',
	langcodes = 'Module:Dictionnaire Wikidata/Codes langue', -- big, infrequently used
	invertedlangcodes = 'Module:Dictionnaire Wikidata/Codes langue/inversé'
}
local modulesNames = {
	reference = 'Module:Wikidata/Références',
	linguistic = 'Module:Linguistique',
	datemodule = 'Module:Date',
	formatDate = 'Module:Date complexe',
	formatNum = 'Module:Conversion',
	langmodule = 'Module:Langue',
	cite = 'Module:Biblio',
	weblink = 'Module:Weblink'
}

local function loadDatabase( t, key )
	if databasesNames[key] then
		local m = mw.loadData( databasesNames[key] )
		t[key] = m
		return m
	end
end
local function loadModule( t, key )
	if modulesNames[key] then
		local m = require( modulesNames[key] )
		t[key] = m
		return m
	end
end
setmetatable( databases, { __index = loadDatabase } )
setmetatable( modules, { __index = loadModule } ) -- ainsi le require() sera opéré seulement si nécessaire par modules.(nom du module)

local datequalifiers = {'P585', 'P571', 'P580', 'P582', 'P1319', 'P1326'}

-- === I18n ===
local defaultlang = mw.getContentLanguage():getCode()

function wd.translate(str, rep1, rep2)
	str = databases.i18n[str] or str
	if rep1 and (type (rep1) == 'string') then
		str = str:gsub('$1', rep1)
	end
	if rep2 and (type (rep2) == 'string')then
		str = str:gsub('$2', rep2)
	end
	return str
end

local function addCat(cat, sortkey)
	if sortkey then
		return '[[Category:' .. cat .. '|' .. sortkey .. ']]'
	end
	return '[[Category:' .. cat .. ']]'
end

local function formatError( key , category, debug)
    if debug then
        return error(databases.i18n[key] or key)
    end
    if category then
        return addCat(category, key)
    else
        return addCat(databases.i18n['cat-unsorted-issue'], key)
    end
end

--

function wd.isSpecial(snak)
	return (snak.snaktype ~= 'value')
end

function wd.getId(snak)
	if (snak.snaktype == 'value') then
		return 'Q' .. snak.datavalue.value['numeric-id']
	end
end

function wd.getNumericId(snak)
	if (snak.snaktype == 'value') then
		return snak.datavalue.value['numeric-id']
	end
end

function wd.getMainId(claim)
	return wd.getId(claim.mainsnak)
end

function wd.entityId(entity)
	if type(entity) == 'string' then
		return entity
	elseif type(entity) == 'table' then
		return entity.id
	end
end

function wd.getEntityIdForCurrentPage()
	return mw.wikibase.getEntityIdForCurrentPage()
end

-- function that returns true if the "qid" parameter is the qid
-- of the item that is linked to the calling page
function wd.isPageOfQId(qid)
	local self_id = mw.wikibase.getEntityIdForCurrentPage()
	return self_id ~= nil and qid == self_id
end

function wd.getEntity( val )
	if type(val) == 'table' then
		return val
	end
	if val == '-' then
		return nil
	end
	if val == '' then
		val = nil
	end
	return mw.wikibase.getEntity(val)
end

function wd.splitStr(val) -- transforme en table les chaînes venant du Wikitexte qui utilisent des virgules de séparation
	if type(val) == 'string' then
		val = mw.text.split(val, ",")
	end
	return val
end

function wd.isHere(searchset, val, matchfunction)
	for i, j in pairs(searchset) do
		if matchfunction then
			if matchfunction(val,j) then
				return true
			end
		else
			if val == j then
				return true
			end
		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 wd.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 charger l'élément entier
		local link = mw.wikibase.getSitelink(entity)
		if link then
			local test_redirect = mw.title.new(link) -- remplacement des redirections (retirer si trop coûteux)
			if test_redirect.isRedirect and test_redirect.redirectTarget then
				link = test_redirect.redirectTarget.fullText
				if link == mw.title.getCurrentTitle().text then return nil end --redirection vers l'article de départ : pas de lien plutôt qu'une mise en gras
			end
		end
		return link, 'wiki', defaultlang
	end
	if project == 'wikidata' then
		return wikidataLink(entity), 'wikidata'
	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},
		commonswiki = {'commonswiki', 'commons', false},
		wikiquote = {'wikiquote', 'q', true},
		wikivoyage = {'wikivoyage', 'voy', true},
		wikibooks = {'wikibooks', 'b', true},
		wikinews = {'wikinews', 'n', true},
		wikiversity = {'wikiversity', 'v', true},
		wikisource = {'wikisource', 's', true},
		wiktionary = {'wiktionary', 'wikt', true},
		specieswiki = {'specieswiki', 'species', false},
		metawiki = {'metawiki', 'm', false},
		incubator = {'incubator', 'incubator', false},
		outreach = {'outreach', 'outreach', false},
		mediawiki = {'mediawiki', 'mw', false}
	}

	local entityid = entity.id or entity

	local projectdata = projects[project:lower()]
	if not projectdata then -- defaultlink might be in the form "dewiki" rather than "project: 'wiki', lang: 'de' "
		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)
				project = project:sub(#lang + 1, #project)
				projectdata = projects[project]
				break
			end
		end
		if not mw.language.isKnownLanguageTag(lang) then
			return --formatError('invalid-project-code', projet or 'nil')
		end
	end
	if not projectdata then
		return -- formatError('invalid-project-code', projet or 'nil')
	end

	local linkcode = projectdata[1]
	local prefix = projectdata[2]
	local multiversion = projectdata[3]
	if multiversion then
		linkcode = lang .. linkcode
	end
	local link = mw.wikibase.getSitelink(entityid, 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 wd.addNewValues(olditems, newitems, maxnum, stopval)
	if not newitems then
		return olditems
	end
	for _, qid in pairs(newitems) do
		if stopval and (qid == stopval) then
			table.insert(olditems, qid)
			return olditems
		end
		if maxnum and (#olditems >= maxnum) then
			return olditems
		end
		if not wd.isHere(olditems, qid) then
			table.insert(olditems, qid)
		end
	end
	return olditems
end

--=== FILTER CLAIMS ACCORDING TO VARIOUS CRITERIA : FUNCTION GETCLAIMS et alii ===

local function notSpecial(claim)
	local type

	if claim.mainsnak ~= nil then
		type = claim.mainsnak.snaktype
	else
		-- condition respectée quand showonlyqualifier est un paramètre renseigné
		-- dans ce cas, claim n'est pas une déclaration entière, mais UNE snak qualifiée du main snak
		type = claim.snaktype
	end

	return type == 'value'
end

local function hasTargetValue(claim, targets) -- retourne true si la valeur est dans la liste des target, ou si c'est une valeur spéciale filtrée séparément par excludespecial
	local id = wd.getMainId(claim)
	local targets = wd.splitStr(targets)
	return wd.isHere(targets, id) or wd.isSpecial(claim.mainsnak)
end

local function excludeValues(claim, values) -- true si la valeur n'est pas dans la liste, ou si c'est une valeur spéciale (filtrée à part par excludespecial)
	return wd.isSpecial(claim.mainsnak) or not ( hasTargetValue(claim, values) )
end

local function hasTargetClass(claim, targets, maxdepth) -- retourne true si la valeur est une instance d'une classe dans la liste des target, ou si c'est une valeur spéciale filtrée séparément par excludespecial
	local id = wd.getMainId(claim)
	local targets = wd.splitStr(targets)
	local maxdepth = maxdepth or 10
	local matchfunction = function(value, target) return wd.isInstance(target, value, maxdepth) end
	return wd.isHere(targets, id, matchfunction) or wd.isSpecial(claim.mainsnak)
end

local function excludeClasses(claim, classes, maxdepth) -- true si la valeur n'est pas une instance d'une classe dans la liste, ou si c'est une valeur spéciale (filtrée à part par excludespecial)
	return wd.isSpecial(claim.mainsnak) or not ( hasTargetClass(claim, classes, maxdepth) )
end

local function hasTargetSuperclass(claim, targets, maxdepth) -- retourne true si la valeur est une sous-classe d'une classe dans la liste des target, ou si c'est une valeur spéciale filtrée séparément par excludespecial
	local id = wd.getMainId(claim)
	local targets = wd.splitStr(targets)
	local maxdepth = maxdepth or 10
	local matchfunction = function(value, target) return wd.isSubclass(target, value, maxdepth) end
	return wd.isHere(targets, id, matchfunction) or wd.isSpecial(claim.mainsnak)
end

local function excludeSuperclasses(claim, classes, maxdepth) -- true si la valeur n'est pas une sous-classe d'une classe dans la liste, ou si c'est une valeur spéciale (filtrée à part par excludespecial)
	return wd.isSpecial(claim.mainsnak) or not ( hasTargetSuperclass(claim, classes, maxdepth) )
end


local function bestRanked(claims)
	if not claims then
		return nil
	end
	local preferred, normal = {}, {}
	for i, j in pairs(claims) do
		if j.rank == 'preferred' then
			table.insert(preferred, j)
		elseif j.rank == 'normal' then
			table.insert(normal, j)
		end
	end
	if #preferred > 0 then
		return preferred
	else
		return normal
	end
end

local function withRank(claims, target)
	if target == 'best' then
		return bestRanked(claims)
	end
	local newclaims = {}
	for pos, claim in pairs(claims) do
		if target == 'valid' then
			if claim.rank ~= 'deprecated' then
				table.insert(newclaims, claim)
			end
		elseif claim.rank == target then
			table.insert(newclaims, claim)
		end
	end
	return newclaims
end

function wd.hasQualifier(claim, acceptedqualifs, acceptedvals, excludequalifiervalues)
	local claimqualifs = claim.qualifiers

	if (not claimqualifs) then
		return false
	end

	acceptedqualifs = wd.splitStr(acceptedqualifs)
	acceptedvals = wd.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 wd.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

function wd.hasQualifierNumber(claim, acceptedqualifs, acceptedvals, excludequalifiervalues)
	local claimqualifs = claim.qualifiers

	if (not claimqualifs) then
		return false
	end

	acceptedqualifs = wd.splitStr(acceptedqualifs)
	acceptedvals = wd.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 mw.wikibase.renderSnak(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

local function hasSource(claim, targetsource, sourceproperty)
	sourceproperty = sourceproperty or 'P248'
	if targetsource == "-" then
		return true
	end
	if (not claim.references) then
		return false
	end
	local candidates = claim.references[1].snaks[sourceproperty] -- les snaks utilisant la propriété demandée
	if (not candidates) then
		return false
	end
	if (targetsource == "any") then -- si n'importe quelle valeur est acceptée tant qu'elle utilise en ref la propriété demandée
		return true
	end
	targetsource = wd.splitStr(targetsource)
	for _, source in pairs(candidates) do
		local s = wd.getId(source)
		for i, target in pairs(targetsource) do
			if s == target then return true end
		end
	end
	return false
end

local function excludeQualifier(claim, qualifier, qualifiervalues)
	return not wd.hasQualifier(claim, qualifier, qualifiervalues)
end

function wd.hasDate(claim)
	if not claim then
		return false --error() ?
	end
	if wd.getDateFromQualif(claim, 'P585') or wd.getDateFromQualif(claim, 'P580') or wd.getDateFromQualif(claim, 'P582') then
		return true
	end
	return false
end

local function hasLink(claim, site, lang)
	if (claim.mainsnak.snaktype ~= 'value') then -- ne pas supprimer les valeurs spéciales, il y a une fonction dédiée pour ça
		return true
	end
	local id = wd.getMainId(claim)
	local link = wd.siteLink(id, site, lang)
	if link then
		return true
	end
end

local function isInLanguage(claim, lang)
	if type(lang) == 'table' then -- si c'est une table de language séparées par des virgules, on les accepte toutes
		for i, l in pairs(lang) do
			local v = isInLanguage(claim, l)
			if v then
				return true
			end
		end
	end
	if type(lang) ~= ('string') then
		return --?
	end

	if (lang == '-') then
		return true
	end
	if (lang == 'locallang') then
		lang = mw.getContentLanguage():getCode()
	end

	-- pour les monolingual text
	local snak = claim.mainsnak or claim
	if snak.snaktype == 'value' and snak.datavalue.type == 'monolingualtext' then
		if snak.datavalue.value.language == lang then
			return true
		end
		return false
	end

	-- pour les autres types de données : recherche dans les qualificatifs
	if (lang == 'fr') then
		lang = 'Q150'
	elseif (lang == 'en') then
		lang = 'Q1860'
	else
		lang = databases.invertedlangcodes[lang]
	end
	if claim.qualifiers and claim.qualifiers.P407 then
		if wd.hasQualifier(claim, {'P407'}, {lang}) then
			return true
		else
			return false
		end
	end

	return true -- si on ne ne sait pas la langue, on condière que c'est bon
end

local function firstVals(claims, numval) -- retourn les numval premières valeurs de la table claims
    local numval = tonumber(numval) or 0 -- raise a error if numval is not a positive integer ?
    if not claims then
    	return nil
    end
    while (#claims > numval) do
    	table.remove(claims)
    end
    return claims
end

local function lastVals(claims, numval2) -- retourn les valeurs de la table claims à partir de numval2
    local numval2 = tonumber(numval2) or 0 -- raise a error if numval is not a positive integer ?
    if not claims then
    	return nil
    end
    for i=1,numval2 do
    	table.remove(claims, 1)
    end
    return claims
end

-- retourne les valeurs de la table claims à partir de removedupesdate,
-- sans les dates en doublons avec conversion entre les calendrier julien et grégorien,
-- ou uniquement en catégorisant si le paramètre removedupesdate est égale à 'cat'
local function removeDupesDate(claims, removedupesdate)
	if not claims or #claims < 2 then
		return claims, ''
	end
	local cat = ''
	local newClaims = {}
	local newIsos = {}

	local function findIndex(searchset, val) -- similaire à wd.isHere mais retourne l'index de la valeur trouvée
		for i, j in pairs(searchset) do
			if val == j then
				return i
			end
		end
		return -1
	end

	for _, claim in ipairs( claims ) do
		local snak = claim.mainsnak or claim
		if (snak.snaktype == 'value') and (snak.datatype == 'time') and snak.datavalue.value.precision >= 11 then -- s'il s'agit d'un time et que la précision est au moins l'année
			local iso = snak.datavalue.value.time
			_, _, iso = string.find(iso, "(+%d+-%d+-%d+T)")
			local deleteIfDuplicate = false
			if snak.datavalue.value.calendarmodel == 'http://www.wikidata.org/entity/Q1985727' then -- si la date est grégorienne
				if modules.formatDate.before('+1582', iso) then -- si avant 1582 on calcule la date julienne
					_, _, y, m, d = string.find(iso, "+(%d+)-(%d+)-(%d+)T")
					y, m , d = modules.datemodule.gregorianToJulian(y, m , d)
					if m < 10 then m = '0' .. m end
					if d < 10 then d = '0' .. d end
					iso = '+' .. y .. '-' .. m .. '-' .. d .. 'T'
					deleteIfDuplicate = true
				end
				local index = findIndex(newIsos, iso)
				if index >= 0 then -- si la date est déjà présente
					cat = cat .. '[[Catégorie:Article avec des dates identiques venant de wikidata dans le code de l\'infobox]]'
					if removedupesdate == "cat" then -- ne faire que catégoriser
						table.insert(newIsos, iso)
						table.insert(newClaims, claim)
					elseif not deleteIfDuplicate then -- supprimer l'autre date si la date courante n'a pas été convertie
						newClaims[index] = claim
					end -- sinon supprimer la date courante
				else -- pas de doublon
					table.insert(newIsos, iso)
					table.insert(newClaims, claim)
				end
			elseif snak.datavalue.value.calendarmodel == 'http://www.wikidata.org/entity/Q1985786' then -- si date julienne
				if not modules.formatDate.before('+1582', iso) then -- si après 1582 on calcule la date grégorienne
					_, _, y, m, d = string.find(iso, "+(%d+)-(%d+)-(%d+)T")
					y, m , d = modules.datemodule.julianToGregorian(y, m , d)
					if m < 10 then m = '0' .. m end
					if d < 10 then d = '0' .. d end
					iso = '+' .. y .. '-' .. m .. '-' .. d .. 'T'
					deleteIfDuplicate = true
				end
				local index = findIndex(newIsos, iso)
				if index >= 0 then -- si date déjà présente
					cat = cat .. '[[Catégorie:Article avec des dates identiques venant de wikidata dans le code de l\'infobox]]'
					if removedupesdate == "cat" then -- ne faire que catégoriser
						table.insert(newIsos, iso)
						table.insert(newClaims, claim)
					elseif not deleteIfDuplicate then -- supprimer l'autre date si la date courante n'a pas été convertie
						newClaims[index] = claim
					end -- sinon supprimer la date courante
				else -- pas de doublon
					table.insert(newIsos, iso)
					table.insert(newClaims, claim)
				end
			else -- autre calendrier
				table.insert(newIsos, iso)
				table.insert(newClaims, claim)
			end
		else -- précision insuffisante
			table.insert(newIsos, iso)
			table.insert(newClaims, claim)
		end
	end
	return newClaims, cat
end

local function timeFromQualifs(claim, qualifs)
	local claimqualifs = claim.qualifiers
	if not claimqualifs then
		return nil
	end
	for i, qualif in ipairs(qualifs or datequalifiers) do
		local vals = claimqualifs[qualif]
		if vals and (vals[1].snaktype == 'value') then
			return vals[1].datavalue.value.time, vals[1].datavalue.value.precision
		end
	end
end

local function atDate(claim, mydate)
	if mydate == "today" then
		mydate = os.date("!%Y-%m-%dT%TZ")
	end
	-- determines required precision depending on the atdate format
	local d = mw.text.split(mydate, "-")

	local myprecision
	if d[3] then
		myprecision = 11 -- day
	elseif d[2] then
		myprecision = 10 -- month
	else
		myprecision = 9 -- year
 	end

	-- with point in time
	local d, storedprecision = timeFromQualifs(claim, {'P585'})
	if d then
		return modules.formatDate.equal(mydate, d, math.min(myprecision, storedprecision))
	end
	-- with start or end date -- TODO: precision
	local mindate = timeFromQualifs(claim, {'P580'})
	local maxdate = timeFromQualifs(claim, {'P582'})
	if modules.formatDate.before(mydate, mindate) and modules.formatDate.before(maxdate, mydate) then
		return true
	end
	return false
end

local function check(claim, condition)
	if type(condition) == 'function' then -- cas standard
		return condition(claim)
	end
	return formatError('invalid type', 'function', type(condition))
end

local function minPrecision(claim, minprecision)
	local snak
	if claim.qualifiers then -- si une date est donnée en qualificatif, c'est elle qu'on utilise de préférence au mainsnak
		for i, j in ipairs(datequalifiers) do
			if claim.qualifiers[j] then
				snak = claim.qualifiers[j][1]
				break
			end
		end
	end
	if not snak then
		snak = claim.mainsnak or claim
	end
	if (snak.snaktype == 'value') and (snak.datatype == 'time') and (snak.datavalue.value.precision < minprecision) then
		return false
	end
	return true
end

function wd.sortClaims(claims, sorttype)
	if not claims then
		return nil
	end
	if wd.isHere({'chronological', 'order', 'inverted', 'age', 'ageinverted'}, sorttype) then
		return wd.chronoSort(claims, sorttype)
	elseif sorttype == 'ascending' then
		return wd.quantitySort(claims)
	elseif sorttype == 'descending' then
		return wd.quantitySort(claims, true)
	elseif sorttype == 'alphabetical' then
		return wd.alphabetSort(claims)
	elseif sorttype == "language" then
		return wd.sortByLanguage(claims)
	elseif type(sorttype) == 'function' then
		table.sort(claims, sorttype)
		return claims
	elseif type(sorttype) == 'string' and sorttype:sub(1, 1) == 'P' then
		return wd.numericPropertySort(claims, sorttype)
	end
	return claims
end

function wd.filterClaims(claims, args) --retire de la tables de claims celles qui sont éliminés par un des filters de la table des filters

	local function filter(condition, filterfunction, funargs)
		if not args[condition] then
			return
		end
		for i = #claims, 1, -1 do
			if not( filterfunction(claims[i], args[funargs[1]], args[funargs[2]], args[funargs[3]]) ) then
				table.remove(claims, i)
			end
		end
	end
	filter('isinlang', isInLanguage, {'isinlang'} )
	filter('excludespecial', notSpecial, {} )
	filter('condition', check, {'condition'} )
	if claims[1] and claims[1].mainsnak then
		filter('targetvalue', hasTargetValue, {'targetvalue','maxdepth'} )
		filter('targetclass', hasTargetClass, {'targetclass','maxdepth'} )
		filter('targetsuperclass', hasTargetSuperclass, {'targetsuperclass'} )
		filter('atdate', atDate, {'atdate'} )
		filter('qualifier', wd.hasQualifier, {'qualifier', 'qualifiervalue'} )
		filter('qualifiernumber', wd.hasQualifierNumber, {'qualifiernumber', 'qualifiernumbervalue'} )
		filter('excludequalifier', excludeQualifier, {'excludequalifier', 'excludequalifiervalue'} )
		filter('withsource', hasSource, {'withsource', 'sourceproperty'} )
		filter('withdate', wd.hasDate, {} )
		filter('excludevalues', excludeValues, {'excludevalues'})
		filter('excludeclasses', excludeClasses, {'excludeclasses','maxdepth'})
		filter('excludesuperclasses', excludeSuperclasses, {'excludesuperclasses','maxdepth'})
		filter('withlink', hasLink, {'withlink', 'linklang'} )
		filter('minprecision', minPrecision, {'minprecision'} )
		claims = withRank(claims, args.rank or 'best')
	end
	if #claims == 0 then
		return nil
	end
	if args.sorttype then
		claims = wd.sortClaims(claims, args.sorttype)
	end
	if args.numval2 then
		claims = lastVals(claims, args.numval2)
	end
	if args.numval then
		claims = firstVals(claims, args.numval)
	end
	return claims

end

function wd.loadEntity(entity, cache)
	if type(entity) ~= 'table' then
		if cache then
			if not cache[entity] then
				cache[entity] = mw.wikibase.getEntity(entity)
				mw.log("cached")
     		end
			return cache[entity]
        else
			if entity == '' or (entity == '-') then
				entity = nil
			end
        	return mw.wikibase.getEntity(entity)
        end
    else
    	return entity
    end
end


function wd.getClaims( args ) -- returns a table of the claims matching some conditions given in args
	if args.claims then -- if claims have already been set, return them
		return args.claims
	end
	local properties = args.property
	if type(properties) == 'string' then
		properties = wd.splitStr(string.upper(args.property))
	end
	if not properties then
		return formatError( 'property-param-not-provided' )
	end

	--Get entity
	local entity = args.entity
	if type(entity) == 'string' then
		if entity == '' then
			entity = nil
		end
	elseif type(entity) == 'table' then
		entity = entity.id
	end
        if (not entity) then
            entity = mw.wikibase.getEntityIdForCurrentPage()
        end
	if (not entity) or (entity == '-') or (entity == wd.translate('somevalue')) or (entity == modules.linguistic.ucfirst(wd.translate('somevalue'))) then
		return nil
	end
	if args.labelformat and args.labelformat == 'gendered' then
		args.labelformat = wd.getgender(entity)
	end
	local claims = {}

	if #properties == 1 then
		claims = mw.wikibase.getAllStatements(entity, properties[1]) -- do not use mw.wikibase.getBestStatements at this stage, as it may remove the best ranked values that match other criteria in the query
	else
		for i, prop in ipairs(properties) do
			local newclaims = mw.wikibase.getAllStatements(entity, prop)
			if newclaims and #newclaims > 0 then
				for j, claim in ipairs(newclaims) do
					table.insert(claims, claim)
				end
			end
		end
	end


	if (not claims) or (#claims == 0) then
		return nil
	end
	return wd.filterClaims(claims, args)
end

--=== ENTITY FORMATTING ===

function wd.getLabel(entity, lang1, lang2)

	if (not entity) then
		return nil -- ou option de gestion des erreurs ?
	end
	entity = entity.id or ( type(entity) == "string" and entity)
	if not(type(entity) == 'string') then return nil end

	lang1 = lang1 or defaultlang

	local str, lang --str : texte rendu, lang : langue de celui-ci
	if lang1 == defaultlang then -- le plus économique
		str, lang = mw.wikibase.getLabelWithLang(entity) -- le libellé peut être en français ou en anglais
	else
		str = mw.wikibase.getLabelByLang(entity, lang1)
		if str then lang = lang1 end
	end
	if str and (lang == lang1 or lang == "mul") then --pas de catégorie "à traduire" si on a obtenu un texte dans la langue désirée (normalement fr) ou multilingue
		return str
	end
	if lang2 then -- langue secondaire, avec catégorie "à traduire"
		str2 = mw.wikibase.getLabelByLang(entity, lang2)
		if str2 then
			lang = lang2
			str = str2
		end
	end
	if not str then --si ni lang1, ni lang2 ni l'anglais ne sont présents, parcours de la hiérarchie des langues
		for _, trylang in ipairs(databases.langhierarchy.codes) do
			str = mw.wikibase.getLabelByLang(entity, trylang)
			if str then
				lang = trylang
				break
			end
		end
	end

	if str then
		local translationCat = databases.i18n['to translate']
		translationCat = translationCat .. (databases.langhierarchy.cattext[lang] or '')
		translationCat = addCat(translationCat)
		return str, translationCat
	end
end

function wd.formatEntity( entity, params )

	if (not entity) then
		return nil --formatError('entity-not-found')
	end
	local id = entity
	if type(id) == 'table' then
		id = id.id
	end

	params = params or {}
	local lang = params.lang or defaultlang
	local speciallabels = params.speciallabels
	local displayformat = params.displayformat
	local labelformat = params.labelformat
	local labelformat2 = params.labelformat2
	local defaultlabel = params.defaultlabel or id
	local linktype = params.link
	local defaultlink = params.defaultlink
	local defaultlinkquery = params.defaultlinkquery

	if speciallabels and speciallabels[id] then --speciallabels override the standard label + link combination
		return speciallabels[id]
	end
	if params.displayformat == 'raw' then
		return id
	end
	if wd.isGender[params.labelformat] then
		labelformat = function(objectid) return wd.genderedlabel(objectid, params.labelformat) end
	end

	local label, translationCat

	if type(labelformat) == 'function' then -- sert à des cas particuliers
		label, translationCat = labelformat(entity)
	end

	if not label then
		label, translationCat = wd.getLabel(entity, lang, params.wikidatalang)
	end

	if labelformat == 'bold' or labelformat2 == 'bold' then --mises en forme typographiques simples accessibles depius le wikicode
		labelformat2 = function(str) if label then return "'''" .. str .. "'''" end end
	end
	if labelformat == 'italic' or labelformat2 == 'italic' then
		labelformat2 = function(str) if label then return "''" .. str .. "''" end end
	end
	if type(labelformat2) == 'function' and label then
		label = labelformat2(label)
	end

	translationCat = translationCat or "" -- sera toujours ajoutée au résultat mais sera vide si la catégorie de maintenance n'est pas nécessaire

	if not label then
		if (defaultlabel == '-') then
			return nil
		end
		local link = wd.siteLink(id, 'wikidata')
		return '[[' .. link .. '|' .. id .. ']]' .. translationCat
-- si pas de libellé, on met un lien vers Wikidata pour qu'on comprenne à quoi ça fait référence
	end
	
	if params.ucfirst_entity then
		label =  modules.linguistic.ucfirst(label)
	end

	-- détermination du fait qu'on soit ou non en train de rendre l'élément sur la page de son article
	local rendering_entity_on_its_page = wd.isPageOfQId(id)

	if (linktype == '-') or rendering_entity_on_its_page then
		return label .. translationCat
	end

	local link = wd.siteLink(entity, linktype, lang)

	-- defaultlinkquery will try to link to another page on this Wiki
	if (not link) and defaultlinkquery then
		if type(defaultlinkquery) == 'string' then
				defaultlinkquery = {property = defaultlinkquery}
		end
		defaultlinkquery.excludespecial = true
		defaultlinkquery.entity = entity
		local claims = wd.getClaims(defaultlinkquery)
		if claims then
			for i, j in pairs(claims) do
				local id = wd.getMainId(j)
				link = wd.siteLink(id, linktype, lang)
				if link then
					break
				end
			end
		end
	end

	if link then
		if link:match('^Category:') or link:match('^Catégorie:') then -- attention, le « é » est multibyte
			-- lier vers une catégorie au lieu de catégoriser
			link = ':' .. link
		end
		return '[[' .. link .. '|' .. label .. ']]' .. translationCat
	end

	-- if not link, you can use defaultlink: a sidelink to another Wikimedia project
	if (not defaultlink) then
		defaultlink = {'enwiki'}
	end
	if defaultlink and (defaultlink ~= '-') then
		local linktype
		local sidelink, site, langcode

		if type(defaultlink) == 'string' then
			defaultlink = {defaultlink}
		end
		for i, j in ipairs(defaultlink) do
			sidelink, site, langcode = wd.siteLink(entity, j, lang)
			if sidelink then
				break
			end
		end
		if not sidelink then
			sidelink, site = wd.siteLink(entity, 'wikidata')
		end

		local icon, class, title = site, nil, nil -- le texte affiché du lien
		if site == 'wiki' then
			icon, class, title = langcode, "indicateur-langue", wd.translate('see-another-language', mw.language.fetchLanguageName(langcode, defaultlang))
		elseif site == 'wikidata' then
			icon, class, title = 'd', "indicateur-langue", wd.translate('see-wikidata')
		else
			title = wd.translate('see-another-project', site)
		end
		local val = '[[' .. sidelink .. '|' .. '<span class = "' .. (class or '').. '" title = "' .. (title or '') .. '">' .. icon .. '</span>]]'
		return label .. ' <small>(' .. val .. ')</small>' .. translationCat
	end
	return label .. translationCat
end

function wd.addTrackingCat(prop, cat) -- doit parfois être appelé par d'autres modules
	if type(prop) == 'table' then
		local catTable = {}
		for i, v in ipairs(prop) do
		    catTable[i] = wd.addTrackingCat(v,cat)
		end
		return table.concat(catTable)
	end
	if not prop and not cat then
		return formatError("property-param-not-provided")
	end
	if not cat then
		cat = wd.translate('trackingcat', prop or 'P??')
	end
	return addCat(cat)
end

local function unknownValue(snak, label)
	local str = label

	if type(str) == "function" then
		str = str(snak)
	end

	if (not str) then
		if snak.datatype == 'time' then
			str = wd.translate('sometime')
		else
			str = wd.translate('somevalue')
		end
	end

	if type(str) ~= "string" then
		return formatError(snak.datatype)
	end
	return str
end

local function noValue(displayformat)
	if not displayformat then
		return wd.translate('novalue')
	end
	if type(displayformat) == 'string' then
		return displayformat
	end
	return formatError()
end

local function getLangCode(entityid)
	return databases.langcodes[tonumber(entityid:sub(2))]
end

local function showLang(statement) -- retourne le code langue entre parenthèses avant la valeur (par exemple pour les biblios et liens externes)
	local mainsnak = statement.mainsnak
	if mainsnak.snaktype ~= 'value' then
		return nil
	end
	local langlist = {}
	if mainsnak.datavalue.type == 'monolingualtext' then
		langlist = {mainsnak.datavalue.value.language}
	elseif (not statement.qualifiers) or (not statement.qualifiers.P407) then
		return
	else
		for i, j in pairs( statement.qualifiers.P407 ) do
			if j.snaktype == 'value' then
				local langentity = wd.getId(j)
				local langcode = getLangCode(langentity)
				table.insert(langlist, langcode)
			end
		end
	end
	if (#langlist > 1) or (#langlist == 1 and langlist[1] ~= defaultlang) then -- si c'est en français, pas besoin de le dire
		langlist.maxLang = 3
		return modules.langmodule.indicationMultilingue(langlist)
	end
end


-- === DATE HANDLING ===

local function fuzzydate(str, precision) -- ajoute le qualificatif "vers" à une date
	if not str then
		return nil
	end
	if (precision >= 11) or (precision == 7) or (precision == 6) then --dates avec jour, siècles, millénaires
		return "vers le " .. str
	end
	if (precision == 8) then --décennies ("années ...")
		return "vers les " .. str
	end
	return "vers " .. str
end

function wd.addStandardQualifs(str, statement, onlygeneral)
	-- qualificateurs de date ou de lieu approximatif ou d'info globalement incertaine ; onlygenereal=true pour rerstreindre à ces derniers
	if (not statement) or (not statement.qualifiers) then
		return str
	end
	if not str then
		return error()-- what's that ?
	end

	if statement.qualifiers.P1480 then
		for i, j in pairs(statement.qualifiers.P1480) do
			local v = wd.getId(j)
			if (v == "Q21818619") and not onlygeneral then --"à proximité de"
				str = wd.translate('approximate-place', str)
			elseif (v == "Q18122778") or (v == "Q18912752") or (v == "Q56644435") or (v == "Q30230067") then --"présumé", "controversé", "probablement", "possible"
				str = wd.translate('uncertain-information', str)
			elseif (v == "Q5727902") and not onlygeneral and statement.mainsnak.datatype == 'time' then --date approximative
				local datevalue = statement.mainsnak.datavalue
				if datevalue then str = fuzzydate(str, datevalue.value.precision) end
			end
		end
	end
	return str
end

function wd.getDateFromQualif(statement, qualif)
	if (not statement) or (not statement.qualifiers) or not (statement.qualifiers[qualif]) then
		return nil
	end
	local v = statement.qualifiers[qualif][1]
	if v.snaktype ~= 'value' then -- que faire dans ce cas ?
		return nil
	end
	return modules.formatDate.dateObject(v.datavalue.value)
end

function wd.getDate(statement)
	local period = wd.getDateFromQualif(statement, 'P585') -- retourne un dateobject
	if period then
		return period
	end
	local begin, ending = wd.getDateFromQualif(statement, 'P580'), wd.getDateFromQualif(statement, 'P582')
	if begin or ending then
		return modules.formatDate.rangeObject(begin, ending) -- retourne un rangeobject fait de deux dateobject
	end
	return nil
end

function wd.getFormattedDate(statement, params)
	if not statement then
		return nil
	end
	local str

	--cherche la date avec les qualifs P580/P582
	local datetable = wd.getDate(statement)
	if datetable then
		str = modules.formatDate.objectToText(datetable, params)
	end

	-- puis limite intérieur / supérieur
	if not str then
		local start, ending = wd.getDateFromQualif(statement, 'P1319'), wd.getDateFromQualif(statement, 'P1326')
		str = modules.formatDate.between(start, ending, params)
	end

	local fromqualif = false
	if str then fromqualif = true end --si la date est tirée des qualificateurs, on n'y ajoute pas l'éventuel "vers ..."

	 -- sinon, le mainsnak, pour les données de type time
	if (not str) and (statement.mainsnak.datatype == 'time') then
		local mainsnak = statement.mainsnak
		if (mainsnak.snaktype == 'value') or (mainsnak.snaktype == 'somevalue') then
			str = wd.formatSnak(mainsnak, params)
		end
	end

	if str and params and (params.addstandardqualifs ~= '-') then
		str = wd.addStandardQualifs(str, statement, fromqualif)
	end

	return str
end

wd.compare.by_quantity = function(c1, c2)
	local v1 = wd.getDataValue(c1.mainsnak)
	local v2 = wd.getDataValue(c2.mainsnak)
	if not (v1 and v2) then
			return true
	end
	return v1 < v2
end

--[[ tri chronologique générique :
             retourne une fonction de tri de liste de déclaration
             en fonction d’une fonction qui calcule la clé de tri
             et d’une fonction qui compare les clés de tri

   paramètres nommés: (appel type wikidata.compare.chrono_key_sort{sortKey="nom clé"})
        sortKey (optionnel)   : chaine, le nom de la clé utilisée pour un tri
                                (pour éviter de rentrer en collision avec "dateSortKey"
                                utilisé par chronoSort au besoin)
        snak_key_get_function : fonction qui calcule la valeur de la clé à partir d’un snak ou d’une déclaration,
        (obligatoire)           le résultat n’est calculé qu’une fois et est stocké en cache dans claim[sortKey]
        key_compare_function  : fonction de comparaison des clés calculées par snak_key_get_function
        (optionnel)
--]]

function wd.chrono_key_sort(arg)

	local snak_key_get_function = arg.snak_key_get_function
	local sortKey = arg.sortKey or "dateSortKey"
	local key_compare_function = arg.key_compare_function or
									function(c1, c2) return c1 < c2 end
	return function(claims)
		for _, claim in ipairs( claims ) do
			if not claim[sortKey] then
				local key = snak_key_get_function(claim)
				if key then
					claim[sortKey] = wd.compare.get_claim_date(key)
				else
					claim[sortKey] = 0
				end
			end
		end
		table.sort(
			claims,
			function(c1, c2)
				return key_compare_function(c1[sortKey], c2[sortKey])
			end
		)
		return claims
	end
end


function wd.quantitySort(claims, inverted)
	local function sort(c1, c2)
		local v1 = wd.getDataValue(c1.mainsnak)
		local v2 = wd.getDataValue(c2.mainsnak)
		if not (v1 and v2) then
				return true
		end
		if inverted then
			return v2 < v1
		end
		return v1 < v2
	end
	table.sort(claims, sort )
	return claims
end

function wd.alphabetSort(claims)
	local function sort(c1, c2)
		local v1 = wd.getDataValue(c1.mainsnak, {link = '-', displayformat = {monolingualtext = 'raw'}})
		local v2 = wd.getDataValue(c2.mainsnak, {link = '-', displayformat = {monolingualtext = 'raw'}})
		if not (v1 and v2) then
			return true
		end
		return modules.linguistic.makeSortkey(v1) < modules.linguistic.makeSortkey(v2)
	end
	table.sort(claims, sort )
	return claims
end

function wd.compare.get_claim_date(claim, datetype) -- rend une date au format numérique pour faire des comparaisons
	local snak = claim.mainsnak or claim

	if datetype and datetype == 'personbirthdate' then -- fonctionne avec un claim dont la valeur est une personne dont on va rendre la date de naissance
		if (snak.snaktype == 'value') and (snak.datatype == 'wikibase-item') then
			local personid = wd.getId(snak)
			local birthclaims = wd.getClaims({ entity = personid, property = 'P569', numval = 1})
			if birthclaims then
				return wd.compare.get_claim_date(birthclaims[1] or birthclaims)
			else return math.huge end
		else return math.huge end -- en cas de donnée manquante, valeur infinie qui entraîne le classement en fin de liste
	end

	local iso, datequalif, isonumber
	if (snak.snaktype == 'value') and (snak.datatype == 'time') then
		iso = snak.datavalue.value.time
	else
		for i, dqualif in ipairs(datequalifiers) do
			iso = timeFromQualifs(claim, {dqualif})
			if iso then
				datequalif = dqualif
				break
			end
		end
		if not iso then return math.huge end
	end
	-- transformation en nombre (indication de la base car gsub retourne deux valeurs)
	isonumber = tonumber( iso:gsub( '(%d)%D', '%1' ), 10 )

	-- ajustement de la date tenant compte du qualificatif dont elle est issue : un fait se terminant à une date est antérieur à un autre commençant à cette date
	if datequalif == 'P582' then --date de fin
		isonumber = isonumber - 2
	elseif datequalif == 'P1326' then -- date au plus tard
		isonumber = isonumber - 1
	elseif datequalif == 'P1319' then -- date au plus tôt
		isonumber = isonumber + 1
	elseif datequalif == 'P571' or datequalif == 'P580' then -- date de début et date de création
		isonumber = isonumber + 2
	end

	return isonumber
end

function wd.compare.chronoCompare(c1, c2)
	return wd.compare.get_claim_date(c1) < wd.compare.get_claim_date(c2)
end

-- fonction pour renverser l’ordre d’une autre fonction
function wd.compare.rev(comp_criteria)
	return function(c1, c2)
		-- attention les tris en lua attendent des fonctions de comparaison strictement inférieur, on doit
		-- vérifier la non égalité quand on inverse l’ordre d’un critère, d’ou "and comp_criteria(c2,c1)"
		return not(comp_criteria(c1,c2)) and comp_criteria(c2,c1)
	end
end

-- Fonction qui trie des Claims de type time selon l'ordre chronologique
-- Une clé de tri nomée « dateSortKey » est ajouté à chaque claim.
-- Si des clés de tri de ce nom existent déjà, elles sont utilisées sans modification.

function wd.chronoSort( claims, sorttype )
	for _, claim in ipairs( claims ) do
		if not claim.dateSortKey then
			if sorttype and (sorttype == 'age' or sorttype == 'ageinverted') then
				claim.dateSortKey = wd.compare.get_claim_date(claim, 'personbirthdate')
			else
				claim.dateSortKey = wd.compare.get_claim_date(claim)
			end
			if sorttype and (sorttype == 'inverted' or sorttype == 'ageinverted') and claim.dateSortKey == math.huge then
				claim.dateSortKey = -math.huge -- quand la donnée est manquante on lui assigne la valeur qui entraîne le classement en fin de liste
			end
		end
	end
	table.sort(
		claims,
		function ( c1, c2 )
			if sorttype and (sorttype == 'inverted' or sorttype == 'ageinverted') then
				return c2.dateSortKey < c1.dateSortKey
			end
			return c1.dateSortKey < c2.dateSortKey
		end
	)
	return claims
end

-- Function to prioritize certain languages in a multilingual label context
-- prioritize french, mul, roman languages, english if not all values needed
function wd.sortByLanguage(claims)
	-- Arbitrary order for now, ask to make it a parameter if needed
	local priority = { 
		fr=1, mul=2, ["en-gb"]=3, en=4, es=5, it=6, de=7 
	}
	-- maybe something to generalize a bit :
	-- local wikilang = mw.getContentLanguage()
	-- local priority = wikilang:getFallbackLanguages()
	-- priority.insert(priority, 1, wikilang.code)
		
	local function get_priority(lang)
		local attempt = priority[lang]
		if attempt ~= nil then
			return attempt
		else
			return math.huge
		end
	end
	local function get_snak_value(mainsnak)
		if mainsnak.datavalue and 
			mainsnak.datavalue.value then
			return mainsnak.datavalue.value
		end
	end
	-- get the main val as an object
	local function get_main_value(claim)
		if claim.mainsnak then 
			return get_snak_value(claim.mainsnak)
		end
	end
	
	local function sortfunction(v1, v2)
		-- get a value if they are statements or qualifiers as well
		local str1 = get_main_value(v1) or get_snak_value(v1) 
		local str2 = get_main_value(v2) or get_snak_value(v2)

		if str1 ~= nil and str2	~= nil then
			local prio1 = get_priority(str1.language)
			local prio2 = get_priority(str2.language)
			
			return prio1 < prio2
		elseif str1 then
			return true
		else 
			return false
		end
		
	end

	table.sort(claims, sortfunction)
	return claims
end

local function get_numeric_claim_value(claim, propertySort)
	local val
	local claimqualifs = claim.qualifiers
	if claimqualifs then
		local vals = claimqualifs[propertySort]
		if vals and vals[1].snaktype == 'value' then
			val = vals[1].datavalue.value
		end
	end
	return tonumber(val or 0)
end

function wd.compare.numeric(propertySort)
	return function(c1, c2)
		return get_numeric_claim_value(c1, propertySort) < get_numeric_claim_value(c2, propertySort)
	end
end

-- Fonction qui trie des Claims de type value selon l'ordre de la propriété fournit
-- Une clé de tri nomée « dateSortKey » est ajouté à chaque claim.
-- Si des clés de tri de ce nom existent déjà, elles sont utilisées sans modification.

function wd.numericPropertySort( claims, propertySort )
	for _, claim in ipairs( claims ) do
		if not claim.dateSortKey then
			local val = get_numeric_claim_value(claim, propertySort)
			claim.dateSortKey = tonumber(val or 0)
		end
	end
	table.sort(
		claims,
		function ( c1, c2 )
			return c1.dateSortKey < c2.dateSortKey
		end
	)
	return claims
end

--[[
test possible en console pour la fonction précédente :
= p.formatStatements{entity = "Q375946", property = 'P50', sorttype = 'P1545', linkback = "true"}
--]]

-- ===================
function wd.getReferences(statement)
	local refdata = statement.references
	if not refdata then
		return nil
	end

	local refs = {}
	local hashes = {}
	for i, ref in pairs(refdata) do
		local s
		local function hasValue(prop) -- checks that the prop is here with valid value
			if ref.snaks[prop] and ref.snaks[prop][1].snaktype == 'value' then
				return true
			end
			return false
		end

		if ref.snaks.P248 then -- cas lorsque P248 (affirmé dans) est utilisé
			for j, source in pairs(ref.snaks.P248) do
				if source.snaktype == 'value' then
					local page, accessdate, quotation
					if hasValue('P304') then -- page
						page = wd.formatSnak(ref.snaks.P304[1])
					end
					if hasValue('P813') then -- date de consultation
						accessdate = wd.formatSnak(ref.snaks.P813[1])
					end
					if hasValue('P1683') then -- citation
						quotation = wd.formatSnak(ref.snaks.P1683[1])
					end
					local sourceId = wd.getId(source)
					s = modules.reference.citeitem(sourceId, {['page'] = page, ['accessdate'] = accessdate, ['citation'] = quotation})
					table.insert(refs, s)
					table.insert(hashes, ref.hash .. sourceId)
				end
			end

		elseif hasValue('P8091') or hasValue('P854') then -- cas lorsque P8091 (Archival Resource Key) ou P854 (URL de la référence)est utilisé
			local arkKey, url, title, author, publisher, accessdate, publishdate, publishlang, quotation, description

			if hasValue('P8091') then
				arkKey = wd.formatSnak(ref.snaks.P8091[1], {text = "-"})
				url = 'https://n2t.net/' .. arkKey
				if hasValue('P1476') then
					title = wd.formatSnak(ref.snaks.P1476[1])
				else
					title = arkKey
				end
			elseif hasValue('P854') then
				url = wd.formatSnak(ref.snaks.P854[1], {text = "-"})
				if hasValue('P1476') then
					title = wd.formatSnak(ref.snaks.P1476[1])
				else
					title = mw.ustring.gsub(url, '^[Hh][Tt][Tt][Pp]([Ss]?):(/?)([^/])', 'http%1://%3')
				end
			end

			--todo : handle multiple values for author, etc.
			if hasValue('P1810') then -- sous le nom
				description = 'sous le nom ' .. wd.formatSnak(ref.snaks.P1810[1])
			end
			if hasValue('P813') then -- date de consultation
				accessdate = wd.formatSnak(ref.snaks.P813[1])
			end
			if hasValue('P50') then -- author (item type)
				author = wd.formatSnak(ref.snaks.P50[1])
			elseif hasValue('P2093') then -- author (string type)
				author = wd.formatSnak(ref.snaks.P2093[1])
			end
			if hasValue('P123') then -- éditeur
				publisher = wd.formatSnak(ref.snaks.P123[1])
			end
			if hasValue('P1683') then -- citation
				quotation = wd.formatSnak(ref.snaks.P1683[1])
			end
			if hasValue('P577') then -- date de publication
				publishdate = wd.formatSnak(ref.snaks.P577[1])
			end
			if hasValue('P407') then -- langue de l'œuvre
				local id = wd.getId(ref.snaks.P407[1])
				publishlang = getLangCode(id)
			end
			s = modules.cite.lienWeb{titre = title, url = url, auteur = author, editeur = publisher, langue = publishlang, ['en ligne le'] = publishdate, ['consulté le'] = accessdate, ['citation'] = quotation, ['description'] = description}
			table.insert(hashes, ref.hash)
			table.insert(refs, s)

		elseif ref.snaks.P854 and ref.snaks.P854[1].snaktype == 'value' then
			s = wd.formatSnak(ref.snaks.P854[1], {text = "-"})
			table.insert(hashes, ref.snaks.P854[1].hash)
			table.insert(refs, s)
		end
	end
	if #refs > 0 then
		if #hashes == #refs then
			return refs, hashes
		end
		return refs
	end
end

function wd.sourceStr(sources, hashes)
	if not sources or (#sources == 0) then
		return nil
	end
	local useHashes = hashes and #hashes == #sources
	for i, j in ipairs(sources) do
		local refArgs = {name = 'ref', content = j}
		if useHashes and hashes[i] ~= '-' then
			refArgs.args = {name = 'wikidata-' .. hashes[i]}
		end
		sources[i] = mw.getCurrentFrame():extensionTag(refArgs)
	end
	return table.concat(sources, '<sup class="reference cite_virgule">,</sup>')
end

function wd.getDataValue(snak, params)
	if not params then
		params = {}
	end
	local speciallabels = params.speciallabels -- parfois on a besoin de faire une liste d'éléments pour lequel le libellé doit être changé, pas très pratique d'utiliser une fonction pour ça

	if snak.snaktype ~= 'value' then
		return nil
	end

	local datatype = snak.datatype
	local value = snak.datavalue.value

	local displayformat = params.displayformat
	if type(displayformat) == 'table' then
		displayformat = displayformat[datatype]
	end
	if type(displayformat) == 'function' then
		return displayformat(snak, params)
	end

	if datatype == 'wikibase-item' then
		return wd.formatEntity(wd.getId(snak), params)
	end

	if datatype == 'url' then
		if params.displayformat == 'raw' then
			return value
		else
			return modules.weblink.makelink(value, params.text)
		end
	end

	if datatype == 'math' then
		return mw.getCurrentFrame():extensionTag( "math", value)
	end

	if datatype == 'tabular-data' then
		return mw.ustring.sub(value, 6, 100) -- returns the name of the file, without the "Data:" prefix
	end

	if (datatype == 'string') or (datatype == 'external-id') or (datatype == 'commonsMedia') then -- toutes les données de type string sauf "math"
		if params.urlpattern then
			local urlpattern = params.urlpattern
			if type(urlpattern) == 'function' then
				urlpattern = urlpattern(value)
			end
			-- encodage de l'identifiant qui se retrouve dans le path de l'URL, à l'exception des slashes parfois rencontrés, qui sont des séparateurs à ne pas encoder
			local encodedValue = mw.uri.encode(value, 'PATH'):gsub('%%2F', '/')
			-- les parenthèses autour du encodedValue:gsub() sont nécessaires, sinon sa 2e valeur de retour est aussi passée en argument au mw.ustring.gsub() parent
			local url = mw.ustring.gsub(urlpattern, '$1', (encodedValue:gsub('%%', '%%%%')))
			value = '[' .. url .. ' ' .. (params.text or value) .. ']'
		end
		return value
	end

	if datatype == 'time' then -- format example: +00000001809-02-12T00:00:00Z
		if displayformat == 'raw' then
			return value.time
		else
			local dateobject = modules.formatDate.dateObject(value, {precision = params.precision})
			return modules.formatDate.objectToText(dateobject, params)
		end
	end

	if datatype == 'globe-coordinate' then
		-- retourne une table avec clés latitude, longitude, précision et globe à formater par un autre module (à changer ?)
		if displayformat == 'latitude' then
			return value.latitude
		elseif displayformat == 'longitude' then
			return value.longitude
		else
			local coordvalue = mw.clone( value )
			coordvalue.globe = databases.globes[value.globe] -- transforme l'ID du globe en nom anglais utilisable par geohack
			return coordvalue -- note : les coordonnées Wikidata peuvent être utilisée depuis Module:Coordinates. Faut-il aussi autoriser à appeler Module:Coordiantes ici ?
		end
	end

	if datatype == 'quantity' then -- todo : gérer les paramètres précision
		local amount, unit = value.amount, value.unit

		if unit then
			unit = unit:match('Q%d+')
		end

		if not unit then
			unit = 'dimensionless'
		end

		local raw
		if displayformat == "raw" then
			raw = true
		end
		return modules.formatNum.displayvalue(amount, unit,
			{targetunit = params.targetunit, raw = raw, rounding = params.rounding, showunit = params.showunit or 'short', showlink = params.showlink}
		)
	end
	if datatype == 'monolingualtext' then
		if value.language == defaultlang or displayformat == 'raw' then
			return value.text
		else
			return modules.langmodule.langue({value.language, value.text, nocat=true})
		end
	end
	return formatError('unknown-datavalue-type' )

end

function wd.stringTable(args) -- like getClaims, but get a list of string rather than a list of snaks, for easier manipulation
	local claims = args.claims
	local cat = ''

	if not claims then
		claims = wd.getClaims(args)
	end
	if not claims or claims == {} then
		return {}, {}, cat
	end
	if args.removedupesdate and (args.removedupesdate ~= '-') then
		claims, cat = removeDupesDate(claims, args.removedupesdate)
	end
	local props = {} -- liste des propriétés associété à chaque string pour catégorisation et linkback
	for i, j in pairs(claims) do
		claims[i] = wd.formatStatement(j, args)
		table.insert(props, j.mainsnak.property)
	end
	if args.removedupes and (args.removedupes ~= '-') then
		claims = wd.addNewValues({}, claims) -- devrait aussi supprimer de props celles qui ne sont pas utilisées
	end
	return claims, props, cat
end

function wd.getQualifiers(statement, qualifs, params)
	if not statement.qualifiers then
		return nil
	end
	local vals = {}
	if type(qualifs) == 'string' then
		qualifs = wd.splitStr(qualifs)
	end
	for i, j in pairs(qualifs) do
		if statement.qualifiers[j] then
			for k, l in pairs(statement.qualifiers[j]) do
				table.insert(vals, l)
			end
		end
	end
	if #vals == 0 then
		return nil
	end
	return vals
end

function wd.getFormattedQualifiers(statement, qualifs, params)
	if not params then params = {} end
	local qualiftable = wd.getQualifiers(statement, qualifs)
	if not qualiftable then
		return nil
	end
	
	qualiftable = wd.filterClaims(qualiftable, params) or {}
	
	-- Sorting to prioritize languages
	if(params.showonlyqualifier ~= nil) then
		qualiftable = wd.sortClaims(qualiftable, params.sorttype or params.qualifsorttype)
	end
	
	for i, j in pairs(qualiftable) do
		qualiftable[i] = wd.formatSnak(j, params)
	end
	return modules.linguistic.conj(qualiftable, params.conjtype)
end

function wd.showQualifiers(str, statement, args)
	local qualifs = args.showqualifiers
	if not qualifs then
		return str -- or error ?
	end
	if type(qualifs) == 'string' then
			qualifs = wd.splitStr(qualifs)
	end
	local qualifargs = args.qualifargs or {}
	-- formatage des qualificatifs = args commençant par "qualif", ou à défaut, les mêmes que pour la valeur principale
	qualifargs.displayformat = args.qualifdisplayformat or args.displayformat
	qualifargs.labelformat = args.qualiflabelformat or args.labelformat
	qualifargs.labelformat2 = args.qualiflabelformat2 or args.labelformat2
	qualifargs.link = args.qualiflink or args.link
	qualifargs.linktopic = args.qualiflinktopic or args.linktopic
	qualifargs.conjtype = args.qualifconjtype
	qualifargs.precision = args.qualifprecision
	qualifargs.targetunit = args.qualiftargetunit
	qualifargs.defaultlink = args.qualifdefaultlink or args.defaultlink
	qualifargs.defaultlinkquery = args.qualifdefaultlinkquery or args.defaultlinkquery
	
	if args.qualiflabelformat == 'objectgender' then
		local objectid = wd.getId(statement.mainsnak)
		qualifargs.labelformat = wd.getgender(objectid)
	end

	local formattedqualifs
	if args.qualifformat and type (args.qualifformat) == 'function' then
		formattedqualifs = args.qualifformat(statement, qualifs, qualifargs)
	else
		formattedqualifs = wd.getFormattedQualifiers(statement, qualifs, qualifargs)
	end
	if formattedqualifs and formattedqualifs ~= "" then
		str = str .. " (" .. formattedqualifs .. ")"
	end
	return str
end


function wd.formatSnak( snak, params )
	if not params then params = {} end -- pour faciliter l'appel depuis d'autres modules
	if snak.snaktype == 'somevalue' then
		return unknownValue(snak, params.unknownlabel)
	elseif snak.snaktype == 'novalue' then
		return noValue(params.novaluelabel)
	elseif snak.snaktype == 'value' then
		return wd.getDataValue( snak, params)
	else
		return formatError( 'unknown-snak-type' )
	end
end

function wd.formatStatement( statement, args ) -- FONCTION A REORGANISER (pas très lisible)
	if not args then
		args = {}
	end
	
	if not statement.type or statement.type ~= 'statement' then
		return formatError( 'unknown-claim-type' )
	end
	local prop = statement.mainsnak.property

	local str

	-- special displayformat f
	if args.statementformat and (type(args.statementformat) == 'function') then
		str = args.statementformat(statement, args)
	elseif (statement.mainsnak.datatype == 'time') and (statement.mainsnak.dateformat ~= '-') then
		if args.displayformat == 'raw' and statement.mainsnak.snaktype == 'value' then
			str = statement.mainsnak.datavalue.value.time
		else
			str = wd.getFormattedDate(statement, args)
		end
	elseif args.showonlyqualifier and (args.showonlyqualifier ~= '') then
		str = wd.getFormattedQualifiers(statement, args.showonlyqualifier, args)
		if not str then
			return nil
		end
		if args.addstandardqualifs ~= '-' then
			str = wd.addStandardQualifs(str, statement, true)
		end
	else
		str = wd.formatSnak( statement.mainsnak, args )
		if (args.addstandardqualifs ~= '-') and (args.displayformat ~= 'raw') then
			str = wd.addStandardQualifs(str, statement)
		end
	end

	-- ajouts divers
	if args.showlang == true then
		local indicateur = showLang(statement)
		if indicateur then
			str = indicateur .. '&nbsp;' .. str
		end
	end
	if args.showqualifiers then
		str = wd.showQualifiers(str, statement, args)
	end

	if args.showdate then -- when "showdate and chronosort are both set, date retrieval is performed twice
		local period = wd.getFormattedDate(statement, args, "-") -- 3 arguments indicate the we should not use additional qualifiers, already added by wd.formatStatement
		if period then
			str = str .. " <small>(" .. period .. ")</small>"
		end
	end

	if args.showsource and args.showsource ~= '-' and args.showsource ~= "false" then
		if args.showsource == "only" then str="" end -- si showsource="only", alors ne montrer que la (les) source(s),
		                                             -- sans la valeur qui, auparavant, était enregistrée dans str
		                                             -- Utilisé par le modèle {{PH census}}
		local sources, hashes = wd.getReferences(statement)
		if sources then
			local source = wd.sourceStr(sources, hashes)
			if source then
				str = str .. source
			end
		end
	end

	return str
end

function wd.addLinkBack(str, id, property)
	if not id or id == '' then
		id = wd.getEntityIdForCurrentPage()
	end
	if not id then
		return str
	end
	if type(property) == 'table' then
		property = property[1]
	end

	id = mw.text.trim(wd.entityId(id))

	local class = ''
	if property then
		class = 'wd_' .. string.lower(property)
	end
	local icon = '[[File:Blue pencil.svg|%s|10px|baseline|class=noviewer|link=%s]]'
	local title = wd.translate('see-wikidata-value')
	local url = mw.uri.fullUrl('d:' .. id, 'uselang=fr')
	url.fragment = property -- ajoute une #ancre si paramètre "property" défini
	url = tostring(url)
	local v = mw.html.create('span')
		:addClass(class)
		:wikitext(str)
		:tag('span')
			:addClass('noprint wikidata-linkback skin-invert')
			:wikitext(icon:format(title, url))
		:allDone()
	return tostring(v)
end

function wd.addRefAnchor(str, id)
--[[
	Insère une ancre pour une référence générée à partir d'un élément wd.
	L'id Wikidata sert d'identifiant à l'ancre, à utiliser dans les modèles type "harvsp"
--]]
	return tostring(
		mw.html.create('span')
			:attr('id', id)
			:attr('class', "ouvrage")
			:wikitext(str)
	)
end

--=== FUNCTIONS USING AN ENTITY AS ARGUMENT ===
local function formatStatementsGrouped(args, type)
	-- regroupe les affirmations ayant la même valeur en mainsnak, mais des qualificatifs différents
	-- (seulement pour les propriétés de type élément)

	local claims = wd.getClaims(args)
	if not claims then
		return nil
	end
	local groupedClaims = {}

	-- regroupe les affirmations par valeur de mainsnak
	local function addClaim(claim)
		local id = wd.getMainId(claim)
		for i, j in pairs(groupedClaims) do
			if (j.id == id) then
				table.insert(groupedClaims[i].claims, claim)
				return
			end
		end
		table.insert(groupedClaims, {id = id, claims = {claim}})
	end
	for i, claim in pairs(claims) do
		addClaim(claim)
	end

	local stringTable = {}

	-- instructions ad hoc pour les paramètres concernant la mise en forme d'une déclaration individuelle
	local funs = {
		{param = "showqualifiers", fun = function(str, claims)
			local qualifs = {}
			for i, claim in pairs(claims) do
				local news = wd.getFormattedQualifiers(claim, args.showqualifiers, args)
				if news then
					table.insert(qualifs, news)
				end
			end
			local qualifstr = modules.linguistic.conj(qualifs, wd.translate("qualif-separator"))
			if qualifstr and qualifstr ~= "" then
				str = str .. " (" .. qualifstr .. ")"
			end
			return str
			end
		},
		{param = "showdate", fun = function(str, claims)
			-- toutes les dates sont regroupées à l'intérieur des mêmes parenthèses ex "médaille d'or (1922, 1924)"
			local dates = {}
			for i, statement in pairs(claims) do
				local s = wd.getFormattedDate(statement, args, true)
				if statement then table.insert(dates, s) end
			end
			local datestr = modules.linguistic.conj(dates)
			if datestr and datestr ~= "" then
				str = str .. " <small>(" .. datestr .. ")</small>"
			end
			return str
			end
		},
		{param = "showsource", fun = function(str, claims)
			-- les sources sont toutes affichées au même endroit, à la fin
			-- si deux affirmations ont la même source, on ne l'affiche qu'une fois
			local sources = {}
			local hashes = {}

			local function dupeRef(old, new)
				for i, j in pairs(old) do
					if j == new then
						return true
					end
				end
			end
			for i, claim in pairs(claims) do
				local refs, refHashes = wd.getReferences(claim)
				if refs then
					for i, j in pairs(refs) do
						if not dupeRef(sources, j) then
							table.insert(sources, j)
							local hash = (refHashes and refHashes[i]) or '-'
							table.insert(hashes, hash)
						end
					end
				end
			end
			return str .. (wd.sourceStr(sources, hashes) or "")
			end
		}
	}

	for i, group in pairs(groupedClaims) do -- bricolage pour utiliser les arguments de formatStatements
		local str = wd.formatEntity(group.id, args)
		if not str then
			str = '???' -- pour éviter erreur Lua si formatEntity a retourné nil
		end
		for i, fun in pairs(funs) do
			if args[fun.param] then
				str = fun.fun(str, group.claims, args)
			end
		end
		table.insert(stringTable, str)
	end

	args.valuetable = stringTable
	return wd.formatStatements(args)
end


function wd.formatStatements( args )--Format statement and concat them cleanly

	if args.value == '-' then
		return nil
	end

	-- If a value is already set: use it, except if it's the special value {{WD}} (use wikidata)
	if args.value and args.value ~= '' then
		local valueexpl = wd.translate("activate-query")
		if args.value ~= valueexpl then
			return args.value
		end
	-- There is no value set, and args.expl disables wikidata on empty values
	elseif args.expl then
		return nil
	end

	if args.grouped and args.grouped ~= '' then
		args.grouped = false
		return formatStatementsGrouped(args)
	end
	local valuetable = args.valuetable -- dans le cas où les valeurs sont déjà formatées
	local props -- les propriétés réellement utilisées (dans certains cas, ce ne sont pas toutes celles de args.property
	local cat = ''
	if not valuetable then -- cas le plus courant
		valuetable, props, cat = wd.stringTable(args)
	end

	if args.ucfirst == '-' and args.conjtype == 'new line' then args.conjtype = 'lowercase new line' end
	local str = modules.linguistic.conj(valuetable, args.conjtype)
	if not str then
		return args.default
	end
	if not props then
		props = wd.splitStr(args.property)[1]
	end
	if args.ucfirst ~= '-' then
		str = modules.linguistic.ucfirst(str)
	end

	if args.addcat and (args.addcat ~= '-') then
		str = str .. wd.addTrackingCat(props) .. cat
	end
	if args.linkback and (args.linkback ~= '-') then
		str = wd.addLinkBack(str, args.entity, props)
	end
	if args.returnnumberofvalues then
		return str, #valuetable
	end
	return str
end

function wd.formatAndCat(args)
	if not args then
		return nil
	end
	args.linkback = args.linkback or true
	args.addcat = true
	if args.value then -- do not ignore linkback and addcat, as formatStatements do
		if args.value == '-' then
			return nil
		end
		local val = args.value .. wd.addTrackingCat(args.property)
		val = wd.addLinkBack(val, args.entity, args.property)
		return val
	end
	return wd.formatStatements( args )
end

function wd.getTheDate(args)
	local claims = wd.getClaims(args)
	if not claims then
		return nil
	end
	local formattedvalues = {}
	for i, j in pairs(claims) do
		local v = wd.getFormattedDate(j, args)
		if v then
			table.insert(formattedvalues, v )
		end
	end
	local val = modules.linguistic.conj(formattedvalues)
	if not val then
		return nil
	end
	if args.addcat == true then
		val = val .. wd.addTrackingCat(args.property)
	end
	val = wd.addLinkBack(val, args.entity, args.property)
	return val
end


function wd.keyDate (event, item, params)
	params = params or {}
	params.entity = item
	if type(event) == 'table' then
		for i, j in pairs(event) do
			params.targetvalue = nil -- réinitialisation barbare des paramètres modifiés
			local s = wd.keyDate(j, item, params)
			if s then
				return s
			end
		end
	elseif type(event) ~= 'string' then
		 return formatError('invalid-datatype', type(event), 'string')
	elseif string.sub(event, 1, 1) == 'Q' then -- on demande un élément utilisé dans P:P793 (événement clé)
		params.property = 'P793'
		params.targetvalue = event
		params.addcat = params.addcat or true
		return wd.getTheDate(params)
	elseif string.sub(event, 1, 1) == 'P' then -- on demande une propriété
		params.property = event
		return wd.formatAndCat(params)
	else
		return formatError('invalid-entity-id', event)
	end
end

function wd.mainDate(entity)
	-- essaye P580/P582
	local args = {entity = entity, addcat = true}

	args.property = 'P580'
	local startpoint = wd.formatStatements(args)
	args.property = 'P582'
	local endpoint = wd.formatStatements(args)

	local str
	if (startpoint or endpoint) then
		str = modules.formatDate.daterange(startpoint, endpoint, params)
		str = wd.addLinkBack(str, entity, 'P582')
		return str
	end

	-- défaut : P585
	args.property = {'P585', 'P571'}
	args.linkback = true
	return wd.formatStatements(args)
end

-- ==== Fonctions sur le genre ====

function wd.getgender(id)
	local vals = {
		['Q6581072'] = 'f', -- féminin
		['Q6581097'] = 'm', -- masculin
		['Q1052281'] = 'f', -- femme transgenre
		['Q2449503'] = 'm', -- homme transgenre
		['Q17148251'] = 'f', -- en:Travesti (gender identity)
		['Q43445'] = 'f', -- femelle
		['Q44148'] = 'm', -- mâle
		default      = '?'
	}
	local gender = wd.formatStatements{entity = id, property = 'P21', displayformat = 'raw', numval = 1}
	return vals[gender] or vals.default
end

wd.isGender = {m = true, male = true, f = true, female = true} --reconnaissance des chaînes de caractères désignant un genre

-- catégories de genre/nombre
function wd.getgendernum(claims)
	local personid, gender
	local anym = false
	local anyf = false
	local anyunknown = false

	for i, claim in pairs(claims) do
		local snak = claim.mainsnak or claim
		if(snak.snaktype == 'value') and (snak.datatype == 'wikibase-item') then
			personid = wd.getId(snak)
			gender = wd.getgender(personid)
			anym = anym or (gender == 'm')
			anyf = anyf or (gender == 'f')
			anyunknown = anyunknown or (gender == '?')
		else
			anyunknown = true
		end
	end

	local gendernum
	if #claims > 1 then
		if anyunknown then
			gendernum = 'p'
		else
			if anym and not anyf then gendernum = 'mp' end
			if anyf and not anym then gendernum = 'fp' end
			if anym and anyf then gendernum = 'mixtep' end
		end
	else
		gendernum = 's'
		if anym then gendernum = 'ms' end
		if anyf then gendernum = 'fs' end
	end

	return gendernum
end


-- récupération des libellés genrés de Wikidata
function wd.genderedlabel(id, labelgender)
	local label
	if not labelgender then return nil end
	if labelgender == 'f' or labelgender == 'female' then -- femme : chercher le libellé dans P2521 (libellé féminin)
		label = wd.formatStatements{entity = id, property = 'P2521', isinlang = 'fr', numval = 1, ucfirst = '-'}
	elseif labelgender == 'm' or labelgender == 'male' then -- homme : chercher le libellé dans P3321 (libellé masculin)
		label = wd.formatStatements{entity = id, property = 'P3321', isinlang = 'fr', numval = 1, ucfirst = '-'}
	end
	if not label then
		label = wd.getLabel(id)
	end
	return label
end


-- === FUNCTIONS FOR TRANSITIVE PROPERTIES ===

function wd.getIds(item, query)
	query.excludespecial = true
	query.displayformat = 'raw'
	query.entity = item
	query.addstandardqualifs = '-'
	return wd.stringTable(query)
end


-- recursively adds a list of qid to an existing list, based on the results of a query
function wd.addVals(list, query, maxdepth, maxnodes, stopval)
	maxdepth = tonumber(maxdepth) or 10
	maxnodes = tonumber(maxnodes) or 100
	if (maxdepth < 0) then
		return list
	end
	if stopval and wd.isHere(list, stopval) then
		return list
	end
	local origsize = #list
	for i = 1, origsize do
		-- tried a "checkpos" param instead of starting to 1 each time, but no impact on performance
		local candidates = wd.getIds(list[i], query)
		list = wd.addNewValues(list, candidates, maxnodes, stopval)
		if list[#list] == stopval then
			return list
		end
		if #list >= maxnodes then
			return list
		end
	end

	if (#list == origsize) then
		return list
	end
	return wd.addVals(list, query, maxdepth - 1, maxnodes, stopval, origsize + 1)
end

-- returns a list of items transitively matching a query (orig item is not included in the list)

function wd.transitiveVals(item, query, maxdepth, maxnodes, stopval)
	maxdepth = tonumber(maxdepth) or 5
	if type(query) == "string" then
		query = {property = query}
	end

	-- récupération des valeurs
	local vals = wd.getIds(item, query)
	if not vals then
		return nil
	end
	local v = wd.addVals(vals, query, maxdepth - 1, maxnodes, stopval)
	if not v then
		return nil
	end

	-- réarrangement des valeurs
	if query.valorder == "inverted" then
		local a = {}
		for i = #v, 1, -1 do
			a[#a+1] = v[i]
		end
		v = a
	end

	return v
end

-- returns true if an item is the value of a query, transitively
function wd.inTransitiveVals(searchedval, sourceval, query, maxdepth, maxnodes )
	local vals = wd.transitiveVals(sourceval, query, maxdepth, maxnodes, searchedval )
	if (not vals) then
		return false
	end
	for _, val in ipairs(vals) do
		if (val == searchedval) then
			return true
		end
	end
	return false
end

-- returns true if an item is a superclass of another, based on P279
function wd.isSubclass(class, item, maxdepth)
	local query = {property = 'P279'}
	if class == item then -- item is a subclass of itself iff it is a class
		if wd.getIds(item, query) then
			return true
		end
		return false
	end
	return wd.inTransitiveVals(class, item, query, maxdepth )
end

-- returns true if one of the best ranked P31 values of an item is the target or a subclass of the target
-- rank = 'valid' would seem to make sense, but it would need to check for date qualifiers as some P31 values have begin or end date
function wd.isInstance(targetclass, item, maxdepth)
	maxdepth = maxdepth or 10
	local directclasses = wd.transitiveVals(item, {property = 'P31'}, 1)
	if not directclasses then
		return false
	end
	for i, class in pairs(directclasses) do
		if wd.isSubclass(targetclass, class, maxdepth - 1) then
			return true
		end
	end
	return false
end

-- return the first value in a transitive query that belongs to a particular class. For instance find a value of P131 that is a province of Canada
function wd.findVal(sourceitem, targetclass, query, recursion, instancedepth)
	if type(query) == "string" then
		query = {property = query}
	end
	local candidates = wd.getIds(sourceitem, query)
	if candidates then
		for i, j in pairs(candidates) do
			if wd.isInstance(targetclass, j, instancedepth) then
				return j
			end
		end
		if not recursion then
			recursion = 3
		else
			recursion = recursion - 1
		end
		if recursion < 0 then
			return nil
		end
		for i, candidate in pairs(candidates) do
			return wd.findVal(candidate, targetclass, query, recursion, instancedepth)
		end
	end
end


-- === VARIA ===
function wd.getDescription(entity, lang)
	lang = lang or defaultlang

	local description
	if lang == defaultlang then
		return mw.wikibase.description(qid)
	end
	if not entity.descriptions then
		return wd.translate('no description')
	end
	local descriptions = entity.descriptions
	if not descriptions then
		return nil
	end
	if descriptions[lang] then
		return descriptions[delang].value
	end
	return entity.id
end

function wd.Dump(entity)
	entity = wd.getEntity(entity)
	if not entity then
		return formatError("entity-param-not-provided")
	end
	return "<pre>"..mw.dumpObject(entity).."</pre>"
end

function wd.frameFun(frame)
	local args = frame.args
	local funname = args[1]
	table.remove(args, 1)
	return wd[funname](args)
end


return wd