You are here

function feed_import_cron in Feed Import 7

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

Implements hook_cron().

File

./feed_import.module, line 223
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)) {
      $last_executed = variable_get('feed_import_last_executed_import', 0);
      $time_between = variable_get('feed_import_time_between_imports', 3600);

      // Check if time between imports elapsed
      if ($last_executed + $time_between < REQUEST_TIME) {
        $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);
}