@@ -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); |
@@ -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 | |
@@ -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'), |
@@ -22,7 +22,7 @@ discard block |
||
| 22 | 22 | //return false; |
| 23 | 23 | // |
| 24 | 24 | |
| 25 | - if ($data['ClassesToSearch'] == array()) { |
|
| 25 | + if($data['ClassesToSearch'] == array()) { |
|
| 26 | 26 | $data['ClassesToSearch'] = ''; |
| 27 | 27 | } |
| 28 | 28 | |
@@ -30,8 +30,8 @@ discard block |
||
| 30 | 30 | echo "STDATA:$debug\n"; |
| 31 | 31 | |
| 32 | 32 | // Check if any classes to search if site tree only is not ticked |
| 33 | - if (!$data['SiteTreeOnly']) { |
|
| 34 | - if (!$data['ClassesToSearch']) { |
|
| 33 | + if(!$data['SiteTreeOnly']) { |
|
| 34 | + if(!$data['ClassesToSearch']) { |
|
| 35 | 35 | $valid = false; |
| 36 | 36 | $this->validationError("ClassesToSearch", |
| 37 | 37 | "Please provide at least one class to search, or select 'Site Tree Only'", |
@@ -39,15 +39,15 @@ discard block |
||
| 39 | 39 | ); |
| 40 | 40 | } else { |
| 41 | 41 | $toSearch = $data['ClassesToSearch']; |
| 42 | - foreach ($toSearch as $clazz) { |
|
| 42 | + foreach($toSearch as $clazz) { |
|
| 43 | 43 | try { |
| 44 | 44 | $instance = Injector::inst()->create($clazz); |
| 45 | - if (!$instance->hasExtension('SilverStripe\Elastica\Searchable')) { |
|
| 46 | - $this->validationError('ClassesToSearch', 'The class '.$clazz.' must have the Searchable extension'); |
|
| 45 | + if(!$instance->hasExtension('SilverStripe\Elastica\Searchable')) { |
|
| 46 | + $this->validationError('ClassesToSearch', 'The class ' . $clazz . ' must have the Searchable extension'); |
|
| 47 | 47 | } |
| 48 | 48 | } catch (ReflectionException $e) { |
| 49 | 49 | $this->validationError("ClassesToSearch", |
| 50 | - 'The class '.$clazz.' does not exist', |
|
| 50 | + 'The class ' . $clazz . ' does not exist', |
|
| 51 | 51 | 'error' |
| 52 | 52 | ); |
| 53 | 53 | } |
@@ -58,23 +58,23 @@ discard block |
||
| 58 | 58 | |
| 59 | 59 | // Check the identifier is unique |
| 60 | 60 | $mode = Versioned::get_reading_mode(); |
| 61 | - $suffix = ''; |
|
| 62 | - if ($mode == 'Stage.Live') { |
|
| 61 | + $suffix = ''; |
|
| 62 | + if($mode == 'Stage.Live') { |
|
| 63 | 63 | $suffix = '_Live'; |
| 64 | 64 | } |
| 65 | - $where = 'ElasticSearchPage'.$suffix.'.ID != '.$data['ID']." AND `Identifier` = '".$data['Identifier']."'"; |
|
| 65 | + $where = 'ElasticSearchPage' . $suffix . '.ID != ' . $data['ID'] . " AND `Identifier` = '" . $data['Identifier'] . "'"; |
|
| 66 | 66 | $existing = ElasticSearchPage::get()->where($where)->count(); |
| 67 | - if ($existing > 0) { |
|
| 67 | + if($existing > 0) { |
|
| 68 | 68 | $valid = false; |
| 69 | 69 | $this->validationError('Identifier', |
| 70 | - 'The identifier '.$data['Identifier'].' already exists', |
|
| 70 | + 'The identifier ' . $data['Identifier'] . ' already exists', |
|
| 71 | 71 | 'error' |
| 72 | 72 | ); |
| 73 | 73 | } |
| 74 | 74 | |
| 75 | 75 | |
| 76 | 76 | // Check number of results per page >= 1 |
| 77 | - if ($data['ResultsPerPage'] <= 0) { |
|
| 77 | + if($data['ResultsPerPage'] <= 0) { |
|
| 78 | 78 | $valid = false; |
| 79 | 79 | $this->validationError('ResultsPerPage', |
| 80 | 80 | 'Results per page must be >=1' |
@@ -12,7 +12,7 @@ discard block |
||
| 12 | 12 | |
| 13 | 13 | $searchPage = $this->getSearchPage($identifier); |
| 14 | 14 | |
| 15 | - if ($searchPage) { |
|
| 15 | + if($searchPage) { |
|
| 16 | 16 | $result = $searchPage->AbsoluteLink(); |
| 17 | 17 | } |
| 18 | 18 | return $result; |
@@ -24,7 +24,7 @@ discard block |
||
| 24 | 24 | |
| 25 | 25 | $searchPage = $this->getSearchPage($identifier); |
| 26 | 26 | |
| 27 | - if ($searchPage) { |
|
| 27 | + if($searchPage) { |
|
| 28 | 28 | $result = $searchPage->SearchForm($buttonTextOverride); |
| 29 | 29 | } |
| 30 | 30 | return $result; |
@@ -32,7 +32,7 @@ discard block |
||
| 32 | 32 | |
| 33 | 33 | |
| 34 | 34 | public function getSearchPage($identifier) { |
| 35 | - if (!isset($this->_CachedLastEdited)) { |
|
| 35 | + if(!isset($this->_CachedLastEdited)) { |
|
| 36 | 36 | $this->_CachedLastEdited = ElasticSearchPage::get()->max('LastEdited'); |
| 37 | 37 | } |
| 38 | 38 | $ck = $this->_CachedLastEdited; |
@@ -42,10 +42,10 @@ discard block |
||
| 42 | 42 | |
| 43 | 43 | $cache = SS_Cache::factory('searchpagecache'); |
| 44 | 44 | $searchPage = null; |
| 45 | - $cachekeyname = 'searchpageuri'.$identifier.$this->owner->Locale.$ck; |
|
| 45 | + $cachekeyname = 'searchpageuri' . $identifier . $this->owner->Locale . $ck; |
|
| 46 | 46 | |
| 47 | 47 | if(!($searchPage = unserialize($cache->load($cachekeyname)))) { |
| 48 | - $searchPage = ElasticSearchPage::get()->filter('Identifier',$identifier)->first(); |
|
| 48 | + $searchPage = ElasticSearchPage::get()->filter('Identifier', $identifier)->first(); |
|
| 49 | 49 | $cache->save(serialize($searchPage), $cachekeyname); |
| 50 | 50 | } |
| 51 | 51 | return $searchPage; |
@@ -27,13 +27,13 @@ discard block |
||
| 27 | 27 | */ |
| 28 | 28 | |
| 29 | 29 | public function run($request) { |
| 30 | - $message = function ($content) { |
|
| 30 | + $message = function($content) { |
|
| 31 | 31 | print(\Director::is_cli() ? "$content\n" : "<p>$content</p>"); |
| 32 | 32 | }; |
| 33 | 33 | |
| 34 | 34 | $locales = array(); |
| 35 | 35 | if(class_exists('Translatable') && singleton('SiteTree')->hasExtension('Translatable')) { |
| 36 | - foreach (\Translatable::get_existing_content_languages('SiteTree') as $code => $val) { |
|
| 36 | + foreach(\Translatable::get_existing_content_languages('SiteTree') as $code => $val) { |
|
| 37 | 37 | array_push($locales, $code); |
| 38 | 38 | } |
| 39 | 39 | } else { |
@@ -41,11 +41,11 @@ discard block |
||
| 41 | 41 | } |
| 42 | 42 | |
| 43 | 43 | // now iterate all the locales indexing each locale in turn using it's owner index settings |
| 44 | - foreach ($locales as $locale) { |
|
| 44 | + foreach($locales as $locale) { |
|
| 45 | 45 | Searchable::$index_ctr = 0; |
| 46 | - $message('Indexing locale '.$locale); |
|
| 46 | + $message('Indexing locale ' . $locale); |
|
| 47 | 47 | |
| 48 | - if (class_exists('Translatable')) { |
|
| 48 | + if(class_exists('Translatable')) { |
|
| 49 | 49 | \Translatable::set_current_locale($locale); |
| 50 | 50 | } |
| 51 | 51 | |
@@ -62,10 +62,10 @@ discard block |
||
| 62 | 62 | $this->service->refresh(); |
| 63 | 63 | // display indexing speed stats |
| 64 | 64 | $endTime = microtime(true); |
| 65 | - $elapsed = $endTime-$startTime; |
|
| 65 | + $elapsed = $endTime - $startTime; |
|
| 66 | 66 | $perSecond = Searchable::$index_ctr / $elapsed; |
| 67 | - $info = "\nReindexing $locale completed \n ".Searchable::$index_ctr." docs in ".round($elapsed,2)." seconds "; |
|
| 68 | - $info .= "at ".round($perSecond,2)." documents per second\n\n"; |
|
| 67 | + $info = "\nReindexing $locale completed \n " . Searchable::$index_ctr . " docs in " . round($elapsed, 2) . " seconds "; |
|
| 68 | + $info .= "at " . round($perSecond, 2) . " documents per second\n\n"; |
|
| 69 | 69 | $message($info); |
| 70 | 70 | } |
| 71 | 71 | |