You are here

function _yr_verdata_refresh_xml in Yr Weatherdata 7

Same name and namespace in other branches
  1. 6.2 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.

2 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_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…
1 string reference to '_yr_verdata_refresh_xml'
yr_verdata_cron_queue_info in ./yr_verdata.module
Implementation of hook_cron_queue_info().

File

./yr_verdata.module, line 1141
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) ? REQUEST_TIME - $location->updated : 721;

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

    // If this is TRUE...
    // ...yr.no have updated their data, so we can get an updated xml feed from yr.no.
    $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.
      file_unmanaged_save_data($data, $destination, FILE_EXISTS_REPLACE);

      // Clear this location's caches.
      $cache['#cache']['keys'] = array(
        'yr_verdata',
        'page',
        'single',
        $location->yid,
      );
      $cid = drupal_render_cid_create($cache);
      cache_clear_all($cid, 'cache');
    }

    // Update the database entry with the request time in the 'updated' field.
    db_update('yr_verdata')
      ->fields(array(
      'updated' => REQUEST_TIME,
    ))
      ->condition('yid', $location->yid, '=')
      ->execute();
  }
}