[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/sites/all/modules/data/data_search/ -> data_search.module (source)

   1  <?php
   2  
   3  /**
   4   * Implementation of hook_menu()
   5   */
   6  function data_search_menu() {
   7    $items = array();
   8    $items['admin/build/data/edit/%data_ui_table/search'] = array(
   9      'title' => 'Configure search',
  10      'description' => 'Administer data tables.',
  11      'page callback' => 'drupal_get_form',
  12      'page arguments' => array('data_search_admin_form', 4),
  13      'file' => 'data_search.admin.inc',
  14      'access arguments' => array('administer data tables'),
  15      'type' => MENU_LOCAL_TASK,
  16    );
  17    return $items;
  18  }
  19  
  20  /**
  21   * Implementation of hook_theme()
  22   */
  23  function data_search_theme() {
  24    return array(
  25      'data_search_admin_form' => array(
  26        'arguments' => array('form' => array()),
  27      ),
  28    );
  29  }
  30  
  31  /**
  32   * Implementation of hook_cron().
  33   *
  34   * Wipe all orphaned search records.
  35   *
  36   * @todo: Move clean up of deleted records into DataHandler::delete() once there
  37   * is a build query > alter query > execute query pattern implemented.
  38   */
  39  function data_search_cron() {
  40    $tables = data_search_get_tables();
  41  
  42    foreach ($tables as $table) {
  43      data_search_wipe($table);
  44    }
  45  }
  46  
  47  /**
  48   * Implementation of hook_views_data_alter().
  49   */
  50  function data_search_views_data_alter(&$data) {
  51    $tables = data_search_get_tables();
  52    foreach ($tables as $table) {
  53      $name = $table->get('name');
  54      $schema = $table->get('table_schema');
  55      $base_field = current($schema['primary key']);
  56  
  57      // Explain how the search index joins to data tables.
  58      $data['search_index']['table']['join'][$name] = array(
  59        'left_field' => $base_field,
  60        'field' => 'sid',
  61      );
  62      $data['search_dataset']['table']['join'][$name] = array(
  63        'left_table' => 'search_index',
  64        'left_field' => 'sid',
  65        'field' => 'sid',
  66        'extra' => 'search_index.type = search_dataset.type',
  67        'type' => 'INNER',
  68      );
  69    }
  70  }
  71  
  72  /**
  73   * Implementation of hook_update_index().
  74   */
  75  function data_search_update_index() {
  76    $limit = (int)variable_get('search_cron_limit', 100);
  77  
  78    $tables = data_search_get_tables();
  79    foreach ($tables as $table) {
  80      $name = db_escape_table($table->get('name'));
  81      $schema = $table->get('table_schema');
  82  
  83      $fields = data_search_get_fields($table);
  84      $fields = implode(', ', $fields);
  85      $base_field = current($schema['primary key']);
  86  
  87      $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);
  88  
  89      while ($row = db_fetch_object($result)) {
  90        $values = db_fetch_array(db_query("SELECT {$fields} FROM {{$name}} WHERE {$base_field} = '%s'", $row->id));
  91        $fulltext = '';
  92        foreach ($values as $field => $value) {
  93          $fulltext .= "{$value}\n\n";
  94        }
  95        search_index($row->id, $name, $fulltext);
  96      }
  97      // Delete orphaned data search records, no nodeapi to take care of this as it occurs.
  98      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");
  99    }
 100  }
 101  
 102  /**
 103   * Implementation of hook_search().
 104   */
 105  function data_search_search($op = 'search', $keys = NULL) {
 106    switch ($op) {
 107      case 'name':
 108        return t('Data');
 109  
 110      case 'reset':
 111        $tables = data_search_get_tables();
 112        foreach ($tables as $table) {
 113          $name = $table->get('name');
 114          db_query("UPDATE {search_dataset} SET reindex = %d WHERE type = '%s'", time(), $name);
 115        }
 116        return;
 117  
 118      case 'status':
 119        $total = $remaining = 0;
 120        $tables = data_search_get_tables();
 121        foreach ($tables as $table) {
 122          $name = db_escape_table($table->get('name'));
 123          $schema = $table->get('table_schema');
 124          $base_field = current($schema['primary key']);
 125  
 126          $total = $total + db_result(db_query("SELECT COUNT(*) FROM {{$name}}"));
 127          $remaining = $remaining + db_result(db_query("SELECT COUNT(*) 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)"));
 128        }
 129        return array('remaining' => $remaining, 'total' => $total);
 130    }
 131  }
 132  
 133  /**
 134   * Wipe all orphaned entries for given Data table. Use instead of search_wipe()
 135   * if all items that have been deleted from table $table should be wiped. In
 136   * this case, data_search_wipe() is faster than search_wipe().
 137   *
 138   * Note: Like search_wipe(), this function does not reset the word counts in
 139   * search_total.
 140   *
 141   * @param $table
 142   *   DataTable object.
 143   */
 144  function data_search_wipe($table) {
 145    $schema = $table->get('table_schema');
 146    $name = db_escape_table($table->get('name'));
 147    $field = current($schema['primary key']);
 148  
 149    db_query("DELETE s FROM {search_dataset} s LEFT JOIN {{$name}} t ON s.sid = t.$field WHERE s.type = '%s' AND t.$field IS NULL", $table->get('name'));
 150    db_query("DELETE s FROM {search_index} s LEFT JOIN {{$name}} t ON s.sid = t.$field WHERE s.type = '%s' AND t.$field IS NULL", $table->get('name'));
 151  }
 152  
 153  /**
 154   * Gather all tables which might be eligible for searching.
 155   */
 156  function data_search_get_tables() {
 157    $tables = array();
 158    foreach (data_get_all_tables() as $table) {
 159      $schema = $table->get('table_schema');
 160      $fields = data_search_get_fields($table);
 161      if (isset($schema['primary key']) && count($schema['primary key']) >= 1 && !empty($fields)) {
 162        $tables[] = $table;
 163      }
 164    }
 165    return $tables;
 166  }
 167  
 168  /**
 169   * Gather all fields for a particular table which should be added to the search index.
 170   */
 171  function data_search_get_fields($table) {
 172    $fields = array();
 173    $schema = $table->get('table_schema');
 174    $meta = $table->get('meta');
 175    foreach (array_keys($schema['fields']) as $field_name) {
 176      if (!empty($meta['fields'][$field_name]['search'])) {
 177        $fields[] = db_escape_table($field_name);
 178      }
 179    }
 180    return $fields;
 181  }


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7