You are here

function biblio_pm_cron in Bibliography Module 7.2

Same name and namespace in other branches
  1. 6.2 modules/pubmed/biblio_pm.module \biblio_pm_cron()
  2. 7 modules/pubmed/biblio_pm.module \biblio_pm_cron()

File

modules/pubmed/biblio_pm.module, line 6

Code

function biblio_pm_cron() {
  if (variable_get('biblio_pm_auto_update', 0)) {
    $interval = variable_get('biblio_pm_update_interval', 3600);

    // defaults to once per hour
    $count_limit = variable_get('biblio_pm_update_limit', 100);

    // only update 100 at a time
    $age = variable_get('biblio_pm_age_limit', 2419200);

    // defaults to one month since last update
    $age_limit = time() - $age;
    if (time() >= variable_get('biblio_pm_update_next_execution', 0)) {
      $ids = array();
      $result = db_select('biblio_pubmed', 'bpm')
        ->fields('bpm', array(
        'bid',
        'biblio_pubmed_id',
      ))
        ->condition('biblio_pm_changed', $age_limit, '<')
        ->orderBy('bid', 'ASC')
        ->range(0, $count_limit)
        ->execute();
      foreach ($result as $pm) {
        $ids[$pm->bid] = $pm->biblio_pubmed_id;
      }
      if (count($ids)) {
        list($bids, $dups) = biblio_pm_import_ids($ids);
        if (count($bids)) {
          foreach ($bids as $bid) {
            $message = '';
            $message = t('!bid was updated due to changes originating at !url', array(
              '!bid' => l($bid, 'biblio/' . $bid),
              '!url' => l('PubMed', 'http://www.ncbi.nlm.nih.gov/pubmed/' . $ids[$bid]),
            ));
            watchdog('biblio_pm', $message, array(), WATCHDOG_WARNING);
          }
        }
        if (count($dups)) {
          $count = count($dups);
          $message = format_plural($count, 'One duplicate PubMed entry was checked, but no changes were found.', '@count PubMed entries were checked, but no changes were found.');
          watchdog('biblio_pm', $message, array(
            '@count' => $count,
          ), WATCHDOG_INFO);
          $now = time();
          db_update('biblio_pubmed')
            ->fields(array(
            'biblio_pm_changed' => $now,
          ))
            ->condition('bid', $dups, 'IN')
            ->execute();
        }
      }
      $message = t('There were no PubMed entries older than @age to check.', array(
        '@age' => format_interval($age),
      ));
      watchdog('biblio_pm', $message, array(), WATCHDOG_INFO);
      variable_set('biblio_pm_update_next_execution', time() + $interval);
    }
  }
}