? weather-967646.patch
Index: weather.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/weather/weather.install,v
retrieving revision 1.36.2.20
diff -u -p -r1.36.2.20 weather.install
--- weather.install	23 Sep 2010 16:54:25 -0000	1.36.2.20
+++ weather.install	12 Nov 2010 02:57:55 -0000
@@ -27,7 +27,53 @@
  * Database installation of weather module.
  */
 
+/**
+ * Implementation of hook_requirements().
+ */
+function weather_requirements($phase) {
+  $requirements = array();
+  // Ensure translations don't break at install time
+  $t = get_t();
+
+  // Report info for status report
+  if ($phase == 'runtime') {
+    // See if any weather stations failed on download
+    $bad_icaos = variable_get('weather_bad_icaos', array());
+    if (!empty($bad_icaos)) {
+      $requirements['weather_download'] = array(
+        'title'     => $t('Weather'),
+        'severity'  => REQUIREMENT_ERROR,
+        'value'     => $t('The following weather stations failed to download the data correctly: %list', array('%list' => implode(', ', $bad_icaos))),
+      );
+    }
+    // See if download was not done by FTP
+    if (variable_get('weather_fetch', 'FTP') != 'FTP') {
+      $requirements['weather_download'] = array(
+        'title'     => $t('Weather'),
+        'severity'  => REQUIREMENT_WARNING,
+        'value'     => $t('Last download of weather data did not use FTP. HTTP will be slower.'),
+      );
+    }
+
+    // Nothing wrong, report all OK
+    if (empty($requirements)) {
+      $data = db_result(db_query("SELECT next_update_on FROM {weather} ORDER BY next_update_on ASC LIMIT 0, 1"));
+      if (is_numeric($data)) {
+        $time = format_interval(max(0, $data - time()));
+      }
+      else {
+        $time = t('0 sec');
+      }
+      $requirements['weather'] = array(
+        'title'       => $t('Weather'),
+        'severity'    => REQUIREMENT_OK,
+        'value'       => $t('Next weather update will happen after %time has elasped when the next cron run will run.', array('%time' => $time)),
+      );
+    }
+  }
 
+  return $requirements;
+}
 
 /**
  * Implementation of hook_install().
@@ -50,6 +96,7 @@ function weather_uninstall() {
   variable_del('weather_fetch');
   variable_del('weather_use_cron');
   variable_del('weather_image_directory');
+  variable_del('weather_bad_icaos');
 
   // Remove the database schema
   drupal_uninstall_schema('weather');
@@ -532,7 +579,7 @@ function weather_update_6502() {
   $sql = "INSERT INTO {weather_icao} (icao, country, name, latitude, longitude)
     VALUES ('LRSV', 'Romania', 'Suceava', 47.687500, 26.354056)";
   $ret[] = update_sql($sql);
-  
+
   // Insert new city
   $sql = "INSERT INTO {weather_icao} (icao, country, name, latitude, longitude)
     VALUES ('LIRI', 'Italy', 'Salerno', 40.620400, 14.911294)";
Index: weather.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/weather/weather.module,v
retrieving revision 1.205.2.22
diff -u -p -r1.205.2.22 weather.module
--- weather.module	24 Aug 2010 15:20:35 -0000	1.205.2.22
+++ weather.module	12 Nov 2010 02:57:55 -0000
@@ -191,14 +191,32 @@ function weather_help($path, $arg) {
  * check for system weather blocks.
  */
 function weather_cron() {
+  // Update weather data
+  $bad_icaos = array();
+  $result = db_query("SELECT * FROM {weather} WHERE next_update_on <= %d", time());
+  while ($row = db_fetch_array($result)) {
+    $icao = $row['icao'];
+    $metar_raw = _weather_retrieve_data($icao);
+    if ($metar_raw) {
+      $metar = weather_parse_metar($metar_raw);
+      weather_store_metar($metar);
+    }
+    else {
+      $bad_icaos[$icao] = $icao;
+    }
+  }
+  variable_set('weather_bad_icaos', $bad_icaos);
+
   if (variable_get('weather_use_cron', FALSE)) {
-    $sql = "SELECT * FROM {weather} LEFT JOIN {weather_config}
-      ON {weather}.icao={weather_config}.icao
-      WHERE {weather_config}.uid < 0 ORDER BY next_update_on ASC";
-    $result = db_query($sql);
-    $row = db_fetch_array($result);
-    if (isset($row['next_update_on'])) {
-      if ($row['next_update_on'] <= time()) {
+    $result = db_query("
+      SELECT next_update_on
+      FROM {weather} AS w
+      LEFT JOIN {weather_config} AS wc using (icao)
+      WHERE wc.uid < 0
+      ORDER BY w.next_update_on ASC");
+    if ($result) {
+      $next_update_on = db_result($result);
+      if ($next_update_on <= time()) {
         cache_clear_all();
       }
     }
@@ -1983,43 +2001,28 @@ function weather_get_icao_from_lat_lon($
   return $station;
 }
 
-
-
 /**
- * Fetches the latest METAR data from the database or internet
+ * Gets the latest METAR data.
+ *
+ * Will get data from internet if new entry
  */
 function weather_get_metar($icao) {
   // see if there's a report in the database
   $icao = strtoupper($icao);
+  $metar = FALSE;
 
   $sql = "SELECT * FROM {weather} WHERE icao='%s'";
   $result = db_query($sql, $icao);
   $data = db_fetch_array($result);
 
-  // if there is no report, initialize the array
-  if (!isset($data['metar_raw']) or !isset($data['next_update_on'])) {
+  // if there is no report, initialize the array & download report
+  if (empty($data['metar_raw'])) {
     $data['next_update_on'] = 0;
-    $data['metar_raw'] = '';
-  }
-
-  // if the time has come, download again
-  if ($data['next_update_on'] <= time()) {
-    $data['metar_raw'] = '';
-  }
-
-  // fetch data from the internet
-  if ($data['metar_raw'] == '') {
     $data['metar_raw'] = _weather_retrieve_data($icao);
     if ($data['metar_raw']) {
       $metar = weather_parse_metar($data['metar_raw']);
       weather_store_metar($metar);
     }
-    else {
-      // the internet retrieval has not been successful.
-      // try again in 10 minutes
-      $sql = "UPDATE {weather} SET next_update_on=%d WHERE icao='%s'";
-      db_query($sql, time() + 10*60, $icao);
-    }
   }
   else {
     $metar = weather_parse_metar($data['metar_raw']);
@@ -2044,20 +2047,20 @@ function weather_store_metar($metar) {
     (icao, next_update_on, metar_raw)
     VALUES ('%s', %d, '%s')";
 
-  // calculate the next scheduled update: normally, we use 62
+  // calculate the next scheduled update: normally, we use 30
   // minutes after the reported timestamp, to allow the data
   // to propagate to the server servers.
-  $next_update_on = $metar['reported_on'] + 62*60;
+  $next_update_on = $metar['reported_on'] + 30*60;
 
-  // However, if the current time is more than 62 minutes
+  // However, if the current time is more than 30 minutes
   // over the reported timestamp, allow ten more minutes
-  // to not fetch the data on each page request.
+  // to not fetch the data on cron runs.
   if ($next_update_on < time()) {
     $next_update_on = time() + 10*60;
   }
 
   db_query($sql, $metar['icao'],
-    // make the next update roughly one hour after the report,
+    // make the next update roughly 30 minutes after the report,
     // to allow the data to propagate
     $next_update_on,
     $metar['#raw']);
@@ -2121,8 +2124,7 @@ function _weather_retrieve_data($icao) {
   // check on errors
   if ($metar_raw === FALSE) {
     // Make an entry about this error into the watchdog table.
-    watchdog('content', 'Download location for METAR data is not accessible.',
-      array(), WATCHDOG_ERROR);
+    watchdog('weather', 'Download location for METAR data is not accessible.', array(), WATCHDOG_ERROR);
     // Reset the fetch method to try again FTP if HTTP didn't work either
     variable_set('weather_fetch', 'FTP');
     // Show a message to users with administration priviledges
