« Module:Country data » : différence entre les versions

De Lagny-sur-Marne Wiki
Aller à la navigation Aller à la recherche
0x010D (discussion | contributions)
Aucun résumé des modifications
0x010D (discussion | contributions)
getDemonym : c'est l'adjectif qui doit être mis en majuscule, le gentilé devrait déjà y être
 
(28 versions intermédiaires par le même utilisateur non affichées)
Ligne 6 : Ligne 6 :
local p = {};
local p = {};


-- Chargement de la banque de données des langues avec gestion d'erreur.
local gdata = mw.loadData ("Module:Country data/liste" )
local gdata
local dates = require "Module:date complexe"
local linguistic = require "Module:Linguistique"
local linguistic = require "Module:Linguistique"
local success, resultat = pcall (mw.loadData, "Module:Country data/liste" )


-- chargement des données
local function _getCompetition(c,aaaa)
if success then
local gdomain = mw.loadData ( "Module:Drapeau/Domaine" ) -- chargé seulement sur les articles où ça sert, ça prend un peu de place
    gdata = resultat
else
local symbs = {
    -- Banque de données à minima en cas de bogue dans le Module:Langue/Data
['cm'] = "à la Coupe du monde",
    gdata={
['coupedumonde'] = "à la Coupe du monde",
['france']={item="Q142", flag= "Flag_of_France.svg", name ="France"}
['ce'] = "au championnat d'Europe",
['euro'] = "au championnat d'Europe",
['chm'] = "au championnat du monde",
['can'] = "à la Coupe des Confédérations",
['coupedesconfederations'] = "à la Coupe des Confédérations",
['en'] = "en",
['jo'] = "aux Jeux olympiques",
['jp'] = "aux Jeux paralympiques",
}
}
end


local gdomain
local str = symbs[string.lower(c or '')] or ""
local success, resultat = pcall (mw.loadData, "Module:Drapeau/Domaine" )
if success then
--edition
gdomain = resultat
if(aaaa ~= nil and aaaa ~= "") then
end
if(c=="jo"  or c=="jp" ) then
local o=gdomain.jo["_"..aaaa];
if(o ~= nil) then
str = str .." "..o
end
else
  str = str .." "..aaaa
end
end


 
return str
local function _getCompetition(c,aaaa)
local rep="";
if(c=="CM" or c=="coupedumonde") then
rep="à la Coupe du monde"
elseif(c=="CE" or c=="euro") then
rep="au championnat d'Europe"
elseif(c=="ChM") then
rep="au championnat du monde"
elseif(c=="CAN" or c=="coupedesconfederations") then
        rep="à la Coupe des Confédérations"
    elseif(c=="en" ) then
        rep="en"
    elseif(c=="jo" ) then
        rep="aux Jeux olympiques"
    elseif(c=="jp" ) then
        rep="aux Jeux paralympiques"
    end
   
    --edition
    if(aaaa ~= nil and aaaa ~= "") then
        if(c=="jo"  or c=="jp" ) then
            local o=gdomain.jo["_"..aaaa];
            if(o ~= nil) then
                rep=rep.." "..o
            end           
        else
            rep=rep.." "..aaaa
        end
    end
 
    return rep
end
end


function printIcon(filescr, alt, size, border)
local function printFlag(flagfile, alt, displayformat)
if (border ~= "no") or (border == false) then
border='border|'
displayformat = displayformat or {}
local size = displayformat.size or '20x15'
local border = 'border|'
if displayformat.border == '-' then
border = ''
end
end
if not alt then
if not alt then
alt = ''
alt = ''
end
end
    return '<span class="flagicon">' ..
return '<span class="flagicon">' ..
                '[[Fichier:' .. filescr .. '|' .. size ..'px|' .. border  .. alt ..']]' ..
'[[Fichier:' .. flagfile .. '|' .. size ..'px|' .. border  .. alt ..']]' ..
            '</span>';
'</span>';
end
end


local function getData(datatable, typedata, topic, year)
local function bestfordate(data, period) -- data contient une table dont les clés sont des dates au format ['2010'] = ou ['2010-05-04'] =  
-- récupères les données stockées sur datatable
if type(data) == 'string' then
-- typedata = flag ou nom
return data
-- datedata = year
end
-- domain = arméée etc.)
 
local val = datatable[typedata] --(typedata = nom, drapeau, etc.)
-- très artisanal, à améliorer
if not topic then  --(topic = army, air force, etc.)
if (not period) or (period == 'default') then
topic = 'default'
return data.default
end
end
local alt
-- si val est une chaîne, la retourner, sinon, ça doit être une table
local val = data.default
if type(val) == 'string' then
local bestdate = '-1000-01-01'
return val
for i, j in pairs(data) do
if j ~= 'default' and dates.before(period, i) and dates.before(i, bestdate) then -- si la valeur est plus récente que period moins récente que la best trouvée
val = j
bestdate = i
end
end
end
if type(val) ~= 'table' then  
return val
return error('demande invalide')
end
 
 
local function valueAtDate(data, period, topic) -- topic: type de drapeau, genre de l'adjectif, etc.
if type(data) == 'string' or type(data) == 'nil' then
return data
end
end
if type(data) ~= 'table' then
return error('bad datatabpe ' .. type(data))
end
topic = topic or "default"
if data[topic] then
return bestfordate(data[topic], period)
else
return bestfordate(data, period)
end
return error()
end
local function getData(datatable, typedata, topic, period) -- récupère la chaîne de caractère la plus appropriée dans la datatable
-- datatable: la table de sous module par exemple [[Module:Country data/grenade]]
-- typedata: "flag" / "name" / "adjective"
-- period: data in ISO format
-- topic: for instance "navy" for naval ensign
local val = datatable[typedata]


-- s'il existe une valeur spéciale pour ce topic (comme une valeur spéciale de flag pour topic = army : soit une chaîne, soit une table avec des valeurs chronologiques
if not val then -- error handling ?
if type(val[topic]) == 'table' then
return nil
val = val[topic]
end
 
local val = valueAtDate(val, period, topic)
 
if type(val) ~= 'string' then -- error handling ?
return error(val)
end
return val
end
 
local function getAdjective(data, gender, number)
if not gender then
gender = 'm'
end
if not number then
number = 's'
end
if (gender ~= 'm' and gender ~= 'f') then
return error('gender should be m or f is ' .. gender)
end
end
if type(val[topic]) == 'string' then
if (number ~= 's' and number ~= 'p') then
return val[topic]
return error('number should be s or p is ' .. number)
end
end
return getData(data, 'adjective', (gender .. number))
end


alt = datatable[typedata].alt
local function getDemonym (data, gender, number)
if data.demonym == nil then
local numericdate
return linguistic.ucfirst( getAdjective(data, gender, number) )
if year then
numericdate = tonumber(year)
end
end
if numericdate then
gender = gender or 'm'
local bestdate = -10000
number = number or 's'
for i, j in pairs (datatable[typedata]) do
if (gender ~= 'm' and gender ~= 'f') then
local k = tonumber(i)
return error('gender should be m or f is ' .. gender)
if k and (k < numericdate) and (k > bestdate) then
val = j
bestdate = k
end
end
end
end
if not type(val) == 'string' then
if (number ~= 's' and number ~= 'p') then
return nil
return error('number should be s or p is ' .. number)
end
end
return val, alt
return getData(data, 'demonym', (gender .. number))
end
end


local function printLabel(data, topic)
local function getLabel(data, topic, period, form)
local label
local label
if not name then
if (not form) or form == 'short' then
return getData(data, 'name')
label = getData(data, 'shortname', topic, period)
end
end
if not label then
label = getData(data, 'name')
end
if (not label) and data.item then
label = mw.wikibase.label(getData(data, 'item'))
end
return label
end
end


local function printLink(areadata, topic)
local function getLink(data, topic, period)
if areadata.link then
local link = getData(data, 'link', topic, period)
return areadata.link[topic] or areadata.link['default']
if (not link) and data.item then
link =  mw.wikibase.label(getData(data, 'item'))
end
end
return mw.wikibase.sitelink(data.item)
return link
end
end


Ligne 155 : Ligne 190 :
end
end
return str
return str
end
local function standardText(areadata, topicdata)
-- format standards stockées dans Module:Drapeau/domain
local link = topicdata.link
local label = topicdata.label
return link, label
end
end


Ligne 173 : Ligne 201 :
end
end


function p.List(frame)
 
    return p.list(frame)
local function flagIcon(data, flagtype, period, displayformat)
local flagimage = getData(data, 'flag', flagtype, period)
if flagimage then
return printFlag(flagimage, '', displayformat)
end
end
end


Ligne 191 : Ligne 223 :


   -- ajout des valeurs par défaut
   -- ajout des valeurs par défaut
local size = args["size"] or '20x15'
local size = args.flagsize or '20x15'
local flagtype = args["type"]
local flagtype = args.type


local domain
local align = args.align or 'left'
if args['domain'] and gdomain then
local link = args.link
domain = args['domain']
local period = args.date
domain = mw.ustring.lower(string.gsub(domain, " ", ""))
local competition = args.competition
domain = linguistic.toascii(domain)
local edition = args.edition
domain = gdomain.domainlist["domain"]
end
if args["domain"] and not domain then
return error("domaine non reconnu:" .. args["domain"])
end
local align = args["align"] or 'left'
local link = args["lien"]
local label = args["label"]
local year = args[2] or args["année"] or ''
local align = args["align"] or "left"
local target = args["target"]
local competition = args["compétition"]
local extra = ''
local extra = ''
 
-- chargement des données
if not zone then
return nil -- error('paramètre 1 (pays) manquant')
end
local success, data = pcall(getDatatable, zone)
local success, data = pcall(getDatatable, zone)
if not success then
if not success then
Ligne 222 : Ligne 238 :
return error('lieu non reconnu')
return error('lieu non reconnu')
end
end
return zone, false -- false == échec
return nil
end
end
-- image
-- image
local flagimage, alt = getData(data, 'flag', domain, year)
local flag = flagIcon(data, flagtype, period)
local icon -- le fichier mis en forme
if (args.label == '-') then
if flagimage then
return flag, true
icon = printIcon(flagimage, alt, size, border)
end
if (label == '-') then
return icon
end
if domain then
link, label = standardText(data, domain)
end
end


if (not link) then
-- text
link = printLink(data, flagtype)
local text
end
local label = getLabel(data, flagtype)
if (not label) then
local link = getLink(data, flagtype)
label = printLabel(data, flagtype)
 
end
if competition then  
if competition then  
competition = _getCompetition(linguistic.toascii(args["compétition"]),args["édition"])
competition = _getCompetition(linguistic.toascii(competition),args["édition"])
end
end
if link and competition then
if link and competition then
link = link .. ' ' .. competition
link = link .. ' ' .. competition
Ligne 256 : Ligne 262 :
text = '[[' .. link .. '|' .. label .. ']]'
text = '[[' .. link .. '|' .. label .. ']]'
end
end
if target then
 
text = target .. ' (' .. text .. ')'
-- si les données sont extraites d'un formulaire standard comme module:Drapeau/domain, appliquer des regex
end
if string.match(text, '%$') then
if string.find(text, '$') then -- si les données sont extraites d'un formulaire standard comme module:Drapeau/domain
text = applyregex(text, data)
text = applyregex(text, data)
end
end
Ligne 266 : Ligne 270 :
local val
local val
if align == 'right' then
if align == 'right' then
val = text .. '&nbsp;' ..icon
val = text .. '&nbsp;' .. flag
else
else
val = icon .. '&nbsp;' .. text
val = flag .. '&nbsp;' .. text
end
end
return val, true -- true indique le succès
return val, true -- true indique le succès
end
end


function p.drapeau(frame) -- pour appel depuis un modèle : {{#invoke:Country data|drapeau|nom du pays}}
function p.nationality(zone, gender, number, topic, period)
local success, val = pcall(p._drapeau, frame.args)
local success, data = pcall(getDatatable, zone)
    if success then  
if not success then return
    return val
zone
    end
end
--   return p._drapeau(frame.args)
return frame.args[1]
local str = getDemonym(data, gender, number)
if not str then
return zone
end
local link = getLink(data, topic, period)
if link then
str = '[[' .. link .. '|' .. str .. ']]'
end
return str, true-- true indique le succès
end
 
 
function p.getNationality(args) -- pour obtenir la nationalité d'une personne sur Wikidata sous forme d'adjectifs
if not args then
return nil
end
local wikidata = require "Module:Interface Wikidata".fromLua
local complexdate = require "Module:Date complexe"
if type(args) == 'string' then -- si un seul argument, c'est l'entité à utiliser
args {item = args}
end
if args.args then -- si vient de frame
args = args.args
end
local item = args.item or args[1]
-- établit la variable gender pour l'élément
local vals = {
['Q6581072'] = 'f',
['Q6581097'] = 'm',
default   = '?'
}
local gender = args.gender
if not gender then
gender = wikidata.formatStatements{entity = item, property = 'P21', displayformat = 'raw'}
gender = vals[gender] or vals.default
end
-- désactivation si date de naissance avant l'Ère contemporaine : trop d'imprécisions et d'anachronismes
local mindate = args.mindate or '1789'
if mindate ~= "-" then
local birthdate = wikidata.formatStatements{entity = item, property = 'P569', displayformat = 'raw', numval = 1}
if (not birthdate) or complexdate.before(mindate, birthdate) then
return nil
end
end
return wikidata.formatStatements{
property = 'P27',
vals = statements,
showdate = true,
entity = item,
conjtype = 'and',
removedupes = true,
linktopic = '-',
displayformat =
function(snak)
local g = gender -- genre de la personne, pour affichage du gentilé
if g == '?' then -- si inconnu, au masculin
g = 'm'
end
local val, success = p.nationality(wikidata.getid(snak), g)
if not success then
val = wikidata.formatSnak(snak)
end
return val
end
}
end
end


return p
return p

Dernière version du 30 novembre 2016 à 10:32

--[[ This module is intended to replace the functionality of Drapeau [[Modèle:Drapeau2/Libellé]] and related templates. It provides several methods, including ]]

local p = {};

local gdata = mw.loadData ("Module:Country data/liste" ) local dates = require "Module:date complexe" local linguistic = require "Module:Linguistique"

local function _getCompetition(c,aaaa) local gdomain = mw.loadData ( "Module:Drapeau/Domaine" ) -- chargé seulement sur les articles où ça sert, ça prend un peu de place

local symbs = { ['cm'] = "à la Coupe du monde", ['coupedumonde'] = "à la Coupe du monde", ['ce'] = "au championnat d'Europe", ['euro'] = "au championnat d'Europe", ['chm'] = "au championnat du monde", ['can'] = "à la Coupe des Confédérations", ['coupedesconfederations'] = "à la Coupe des Confédérations", ['en'] = "en", ['jo'] = "aux Jeux olympiques", ['jp'] = "aux Jeux paralympiques", }

local str = symbs[string.lower(c or )] or ""

--edition if(aaaa ~= nil and aaaa ~= "") then if(c=="jo" or c=="jp" ) then local o=gdomain.jo["_"..aaaa]; if(o ~= nil) then str = str .." "..o end else str = str .." "..aaaa end end

return str end

local function printFlag(flagfile, alt, displayformat)

displayformat = displayformat or {} local size = displayformat.size or '20x15' local border = 'border|'

if displayformat.border == '-' then border = end

if not alt then alt = end return '' .. '' .. border .. alt ..'' .. ''; end

local function bestfordate(data, period) -- data contient une table dont les clés sont des dates au format ['2010'] = ou ['2010-05-04'] = if type(data) == 'string' then return data end

-- très artisanal, à améliorer if (not period) or (period == 'default') then return data.default end

local val = data.default local bestdate = '-1000-01-01' for i, j in pairs(data) do if j ~= 'default' and dates.before(period, i) and dates.before(i, bestdate) then -- si la valeur est plus récente que period moins récente que la best trouvée val = j bestdate = i end end return val end


local function valueAtDate(data, period, topic) -- topic: type de drapeau, genre de l'adjectif, etc. if type(data) == 'string' or type(data) == 'nil' then return data end if type(data) ~= 'table' then return error('bad datatabpe ' .. type(data)) end topic = topic or "default" if data[topic] then return bestfordate(data[topic], period) else return bestfordate(data, period) end return error() end

local function getData(datatable, typedata, topic, period) -- récupère la chaîne de caractère la plus appropriée dans la datatable -- datatable: la table de sous module par exemple Module:Country data/grenade -- typedata: "flag" / "name" / "adjective" -- period: data in ISO format -- topic: for instance "navy" for naval ensign local val = datatable[typedata]

if not val then -- error handling ? return nil end

local val = valueAtDate(val, period, topic)

if type(val) ~= 'string' then -- error handling ? return error(val) end return val end

local function getAdjective(data, gender, number) if not gender then gender = 'm' end if not number then number = 's' end if (gender ~= 'm' and gender ~= 'f') then return error('gender should be m or f is ' .. gender) end if (number ~= 's' and number ~= 'p') then return error('number should be s or p is ' .. number) end return getData(data, 'adjective', (gender .. number)) end

local function getDemonym (data, gender, number) if data.demonym == nil then return linguistic.ucfirst( getAdjective(data, gender, number) ) end gender = gender or 'm' number = number or 's' if (gender ~= 'm' and gender ~= 'f') then return error('gender should be m or f is ' .. gender) end if (number ~= 's' and number ~= 'p') then return error('number should be s or p is ' .. number) end return getData(data, 'demonym', (gender .. number)) end

local function getLabel(data, topic, period, form) local label if (not form) or form == 'short' then label = getData(data, 'shortname', topic, period) end if not label then label = getData(data, 'name') end if (not label) and data.item then label = mw.wikibase.label(getData(data, 'item')) end return label end

local function getLink(data, topic, period) local link = getData(data, 'link', topic, period) if (not link) and data.item then link = mw.wikibase.label(getData(data, 'item')) end return link end

local function applyregex(str, areadata) local cio = 'code CIO (en attente)' --require('Module:Wikidata')._formatStatements({entity= areadata.item, property= 'P984'}) or '??'

local label = getData(areadata, 'name') local of = linguistic.of(label, areadata.genre)

str = mw.ustring.gsub(str, '$de$label', of) str = mw.ustring.gsub(str, '$label', label) str = mw.ustring.gsub(str, '$cio', 'cio')

if string.find(str, '$gentile') then local function get(genre) return getData(areadata, 'adjective', genre) end local gentileMS, gentileFS, gentileMP, gentileFP = get('ms'), get('fs'), get('mp'), get('fp') str = mw.ustring.gsub(str, '$gentileMS', gentileMS) str = mw.ustring.gsub(str, '$gentileFS', gentileFS) str = mw.ustring.gsub(str, '$gentileMP', gentileMP) str = mw.ustring.gsub(str, '$gentileFP', gentileFP) end return str end

local function getDatatable(zone) zone = mw.ustring.lower(zone) if gdata[zone] then return require('Module:Country data/' .. gdata[zone]) else -- si le module existe mais n'est pas dans la liste return require('Module:Country data/' .. zone) end end


local function flagIcon(data, flagtype, period, displayformat) local flagimage = getData(data, 'flag', flagtype, period) if flagimage then return printFlag(flagimage, , displayformat) end end

function p.standarddisplay(zone, args) if not zone then return nil end -- nettoyage des paramètres if not args then args = {} end for i, j in pairs(args) do args[i] = mw.text.trim(j) -- remove whitespaces if args[i] == then args[i] = nil end end

  -- ajout des valeurs par défaut

local size = args.flagsize or '20x15' local flagtype = args.type

local align = args.align or 'left' local link = args.link local period = args.date local competition = args.competition local edition = args.edition local extra =

local success, data = pcall(getDatatable, zone) if not success then if args.strict then return error('lieu non reconnu') end return nil end

-- image local flag = flagIcon(data, flagtype, period) if (args.label == '-') then return flag, true end

-- text local text local label = getLabel(data, flagtype) local link = getLink(data, flagtype)

if competition then competition = _getCompetition(linguistic.toascii(competition),args["édition"]) end if link and competition then link = link .. ' ' .. competition end

if link then text = '' .. label .. '' end

-- si les données sont extraites d'un formulaire standard comme module:Drapeau/domain, appliquer des regex if string.match(text, '%$') then text = applyregex(text, data) end

local val if align == 'right' then val = text .. ' ' .. flag else val = flag .. ' ' .. text end

return val, true -- true indique le succès end

function p.nationality(zone, gender, number, topic, period) local success, data = pcall(getDatatable, zone) if not success then return zone end

local str = getDemonym(data, gender, number) if not str then return zone end local link = getLink(data, topic, period) if link then str = '' .. str .. '' end return str, true-- true indique le succès end


function p.getNationality(args) -- pour obtenir la nationalité d'une personne sur Wikidata sous forme d'adjectifs if not args then return nil end local wikidata = require "Module:Interface Wikidata".fromLua local complexdate = require "Module:Date complexe"

if type(args) == 'string' then -- si un seul argument, c'est l'entité à utiliser args {item = args} end if args.args then -- si vient de frame args = args.args end local item = args.item or args[1]

-- établit la variable gender pour l'élément local vals = { ['Q6581072'] = 'f', ['Q6581097'] = 'm', default = '?' } local gender = args.gender if not gender then gender = wikidata.formatStatements{entity = item, property = 'P21', displayformat = 'raw'} gender = vals[gender] or vals.default end -- désactivation si date de naissance avant l'Ère contemporaine : trop d'imprécisions et d'anachronismes local mindate = args.mindate or '1789' if mindate ~= "-" then local birthdate = wikidata.formatStatements{entity = item, property = 'P569', displayformat = 'raw', numval = 1} if (not birthdate) or complexdate.before(mindate, birthdate) then return nil end end return wikidata.formatStatements{ property = 'P27', vals = statements, showdate = true, entity = item, conjtype = 'and', removedupes = true, linktopic = '-', displayformat = function(snak) local g = gender -- genre de la personne, pour affichage du gentilé if g == '?' then -- si inconnu, au masculin g = 'm' end local val, success = p.nationality(wikidata.getid(snak), g) if not success then val = wikidata.formatSnak(snak) end return val end } end

return p