Mercurial > hg > MPIWG-drupal-modules
comparison sites/all/modules/custom/solrconnect/apachesolr.admin.inc @ 0:015d06b10d37 default tip
initial
author | dwinter |
---|---|
date | Wed, 31 Jul 2013 13:49:13 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:015d06b10d37 |
---|---|
1 <?php | |
2 | |
3 /** | |
4 * @file | |
5 * Administrative pages for the Apache Solr framework. | |
6 */ | |
7 | |
8 /** | |
9 * Form to delete a search environment | |
10 * | |
11 * @param array $form | |
12 * @param array $form_state | |
13 * @param array $environment | |
14 * | |
15 * @return array output of confirm_form() | |
16 */ | |
17 function apachesolr_environment_delete_form(array $form, array &$form_state, array $environment) { | |
18 $form['env_id'] = array( | |
19 '#type' => 'value', | |
20 '#value' => $environment['env_id'], | |
21 ); | |
22 if (isset($environment['export_type']) && $environment['export_type'] == 3) { | |
23 $verb = t('Revert'); | |
24 } | |
25 else { | |
26 $verb = t('Delete'); | |
27 } | |
28 return confirm_form( | |
29 $form, | |
30 t('Are you sure you want to !verb search environment %name?', array('%name' => $environment['name'], '!verb' => strtolower($verb))), | |
31 'admin/config/search/apachesolr', | |
32 t('This action cannot be undone.'), | |
33 $verb, | |
34 t('Cancel') | |
35 ); | |
36 } | |
37 | |
38 /** | |
39 * Submit handler for the delete form | |
40 * | |
41 * @param array $form | |
42 * @param array $form_state | |
43 */ | |
44 function apachesolr_environment_delete_form_submit(array $form, array &$form_state) { | |
45 if (apachesolr_environment_delete($form_state['values']['env_id'])) { | |
46 drupal_set_message(t('The search environment was deleted')); | |
47 } | |
48 $form_state['redirect'] = 'admin/config/search/apachesolr/settings'; | |
49 } | |
50 | |
51 function apachesolr_environment_edit_delete_submit($form, &$form_state) { | |
52 $form_state['redirect'] = 'admin/config/search/apachesolr/settings/' . $form_state['values']['env_id'] . '/delete'; | |
53 | |
54 // Regardlessly of the destination parameter we want to go to another page | |
55 unset($_GET['destination']); | |
56 drupal_static_reset('drupal_get_destination'); | |
57 drupal_get_destination(); | |
58 } | |
59 | |
60 /** | |
61 * Settings page for a specific environment (or default one if not provided) | |
62 * | |
63 * @param array|bool $environment | |
64 * | |
65 * @return array Render array for a settings page | |
66 */ | |
67 function apachesolr_environment_settings_page(array $environment = array()) { | |
68 if (empty($environment)) { | |
69 $env_id = apachesolr_default_environment(); | |
70 $environment = apachesolr_environment_load($env_id); | |
71 } | |
72 $env_id = $environment['env_id']; | |
73 | |
74 // Initializes output with information about which environment's setting we are | |
75 // editing, as it is otherwise not transparent to the end user. | |
76 $output = array( | |
77 'apachesolr_environment' => array( | |
78 '#theme' => 'apachesolr_settings_title', | |
79 '#env_id' => $env_id, | |
80 ), | |
81 ); | |
82 $output['form'] = drupal_get_form('apachesolr_environment_edit_form', $environment); | |
83 return $output; | |
84 } | |
85 | |
86 /** | |
87 * Form to clone a certain environment | |
88 * | |
89 * @param array $form | |
90 * @param array $form_state | |
91 * @param array $environment | |
92 * | |
93 * @return array output of confirm_form() | |
94 */ | |
95 function apachesolr_environment_clone_form(array $form, array &$form_state, array $environment) { | |
96 $form['env_id'] = array( | |
97 '#type' => 'value', | |
98 '#value' => $environment['env_id'], | |
99 ); | |
100 return confirm_form( | |
101 $form, | |
102 t('Are you sure you want to clone search environment %name?', array('%name' => $environment['name'])), | |
103 'admin/config/search/apachesolr', | |
104 '', | |
105 t('Clone'), | |
106 t('Cancel') | |
107 ); | |
108 } | |
109 | |
110 /** | |
111 * Submit handler for the clone form | |
112 * | |
113 * @param array $form | |
114 * @param array $form_state | |
115 */ | |
116 function apachesolr_environment_clone_form_submit(array $form, array &$form_state) { | |
117 if (apachesolr_environment_clone($form_state['values']['env_id'])) { | |
118 drupal_set_message(t('The search environment was cloned')); | |
119 } | |
120 $form_state['redirect'] = 'admin/config/search/apachesolr/settings'; | |
121 } | |
122 | |
123 /** | |
124 * Submit handler for the confirmation page of cloning an environment | |
125 * | |
126 * @param array $form | |
127 * @param array $form_state | |
128 */ | |
129 function apachesolr_environment_clone_submit(array $form, array &$form_state) { | |
130 $form_state['redirect'] = 'admin/config/search/apachesolr/settings/' . $form_state['values']['env_id'] . '/clone'; | |
131 } | |
132 | |
133 /** | |
134 * Form builder for adding/editing a Solr environment used as a menu callback. | |
135 */ | |
136 function apachesolr_environment_edit_form(array $form, array &$form_state, array $environment = array()) { | |
137 if (empty($environment)) { | |
138 $environment = array(); | |
139 } | |
140 $environment += array('env_id' => '', 'name' => '', 'url' => '', 'service_class' => '', 'conf' => array()); | |
141 | |
142 $form['#environment'] = $environment; | |
143 $form['url'] = array( | |
144 '#type' => 'textfield', | |
145 '#title' => t('Solr server URL'), | |
146 '#default_value' => $environment['url'], | |
147 '#description' => t('Example: http://localhost:8983/solr'), | |
148 '#required' => TRUE, | |
149 ); | |
150 $is_default = $environment['env_id'] == apachesolr_default_environment(); | |
151 $form['make_default'] = array( | |
152 '#type' => 'checkbox', | |
153 '#title' => t('Make this Solr search environment the default'), | |
154 '#default_value' => $is_default, | |
155 '#disabled' => $is_default, | |
156 ); | |
157 $form['name'] = array( | |
158 '#type' => 'textfield', | |
159 '#title' => t('Description'), | |
160 '#default_value' => $environment['name'], | |
161 '#required' => TRUE, | |
162 ); | |
163 $form['env_id'] = array( | |
164 '#type' => 'machine_name', | |
165 '#title' => t('Environment id'), | |
166 '#machine_name' => array( | |
167 'exists' => 'apachesolr_environment_load', | |
168 ), | |
169 '#default_value' => $environment['env_id'], | |
170 '#disabled' => !empty($environment['env_id']), // Cannot change it once set. | |
171 '#description' => t('Unique, machine-readable identifier for this Solr environment.'), | |
172 '#required' => TRUE, | |
173 ); | |
174 $form['service_class'] = array( | |
175 '#type' => 'value', | |
176 '#value' => $environment['service_class'], | |
177 ); | |
178 $form['conf'] = array( | |
179 '#tree' => TRUE, | |
180 ); | |
181 $form['conf']['apachesolr_read_only'] = array( | |
182 '#type' => 'radios', | |
183 '#title' => t('Index write access'), | |
184 '#default_value' => isset($environment['conf']['apachesolr_read_only']) ? $environment['conf']['apachesolr_read_only'] : APACHESOLR_READ_WRITE, | |
185 '#options' => array(APACHESOLR_READ_WRITE => t('Read and write (normal)'), APACHESOLR_READ_ONLY => t('Read only')), | |
186 '#description' => t('<em>Read only</em> stops this site from sending updates to this search environment. Useful for development sites.'), | |
187 ); | |
188 $form['actions'] = array( | |
189 '#type' => 'actions', | |
190 ); | |
191 $form['actions']['save'] = array( | |
192 '#type' => 'submit', | |
193 '#validate' => array('apachesolr_environment_edit_validate'), | |
194 '#submit' => array('apachesolr_environment_edit_submit'), | |
195 '#value' => t('Save'), | |
196 ); | |
197 $form['actions']['save_edit'] = array( | |
198 '#type' => 'submit', | |
199 '#validate' => array('apachesolr_environment_edit_validate'), | |
200 '#submit' => array('apachesolr_environment_edit_submit'), | |
201 '#value' => t('Save and edit'), | |
202 ); | |
203 $form['actions']['test'] = array( | |
204 '#type' => 'submit', | |
205 '#validate' => array('apachesolr_environment_edit_validate'), | |
206 '#submit' => array('apachesolr_environment_edit_test_submit'), | |
207 '#value' => t('Test connection'), | |
208 ); | |
209 if (!empty($environment['env_id']) && !$is_default) { | |
210 $form['actions']['delete'] = array( | |
211 '#type' => 'submit', | |
212 '#submit' => array('apachesolr_environment_edit_delete_submit'), | |
213 '#value' => t('Delete'), | |
214 ); | |
215 } | |
216 | |
217 // Ensures destination is an internal URL, builds "cancel" link. | |
218 if (isset($_GET['destination']) && !url_is_external($_GET['destination'])) { | |
219 $destination = $_GET['destination']; | |
220 } | |
221 else { | |
222 $destination = 'admin/config/search/apachesolr/settings'; | |
223 } | |
224 $form['actions']['cancel'] = array( | |
225 '#type' => 'link', | |
226 '#title' => t('Cancel'), | |
227 '#href' => $destination, | |
228 ); | |
229 | |
230 return $form; | |
231 } | |
232 | |
233 /** | |
234 * Submit handler for the test button in the environment edit page | |
235 * | |
236 * @param array $form | |
237 * @param array $form_state | |
238 */ | |
239 function apachesolr_environment_edit_test_submit(array $form, array &$form_state) { | |
240 $ping = apachesolr_server_status($form_state['values']['url'], $form_state['values']['service_class']); | |
241 if ($ping) { | |
242 drupal_set_message(t('Your site has contacted the Apache Solr server.')); | |
243 } | |
244 else { | |
245 drupal_set_message(t('Your site was unable to contact the Apache Solr server.'), 'error'); | |
246 } | |
247 $form_state['rebuild'] = TRUE; | |
248 } | |
249 | |
250 /** | |
251 * Validate handler for the environment edit page | |
252 * | |
253 * @param array $form | |
254 * @param array $form_state | |
255 */ | |
256 function apachesolr_environment_edit_validate(array $form, array &$form_state) { | |
257 $parts = parse_url($form_state['values']['url']); | |
258 foreach (array('scheme', 'host', 'path') as $key) { | |
259 if (empty($parts[$key])) { | |
260 form_set_error('url', t('The Solr server URL needs to include a !part', array('!part' => $key))); | |
261 } | |
262 } | |
263 if (isset($parts['port'])) { | |
264 // parse_url() should always give an integer for port. Since drupal_http_request() | |
265 // also uses parse_url(), we don't need to validate anything except the range. | |
266 $pattern = empty($parts['user']) ? '@://[^:]+:([^/]+)@' : '#://[^@]+@[^:]+:([^/]+)#'; | |
267 preg_match($pattern, $form_state['values']['url'], $m); | |
268 if (empty($m[1]) || !ctype_digit($m[1]) || $m[1] < 1 || $m[1] > 65535) { | |
269 form_set_error('port', t('The port has to be an integer between 1 and 65535.')); | |
270 } | |
271 else { | |
272 // Normalize the url by removing extra slashes and whitespace. | |
273 $form_state['values']['url'] = trim($form_state['values']['url'], "/ \t\r\n\0\x0B"); | |
274 } | |
275 } | |
276 } | |
277 | |
278 /** | |
279 * Submit handler for the environment edit page | |
280 * | |
281 * @param array $form | |
282 * @param array $form_state | |
283 */ | |
284 function apachesolr_environment_edit_submit(array $form, array &$form_state) { | |
285 apachesolr_environment_save($form_state['values']); | |
286 if (!empty($form_state['values']['make_default'])) { | |
287 apachesolr_set_default_environment($form_state['values']['env_id']); | |
288 } | |
289 cache_clear_all('apachesolr:environments', 'cache_apachesolr'); | |
290 drupal_set_message(t('The %name search environment has been saved.', array('%name' => $form_state['values']['name']))); | |
291 if ($form_state['values']['op'] == t('Save')) { | |
292 $form_state['redirect'] = 'admin/config/search/apachesolr/settings'; | |
293 } | |
294 else { | |
295 $form_state['redirect'] = current_path(); | |
296 } | |
297 // Regardlessly of the destination parameter we want to go to another page | |
298 unset($_GET['destination']); | |
299 drupal_static_reset('drupal_get_destination'); | |
300 drupal_get_destination(); | |
301 } | |
302 | |
303 /** | |
304 * Check to see if the facetapi module is installed, and if not put up | |
305 * a message. | |
306 * | |
307 * Only call this function if the user is already in a position for this to | |
308 * be useful. | |
309 */ | |
310 function apachesolr_check_facetapi() { | |
311 if (!module_exists('facetapi')) { | |
312 $filename = db_query_range("SELECT filename FROM {system} WHERE type = 'module' AND name = 'facetapi'", 0, 1) | |
313 ->fetchField(); | |
314 if ($filename && file_exists($filename)) { | |
315 drupal_set_message(t('If you <a href="@modules">enable the facetapi module</a>, Apache Solr Search will provide you with configurable facets.', array('@modules' => url('admin/modules')))); | |
316 } | |
317 else { | |
318 drupal_set_message(t('If you install the facetapi module from !href, Apache Solr Search will provide you with configurable facets.', array('!href' => url('http://drupal.org/project/facetapi')))); | |
319 } | |
320 } | |
321 } | |
322 | |
323 /** | |
324 * Form builder for general settings used as a menu callback. | |
325 * | |
326 * @param array $form | |
327 * @param array $form_state | |
328 * | |
329 * @return array Output of the system_settings_form() | |
330 */ | |
331 function apachesolr_settings(array $form, array &$form_state) { | |
332 $form = array(); | |
333 $rows = array(); | |
334 | |
335 // Environment settings | |
336 $id = apachesolr_default_environment(); | |
337 $environments = apachesolr_load_all_environments(); | |
338 $default_environment = apachesolr_default_environment(); | |
339 apachesolr_check_facetapi(); | |
340 | |
341 // Reserve a row for the default one | |
342 $rows[$default_environment] = array(); | |
343 | |
344 foreach ($environments as $environment_id => $data) { | |
345 // Define all the Operations | |
346 $confs = array(); | |
347 $ops = array(); | |
348 // Whenever facetapi is enabled we also enable our operation link | |
349 if (module_exists('facetapi')) { | |
350 $confs['facets'] = array( | |
351 'class' => 'operation', | |
352 'data' => l(t('Facets'), | |
353 'admin/config/search/apachesolr/settings/' . $data['env_id'] . '/facets', | |
354 array('query' => array('destination' => current_path())) | |
355 ), | |
356 ); | |
357 } | |
358 // These are our result and bias settings | |
359 if (module_exists('apachesolr_search')) { | |
360 $confs['result_bias'] = array( | |
361 'class' => 'operation', | |
362 'data' => l(t('Bias'), | |
363 'admin/config/search/apachesolr/settings/' . $data['env_id'] . '/bias', | |
364 array('query' => array('destination' => current_path())) | |
365 ), | |
366 ); | |
367 } | |
368 $confs['index'] = array( | |
369 'class' => 'operation', | |
370 'data' => l(t('Index'), | |
371 'admin/config/search/apachesolr/settings/' . $data['env_id'] . '/index' | |
372 ), | |
373 ); | |
374 $ops['edit'] = array( | |
375 'class' => 'operation', | |
376 'data' => l(t('Edit'), | |
377 'admin/config/search/apachesolr/settings/' . $data['env_id'] . '/edit', | |
378 array('query' => array('destination' => current_path())) | |
379 ), | |
380 ); | |
381 | |
382 $ops['clone'] = array( | |
383 'class' => 'operation', | |
384 'data' => l(t('Clone'), | |
385 'admin/config/search/apachesolr/settings/' . $data['env_id'] . '/clone', | |
386 array('query' => array('destination' => $_GET['q'])) | |
387 ), | |
388 ); | |
389 $env_name = l($data['name'], 'admin/config/search/apachesolr/settings/' . $data['env_id'] . '/edit', array('query' => array('destination' => $_GET['q']))); | |
390 | |
391 // Is this row our default environment? | |
392 if ($environment_id == $default_environment) { | |
393 $env_name = t('!environment <em>(Default)</em>', array('!environment' => $env_name)); | |
394 $env_class_row = 'default-environment'; | |
395 } | |
396 else { | |
397 $env_class_row = ''; | |
398 } | |
399 // For every non-default we add a delete link | |
400 // Allow to revert a search environment or to delete it | |
401 $delete_value = ''; | |
402 if (!isset($data['in_code_only'])) { | |
403 if ((isset($data['type']) && $data['type'] == 'Overridden')) { | |
404 $delete_value = array( | |
405 'class' => 'operation', | |
406 'data' => l(t('Revert'), 'admin/config/search/apachesolr/settings/' . $data['env_id'] . '/delete'), | |
407 ); | |
408 } | |
409 // don't allow the deletion of the default environment | |
410 elseif ($environment_id != $default_environment) { | |
411 $delete_value = array( | |
412 'class' => 'operation', | |
413 'data' => l(t('Delete'), 'admin/config/search/apachesolr/settings/' . $data['env_id'] . '/delete'), | |
414 ); | |
415 } | |
416 } | |
417 $ops['delete'] = $delete_value; | |
418 | |
419 // When we are receiving a http POST (so the page does not show) we do not | |
420 // want to check the statusses of any environment | |
421 $class = ''; | |
422 if (empty($form_state['input'])) { | |
423 $class = apachesolr_server_status($data['url'], $data['service_class']) ? 'ok' : 'error'; | |
424 } | |
425 | |
426 $headers = array( | |
427 array('data' => t('Name'), 'colspan' => 2), | |
428 t('URL'), | |
429 array('data' => t('Configuration'), 'colspan' => count($confs)), | |
430 array('data' => t('Operations'), 'colspan' => count($ops)), | |
431 ); | |
432 | |
433 $rows[$environment_id] = array('data' => | |
434 array( | |
435 // Cells | |
436 array( | |
437 'class' => 'status-icon', | |
438 'data' => '<div title="' . $class . '"><span class="element-invisible">' . $class . '</span></div>', | |
439 ), | |
440 array( | |
441 'class' => $env_class_row, | |
442 'data' => $env_name, | |
443 ), | |
444 check_plain($data['url']), | |
445 ), | |
446 'class' => array(drupal_html_class($class)), | |
447 ); | |
448 // Add the links to the page | |
449 $rows[$environment_id]['data'] = array_merge($rows[$environment_id]['data'], $confs); | |
450 $rows[$environment_id]['data'] = array_merge($rows[$environment_id]['data'], $ops); | |
451 } | |
452 | |
453 $form['apachesolr_host_settings']['actions'] = array( | |
454 '#markup' => '<ul class="action-links">' . drupal_render($actions) . '</ul>', | |
455 ); | |
456 $form['apachesolr_host_settings']['table'] = array( | |
457 '#theme' => 'table', | |
458 '#header' => $headers, | |
459 '#rows' => array_values($rows), | |
460 '#attributes' => array('class' => array('admin-apachesolr')), | |
461 ); | |
462 | |
463 $form['advanced'] = array( | |
464 '#type' => 'fieldset', | |
465 '#title' => t('Advanced configuration'), | |
466 '#collapsed' => TRUE, | |
467 '#collapsible' => TRUE, | |
468 ); | |
469 $form['advanced']['apachesolr_set_nodeapi_messages'] = array( | |
470 '#type' => 'radios', | |
471 '#title' => t('Extra help messages for administrators'), | |
472 '#description' => t('Adds notices to a page whenever Drupal changed content that needs reindexing'), | |
473 '#default_value' => variable_get('apachesolr_set_nodeapi_messages', 1), | |
474 '#options' => array(0 => t('Disabled'), 1 => t('Enabled')), | |
475 ); | |
476 | |
477 // Number of Items to index | |
478 $numbers = drupal_map_assoc(array(1, 5, 10, 20, 50, 100, 200)); | |
479 $default_cron_limit = variable_get('apachesolr_cron_limit', 50); | |
480 | |
481 // apachesolr_cron_limit may be overridden in settings.php. If its current | |
482 // value is not among the default set of options, add it. | |
483 if (!isset($numbers[$default_cron_limit])) { | |
484 $numbers[$default_cron_limit] = $default_cron_limit; | |
485 } | |
486 $form['advanced']['apachesolr_cron_limit'] = array( | |
487 '#type' => 'select', | |
488 '#title' => t('Number of items to index per cron run'), | |
489 '#default_value' => $default_cron_limit, | |
490 '#options' => $numbers, | |
491 '#description' => t('Reduce the number of items to prevent timeouts and memory errors while indexing.', array('@cron' => url('admin/reports/status'))) | |
492 ); | |
493 | |
494 $options = array('apachesolr:show_error' => t('Show error message')); | |
495 $system_info = system_get_info('module'); | |
496 if (module_exists('search')) { | |
497 foreach (search_get_info() as $module => $search_info) { | |
498 // Don't allow apachesolr to return results on failure of apachesolr. | |
499 if ($module == 'apachesolr_search') { | |
500 continue; | |
501 } | |
502 $options[$module] = t('Show @name search results', array('@name' => $system_info[$module]['name'])); | |
503 } | |
504 } | |
505 | |
506 $options['apachesolr:show_no_results'] = t('Show no results'); | |
507 $form['advanced']['apachesolr_failure'] = array( | |
508 '#type' => 'select', | |
509 '#title' => t('On failure'), | |
510 '#options' => $options, | |
511 '#default_value' => variable_get('apachesolr_failure', 'apachesolr:show_error'), | |
512 ); | |
513 | |
514 return system_settings_form($form); | |
515 } | |
516 | |
517 /** | |
518 * Gets information about the fields already in solr index. | |
519 * | |
520 * @param array $environment | |
521 * The environment for which we need to ask the status from | |
522 * | |
523 * @return array page render array | |
524 */ | |
525 function apachesolr_status_page($environment = array()) { | |
526 if (empty($environment)) { | |
527 $env_id = apachesolr_default_environment(); | |
528 $environment = apachesolr_environment_load($env_id); | |
529 } | |
530 else { | |
531 $env_id = $environment['env_id']; | |
532 } | |
533 | |
534 // Check for availability | |
535 if (!apachesolr_server_status($environment['url'], $environment['service_class'])) { | |
536 drupal_set_message(t('The server seems to be unavailable. Please verify the server settings at the <a href="!settings_page">settings page</a>', array('!settings_page' => url("admin/config/search/apachesolr/settings/{$environment['env_id']}/edit", array('query' => drupal_get_destination())))), 'warning'); | |
537 return ''; | |
538 } | |
539 | |
540 try { | |
541 $solr = apachesolr_get_solr($environment["env_id"]); | |
542 $solr->clearCache(); | |
543 $data = $solr->getLuke(); | |
544 } | |
545 catch (Exception $e) { | |
546 watchdog('Apache Solr', nl2br(check_plain($e->getMessage())), NULL, WATCHDOG_ERROR); | |
547 drupal_set_message(nl2br(check_plain($e->getMessage())), "warning"); | |
548 $data = new stdClass; | |
549 $data->fields = array(); | |
550 } | |
551 | |
552 $messages = array(); | |
553 if (isset($data->index->numDocs)) { | |
554 try { | |
555 // Collect the stats | |
556 $stats_summary = $solr->getStatsSummary(); | |
557 module_load_include('inc', 'apachesolr', 'apachesolr.index'); | |
558 $status = apachesolr_index_status($environment["env_id"]); | |
559 // We need a schema version greater than beta3. This is mostly to catch | |
560 // people using the Drupal 6 schema. | |
561 if (preg_match('/^drupal-[13]/', $stats_summary['@schema_version'])) { | |
562 $minimum = 'drupal-3.0-beta4'; | |
563 if (version_compare($stats_summary['@schema_version'], $minimum, '<')) { | |
564 drupal_set_message(t('Your schema.xml version is too old. You must update it to at least %minimum and re-index your content.', array('%minimum' => $minimum)), 'error'); | |
565 } | |
566 } | |
567 $pending_msg = $stats_summary['@pending_docs'] ? t('(@pending_docs sent but not yet processed)', $stats_summary) : ''; | |
568 $index_msg = $stats_summary['@index_size'] ? t('(@index_size on disk)', $stats_summary) : ''; | |
569 $indexed_message = t('@num Items !pending !index_msg', array( | |
570 '@num' => $data->index->numDocs, | |
571 '!pending' => $pending_msg, | |
572 '!index_msg' => $index_msg, | |
573 )); | |
574 $messages[] = array(t('Indexed'), $indexed_message); | |
575 | |
576 $remaining_message = t('@items (@percentage% has been sent to the server)', array( | |
577 '@items' => format_plural($status['remaining'], t('1 item'), t('@count items')), | |
578 '@percentage' => ((int)min(100, 100 * ($status['total'] - $status['remaining']) / max(1, $status['total']))), | |
579 ) | |
580 ); | |
581 $messages[] = array(t('Remaining'), $remaining_message); | |
582 | |
583 $messages[] = array(t('Schema'), t('@schema_version', $stats_summary)); | |
584 if (!empty($stats_summary['@core_name'])) { | |
585 $messages[] = array(t('Solr Core Name'), t('@core_name', $stats_summary)); | |
586 } | |
587 $messages[] = array(t('Delay'), t('@autocommit_time before updates are processed.', $stats_summary)); | |
588 $messages[] = array(t('Pending Deletions'), t('@deletes_total', $stats_summary)); | |
589 } | |
590 catch (Exception $e) { | |
591 watchdog('Apache Solr', nl2br(check_plain($e->getMessage())), NULL, WATCHDOG_ERROR); | |
592 } | |
593 } | |
594 if (empty($messages)) { | |
595 $messages[] = array(t('Error'), t('No data was returned from the server. Check your log messages.')); | |
596 } | |
597 // Initializes output with information about which server's setting we are | |
598 // editing, as it is otherwise not transparent to the end user. | |
599 $output['apachesolr_index_action_status'] = array( | |
600 '#prefix' => '<h3>' . t('@environment: Search Index Content', array('@environment' => $environment['name'])) . '</h3>', | |
601 '#theme' => 'table', | |
602 '#header' => array(t('Type'), t('Value')), | |
603 '#rows' => $messages, | |
604 ); | |
605 | |
606 $output['viewmore'] = array( | |
607 '#markup' => l(t('View more details on the search index contents'), 'admin/reports/apachesolr'), | |
608 ); | |
609 | |
610 $write_status = apachesolr_environment_variable_get($env_id, 'apachesolr_read_only', APACHESOLR_READ_WRITE); | |
611 if ($write_status == APACHESOLR_READ_WRITE) { | |
612 $output['index_action_form'] = drupal_get_form('apachesolr_index_action_form', $env_id); | |
613 $output['index_config_form'] = drupal_get_form('apachesolr_index_config_form', $env_id); | |
614 } | |
615 else { | |
616 drupal_set_message(t('Options for deleting and re-indexing are not available because the index is read-only. This can be changed on the <a href="!settings_page">settings page</a>', array('!settings_page' => url('admin/config/search/apachesolr/settings/' . $env_id . '/edit', array('query' => drupal_get_destination())))), 'warning'); | |
617 } | |
618 | |
619 return $output; | |
620 } | |
621 | |
622 /** | |
623 * Get the report, eg.: some statistics and useful data from the Apache Solr index | |
624 * | |
625 * @param array $environment | |
626 * | |
627 * @return array page render array | |
628 */ | |
629 function apachesolr_index_report(array $environment = array()) { | |
630 if (empty($environment)) { | |
631 $env_id = apachesolr_default_environment(); | |
632 drupal_goto('admin/reports/apachesolr/' . $env_id); | |
633 } | |
634 $environments = apachesolr_load_all_environments(); | |
635 $environments_list = array(); | |
636 foreach ($environments as $env) { | |
637 $var_status = array('!name' =>$env['name']); | |
638 $environments_list[] = l(t('Statistics for !name', $var_status), 'admin/reports/apachesolr/' . $env['env_id']); | |
639 } | |
640 $output['environments_list'] = array( | |
641 '#theme' => 'item_list', | |
642 '#items' => $environments_list, | |
643 ); | |
644 | |
645 try { | |
646 $solr = apachesolr_get_solr($environment['env_id']); | |
647 $solr->clearCache(); | |
648 $data = $solr->getLuke(); | |
649 } | |
650 catch (Exception $e) { | |
651 watchdog('Apache Solr', nl2br(check_plain($e->getMessage())), NULL, WATCHDOG_ERROR); | |
652 drupal_set_message(nl2br(check_plain($e->getMessage())), "warning"); | |
653 return $output; | |
654 } | |
655 | |
656 $messages = array(); | |
657 $messages[] = array(t('Number of documents in index'), $data->index->numDocs); | |
658 | |
659 $limit = variable_get('apachesolr_luke_limit', 20000); | |
660 if (isset($data->index->numDocs) && $data->index->numDocs > $limit) { | |
661 $messages[] = array(t('Limit'), t('You have more than @limit documents, so term frequencies are being omitted for performance reasons.', array('@limit' => $limit))); | |
662 $not_found = t('<em>Omitted</em>'); | |
663 } | |
664 elseif (isset($data->index->numDocs)) { | |
665 $not_found = t('Not indexed'); | |
666 try { | |
667 $solr = apachesolr_get_solr($environment['env_id']); | |
668 // Note: we use 2 since 1 fails on Ubuntu Hardy. | |
669 $data = $solr->getLuke(2); | |
670 if (isset($data->index->numTerms)) { | |
671 $messages[] = array(t('# of terms in index'), $data->index->numTerms); | |
672 } | |
673 } | |
674 catch (Exception $e) { | |
675 watchdog('Apache Solr', nl2br(check_plain($e->getMessage())), NULL, WATCHDOG_ERROR); | |
676 $data->fields = array(); | |
677 } | |
678 } | |
679 // Initializes output with information about which server's setting we are | |
680 // editing, as it is otherwise not transparent to the end user. | |
681 $fields = (array)$data->fields; | |
682 if ($fields) { | |
683 $messages[] = array(t('# of fields in index'), count($fields)); | |
684 } | |
685 | |
686 // Output the messages we have for this page | |
687 $output['apachesolr_index_report'] = array( | |
688 '#theme' => 'table', | |
689 '#header' => array('type', 'value'), | |
690 '#rows' => $messages, | |
691 ); | |
692 | |
693 if ($fields) { | |
694 // Initializes table header. | |
695 $header = array( | |
696 'name' => t('Field name'), | |
697 'type' => t('Index type'), | |
698 'terms' => t('Distinct terms'), | |
699 ); | |
700 | |
701 // Builds table rows. | |
702 $rows = array(); | |
703 foreach ($fields as $name => $field) { | |
704 // TODO: try to map the name to something more meaningful. | |
705 $rows[$name] = array( | |
706 'name' => $name, | |
707 'type' => $field->type, | |
708 'terms' => isset($field->distinct) ? $field->distinct : $not_found | |
709 ); | |
710 } | |
711 ksort($rows); | |
712 // Output the fields we found for this environment | |
713 $output['field_table'] = array( | |
714 '#theme' => 'table', | |
715 '#header' => $header, | |
716 '#rows' => $rows, | |
717 ); | |
718 } | |
719 else { | |
720 $output['field_table'] = array('#markup' => t('No data on indexed fields.')); | |
721 } | |
722 return $output; | |
723 } | |
724 | |
725 /** | |
726 * Page callback to show available conf files. | |
727 * | |
728 * @param array $environment | |
729 * | |
730 * @return string | |
731 * A non-render array but plain theme output for the config files overview. Could be done better probably | |
732 */ | |
733 function apachesolr_config_files_overview(array $environment = array()) { | |
734 if (empty($environment)) { | |
735 $env_id = apachesolr_default_environment(); | |
736 } | |
737 else { | |
738 $env_id = $environment['env_id']; | |
739 } | |
740 | |
741 $xml = NULL; | |
742 try { | |
743 $solr = apachesolr_get_solr($env_id); | |
744 $response = $solr->makeServletRequest('admin/file', array('wt' => 'xml')); | |
745 $xml = simplexml_load_string($response->data); | |
746 } | |
747 catch (Exception $e) { | |
748 watchdog('Apache Solr', nl2br(check_plain($e->getMessage())), NULL, WATCHDOG_ERROR); | |
749 drupal_set_message(nl2br(check_plain($e->getMessage())), "warning"); | |
750 } | |
751 | |
752 if ($xml) { | |
753 // Retrieve our items from the xml using xpath | |
754 $items = $xml->xpath('//lst[@name="files"]/lst'); | |
755 | |
756 // Add all the data of the file in a files array | |
757 $files = array(); | |
758 foreach ($items as $item_id => $item) { | |
759 // Do not list directories. Always a bool | |
760 if (isset($item->bool)) { | |
761 break; | |
762 } | |
763 // Get data from the files. | |
764 $name = $item->attributes(); | |
765 $name = ((string)$item->attributes()) ? (string)$item->attributes() : t('No name found'); | |
766 $files[$item_id]['name'] = l($name, 'admin/reports/apachesolr/' . $env_id . '/conf/' . $name); | |
767 | |
768 // Retrieve the date attribute | |
769 if (isset($item->date)) { | |
770 $modified = ((string)$item->date->attributes() == 'modified') ? (string) $item->date : t('No date found'); | |
771 $files[$item_id]['modified'] = format_date(strtotime($modified)); | |
772 } | |
773 | |
774 // Retrieve the size attribute | |
775 if (isset($item->long)) { | |
776 $size = ((string)$item->long->attributes() == 'size') ? (string) $item->long : t('No size found'); | |
777 $files[$item_id]['size'] = t('Size (bytes): @bytes', array('@bytes' => $size)); | |
778 } | |
779 } | |
780 // Sort our files alphabetically | |
781 ksort($files); | |
782 | |
783 // Initializes table header. | |
784 $header = array( | |
785 'name' => t('File name'), | |
786 'date' => t('Modified'), | |
787 'size' => t('Size'), | |
788 ); | |
789 | |
790 // Display the table of field names, index types, and term counts. | |
791 $variables = array( | |
792 'header' => $header, | |
793 'rows' => $files, | |
794 ); | |
795 $output = theme('table', $variables); | |
796 } | |
797 else { | |
798 $output = '<p>' . t('No data about any file found.') . "</p>\n"; | |
799 } | |
800 return $output; | |
801 } | |
802 | |
803 /** | |
804 * Page callback to show one conf file. | |
805 * | |
806 * @param string $name | |
807 * @param array $environment | |
808 * | |
809 * @return string | |
810 * the requested config file | |
811 */ | |
812 function apachesolr_config_file($name, array $environment = array()) { | |
813 if (empty($environment)) { | |
814 $env_id = apachesolr_default_environment(); | |
815 } | |
816 else { | |
817 $env_id = $environment['env_id']; | |
818 } | |
819 | |
820 $output = ''; | |
821 try { | |
822 $solr = apachesolr_get_solr($env_id); | |
823 $response = $solr->makeServletRequest('admin/file', array('file' => $name)); | |
824 $raw_file = $response->data; | |
825 $output = '<pre>' . check_plain($raw_file) . '</pre>'; | |
826 drupal_set_title(check_plain($name)); | |
827 } | |
828 catch (Exception $e) { | |
829 watchdog('Apache Solr', nl2br(check_plain($e->getMessage())), NULL, WATCHDOG_ERROR); | |
830 drupal_set_message(nl2br(check_plain($e->getMessage())), "warning"); | |
831 } | |
832 return $output; | |
833 } | |
834 | |
835 /** | |
836 * Form builder for the Apachesolr Indexer actions form. | |
837 * | |
838 * @param array $form | |
839 * @param array $form_state | |
840 * @param string $env_id | |
841 * The machine name of the environment. | |
842 * @see apachesolr_index_action_form_delete_submit(). | |
843 * | |
844 * @return array $form | |
845 */ | |
846 function apachesolr_index_action_form(array $form, array $form_state, $env_id) { | |
847 $form = array(); | |
848 $form['action'] = array( | |
849 '#type' => 'fieldset', | |
850 '#title' => t('Actions'), | |
851 '#collapsible' => TRUE, | |
852 ); | |
853 | |
854 $form['action']['env_id'] = array( | |
855 '#type' => 'value', | |
856 '#value' => $env_id, | |
857 ); | |
858 | |
859 $form['action']['cron'] = array( | |
860 '#prefix' => '<div>', | |
861 '#type' => 'submit', | |
862 '#value' => t('Index queued content (!amount)', array('!amount' => variable_get('apachesolr_cron_limit', 50))), | |
863 '#submit' => array('apachesolr_index_action_form_cron_submit'), | |
864 ); | |
865 $form['action']['cron_description'] = array( | |
866 '#prefix' => '<span>', | |
867 '#suffix' => '</span></div>', | |
868 '#markup' => t('Indexes just as many items as 1 cron run would do.'), | |
869 ); | |
870 | |
871 $form['action']['remaining'] = array( | |
872 '#prefix' => '<div>', | |
873 '#type' => 'submit', | |
874 '#value' => t('Index all queued content'), | |
875 '#submit' => array('apachesolr_index_action_form_remaining_submit'), | |
876 ); | |
877 $form['action']['remaining_description'] = array( | |
878 '#prefix' => '<span>', | |
879 '#suffix' => '</span></div>', | |
880 '#markup' => t('Could take time and could put an increased load on your server.'), | |
881 ); | |
882 | |
883 $form['action']['reset'] = array( | |
884 '#prefix' => '<div>', | |
885 '#suffix' => '</div>', | |
886 '#type' => 'submit', | |
887 '#value' => t('Queue all content for reindexing'), | |
888 '#submit' => array('apachesolr_index_action_form_reset_submit'), | |
889 ); | |
890 $form['action']['delete'] = array( | |
891 '#prefix' => '<div>', | |
892 '#type' => 'submit', | |
893 '#value' => t('Delete the Search & Solr index'), | |
894 '#submit' => array('apachesolr_index_action_form_delete_submit'), | |
895 ); | |
896 $form['action']['delete_description'] = array( | |
897 '#prefix' => '<span>', | |
898 '#suffix' => '</span></div>', | |
899 '#markup' => t('Useful with a corrupt index or a new schema.xml.'), | |
900 ); | |
901 | |
902 return $form; | |
903 } | |
904 | |
905 /** | |
906 * Submit handler for the Indexer actions form, delete button. | |
907 * | |
908 * @param array $form | |
909 * @param array $form_state | |
910 */ | |
911 function apachesolr_index_action_form_remaining_submit(array $form, array &$form_state) { | |
912 $destination = array(); | |
913 if (isset($_GET['destination'])) { | |
914 $destination = drupal_get_destination(); | |
915 unset($_GET['destination']); | |
916 } | |
917 $env_id = $form_state['values']['env_id']; | |
918 $form_state['redirect'] = array('admin/config/search/apachesolr/settings/' . $env_id . '/index/remaining', array('query' => $destination)); | |
919 } | |
920 | |
921 /** | |
922 * Submit handler for the Indexer actions form, delete button. | |
923 * | |
924 * @param array $form | |
925 * @param array $form_state | |
926 */ | |
927 function apachesolr_index_action_form_delete_submit(array $form, array &$form_state) { | |
928 $destination = array(); | |
929 if (isset($_GET['destination'])) { | |
930 $destination = drupal_get_destination(); | |
931 unset($_GET['destination']); | |
932 } | |
933 $env_id = $form_state['values']['env_id']; | |
934 $form_state['redirect'] = array('admin/config/search/apachesolr/settings/' . $env_id . '/index/delete', array('query' => $destination)); | |
935 } | |
936 | |
937 /** | |
938 * Submit handler for the Indexer actions form, delete button. | |
939 * | |
940 * @param array $form | |
941 * @param array $form_state | |
942 */ | |
943 function apachesolr_index_action_form_reset_submit(array $form, array &$form_state) { | |
944 $destination = array(); | |
945 if (isset($_GET['destination'])) { | |
946 $destination = drupal_get_destination(); | |
947 unset($_GET['destination']); | |
948 } | |
949 $env_id = $form_state['values']['env_id']; | |
950 $form_state['redirect'] = array('admin/config/search/apachesolr/settings/' . $env_id . '/index/reset', array('query' => $destination)); | |
951 } | |
952 | |
953 /** | |
954 * Submit handler for the deletion form. | |
955 * | |
956 * @param array $form | |
957 * @param array $form_state | |
958 */ | |
959 function apachesolr_index_action_form_cron_submit(array $form, array &$form_state) { | |
960 if (!empty($form_state['build_info']['args'][0])) { | |
961 $env_id = $form_state['build_info']['args'][0]; | |
962 $form_state['redirect'] = 'admin/config/search/apachesolr/settings/' . $env_id . '/index'; | |
963 } | |
964 else { | |
965 $env_id = apachesolr_default_environment(); | |
966 $form_state['redirect'] = 'admin/config/search/apachesolr'; | |
967 } | |
968 apachesolr_cron($env_id); | |
969 drupal_set_message(t('Apachesolr cron succesfully executed')); | |
970 } | |
971 | |
972 /** | |
973 * Form builder for to reindex the remaining items left in the queue. | |
974 * | |
975 * @see apachesolr_index_action_form_delete_confirm_submit(). | |
976 * | |
977 * @param array $form | |
978 * @param array $form_state | |
979 * @param array $environment | |
980 * | |
981 * @return mixed | |
982 */ | |
983 function apachesolr_index_action_form_remaining_confirm(array $form, array &$form_state, array $environment) { | |
984 return confirm_form($form, | |
985 t('Are you sure you want index all remaining content?'), | |
986 'admin/config/search/apachesolr/settings/' . $environment['env_id'] . '/index', | |
987 NULL, | |
988 t('Index all remaining') | |
989 ); | |
990 } | |
991 | |
992 /** | |
993 * Submit handler for the deletion form. | |
994 * | |
995 * @param array $form | |
996 * @param array $form_state | |
997 */ | |
998 function apachesolr_index_action_form_remaining_confirm_submit(array $form, array &$form_state) { | |
999 if (!empty($form_state['build_info']['args'][0]['env_id'])) { | |
1000 $env_id = $form_state['build_info']['args'][0]['env_id']; | |
1001 $form_state['redirect'] = 'admin/config/search/apachesolr/settings/' . $env_id . '/index'; | |
1002 } | |
1003 else { | |
1004 $env_id = apachesolr_default_environment(); | |
1005 $form_state['redirect'] = 'admin/config/search/apachesolr'; | |
1006 } | |
1007 apachesolr_index_batch_index_remaining($env_id); | |
1008 } | |
1009 | |
1010 /** | |
1011 * Form builder for the index re-enqueue form. | |
1012 * | |
1013 * @see apachesolr_index_action_form_reset_confirm_submit(). | |
1014 * | |
1015 * @param array $form | |
1016 * @param array $form_state | |
1017 * @param array $environment | |
1018 * | |
1019 * @return mixed | |
1020 */ | |
1021 function apachesolr_index_action_form_reset_confirm(array $form, array &$form_state, array $environment) { | |
1022 return confirm_form($form, | |
1023 t('Are you sure you want to queue content for reindexing?'), | |
1024 'admin/config/search/apachesolr/settings/' . $environment['env_id'] . '/index', | |
1025 t('All content on the site will be queued for indexing. The documents currently in the Solr index will remain searchable.'), | |
1026 t('Queue all content') | |
1027 ); | |
1028 } | |
1029 | |
1030 /** | |
1031 * Submit handler for the deletion form. | |
1032 * | |
1033 * @param array $form | |
1034 * @param array $form_state | |
1035 */ | |
1036 function apachesolr_index_action_form_reset_confirm_submit(array $form, array &$form_state) { | |
1037 if (!empty($form_state['build_info']['args'][0]['env_id'])) { | |
1038 $env_id = $form_state['build_info']['args'][0]['env_id']; | |
1039 $form_state['redirect'] = 'admin/config/search/apachesolr/settings/' . $env_id . '/index'; | |
1040 } | |
1041 else { | |
1042 $env_id = apachesolr_default_environment(); | |
1043 $form_state['redirect'] = 'admin/config/search/apachesolr'; | |
1044 } | |
1045 module_load_include('inc', 'apachesolr', 'apachesolr.index'); | |
1046 apachesolr_index_mark_for_reindex($env_id); | |
1047 drupal_set_message(t('All the content on your site is queued for indexing. You can wait for it to be indexed during cron runs, or you can manually reindex it.')); | |
1048 } | |
1049 | |
1050 /** | |
1051 * Form builder for the index delete/clear form. | |
1052 * | |
1053 * @see apachesolr_index_action_form_delete_confirm_submit(). | |
1054 | |
1055 * @param array $form | |
1056 * @param array $form_state | |
1057 * @param array $environment | |
1058 * | |
1059 * @return array output of confirm_form() | |
1060 */ | |
1061 function apachesolr_index_action_form_delete_confirm(array $form, array &$form_state, array $environment) { | |
1062 return confirm_form($form, | |
1063 t('Are you sure you want to clear your index?'), | |
1064 'admin/config/search/apachesolr/settings/' . $environment['env_id'] . '/index', | |
1065 t('This will remove all data from your index and all search results will be incomplete until your site is reindexed.'), | |
1066 t('Delete index') | |
1067 ); | |
1068 } | |
1069 | |
1070 /** | |
1071 * Submit handler for the deletion form. | |
1072 * | |
1073 * @param array $form | |
1074 * @param array $form_state | |
1075 */ | |
1076 function apachesolr_index_action_form_delete_confirm_submit(array $form, array &$form_state) { | |
1077 if (!empty($form_state['build_info']['args'][0]['env_id'])) { | |
1078 $env_id = $form_state['build_info']['args'][0]['env_id']; | |
1079 $form_state['redirect'] = 'admin/config/search/apachesolr/settings/' . $env_id . '/index'; | |
1080 } | |
1081 else { | |
1082 $env_id = apachesolr_default_environment(); | |
1083 $form_state['redirect'] = 'admin/config/search/apachesolr'; | |
1084 } | |
1085 // Rebuild our tracking table. | |
1086 module_load_include('inc', 'apachesolr', 'apachesolr.index'); | |
1087 apachesolr_index_delete_index($env_id); | |
1088 drupal_set_message(t('The index has been deleted.')); | |
1089 } | |
1090 | |
1091 /** | |
1092 * Submit a batch job to index the remaining, non-indexed content. | |
1093 * | |
1094 * @param string $env_id | |
1095 * The environment ID where it needs to index the remaining items for | |
1096 */ | |
1097 function apachesolr_index_batch_index_remaining($env_id, $total_limit = null) { | |
1098 $batch = array( | |
1099 'operations' => array( | |
1100 array( | |
1101 'apachesolr_index_batch_index_entities', | |
1102 array( | |
1103 $env_id, | |
1104 $total_limit, | |
1105 ), | |
1106 ), | |
1107 ), | |
1108 'finished' => 'apachesolr_index_batch_index_finished', | |
1109 'title' => t('Indexing'), | |
1110 'init_message' => t('Preparing to submit content to Solr for indexing...'), | |
1111 'progress_message' => t('Submitting content to Solr...'), | |
1112 'error_message' => t('Solr indexing has encountered an error.'), | |
1113 'file' => drupal_get_path('module', 'apachesolr') . '/apachesolr.admin.inc', | |
1114 ); | |
1115 batch_set($batch); | |
1116 } | |
1117 | |
1118 | |
1119 /** | |
1120 * Batch Operation Callback | |
1121 * | |
1122 * @param string $env_id | |
1123 * The machine name of the environment. | |
1124 * @param $total_limit | |
1125 * The total number of items to index across all batches | |
1126 * @param array $context | |
1127 * | |
1128 * @return false | |
1129 * return false when an exception was caught | |
1130 * | |
1131 * @throws Exception | |
1132 * When solr gives an error, throw an exception that solr is not available | |
1133 */ | |
1134 function apachesolr_index_batch_index_entities($env_id, $total_limit = NULL, &$context) { | |
1135 module_load_include('inc', 'apachesolr', 'apachesolr.index'); | |
1136 if (empty($context['sandbox'])) { | |
1137 try { | |
1138 // Get the $solr object | |
1139 $solr = apachesolr_get_solr($env_id); | |
1140 // If there is no server available, don't continue. | |
1141 if (!$solr->ping()) { | |
1142 throw new Exception(t('No Solr instance available during indexing.')); | |
1143 } | |
1144 } | |
1145 catch (Exception $e) { | |
1146 watchdog('Apache Solr', $e->getMessage(), NULL, WATCHDOG_ERROR); | |
1147 return FALSE; | |
1148 } | |
1149 | |
1150 $status = apachesolr_index_status($env_id); | |
1151 $context['sandbox']['progress'] = 0; | |
1152 $context['sandbox']['submitted'] = 0; | |
1153 | |
1154 // How many items do we want to index? All or a limited set of items | |
1155 if (empty($total_limit)) { | |
1156 $context['sandbox']['max'] = $status['remaining']; | |
1157 } | |
1158 else { | |
1159 $context['sandbox']['max'] = $total_limit; | |
1160 } | |
1161 } | |
1162 | |
1163 // We can safely process the apachesolr_cron_limit nodes at a time without a | |
1164 // timeout or out of memory error. | |
1165 $limit = variable_get('apachesolr_cron_limit', 50); | |
1166 | |
1167 // Reduce the limit for our final batch if we would be processing more than had been requested | |
1168 if ($limit + $context['sandbox']['progress'] > $context['sandbox']['max']) { | |
1169 $limit = $context['sandbox']['max'] - $context['sandbox']['progress']; | |
1170 } | |
1171 | |
1172 if ($context['sandbox']['max'] >= $context['sandbox']['progress'] + $limit) { | |
1173 $context['sandbox']['progress'] += $limit; | |
1174 } | |
1175 else { | |
1176 $context['sandbox']['progress'] = $context['sandbox']['max']; | |
1177 } | |
1178 $context['sandbox']['submitted'] += apachesolr_index_entities($env_id, $limit); | |
1179 | |
1180 $arguments = array( | |
1181 '@current' => $context['sandbox']['progress'], | |
1182 '@total' => $context['sandbox']['max'], | |
1183 '@submitted' => $context['sandbox']['submitted'], | |
1184 ); | |
1185 $context['message'] = t('Inspected @current of @total entities. Submitted @submitted documents to Solr', $arguments); | |
1186 | |
1187 // Inform the batch engine that we are not finished, and provide an | |
1188 // estimation of the completion level we reached. | |
1189 $context['finished'] = empty($context['sandbox']['max']) ? 1 : $context['sandbox']['progress'] / $context['sandbox']['max']; | |
1190 | |
1191 // Put the total into the results section when we're finished so we can | |
1192 // show it to the admin. | |
1193 if ($context['finished']) { | |
1194 $context['results']['count'] = $context['sandbox']['progress']; | |
1195 $context['results']['submitted'] = $context['sandbox']['submitted']; | |
1196 } | |
1197 } | |
1198 | |
1199 /** | |
1200 * Batch 'finished' callback | |
1201 * | |
1202 * @param bool $success | |
1203 * Whether the batch ended with success or not | |
1204 * @param array $results | |
1205 * @param array $operations | |
1206 */ | |
1207 function apachesolr_index_batch_index_finished($success, array $results, array $operations) { | |
1208 $message = ''; | |
1209 // $results['count'] will not be set if Solr is unavailable. | |
1210 if (isset($results['count'])) { | |
1211 $message .= format_plural($results['count'], '1 item processed successfully. ', '@count items successfully processed. '); | |
1212 } | |
1213 if (isset($results['submitted'])) { | |
1214 $message .= format_plural($results['submitted'], '1 document successfully sent to Solr.', '@count documents successfully sent to Solr.'); | |
1215 } | |
1216 if ($success) { | |
1217 $type = 'status'; | |
1218 } | |
1219 else { | |
1220 // An error occurred. $operations contains the unprocessed operations. | |
1221 $error_operation = reset($operations); | |
1222 $message .= ' ' . t('An error occurred while processing @num with arguments: @args', array('@num' => $error_operation[0], '@args' => print_r($error_operation[0], TRUE))); | |
1223 $type = 'error'; | |
1224 } | |
1225 drupal_set_message($message, $type); | |
1226 } | |
1227 | |
1228 /** | |
1229 * Form builder for the bundle configuration form. | |
1230 * | |
1231 * @see apachesolr_index_config_form_submit(). | |
1232 * | |
1233 * @param array $form | |
1234 * @param array $form_state | |
1235 * @param string $env_id | |
1236 * The machine name of the environment. | |
1237 * | |
1238 * @return array $form | |
1239 */ | |
1240 function apachesolr_index_config_form(array $form, array $form_state, $env_id) { | |
1241 $form['config'] = array( | |
1242 '#type' => 'fieldset', | |
1243 '#title' => t('Configuration'), | |
1244 '#collapsible' => TRUE, | |
1245 ); | |
1246 | |
1247 $form['config']['bundles'] = array( | |
1248 '#type' => 'markup', | |
1249 '#markup' => t('Select the entity types and bundles that should be indexed.'), | |
1250 ); | |
1251 | |
1252 // For future extensibility, when we have multiple cores. | |
1253 $form['config']['env_id'] = array( | |
1254 '#type' => 'value', | |
1255 '#value' => $env_id, | |
1256 ); | |
1257 | |
1258 foreach (entity_get_info() as $entity_type => $entity_info) { | |
1259 if (!empty($entity_info['apachesolr']['indexable'])) { | |
1260 $options = array(); | |
1261 foreach ($entity_info['bundles'] as $key => $info) { | |
1262 $options[$key] = $info['label']; | |
1263 } | |
1264 | |
1265 $form['config']['entities']['#tree'] = TRUE; | |
1266 $form['config']['entities'][$entity_type] = array( | |
1267 '#type' => 'checkboxes', | |
1268 '#title' => check_plain($entity_info['label']), | |
1269 '#options' => $options, | |
1270 '#default_value' => apachesolr_get_index_bundles($env_id, $entity_type), | |
1271 ); | |
1272 } | |
1273 } | |
1274 | |
1275 $form['config']['submit'] = array('#type' => 'submit', '#value' => t('Save')); | |
1276 | |
1277 return $form; | |
1278 } | |
1279 | |
1280 /** | |
1281 * Submit handler for the bundle configuration form. | |
1282 * | |
1283 * @param array $form | |
1284 * @param array $form_state | |
1285 */ | |
1286 function apachesolr_index_config_form_submit(array $form, array &$form_state) { | |
1287 module_load_include('inc', 'apachesolr', 'apachesolr.index'); | |
1288 $form_values = $form_state['values']; | |
1289 $env_id = $form_values['env_id']; | |
1290 | |
1291 foreach ($form_values['entities'] as $entity_type => $bundles) { | |
1292 $existing_bundles = apachesolr_get_index_bundles($env_id, $entity_type); | |
1293 $all_bundles = array_keys($bundles); | |
1294 $new_bundles = array_values(array_filter($bundles)); | |
1295 apachesolr_index_set_bundles($env_id, $entity_type, $new_bundles); | |
1296 | |
1297 // Remove all excluded bundles - this happens on form submit | |
1298 // even if there is no change so the admin can remove | |
1299 // bundles if there was an error. | |
1300 $excluded_bundles = array_diff($all_bundles, $new_bundles); | |
1301 if (apachesolr_index_delete_bundles($env_id, $entity_type, $excluded_bundles)) { | |
1302 $callback = apachesolr_entity_get_callback($entity_type, 'bundles changed callback'); | |
1303 if (!empty($callback)) { | |
1304 call_user_func($callback, $env_id, $existing_bundles, $new_bundles); | |
1305 } | |
1306 } | |
1307 else { | |
1308 drupal_set_message(t('Search is temporarily unavailable. If the problem persists, please contact the site administrator.'), 'error'); | |
1309 } | |
1310 } | |
1311 | |
1312 // Clear the entity cache, since we will be changing its data. | |
1313 entity_info_cache_clear(); | |
1314 cache_clear_all('apachesolr:environments', 'cache_apachesolr'); | |
1315 drupal_set_message(t('Your settings have been saved.')); | |
1316 } | |
1317 | |
1318 /** | |
1319 * Page callback for node/%node/devel/apachesolr. | |
1320 * | |
1321 * @param object $node | |
1322 * @return string debugging information | |
1323 */ | |
1324 function apachesolr_devel($node) { | |
1325 module_load_include('inc', 'apachesolr', 'apachesolr.index'); | |
1326 $item = new stdClass(); | |
1327 $item->entity_type = 'node'; | |
1328 $item->entity_id = $node->nid; | |
1329 $output = ''; | |
1330 foreach (apachesolr_load_all_environments() as $env_id => $environment) { | |
1331 $documents = apachesolr_index_entity_to_documents($item, $env_id); | |
1332 $output .= '<h1>' . t('Environment %name (%env_id)', array('%name' => $environment['name'], '%env_id' => $env_id)). '</h1>'; | |
1333 foreach ($documents as $document) { | |
1334 $debug_data = array(); | |
1335 foreach ($document as $key => $value) { | |
1336 $debug_data[$key] = $value; | |
1337 } | |
1338 $output .= kdevel_print_object($debug_data); | |
1339 } | |
1340 } | |
1341 return $output; | |
1342 } |