diff --git a/class_weather.php b/class_weather.php index 5d716ce..97a0e91 100644 --- a/class_weather.php +++ b/class_weather.php @@ -17,10 +17,19 @@ class Weather { . "&appid=" . WEATHERAPIKEY . "&units=" . WEATHERUNITS; - public static function fetchWeather() { + public static function deleteOldWeather($days = 10) { + if ( !is_int($days) ) return false; + if ( $days < 0 ) return false; + $query = "DELETE FROM " . AppDB::TABLE_WEATHER . " WHERE DATEDIFF(NOW(), sampledate) > :days"; + $sth = AppDB::getSTH($query); + if ( $sth === false ) return false; + $sth->bindValue(":days", $days, PDO::PARAM_INT); + $sth->execute(); + } + public static function fetchWeather() { $weather = file_get_contents(Weather::WEATHERURL); - $query = "UPDATE " . AppDB::TABLE_WEATHER . " SET weather=:weather"; + $query = "INSERT INTO " . AppDB::TABLE_WEATHER . " (sampledate, weather) VALUES(NOW(), :weather)"; $sth = AppDB::getSTH($query); if ( $sth === false ) return false; $sth->bindValue(":weather", $weather, PDO::PARAM_STR); @@ -28,7 +37,7 @@ class Weather { } public static function getWeather() { - $query = "SELECT weather FROM " . AppDB::TABLE_WEATHER . " LIMIT 1"; + $query = "SELECT weather FROM " . AppDB::TABLE_WEATHER . " ORDER BY sampledate DESC LIMIT 1"; $sth = AppDB::getSTH($query); if ( $sth === false ) return false; $sth->execute(); diff --git a/install/hometasks.sql b/install/hometasks.sql index 085b9be..8414192 100644 --- a/install/hometasks.sql +++ b/install/hometasks.sql @@ -42,7 +42,9 @@ DROP TABLE IF EXISTS `weather`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `weather` ( - `weather` text NOT NULL + `sampledate` datetime NOT NULL, + `weather` text NOT NULL, + PRIMARY KEY(`sampledate`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC; /*!40101 SET character_set_client = @saved_cs_client */; /*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */; diff --git a/weather.php b/weather.php index 30d8492..3944131 100644 --- a/weather.php +++ b/weather.php @@ -10,5 +10,6 @@ require_once("authfunctions.php"); if ( php_sapi_name() != "cli" ) exit(); $fetched = Weather::fetchWeather(); +$deleted = Weather::deleteOldWeather(); exit();