Completed
Push — dev2 ( aa8e58...262bc1 )
by Gordon
03:07
created
src/SilverStripe/Elastica/ElasticaService.php 1 patch
Spacing   +48 added lines, -48 removed lines patch added patch discarded remove patch
@@ -137,12 +137,12 @@  discard block
 block discarded – undo
137 137
 	 */
138 138
 	public function search($query, $types = '') {
139 139
 		$query = Query::create($query); // may be a string
140
-		if (is_string($types)) {
140
+		if(is_string($types)) {
141 141
 			$types = explode(',', $types);
142 142
 		}
143 143
 
144 144
 		$data = $query->toArray();
145
-		if (isset($data['query']['more_like_this'])) {
145
+		if(isset($data['query']['more_like_this'])) {
146 146
 			$query->MoreLikeThis = true;
147 147
 		} else {
148 148
 			$query->MoreLikeThis = false;
@@ -151,7 +151,7 @@  discard block
 block discarded – undo
151 151
 
152 152
 		$search = new Search(new Client());
153 153
 
154
-		if ($this->test_mode) {
154
+		if($this->test_mode) {
155 155
 			$search->setOption('search_type', Search::OPTION_SEARCH_TYPE_DFS_QUERY_THEN_FETCH);
156 156
 		}
157 157
 
@@ -161,7 +161,7 @@  discard block
 block discarded – undo
161 161
 
162 162
 		// If the query is a 'more like this' we can get the terms used for searching by performing
163 163
 		// an extra query, in this case a query validation with explain and rewrite turned on
164
-		if ($query->MoreLikeThis) {
164
+		if($query->MoreLikeThis) {
165 165
 			$path = $search->getPath();
166 166
 
167 167
 			$termData = array();
@@ -169,7 +169,7 @@  discard block
 block discarded – undo
169 169
 
170 170
 			$path = str_replace('_search', '_validate/query', $path);
171 171
 			$params = array('explain' => true, 'rewrite' => true);
172
-			if ($this->test_mode) {
172
+			if($this->test_mode) {
173 173
 				$params['search_type'] = Search::OPTION_SEARCH_TYPE_DFS_QUERY_THEN_FETCH;
174 174
 			}
175 175
 
@@ -183,19 +183,19 @@  discard block
 block discarded – undo
183 183
 			$r = $response->getData();
184 184
 			$terms = null; // keep in scope
185 185
 
186
-			if (isset($r['explanations'])) {
186
+			if(isset($r['explanations'])) {
187 187
 				$explanation = $r['explanations'][0]['explanation'];
188 188
 				//echo $explanation;
189 189
 				$terms = ElasticaUtil::parseSuggestionExplanation($explanation);
190 190
 			}
191 191
 
192
-			if (isset($terms)) {
192
+			if(isset($terms)) {
193 193
 				$this->MoreLikeThisTerms = $terms;
194 194
 			}
195 195
 		}
196 196
 
197
-		if (!$empty($types)) {
198
-			foreach ($types as $type) {
197
+		if(!$empty($types)) {
198
+			foreach($types as $type) {
199 199
 				$search->addType($type);
200 200
 			}
201 201
 		}
@@ -215,7 +215,7 @@  discard block
 block discarded – undo
215 215
 		$stringFields = $this->highlightedFields;
216 216
 		$usingProvidedHighlightFields = true;
217 217
 
218
-		if (sizeof($stringFields) == 0) {
218
+		if(sizeof($stringFields) == 0) {
219 219
 			$filter = array('Type' => 'string', 'ShowHighlights' => true);
220 220
 			$stringFields = \SearchableField::get()->filter($filter)->map('Name')->toArray();
221 221
 			$usingProvidedHighlightFields = false;
@@ -223,10 +223,10 @@  discard block
 block discarded – undo
223 223
 
224 224
 
225 225
 		$highlightFields = array();
226
-		foreach ($stringFields as $name) {
226
+		foreach($stringFields as $name) {
227 227
 			// Add the stemmed and the unstemmed for now
228 228
 			$fieldName = $name;
229
-			if (!$usingProvidedHighlightFields) {
229
+			if(!$usingProvidedHighlightFields) {
230 230
 				$fieldName .= '.standard';
231 231
 			}
232 232
 			$highlightFields[$fieldName] = array(
@@ -242,9 +242,9 @@  discard block
 block discarded – undo
242 242
 			'fields' => $highlightFields
243 243
 		);
244 244
 
245
-		if ($query->MoreLikeThis) {
245
+		if($query->MoreLikeThis) {
246 246
 			$termsMatchingQuery = array();
247
-			foreach ($this->MoreLikeThisTerms as $field => $terms) {
247
+			foreach($this->MoreLikeThisTerms as $field => $terms) {
248 248
 				$termQuery = array('multi_match' => array(
249 249
 					'query' => implode(' ', $terms),
250 250
 					'type' => 'most_fields',
@@ -262,8 +262,8 @@  discard block
 block discarded – undo
262 262
 
263 263
 		//$search = new Search(new Client());
264 264
 		$search->addIndex($this->getLocaleIndexName());
265
-		if (!$empty($types)) {
266
-			foreach ($types as $type) {
265
+		if(!$empty($types)) {
266
+			foreach($types as $type) {
267 267
 				$search->addType($type);
268 268
 			}
269 269
 		}
@@ -272,7 +272,7 @@  discard block
 block discarded – undo
272 272
 		$path = $search->getPath();
273 273
 		$params = $search->getOptions();
274 274
 		$searchResults = $search->search($query, $params);
275
-		if (isset($this->MoreLikeThisTerms)) {
275
+		if(isset($this->MoreLikeThisTerms)) {
276 276
 			$searchResults->MoreLikeThisTerms = $this->MoreLikeThisTerms;
277 277
 		}
278 278
 
@@ -285,7 +285,7 @@  discard block
 block discarded – undo
285 285
 	 */
286 286
 	protected function ensureIndex() {
287 287
 		$index = $this->getIndex();
288
-		if (!$index->exists()) {
288
+		if(!$index->exists()) {
289 289
 			$this->createIndex();
290 290
 		}
291 291
 	}
@@ -300,7 +300,7 @@  discard block
 block discarded – undo
300 300
 	 */
301 301
 	protected function ensureMapping(\Elastica\Type $type, \DataObject $record) {
302 302
 		$mapping = $type->getMapping();
303
-		if ($mapping == array()) {
303
+		if($mapping == array()) {
304 304
 			$this->ensureIndex();
305 305
 			$mapping = $record->getElasticaMapping();
306 306
 			$type->setMapping($mapping);
@@ -319,8 +319,8 @@  discard block
 block discarded – undo
319 319
 		$document = $record->getElasticaDocument();
320 320
 		$typeName = $record->getElasticaType();
321 321
 
322
-		if ($this->buffered) {
323
-			if (array_key_exists($typeName, $this->buffer)) {
322
+		if($this->buffered) {
323
+			if(array_key_exists($typeName, $this->buffer)) {
324 324
 				$this->buffer[$typeName][] = $document;
325 325
 			} else {
326 326
 				$this->buffer[$typeName] = array($document);
@@ -363,17 +363,17 @@  discard block
 block discarded – undo
363 363
 	 */
364 364
 	public function endBulkIndex() {
365 365
 		$index = $this->getIndex();
366
-		foreach ($this->buffer as $type => $documents) {
366
+		foreach($this->buffer as $type => $documents) {
367 367
 			$amount = 0;
368 368
 
369
-			foreach (array_keys($this->buffer) as $key) {
369
+			foreach(array_keys($this->buffer) as $key) {
370 370
 				$amount += sizeof($this->buffer[$key]);
371 371
 			}
372 372
 			$index->getType($type)->addDocuments($documents);
373 373
 			$index->refresh();
374 374
 
375 375
 			ElasticaUtil::message("\tAdding $amount documents to the index\n");
376
-			if (isset($this->StartTime)) {
376
+			if(isset($this->StartTime)) {
377 377
 				$elapsed = microtime(true) - $this->StartTime;
378 378
 				$timePerDoc = ($elapsed) / ($this->nDocumentsIndexed);
379 379
 				$documentsRemaining = $this->nDocumentsToIndexForLocale - $this->nDocumentsIndexed;
@@ -412,12 +412,12 @@  discard block
 block discarded – undo
412 412
 		$index = $this->getIndex();
413 413
 
414 414
 		# Recreate the index
415
-		if ($index->exists()) {
415
+		if($index->exists()) {
416 416
 			$index->delete();
417 417
 		}
418 418
 		$this->createIndex();
419 419
 
420
-		foreach ($this->getIndexedClasses() as $class) {
420
+		foreach($this->getIndexedClasses() as $class) {
421 421
 			/** @var $sng Searchable */
422 422
 			$sng = singleton($class);
423 423
 			$mapping = $sng->getElasticaMapping();
@@ -434,8 +434,8 @@  discard block
 block discarded – undo
434 434
 	 * @param \DataList $records
435 435
 	 */
436 436
 	protected function refreshRecords($records) {
437
-		foreach ($records as $record) {
438
-			if ($record->showRecordInSearch()) {
437
+		foreach($records as $record) {
438
+			if($record->showRecordInSearch()) {
439 439
 				$this->index($record);
440 440
 			}
441 441
 		}
@@ -453,14 +453,14 @@  discard block
 block discarded – undo
453 453
 	protected function recordsByClassConsiderVersioned($class, $pageSize = 0, $page = 0) {
454 454
 		$offset = $page * $pageSize;
455 455
 
456
-		if ($class::has_extension("Versioned")) {
457
-			if ($pageSize > 0) {
456
+		if($class::has_extension("Versioned")) {
457
+			if($pageSize > 0) {
458 458
 				$records = \Versioned::get_by_stage($class, 'Live')->limit($pageSize, $offset);
459 459
 			} else {
460 460
 				$records = \Versioned::get_by_stage($class, 'Live');
461 461
 			}
462 462
 		} else {
463
-			if ($pageSize > 0) {
463
+			if($pageSize > 0) {
464 464
 				$records = $class::get()->limit($pageSize, $offset);
465 465
 			} else {
466 466
 				$records = $class::get();
@@ -482,7 +482,7 @@  discard block
 block discarded – undo
482 482
 		$pages = $nRecords / $batchSize + 1;
483 483
 		$processing = true;
484 484
 
485
-		for ($i = 0; $i < $pages; $i++) {
485
+		for($i = 0; $i < $pages; $i++) {
486 486
 			$this->startBulkIndex();
487 487
 			$pagedRecords = $this->recordsByClassConsiderVersioned($class, $batchSize, $i);
488 488
 			$this->nDocumentsIndexed += $pagedRecords->count();
@@ -507,7 +507,7 @@  discard block
 block discarded – undo
507 507
 		//Count the number of documents for this locale
508 508
 		$amount = 0;
509 509
 		echo "CURRENT LOCALE:" . $this->locale;
510
-		foreach ($classes as $class) {
510
+		foreach($classes as $class) {
511 511
 			$amount += $this->recordsByClassConsiderVersioned($class)->count();
512 512
 		}
513 513
 
@@ -516,17 +516,17 @@  discard block
 block discarded – undo
516 516
 
517 517
 		$index = $this->getIndex();
518 518
 
519
-		foreach ($this->getIndexedClasses() as $classname) {
519
+		foreach($this->getIndexedClasses() as $classname) {
520 520
 			ElasticaUtil::message("Indexing class $classname");
521 521
 
522 522
 			$inSiteTree = $classname === 'SiteTree' ? true : false;
523
-			if (isset(self::$site_tree_classes[$classname])) {
523
+			if(isset(self::$site_tree_classes[$classname])) {
524 524
 				$inSiteTree = self::$site_tree_classes[$classname];
525 525
 			} else {
526 526
 				$class = new \ReflectionClass($classname);
527
-				while ($class = $class->getParentClass()) {
527
+				while($class = $class->getParentClass()) {
528 528
 					$parentClass = $class->getName();
529
-					if ($parentClass == 'SiteTree') {
529
+					if($parentClass == 'SiteTree') {
530 530
 						$inSiteTree = true;
531 531
 						break;
532 532
 					}
@@ -537,9 +537,9 @@  discard block
 block discarded – undo
537 537
 			//$this->refreshClass($classname);
538 538
 
539 539
 
540
-			if ($inSiteTree) {
540
+			if($inSiteTree) {
541 541
 				// this prevents the same item being indexed twice due to class inheritance
542
-				if ($classname === 'SiteTree') {
542
+				if($classname === 'SiteTree') {
543 543
 					$this->refreshClass($classname);
544 544
 				}
545 545
 			// Data objects
@@ -578,7 +578,7 @@  discard block
 block discarded – undo
578 578
 	public function getIndexSettingsForCurrentLocale() {
579 579
 		$result = null;
580 580
 		$indexSettings = \Config::inst()->get('Elastica', 'indexsettings');
581
-		if (isset($indexSettings[$this->locale])) {
581
+		if(isset($indexSettings[$this->locale])) {
582 582
 			$settingsClassName = $indexSettings[$this->locale];
583 583
 			$result = \Injector::inst()->create($settingsClassName);
584 584
 		} else {
@@ -600,14 +600,14 @@  discard block
 block discarded – undo
600 600
 		$whitelist = array('SearchableTestPage', 'SearchableTestFatherPage', 'SearchableTestGrandFatherPage',
601 601
 			'FlickrPhotoTO', 'FlickrTagTO', 'FlickrPhotoTO', 'FlickrAuthorTO', 'FlickrSetTO');
602 602
 
603
-		foreach (\ClassInfo::subclassesFor('DataObject') as $candidate) {
603
+		foreach(\ClassInfo::subclassesFor('DataObject') as $candidate) {
604 604
 			$instance = singleton($candidate);
605 605
 
606 606
 			$interfaces = class_implements($candidate);
607 607
 			// Only allow test classes in testing mode
608
-			if (isset($interfaces['TestOnly'])) {
609
-				if (in_array($candidate, $whitelist)) {
610
-					if (!$this->test_mode) {
608
+			if(isset($interfaces['TestOnly'])) {
609
+				if(in_array($candidate, $whitelist)) {
610
+					if(!$this->test_mode) {
611 611
 						continue;
612 612
 					}
613 613
 				} else {
@@ -616,7 +616,7 @@  discard block
 block discarded – undo
616 616
 				}
617 617
 			}
618 618
 
619
-			if ($instance->hasExtension('SilverStripe\\Elastica\\Searchable')) {
619
+			if($instance->hasExtension('SilverStripe\\Elastica\\Searchable')) {
620 620
 				$classes[] = $candidate;
621 621
 			}
622 622
 		}
@@ -652,15 +652,15 @@  discard block
 block discarded – undo
652 652
 
653 653
 		$fields = array_keys($fieldMappings);
654 654
 		$allFields = array();
655
-		foreach ($fields as $field) {
655
+		foreach($fields as $field) {
656 656
 			array_push($allFields, $field);
657 657
 
658 658
 			$mapping = $fieldMappings[$field];
659 659
 
660 660
 
661
-			if (isset($mapping['fields'])) {
661
+			if(isset($mapping['fields'])) {
662 662
 				$subFields = array_keys($mapping['fields']);
663
-				foreach ($subFields as $subField) {
663
+				foreach($subFields as $subField) {
664 664
 					$name = $field . '.' . $subField;
665 665
 					array_push($allFields, $name);
666 666
 				}
Please login to merge, or discard this patch.