Projet

Général

Profil

Paste
Télécharger (7,52 ko) Statistiques
| Branche: | Révision:

root / drupal7 / modules / search / search.admin.inc @ 76597ebf

1
<?php
2

    
3
/**
4
 * @file
5
 * Admin page callbacks for the search module.
6
 */
7

    
8
/**
9
 * Menu callback: confirm wiping of the index.
10
 */
11
function search_reindex_confirm() {
12
  return confirm_form(array(), t('Are you sure you want to re-index the site?'),
13
                  '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'));
14
}
15

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

    
28
/**
29
 * Helper function to get real module names.
30
 */
31
function _search_get_module_names() {
32

    
33
  $search_info = search_get_info(TRUE);
34
  $system_info = system_get_info('module');
35
  $names = array();
36
  foreach ($search_info as $module => $info) {
37
    $names[$module] = $system_info[$module]['name'];
38
  }
39
  asort($names, SORT_STRING);
40
  return $names;
41
}
42

    
43
/**
44
 * Menu callback: displays the search module settings page.
45
 *
46
 * @ingroup forms
47
 *
48
 * @see search_admin_settings_validate()
49
 * @see search_admin_settings_submit()
50
 * @see search_admin_reindex_submit()
51
 */
52
function search_admin_settings($form) {
53
  // Collect some stats
54
  $remaining = 0;
55
  $total = 0;
56
  foreach (variable_get('search_active_modules', array('node', 'user')) as $module) {
57
    if ($status = module_invoke($module, 'search_status')) {
58
      $remaining += $status['remaining'];
59
      $total += $status['total'];
60
    }
61
  }
62

    
63
  $count = format_plural($remaining, 'There is 1 item left to index.', 'There are @count items left to index.');
64
  $percentage = ((int)min(100, 100 * ($total - $remaining) / max(1, $total))) . '%';
65
  $status = '<p><strong>' . t('%percentage of the site has been indexed.', array('%percentage' => $percentage)) . ' ' . $count . '</strong></p>';
66
  $form['status'] = array('#type' => 'fieldset', '#title' => t('Indexing status'));
67
  $form['status']['status'] = array('#markup' => $status);
68
  $form['status']['wipe'] = array('#type' => 'submit', '#value' => t('Re-index site'), '#submit' => array('search_admin_reindex_submit'));
69

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

    
72
  // Indexing throttle:
73
  $form['indexing_throttle'] = array(
74
    '#type' => 'fieldset',
75
    '#title' => t('Indexing throttle')
76
  );
77
  $form['indexing_throttle']['search_cron_limit'] = array(
78
    '#type' => 'select',
79
    '#title' => t('Number of items to index per cron run'),
80
    '#default_value' => variable_get('search_cron_limit', 100),
81
    '#options' => $items,
82
    '#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')))
83
  );
84
  // Indexing settings:
85
  $form['indexing_settings'] = array(
86
    '#type' => 'fieldset',
87
    '#title' => t('Indexing settings')
88
  );
89
  $form['indexing_settings']['info'] = array(
90
    '#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>')
91
  );
92
  $form['indexing_settings']['minimum_word_size'] = array(
93
    '#type' => 'textfield',
94
    '#title' => t('Minimum word length to index'),
95
    '#default_value' => variable_get('minimum_word_size', 3),
96
    '#size' => 5,
97
    '#maxlength' => 3,
98
    '#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).'),
99
    '#element_validate' => array('element_validate_integer_positive'),
100
  );
101
  $form['indexing_settings']['overlap_cjk'] = array(
102
    '#type' => 'checkbox',
103
    '#title' => t('Simple CJK handling'),
104
    '#default_value' => variable_get('overlap_cjk', TRUE),
105
    '#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.')
106
  );
107

    
108
  $form['active'] = array(
109
    '#type' => 'fieldset',
110
    '#title' => t('Active search modules')
111
  );
112
  $module_options = _search_get_module_names();
113
  $form['active']['search_active_modules'] = array(
114
    '#type' => 'checkboxes',
115
    '#title' => t('Active modules'),
116
    '#title_display' => 'invisible',
117
    '#default_value' => variable_get('search_active_modules', array('node', 'user')),
118
    '#options' => $module_options,
119
    '#description' => t('Choose which search modules are active from the available modules.')
120
  );
121
  $form['active']['search_default_module'] = array(
122
    '#title' => t('Default search module'),
123
    '#type' => 'radios',
124
    '#default_value' => variable_get('search_default_module', 'node'),
125
    '#options' => $module_options,
126
    '#description' => t('Choose which search module is the default.')
127
  );
128
  $form['#validate'][] = 'search_admin_settings_validate';
129
  $form['#submit'][] = 'search_admin_settings_submit';
130

    
131
  // Per module settings
132
  foreach (variable_get('search_active_modules', array('node', 'user')) as $module) {
133
    $added_form = module_invoke($module, 'search_admin');
134
    if (is_array($added_form)) {
135
      $form = array_merge($form, $added_form);
136
    }
137
  }
138

    
139
  return system_settings_form($form);
140
}
141

    
142
/**
143
 * Form validation handler for search_admin_settings().
144
 */
145
function search_admin_settings_validate($form, &$form_state) {
146
  // Check whether we selected a valid default.
147
  if ($form_state['triggering_element']['#value'] != t('Reset to defaults')) {
148
    $new_modules = array_filter($form_state['values']['search_active_modules']);
149
    $default = $form_state['values']['search_default_module'];
150
    if (!in_array($default, $new_modules, TRUE)) {
151
      form_set_error('search_default_module', t('Your default search module is not selected as an active module.'));
152
    }
153
  }
154
}
155

    
156
/**
157
 * Form submission handler for search_admin_settings().
158
 */
159
function search_admin_settings_submit($form, &$form_state) {
160
  // If these settings change, the index needs to be rebuilt.
161
  if ((variable_get('minimum_word_size', 3) != $form_state['values']['minimum_word_size']) ||
162
      (variable_get('overlap_cjk', TRUE) != $form_state['values']['overlap_cjk'])) {
163
    drupal_set_message(t('The index will be rebuilt.'));
164
    search_reindex();
165
  }
166
  $current_modules = variable_get('search_active_modules', array('node', 'user'));
167
  // Check whether we are resetting the values.
168
  if ($form_state['triggering_element']['#value'] == t('Reset to defaults')) {
169
    $new_modules = array('node', 'user');
170
  }
171
  else {
172
    $new_modules = array_filter($form_state['values']['search_active_modules']);
173
  }
174
  if (array_diff($current_modules, $new_modules)) {
175
    drupal_set_message(t('The active search modules have been changed.'));
176
    variable_set('menu_rebuild_needed', TRUE);
177
  }
178
}
179

    
180
/**
181
 * Form submission handler for reindex button on search_admin_settings_form().
182
 */
183
function search_admin_reindex_submit($form, &$form_state) {
184
  // send the user to the confirmation page
185
  $form_state['redirect'] = 'admin/config/search/settings/reindex';
186
}