Mercurial > hg > solrsearch
comparison solrsearch.admin.inc @ 0:a2b4f67e73dc default tip
initial
author | Dirk Wintergruen <dwinter@mpiwg-berlin.mpg.de> |
---|---|
date | Mon, 08 Jun 2015 10:21:54 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:a2b4f67e73dc |
---|---|
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 solrsearch_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/solrsearch', | |
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 solrsearch_environment_delete_form_submit(array $form, array &$form_state) { | |
45 if (solrsearch_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/solrsearch/settings'; | |
49 } | |
50 | |
51 function solrsearch_environment_edit_delete_submit($form, &$form_state) { | |
52 $form_state['redirect'] = 'admin/config/search/solrsearch/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 solrsearch_environment_settings_page(array $environment = array()) { | |
68 if (empty($environment)) { | |
69 $env_id = solrsearch_default_environment(); | |
70 $environment = solrsearch_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 'solrsearch_environment' => array( | |
78 '#theme' => 'solrsearch_settings_title', | |
79 '#env_id' => $env_id, | |
80 ), | |
81 ); | |
82 $output['form'] = drupal_get_form('solrsearch_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 solrsearch_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/solrsearch', | |
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 solrsearch_environment_clone_form_submit(array $form, array &$form_state) { | |
117 if (solrsearch_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/solrsearch/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 solrsearch_environment_clone_submit(array $form, array &$form_state) { | |
130 $form_state['redirect'] = 'admin/config/search/solrsearch/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 solrsearch_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'] == solrsearch_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' => 'solrsearch_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']['solrsearch_read_only'] = array( | |
182 '#type' => 'radios', | |
183 '#title' => t('Index write access'), | |
184 '#default_value' => isset($environment['conf']['solrsearch_read_only']) ? $environment['conf']['solrsearch_read_only'] : solrsearch_READ_WRITE, | |
185 '#options' => array(solrsearch_READ_WRITE => t('Read and write (normal)'), solrsearch_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('solrsearch_environment_edit_validate'), | |
194 '#submit' => array('solrsearch_environment_edit_submit'), | |
195 '#value' => t('Save'), | |
196 ); | |
197 $form['actions']['save_edit'] = array( | |
198 '#type' => 'submit', | |
199 '#validate' => array('solrsearch_environment_edit_validate'), | |
200 '#submit' => array('solrsearch_environment_edit_submit'), | |
201 '#value' => t('Save and edit'), | |
202 ); | |
203 $form['actions']['test'] = array( | |
204 '#type' => 'submit', | |
205 '#validate' => array('solrsearch_environment_edit_validate'), | |
206 '#submit' => array('solrsearch_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('solrsearch_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/solrsearch/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 solrsearch_environment_edit_test_submit(array $form, array &$form_state) { | |
240 $ping = solrsearch_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 solrsearch_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 solrsearch_environment_edit_submit(array $form, array &$form_state) { | |
285 solrsearch_environment_save($form_state['values']); | |
286 if (!empty($form_state['values']['make_default'])) { | |
287 solrsearch_set_default_environment($form_state['values']['env_id']); | |
288 } | |
289 cache_clear_all('solrsearch:environments', 'cache_solrsearch'); | |
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/solrsearch/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 solrsearch_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 solrsearch_settings(array $form, array &$form_state) { | |
332 $form = array(); | |
333 $rows = array(); | |
334 | |
335 // Environment settings // Take environment settings form solrsearch | |
336 $id = solrsearch_default_environment(); | |
337 | |
338 $environments = solrsearch_load_all_environments(); | |
339 $default_environment = solrsearch_default_environment(); | |
340 solrsearch_check_facetapi(); | |
341 | |
342 // Reserve a row for the default one | |
343 $rows[$default_environment] = array(); | |
344 | |
345 foreach ($environments as $environment_id => $data) { | |
346 // Define all the Operations | |
347 $confs = array(); | |
348 $ops = array(); | |
349 // Whenever facetapi is enabled we also enable our operation link | |
350 if (module_exists('facetapi')) { | |
351 $confs['facets'] = array( | |
352 'class' => 'operation', | |
353 'data' => l(t('Facets'), | |
354 'admin/config/search/solrsearch/settings/' . $data['env_id'] . '/facets', | |
355 array('query' => array('destination' => current_path())) | |
356 ), | |
357 ); | |
358 } | |
359 // These are our result and bias settings | |
360 if (module_exists('solrsearch_search')) { | |
361 $confs['result_bias'] = array( | |
362 'class' => 'operation', | |
363 'data' => l(t('Bias'), | |
364 'admin/config/search/solrsearch/settings/' . $data['env_id'] . '/bias', | |
365 array('query' => array('destination' => current_path())) | |
366 ), | |
367 ); | |
368 } | |
369 | |
370 $ops['edit'] = array( | |
371 'class' => 'operation', | |
372 'data' => l(t('Edit'), | |
373 'admin/config/search/solrsearch/settings/' . $data['env_id'] . '/edit', | |
374 array('query' => array('destination' => current_path())) | |
375 ), | |
376 ); | |
377 | |
378 $ops['clone'] = array( | |
379 'class' => 'operation', | |
380 'data' => l(t('Clone'), | |
381 'admin/config/search/solrsearch/settings/' . $data['env_id'] . '/clone', | |
382 array('query' => array('destination' => $_GET['q'])) | |
383 ), | |
384 ); | |
385 $env_name = l($data['name'], 'admin/config/search/solrsearch/settings/' . $data['env_id'] . '/edit', array('query' => array('destination' => $_GET['q']))); | |
386 | |
387 // Is this row our default environment? | |
388 if ($environment_id == $default_environment) { | |
389 $env_name = t('!environment <em>(Default)</em>', array('!environment' => $env_name)); | |
390 $env_class_row = 'default-environment'; | |
391 } | |
392 else { | |
393 $env_class_row = ''; | |
394 } | |
395 // For every non-default we add a delete link | |
396 // Allow to revert a search environment or to delete it | |
397 $delete_value = ''; | |
398 if (!isset($data['in_code_only'])) { | |
399 if ((isset($data['type']) && $data['type'] == 'Overridden')) { | |
400 $delete_value = array( | |
401 'class' => 'operation', | |
402 'data' => l(t('Revert'), 'admin/config/search/solrsearch/settings/' . $data['env_id'] . '/delete'), | |
403 ); | |
404 } | |
405 // don't allow the deletion of the default environment | |
406 elseif ($environment_id != $default_environment) { | |
407 $delete_value = array( | |
408 'class' => 'operation', | |
409 'data' => l(t('Delete'), 'admin/config/search/solrsearch/settings/' . $data['env_id'] . '/delete'), | |
410 ); | |
411 } | |
412 } | |
413 $ops['delete'] = $delete_value; | |
414 | |
415 // When we are receiving a http POST (so the page does not show) we do not | |
416 // want to check the statusses of any environment | |
417 $class = ''; | |
418 if (empty($form_state['input'])) { | |
419 $class = solrsearch_server_status($data['url'], $data['service_class']) ? 'ok' : 'error'; | |
420 } | |
421 | |
422 $headers = array( | |
423 array('data' => t('Name'), 'colspan' => 2), | |
424 t('URL'), | |
425 array('data' => t('Configuration'), 'colspan' => count($confs)), | |
426 array('data' => t('Operations'), 'colspan' => count($ops)), | |
427 ); | |
428 | |
429 $rows[$environment_id] = array('data' => | |
430 array( | |
431 // Cells | |
432 array( | |
433 'class' => 'status-icon', | |
434 'data' => '<div title="' . $class . '"><span class="element-invisible">' . $class . '</span></div>', | |
435 ), | |
436 array( | |
437 'class' => $env_class_row, | |
438 'data' => $env_name, | |
439 ), | |
440 check_plain($data['url']), | |
441 ), | |
442 'class' => array(drupal_html_class($class)), | |
443 ); | |
444 // Add the links to the page | |
445 $rows[$environment_id]['data'] = array_merge($rows[$environment_id]['data'], $confs); | |
446 $rows[$environment_id]['data'] = array_merge($rows[$environment_id]['data'], $ops); | |
447 } | |
448 | |
449 $form['solrsearch_host_settings']['actions'] = array( | |
450 '#markup' => '<ul class="action-links">' . drupal_render($actions) . '</ul>', | |
451 ); | |
452 $form['solrsearch_host_settings']['table'] = array( | |
453 '#theme' => 'table', | |
454 '#header' => $headers, | |
455 '#rows' => array_values($rows), | |
456 '#attributes' => array('class' => array('admin-solrsearch')), | |
457 ); | |
458 | |
459 $form['advanced'] = array( | |
460 '#type' => 'fieldset', | |
461 '#title' => t('Advanced configuration'), | |
462 '#collapsed' => TRUE, | |
463 '#collapsible' => TRUE, | |
464 ); | |
465 $form['advanced']['solrsearch_set_nodeapi_messages'] = array( | |
466 '#type' => 'radios', | |
467 '#title' => t('Extra help messages for administrators'), | |
468 '#description' => t('Adds notices to a page whenever Drupal changed content that needs reindexing'), | |
469 '#default_value' => variable_get('solrsearch_set_nodeapi_messages', 1), | |
470 '#options' => array(0 => t('Disabled'), 1 => t('Enabled')), | |
471 ); | |
472 | |
473 // Number of Items to index | |
474 $numbers = drupal_map_assoc(array(1, 5, 10, 20, 50, 100, 200)); | |
475 $default_cron_limit = variable_get('solrsearch_cron_limit', 50); | |
476 | |
477 // solrsearch_cron_limit may be overridden in settings.php. If its current | |
478 // value is not among the default set of options, add it. | |
479 if (!isset($numbers[$default_cron_limit])) { | |
480 $numbers[$default_cron_limit] = $default_cron_limit; | |
481 } | |
482 $form['advanced']['solrsearch_cron_limit'] = array( | |
483 '#type' => 'select', | |
484 '#title' => t('Number of items to index per cron run'), | |
485 '#default_value' => $default_cron_limit, | |
486 '#options' => $numbers, | |
487 '#description' => t('Reduce the number of items to prevent timeouts and memory errors while indexing.', array('@cron' => url('admin/reports/status'))) | |
488 ); | |
489 | |
490 $options = array('solrsearch:show_error' => t('Show error message')); | |
491 $system_info = system_get_info('module'); | |
492 if (module_exists('search')) { | |
493 foreach (search_get_info() as $module => $search_info) { | |
494 // Don't allow solrsearch to return results on failure of solrsearch. | |
495 if ($module == 'solrsearch_search') { | |
496 continue; | |
497 } | |
498 $options[$module] = t('Show @name search results', array('@name' => $system_info[$module]['name'])); | |
499 } | |
500 } | |
501 | |
502 $options['solrsearch:show_no_results'] = t('Show no results'); | |
503 $form['advanced']['solrsearch_failure'] = array( | |
504 '#type' => 'select', | |
505 '#title' => t('On failure'), | |
506 '#options' => $options, | |
507 '#default_value' => variable_get('solrsearch_failure', 'solrsearch:show_error'), | |
508 ); | |
509 | |
510 return system_settings_form($form); | |
511 } | |
512 | |
513 /** | |
514 * Gets information about the fields already in solr index. | |
515 * | |
516 * @param array $environment | |
517 * The environment for which we need to ask the status from | |
518 * | |
519 * @return array page render array | |
520 */ | |
521 function solrsearch_status_page($environment = array()) { | |
522 | |
523 if (empty($environment)) { | |
524 | |
525 $env_id = solrsearch_default_environment(); | |
526 $environment = solrsearch_environment_load($env_id); | |
527 } | |
528 else { | |
529 $env_id = $environment['env_id']; | |
530 } | |
531 | |
532 // Check for availability | |
533 if (!solrsearch_server_status($environment['url'], $environment['service_class'])) { | |
534 | |
535 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/solrsearch/settings/{$environment['env_id']}/edit", array('query' => drupal_get_destination())))), 'warning'); | |
536 return ''; | |
537 } | |
538 | |
539 try { | |
540 $solr = solrsearch_get_solr($environment["env_id"]); | |
541 $solr->clearCache(); | |
542 $data = $solr->getLuke(); | |
543 } | |
544 catch (Exception $e) { | |
545 watchdog('Apache Solr', nl2br(check_plain($e->getMessage())), NULL, WATCHDOG_ERROR); | |
546 drupal_set_message(nl2br(check_plain($e->getMessage())), "warning"); | |
547 $data = new stdClass; | |
548 $data->fields = array(); | |
549 } | |
550 | |
551 $messages = array(); | |
552 | |
553 // Initializes output with information about which server's setting we are | |
554 // editing, as it is otherwise not transparent to the end user. | |
555 $output['solrsearch_index_action_status'] = array( | |
556 '#prefix' => '<h3>' . t('@environment: Search Index Content', array('@environment' => $environment['name'])) . '</h3>', | |
557 '#theme' => 'table', | |
558 '#header' => array(t('Type'), t('Value')), | |
559 '#rows' => $messages, | |
560 ); | |
561 | |
562 $output['viewmore'] = array( | |
563 '#markup' => l(t('View more details on the search index contents'), 'admin/reports/solrsearch'), | |
564 ); | |
565 | |
566 $write_status = solrsearch_environment_variable_get($env_id, 'solrsearch_read_only', solrsearch_READ_WRITE); | |
567 if ($write_status == solrsearch_READ_WRITE) { | |
568 /*$output['index_action_form'] = drupal_get_form('solrsearch_index_action_form', $env_id); | |
569 $output['index_config_form'] = drupal_get_form('solrsearch_index_config_form', $env_id);*/ | |
570 } | |
571 else { | |
572 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/solrsearch/settings/' . $env_id . '/edit', array('query' => drupal_get_destination())))), 'warning'); | |
573 } | |
574 | |
575 return $output; | |
576 } | |
577 | |
578 /** | |
579 * Get the report, eg.: some statistics and useful data from the Apache Solr index | |
580 * | |
581 * @param array $environment | |
582 * | |
583 * @return array page render array | |
584 */ | |
585 function solrsearch_index_report(array $environment = array()) { | |
586 if (empty($environment)) { | |
587 $env_id = solrsearch_default_environment(); | |
588 drupal_goto('admin/reports/solrsearch/' . $env_id); | |
589 } | |
590 $environments = solrsearch_load_all_environments(); | |
591 $environments_list = array(); | |
592 foreach ($environments as $env) { | |
593 $var_status = array('!name' =>$env['name']); | |
594 $environments_list[] = l(t('Statistics for !name', $var_status), 'admin/reports/solrsearch/' . $env['env_id']); | |
595 } | |
596 $output['environments_list'] = array( | |
597 '#theme' => 'item_list', | |
598 '#items' => $environments_list, | |
599 ); | |
600 | |
601 try { | |
602 $solr = solrsearch_get_solr($environment['env_id']); | |
603 $solr->clearCache(); | |
604 $data = $solr->getLuke(); | |
605 } | |
606 catch (Exception $e) { | |
607 watchdog('Apache Solr', nl2br(check_plain($e->getMessage())), NULL, WATCHDOG_ERROR); | |
608 drupal_set_message(nl2br(check_plain($e->getMessage())), "warning"); | |
609 return $output; | |
610 } | |
611 | |
612 $messages = array(); | |
613 $messages[] = array(t('Number of documents in index'), $data->index->numDocs); | |
614 | |
615 $limit = variable_get('solrsearch_luke_limit', 20000); | |
616 if (isset($data->index->numDocs) && $data->index->numDocs > $limit) { | |
617 $messages[] = array(t('Limit'), t('You have more than @limit documents, so term frequencies are being omitted for performance reasons.', array('@limit' => $limit))); | |
618 $not_found = t('<em>Omitted</em>'); | |
619 } | |
620 elseif (isset($data->index->numDocs)) { | |
621 $not_found = t('Not indexed'); | |
622 try { | |
623 $solr = solrsearch_get_solr($environment['env_id']); | |
624 // Note: we use 2 since 1 fails on Ubuntu Hardy. | |
625 $data = $solr->getLuke(2); | |
626 if (isset($data->index->numTerms)) { | |
627 $messages[] = array(t('# of terms in index'), $data->index->numTerms); | |
628 } | |
629 } | |
630 catch (Exception $e) { | |
631 watchdog('Apache Solr', nl2br(check_plain($e->getMessage())), NULL, WATCHDOG_ERROR); | |
632 $data->fields = array(); | |
633 } | |
634 } | |
635 // Initializes output with information about which server's setting we are | |
636 // editing, as it is otherwise not transparent to the end user. | |
637 $fields = (array)$data->fields; | |
638 if ($fields) { | |
639 $messages[] = array(t('# of fields in index'), count($fields)); | |
640 } | |
641 | |
642 // Output the messages we have for this page | |
643 $output['solrsearch_index_report'] = array( | |
644 '#theme' => 'table', | |
645 '#header' => array('type', 'value'), | |
646 '#rows' => $messages, | |
647 ); | |
648 | |
649 if ($fields) { | |
650 // Initializes table header. | |
651 $header = array( | |
652 'name' => t('Field name'), | |
653 'type' => t('Index type'), | |
654 'terms' => t('Distinct terms'), | |
655 ); | |
656 | |
657 // Builds table rows. | |
658 $rows = array(); | |
659 foreach ($fields as $name => $field) { | |
660 // TODO: try to map the name to something more meaningful. | |
661 $rows[$name] = array( | |
662 'name' => $name, | |
663 'type' => $field->type, | |
664 'terms' => isset($field->distinct) ? $field->distinct : $not_found | |
665 ); | |
666 } | |
667 ksort($rows); | |
668 // Output the fields we found for this environment | |
669 $output['field_table'] = array( | |
670 '#theme' => 'table', | |
671 '#header' => $header, | |
672 '#rows' => $rows, | |
673 ); | |
674 } | |
675 else { | |
676 $output['field_table'] = array('#markup' => t('No data on indexed fields.')); | |
677 } | |
678 return $output; | |
679 } | |
680 | |
681 /** | |
682 * Page callback to show available conf files. | |
683 * | |
684 * @param array $environment | |
685 * | |
686 * @return string | |
687 * A non-render array but plain theme output for the config files overview. Could be done better probably | |
688 */ | |
689 function solrsearch_config_files_overview(array $environment = array()) { | |
690 if (empty($environment)) { | |
691 $env_id = solrsearch_default_environment(); | |
692 } | |
693 else { | |
694 $env_id = $environment['env_id']; | |
695 } | |
696 | |
697 $xml = NULL; | |
698 try { | |
699 $solr = solrsearch_get_solr($env_id); | |
700 $response = $solr->makeServletRequest('admin/file', array('wt' => 'xml')); | |
701 $xml = simplexml_load_string($response->data); | |
702 } | |
703 catch (Exception $e) { | |
704 watchdog('Apache Solr', nl2br(check_plain($e->getMessage())), NULL, WATCHDOG_ERROR); | |
705 drupal_set_message(nl2br(check_plain($e->getMessage())), "warning"); | |
706 } | |
707 | |
708 if ($xml) { | |
709 // Retrieve our items from the xml using xpath | |
710 $items = $xml->xpath('//lst[@name="files"]/lst'); | |
711 | |
712 // Add all the data of the file in a files array | |
713 $files = array(); | |
714 foreach ($items as $item_id => $item) { | |
715 // Do not list directories. Always a bool | |
716 if (isset($item->bool)) { | |
717 break; | |
718 } | |
719 // Get data from the files. | |
720 $name = $item->attributes(); | |
721 $name = ((string)$item->attributes()) ? (string)$item->attributes() : t('No name found'); | |
722 $files[$item_id]['name'] = l($name, 'admin/reports/solrsearch/' . $env_id . '/conf/' . $name); | |
723 | |
724 // Retrieve the date attribute | |
725 if (isset($item->date)) { | |
726 $modified = ((string)$item->date->attributes() == 'modified') ? (string) $item->date : t('No date found'); | |
727 $files[$item_id]['modified'] = format_date(strtotime($modified)); | |
728 } | |
729 | |
730 // Retrieve the size attribute | |
731 if (isset($item->long)) { | |
732 $size = ((string)$item->long->attributes() == 'size') ? (string) $item->long : t('No size found'); | |
733 $files[$item_id]['size'] = t('Size (bytes): @bytes', array('@bytes' => $size)); | |
734 } | |
735 } | |
736 // Sort our files alphabetically | |
737 ksort($files); | |
738 | |
739 // Initializes table header. | |
740 $header = array( | |
741 'name' => t('File name'), | |
742 'date' => t('Modified'), | |
743 'size' => t('Size'), | |
744 ); | |
745 | |
746 // Display the table of field names, index types, and term counts. | |
747 $variables = array( | |
748 'header' => $header, | |
749 'rows' => $files, | |
750 ); | |
751 $output = theme('table', $variables); | |
752 } | |
753 else { | |
754 $output = '<p>' . t('No data about any file found.') . "</p>\n"; | |
755 } | |
756 return $output; | |
757 } | |
758 | |
759 /** | |
760 * Page callback to show one conf file. | |
761 * | |
762 * @param string $name | |
763 * @param array $environment | |
764 * | |
765 * @return string | |
766 * the requested config file | |
767 */ | |
768 function solrsearch_config_file($name, array $environment = array()) { | |
769 if (empty($environment)) { | |
770 $env_id = solrsearch_default_environment(); | |
771 } | |
772 else { | |
773 $env_id = $environment['env_id']; | |
774 } | |
775 | |
776 $output = ''; | |
777 try { | |
778 $solr = solrsearch_get_solr($env_id); | |
779 $response = $solr->makeServletRequest('admin/file', array('file' => $name)); | |
780 $raw_file = $response->data; | |
781 $output = '<pre>' . check_plain($raw_file) . '</pre>'; | |
782 drupal_set_title(check_plain($name)); | |
783 } | |
784 catch (Exception $e) { | |
785 watchdog('Apache Solr', nl2br(check_plain($e->getMessage())), NULL, WATCHDOG_ERROR); | |
786 drupal_set_message(nl2br(check_plain($e->getMessage())), "warning"); | |
787 } | |
788 return $output; | |
789 } | |
790 | |
791 | |
792 | |
793 /** | |
794 * Page callback for node/%node/devel/solrsearch. | |
795 * | |
796 * @param object $node | |
797 * @return string debugging information | |
798 */ | |
799 function solrsearch_devel($node) { | |
800 module_load_include('inc', 'solrsearch', 'solrsearch.index'); | |
801 $item = new stdClass(); | |
802 $item->entity_type = 'node'; | |
803 $item->entity_id = $node->nid; | |
804 $output = ''; | |
805 foreach (solrsearch_load_all_environments() as $env_id => $environment) { | |
806 $documents = solrsearch_index_entity_to_documents($item, $env_id); | |
807 $output .= '<h1>' . t('Environment %name (%env_id)', array('%name' => $environment['name'], '%env_id' => $env_id)). '</h1>'; | |
808 foreach ($documents as $document) { | |
809 $debug_data = array(); | |
810 foreach ($document as $key => $value) { | |
811 $debug_data[$key] = $value; | |
812 } | |
813 $output .= kdevel_print_object($debug_data); | |
814 } | |
815 } | |
816 return $output; | |
817 } |