search.admin.inc 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. /**
  3. * @file
  4. * Admin page callbacks for the search module.
  5. */
  6. /**
  7. * Menu callback: confirm wiping of the index.
  8. */
  9. function search_reindex_confirm() {
  10. return confirm_form(array(), t('Are you sure you want to re-index the site?'),
  11. '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'));
  12. }
  13. /**
  14. * Handler for wipe confirmation
  15. */
  16. function search_reindex_confirm_submit(&$form, &$form_state) {
  17. if ($form['confirm']) {
  18. search_reindex();
  19. drupal_set_message(t('The index will be rebuilt.'));
  20. $form_state['redirect'] = 'admin/config/search/settings';
  21. return;
  22. }
  23. }
  24. /**
  25. * Helper function to get real module names.
  26. */
  27. function _search_get_module_names() {
  28. $search_info = search_get_info(TRUE);
  29. $system_info = system_get_info('module');
  30. $names = array();
  31. foreach ($search_info as $module => $info) {
  32. $names[$module] = $system_info[$module]['name'];
  33. }
  34. asort($names, SORT_STRING);
  35. return $names;
  36. }
  37. /**
  38. * Menu callback: displays the search module settings page.
  39. *
  40. * @ingroup forms
  41. *
  42. * @see search_admin_settings_validate()
  43. * @see search_admin_settings_submit()
  44. * @see search_admin_reindex_submit()
  45. */
  46. function search_admin_settings($form) {
  47. // Collect some stats
  48. $remaining = 0;
  49. $total = 0;
  50. foreach (variable_get('search_active_modules', array('node', 'user')) as $module) {
  51. if ($status = module_invoke($module, 'search_status')) {
  52. $remaining += $status['remaining'];
  53. $total += $status['total'];
  54. }
  55. }
  56. $count = format_plural($remaining, 'There is 1 item left to index.', 'There are @count items left to index.');
  57. $percentage = ((int)min(100, 100 * ($total - $remaining) / max(1, $total))) . '%';
  58. $status = '<p><strong>' . t('%percentage of the site has been indexed.', array('%percentage' => $percentage)) . ' ' . $count . '</strong></p>';
  59. $form['status'] = array('#type' => 'fieldset', '#title' => t('Indexing status'));
  60. $form['status']['status'] = array('#markup' => $status);
  61. $form['status']['wipe'] = array('#type' => 'submit', '#value' => t('Re-index site'), '#submit' => array('search_admin_reindex_submit'));
  62. $items = drupal_map_assoc(array(10, 20, 50, 100, 200, 500));
  63. // Indexing throttle:
  64. $form['indexing_throttle'] = array(
  65. '#type' => 'fieldset',
  66. '#title' => t('Indexing throttle')
  67. );
  68. $form['indexing_throttle']['search_cron_limit'] = array(
  69. '#type' => 'select',
  70. '#title' => t('Number of items to index per cron run'),
  71. '#default_value' => variable_get('search_cron_limit', 100),
  72. '#options' => $items,
  73. '#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')))
  74. );
  75. // Indexing settings:
  76. $form['indexing_settings'] = array(
  77. '#type' => 'fieldset',
  78. '#title' => t('Indexing settings')
  79. );
  80. $form['indexing_settings']['info'] = array(
  81. '#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>')
  82. );
  83. $form['indexing_settings']['minimum_word_size'] = array(
  84. '#type' => 'textfield',
  85. '#title' => t('Minimum word length to index'),
  86. '#default_value' => variable_get('minimum_word_size', 3),
  87. '#size' => 5,
  88. '#maxlength' => 3,
  89. '#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).'),
  90. '#element_validate' => array('element_validate_integer_positive'),
  91. );
  92. $form['indexing_settings']['overlap_cjk'] = array(
  93. '#type' => 'checkbox',
  94. '#title' => t('Simple CJK handling'),
  95. '#default_value' => variable_get('overlap_cjk', TRUE),
  96. '#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.')
  97. );
  98. $form['active'] = array(
  99. '#type' => 'fieldset',
  100. '#title' => t('Active search modules')
  101. );
  102. $module_options = _search_get_module_names();
  103. $form['active']['search_active_modules'] = array(
  104. '#type' => 'checkboxes',
  105. '#title' => t('Active modules'),
  106. '#title_display' => 'invisible',
  107. '#default_value' => variable_get('search_active_modules', array('node', 'user')),
  108. '#options' => $module_options,
  109. '#description' => t('Choose which search modules are active from the available modules.')
  110. );
  111. $form['active']['search_default_module'] = array(
  112. '#title' => t('Default search module'),
  113. '#type' => 'radios',
  114. '#default_value' => variable_get('search_default_module', 'node'),
  115. '#options' => $module_options,
  116. '#description' => t('Choose which search module is the default.')
  117. );
  118. $form['logging'] = array(
  119. '#type' => 'fieldset',
  120. '#title' => t('Logging')
  121. );
  122. $form['logging']['search_logging'] = array(
  123. '#type' => 'checkbox',
  124. '#title' => t('Log searches'),
  125. '#default_value' => variable_get('search_logging', 1),
  126. '#description' => t('If checked, all searches will be logged. Uncheck to skip logging. Logging may affect performance.'),
  127. );
  128. $form['#validate'][] = 'search_admin_settings_validate';
  129. $form['#submit'][] = 'search_admin_settings_submit';
  130. // Per module settings
  131. foreach (variable_get('search_active_modules', array('node', 'user')) as $module) {
  132. $added_form = module_invoke($module, 'search_admin');
  133. if (is_array($added_form)) {
  134. $form = array_merge($form, $added_form);
  135. }
  136. }
  137. return system_settings_form($form);
  138. }
  139. /**
  140. * Form validation handler for search_admin_settings().
  141. */
  142. function search_admin_settings_validate($form, &$form_state) {
  143. // Check whether we selected a valid default.
  144. if ($form_state['triggering_element']['#value'] != t('Reset to defaults')) {
  145. $new_modules = array_filter($form_state['values']['search_active_modules']);
  146. $default = $form_state['values']['search_default_module'];
  147. if (!in_array($default, $new_modules, TRUE)) {
  148. form_set_error('search_default_module', t('Your default search module is not selected as an active module.'));
  149. }
  150. }
  151. }
  152. /**
  153. * Form submission handler for search_admin_settings().
  154. */
  155. function search_admin_settings_submit($form, &$form_state) {
  156. // If these settings change, the index needs to be rebuilt.
  157. if ((variable_get('minimum_word_size', 3) != $form_state['values']['minimum_word_size']) ||
  158. (variable_get('overlap_cjk', TRUE) != $form_state['values']['overlap_cjk'])) {
  159. drupal_set_message(t('The index will be rebuilt.'));
  160. search_reindex();
  161. }
  162. $current_modules = variable_get('search_active_modules', array('node', 'user'));
  163. // Check whether we are resetting the values.
  164. if ($form_state['triggering_element']['#value'] == t('Reset to defaults')) {
  165. $new_modules = array('node', 'user');
  166. }
  167. else {
  168. $new_modules = array_filter($form_state['values']['search_active_modules']);
  169. }
  170. if (array_diff($current_modules, $new_modules)) {
  171. drupal_set_message(t('The active search modules have been changed.'));
  172. variable_set('menu_rebuild_needed', TRUE);
  173. }
  174. }
  175. /**
  176. * Form submission handler for reindex button on search_admin_settings_form().
  177. */
  178. function search_admin_reindex_submit($form, &$form_state) {
  179. // send the user to the confirmation page
  180. $form_state['redirect'] = 'admin/config/search/settings/reindex';
  181. }