Try to avoid bots in analytics and improve page titles
Try to avoid bots in analytics and improve page titles

file:b/.box (new)
--- /dev/null
+++ b/.box
@@ -1,1 +1,5 @@
+shared_writable_dirs:
+    - /labs/tiles
+    - /lib/staticmaplite/cache
+php_extensions: [pgsql, pdo, pdo_pgsql, curl]
 

--- a/include/common-template.inc.php
+++ b/include/common-template.inc.php
@@ -32,7 +32,7 @@
 	<head>
         <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1"> 	
-<title>' . $pageTitle . '</title>
+<title>' . $pageTitle . ' - Canberra Bus Timetable</title>
         <meta name="google-site-verification" content="-53T5Qn4TB_de1NyfR_ZZkEVdUNcNFSaYKSFkWKx-sY" />
 <link rel="dns-prefetch" href="//code.jquery.com">
 <link rel="dns-prefetch" href="//ajax.googleapis.com">

--- a/include/common.inc.php
+++ b/include/common.inc.php
@@ -31,7 +31,8 @@
 
 function isDebugServer()
 {
-	return !isset($_SERVER['SERVER_NAME']) || $_SERVER['SERVER_NAME'] == "10.0.1.154" || $_SERVER['SERVER_NAME'] == "10.1.0.4" || $_SERVER['SERVER_NAME'] == "localhost" || $_SERVER['SERVER_NAME'] == "127.0.0.1" ;
+	return isset($_SERVER['SERVER_NAME']) && ( $_SERVER['SERVER_NAME'] == "10.0.1.154" || $_SERVER['SERVER_NAME'] == "10.1.0.4" || $_SERVER['SERVER_NAME'] == 
+"localhost" || $_SERVER['SERVER_NAME'] == "127.0.0.1") ;
 }
 
 include_once ("common-geo.inc.php");
@@ -46,7 +47,9 @@
 
 function isAnalyticsOn()
 {
-	return !isDebugServer();
+ $user_agent = $_SERVER['HTTP_USER_AGENT'];
+	return !isDebugServer() && !preg_match('/cloudkick/i', $user_agent) && !preg_match('/googlebot/i', $user_agent) && 
+!preg_match('/baidu/i', $user_agent);
 }
 function isDebug($debugReason = "other")
 {

--- a/include/db/trip-dao.inc.php
+++ b/include/db/trip-dao.inc.php
@@ -173,6 +173,22 @@
 	$r = $query->fetch(PDO::FETCH_ASSOC);
 	return $r['arrival_time'];
 }
+function getTripEndTime($tripID)
+{
+	global $conn;
+	$query = "SELECT trip_id,max(arrival_time) as arrival_time from stop_times
+	WHERE stop_times.arrival_time IS NOT NULL and trip_id = :tripID group by trip_id";
+	debug($query, "database");
+	$query = $conn->prepare($query);
+	$query->bindParam(":tripID", $tripID);
+	$query->execute();
+	if (!$query) {
+		databaseError($conn->errorInfo());
+		return Array();
+	}
+	$r = $query->fetch(PDO::FETCH_ASSOC);
+	return $r['arrival_time'];
+}
 function getActiveTrips($time)
 {
 	global $conn;

--- a/labs/busstopdensity.tile.php
+++ b/labs/busstopdensity.tile.php
@@ -15,8 +15,8 @@
 	set_time_limit(120);//2mn
 	ini_set('memory_limit', '256M');
 error_reporting(E_ALL ^ E_DEPRECATED);
-	require_once ($labsPath . 'lib/GoogleMapUtility.php');
-	require_once ($labsPath . 'lib/HeatMap.php');
+	require_once ('lib/GoogleMapUtility.php');
+	require_once ('lib/HeatMap.php');
 
 	//Root folder to store generated tiles
 	define('TILE_DIR', 'tiles/');
@@ -103,14 +103,13 @@
             global $conn;
 		$nbPOIInsideTile = 0;
 
+	$result = pg_query($conn, $query);
         $spots = Array();
-	$query = $conn->prepare($query);
-	$query->execute();
-	if (!$query) {
-		databaseError($conn->errorInfo());
+	if (!$result) {
+		databaseError(pg_result_error($result));
 		return Array();
 	}
-	foreach( $query->fetchAll() as $row){
+	foreach( pg_fetch_all($result) as $row){
 				$point = GoogleMapUtility::getOffsetPixelCoords($row['stop_lat'], $row['stop_lon'], $zoom, $X, $Y);
 				//Count result only in the tile
 				if( ($point->x > -$offset) && ($point->x < (GoogleMapUtility::TILE_SIZE+$offset)) && ($point->y > -$offset) && ($point->y < (GoogleMapUtility::TILE_SIZE+$offset))){

--- a/labs/myway_timeliness_calculate.php
+++ b/labs/myway_timeliness_calculate.php
@@ -1,6 +1,8 @@
 <?php
 include ('../include/common.inc.php');
 include_header("MyWay Delta Calculate", "mywayDeltaCalc");
+flush();
+ob_flush();
 function abssort($a, $b)
 {
 	if ($a['timeDiff'] == $b['timeDiff']) {
@@ -40,8 +42,10 @@
 		echo "error, route '{$obsv['myway_route']}' unknown";
 		continue;
 	}
-	//		:convert timestamp into time of day and date
+	// convert timestamp into time of day and date
+// timezones from http://www.postgresql.org/docs/8.0/static/datetime-keywords.html
 	$time = date("H:i:s", strtotime($obsv['time']));
+    $time_tz = date("H:i:s", strtotime($obsv['time']))." AESST";
         $search_time = date("H:i:s", strtotime($obsv['time'])-(30*60)); // 30 minutes margin
 	$date = date("c", strtotime($obsv['time']));
 	$timing_period = service_period(strtotime($date));
@@ -102,6 +106,8 @@
 			//print out that stops/does not stop
 			echo "No matching routes found at {$potentialStop['stop_code']}<br>";
                         var_dump($stopRoutes);
+                        			flush();
+
 		}
 	}
 	//   lowest delta is recorded delta
@@ -119,7 +125,7 @@
 		$stmt->bindParam(':route_full_name', $route_full_name);
 		$stmt->bindParam(':stop_code', $stop_code);
 		$stmt->bindParam(':timing_delta', $lowestDelta);
-		$stmt->bindParam(':time', $time);
+		$stmt->bindParam(':time', $time_tz);
 		$stmt->bindParam(':date', $date);
 		$stmt->bindParam(':timing_period', $timing_period);
                 $stmt->bindParam(':stop_sequence', $stop_sequence);
@@ -129,6 +135,7 @@
 			echo "Recorded.<br>";
 		}
 		var_dump($conn->errorInfo());
+			flush();
 	}
 	flush();
 }

--- a/labs/myway_timeliness_route.php
+++ b/labs/myway_timeliness_route.php
@@ -34,6 +34,10 @@
                 tickFormatter: yformatter
             },
             grid: { hoverable: true, clickable: true, labelMargin: 32   },
+series: {
+      lines: { show: false },
+      points: { show: true }
+    }
     };
     
     var plot = $.plot(placeholder, data, options);
@@ -118,3 +122,4 @@
     });
 
 </script> 
+

--- a/labs/myway_timeliness_stop.json.php
+++ b/labs/myway_timeliness_stop.json.php
@@ -29,3 +29,4 @@
     else echo "[".implode(",",$points)."]";
 ?>
 }
+

--- a/labs/myway_timeliness_stop.php
+++ b/labs/myway_timeliness_stop.php
@@ -40,6 +40,10 @@
                 tickFormatter: yformatter
             },
             grid: { hoverable: true, clickable: true, labelMargin: 32   },
+ series: {
+      lines: { show: false },
+      points: { show: true }
+    }
     };
 
     var plot = $.plot(placeholder, data, options);
@@ -129,3 +133,4 @@
     });
 
 </script> 
+

--- /dev/null
+++ b/labs/travelAllRoutes.php
@@ -1,1 +1,23 @@
+<?php
+include ('../include/common.inc.php');
+	$query = "Select route_short_name,max(route_id) as route_id from routes where route_short_name NOT LIKE '7__'  AND route_short_name != '170' AND route_short_name NOT LIKE '9__' group by route_short_name order by route_short_name ;";
+	debug($query, "database");
+	$query = $conn->prepare($query);
+	$query->execute();
+echo "<table><tr><th>Route Number</th><th>First Trip Start</th><th>First Trip End</th><th>Length</th>";
+$total = 0;
+$count = 0;
+foreach($query->fetchAll() as $r) {
+        $trips = getRouteTrips($r['route_id']);
+    $startTime = $trips[0]['arrival_time'];
+    $endTime = getTripEndTime($trips[0]['trip_id']);
+    $timeDiff = strtotime($endTime) - strtotime($startTime);
+    $total += $timeDiff;
+    $count ++;
+    echo "<tr><td>{$r['route_short_name']}</td><td>$startTime</td><td>$endTime</td><td>$timeDiff seconds ie. ". ($timeDiff/60). " minutes</td></tr>";
 
+}
+echo "</table>";
+echo "Total time: $total seconds ie. " .($total/60/60). " hours<br>";
+echo "$count Routes";
+?>