You are here

function _yr_verdata_refresh_xml in Yr Weatherdata 6.2

Same name and namespace in other branches
  1. 7 yr_verdata.module \_yr_verdata_refresh_xml()

Function for updating an xml file from yr.no on cron run.

Parameters

$location: A location object from the {yr_verdata} table.

3 calls to _yr_verdata_refresh_xml()
yr_verdata_add_form_submit in ./yr_verdata.admin.inc
Submit handler for yr_verdata_add_form().
yr_verdata_cron in ./yr_verdata.module
Implementation of hook_cron().
yr_verdata_generate in ./yr_verdata.module
Function for generating a forecast for a location. This function should only be called after having checked if there is an up-to-date cache available, as this will load and parse an xml-file, possibly getting it from a remote host first, which is…

File

./yr_verdata.module, line 1079
yr_verdata.module This file contains the code for getting the forecast from yr.no and displaying it on a Drupal site.

Code

function _yr_verdata_refresh_xml($location) {

  // Initiate our local file.
  $destination = _yr_verdata_local_file($location);

  // Set the nextupdate timestamp to 0, in case this is the first time
  // we are getting the forecast for this location. Otherwise, see if the
  // "nextupdate" property of our existing file has passed.
  $nextupdate = 0;
  if (file_exists($destination)) {
    $xml = simplexml_load_file($destination);
    $nextupdate = strtotime($xml->meta->nextupdate);
  }

  // We don't connect to yr for the file unless it has been at least 12 minutes since the last time we did.
  $cooldown = !empty($location->updated) ? time() - $location->updated : 721;

  // To avoid a PHP notice.
  if ($nextupdate <= time() && $cooldown > 720) {

    // If this is TRUE...
    // ...yr.no have updated their data, so we can get an updated xml feed from yr.no.
    if (variable_get('yr_verdata_debug', 0) == 1) {
      watchdog('yr_verdata', '@location needs updating, attempting to do so.', array(
        '@location' => $location->name,
      ), WATCHDOG_DEBUG);
    }
    $data = _yr_verdata_fetch_xml($location);
    if ($data != FALSE) {

      // If the data was valid, save it. Otherwise, it will be re-checked on next cron run.
      if (variable_get('yr_verdata_debug', 0) == 1) {
        watchdog('yr_verdata', 'A file was saved for @location.', array(
          '@location' => $location->name,
        ), WATCHDOG_DEBUG);
      }
      file_save_data($data, $destination, FILE_EXISTS_REPLACE);
    }

    // Update the database entry with the request time in the 'updated' field.
    db_query("UPDATE {yr_verdata} SET updated = %d WHERE yid = %d", time(), $location->yid);
  }
  else {
    if (variable_get('yr_verdata_debug', 0) == 1) {
      watchdog('yr_verdata', '@location xml file was up-to-date. Only local HTML output needs updating.', array(
        '@location' => $location->name,
      ), WATCHDOG_DEBUG);
    }
  }
}