local p = {}

local Outils = require 'Module:Outils' local wikidata = require 'Module:Wikidata' local TNT = require 'Module:TNT'

local defaultLang = 'fr' local taille_logo = 12 local debug = false local ignoreCoordinate = false local linkback

function getMessage(key) return TNT.formatInLanguage(defaultLang, 'I18n/Template:Stations-voisines.tab', key) end

function p.Stations_voisines( id, onlyLineService, rows ) if not id then id = mw.wikibase.getEntityIdForCurrentPage() end local argsData = { entity = id, property = 'P197' } if onlyLineService then argsData.qualifier = { 'P1192', 'P81' } -- ligne ou service argsData.qualifiervalue = onlyLineService end

-- ajout d'une ligne dans le tableau local function addLineDetail( lineDetail ) if lineDetail.color then lineDetail.color = 'style="background:#' .. lineDetail.color .. ';" width="1*" |' else lineDetail.color = end local textBefore = { '| ' } local textAfter = { '| ' } if lineDetail.after and lineDetail.after ~= '-' then -- no value textAfter[ #textAfter + 1] = lineDetail.after if lineDetail.afterDestination and lineDetail.afterDestination ~= '-' then textAfter[ #textAfter + 1] = "
" textAfter[ #textAfter + 1] = getMessage( 'towards' ) textAfter[ #textAfter + 1] = " " textAfter[ #textAfter + 1] = lineDetail.afterDestination textAfter[ #textAfter + 1] = " " end if lineDetail.afterVia and lineDetail.afterVia ~= '-' then textAfter[ #textAfter + 1] = "
" textAfter[ #textAfter + 1] = getMessage( 'via' ) textAfter[ #textAfter + 1] = lineDetail.afterVia textAfter[ #textAfter + 1] = " " end lineDetail.colorAfter = lineDetail.color elseif lineDetail.Y then lineDetail.colorAfter = else textAfter[ #textAfter + 1] = "" textAfter[ #textAfter + 1] = getMessage( 'terminus' ) textAfter[ #textAfter + 1] = "" lineDetail.colorAfter = end if lineDetail.before and lineDetail.before ~= '-' then -- no value textBefore[ #textBefore + 1] = lineDetail.before if lineDetail.beforeDestination and lineDetail.beforeDestination ~= '-' then textBefore[ #textBefore + 1] = "
" textBefore[ #textBefore + 1] = getMessage( 'towards' ) textBefore[ #textBefore + 1] = " " textBefore[ #textBefore + 1] = lineDetail.beforeDestination textBefore[ #textBefore + 1] = " " end if lineDetail.beforeVia and lineDetail.beforeVia ~= '-' then textBefore[ #textBefore + 1] = "
via " textBefore[ #textBefore + 1] = lineDetail.beforeVia textBefore[ #textBefore + 1] = " " end lineDetail.colorBefore = lineDetail.color else textBefore[ #textBefore + 1] = "" textBefore[ #textBefore + 1] = getMessage( 'terminus' ) textBefore[ #textBefore + 1] = "" lineDetail.colorBefore = end local logos = if lineDetail.logo then for _, logo in ipairs( lineDetail.logo ) do logos = logos .. '' .. taille_logo .. ' px ' end end local inverted if lineDetail.beforeCoordinate and lineDetail.afterCoordinate then local direction = lineDetail.beforeDirection or lineDetail.afterDirection if direction == 'Q679' -- west or direction == 'Q684' then -- est if lineDetail.beforeCoordinate.longitude and lineDetail.afterCoordinate.longitude and lineDetail.beforeCoordinate.longitude > lineDetail.afterCoordinate.longitude then inverted = true end else -- north or south if lineDetail.beforeCoordinate.latitude and lineDetail.afterCoordinate.latitude and lineDetail.beforeCoordinate.latitude < lineDetail.afterCoordinate.latitude then inverted = true end end end

rows[ #rows + 1 ] = '|-' if inverted then if debug then lineDetail.name = lineDetail.name .. '
' if lineDetail.afterDirection then lineDetail.name = lineDetail.name .. lineDetail.afterDirection end lineDetail.name = lineDetail.name .. ' <=> ' if lineDetail.beforeDirection then lineDetail.name = lineDetail.name .. lineDetail.beforeDirection end end rows[ #rows + 1 ] = table.concat( textAfter ) rows[ #rows + 1 ] = '| ' .. lineDetail.colorAfter rows[ #rows + 1 ] = '| ' .. logos .. lineDetail.name rows[ #rows + 1 ] = '| ' .. lineDetail.colorBefore rows[ #rows + 1 ] = table.concat( textBefore ) else if debug then lineDetail.name = lineDetail.name .. '
' if lineDetail.beforeDirection then lineDetail.name = lineDetail.name .. lineDetail.beforeDirection end lineDetail.name = lineDetail.name .. ' --- ' if lineDetail.afterDirection then lineDetail.name = lineDetail.name .. lineDetail.afterDirection end end rows[ #rows + 1 ] = table.concat( textBefore ) rows[ #rows + 1 ] = '| ' .. lineDetail.colorBefore rows[ #rows + 1 ] = '| ' .. logos .. lineDetail.name rows[ #rows + 1 ] = '| ' .. lineDetail.colorAfter rows[ #rows + 1 ] = table.concat( textAfter ) end end

local claims = wikidata.getClaims( argsData ) local function getClaimDetail( claim ) claim.lineid = wikidata.getId( claim.currentline ) claim.lineName = wikidata.formatSnak( claim.currentline, { } ) if not claim.lineName or claim.lineName == '-' then -- no value claim.lineName = end local routenumber = wikidata.formatStatements( { entity = claim.lineid, property = 'P1671', numval = 1 } ) if routenumber then claim.sortKey = tonumber( routenumber ) -- 570000 or tonumber( tostring( routenumber:gsub( ' ', ) ) ) -- 655 000 or routenumber:upper() -- KBS 566 else claim.sortKey = claim.lineName:upper() end end

if claims and #claims > 0 then -- récupération des données de bases et de tri local newClaims = { } for _, claim in ipairs( claims ) do local currentlines = wikidata.getQualifiers( claim, { 'P1192', 'P81' }, { } ) -- ligne ou service if currentlines and #currentlines > 0 then for _,currentline in ipairs( currentlines ) do local newClaim = mw.clone( claim ) newClaim.currentline = currentline getClaimDetail( newClaim ) if not onlyLineService or onlyLineService == newClaim.lineid then newClaims[ #newClaims + 1 ] = newClaim end end else claim.lineName = claim.sortKey = claim.lineName:upper() newClaims[ #newClaims + 1 ] = claim end end table.sort( newClaims, function( c1, c2 ) if type( c1.sortKey ) ~= 'string' and type( c2.sortKey ) == 'string' then c1.sortKey = tostring( c1.sortKey ) elseif type( c2.sortKey ) ~= 'string' and type( c1.sortKey ) == 'string' then c2.sortKey = tostring( c2.sortKey ) end return c1.sortKey < c2.sortKey end )

-- parcours de toutes les gares local lineDetail = { } for _, claim in ipairs( newClaims ) do -- nouvelle ligne if claim.lineName ~= lineDetail.name then if lineDetail.name ~= nil then addLineDetail( lineDetail ) end lineDetail = { } lineDetail.name = claim.lineName if claim.currentline then lineDetail.color = wikidata.formatStatements( { entity = claim.lineid, property = 'P465', numval = 1 } ) lineDetail.logo = wikidata.stringTable( { entity = claim.lineid, property = 'P154' } ) end end

local gare = wikidata.formatStatement( claim, { } ) local destination = wikidata.getFormattedQualifiers( claim, { 'P5051' }, { conjtype = ' / ' } ) local via = wikidata.getFormattedQualifiers( claim, { 'P2825' }, { conjtype = 'and' } ) local coordinate = ( not ignoreCoordinate ) and wikidata.stringTable( { entity = wikidata.getMainId( claim ), property = 'P625', numval = 1 } ) local terminusDirection if coordinate and #coordinate > 0 then -- si des coordonées sont présente coordinate = coordinate[ 1 ] local terminusDirections = wikidata.getClaims( { entity = claim.lineid, property = 'P559', showqualifier = 'P560' } ) -- récupérer la liste des terminus et des directions de la ligne if terminusDirections and #terminusDirections > 0 then local terminus = wikidata.getQualifiers( claim, { 'P5051' } ) -- récupérer les terminus de la connexion en cours local terminusQid = { } if terminus then for i, t in ipairs( terminus ) do local tid = wikidata.getId( t ) if tid then terminusQid[ tid ] = true end end end for _, td in ipairs( terminusDirections ) do local d = wikidata.getFormattedQualifiers( td, 'P560', { displayformat = 'raw' } ) local t = wikidata.getMainId( td ) if not terminusDirection and d and terminusQid[ t ] then -- si ce terminus de la ligne fait parti des terminus de ma connexion prendre cet direction terminusDirection = d -- plusieurs résultat possible, prendre le premier end end end end

if not lineDetail.before then -- station 1 lineDetail.before = gare lineDetail.beforeDestination = destination lineDetail.beforeVia = via lineDetail.beforeCoordinate = coordinate lineDetail.beforeDirection = terminusDirection elseif not lineDetail.after then -- station 2 lineDetail.after = gare lineDetail.afterDestination = destination lineDetail.afterVia = via lineDetail.afterCoordinate = coordinate lineDetail.afterDirection = terminusDirection else -- station 3 > nouvelle ligne dans le tableau newLineDetail = { } newLineDetail.name = lineDetail.name newLineDetail.color = lineDetail.color newLineDetail.logo = lineDetail.logo newLineDetail.before = gare newLineDetail.beforeDestination = destination newLineDetail.beforeVia = via newLineDetail.beforeCoordinate = coordinate newLineDetail.beforeDirection = terminusDirection newLineDetail.Y = true addLineDetail( lineDetail ) lineDetail = newLineDetail end end addLineDetail( lineDetail ) -- ajout de la dernière ligne construite

local lb = if linkback ~= '-' then lb = 'modifier Wikidata' rows[ #rows + 1 ] = '|-' rows[ #rows + 1 ] = '| class="noprint navigation-not-searchable tfoot" colspan="5" | ' .. lb end return true else return false end end

function p.main( frame ) local rows = {} local args = Outils.extractArgs( frame ) local id = Outils.validTextArg( args, 1, 'id', 'wikidata', 'entity' ) local onlyLineService = Outils.validTextArg( args, 2, 'ligne', 'line', 'service' ) linkback = Outils.validTextArg( args, 'linkback' ) titre = Outils.validTextArg( args, 'titre', 3 ) or if id and titre == then titre = wikidata.formatEntity( id ) if linkback ~= '-' then titre = wikidata.addLinkBack( titre, id, 'P197' ) linkback = '-' end elseif titre == '-' then titre = end ignoreCoordinate = Outils.validTextArg( args, 'ignore coordinate', 'ignore coordonnées' ) debug = Outils.validTextArg( args, 'debug', 'showinverted' ) -- pour les tests uniquement

rows[ #rows + 1 ] = '{| class="wikitable stations-voisines"' rows[ #rows + 1 ] = '! scope=col | ' .. getMessage( 'station_before' ) rows[ #rows + 1 ] = '! scope=col colspan="3" | ' .. titre rows[ #rows + 1 ] = '! scope=col | ' .. getMessage( 'station_after' )

local found = p.Stations_voisines( id, onlyLineService, rows )

if found then rows[ #rows + 1 ] = '|}' return table.concat( rows, '\n' ) else return nil end end

return p