Module:Tableau Liste commune de France 2
La documentation de ce module est générée par le modèle {{Documentation module}}.
Les éditeurs peuvent travailler dans le bac à sable (créer).
Voir les statistiques d'appel depuis le wikicode sur l'outil wstat et les appels depuis d'autres modules.
local p = {}
-- lookup caches for performances
local mw = mw
local languageObj = mw.getContentLanguage()
local currentFrame = mw.getCurrentFrame()
local function formatnum( nombre )
return languageObj:formatNum( nombre )
end
local function fullurl( page, query )
return tostring( mw.uri.fullUrl( page, query ) )
end
local function template( title, args )
return currentFrame:expandTemplate{ title = title, args = args }
end
local function abrd( abreviation, signification )
return template( 'abréviation discrète', { abreviation, signification } )
end
local function nobr( texte )
return template( 'nobr', { texte } )
end
function p._debut( args )
local charte = args.charte or ''
local titre = args.titre
if not titre or titre == '' then
local switch = {
['localité'] = 'de la localité',
['intercommunalité'] = 'de l\'intercommunalité',
['canton'] = 'du canton',
['circonscription'] = 'de la circonscription',
['arrondissement'] = 'de l\'arrondissement',
['département'] = 'du département',
['région'] = 'de la région',
['unité urbaine'] = 'de l\'unité urbaine',
['aire urbaine'] = 'de l\'aire urbaine',
['métropole'] = 'de la métropole',
['commune nouvelle'] = 'déléguées',
}
titre = 'Liste des communes'
if switch[charte] then
titre = titre .. ' ' .. switch[charte]
elseif charte == 'autre' and args.autre and args.autre ~= '' then
titre = titre .. ' ' .. args.autre
end
end
local bgColors = {
['intercommunalité'] = 'ffe2bf',
['canton'] = 'ece5ca',
['circonscription'] = 'ece5ca',
['arrondissement'] = 'e1e1e1',
['département'] = 'f6f3dd',
['métropole'] = 'f6f3dd',
['région'] = 'bbdefd',
['unité urbaine'] = 'ece5ca',
['aire urbaine'] = 'ece5ca',
['commune nouvelle'] = 'ddffdd',
['_default'] = 'f2f2f2',
}
local bgColor = bgColors[charte] or bgColors._default
local titresColonne3 = {
['métropole'] = '[[Gentilé]]',
['intercommunalité'] = '[[Gentilé]]',
['unité urbaine'] = 'Statut',
['aire urbaine'] = 'Statut',
['canton'] = 'Intercommunalité',
['circonscription'] = 'Intercommunalité',
['arrondissement'] = 'Intercommunalité',
['département'] = 'Intercommunalité',
['région'] = 'Intercommunalité',
['autre'] = 'Intercommunalité',
['_default'] = 'Intercommunalité',
}
local titreColonne3 = titresColonne3[charte] or titresColonne3._default
local exp2 = template( '2' )
return table.concat( {
'{| class="wikitable sortable" align="center"',
'|+ ' .. titre,
'|- align="center"',
'! scope="col" style="background-color:#' .. bgColor .. '" | Nom',
'! scope="col" style="background-color:#' .. bgColor .. '" | Code <br> ' .. abrd( 'Insee', 'Institut national de la statistique et des études économiques' ),
'! scope="col" style="background-color:#' .. bgColor .. '" | ' .. titreColonne3,
'! scope="col" style="background-color:#' .. bgColor .. '" | Superficie <br> <small>' ..'('.. abrd( 'km' .. exp2, 'kilomètres carrés' ) ..')'.. '</small>',
'! scope="col" style="background-color:#' .. bgColor .. '" | Population <br> <small>' .. nobr( '(dernière [[Recensement de la population en France#Décomposition de la population légale depuis 2009|' .. abrd( 'pop. légale', 'population légale' ) .. ']])' ) .. '</small>',
'! scope="col" style="background-color:#' .. bgColor .. '" | Densité <br> <small>' .. nobr( '('.. abrd( 'hab./km' .. exp2, 'habitants par kilomètre carré' ) ..')' ) .. '</small>',
'|-',
}, '\n' )
end
function p._lignes( args )
local charte = args.charte or ''
local switchComplement = {
['intercommunalité'] = 'siège',
['métropole'] = 'siège',
['arrondissement'] = 'chef-lieu',
['canton'] = 'bureau centralisateur',
['département'] = 'préfecture',
['région'] = 'préfecture',
['_default'] = 'siège',
}
local complement = switchComplement[charte] or switchComplement._default
local result = {}
result[#result+1] = p._ligne( args['chef-lieu'], charte, complement )
result[#result+1] = p._ligne( args['bureau'], charte, complement )
for i = 1, 453 do
local commune = args['commune ' .. i]
if commune and commune ~= '' then
result[#result+1] = p._ligne( commune, charte )
end
end
return table.concat( result, '\n' )
end
function p._ligne( commune, charte, complement )
if not commune or commune == '' then
return nil
end
local pageInfosGenerales = 'Modèle:Données/' .. commune .. '/informations générales'
local titleObj = mw.title.new( pageInfosGenerales )
if not titleObj or not titleObj.exists then
return table.concat( {
'|- align="center"',
'| align="left" | ' .. commune,
'| <span class="plainlinks">[' .. fullurl( pageInfosGenerales, 'action=edit§ion=0' ) .. ' <span style="color:#F00">?????</span>]</span> [[Catégorie:Article avec modèle Tableau Liste commune de France 2 erroné]]',
'|',
'|',
'|',
'|',
}, '\n' )
end
local function getInfoGenerale( name )
local result = template( pageInfosGenerales, { name } )
if result == '' then
return nil
end
return result
end
local modulePopulation = require 'Module:Population de France'
local donneesPopulation = modulePopulation.derniere_population_m{ nom = commune, type = 'table' }
if type( donneesPopulation ) ~= 'table' then
donneesPopulation = {}
end
local data = {
nomAffiche = getInfoGenerale( 'nom' ),
insee = getInfoGenerale( 'insee' ),
superficie = tonumber( getInfoGenerale( 'superficie' ) ),
dernierePopulation = donneesPopulation[2],
derniereAnnee = donneesPopulation[1],
}
local function cellNomCommune()
local lien
if data.nomAffiche then
lien = '[[' .. commune .. '|' .. data.nomAffiche .. ']]'
else
lien = '[[' .. commune .. ']]'
end
if complement then
return "'''" .. lien .. "'''" .. '<br><small>(' .. complement .. ')</small>'
else
return lien
end
end
local function cellInsee()
if not data.insee then
return ''
end
return '<span class="plainlinks">[' .. fullurl( pageInfosGenerales, 'action=edit§ion=0' ) .. ' <span style="color:#000">' .. data.insee .. '</span>]</span>'
end
local function cellColonne3()
local switch = {
['métropole'] = 'gentilé',
['intercommunalité'] = 'gentilé',
['unité urbaine'] = 'statut',
['aire urbaine'] = 'statut',
['canton'] = 'intercommunalité',
['circonscription'] = 'intercommunalité',
['arrondissement'] = 'intercommunalité',
['département'] = 'intercommunalité',
['région'] = 'intercommunalité',
['_default'] = 'intercommunalité',
}
local infoColonne3 = getInfoGenerale( switch[charte] or switch._default )
return infoColonne3 or ''
end
local function cellSuperficie()
if not data.superficie then
return ''
end
return '<span class="plainlinks">[' .. fullurl( pageInfosGenerales, 'action=edit§ion=0' ) .. ' <span style="color:#000">' .. formatnum( data.superficie ) .. '</span>]</span>'
end
local function cellDernierePopulation()
if not data.dernierePopulation or not data.derniereAnnee then
return ''
end
return template( 'ntsh', { data.dernierePopulation } ) .. '<span class="plainlinks">[' .. fullurl( 'Module:Données/' .. commune .. '/évolution population', 'action=edit' ) .. ' <span style="color:#000">' .. formatnum( data.dernierePopulation ) .. '</span>]</span> <small>(' .. data.derniereAnnee .. ')</small>'
end
local function cellDensite()
if not data.dernierePopulation or not data.superficie then
return ''
end
local densite = data.dernierePopulation / data.superficie
local arrondi
if densite > 10 then
arrondi = 0
elseif densite >= 1 then
arrondi = 1
else
arrondi = 2
end
return formatnum( tonumber( ( '%.' .. arrondi .. 'f' ):format( densite ) ) )
end
return table.concat( {
'|- align="center"',
'| align="left" | ' .. cellNomCommune(),
'| ' .. cellInsee(),
'| align="left" | ' .. cellColonne3(),
'| align="right" | ' .. cellSuperficie(),
'| align="right" | ' .. cellDernierePopulation(),
'| align="right" | ' .. cellDensite(),
}, '\n' )
end
function p._fin()
return '|}' .. '[[Catégorie:Article avec modèle Tableau Liste commune de France 2]]'
end
function p.main( frame )
local parentArgs = frame:getParent().args
return table.concat( {
p._debut( parentArgs ),
p._lignes( parentArgs ),
p._fin(),
}, '\n' )
end
return p