@@ -24,7 +24,7 @@ |
||
24 | 24 | * Execute the task to delete the currently configured index |
25 | 25 | */ |
26 | 26 | public function run($request) { |
27 | - $message = function ($content) { |
|
27 | + $message = function($content) { |
|
28 | 28 | print(\Director::is_cli() ? "$content\n" : "<p>$content</p>"); |
29 | 29 | }; |
30 | 30 |
@@ -555,8 +555,7 @@ discard block |
||
555 | 555 | // this goes to elastica service |
556 | 556 | $this->service->remove($this->owner); |
557 | 557 | } |
558 | - } |
|
559 | - catch(\Elastica\Exception\NotFoundException $e) { |
|
558 | + } catch(\Elastica\Exception\NotFoundException $e) { |
|
560 | 559 | trigger_error("Deleted document ".$this->owner->ClassName." (".$this->owner->ID. |
561 | 560 | ") not found in search index.", E_USER_NOTICE); |
562 | 561 | } |
@@ -574,7 +573,9 @@ discard block |
||
574 | 573 | $fields = \Config::inst()->get(get_class($this->owner), 'searchable_fields'); |
575 | 574 | |
576 | 575 | // fallback to default method |
577 | - if(!$fields) user_error('The field $searchable_fields must be set for the class '.$this->owner->ClassName); |
|
576 | + if(!$fields) { |
|
577 | + user_error('The field $searchable_fields must be set for the class '.$this->owner->ClassName); |
|
578 | + } |
|
578 | 579 | |
579 | 580 | // get the values of these fields |
580 | 581 | $elasticaMapping = $this->fieldsToElasticaConfig($this->owner, $fields); |
@@ -595,7 +596,9 @@ discard block |
||
595 | 596 | |
596 | 597 | $relClass = $has_lists[$methodName]; |
597 | 598 | $fields = \Config::inst()->get($relClass, 'searchable_fields'); |
598 | - if(!$fields) user_error('The field $searchable_fields must be set for the class '.$relClass); |
|
599 | + if(!$fields) { |
|
600 | + user_error('The field $searchable_fields must be set for the class '.$relClass); |
|
601 | + } |
|
599 | 602 | $rewrite = $this->fieldsToElasticaConfig($relClass, $fields); |
600 | 603 | |
601 | 604 | // mark as a method, the resultant fields are correct |
@@ -605,7 +608,9 @@ discard block |
||
605 | 608 | } else if (isset($has_ones[$methodName])) { |
606 | 609 | $relClass = $has_ones[$methodName]; |
607 | 610 | $fields = \Config::inst()->get($relClass, 'searchable_fields'); |
608 | - if(!$fields) user_error('The field $searchable_fields must be set for the class '.$relClass); |
|
611 | + if(!$fields) { |
|
612 | + user_error('The field $searchable_fields must be set for the class '.$relClass); |
|
613 | + } |
|
609 | 614 | $rewrite = $this->fieldsToElasticaConfig($relClass, $fields); |
610 | 615 | $classname = $has_ones[$methodName]; |
611 | 616 | |
@@ -687,7 +692,7 @@ discard block |
||
687 | 692 | |
688 | 693 | if (isset($searchableField['type'])) { |
689 | 694 | $doSF->Type = $searchableField['type']; |
690 | - } else { |
|
695 | + } else { |
|
691 | 696 | $doSF->Name = $searchableField['properties']['__method']; |
692 | 697 | $doSF->Type = 'relationship'; |
693 | 698 | } |
@@ -131,32 +131,32 @@ discard block |
||
131 | 131 | $fields = $this->getAllSearchableFields(); |
132 | 132 | $result = array(); |
133 | 133 | |
134 | - foreach ($fields as $name => $params) { |
|
134 | + foreach($fields as $name => $params) { |
|
135 | 135 | $spec = array(); |
136 | 136 | $name = str_replace('()', '', $name); |
137 | 137 | |
138 | - if (array_key_exists($name, $db)) { |
|
138 | + if(array_key_exists($name, $db)) { |
|
139 | 139 | $class = $db[$name]; |
140 | 140 | |
141 | - if (($pos = strpos($class, '('))) { |
|
141 | + if(($pos = strpos($class, '('))) { |
|
142 | 142 | // Valid in the case of Varchar(255) |
143 | 143 | $class = substr($class, 0, $pos); |
144 | 144 | } |
145 | 145 | |
146 | - if (array_key_exists($class, self::$mappings)) { |
|
146 | + if(array_key_exists($class, self::$mappings)) { |
|
147 | 147 | $spec['type'] = self::$mappings[$class]; |
148 | - if ($spec['type'] === 'date') { |
|
149 | - if ($class == 'Date') { |
|
148 | + if($spec['type'] === 'date') { |
|
149 | + if($class == 'Date') { |
|
150 | 150 | $spec['format'] = 'y-M-d'; |
151 | - } elseif ($class == 'SS_Datetime') { |
|
151 | + } elseif($class == 'SS_Datetime') { |
|
152 | 152 | $spec['format'] = 'y-M-d H:m:s'; |
153 | - } elseif ($class == 'Datetime') { |
|
153 | + } elseif($class == 'Datetime') { |
|
154 | 154 | $spec['format'] = 'y-M-d H:m:s'; |
155 | - } elseif ($class == 'Time') { |
|
155 | + } elseif($class == 'Time') { |
|
156 | 156 | $spec['format'] = 'H:m:s'; |
157 | 157 | } |
158 | 158 | } |
159 | - if ($class === 'HTMLText' || $class === 'HTMLVarchar') { |
|
159 | + if($class === 'HTMLText' || $class === 'HTMLVarchar') { |
|
160 | 160 | array_push($this->html_fields, $name); |
161 | 161 | } |
162 | 162 | } |
@@ -167,7 +167,7 @@ discard block |
||
167 | 167 | $has_ones = $this->owner->has_one(); |
168 | 168 | |
169 | 169 | // check has_many and many_many relations |
170 | - if (isset($has_lists[$name])) { |
|
170 | + if(isset($has_lists[$name])) { |
|
171 | 171 | // FIX ME how to do nested mapping |
172 | 172 | // See https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-array-type.html |
173 | 173 | |
@@ -178,13 +178,13 @@ discard block |
||
178 | 178 | $resultTypeMapping = array(); |
179 | 179 | |
180 | 180 | // get the fields for the result type, but do not recurse |
181 | - if ($recurse) { |
|
181 | + if($recurse) { |
|
182 | 182 | $resultTypeMapping = $resultTypeInstance->getElasticaFields($storeMethodName, false); |
183 | 183 | } |
184 | 184 | |
185 | 185 | $resultTypeMapping['ID'] = array('type' => 'integer'); |
186 | 186 | |
187 | - if ($storeMethodName) { |
|
187 | + if($storeMethodName) { |
|
188 | 188 | $resultTypeMapping['__method'] = $name; |
189 | 189 | } |
190 | 190 | |
@@ -193,20 +193,20 @@ discard block |
||
193 | 193 | |
194 | 194 | // we now change the name to the result type, not the method name |
195 | 195 | $name = $resultType; |
196 | - } else if (isset($has_ones[$name])) { |
|
196 | + } else if(isset($has_ones[$name])) { |
|
197 | 197 | $resultType = $has_ones[$name]; |
198 | 198 | $resultTypeInstance = \Injector::inst()->create($resultType); |
199 | 199 | |
200 | 200 | $resultTypeMapping = array(); |
201 | 201 | |
202 | 202 | // get the fields for the result type, but do not recurse |
203 | - if ($recurse) { |
|
203 | + if($recurse) { |
|
204 | 204 | $resultTypeMapping = $resultTypeInstance->getElasticaFields($storeMethodName, false); |
205 | 205 | } |
206 | 206 | |
207 | 207 | $resultTypeMapping['ID'] = array('type' => 'integer'); |
208 | 208 | |
209 | - if ($storeMethodName) { |
|
209 | + if($storeMethodName) { |
|
210 | 210 | $resultTypeMapping['__method'] = $name; |
211 | 211 | } |
212 | 212 | $spec = array('properties' => $resultTypeMapping); |
@@ -220,8 +220,8 @@ discard block |
||
220 | 220 | } |
221 | 221 | |
222 | 222 | // in the case of a relationship type will not be set |
223 | - if (isset($spec['type'])) { |
|
224 | - if ($spec['type'] == 'string') { |
|
223 | + if(isset($spec['type'])) { |
|
224 | + if($spec['type'] == 'string') { |
|
225 | 225 | $unstemmed = array(); |
226 | 226 | $unstemmed['type'] = "string"; |
227 | 227 | $unstemmed['analyzer'] = "unstemmed"; |
@@ -237,7 +237,7 @@ discard block |
||
237 | 237 | //Add autocomplete field if so required |
238 | 238 | $autocomplete = \Config::inst()->get($this->owner->ClassName, 'searchable_autocomplete'); |
239 | 239 | |
240 | - if (isset($autocomplete) && in_array($name, $autocomplete)) { |
|
240 | + if(isset($autocomplete) && in_array($name, $autocomplete)) { |
|
241 | 241 | $autocompleteField = array(); |
242 | 242 | $autocompleteField['type'] = "string"; |
243 | 243 | $autocompleteField['index_analyzer'] = "autocomplete_index_analyzer"; |
@@ -256,7 +256,7 @@ discard block |
||
256 | 256 | $result[$name] = $spec; |
257 | 257 | } |
258 | 258 | |
259 | - if ($this->owner->hasMethod('updateElasticHTMLFields')) { |
|
259 | + if($this->owner->hasMethod('updateElasticHTMLFields')) { |
|
260 | 260 | $this->html_fields = $this->owner->updateElasticHTMLFields($this->html_fields); |
261 | 261 | } |
262 | 262 | |
@@ -276,7 +276,7 @@ discard block |
||
276 | 276 | |
277 | 277 | $localeMapping = array(); |
278 | 278 | |
279 | - if ($this->owner->hasField('Locale')) { |
|
279 | + if($this->owner->hasField('Locale')) { |
|
280 | 280 | $localeMapping['type'] = 'string'; |
281 | 281 | // we wish the locale to be stored as is |
282 | 282 | $localeMapping['index'] = 'not_analyzed'; |
@@ -295,7 +295,7 @@ discard block |
||
295 | 295 | //_all field picks up all possible suggestions |
296 | 296 | $mapping->enableAllField(); |
297 | 297 | |
298 | - if ($this->owner->hasMethod('updateElasticsearchMapping')) { |
|
298 | + if($this->owner->hasMethod('updateElasticsearchMapping')) { |
|
299 | 299 | $mapping = $this->owner->updateElasticsearchMapping($mapping); |
300 | 300 | } |
301 | 301 | return $mapping; |
@@ -311,20 +311,20 @@ discard block |
||
311 | 311 | self::$index_ctr++; |
312 | 312 | $fields = $this->getFieldValuesAsArray(); |
313 | 313 | |
314 | - if (isset($_GET['progress'])) { |
|
314 | + if(isset($_GET['progress'])) { |
|
315 | 315 | $progress = $_GET['progress']; |
316 | - self::$progressInterval = (int) $progress; |
|
316 | + self::$progressInterval = (int)$progress; |
|
317 | 317 | } |
318 | 318 | |
319 | - if (self::$progressInterval > 0) { |
|
320 | - if (self::$index_ctr % self::$progressInterval === 0) { |
|
321 | - ElasticaUtil::message("\t".$this->owner->ClassName." - Prepared ".self::$index_ctr." for indexing..."); |
|
319 | + if(self::$progressInterval > 0) { |
|
320 | + if(self::$index_ctr % self::$progressInterval === 0) { |
|
321 | + ElasticaUtil::message("\t" . $this->owner->ClassName . " - Prepared " . self::$index_ctr . " for indexing..."); |
|
322 | 322 | } |
323 | 323 | } |
324 | 324 | |
325 | 325 | // Optionally update the document |
326 | 326 | $document = new Document($this->owner->ID, $fields); |
327 | - if ($this->owner->hasMethod('updateElasticsearchDocument')) { |
|
327 | + if($this->owner->hasMethod('updateElasticsearchDocument')) { |
|
328 | 328 | $document = $this->owner->updateElasticsearchDocument($document); |
329 | 329 | } |
330 | 330 | |
@@ -335,11 +335,11 @@ discard block |
||
335 | 335 | |
336 | 336 | $document->set('IsInSiteTree', $inSiteTree); |
337 | 337 | |
338 | - if ($inSiteTree) { |
|
338 | + if($inSiteTree) { |
|
339 | 339 | $document->set('Link', $this->owner->AbsoluteLink()); |
340 | 340 | } |
341 | 341 | |
342 | - if (isset($this->owner->Locale)) { |
|
342 | + if(isset($this->owner->Locale)) { |
|
343 | 343 | $document->set('Locale', $this->owner->Locale); |
344 | 344 | } |
345 | 345 | |
@@ -351,10 +351,10 @@ discard block |
||
351 | 351 | $fields = array(); |
352 | 352 | $has_ones = $this->owner->has_one(); |
353 | 353 | |
354 | - foreach ($this->getElasticaFields($recurse) as $field => $config) { |
|
355 | - if (null === $this->owner->$field && is_callable(get_class($this->owner) . "::" . $field)) { |
|
354 | + foreach($this->getElasticaFields($recurse) as $field => $config) { |
|
355 | + if(null === $this->owner->$field && is_callable(get_class($this->owner) . "::" . $field)) { |
|
356 | 356 | // call a method to get a field value |
357 | - if (in_array($field, $this->html_fields)) { |
|
357 | + if(in_array($field, $this->html_fields)) { |
|
358 | 358 | // Parse short codes in HTML, and then convert to text |
359 | 359 | $fields[$field] = $this->owner->$field; |
360 | 360 | $html = ShortcodeParser::get_active()->parse($this->owner->$field()); |
@@ -366,30 +366,30 @@ discard block |
||
366 | 366 | } |
367 | 367 | |
368 | 368 | } else { |
369 | - if (in_array($field, $this->html_fields)) { |
|
369 | + if(in_array($field, $this->html_fields)) { |
|
370 | 370 | $fields[$field] = $this->owner->$field; |
371 | - if (gettype($this->owner->$field) !== 'NULL') { |
|
371 | + if(gettype($this->owner->$field) !== 'NULL') { |
|
372 | 372 | $html = ShortcodeParser::get_active()->parse($this->owner->$field); |
373 | 373 | $txt = \Convert::html2raw($html); |
374 | 374 | $fields[$field] = $txt; |
375 | 375 | } |
376 | 376 | } else { |
377 | - if (isset($config['properties']['__method'])) { |
|
377 | + if(isset($config['properties']['__method'])) { |
|
378 | 378 | $methodName = $config['properties']['__method']; |
379 | 379 | $data = $this->owner->$methodName(); |
380 | 380 | $relArray = array(); |
381 | 381 | |
382 | 382 | // get the fields of a has_one relational object |
383 | - if (isset($has_ones[$methodName])) { |
|
384 | - if ($data->ID > 0) { |
|
383 | + if(isset($has_ones[$methodName])) { |
|
384 | + if($data->ID > 0) { |
|
385 | 385 | $item = $data->getFieldValuesAsArray(false); |
386 | 386 | $relArray = $item; |
387 | 387 | } |
388 | 388 | |
389 | 389 | // get the fields for a has_many or many_many relational list |
390 | 390 | } else { |
391 | - foreach ($data->getIterator() as $item) { |
|
392 | - if ($recurse) { |
|
391 | + foreach($data->getIterator() as $item) { |
|
392 | + if($recurse) { |
|
393 | 393 | // populate the subitem but do not recurse any further if more relationships |
394 | 394 | $itemDoc = $item->getFieldValuesAsArray(false); |
395 | 395 | array_push($relArray, $itemDoc); |
@@ -426,8 +426,8 @@ discard block |
||
426 | 426 | * Delete the record from the search index if ShowInSearch is deactivated (non-SiteTree). |
427 | 427 | */ |
428 | 428 | public function onBeforeWrite() { |
429 | - if (($this->owner instanceof \SiteTree)) { |
|
430 | - if ($this->owner->hasField('ShowInSearch') && |
|
429 | + if(($this->owner instanceof \SiteTree)) { |
|
430 | + if($this->owner->hasField('ShowInSearch') && |
|
431 | 431 | $this->owner->isChanged('ShowInSearch', 2) && false == $this->owner->ShowInSearch) { |
432 | 432 | $this->doDeleteDocument(); |
433 | 433 | } |
@@ -439,11 +439,11 @@ discard block |
||
439 | 439 | * Delete the record from the search index if ShowInSearch is deactivated (SiteTree). |
440 | 440 | */ |
441 | 441 | public function onBeforePublish() { |
442 | - if (false == $this->owner->ShowInSearch) { |
|
443 | - if ($this->owner->isPublished()) { |
|
442 | + if(false == $this->owner->ShowInSearch) { |
|
443 | + if($this->owner->isPublished()) { |
|
444 | 444 | $liveRecord = \Versioned::get_by_stage(get_class($this->owner), 'Live')-> |
445 | 445 | byID($this->owner->ID); |
446 | - if ($liveRecord->ShowInSearch != $this->owner->ShowInSearch) { |
|
446 | + if($liveRecord->ShowInSearch != $this->owner->ShowInSearch) { |
|
447 | 447 | $this->doDeleteDocument(); |
448 | 448 | } |
449 | 449 | } |
@@ -471,8 +471,8 @@ discard block |
||
471 | 471 | * Updates the record in the search index. |
472 | 472 | */ |
473 | 473 | protected function doIndexDocument() { |
474 | - if ($this->showRecordInSearch()) { |
|
475 | - if (!$this->owner->IndexingOff) { |
|
474 | + if($this->showRecordInSearch()) { |
|
475 | + if(!$this->owner->IndexingOff) { |
|
476 | 476 | $this->service->index($this->owner); |
477 | 477 | } |
478 | 478 | } |
@@ -499,7 +499,7 @@ discard block |
||
499 | 499 | * Removes the record from the search index if the "ShowInSearch" attribute is set to true. |
500 | 500 | */ |
501 | 501 | protected function doDeleteDocumentIfInSearch() { |
502 | - if ($this->showRecordInSearch()) { |
|
502 | + if($this->showRecordInSearch()) { |
|
503 | 503 | $this->doDeleteDocument(); |
504 | 504 | } |
505 | 505 | } |
@@ -509,14 +509,14 @@ discard block |
||
509 | 509 | * Removes the record from the search index. |
510 | 510 | */ |
511 | 511 | protected function doDeleteDocument() { |
512 | - try{ |
|
513 | - if (!$this->owner->IndexingOff) { |
|
512 | + try { |
|
513 | + if(!$this->owner->IndexingOff) { |
|
514 | 514 | // this goes to elastica service |
515 | 515 | $this->service->remove($this->owner); |
516 | 516 | } |
517 | 517 | } |
518 | - catch(\Elastica\Exception\NotFoundException $e) { |
|
519 | - trigger_error("Deleted document ".$this->owner->ClassName." (".$this->owner->ID. |
|
518 | + catch (\Elastica\Exception\NotFoundException $e) { |
|
519 | + trigger_error("Deleted document " . $this->owner->ClassName . " (" . $this->owner->ID . |
|
520 | 520 | ") not found in search index.", E_USER_NOTICE); |
521 | 521 | } |
522 | 522 | |
@@ -533,42 +533,42 @@ discard block |
||
533 | 533 | $fields = \Config::inst()->get(get_class($this->owner), 'searchable_fields'); |
534 | 534 | |
535 | 535 | // fallback to default method |
536 | - if(!$fields) user_error('The field $searchable_fields must be set for the class '.$this->owner->ClassName); |
|
536 | + if(!$fields) user_error('The field $searchable_fields must be set for the class ' . $this->owner->ClassName); |
|
537 | 537 | |
538 | 538 | // get the values of these fields |
539 | 539 | $elasticaMapping = $this->fieldsToElasticaConfig($fields); |
540 | 540 | |
541 | - if ($recurse) { |
|
541 | + if($recurse) { |
|
542 | 542 | // now for the associated methods and their results |
543 | 543 | $methodDescs = \Config::inst()->get(get_class($this->owner), 'searchable_relationships'); |
544 | 544 | $has_ones = $this->owner->has_one(); |
545 | 545 | $has_lists = $this->getListRelationshipMethods(); |
546 | 546 | |
547 | - if (isset($methodDescs) && is_array($methodDescs)) { |
|
548 | - foreach ($methodDescs as $methodDesc) { |
|
547 | + if(isset($methodDescs) && is_array($methodDescs)) { |
|
548 | + foreach($methodDescs as $methodDesc) { |
|
549 | 549 | // split before the brackets which can optionally list which fields to index |
550 | 550 | $splits = explode('(', $methodDesc); |
551 | 551 | $methodName = $splits[0]; |
552 | 552 | |
553 | - if (isset($has_lists[$methodName])) { |
|
553 | + if(isset($has_lists[$methodName])) { |
|
554 | 554 | |
555 | 555 | $relClass = $has_lists[$methodName]; |
556 | 556 | $fields = \Config::inst()->get($relClass, 'searchable_fields'); |
557 | - if(!$fields) user_error('The field $searchable_fields must be set for the class '.$relClass); |
|
557 | + if(!$fields) user_error('The field $searchable_fields must be set for the class ' . $relClass); |
|
558 | 558 | $rewrite = $this->fieldsToElasticaConfig($fields); |
559 | 559 | |
560 | 560 | // mark as a method, the resultant fields are correct |
561 | - $elasticaMapping[$methodName.'()'] = $rewrite; |
|
562 | - } else if (isset($has_ones[$methodName])) { |
|
561 | + $elasticaMapping[$methodName . '()'] = $rewrite; |
|
562 | + } else if(isset($has_ones[$methodName])) { |
|
563 | 563 | $relClass = $has_ones[$methodName]; |
564 | 564 | $fields = \Config::inst()->get($relClass, 'searchable_fields'); |
565 | - if(!$fields) user_error('The field $searchable_fields must be set for the class '.$relClass); |
|
565 | + if(!$fields) user_error('The field $searchable_fields must be set for the class ' . $relClass); |
|
566 | 566 | $rewrite = $this->fieldsToElasticaConfig($fields); |
567 | 567 | |
568 | 568 | // mark as a method, the resultant fields are correct |
569 | - $elasticaMapping[$methodName.'()'] = $rewrite; |
|
569 | + $elasticaMapping[$methodName . '()'] = $rewrite; |
|
570 | 570 | } else { |
571 | - user_error('The method '.$methodName.' not found in class '.$this->owner->ClassName. |
|
571 | + user_error('The method ' . $methodName . ' not found in class ' . $this->owner->ClassName . |
|
572 | 572 | ', please check configuration'); |
573 | 573 | } |
574 | 574 | } |
@@ -604,11 +604,11 @@ discard block |
||
604 | 604 | public function requireDefaultRecords() { |
605 | 605 | parent::requireDefaultRecords(); |
606 | 606 | |
607 | - $searchableFields = $this->getElasticaFields(true,true); |
|
607 | + $searchableFields = $this->getElasticaFields(true, true); |
|
608 | 608 | |
609 | 609 | |
610 | 610 | $doSC = \SearchableClass::get()->filter(array('Name' => $this->owner->ClassName))->first(); |
611 | - if (!$doSC) { |
|
611 | + if(!$doSC) { |
|
612 | 612 | $doSC = new \SearchableClass(); |
613 | 613 | $doSC->Name = $this->owner->ClassName; |
614 | 614 | |
@@ -618,9 +618,9 @@ discard block |
||
618 | 618 | $doSC->write(); |
619 | 619 | } |
620 | 620 | |
621 | - foreach ($searchableFields as $name => $searchableField) { |
|
621 | + foreach($searchableFields as $name => $searchableField) { |
|
622 | 622 | // check for existence of methods and if they exist use that as the name |
623 | - if (!isset($searchableField['type'])) { |
|
623 | + if(!isset($searchableField['type'])) { |
|
624 | 624 | $name = $searchableField['properties']['__method']; |
625 | 625 | } |
626 | 626 | |
@@ -628,25 +628,25 @@ discard block |
||
628 | 628 | $doSF = \SearchableField::get()->filter($filter)->first(); |
629 | 629 | |
630 | 630 | |
631 | - if (!$doSF) { |
|
631 | + if(!$doSF) { |
|
632 | 632 | $doSF = new \SearchableField(); |
633 | 633 | $doSF->ClazzName = $this->owner->ClassName; |
634 | 634 | $doSF->Name = $name; |
635 | 635 | |
636 | - if (isset($searchableField['type'])) { |
|
636 | + if(isset($searchableField['type'])) { |
|
637 | 637 | $doSF->Type = $searchableField['type']; |
638 | - } else { |
|
638 | + } else { |
|
639 | 639 | $doSF->Name = $searchableField['properties']['__method']; |
640 | 640 | $doSF->Type = 'relationship'; |
641 | 641 | } |
642 | 642 | $doSF->SearchableClassID = $doSC->ID; |
643 | 643 | |
644 | - if (isset($searchableField['fields']['autocomplete'])) { |
|
644 | + if(isset($searchableField['fields']['autocomplete'])) { |
|
645 | 645 | $doSF->Autocomplete = true; |
646 | 646 | } |
647 | 647 | |
648 | 648 | $doSF->write(); |
649 | - \DB::alteration_message("Created new searchable editable field ".$name,"changed"); |
|
649 | + \DB::alteration_message("Created new searchable editable field " . $name, "changed"); |
|
650 | 650 | } |
651 | 651 | |
652 | 652 | // FIXME deal with deletions |
@@ -660,7 +660,7 @@ discard block |
||
660 | 660 | |
661 | 661 | // array of method name to retuned object ClassName for relationships returning lists |
662 | 662 | $has_lists = $has_manys; |
663 | - foreach (array_keys($many_manys) as $key) { |
|
663 | + foreach(array_keys($many_manys) as $key) { |
|
664 | 664 | $has_lists[$key] = $many_manys[$key]; |
665 | 665 | } |
666 | 666 | |
@@ -671,11 +671,11 @@ discard block |
||
671 | 671 | private function isInSiteTree($classname) { |
672 | 672 | $inSiteTree = $classname === 'SiteTree' ? true : false; |
673 | 673 | |
674 | - if (!$inSiteTree) { |
|
674 | + if(!$inSiteTree) { |
|
675 | 675 | $class = new \ReflectionClass($this->owner->ClassName); |
676 | - while ($class = $class->getParentClass()) { |
|
676 | + while($class = $class->getParentClass()) { |
|
677 | 677 | $parentClass = $class->getName(); |
678 | - if ($parentClass == 'SiteTree') { |
|
678 | + if($parentClass == 'SiteTree') { |
|
679 | 679 | $inSiteTree = true; |
680 | 680 | break; |
681 | 681 | } |
@@ -691,7 +691,7 @@ discard block |
||
691 | 691 | */ |
692 | 692 | public function RenderResult($linkToContainer = '') { |
693 | 693 | $vars = new \ArrayData(array('SearchResult' => $this->owner, 'ContainerLink' => $linkToContainer)); |
694 | - $possibleTemplates = array($this->owner->ClassName.'ElasticSearchResult', 'ElasticSearchResult'); |
|
694 | + $possibleTemplates = array($this->owner->ClassName . 'ElasticSearchResult', 'ElasticSearchResult'); |
|
695 | 695 | return $this->owner->customise($vars)->renderWith($possibleTemplates); |
696 | 696 | } |
697 | 697 | |
@@ -705,10 +705,10 @@ discard block |
||
705 | 705 | public function updateCMSFields(\FieldList $fields) { |
706 | 706 | $isIndexed = false; |
707 | 707 | // SIteTree object must have a live record, ShowInSearch = true |
708 | - if ($this->isInSiteTree($this->owner->ClassName)) { |
|
708 | + if($this->isInSiteTree($this->owner->ClassName)) { |
|
709 | 709 | $liveRecord = \Versioned::get_by_stage(get_class($this->owner), 'Live')-> |
710 | 710 | byID($this->owner->ID); |
711 | - if ($liveRecord->ShowInSearch) { |
|
711 | + if($liveRecord->ShowInSearch) { |
|
712 | 712 | $isIndexed = true; |
713 | 713 | } else { |
714 | 714 | $isIndexed = false; |
@@ -718,27 +718,27 @@ discard block |
||
718 | 718 | $isIndexed = true; |
719 | 719 | } |
720 | 720 | |
721 | - if ($isIndexed) { |
|
721 | + if($isIndexed) { |
|
722 | 722 | $termVectors = $this->getTermVectors(); |
723 | 723 | $termFields = array_keys($termVectors); |
724 | 724 | sort($termFields); |
725 | 725 | |
726 | - foreach ($termFields as $field) { |
|
726 | + foreach($termFields as $field) { |
|
727 | 727 | $terms = new \ArrayList(); |
728 | 728 | |
729 | - foreach (array_keys($termVectors[$field]['terms']) as $term) { |
|
729 | + foreach(array_keys($termVectors[$field]['terms']) as $term) { |
|
730 | 730 | $do = new \DataObject(); |
731 | 731 | $do->Term = $term; |
732 | 732 | $stats = $termVectors[$field]['terms'][$term]; |
733 | - if (isset($stats['ttf'])) { |
|
733 | + if(isset($stats['ttf'])) { |
|
734 | 734 | $do->TTF = $stats['ttf']; |
735 | 735 | } |
736 | 736 | |
737 | - if (isset($stats['doc_freq'])) { |
|
737 | + if(isset($stats['doc_freq'])) { |
|
738 | 738 | $do->DocFreq = $stats['doc_freq']; |
739 | 739 | } |
740 | 740 | |
741 | - if (isset($stats['term_freq'])) { |
|
741 | + if(isset($stats['term_freq'])) { |
|
742 | 742 | $do->TermFreq = $stats['term_freq']; |
743 | 743 | } |
744 | 744 | $terms->push($do); |
@@ -755,12 +755,12 @@ discard block |
||
755 | 755 | $underscored = str_replace('.', '_', $field); |
756 | 756 | |
757 | 757 | $gridField = new \GridField( |
758 | - 'TermsFor'.$underscored, // Field name |
|
759 | - $field.'TITLE'.$field, // Field title |
|
758 | + 'TermsFor' . $underscored, // Field name |
|
759 | + $field . 'TITLE' . $field, // Field title |
|
760 | 760 | $terms, |
761 | 761 | $config |
762 | 762 | ); |
763 | - $fields->addFieldToTab('Root.ElasticaTerms.'.$underscored, $gridField); |
|
763 | + $fields->addFieldToTab('Root.ElasticaTerms.' . $underscored, $gridField); |
|
764 | 764 | } |
765 | 765 | |
766 | 766 | } |
@@ -27,17 +27,17 @@ discard block |
||
27 | 27 | */ |
28 | 28 | public function __construct($controller, $name, $fields = null, $actions = null) { |
29 | 29 | $searchText = isset($this->Query) ? $this->Query : ''; |
30 | - $fields = new FieldList( |
|
31 | - $tf = new TextField("q", "", $searchText) |
|
32 | - ); |
|
30 | + $fields = new FieldList( |
|
31 | + $tf = new TextField("q", "", $searchText) |
|
32 | + ); |
|
33 | 33 | |
34 | 34 | |
35 | - $buttonText = _t('SearchPage.SEARCH', 'Search'); |
|
36 | - $actions = new FieldList( |
|
37 | - $fa = new FormAction('submit', $buttonText) |
|
38 | - ); |
|
35 | + $buttonText = _t('SearchPage.SEARCH', 'Search'); |
|
36 | + $actions = new FieldList( |
|
37 | + $fa = new FormAction('submit', $buttonText) |
|
38 | + ); |
|
39 | 39 | |
40 | - $this->SubmitButton = $fa; |
|
40 | + $this->SubmitButton = $fa; |
|
41 | 41 | |
42 | 42 | if(class_exists('Translatable') && singleton('SiteTree')->hasExtension('Translatable')) { |
43 | 43 | $fields->push(new HiddenField('searchlocale', 'searchlocale', Translatable::get_current_locale())); |
@@ -64,7 +64,7 @@ discard block |
||
64 | 64 | */ |
65 | 65 | public function setButtonText($newButtonText) { |
66 | 66 | $this->actions = new FieldList( |
67 | - $fa = new FormAction('submit', $newButtonText) |
|
68 | - ); |
|
67 | + $fa = new FormAction('submit', $newButtonText) |
|
68 | + ); |
|
69 | 69 | } |
70 | 70 | } |
@@ -233,6 +233,9 @@ |
||
233 | 233 | |
234 | 234 | |
235 | 235 | |
236 | + /** |
|
237 | + * @param PaginatedList $paginated |
|
238 | + */ |
|
236 | 239 | private function makeCode($paginated) { |
237 | 240 | $results = $paginated->getList()->toArray(); |
238 | 241 | $ctr = 0; |
@@ -49,7 +49,7 @@ discard block |
||
49 | 49 | $results = $es->moreLikeThis($fp, $fields, true); |
50 | 50 | |
51 | 51 | echo "RESULTS:\n"; |
52 | - foreach ($results as $result) { |
|
52 | + foreach($results as $result) { |
|
53 | 53 | echo "-\t{$result->Title}\n"; |
54 | 54 | } |
55 | 55 | |
@@ -129,7 +129,7 @@ discard block |
||
129 | 129 | $es->setClasses('FlickrPhotoTO'); |
130 | 130 | $fields = array('Title.standard' => 1, 'Description.standard' => 1); |
131 | 131 | $paginated = $es->moreLikeThis($fp, $fields, true); |
132 | - foreach ($paginated->getList() as $result) { |
|
132 | + foreach($paginated->getList() as $result) { |
|
133 | 133 | echo $result->ID . ' : ' . $result->Title . "\n"; |
134 | 134 | } |
135 | 135 | $this->assertEquals(32, $paginated->getTotalItems()); |
@@ -155,7 +155,7 @@ discard block |
||
155 | 155 | $es->setClasses('FlickrPhotoTO'); |
156 | 156 | $fields = array('Title.standard' => 1, 'Description.standard' => 1); |
157 | 157 | $paginated = $es->moreLikeThis($fp, $fields, true); |
158 | - foreach ($paginated->getList() as $result) { |
|
158 | + foreach($paginated->getList() as $result) { |
|
159 | 159 | echo $result->ID . ' : ' . $result->Title . "\n"; |
160 | 160 | } |
161 | 161 | $this->assertEquals(14, $paginated->getTotalItems()); |
@@ -211,16 +211,16 @@ discard block |
||
211 | 211 | $paginated = $es->search($query, $fields); |
212 | 212 | $ctr = 0; |
213 | 213 | |
214 | - foreach ($paginated->getList()->toArray() as $result) { |
|
214 | + foreach($paginated->getList()->toArray() as $result) { |
|
215 | 215 | $ctr++; |
216 | - foreach ($result->SearchHighlightsByField->Description_standard->getIterator() as $highlight) { |
|
216 | + foreach($result->SearchHighlightsByField->Description_standard->getIterator() as $highlight) { |
|
217 | 217 | $snippet = $highlight->Snippet; |
218 | 218 | $snippet = strtolower($snippet); |
219 | 219 | $wordFound = false; |
220 | 220 | $lcquery = explode(' ', strtolower($query)); |
221 | - foreach ($lcquery as $part) { |
|
221 | + foreach($lcquery as $part) { |
|
222 | 222 | $bracketed = '<strong class="hl">' . $part . '</strong>'; |
223 | - if (strpos($snippet, $bracketed) > 0) { |
|
223 | + if(strpos($snippet, $bracketed) > 0) { |
|
224 | 224 | $wordFound = true; |
225 | 225 | } |
226 | 226 | } |
@@ -240,17 +240,17 @@ discard block |
||
240 | 240 | $paginated = $es->search($query, $fields); |
241 | 241 | $ctr = 0; |
242 | 242 | |
243 | - foreach ($paginated->getList()->toArray() as $result) { |
|
243 | + foreach($paginated->getList()->toArray() as $result) { |
|
244 | 244 | $ctr++; |
245 | 245 | |
246 | - foreach ($result->SearchHighlightsByField->Description->getIterator() as $highlight) { |
|
246 | + foreach($result->SearchHighlightsByField->Description->getIterator() as $highlight) { |
|
247 | 247 | $snippet = $highlight->Snippet; |
248 | 248 | $snippet = strtolower($snippet); |
249 | 249 | $wordFound = false; |
250 | 250 | $lcquery = explode(' ', strtolower($query)); |
251 | - foreach ($lcquery as $part) { |
|
251 | + foreach($lcquery as $part) { |
|
252 | 252 | $bracketed = '<strong class="hl">' . $part . '</strong>'; |
253 | - if (strpos($snippet, $bracketed) > 0) { |
|
253 | + if(strpos($snippet, $bracketed) > 0) { |
|
254 | 254 | $wordFound = true; |
255 | 255 | } |
256 | 256 | } |
@@ -267,7 +267,7 @@ discard block |
||
267 | 267 | $query = 'Lond'; |
268 | 268 | $results = $es->autocomplete_search($query, 'Title'); |
269 | 269 | $this->assertEquals(7, $results->getTotalItems()); |
270 | - foreach ($results->toArray() as $result) { |
|
270 | + foreach($results->toArray() as $result) { |
|
271 | 271 | $this->assertTrue(strpos($result->Title, $query) > 0); |
272 | 272 | } |
273 | 273 | } |
@@ -277,7 +277,7 @@ discard block |
||
277 | 277 | $results = $paginated->getList()->toArray(); |
278 | 278 | $ctr = 0; |
279 | 279 | echo '$result = $paginated->getList()->toArray();' . "\n"; |
280 | - foreach ($results as $result) { |
|
280 | + foreach($results as $result) { |
|
281 | 281 | echo '$this->assertEquals("' . $result->Title . '", $results[' . $ctr . ']->Title);' . "\n"; |
282 | 282 | $ctr++; |
283 | 283 | } |
@@ -479,6 +479,9 @@ |
||
479 | 479 | } |
480 | 480 | |
481 | 481 | |
482 | + /** |
|
483 | + * @param string $query |
|
484 | + */ |
|
482 | 485 | private function getResultsFor($query, $pageLength = 10, $fields = array('Title' => 1, 'Description' => 1)) { |
483 | 486 | $es = new ElasticSearcher(); |
484 | 487 | $es->setStart(0); |
@@ -540,7 +540,7 @@ |
||
540 | 540 | |
541 | 541 | // MUST REMOVE FIRST. Otherwise append and the erroroneus value above still exists |
542 | 542 | $config->remove('FlickrPhotoTO', 'searchable_relationships'); |
543 | - $config->update('FlickrPhotoTO' ,'searchable_relationships', $sr); |
|
543 | + $config->update('FlickrPhotoTO', 'searchable_relationships', $sr); |
|
544 | 544 | } |
545 | 545 | |
546 | 546 |
@@ -9,7 +9,7 @@ |
||
9 | 9 | public function setUp() { |
10 | 10 | // this needs to be called in order to create the list of searchable |
11 | 11 | // classes and fields that are available. Simulates part of a build |
12 | - $classes = array('SearchableTestPage','SiteTree','Page','FlickrPhotoTO','FlickrSetTO', |
|
12 | + $classes = array('SearchableTestPage', 'SiteTree', 'Page', 'FlickrPhotoTO', 'FlickrSetTO', |
|
13 | 13 | 'FlickrTagTO', 'FlickrAuthorTO', 'FlickrSetTO'); |
14 | 14 | $this->requireDefaultRecordsFrom = $classes; |
15 | 15 |
@@ -133,7 +133,7 @@ |
||
133 | 133 | * |
134 | 134 | * @param \Elastica\Query|string|array $query |
135 | 135 | * @param string|array $types List of comma separated SilverStripe classes to search, or blank for all |
136 | - * @return \Elastica\ResultList |
|
136 | + * @return \Elastica\ResultSet |
|
137 | 137 | */ |
138 | 138 | public function search($query, $types = '') { |
139 | 139 | $query = Query::create($query); // may be a string |
@@ -27,7 +27,7 @@ discard block |
||
27 | 27 | parent::requireDefaultRecords(); |
28 | 28 | |
29 | 29 | $similar = AutoCompleteOption::get()->filter('Name', 'Similar')->first(); |
30 | - if (!$similar) { |
|
30 | + if(!$similar) { |
|
31 | 31 | $similar = new AutoCompleteOption(); |
32 | 32 | $similar->Name = 'Similar'; |
33 | 33 | $similar->Slug = 'SIMILAR'; |
@@ -37,7 +37,7 @@ discard block |
||
37 | 37 | } |
38 | 38 | |
39 | 39 | $search = AutoCompleteOption::get()->filter('Name', 'Search')->first(); |
40 | - if (!$search) { |
|
40 | + if(!$search) { |
|
41 | 41 | $search = new AutoCompleteOption(); |
42 | 42 | $search->Name = 'Search'; |
43 | 43 | $search->Description = 'Find records similar to the selected item'; |
@@ -47,7 +47,7 @@ discard block |
||
47 | 47 | } |
48 | 48 | |
49 | 49 | $goto = AutoCompleteOption::get()->filter('Name', 'GoToRecord')->first(); |
50 | - if (!$goto) { |
|
50 | + if(!$goto) { |
|
51 | 51 | $goto = new AutoCompleteOption(); |
52 | 52 | $goto->Name = 'GoToRecord'; |
53 | 53 | $goto->Description = 'Go to the page of the selected item, found by the Link() method'; |
@@ -20,24 +20,24 @@ |
||
20 | 20 | // Makes most sense to only provide one field here, e.g. Title, Name |
21 | 21 | $field = $this->request->getVar('field'); |
22 | 22 | |
23 | - error_log('QUERY:'.$query); |
|
23 | + error_log('QUERY:' . $query); |
|
24 | 24 | |
25 | 25 | // start, and page length, i.e. pagination |
26 | 26 | $es->setPageLength(10); |
27 | - if ($classes) { |
|
27 | + if($classes) { |
|
28 | 28 | $es->setClasses($classes); |
29 | 29 | } |
30 | 30 | |
31 | - if ($filter) { |
|
31 | + if($filter) { |
|
32 | 32 | $es->addFilter('InSiteTree', true); |
33 | 33 | } |
34 | 34 | |
35 | - $resultList = $es->autocomplete_search($query,$field); |
|
35 | + $resultList = $es->autocomplete_search($query, $field); |
|
36 | 36 | $result = array(); |
37 | 37 | $result['Query'] = $query; |
38 | 38 | $suggestions = array(); |
39 | 39 | |
40 | - foreach ($resultList->getResults() as $singleResult) { |
|
40 | + foreach($resultList->getResults() as $singleResult) { |
|
41 | 41 | $suggestion = array('value' => $singleResult->Title); |
42 | 42 | $suggestion['data'] = array( |
43 | 43 | 'ID' => $singleResult->getParam('_id'), |