Module:Opvolgende stations

Uit Wikipedia, de vrije encyclopedie
Naar navigatie springen Naar zoeken springen

Documentatie voor deze module kan aangemaakt worden op de volgende pagina: Module:Opvolgende stations/doc

local p = {}
--Lua code made by Eru aimed at presenting preceding and following subway stations, sorted by geographical order and by service/line number/name. 
--For time being, still in bêta. Original code at https://commons.wikimedia.org/wiki/Module:Adjacent_stations
--If any requests or comments, please add them to (where?)
--Do not copy for time being, experimentele module JB63

-- default params

local defaultLang = mw.language.getContentLanguage().code
local defaultLink = 'nlwiki' -- modify to your local wiki, like frwiki
local size_logo = 24
local showdebug = false
local ignoreCoordinate = false
local linkback
local showsource = false--parameter to help present sources, if they are present in Wikidata. Make sure false for frwiki as they do not want sources in that table
local with_bearing=true--parameter to help decide to calculate with bearing or with classic line direction method (false)

-- module loading

local function loadModule(name, name2)
	local exist, res = pcall(require, name)
	if not exist then
		exist, res = pcall(require, name2)
	end
	return exist and res
end
local wikidata
if defaultLink == 'eowiki' or defaultLink == 'wikidata' or defaultLink == 'ptwiki' or defaultLink == 'nlwiki' then -- too much incompatibilities in [[Module:Wikidata]]
	wikidata = loadModule( 'Module:Wikidata frwiki' ) -- original code at https://fr.wikipedia.org/wiki/Module:Wikidata
	wikidata.getReferences = nil
	wikidata.sourceStr = nil
else
	wikidata = loadModule( 'Module:Wikidata' ) -- original code at https://fr.wikipedia.org/wiki/Module:Wikidata or https://commons.wikimedia.org/wiki/Module:Wikidata
end

local tools = loadModule( 'Module:Wikidata/Tools' ) -- original code at https://commons.wikimedia.org/wiki/Module:Wikidata/Tools
local TNT = require 'Module:TNT' -- https://www.mediawiki.org/wiki/Module:TNT

-- compatiblity of [[Module:Wikidata]] on various wiki

wikidata.formatEntity = wikidata.formatEntity -- frwiki
	or wikidata._getLabel -- commons
wikidata.getId = wikidata.getId -- frwiki
	or ( tools and tools.getId ) -- commons
wikidata.getFormattedQualifiers = wikidata.getFormattedQualifiers -- frwiki
	or wikidata.formatStatementQualifiers -- commons
wikidata.sourceStr = wikidata.sourceStr -- frwiki
	or function( sources, hashes ) -- commons
	return sources
end
wikidata.stringTable = wikidata.stringTable -- commons
	or wikidatafr.stringTable -- eowiki
wikidata.getClaims = wikidata.getClaims -- commons
	or wikidatafr.getClaims -- eowiki

-- private functions

local function getMessage(key, ...)
	return TNT.formatInLanguage(defaultLang, 'I18n/Template:Stations-voisines.tab', key, {...}) -- loading of https://commons.wikimedia.org/wiki/Data:I18n/Template:Stations-voisines.tab
end
--from https://commons.wikimedia.org/wiki/Module:Bearing
require ('Module:No globals');

local function bearing (from_lat, from_long, to_lat, to_long)
	if not (to_lat and to_long) then
		return nil
	end
	local from_lat_rad = math.rad (from_lat);									-- convert degree inputs to radians
	local from_long_rad = math.rad (from_long);
	local to_lat_rad = math.rad (to_lat);
	local to_long_rad = math.rad (to_long);
	
	local delta_long_rad = to_long_rad - from_long_rad;
	local x = math.cos (to_lat_rad) * math.sin (delta_long_rad);
	local y = (math.cos (from_lat_rad) * math.sin (to_lat_rad)) - (math.sin (from_lat_rad) * math.cos (to_lat_rad) * math.cos (delta_long_rad));
	local beta = math.atan2 (x, y);												-- result in radians
	beta = math.deg (beta);														-- convert to degrees
	return (0.0 > beta) and (beta + 360.0) or beta;								-- when beta is negative, add 360 degrees to make a positive bearing
end

--[[--------------------------< E X P O R T S >----------------------------------------------------------------
]]


-- from [[fr:Module:Outils]]

local function trim( texte )
	if type( texte ) == 'string' and texte ~= '' then
		texte = texte:gsub( '^%s*(%S?.-)%s*$', '%1' )
		if texte ~= '' then
			return texte
		end
	end
	return nil
end

local function validTextArg( args, name, ... )
	local texte = trim( args[name] )
	if texte then
		return texte
	end
	if select( '#', ... ) > 0 then
		return validTextArg( args, ... )
	end
	return nil
end

local function extractArgs ( frame )
	if type( frame.getParent ) == 'function' then
		local args = frame:getParent().args
		for k,v in pairs( frame.args ) do
			args[k] = v;
		end
		return args
	else
		return frame
	end
end

-- from [[fr:Module:Wikidata]]

local function getMainId(claim)
	return wikidata.getId(claim.mainsnak)
end

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

local function addLinkBack(str, id, property)
	if not id or id == '' then
		id = wikidata.getEntityIdForCurrentPage()
	end
	if not id then
		return str
	end
	if type(property) == 'table' then
		property = property[1]
	end
	
	id = 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]]'--pencil icon to help modify Wikidata
	local title = getMessage('see-wikidata-value')
	local url = mw.uri.fullUrl('d:' .. id, 'uselang=nl')
	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')
			:wikitext(icon:format(title, url))
		:allDone()
	return tostring(v)
end

-- from [[commons:Module:Wikidata]]

local function getQualifiers(statement, qualifs, params)
	if not statement.qualifiers then
		return nil
	end
	local vals = {}
	for i, j in pairs(qualifs) do
		j = string.upper(j)
		if statement.qualifiers[j] then
			local inserted = false
			if statement.qualifiers[j][1].datatype == 'monolingualtext' then
				local in_preferred_lang
				for _, language in ipairs(fb.fblist(params.lang or defaultlang, true)) do
					for _, snak in ipairs(statement.qualifiers[j]) do
						if isInLanguage(snak, language) then
							in_preferred_lang = snak
							break
						end
					end
					if in_preferred_lang then
						break
					end
				end
				if in_preferred_lang then
					table.insert(vals, in_preferred_lang)
					inserted = true
				end
			end
			if not inserted then
				for _, snak in pairs(statement.qualifiers[j]) do
					table.insert(vals, snak)
				end
			end
		end
	end
	if #vals == 0 then
		return nil
	end
	return vals
end

wikidata.getReferences = wikidata.getReferences or function(statement)
	local cite = loadModule( 'Module:Cite' )
	local isSpecial = wikidata.isSpecial or ( tools and tools.isSpecial )
	if not isSpecial or not cite or not statement.references then
		return ''	
	end
	local frame = mw.getCurrentFrame()
	local sourcestring = ''
	for i, ref in pairs(statement.references) do
		local s
		if ref.snaks.P248 then
			for j, source in pairs(ref.snaks.P248) do
				if not isSpecial(source) then
					local page
					if ref.snaks.P304 and not isSpecial(ref.snaks.P304[1]) then
						page = ref.snaks.P304[1].datavalue.value
					end
					s = cite.citeitem('Q' .. source.datavalue.value['numeric-id'], lang, page)
					s = frame:extensionTag('ref', s)
					sourcestring = sourcestring .. s
				end
			end
		elseif ref.snaks.P854 and not isSpecial(ref.snaks.P854[1]) then
			s = frame:extensionTag('ref', wikidata.formatSnak(ref.snaks.P854[1], {}))
			sourcestring = sourcestring .. s
		end
	end
	return sourcestring
end

-- public functions
function p.Lijn_info (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 or service
		argsData.qualifiervalue = onlyLineService
	end
	--localisation principale
	local mainCoordinate = ( not ignoreCoordinate ) and wikidata.stringTable( { entity = id, property = 'P625', numval = 1 } )
	if mainCoordinate and #mainCoordinate > 0 then -- si des coordonnées pour la présente station sont présentes / if coordinates are present for the current subway
		mainCoordinate = mainCoordinate[ 1 ]
	end
    local function Split(s, delimiter)
        result = {};
        for match in (s..delimiter):gmatch("(.-)"..delimiter) do
            table.insert(result, match);
        end
        return result;
    end
    local claims = wikidata.getClaims( argsData )
	local function getClaimDetail( claim )
		claim.lineid = wikidata.getId( claim.currentline )
		claim.lineName = wikidata.formatSnak( claim.currentline, { link = defaultLink } )
		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 --stations gevonden
		-- récupération des données de bases et de tri des lignes / recovery of basic data and line sorting
		local newClaims = { }
		local defaultSortKey = 1
		for _, claim in ipairs( claims ) do
			claim.defaultSortKey = defaultSortKey
			defaultSortKey = defaultSortKey + 1
			local currentlines = getQualifiers( claim, { 'P1192', 'P81' }, { } ) -- ligne or 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 c1.sortKey == c2.sortKey then
					return c1.defaultSortKey < c2.defaultSortKey
				elseif type( c1.sortKey ) == 'number' and type( c2.sortKey ) == 'number' then
					return c1.sortKey < c2.sortKey
				else
					return tostring( c1.sortKey ) < tostring( c2.sortKey )
				end
			end
		)

		-- doorloop de gevonden stations
		local lineDetail = { }
        local destinaziuns = { } 
        local zulu = 0
        local terminusDirection
        local terminusDirections
        local zelf = wikidata.formatEntity( id , { link = defaultLink } )
		for _, claim in ipairs( newClaims ) do
            
			-- nieuwe lijn
            if claim.lineName ~= lineDetail.name then
               if lineDetail.name ~= nil then
				  if terminusDirections and #terminusDirections > 0 then
                     for _, terminusDirection in ipairs( terminusDirections ) do
                       if zulu>0 then
                           zulu=zulu-1 
                           local destinaziun = wikidata.formatStatement( terminusDirection, { link = defaultLink }  )
                           if lineDetail.logolink == nil then lineDetail.logolink = zelf end                          
                           if lineDetail.logo then
			                  for _, logo in ipairs( lineDetail.logo ) do 
                                 if destinaziuns[destinaziun]~='zulu' then
                                     local str=destinaziuns[destinaziun]
                                     local fermeda=''
                                     local memo = destinaziun 
                                     local k=0 
                                     local cc=true
                                     for i = 1, #str do
                                         k= k + 1
                                         local c = str:sub(i,i)
                                         if c =='~' then 
                                            if cc==false then cc=true else cc=false end
                                            if k==#str or cc==true then 
rows[ #rows + 1 ] = '<tr><td style="width:32px">[[Image:' ..logo .. '|' .. size_logo .. ' px|link='..lineDetail.logolink..']]</td><td style="width:116px">' ..memo.. '</td><td style="width:116px">' ..fermeda.. '</td></tr>'
                                            fermeda = ''
                                            memo = destinaziun 
                                            end
                                         else
                                            if cc==true then fermeda = fermeda..c
                                            else memo = memo..c 
                                            end
                                         end
                                     end
                                 end  
                              end
                           end
                       end
                       
                    end
                  end    
				end 
				zulu=0
                
                lineDetail = { }
			    destinaziuns = { }
                local textLine = {''} 
			    lineDetail.name = claim.lineName
			    lineDetail.logo = wikidata.stringTable( { entity = claim.lineid, property = 'P154' } )
                terminusDirections = wikidata.getClaims( { entity = claim.lineid, property = 'P559'} ) -- verzamel de eindpunten van de lijn
			    if terminusDirections and #terminusDirections > 0 then
                   for _, terminusDirection in ipairs( terminusDirections ) do
                       if zulu<#terminusDirections then 
                             local destinaziun = wikidata.formatStatement( terminusDirection, { link = defaultLink }  )
                             zulu = zulu +1
                             destinaziuns[destinaziun] = 'zulu'
                        
                       end                       
                   end
               end
               destinaziuns[zelf] = 'Eindpunt~'
           end --einde nieuwe lijn 
           local station = wikidata.formatStatement( claim, { link = defaultLink }  )
           local destination = wikidata.getFormattedQualifiers( claim, { 'P5051' }, { conjtype = getMessage( 'or', ' ' ), link = defaultLink } )
           local via = wikidata.getFormattedQualifiers( claim, { 'P2825' }, { conjtype = getMessage( 'and' ), link = defaultLink } )
           if claim.currentline ~= nil then
           local str = wikidata.formatSnak( claim.currentline, { link = defaultLink } )
           local logolink =""
           local cc=true
           for i = 1, #str do
               local c = str:sub(i,i)
               if c =='|' then cc=false end
               if cc then
                   if c ~='[' then 
                       logolink = logolink..c
                   end
               end
           end
           lineDetail.logolink = logolink
           if station == nil or station =='' then station='station' end
           if via ~= nil and via ~='' then
               via='~<br><small>via: '..via..'</small>'
           else 
               via=''
           end
           if destination ~= nil and destination ~='' then
               local j = 0
               local cc=false
               local destinaziun=''
               for i = 1, #destination do
                   local c = destination:sub(i,i)
                   if c =='[' then
                       cc=true
                       j = 0
                   end
                   if cc then
                       if c ==']' then j= j+1 end  
                       if j<3 then
                           destinaziun = destinaziun..c
                       end
                       if j==2 then
                           if destinaziuns[destinaziun] ~= nil and destinaziuns[destinaziun] ~='' then
                               if destinaziuns[destinaziun]=='zulu' then
                                   destinaziuns[destinaziun]=station..via..'~'
                               else
                                   destinaziuns[destinaziun]=destinaziuns[destinaziun]..station..via..'~'
                               end
                           end
                           destinaziun=''
                           cc=false
                       end
                   end
               end
              end
           end
        end --einde doorloop gevonden stations

        if lineDetail.name ~= nil then
				  if terminusDirections and #terminusDirections > 0 then
                     for _, terminusDirection in ipairs( terminusDirections ) do
                       if zulu>0 then
                           zulu=zulu-1 
                           local destinaziun = wikidata.formatStatement( terminusDirection, { link = defaultLink }  )
                           if lineDetail.logolink == nil then lineDetail.logolink = zelf end                          
                           if lineDetail.logo then
			                  for _, logo in ipairs( lineDetail.logo ) do 
                                 if destinaziuns[destinaziun]~='zulu' then
                                     local str=destinaziuns[destinaziun]
                                     local fermeda=''
                                     local memo = destinaziun 
                                     local k=0 
                                     local cc=true
                                     for i = 1, #str do
                                         k= k + 1
                                         local c = str:sub(i,i)
                                         if c =='~' then 
                                            if cc==false then cc=true else cc=false end
                                            if k==#str or cc==true then 
rows[ #rows + 1 ] = '<tr><td style="width:32px">[[Image:' ..logo .. '|' .. size_logo .. ' px|link='..lineDetail.logolink..']]</td><td style="width:116px">' ..memo.. '</td><td style="width:116px">' ..fermeda.. '</td></tr>'
                                            fermeda = ''
                                            memo = destinaziun 
                                            end
                                         else
                                            if cc==true then fermeda = fermeda..c
                                            else memo = memo..c 
                                            end
                                         end
                                     end
                                 end  
                              end
                           end
                       end
                       
                    end
                  end    
				end 
				
	    return true
	else
		return false
	end -- einde #claims>0

end

function p.main( frame )
	local rows = {}
	local args = extractArgs( frame )
	local id = validTextArg( args, 1, 'id', 'wikidata', 'entity' )
	local onlyLineService = validTextArg( args, 2, 'ligne', 'line', 'service' )
	linkback = validTextArg( args, 'linkback' )
	local ssource = validTextArg( args, 'showsource' )
	if ssource and ( ssource == '-' or ssource == 'non' or ssource == 'no' or ssource == 'false' ) then
		showsource = false
	end
	local title = validTextArg( args, 'titre', 'title', 3 ) or ''
    if id and title == '' then
        title = wikidata.formatEntity( id , { link = defaultLink } )
		if linkback ~= '-' and defaultLink ~= 'wikidata' then
			title = addLinkBack( title, id, 'P197' )
			linkback = '-'
		end
	elseif title == '-' then
		title = ''
	end
    
	ignoreCoordinate = validTextArg( args, 'ignore coordinate', 'ignore coordonnées' )
	showdebug = validTextArg( args, 'debug' ) -- pour les tests uniquement / for testing only
    
	rows[ #rows + 1 ] = '<table style="width:264px">'
    rows[ #rows + 1 ] = '<tr><td style="width:32px">Lijn</td><td style="width:116px">Richting</td><td style="width:116px">Volgend station</td></tr>'
	
    local found = p.Lijn_info( id, onlyLineService, rows ) 

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

return p