updated to 1.2

This commit is contained in:
Bachir Soussi Chiadmi
2013-09-26 16:23:17 +02:00
parent c9912105d5
commit d8237ffb99
135 changed files with 5808 additions and 26071 deletions

View File

@@ -1,76 +1,27 @@
<?php
/**
* @file
* Provides a Solr-based service class for the Search API.
*/
/**
* Implements hook_init().
* Implements hook_menu().
*/
function search_api_solr_init() {
spl_autoload_register('_search_api_solr_autoload');
}
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 path to SolrPhpClient library path, or FALSE if not found.
*/
function _search_api_solr_solrphpclient_path() {
static $path = NULL;
if (!isset($path)) {
$path = FALSE;
// If Libraries API is installed, we first use that to try and find the
// library. Otherwise we manually check a few locations.
$search_dirs = array();
if (function_exists('libraries_get_path')) {
$dir = libraries_get_path('SolrPhpClient');
// Confusingly, Libraries API 1.x will return sites/all/libraries/NAME on
// failure, while Libraries API 2.x returns FALSE in that case.
if ($dir) {
$search_dirs[] = $dir;
}
}
else {
// Include libraries + current profile folders in searched directories.
$search_dirs[] = 'sites/all/libraries/SolrPhpClient';
$search_dirs[] = 'profiles/' . drupal_get_profile() . '/libraries/SolrPhpClient';
}
$search_dirs[] = drupal_get_path('module', 'search_api_solr') . '/SolrPhpClient';
foreach ($search_dirs as $dir) {
$dir = DRUPAL_ROOT . '/' . $dir;
if (is_dir($dir)) {
$path = $dir;
break;
}
}
}
if ($path == FALSE) {
throw new Exception('SolrPhpClient library not found! Please follow the instructions in search_api_solr/INSTALL.txt for installing the Solr search module.');
}
return $path;
}
/**
* Autoloader for the SolrPhpClient classes.
*/
function _search_api_solr_autoload($name) {
static $lookup_cache = array();
if (isset($lookup_cache[$name])) {
return $lookup_cache[$name];
}
elseif (substr($name, 0, 11) == 'Apache_Solr') {
$path = _search_api_solr_solrphpclient_path();
if (file_exists($file_path = $path . '/' . str_replace('_', '/', $name) . '.php')) {
require_once $file_path;
$lookup_cache[$name] = TRUE;
return TRUE;
}
}
$lookup_cache[$name] = FALSE;
return FALSE;
return $items;
}
/**
@@ -135,7 +86,7 @@ function search_api_solr_cron() {
$conditions = array('class' => 'search_api_solr_service', 'enabled' => TRUE);
foreach (search_api_server_load_multiple(FALSE, $conditions) as $server) {
try {
$server->getSolrConnection()->optimize(FALSE, FALSE);
$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));
@@ -145,79 +96,119 @@ function search_api_solr_cron() {
}
/**
* Returns either all dynamic field types, or a specific one.
*
* @param $type
* If specified, the type whose definition should be returned.
*
* @return array
* If $type was not given, an array containing all custom dynamic fields, in
* the format specified by hook_search_api_solr_dynamic_field_info().
* Otherwise, the definition for the given type, or NULL if it is unknown.
*
* @see hook_search_api_solr_dynamic_field_info().
* Implements hook_flush_caches().
*/
function search_api_solr_get_dynamic_field_info($type = NULL) {
$types = &drupal_static(__FUNCTION__);
if (!isset($types)) {
$types = module_invoke_all('search_api_solr_dynamic_field_info');
$types = $types ? $types : array();
drupal_alter('search_api_solr_dynamic_field_info', $types);
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();
}
if (isset($type)) {
return isset($types[$type]) ? $types[$type] : NULL;
}
return $types;
}
/**
* Implements hook_search_api_solr_dynamic_field_info().
* 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_search_api_solr_dynamic_field_info() {
return array(
'text' => array(
'prefix' => 'tm',
'always multiValued' => TRUE,
),
'tokens' => array(
'prefix' => 'tm',
'always multiValued' => TRUE,
),
'string' => array(
'prefix' => 's',
'always multiValued' => FALSE,
),
'integer' => array(
'prefix' => 'i',
'always multiValued' => FALSE,
),
'decimal' => array(
'prefix' => 'f',
'always multiValued' => FALSE,
),
'date' => array(
'prefix' => 'd',
'always multiValued' => FALSE,
),
'duration' => array(
'i',
'always multiValued' => FALSE,
),
'boolean' => array(
'b',
'always multiValued' => FALSE,
),
'uri' => array(
's',
'always multiValued' => FALSE,
),
'location' => array(
'loc',
'always multiValued' => FALSE,
),
'geohash' => array(
'geohash',
'always multiValued' => FALSE,
),
);
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);
}
}