You are here

function twitter_cron in Twitter 7.4

Same name and namespace in other branches
  1. 6.5 twitter.module \twitter_cron()
  2. 6.2 twitter.module \twitter_cron()
  3. 6.3 twitter.module \twitter_cron()
  4. 6.4 twitter.module \twitter_cron()
  5. 7.6 twitter.module \twitter_cron()
  6. 7.3 twitter.module \twitter_cron()
  7. 7.5 twitter.module \twitter_cron()

Implements hook_cron().

Imports new Twitter statuses for site users, and deletes expired tweets.

1 call to twitter_cron()
TwitterTest::testAccountAdditionNoOauth in tests/core.test
Tests account addition without Oauth module activated

File

./twitter.module, line 136
Provides API integration with the Twitter microblogging service.

Code

function twitter_cron() {
  if (!variable_get('twitter_import', TRUE) || !twitter_api_keys()) {
    return;
  }

  // Pull up a list of Twitter accounts that are flagged for updating,
  // sorted by how long it's been since we last updated them. This ensures
  // that the most out-of-date accounts get updated first.
  module_load_include('inc', 'twitter');
  $result = db_query_range("SELECT twitter_uid\n                            FROM {twitter_account}\n                            WHERE import = :import\n                            AND oauth_token <> ''\n                            AND oauth_token_secret <> ''\n                            ORDER BY last_refresh ASC", 0, 20, array(
    ':import' => 1,
  ));
  try {
    foreach ($result as $account) {

      // Fetch tweets and mentions.
      twitter_fetch_user_timeline($account->twitter_uid);
      $twitter_account = twitter_account_load($account->twitter_uid);
      if ($twitter_account->mentions) {
        twitter_fetch_mentions_timeline($twitter_account->id);
      }

      // Mark the time this account was updated.
      db_update('twitter_account')
        ->fields(array(
        'last_refresh' => REQUEST_TIME,
      ))
        ->condition('twitter_uid', $twitter_account->id)
        ->execute();
    }
  } catch (TwitterException $e) {

    // The exception has already been logged so we do not need to do anything here apart from catching it.
  }

  // Nuke old statuses.
  if ($age = variable_get('twitter_expire', 0)) {
    db_delete('twitter')
      ->condition('created_time', REQUEST_TIME - $age, '<')
      ->execute();
  }
}