You are here

function advanced_help_update_index in Advanced Help 5

Same name and namespace in other branches
  1. 6 advanced_help.module \advanced_help_update_index()
  2. 7 advanced_help.module \advanced_help_update_index()

Implementation of hook_update_index().

File

./advanced_help.module, line 670
advanced_help.module

Code

function advanced_help_update_index() {
  global $language;

  // If we got interrupted by limit, this will contain the last module
  // and topic we looked at.
  $last = variable_get('advanced_help_last_cron', array(
    'time' => 0,
  ));
  $limit = intval(variable_get('search_cron_limit', 100));
  $topics = advanced_help_get_topics();
  advanced_help_get_sids($topics);
  $count = 0;
  foreach ($topics as $module => $module_topics) {

    // Fast forward if necessary.
    if (!empty($last['module']) && $last['module'] != $module) {
      continue;
    }
    foreach ($module_topics as $topic => $info) {

      // Fast forward if necessary.
      if (!empty($last['topic']) && $last['topic'] != $topic) {
        continue;
      }

      // If we've been looking to catch up, and we have, reset so we
      // stop fast forwarding.
      if (!empty($last['module'])) {
        unset($last['topic']);
        unset($last['module']);
      }
      $file = advanced_help_get_topic_filename($module, $topic);
      if ($file && (empty($info['sid']) || filemtime($file) > $last['time'])) {
        if (empty($info['sid'])) {
          $info['sid'] = db_next_id('{advanced_help_index}_sid');
          db_query("INSERT INTO {advanced_help_index} (sid, module, topic, language) VALUES (%d, '%s', '%s', '%s')", $info['sid'], $module, $topic, $language->language);
        }
        search_index($info['sid'], 'help', '<h1>' . $info['title'] . '</h1>' . file_get_contents($file));
        $count++;
        if ($count >= $limit) {
          $last['topic'] = $topic;
          $last['module'] = $module;

          // Don't change time if we stop.
          variable_set('advanced_help_last_cron', $last);
          return;
        }
      }
    }
  }
  variable_set('advanced_help_last_cron', array(
    'time' => time(),
  ));
}