You are here

function data_search_update_index in Data 6

Same name and namespace in other branches
  1. 8 data_search/data_search.module \data_search_update_index()
  2. 7 data_search/data_search.module \data_search_update_index()

Implementation of hook_update_index().

File

data_search/data_search.module, line 75

Code

function data_search_update_index() {
  $limit = (int) variable_get('search_cron_limit', 100);
  $tables = data_search_get_tables();
  foreach ($tables as $table) {
    $name = db_escape_table($table
      ->get('name'));
    $schema = $table
      ->get('table_schema');
    $fields = data_search_get_fields($table);
    $fields = implode(', ', $fields);
    $base_field = current($schema['primary key']);
    $result = db_query_range("SELECT dt.{$base_field} id FROM {{$name}} dt LEFT JOIN {search_dataset} d ON d.type = '{$name}' AND d.sid = dt.{$base_field} WHERE d.sid IS NULL OR d.reindex <> 0 ORDER BY d.reindex ASC, dt.{$base_field} ASC", 0, $limit);
    while ($row = db_fetch_object($result)) {
      $values = db_fetch_array(db_query("SELECT {$fields} FROM {{$name}} WHERE {$base_field} = '%s'", $row->id));
      $fulltext = '';
      foreach ($values as $field => $value) {
        $fulltext .= "{$value}\n\n";
      }
      search_index($row->id, $name, $fulltext);
    }

    // Delete orphaned data search records, no nodeapi to take care of this as it occurs.
    db_query("DELETE sd, si, snl FROM {search_dataset} sd LEFT JOIN {{$name}} dt ON sd.type = '{$name}' AND sd.sid = dt.{$base_field} LEFT JOIN {search_index} si ON sd.sid = si.sid AND sd.type = si.type LEFT JOIN {search_node_links} snl ON sd.sid = snl.sid AND sd.type = snl.type WHERE sd.type = '{$name}' AND dt.{$base_field} IS NULL");
  }
}