Merge pull request #223 from creative-commoners/pulls/3.2/use-exception

FIX Use \Exception for catching Solr exceptions
This commit is contained in:
Robbie Averill 2018-06-27 17:00:12 +12:00 committed by GitHub
commit d2c4181826
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 37 additions and 36 deletions

View File

@ -61,7 +61,7 @@ class SearchUpdater_ObjectHandler extends DataExtension
$class = $this->owner->ClassName; $class = $this->owner->ClassName;
$state = SearchVariant::current_state($class); $state = SearchVariant::current_state($class);
$base = DataObject::getSchema()->baseDataClass($class); $base = DataObject::getSchema()->baseDataClass($class);
$key = "$id:$base:".serialize($state); $key = "$id:$base:" . serialize($state);
$statefulids = array(array( $statefulids = array(array(
'id' => $id, 'id' => $id,

View File

@ -438,7 +438,7 @@ abstract class SearchIndex extends ViewableData
continue; continue;
} }
$key = sha1($field['base'].serialize($field['lookup_chain'])); $key = sha1($field['base'] . serialize($field['lookup_chain']));
$fieldname = "{$field['class']}:{$field['field']}"; $fieldname = "{$field['class']}:{$field['field']}";
if (isset($this->derivedFields[$key])) { if (isset($this->derivedFields[$key])) {
@ -619,7 +619,7 @@ abstract class SearchIndex extends ViewableData
$tableName = DataObject::getSchema()->tableName($step['class']); $tableName = DataObject::getSchema()->tableName($step['class']);
if ($step['through'] == 'has_one') { if ($step['through'] == 'has_one') {
$sql = new SQLSelect('"ID"', '"'.$tableName.'"', '"'.$step['foreignkey'].'" IN ('.implode(',', $ids).')'); $sql = new SQLSelect('"ID"', '"' . $tableName . '"', '"' . $step['foreignkey'] . '" IN (' . implode(',', $ids) . ')');
singleton($step['class'])->extend('augmentSQL', $sql); singleton($step['class'])->extend('augmentSQL', $sql);
$ids = $sql->execute()->column(); $ids = $sql->execute()->column();
@ -627,8 +627,8 @@ abstract class SearchIndex extends ViewableData
// Use TableName for queries // Use TableName for queries
$otherTableName = DataObject::getSchema()->tableName($step['otherclass']); $otherTableName = DataObject::getSchema()->tableName($step['otherclass']);
$sql = new SQLSelect('"'.$tableName.'"."ID"', '"'.$tableName.'"', '"'.$otherTableName.'"."ID" IN ('.implode(',', $ids).')'); $sql = new SQLSelect('"' . $tableName . '"."ID"', '"' . $tableName . '"', '"' . $otherTableName . '"."ID" IN (' . implode(',', $ids) . ')');
$sql->addInnerJoin($otherTableName, '"'.$tableName.'"."ID" = "'.$otherTableName.'"."'.$step['foreignkey'].'"'); $sql->addInnerJoin($otherTableName, '"' . $tableName . '"."ID" = "' . $otherTableName . '"."' . $step['foreignkey'] . '"');
singleton($step['class'])->extend('augmentSQL', $sql); singleton($step['class'])->extend('augmentSQL', $sql);
$ids = $sql->execute()->column(); $ids = $sql->execute()->column();

View File

@ -170,7 +170,7 @@ class SearchUpdateCommitJobProcessor implements QueuedJob
// the current request // the current request
$cooldown = Config::inst()->get(__CLASS__, 'cooldown'); $cooldown = Config::inst()->get(__CLASS__, 'cooldown');
$now = new DateTime(DBDatetime::now()->getValue()); $now = new DateTime(DBDatetime::now()->getValue());
$now->add(new DateInterval('PT'.$cooldown.'S')); $now->add(new DateInterval('PT' . $cooldown . 'S'));
$runat = $now->Format('Y-m-d H:i:s'); $runat = $now->Format('Y-m-d H:i:s');
$this->addMessage("Indexing already run this request, but incomplete. Re-scheduling for {$runat}"); $this->addMessage("Indexing already run this request, but incomplete. Re-scheduling for {$runat}");

View File

@ -91,7 +91,7 @@ class SearchUpdater
$fields = isset($details['fields']) ? $details['fields'] : array(); $fields = isset($details['fields']) ? $details['fields'] : array();
$base = DataObject::getSchema()->baseDataClass($class); $base = DataObject::getSchema()->baseDataClass($class);
$key = "$id:$base:".serialize($state); $key = "$id:$base:" . serialize($state);
$statefulids = array(array('id' => $id, 'state' => $state)); $statefulids = array(array('id' => $id, 'state' => $state));
@ -105,8 +105,8 @@ class SearchUpdater
'command' => $command, 'command' => $command,
'fields' => array() 'fields' => array()
); );
} // Otherwise update the class label if it's more specific than the currently recorded one } elseif (is_subclass_of($class, $writes[$key]['class'])) {
elseif (is_subclass_of($class, $writes[$key]['class'])) { // Otherwise update the class label if it's more specific than the currently recorded one
$writes[$key]['class'] = $class; $writes[$key]['class'] = $class;
} }

View File

@ -44,7 +44,7 @@ abstract class SolrReindexBase implements SolrReindexHandler
$indexClasses = $this->getClassesForIndex($indexInstance, $classes); $indexClasses = $this->getClassesForIndex($indexInstance, $classes);
// Clear all records in this index which do not contain the given classes // Clear all records in this index which do not contain the given classes
$logger->info("Clearing obsolete classes from ".$indexInstance->getIndexName()); $logger->info("Clearing obsolete classes from " . $indexInstance->getIndexName());
$indexInstance->clearObsoleteClasses($indexClasses); $indexInstance->clearObsoleteClasses($indexClasses);
// Build queue for each class // Build queue for each class
@ -185,7 +185,7 @@ abstract class SolrReindexBase implements SolrReindexHandler
$indexInstance->add($item); $indexInstance->add($item);
$item->destroy(); $item->destroy();
} }
$logger->info("Updated ".implode(',', $processed)); $logger->info("Updated " . implode(',', $processed));
// This will slow down things a tiny bit, but it is done so that we don't timeout to the database during a reindex // This will slow down things a tiny bit, but it is done so that we don't timeout to the database during a reindex
DB::query('SELECT 1'); DB::query('SELECT 1');

View File

@ -67,9 +67,9 @@ class SolrReindexImmediateHandler extends SolrReindexBase
$statevar = json_encode($state); $statevar = json_encode($state);
if (strpos(PHP_OS, "WIN") !== false) { if (strpos(PHP_OS, "WIN") !== false) {
$statevar = '"'.str_replace('"', '\\"', $statevar).'"'; $statevar = '"' . str_replace('"', '\\"', $statevar) . '"';
} else { } else {
$statevar = "'".$statevar."'"; $statevar = "'" . $statevar . "'";
$class = addslashes($class); $class = addslashes($class);
$indexClassEscaped = addslashes($indexClass); $indexClassEscaped = addslashes($indexClass);
} }

View File

@ -2,6 +2,7 @@
namespace SilverStripe\FullTextSearch\Solr; namespace SilverStripe\FullTextSearch\Solr;
use Exception;
use SilverStripe\Control\Director; use SilverStripe\Control\Director;
use SilverStripe\Core\Environment; use SilverStripe\Core\Environment;
use SilverStripe\FulltextSearch\Search\Indexes\SearchIndex; use SilverStripe\FulltextSearch\Search\Indexes\SearchIndex;
@ -334,7 +335,7 @@ abstract class SolrIndex extends SearchIndex
public function setFieldBoosting($field, $level) public function setFieldBoosting($field, $level)
{ {
if (!isset($this->fulltextFields[$field])) { if (!isset($this->fulltextFields[$field])) {
throw new \InvalidArgumentException("No fulltext field $field exists on ".$this->getIndexName()); throw new \InvalidArgumentException("No fulltext field $field exists on " . $this->getIndexName());
} }
if ($level === null) { if ($level === null) {
unset($this->boostedFields[$field]); unset($this->boostedFields[$field]);
@ -714,7 +715,7 @@ abstract class SolrIndex extends SearchIndex
} }
} }
if ($classq) { if ($classq) {
$fq[] = '+('.implode(' ', $classq).')'; $fq[] = '+(' . implode(' ', $classq) . ')';
} }
// Filter by filters // Filter by filters
@ -735,13 +736,13 @@ abstract class SolrIndex extends SearchIndex
if (!headers_sent() && Director::isDev()) { if (!headers_sent() && Director::isDev()) {
if ($q) { if ($q) {
header('X-Query: '.implode(' ', $q)); header('X-Query: ' . implode(' ', $q));
} }
if ($fq) { if ($fq) {
header('X-Filters: "'.implode('", "', $fq).'"'); header('X-Filters: "' . implode('", "', $fq) . '"');
} }
if ($qf) { if ($qf) {
header('X-QueryFields: '.$qf); header('X-QueryFields: ' . $qf);
} }
} }
@ -898,11 +899,11 @@ abstract class SolrIndex extends SearchIndex
$field = $this->sanitiseClassName($field); $field = $this->sanitiseClassName($field);
$boost = (isset($search['boost'][$field])) ? '^' . $search['boost'][$field] : ''; $boost = (isset($search['boost'][$field])) ? '^' . $search['boost'][$field] : '';
$searchq[] = "{$field}:".$part.$fuzzy.$boost; $searchq[] = "{$field}:" . $part . $fuzzy . $boost;
} }
$q[] = '+('.implode(' OR ', $searchq).')'; $q[] = '+(' . implode(' OR ', $searchq) . ')';
} else { } else {
$q[] = '+'.$part.$fuzzy; $q[] = '+' . $part . $fuzzy;
} }
$hlq[] = $part; $hlq[] = $part;
} }
@ -938,11 +939,11 @@ abstract class SolrIndex extends SearchIndex
} }
$requireq[] = "$field:[$start TO $end]"; $requireq[] = "$field:[$start TO $end]";
} else { } else {
$requireq[] = $field.':"'.$value.'"'; $requireq[] = $field . ':"' . $value . '"';
} }
} }
$fq[] = '+('.implode(' ', $requireq).')'; $fq[] = '+(' . implode(' ', $requireq) . ')';
} }
return $fq; return $fq;
} }
@ -979,11 +980,11 @@ abstract class SolrIndex extends SearchIndex
} }
$excludeq[] = "$field:[$start TO $end]"; $excludeq[] = "$field:[$start TO $end]";
} else { } else {
$excludeq[] = $field.':"'.$value.'"'; $excludeq[] = $field . ':"' . $value . '"';
} }
} }
$fq[] = ($missing ? "+{$field}:[* TO *] " : '') . '-('.implode(' ', $excludeq).')'; $fq[] = ($missing ? "+{$field}:[* TO *] " : '') . '-(' . implode(' ', $excludeq) . ')';
} }
return $fq; return $fq;
} }
@ -1036,7 +1037,7 @@ abstract class SolrIndex extends SearchIndex
); );
// Upload additional files // Upload additional files
foreach (glob($this->getExtrasPath().'/*') as $file) { foreach (glob($this->getExtrasPath() . '/*') as $file) {
if (is_file($file)) { if (is_file($file)) {
$store->uploadFile($this->getIndexName(), $file); $store->uploadFile($this->getIndexName(), $file);
} }

View File

@ -36,7 +36,7 @@ class SolrConfigStore_File implements SolrConfigStore
public function uploadFile($index, $file) public function uploadFile($index, $file)
{ {
$targetDir = $this->getTargetDir($index); $targetDir = $this->getTargetDir($index);
copy($file, $targetDir.'/'.basename($file)); copy($file, $targetDir . '/' . basename($file));
} }
public function uploadString($index, $filename, $string) public function uploadString($index, $filename, $string)
@ -47,6 +47,6 @@ class SolrConfigStore_File implements SolrConfigStore
public function instanceDir($index) public function instanceDir($index)
{ {
return $this->remote.'/'.$index; return $this->remote . '/' . $index;
} }
} }

View File

@ -18,8 +18,8 @@ class SolrConfigStore_WebDAV implements SolrConfigStore
$this->url = implode('', array( $this->url = implode('', array(
'http://', 'http://',
isset($config['auth']) ? $config['auth'].'@' : '', isset($config['auth']) ? $config['auth'] . '@' : '',
$options['host'].':'.(isset($config['port']) ? $config['port'] : $options['port']), $options['host'] . ':' . (isset($config['port']) ? $config['port'] : $options['port']),
$config['path'] $config['path']
)); ));
$this->remote = $config['remotepath']; $this->remote = $config['remotepath'];
@ -43,7 +43,7 @@ class SolrConfigStore_WebDAV implements SolrConfigStore
public function uploadFile($index, $file) public function uploadFile($index, $file)
{ {
$targetDir = $this->getTargetDir($index); $targetDir = $this->getTargetDir($index);
WebDAV::upload_from_file($file, $targetDir.'/'.basename($file)); WebDAV::upload_from_file($file, $targetDir . '/' . basename($file));
} }
public function uploadString($index, $filename, $string) public function uploadString($index, $filename, $string)

View File

@ -92,7 +92,7 @@ class Solr_Configure extends Solr_BuildTask
} elseif (ClassInfo::exists($mode) && ClassInfo::classImplements($mode, SolrConfigStore::class)) { } elseif (ClassInfo::exists($mode) && ClassInfo::classImplements($mode, SolrConfigStore::class)) {
return new $mode($indexstore); return new $mode($indexstore);
} else { } else {
user_error('Unknown Solr index mode '.$indexstore['mode'], E_USER_ERROR); user_error('Unknown Solr index mode ' . $indexstore['mode'], E_USER_ERROR);
} }
} }
} }

View File

@ -33,12 +33,12 @@ class WebDAV
return true; return true;
} }
user_error("Got error from webdav server - ".$code, E_USER_ERROR); user_error("Got error from webdav server - " . $code, E_USER_ERROR);
} }
public static function mkdir($url) public static function mkdir($url)
{ {
$ch = self::curl_init(rtrim($url, '/').'/', 'MKCOL'); $ch = self::curl_init(rtrim($url, '/') . '/', 'MKCOL');
$res = curl_exec($ch); $res = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE); $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

View File

@ -95,7 +95,7 @@ class SolrIndexSubsitesTest extends SapphireTest
if (class_exists(Subsite::class) if (class_exists(Subsite::class)
&& DataObject::getSchema()->hasOneComponent($object->getClassName(), 'Subsite') && DataObject::getSchema()->hasOneComponent($object->getClassName(), 'Subsite')
) { ) {
$variants[] = '"SearchVariantSubsites":"' . $subsiteID. '"'; $variants[] = '"SearchVariantSubsites":"' . $subsiteID . '"';
} }
// Check versioned // Check versioned

View File

@ -85,7 +85,7 @@ class SolrIndexVersionedTest extends SapphireTest
{ {
$id = $object->ID; $id = $object->ID;
$class = DataObject::getSchema()->baseDataClass($object); $class = DataObject::getSchema()->baseDataClass($object);
return $id.'-'.$class.'-{'.json_encode(SearchVariantVersioned::class).':"'.$stage.'"}'; return $id . '-' . $class . '-{' . json_encode(SearchVariantVersioned::class) . ':"' . $stage . '"}';
} }
/** /**