You are here

function biblio_pm_cron in Bibliography Module 7

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

File

modules/pubmed/biblio_pm.module, line 10

Code

function biblio_pm_cron() {
  if (variable_get('biblio_pm_auto_update', 0)) {
    module_load_include('inc', 'biblio', 'includes/biblio.import.export');

    // Defaults to once per hour.
    $interval = variable_get('biblio_pm_update_interval', 3600);

    // Only update 100 at a time.
    $count_limit = variable_get('biblio_pm_update_limit', 100);

    // Defaults to one month since last update.
    $age = variable_get('biblio_pm_age_limit', 2419200);
    $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(
        'nid',
        'biblio_pubmed_id',
      ))
        ->condition('biblio_pm_changed', $age_limit, '<')
        ->orderBy('nid', 'ASC')
        ->range(0, $count_limit)
        ->execute();
      foreach ($result as $pm) {
        $ids[$pm->nid] = $pm->biblio_pubmed_id;
      }
      if (count($ids)) {
        list($nids, $dups) = biblio_pm_import_ids($ids);
        if (count($nids)) {
          foreach ($nids as $nid) {
            $message = '';
            $message = t('!nid was updated due to changes originating at !url', array(
              '!nid' => l($nid, 'node/' . $nid),
              '!url' => l(t('PubMed'), 'https://www.ncbi.nlm.nih.gov/pubmed/' . $ids[$nid]),
            ));
            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('nid', $dups, 'IN')
            ->execute();
        }
      }
      else {
        $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);
    }
  }
}