You are here

function feed_import_cron in Feed Import 7.2

Same name and namespace in other branches
  1. 7 feed_import.module \feed_import_cron()

Implements hook_cron().

File

./feed_import.module, line 347
User interface, cron functions for feed_import module

Code

function feed_import_cron() {

  // Check if cron import is enabled.
  if (variable_get('feed_import_use_cron', FALSE)) {

    // Check if there is an already running import or there are no feeds because
    // overlapping an import for the same entity is bad.
    if (!variable_get('feed_import_import_running', FALSE) || variable_get('feed_import_let_overlap', FALSE)) {
      $can_import = FALSE;
      $time_settings = variable_get('feed_import_time_settings', 0);
      if ($time_settings == 1) {

        // Specified interval.
        $time1 = $time2 = 0;
        list($h, $m) = explode(':', variable_get('feed_import_interval_start', '00:00'));
        $time1 = mktime($h, $m, 0);
        list($h, $m) = explode(':', variable_get('feed_import_interval_stop', '00:00'));
        $time2 = mktime($h, $m, 0);
        if ($time1 < $time2) {
          $can_import = $time1 <= REQUEST_TIME && REQUEST_TIME <= $time2;
        }
        unset($time1, $time2, $h, $m);
      }
      else {
        $last_executed = variable_get('feed_import_last_executed_import', 0);
        $time_between = variable_get('feed_import_time_between_imports', 3600);
        $can_import = $last_executed + $time_between < REQUEST_TIME;
      }

      // Check if we can import.
      if ($can_import) {
        $feeds = FeedImport::loadFeeds(TRUE);
        if (!empty($feeds)) {
          $feed_names = array_keys($feeds);
          $last_feed = variable_get('feed_import_last_imported_feed', '');
          if ($last_feed == '') {
            $last_feed = 0;
          }
          else {
            $last_feed = (array_search($last_feed, $feed_names) + 1) % count($feed_names);
          }
          $last_feed = $feed_names[$last_feed];
          $feeds = $feeds[$last_feed];
          variable_set('feed_import_last_imported_feed', $last_feed);
          variable_set('feed_import_last_executed_import', REQUEST_TIME);

          // Mark import as running.
          variable_set('feed_import_import_running', TRUE);

          // Process feed.
          feed_import_import_items($feeds);

          // Change running status.
          variable_set('feed_import_import_running', FALSE);
          unset($feeds, $feed_names, $last_feed);
        }
      }
    }
  }

  // Delete expired items.
  $ids = FeedImport::getExpiredItems(variable_get('feed_import_delete_items_per_cron', 300));
  feed_import_delete_items($ids);
  unset($ids);
}