Module:Tableau Pont

 Documentation[créer] [purger]
local Pont = { }

local Outils = require( 'Module:Outils' )
local TableBuilder = require( 'Module:TableBuilder' )
local Coordinates = require( 'Module:Coordinates' )
local wd = require( "Module:Wikidata" )

local function colonneAvecTri( valeur, tri )
	local sort = ''
	if type(tri) == 'string' and tri ~= '' then
		return '\n|data-sort-value="' .. tri .. '"|' .. ( valeur or '' )
	else
		return '\n|' .. ( valeur or '' )
	end
end

local function colonneCoord( validArg, nom, ... )
	local entite = validArg ('entite', 'entity')
	local latitude = validArg( 'latitude', 'lat' )
	local longitude = validArg( 'longitude', 'lon' )
	if latitude and longitude then
		local argsCoord = { 
			latitude = latitude, 
			longitude = longitude, 
			format = 'dms long'
		}
		return Coordinates._coord( argsCoord )
	elseif wd.formatStatements{entity = entite, property = 'P625', numval = 1, displayformat=longitude} then
		local argsCoord = { 
			wikidata = 'true',
			wikidataquery = {property = 'P625', entity = entite}
		}
		return Coordinates._coord( argsCoord )
	else
		return 'à géolocaliser'
	end
end

local function colonneImage( args )
	local function getArg( t, Key )
		if type( t[1] ) == 'table' then
			return t[1][ Key ]
		end
	end
	setmetatable( args, { __index = getArg } )
	
	local image = args.image
	local taille = args['taille image']
	local entite = args.entite
	local nom = args.nom
	if type(nom) == 'nil' or nom == '' then
		nom = mw.wikibase.getLabelByLang( tostring(entite), 'fr' ) or ''
	end
	if type(image) == 'nil' or image == '' then
		image = wd.formatStatements{entity = entite, property = 'P18', numval = 1}
	end
	if type( image ) == 'string' and image ~= '' then
		local size
		if tonumber( taille ) and tonumber( taille ) > 10 then
			-- taille en pixels
			size = '|' .. taille .. 'x' .. taille ..'px'
		else
			size = '| 120px'
		end
		return '\n| [[Fichier:' .. image .. size ..'|center|alt=' .. nom .. ']]'
	else
		return '\n|'
	end
end

local function longueur( longueur, entite )
	if tonumber(longueur) then
		if tonumber(longueur) < 1000 then
			return '\n|' .. longueur .. ' m'
	    else 
			return '\n|' .. longueur/1000 .. ' km'
	    end
	elseif longueur and longueur ~= '' then
		return '\n|' .. longueur
	elseif wd.formatStatements{entity = entite, property = 'P2043', numval = 1} then
		return '\n|' .. wd.formatStatements{entity = entite, property = 'P2043', numval = 1}
	else
		return '\n|'
	end
end

Pont.lignePont = function ( frame )
	local args = Outils.extractArgs( frame )
	local validArg = function ( ... ) return Outils.validTextArg( args, ... ) end
	local wiki = TableBuilder.new( '|-' )

	-- nom
	lang = mw.getContentLanguage()
	local nom = args.nom
	if type(nom) == 'nil' or nom == '' then
		nom = wd.formatEntity( tostring(args.entite), {lang='fr'} )
	end
	wiki.minsert( colonneAvecTri(lang:ucfirst(nom), args.tri) )
	
	-- notice
	local notice = args.notice or ''
	local count = 1
	if type(notice) == 'nil' or notice == ''  then
		if wd.formatStatements{entity = args.entite, property = 'P454'} then
			notice = '[https://structurae.net/en/structures/' .. wd.formatStatements{entity = args.entite, property = 'P454', numval=1} .. ' ' .. count ..  ']'
			count = count + 1
		end
		if wd.formatStatements{entity = args.entite, property = 'P380'} then
				notice = notice .. '<br />[https://www.pop.culture.gouv.fr/notice/merimee/' .. wd.formatStatements{entity = args.entite, property = 'P380', numval=1} .. ' ' .. count .. ']'
				count = count + 1
		end
		if wd.formatStatements{entity = args.entite, property = 'P214'} then
				notice = notice .. '<br />[https://viaf.org/viaf/' .. wd.formatStatements{entity = args.entite, property = 'P214', numval=1} .. ' ' .. count .. ']'
				count = count + 1
		end
	end
	wiki.minsert( '\n|', notice )
	
	-- image
	wiki.minsert( colonneImage{ args, ['taille image'] = args['taille image'] or 120} )
	
	-- pays
	local pays = args.pays
	local tabPays = ''
	if pays then
		if pays == 'oui' then
			pays, nb =  wd.formatStatements{entity = args.entite, property = 'P17', returnnumberofvalues=1}
			for i = 1, nb do
				if tabPays == '' then
					tabPays = '<span class="nowrap">[[File:Flag_of_' .. wd.formatStatements{entity = args.entite, property = 'P17', numval=1, numval2=i-1, lang='en', link='-'} .. '.svg|20px|' .. wd.formatStatements{entity = args.entite, property = 'P17', numval=1, numval2=i-1} .. ']] ' .. wd.formatStatements{entity = args.entite, property = 'P17', numval=1, numval2=i-1} .. '</span>'
				else
					tabPays = tabPays .. '<br /><span class="nowrap">[[File:Flag_of_' .. wd.formatStatements{entity = args.entite, property = 'P17', numval=1, numval2=i-1, lang='en', link='-'} .. '.svg|20px|' .. wd.formatStatements{entity = args.entite, property = 'P17', numval=1, numval2=i-1} .. ']] ' .. wd.formatStatements{entity = args.entite, property = 'P17', numval=1, numval2=i-1} .. '</span>'
				end
			end
			wiki.minsert( '\n|', tabPays )
		else
			wiki.minsert( '\n|', pays )
		end
	end
	
	-- subdivision
	local subdivision = args.subdivision
	if type(subdivision) == 'nil' or subdivision == '' then
		subdivision = wd.formatStatements{entity = args.entite, property = 'P131'}
	end
	wiki.minsert( colonneAvecTri(subdivision, args['tri subdivision'] ) )

	-- coordonées
	if args['carte interactive'] then
		local geojson = {}
		local properties = {}
		local coordinates = {}
		query =  {entity = args.entite, property = 'p625'}
		query.formatting = 'raw'
		local claim = wd.getClaims(query)
		local coords
		
		if claim and claim[1] then -- redundant but more robust in case of a change in the code of Module:Wikidata
			coords = wd.formatSnak(claim[1].mainsnak)
		end
		
		properties['title'] = lang:ucfirst(nom)
		properties['marker-symbol'] = "marker"
		properties['marker-size'] = "large"
		properties['marker-color'] = "0050d0"
			    
		geojson = {
			type = "Feature",
			geometry = {
				type = "Point",
				coordinates = {
					tonumber(args.longitude) or coords.longitude,
					tonumber(args.latitude) or coords.latitude
				}
			},
		    properties = properties
		}
		
		local tagArgs = {
			zoom = args.zoom or 14,
			height = args['taille carte'] or 140,
			width = args['taille carte'] or 140,
			frameless = '',
			align = 'center'
		}
		
		wiki.minsert( '\n|', frame:extensionTag('mapframe',  mw.text.jsonEncode(geojson), tagArgs) )
		wiki.minsert( '<br />', colonneCoord( validArg ) )
	else
		wiki.minsert('\n|style="text-align:center; width:9em;"|', colonneCoord( validArg ) )
	end
	
	-- cours d'eau
	if args["cours d'eau"] then
		wiki.minsert( '\n|', args["cours d'eau"] )
	end
	
	-- type
	local tabType = ''
	local entType = ''
	local typ = args.type
	if (type(typ) == 'nil' or typ == '') and args.entite and args.entite ~= '' and wd.formatStatements{entity = args.entite, property = 'P31'} then
		typ, nb = wd.formatStatements{entity = args.entite, property = 'P31', returnnumberofvalues=1}
		for i = 1, nb do
			entType = wd.formatStatements{entity = args.entite, property = 'P31', numval = 1, numval2=i-1, displayformat = 'raw'}
			if entType == 'Q3397526' or entType == 'Q1055465' or entType == 'Q653401' or entType == 'Q158438' or entType == 'Q12570' or entType == 'Q158555' or entType == 'Q5592057'  or entType == 'Q1825472' or entType == 'Q6752690' or entType == 'Q14276458'  or entType == 'Q158218' or entType == 'Q158626' then
				if tabType ~= '' then
					tabType = tabType .. ', ' .. wd.formatStatements{entity = args.entite, property = 'P31', numval = 1, numval2=i-1}
				else
					tabType = wd.formatStatements{entity = args.entite, property = 'P31', numval = 1, numval2=i-1}
				end
			end
		end
		wiki.minsert( '\n|', tabType )
	else
		wiki.minsert( '\n|', typ )
	end
	
	
	-- fonction
	local tabFonction = ''
	local entFonction = ''
	local fonction = args.fonction
	if (type(fonction) == 'nil' or fonction == '') and args.entite and args.entite ~= '' and wd.formatStatements{entity = args.entite, property = 'P31'} then
		useless, nb = wd.formatStatements{entity = args.entite, property = 'P31', returnnumberofvalues=1}
		for i = 1, nb do
			entFonction = wd.formatStatements{entity = args.entite, property = 'P31', numval = 1, numval2=i-1, displayformat = 'raw'}
			if entFonction == 'Q6636777' or entFonction == 'Q537127' or entFonction == 'Q1210334' or entFonction == 'Q1068842' or entFonction == 'Q474' or entFonction == 'Q1030403' or entFonction == 'Q2525227' or entFonction == 'Q1077135'  then
				if tabFonction ~= '' then
					tabFonction = tabFonction .. ', ' .. wd.formatStatements{entity = args.entite, property = 'P31', numval = 1, numval2=i-1}
				else
					tabFonction = wd.formatStatements{entity = args.entite, property = 'P31', numval = 1, numval2=i-1}
				end
			end
		end
	if wd.formatStatements{entity = args.entite, property = 'P2505'} then
		if tabFonction ~= '' then
			tabFonction = tabFonction .. ', ' .. wd.formatStatements{entity = args.entite, property = 'P2505'}
		else
			tabFonction = wd.formatStatements{entity = args.entite, property = 'P2505'}
		end
	end
		wiki.minsert( '\n|', tabFonction )
	else
		wiki.minsert( '\n|', fonction )
	end
	
	-- point KM
	if args["point KM"] then
		wiki.minsert( '\n|', args["point KM"] )
	end
	
	-- longueur
	wiki.minsert( longueur(args.longueur, args.entite) )
 
 	-- protection
	if args.protection then
		wiki.minsert( '\n|', args.protection )
	end
	
	-- date
	local date = args.date
	if type(date) == 'nil' or date == '' then
		date = wd.formatStatements{entity = args.entite, property = 'P1619', linktopic = '-' }
		if type(date) == 'nil' or date == '' then
			date = wd.formatStatements{entity = args.entite, property = 'P571', linktopic = '-' }
		end
	end
	wiki.minsert( '\n|', date )

	return wiki.concat()
end

return Pont