? 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	13 Nov 2010 03:24:36 -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	13 Nov 2010 03:24:37 -0000
@@ -37,9 +37,28 @@
 
 
 
-// Define the start of block deltas for system-wide blocks
+/**
+ * Define the start of block deltas for system-wide blocks.
+ */
 define('SYSTEM_BLOCK_DELTA_START', 3);
 
+/**
+ * Indicates that weather data can be downloaded on a page load.
+ */
+define('WEATHER_UPDATE_ON_PAGE', 0);
+
+/**
+ * Indicates that weather data will only be downloaded when cron runs.
+ */
+define('WEATHER_UPDATE_ON_CRON', 1);
+
+/**
+ * Indicates that weather data will  be downloaded when cron runs or on a page
+ * load depending on the time of the last update.
+ */
+define('WEATHER_UPDATE_ON_PAGE_AND_CRON', 2);
+
+
 // Include the parser for METAR data
 require_once drupal_get_path('module', 'weather') .'/weather_parser.inc';
 
@@ -191,14 +210,35 @@ function weather_help($path, $arg) {
  * check for system weather blocks.
  */
 function weather_cron() {
+  // Update weather data on cron
+  $update = variable_get('weather_update', WEATHER_UPDATE_ON_PAGE);
+  if ($update != WEATHER_UPDATE_ON_PAGE) {
+    $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();
       }
     }
@@ -642,6 +682,17 @@ function weather_main_page_form() {
     '#description' => t('If you use Drupal\'s cache, the system weather blocks will not be updated for anonymous users unless the cache is cleared. This happens e.g. when new nodes are created. If you want the system weather blocks to be updated when new weather data is available, you can clear the cache once per hour. Please note that this might slow down your site.'),
     '#default_value' => variable_get('weather_use_cron', FALSE),
   );
+  $form['weather_update'] = array(
+    '#type' => 'radios',
+    '#title' => t('Weather Update Behavior'),
+    '#description' => t('Select how you wish the weather to be updated. If cron runs every hour updating only on cron is recommended.'),
+    '#default_value' => variable_get('weather_update', WEATHER_UPDATE_ON_PAGE),
+    '#options' => array(
+      WEATHER_UPDATE_ON_PAGE => t('Page load every hour (default)'),
+      WEATHER_UPDATE_ON_CRON => t('Only on cron'),
+      WEATHER_UPDATE_ON_PAGE_AND_CRON => t('On cron and on page load'),
+    ),
+  );
   $form['weather_image_directory'] = array(
     '#type' => 'textfield',
     '#title' => t('Directory for custom images'),
@@ -663,6 +714,8 @@ function weather_main_page_form() {
  */
 function weather_main_page_form_submit($form, &$form_state) {
   variable_set('weather_use_cron', $form_state['values']['use_cron']);
+  variable_set('weather_update', $form_state['values']['weather_update']);
+
   $directory = $form_state['values']['weather_image_directory'];
   // Remove whitespace and directory separators from the string.
   $directory = trim(trim($directory, '/\\'));
@@ -1983,32 +2036,34 @@ 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;
+  $update = variable_get('weather_update', WEATHER_UPDATE_ON_PAGE);
 
   $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 (!isset($data['metar_raw'])) {
     $data['next_update_on'] = 0;
     $data['metar_raw'] = '';
   }
 
   // if the time has come, download again
-  if ($data['next_update_on'] <= time()) {
+  if ($data['next_update_on'] <= time() && $update != WEATHER_UPDATE_ON_CRON) {
     $data['metar_raw'] = '';
   }
 
   // fetch data from the internet
-  if ($data['metar_raw'] == '') {
+  if (isset($data['metar_raw']) && $data['metar_raw'] == '') {
     $data['metar_raw'] = _weather_retrieve_data($icao);
     if ($data['metar_raw']) {
       $metar = weather_parse_metar($data['metar_raw']);
@@ -2121,8 +2176,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
