/api/inc/models/db.php

https://github.com/vasile/transit-map · PHP · 148 lines · 120 code · 28 blank · 0 comment · 19 complexity · dba0544da0187a3eb6d1b050fa4eb4f9 MD5 · raw file

  1. <?php
  2. class DB {
  3. private static $db = null;
  4. private static $use_file_cache = false;
  5. private static function getDB() {
  6. if (is_null(self::$db)) {
  7. $db_path = APP_FOLDER_PATH . '/gtfs-data/gtfs.db';
  8. $db = new SQLite3($db_path, SQLITE3_OPEN_READONLY);
  9. self::$db = $db;
  10. }
  11. return self::$db;
  12. }
  13. private static function getCachedResults($params) {
  14. if (self::$use_file_cache === FALSE) {
  15. return null;
  16. }
  17. $params_default = array(
  18. 'cache_file' => null,
  19. 'ttl' => null,
  20. );
  21. $params = array_merge($params_default, $params);
  22. if (is_null($params['cache_file'])) {
  23. return null;
  24. }
  25. if (file_exists($params['cache_file'])) {
  26. $cache_file = $params['cache_file'];
  27. if (is_null($params['ttl'])) {
  28. return json_decode(file_get_contents($cache_file), 1);
  29. } else {
  30. $file_ts = file_ts($cache_file);
  31. $now_ts = time();
  32. if ($now_ts < ($file_ts + $params['ttl'])) {
  33. return json_decode(file_get_contents($cache_file), 1);
  34. }
  35. }
  36. }
  37. return null;
  38. }
  39. private static function parseDBResult($result, $cache_file = null) {
  40. $rows = array();
  41. while($row = $result->fetchArray(SQLITE3_ASSOC)) {
  42. array_push($rows, $row);
  43. }
  44. if ($cache_file) {
  45. self::cacheResults($cache_file, $rows);
  46. }
  47. return $rows;
  48. }
  49. private static function checkIfWritable($file) {
  50. if (file_exists($file)) {
  51. return is_writable($file);
  52. } else {
  53. return is_writable(dirname($file));
  54. }
  55. return false;
  56. }
  57. private static function cacheResults($file, $content) {
  58. if (self::$use_file_cache === FALSE) {
  59. return null;
  60. }
  61. if (self::checkIfWritable($file)) {
  62. file_put_contents($file, json_encode($content));
  63. } else {
  64. error_log('DB::cacheResults - no write rights for ' . $file);
  65. }
  66. }
  67. public static function getCalendarRows() {
  68. $cache_file = APP_FOLDER_PATH . '/tmp/cache/db/calendar.json';
  69. $cached_results = self::getCachedResults(array(
  70. 'cache_file' => $cache_file
  71. ));
  72. if ($cached_results) {
  73. return $cached_results;
  74. }
  75. $db = self::getDB();
  76. $result = $db->query('SELECT * FROM calendar');
  77. $rows = self::parseDBResult($result, $cache_file);
  78. return $rows;
  79. }
  80. public static function getTripsByMinute($hhmm, $service_ids) {
  81. $cache_file = APP_FOLDER_PATH . '/tmp/cache/db/trips_' . sha1(implode('', $service_ids)) . '_' . $hhmm . '.json';
  82. $cached_results = self::getCachedResults(array(
  83. 'cache_file' => $cache_file
  84. ));
  85. if ($cached_results) {
  86. return $cached_results;
  87. }
  88. $db = self::getDB();
  89. $hhmm_seconds = substr($hhmm, 0, 2) * 3600 + substr($hhmm, 2) * 60;
  90. $hhmm_seconds_midnight = $hhmm_seconds + 24 * 3600;
  91. $sql = "SELECT trip_id, route_short_name, route_long_name, route_color, route_text_color, trip_headsign, shape_id, service_id FROM trips, routes WHERE trips.route_id = routes.route_id AND ((trip_start_seconds < " . $hhmm_seconds . " AND trip_end_seconds > " . $hhmm_seconds . ") OR (trip_start_seconds < " . $hhmm_seconds_midnight . " AND trip_end_seconds > " . $hhmm_seconds_midnight . "))";
  92. $stmt = $db->prepare($sql);
  93. $result = $stmt->execute();
  94. $rows = array();
  95. while($row = $result->fetchArray(SQLITE3_ASSOC)) {
  96. if (empty($service_ids) || in_array($row['service_id'], $service_ids)) {
  97. array_push($rows, $row);
  98. }
  99. }
  100. if ($cache_file) {
  101. self::cacheResults($cache_file, $rows);
  102. }
  103. return $rows;
  104. }
  105. public static function getStopsByTripId($trip_id) {
  106. $cache_file = APP_FOLDER_PATH . '/tmp/cache/db/trip_' . $trip_id . '.json';
  107. $cached_results = self::getCachedResults(array(
  108. 'cache_file' => $cache_file
  109. ));
  110. if ($cached_results) {
  111. return $cached_results;
  112. }
  113. $db = self::getDB();
  114. $stmt = $db->prepare('SELECT stops.stop_id, stops.stop_name, arrival_time, departure_time, stop_shape_percent FROM stop_times, stops WHERE stops.stop_id = stop_times.stop_id AND stop_times.trip_id = :trip_id ORDER BY stop_sequence;');
  115. $stmt->bindValue(':trip_id', $trip_id, SQLITE3_TEXT);
  116. $result = $stmt->execute();
  117. $rows = self::parseDBResult($result, $cache_file);
  118. return $rows;
  119. }
  120. }