<commit_before><?php
/**
 * This script populates the following GTFS files: routes.txt, trips.txt, stop_times.txt
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @license MIT
 */
require("vendor/autoload.php");

use iRail\brail2gtfs\RouteFetcher;

$file_routes = "dist/routes.txt";
$file_trips = "dist/trips.txt";
$file_stop_times = "dist/stop_times.txt";
$file_temp = 'dist/calendar_dates_temp.txt';
$file_calendar_dates = "dist/calendar_dates.txt";

$language = "nn"; // Dutch
// $language = "fr"; // French
// $language = "en"; // English

// Returns hashmap. Key: route_id - Value: array of dates with a distinct service_id
function getRoutesWithDates() {
	$hashmap = array(); // holds route_short_name => array of dates

	if(($handle = fopen('dist/routes_info.tmp.txt', 'r')) !== false)
	{
	    // get the first row, which contains the column-titles (if necessary)
	    $header = fgetcsv($handle);

	    // loop through the file line-by-line
	    while(($line = fgetcsv($handle)) !== false)
	    {
			$route_short_name = $line[0]; // $line is an array of the csv elements
			$service_id = $line[1];
			$date = $line[2];
			
			if (!isset($hashmap[$route_short_name])) {
					$hashmap[$route_short_name] = array();
			}

			// Check if service_id has already been added
			if (!checkForServiceId($hashmap[$route_short_name], $service_id)) {
				$pair = array($date, $service_id);
				array_push($hashmap[$route_short_name], $pair);
			}

	        // I don't know if this is really necessary, but it couldn't harm;
	        // see also: http://php.net/manual/en/features.gc.php
	        unset($line);
	    }
	    fclose($handle);
	}

	return $hashmap;
}

function checkForServiceId($dateServiceIdPairs, $service_id) {
	$contains = false;

	foreach ($dateServiceIdPairs as $pair) {
		// $date = $pair[0];
		$id = $pair[1];
		// Already added
		if($service_id == $id) {
			$contains = true;
		}
	}

	return $contains;
}

// Some services don't drive so those have to be deleted from calendar_dates.txt
// To do this, we generate a temporary file where we put all the services that drive
function makeCorrectCalendarDates($serviceId_date_pairs) {
	global $file_temp, $file_calendar_dates;

	if(($handleRead = fopen($file_calendar_dates, 'r')) !== false && ($handleWrite = fopen($file_temp, 'w')) !== false)
	{
		// Header new calendar_dates.txt file
		$header = "service_id,date,exception_type";
		appendCSV($file_temp, $header);

	    // get the first row, which contains the column-titles (if necessary)
	    $header = fgetcsv($handleRead);

	    // loop through the file line-by-line
	    while(($line = fgetcsv($handleRead)) !== false)
	    {
    		// Not all pairs have been found
	    	if (count($serviceId_date_pairs) > 0) {
				$service_id_ = $line[0]; // $line is an array of the csv elements
				$date_ = $line[1];
				
				foreach ($serviceId_date_pairs as $service_id => $date) {
					if ($service_id == $service_id_ && $date == $date_) {
						// Delete from pairs
						unset($serviceId_date_pairs[$service_id]);
					} else {
						// Write to new temporary CSV-file
						fputcsv($handleWrite, $line);
					}
				}
			// Just write the rest
			} else {
				fputcsv($handleWrite, $line);
			}

	        // I don't know if this is really necessary, but it couldn't harm;
	        // see also: http://php.net/manual/en/features.gc.php
	        unset($line);
	    }
	    fclose($handleRead);
	    fclose($handleWrite);

	    // Delete old calendar_dates.txt
	    if (unset($file_calendar_dates)) {
	    	var_dump("Deleted calendar_dates.txt");
	    }

	    // Rename temporary file so we have a new calendar_dates.txt
	    rename($file_temp, $file_calendar_dates);
	    var_dump("New calendar_dates.txt is ready.");
	}
}

function generateTrip($shortName, $service_id, $trip_id) {
	$trip_entry = [
        "@id" => $trip_id, //Sadly, this is only a local identifier
        "@type" => "gtfs:Trip",
        "gtfs:route" => "http://irail.be/routes/NMBS/" . $shortName,
        "gtfs:service" => $service_id, //Sadly, this is only a local identifier, and we use the same id as the trip for service rules
    ];

    return $trip_entry;
}

function appendCSV($dist, $csv) {
	file_put_contents($dist, trim($csv).PHP_EOL, FILE_APPEND);
}

function addRoute($route_entry) {
	$csv = "";
	$csv .= $route_entry["@id"] . ","; // route_id
	$csv .= $route_entry["gtfs:agency"] . ","; // agency_id
	$csv .= $route_entry["gtfs:shortName"] . ","; // route_short_name
	$csv .= $route_entry["gtfs:longName"] . ","; // route_long_name
	$csv .= $route_entry["gtfs:routeType"]; // route_type<commit_msg>Change unset to unlink to delete file<commit_after><?php
/**
 * This script populates the following GTFS files: routes.txt, trips.txt, stop_times.txt
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @license MIT
 */
require("vendor/autoload.php");

use iRail\brail2gtfs\RouteFetcher;

$file_routes = "dist/routes.txt";
$file_trips = "dist/trips.txt";
$file_stop_times = "dist/stop_times.txt";
$file_temp = 'dist/calendar_dates_temp.txt';
$file_calendar_dates = "dist/calendar_dates.txt";

$language = "nn"; // Dutch
// $language = "fr"; // French
// $language = "en"; // English

// Returns hashmap. Key: route_id - Value: array of dates with a distinct service_id
function getRoutesWithDates() {
	$hashmap = array(); // holds route_short_name => array of dates

	if(($handle = fopen('dist/routes_info.tmp.txt', 'r')) !== false)
	{
	    // get the first row, which contains the column-titles (if necessary)
	    $header = fgetcsv($handle);

	    // loop through the file line-by-line
	    while(($line = fgetcsv($handle)) !== false)
	    {
			$route_short_name = $line[0]; // $line is an array of the csv elements
			$service_id = $line[1];
			$date = $line[2];
			
			if (!isset($hashmap[$route_short_name])) {
					$hashmap[$route_short_name] = array();
			}

			// Check if service_id has already been added
			if (!checkForServiceId($hashmap[$route_short_name], $service_id)) {
				$pair = array($date, $service_id);
				array_push($hashmap[$route_short_name], $pair);
			}

	        // I don't know if this is really necessary, but it couldn't harm;
	        // see also: http://php.net/manual/en/features.gc.php
	        unset($line);
	    }
	    fclose($handle);
	}

	return $hashmap;
}

function checkForServiceId($dateServiceIdPairs, $service_id) {
	$contains = false;

	foreach ($dateServiceIdPairs as $pair) {
		// $date = $pair[0];
		$id = $pair[1];
		// Already added
		if($service_id == $id) {
			$contains = true;
		}
	}

	return $contains;
}

// Some services don't drive so those have to be deleted from calendar_dates.txt
// To do this, we generate a temporary file where we put all the services that drive
function makeCorrectCalendarDates($serviceId_date_pairs) {
	global $file_temp, $file_calendar_dates;

	if(($handleRead = fopen($file_calendar_dates, 'r')) !== false && ($handleWrite = fopen($file_temp, 'w')) !== false)
	{
		// Header new calendar_dates.txt file
		$header = "service_id,date,exception_type";
		appendCSV($file_temp, $header);

	    // get the first row, which contains the column-titles (if necessary)
	    $header = fgetcsv($handleRead);

	    // loop through the file line-by-line
	    while(($line = fgetcsv($handleRead)) !== false)
	    {
    		// Not all pairs have been found
	    	if (count($serviceId_date_pairs) > 0) {
				$service_id_ = $line[0]; // $line is an array of the csv elements
				$date_ = $line[1];
				
				foreach ($serviceId_date_pairs as $service_id => $date) {
					if ($service_id == $service_id_ && $date == $date_) {
						// Delete from pairs
						unset($serviceId_date_pairs[$service_id]);
					} else {
						// Write to new temporary CSV-file
						fputcsv($handleWrite, $line);
					}
				}
			// Just write the rest
			} else {
				fputcsv($handleWrite, $line);
			}

	        // I don't know if this is really necessary, but it couldn't harm;
	        // see also: http://php.net/manual/en/features.gc.php
	        unset($line);
	    }
	    fclose($handleRead);
	    fclose($handleWrite);

	    // Delete old calendar_dates.txt
	    if (unlink($file_calendar_dates)) {
	    	var_dump("Deleted calendar_dates.txt");
	    }

	    // Rename temporary file so we have a new calendar_dates.txt
	    rename($file_temp, $file_calendar_dates);
	    var_dump("New calendar_dates.txt is ready.");
	}
}

function generateTrip($shortName, $service_id, $trip_id) {
	$trip_entry = [
        "@id" => $trip_id, //Sadly, this is only a local identifier
        "@type" => "gtfs:Trip",
        "gtfs:route" => "http://irail.be/routes/NMBS/" . $shortName,
        "gtfs:service" => $service_id, //Sadly, this is only a local identifier, and we use the same id as the trip for service rules
    ];

    return $trip_entry;
}

function appendCSV($dist, $csv) {
	file_put_contents($dist, trim($csv).PHP_EOL, FILE_APPEND);
}

function addRoute($route_entry) {
	$csv = "";
	$csv .= $route_entry["@id"] . ","; // route_id
	$csv .= $route_entry["gtfs:agency"] . ","; // agency_id
	$csv .= $route_entry["gtfs:shortName"] . ","; // route_short_name
	$csv .= $route_entry["gtfs:longName"] . ","; // route_long_name
	$csv .= $route_entry["gtfs:routeType"]; // route_type
{'fraction_non_alphanumeric': 0.15582729141867763, 'fraction_numerical': 0.0019478411427334705, 'mean_word_length': 3.3006049325267566, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3600044', 'n_tokens_mistral': 3412, 'n_tokens_neox': 3112, 'n_words': 773}