You are here

function tweet_feed_cron in Tweet Feed 7.2

Same name and namespace in other branches
  1. 8.3 tweet_feed.module \tweet_feed_cron()
  2. 6 tweet_feed.module \tweet_feed_cron()
  3. 7.3 tweet_feed.module \tweet_feed_cron()
  4. 7 tweet_feed.module \tweet_feed_cron()

Implements hook_cron().

When running on a cron, we are going to update one feed per cron run and keep track of which one was run. These will be run in a round robin order in the order which they were created (in ascending order by ID).

File

./tweet_feed.module, line 156

Code

function tweet_feed_cron() {

  // Get a list of all the available feeds
  $feed = array();
  $result = db_select('tweet_feeds', 'f')
    ->fields('f', array(
    'fid',
  ))
    ->orderBy('fid', 'ASC')
    ->execute();
  while ($fdata = $result
    ->fetchObject()) {
    $feed[] = $fdata->fid;
  }

  // Determine the id of the last feed run
  $last_fid = variable_get('tweet_feed_cron_last_fpos', NULL);

  // If it is zero, or this is the first time being run, then start with the first one
  if ($last_fid === NULL) {
    $current_fid = 0;
  }
  else {
    $current_fid = $last_fid + 1;
    if ($current_fid > count($feed) - 1) {
      $current_fid = 0;
    }
  }

  // Set the last fid used in our variable for future use
  variable_set('tweet_feed_cron_last_fpos', $current_fid);
  variable_set('tweet_feed_cron_last_feed', $feed[$current_fid]);

  // Get all of the tweets to be imported
  $tweets = tweet_feed_pull_data_from_feed($feed[$current_fid], TRUE);

  // If we have no tweets, we can stop here.
  if (!is_array($tweets) || count($tweets) < 1) {
    return FALSE;
  }

  // Get our current tweet_feed_queue.
  $queue = drupalQueue::get('tweet_feed_queue');

  // If we have items left in it that were not processed, then do those first and
  // bail. Could mean we had a time out issue on the last run or some other error.
  $queue_size = $queue
    ->numberOfItems();
  if ($queue_size < 1) {

    // Nothing is in the queue, so we can begin populating it with more stuff
    foreach ($tweets as $key => $tweet) {

      // Initialize our update_node_id
      $update_node_id = 0;
      $hash = NULL;

      // find out if we already have this tweet, if we do, add the update primary key (pk).
      $result = db_select('tweet_hashes', 'h')
        ->fields('h', array(
        'nid',
        'tid',
        'hash',
      ))
        ->condition('h.tid', $tweet->id)
        ->execute();
      if ($result
        ->rowCount() > 0) {
        $tdata = $result
          ->fetchObject();
        $hash = md5(serialize($tweet->text));

        // If our hashes are equal, we have nothing to update and can move along.
        if ($hash == $tdata->hash) {
          continue;
        }
        else {
          $update_node_id = $tdata->nid;
        }
      }
      $queue
        ->createItem(array(
        'tweet' => $tweet,
        'feed' => $feed,
        'update_node_id' => $update_node_id,
        'hash' => $hash,
      ));
    }

    // Get the total number of items we have addedd to our queue
    $queue_size = $queue
      ->numberOfItems();
  }

  // Run through items in the queue
  for ($i = 0; $i < $queue_size; $i++) {
    $item = $queue
      ->claimItem($i);
    $feed_object = tweet_feed_get_feed_object($item->data['feed'][0]);
    tweet_feed_save_tweet($item->data['tweet'], $feed_object, $item->data['update_node_id'], $item->data['hash']);
    $queue
      ->releaseItem($item);
    $queue
      ->deleteItem($item);
  }
}