search.admin.inc 6.37 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
<?php
// $Id$

/**
 * @file
 * Admin page callbacks for the search module.
 */

/**
 * Menu callback: confirm wiping of the index.
 */
12
function search_reindex_confirm() {
13
  return confirm_form(array(), t('Are you sure you want to re-index the site?'),
14
                  'admin/config/search/settings', t(' The search index is not cleared but systematically updated to reflect the new settings. Searching will continue to work but new content won\'t be indexed until all existing content has been re-indexed. This action cannot be undone.'), t('Re-index site'), t('Cancel'));
15
16
17
18
19
}

/**
 * Handler for wipe confirmation
 */
20
function search_reindex_confirm_submit(&$form, &$form_state) {
21
  if ($form['confirm']) {
22
    search_reindex();
23
    drupal_set_message(t('The index will be rebuilt.'));
24
    $form_state['redirect'] = 'admin/config/search/settings';
25
26
27
28
    return;
  }
}

29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
 * Helper function to get real module names.
 */
function _search_get_module_names() {

  $search_info = search_get_info();
  $modules = db_select('system', 's')
    ->fields('s', array('name', 'info'))
    ->condition('s.status', 1)
    ->condition('s.type', 'module')
    ->condition('s.name', array_keys($search_info), 'IN')
    ->orderBy('s.name')
    ->execute();
  $names = array();
  foreach ($modules as $item) {
    $info = unserialize($item->info);
    $names[$item->name] = $info['name'];
  }
  return $names;
}

50
51
52
53
/**
 * Menu callback; displays the search module settings page.
 *
 * @ingroup forms
54
 * @see system_settings_form()
55
56
 * @see search_admin_settings_submit()
 * @see search_admin_reindex_submit()
57
 */
58
function search_admin_settings($form) {
59
60
61
  // Collect some stats
  $remaining = 0;
  $total = 0;
62
63
64
65
66
  foreach(variable_get('search_active_modules', array('node', 'user')) as $module) {
    if ($status = module_invoke($module, 'search_status')) {
      $remaining += $status['remaining'];
      $total += $status['total'];
    }
67
  }
68

69
  $count = format_plural($remaining, 'There is 1 item left to index.', 'There are @count items left to index.');
70
71
  $percentage = ((int)min(100, 100 * ($total - $remaining) / max(1, $total))) . '%';
  $status = '<p><strong>' . t('%percentage of the site has been indexed.', array('%percentage' => $percentage)) . ' ' . $count . '</strong></p>';
72
  $form['status'] = array('#type' => 'fieldset', '#title' => t('Indexing status'));
73
  $form['status']['status'] = array('#markup' => $status);
74
  $form['status']['wipe'] = array('#type' => 'submit', '#value' => t('Re-index site'), '#submit' => array('search_admin_reindex_submit'));
75
76
77
78

  $items = drupal_map_assoc(array(10, 20, 50, 100, 200, 500));

  // Indexing throttle:
79
80
81
82
83
84
85
86
87
88
89
  $form['indexing_throttle'] = array(
    '#type' => 'fieldset',
    '#title' => t('Indexing throttle')
  );
  $form['indexing_throttle']['search_cron_limit'] = array(
    '#type' => 'select',
    '#title' => t('Number of items to index per cron run'),
    '#default_value' => 100,
    '#options' => $items,
    '#description' => t('The maximum number of items indexed in each pass of a <a href="@cron">cron maintenance task</a>. If necessary, reduce the number of items to prevent timeouts and memory errors while indexing.', array('@cron' => url('admin/reports/status')))
  );
90
  // Indexing settings:
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
  $form['indexing_settings'] = array(
    '#type' => 'fieldset',
    '#title' => t('Indexing settings')
  );
  $form['indexing_settings']['info'] = array(
    '#markup' => t('<p><em>Changing the settings below will cause the site index to be rebuilt. The search index is not cleared but systematically updated to reflect the new settings. Searching will continue to work but new content won\'t be indexed until all existing content has been re-indexed.</em></p><p><em>The default settings should be appropriate for the majority of sites.</em></p>')
  );
  $form['indexing_settings']['minimum_word_size'] = array(
    '#type' => 'textfield',
    '#title' => t('Minimum word length to index'),
    '#default_value' => 3,
    '#size' => 5,
    '#maxlength' => 3,
    '#description' => t('The number of characters a word has to be to be indexed. A lower setting means better search result ranking, but also a larger database. Each search query must contain at least one keyword that is this size (or longer).')
  );
  $form['indexing_settings']['overlap_cjk'] = array(
    '#type' => 'checkbox',
    '#title' => t('Simple CJK handling'),
    '#default_value' => TRUE,
    '#description' => t('Whether to apply a simple Chinese/Japanese/Korean tokenizer based on overlapping sequences. Turn this off if you want to use an external preprocessor for this instead. Does not affect other languages.')
  );
112

113
114
115
116
117
  $form['active'] = array(
    '#type' => 'fieldset',
    '#title' => t('Active search modules ')
  );
  $form['active']['search_active_modules'] = array(
118
119
120
121
122
123
    '#type' => 'checkboxes',
    '#default_value' => array('node', 'user'),
    '#options' => _search_get_module_names(),
    '#description' => t('Determine which search modules are active from the available modules.')
  );

124
  $form['#submit'][] = 'search_admin_settings_submit';
125
126

  // Per module settings
127
128
129
130
131
132
133
  foreach(variable_get('search_active_modules', array('node', 'user')) as $module) {
    $added_form = module_invoke($module, 'search_admin');
    if (is_array($added_form)) {
      $form = array_merge($form, $added_form);
    }
  }

134
  return system_settings_form($form, TRUE);
135
136
137
}

/**
138
 * Submit callback.
139
 */
140
function search_admin_settings_submit($form, &$form_state) {
141
142
143
144
  // If these settings change, the index needs to be rebuilt.
  if ((variable_get('minimum_word_size', 3) != $form_state['values']['minimum_word_size']) ||
      (variable_get('overlap_cjk', TRUE) != $form_state['values']['overlap_cjk'])) {
    drupal_set_message(t('The index will be rebuilt.'));
145
    search_reindex();
146
  }
147
148
149
150
151
152
153
154
155
156
157
158
  $current_modules = variable_get('search_active_modules', array('node', 'user'));
  // Check whether we are resetting the values.
  if ($form_state['clicked_button']['#value'] == t('Reset to defaults')) {
    $new_modules = array('node', 'user');
  }
  else {
    $new_modules = array_filter($form_state['values']['search_active_modules']);
  }
  if (array_diff($current_modules, $new_modules)) {
    drupal_set_message(t('The active search modules have been changed.'));
    variable_set('menu_rebuild_needed', TRUE);
  }
159
}
160
161
162
163
164
165

/**
 * Submit callback.
 */
function search_admin_reindex_submit($form, &$form_state) {
  // send the user to the confirmation page
166
  $form_state['redirect'] = 'admin/config/search/settings/reindex';
167
}