function tweet_feed_cron in Tweet Feed 7.3
Same name and namespace in other branches
- 8.3 tweet_feed.module \tweet_feed_cron()
- 6 tweet_feed.module \tweet_feed_cron()
- 7 tweet_feed.module \tweet_feed_cron()
- 7.2 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 187
Code
function tweet_feed_cron() {
// If we are not doing this as per administrative configuration, then we can skup
$tweet_feed_disable_cron = variable_get('tweet_feed_disable_cron', 0);
if (!empty($tweet_feed_disable_cron)) {
return TRUE;
}
// 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;
}
// If there are no feeds created, then we cannot continue.
if (empty($feed)) {
return FALSE;
}
// Determine the id of the last feed run
$last_fid = variable_get('tweet_feed_cron_last_feed', 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 = $feed[0];
}
else {
$key = array_search($last_fid, $feed) + 1;
if (!isset($feed[$key])) {
$current_fid = $feed[0];
}
else {
$current_fid = $feed[$key];
}
}
// Set the last fid used in our variable for future use
variable_set('tweet_feed_pull_data_from_feed', $current_fid);
variable_set('tweet_feed_cron_last_feed', $current_fid);
// Get all of the tweets to be imported
$tweets = tweet_feed_pull_data_from_feed($current_fid, TRUE);
// If this returns false, we have a problem. Reset the counter by deleting the variable.
// And then not running this particular cron.
if (!in_array($current_fid, $feed)) {
variable_del('tweet_feed_pull_data_from_feed');
variable_del('tweet_feed_cron_last_feed');
return FALSE;
}
// 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_str)
->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++) {
if ($item = $queue
->claimItem($i)) {
$feed_object = tweet_feed_get_feed_object($item->data['feed'][0]);
if (!empty($feed_object)) {
tweet_feed_save_tweet($item->data['tweet'], $feed_object, $item->data['update_node_id'], $item->data['hash']);
}
$queue
->releaseItem($item);
$queue
->deleteItem($item);
}
}
}