/** * Update the geoplace reference for this image * * @since Version 3.9.1 * @return void */ public function updateGeoPlace() { if (!filter_var($this->lat, FILTER_VALIDATE_FLOAT) || !filter_var($this->lat, FILTER_VALIDATE_FLOAT)) { return; } $timer = microtime(true); $GeoPlaceID = PlaceUtility::findGeoPlaceID($this->lat, $this->lon); #var_dump($GeoPlaceID);die; $data = ["geoplace" => $GeoPlaceID]; $where = ["id = ?" => $this->id]; $this->db->update("image", $data, $where); $this->Memcached->delete($this->mckey); $this->Redis->delete($this->mckey); Debug::logEvent(__METHOD__, $timer); Debug::LogCLI(__METHOD__, $timer); return; }
/** * Update the geoplace linked to this location * @since Version 3.9.1 * @return void */ private function updateGeoplace() { $id = PlaceUtility::findGeoPlaceID($this->lat, $this->lon); if ($id != $this->geoplace_id) { $data = ["geoplace" => $id]; $where = ["id = ?" => $this->id]; $this->db->update("location", $data, $where); $this->Memcached->delete($this->mckey); } return; }
/** * Get weather forecast for this place * @todo Fix the weather forecasting, since openweathermap.org has started requiring API keys * @since Version 3.8.7 * @return array * @param int $days */ public function getWeatherForecast($days = 14) { return false; $weather = false; /** * Check if we've been given a DateTime object (a date) or a date range (eg 14 days) to work wtih */ $datekey = $days instanceof DateTime ? $days->format("Y-m-d") : $days; /** * Try to get the weather from Memcached first */ $mckey = md5(sprintf("railpage:lat=%s;lon=%s;weather;days=%s", $this->lat, $this->lon, $datekey)); if ($weather = $this->Redis->fetch($mckey)) { return $weather; } /** * Check the database before we try to fetch it from the weather API */ $GeoplaceID = PlaceUtility::findGeoPlaceID($this->lat, $this->lon); if ($days instanceof DateTime) { $query = "SELECT date, min, max, weather, icon FROM geoplace_forecast WHERE date = ? AND geoplace = ?"; $params = [$days->format("Y-m-d"), $GeoplaceID]; } else { $query = "SELECT date, min, max, weather, icon FROM geoplace_forecast WHERE date >= ? AND geoplace = ? LIMIT 0, ?"; $params = array(date("Y-m-d"), $GeoplaceID, $days); } if ($result = $this->db->fetchAll($query, $params)) { $weather = array(); foreach ($result as $row) { $weather[$row['date']]['forecast'] = array("min" => $row['min'], "max" => $row['max'], "weather" => array("title" => $row['weather'], "icon" => $row['icon'])); } return $weather; } /** * Didn't find the weather cached in memory or database, so let's look it up... */ /** * Restrict our maximum date range to 14 days */ if (is_int($days) && $days > 14) { $days = 14; } if ($days instanceof DateTime) { $Date = $days; $url = "http://api.openweathermap.org/data/2.5/forecast/daily?lat=" . $this->lat . "&lon=" . $this->lon . "&units=metric&cnt=14"; $Now = new DateTime(); $diff = $Now->diff($Date); if ($diff->format("%R") != "+" || $diff->format("%a") > 14) { return $weather; } } else { $url = "http://api.openweathermap.org/data/2.5/forecast/daily?lat=" . $this->lat . "&lon=" . $this->lon . "&units=metric&cnt=" . $days; } /** * Try to get the weather forecast from openweathermap */ //try { $response = $this->GuzzleClient->get($url); //} catch (\GuzzleHTTP\RequestException $e) { // return false; //} catch (Exception $e) { // return false; //} if ($response->getStatusCode() == 200) { $forecast = json_decode($response->getBody(), true); } if (is_array($forecast)) { $weather = array(); } foreach ($forecast['list'] as $row) { $ForecastDate = new DateTime("@" . $row['dt']); $weather[$ForecastDate->format("Y-m-d")]['forecast'] = array("min" => round($row['temp']['min']), "max" => round($row['temp']['max']), "weather" => array("title" => $row['weather'][0]['main'], "icon" => function_exists("getWeatherIcon") ? getWeatherIcon($row['weather'][0]['description']) : "")); $data = ["geoplace" => $GeoplaceID, "expires" => date("Y-m-d H:i:s", strtotime("+24 hours")), "date" => $ForecastDate->format("Y-m-d"), "min" => round($row['temp']['min']), "max" => round($row['temp']['max']), "weather" => $row['weather'][0]['main'], "icon" => $weather[$ForecastDate->format("Y-m-d")]['forecast']['weather']['icon']]; $this->db->insert("geoplace_forecast", $data); } if (isset($Date) && $Date instanceof DateTime) { $this->Redis->save($mckey, $weather[$Date->format("Y-m-d")], strtotime("+24 hours")); return $weather[$Date->format("Y-m-d")]; } $this->Redis->save($mckey, $weather, strtotime("+24 hours")); return $weather; }