123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- <?php
- /**
- * @file
- * Provides a Solr-based service class for the Search API.
- */
- /**
- * Implements hook_menu().
- */
- function search_api_solr_menu() {
- $items['admin/config/search/search_api/server/%search_api_server/files'] = array(
- 'title' => 'Files',
- 'description' => 'View Solr configuration files.',
- 'page callback' => 'drupal_get_form',
- 'page arguments' => array('search_api_solr_solr_config_form', 5),
- 'access callback' => 'search_api_access_server_files',
- 'access arguments' => array(5),
- 'file' => 'search_api_solr.admin.inc',
- 'type' => MENU_LOCAL_TASK,
- 'weight' => -1,
- );
- return $items;
- }
- /**
- * Implements hook_search_api_service_info().
- */
- function search_api_solr_search_api_service_info() {
- $services['search_api_solr_service'] = array(
- 'name' => t('Solr service'),
- 'description' => t('<p>Index items using an Apache Solr search server.</p>' .
- '<ul>' . '<li>All field types are supported and indexed in a special way, with URI/String and Integer/Duration being equivalent.</li>' .
- '<li>See <a href="@url">the Solr wiki</a> for information about the "direct" parse mode.</li>' .
- '<li>Supports the search_api_facets and search_api_multi features.</li>' .
- '<li>Will use internal Solr preprocessors, so Search API preprocessors should for the most part be deactivated.</li>' .
- '<li>See the README.txt file provided with this module for details.</li>' . '</ul>',
- array('@url' => url('http://wiki.apache.org/solr/SolrQuerySyntax'))),
- 'class' => 'SearchApiSolrService',
- );
- return $services;
- }
- /**
- * Implements hook_help().
- */
- function search_api_solr_help($path, array $arg = array()) {
- if ($path == 'admin/config/search/search_api') {
- // Included because we need the REQUIREMENT_* constants.
- include_once(DRUPAL_ROOT . '/includes/install.inc');
- module_load_include('install', 'search_api_solr');
- $reqs = search_api_solr_requirements('runtime');
- foreach ($reqs as $req) {
- if (isset($req['description'])) {
- $type = $req['severity'] == REQUIREMENT_ERROR ? 'error' : ($req['severity'] == REQUIREMENT_WARNING ? 'warning' : 'status');
- drupal_set_message($req['description'], $type);
- }
- }
- }
- elseif ($path == 'admin/config/search/search_api/server/%' && !empty($arg[5])) {
- $server = search_api_server_load($arg[5]);
- if ($server && $server->enabled && $server->class == 'search_api_solr_service') {
- $ping = $server->ping();
- $type = $ping ? 'status' : 'error';
- if ($ping) {
- $msg = t('The Solr server could be reached (latency: @millisecs ms).', array('@millisecs' => $ping * 1000));
- }
- else {
- $msg = t('The Solr server could not be reached.');
- }
- drupal_set_message($msg, $type);
- }
- }
- }
- /**
- * Implements hook_cron().
- *
- * Used to execute an optimization operation on all enabled Solr servers once a
- * day.
- */
- function search_api_solr_cron() {
- if (REQUEST_TIME - variable_get('search_api_solr_last_optimize', 0) > 86400) {
- variable_set('search_api_solr_last_optimize', REQUEST_TIME);
- $conditions = array('class' => 'search_api_solr_service', 'enabled' => TRUE);
- foreach (search_api_server_load_multiple(FALSE, $conditions) as $server) {
- try {
- $server->getSolrConnection()->optimize(FALSE);
- }
- catch(Exception $e) {
- watchdog_exception('search_api_solr', $e, '%type while optimizing Solr server @server: !message in %function (line %line of %file).', array('@server' => $server->name));
- }
- }
- }
- }
- /**
- * Implements hook_flush_caches().
- */
- function search_api_solr_flush_caches() {
- return array('cache_search_api_solr');
- }
- /**
- * Implements hook_search_api_server_update().
- */
- function search_api_solr_search_api_server_update(SearchApiServer $server) {
- if ($server->class === 'search_api_solr_service') {
- $server->getSolrConnection()->clearCache();
- }
- }
- /**
- * Retrieves a list of all config files of a server.
- *
- * @param SearchApiServer $server
- * The Solr server whose files should be retrieved.
- * @param string $dir_name
- * (optional) The directory that should be searched for files. Defaults to the
- * root config directory.
- *
- * @return array
- * An associative array of all config files in the given directory. The keys
- * are the file names, values are arrays with information about the file. The
- * files are returned in alphabetical order and breadth-first.
- *
- * @throws SearchApiException
- * If a problem occurred while retrieving the files.
- */
- function search_api_solr_server_get_files(SearchApiServer $server, $dir_name = NULL) {
- $response = $server->getFile($dir_name);
- // Search for directories and recursively merge directory files.
- $files_data = json_decode($response->data, TRUE);
- $files_list = $files_data['files'];
- $result = array('' => array());
- foreach ($files_list as $file_name => $file_info) {
- if (empty($file_info['directory'])) {
- $result[''][$file_name] = $file_info;
- }
- else {
- $result[$file_name] = search_api_solr_server_get_files($server, $file_name);
- }
- }
- ksort($result);
- ksort($result['']);
- return array_reduce($result, 'array_merge', array());
- }
- /**
- * Access callback for a server's "Files" tab.
- *
- * Grants access if the user has the "administer search_api" permission and the
- * server is a Solr server.
- *
- * @param SearchApiServer $server
- * The server for which access should be tested.
- *
- * @return bool
- * TRUE if access should be granted, FALSE otherwise.
- */
- function search_api_access_server_files(SearchApiServer $server) {
- if (!user_access('administer search_api')) {
- return FALSE;
- }
- $service_info = search_api_get_service_info($server->class);
- $service_class = $service_info['class'];
- if (empty($service_class) || !class_exists($service_class)) {
- // Service class not found.
- return FALSE;
- }
- if ($service_class == 'SearchApiSolrService' || in_array('SearchApiSolrService', class_parents($service_class))) {
- // It's an SearchApiSolrService based connection class.
- return TRUE;
- }
- return FALSE;
- }
- /**
- * Switches a server to use clean identifiers.
- *
- * Used as a submit callback in SearchApiSolrService::configurationForm().
- */
- function _search_api_solr_switch_to_clean_ids(array $form, array &$form_state) {
- $server = $form_state['server'];
- $server->options['clean_ids'] = TRUE;
- $server->save();
- drupal_set_message(t('The Solr server was successfully switched to use clean field identifiers.'));
- $count = 0;
- $conditions['server'] = $server->machine_name;
- $conditions['enabled'] = 1;
- foreach (search_api_index_load_multiple(FALSE, $conditions) as $index) {
- if (!empty($index->options['fields'])) {
- foreach ($index->options['fields'] as $key => $field) {
- if (strpos($key, ':') !== FALSE) {
- $index->reindex();
- ++$count;
- break;
- }
- }
- }
- }
- if ($count) {
- $msg = format_plural($count, '1 index was scheduled for re-indexing.', '@count indexes were scheduled for re-indexing.');
- drupal_set_message($msg);
- }
- }
|