From ce173efce39b539de5f799c210aac244da5e1be4 Mon Sep 17 00:00:00 2001 From: Sam Minnee Date: Mon, 4 Oct 2010 04:32:48 +0000 Subject: [PATCH] API CHANGE: Made MySQL fulltext search optional, activated with MySQLFulltextSearchable::enable() (from r101044) git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@111569 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- core/model/SiteTree.php | 3 -- filesystem/File.php | 4 -- search/ContentControllerSearchExtension.php | 36 +++++++++++++++ search/MySQLFulltextSearchable.php | 51 +++++++++++++++++++++ tests/search/SearchFormTest.php | 12 +++++ tests/search/TranslatableSearchFormTest.php | 11 ++++- 6 files changed, 109 insertions(+), 8 deletions(-) create mode 100644 search/ContentControllerSearchExtension.php create mode 100644 search/MySQLFulltextSearchable.php diff --git a/core/model/SiteTree.php b/core/model/SiteTree.php index 93a055934..009071e87 100755 --- a/core/model/SiteTree.php +++ b/core/model/SiteTree.php @@ -84,9 +84,6 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid ); static $indexes = array( - "SearchFields" => Array('type'=>'fulltext', 'name'=>'SearchFields', 'value'=>'Title, MenuTitle, Content, MetaTitle, MetaDescription, MetaKeywords'), - //"TitleSearchFields" => Array('type'=>'fulltext', 'value'=>'Title'), - //"ContentSearchFields" => Array('type'=>'fulltext', 'value'=>'Content'), "URLSegment" => true, ); diff --git a/filesystem/File.php b/filesystem/File.php index 2fdbf8f4f..7c0945f28 100755 --- a/filesystem/File.php +++ b/filesystem/File.php @@ -24,10 +24,6 @@ class File extends DataObject { "Sort" => "Int" ); - static $indexes = array( - "SearchFields" => Array('type'=>'fulltext','name'=>'SearchFields', 'value'=>'Filename,Title,Content'), - ); - static $has_one = array( "Parent" => "File", "Owner" => "Member" diff --git a/search/ContentControllerSearchExtension.php b/search/ContentControllerSearchExtension.php new file mode 100644 index 000000000..e24f1143f --- /dev/null +++ b/search/ContentControllerSearchExtension.php @@ -0,0 +1,36 @@ +owner, 'SearchForm', $fields, $actions); + } + + /** + * Process and render search results. + * + * @param array $data The raw request data submitted by user + * @param SearchForm $form The form instance that was submitted + * @param SS_HTTPRequest $request Request generated for this action + */ + function results($data, $form, $request) { + $data = array( + 'Results' => $form->getResults(), + 'Query' => $form->getSearchQuery(), + 'Title' => 'Search Results' + ); + return $this->owner->customise($data)->renderWith(array('Page_results', 'Page')); + } +} \ No newline at end of file diff --git a/search/MySQLFulltextSearchable.php b/search/MySQLFulltextSearchable.php new file mode 100644 index 000000000..2f7311c19 --- /dev/null +++ b/search/MySQLFulltextSearchable.php @@ -0,0 +1,51 @@ + 'Title,MenuTitle,Content,MetaTitle,MetaDescription,MetaKeywords', + 'File' => 'Filename,Title,Content' + ); + + if(!is_array($searchableClasses)) $searchableClasses = array($searchableClasses); + foreach($searchableClasses as $class) { + if(isset($defaultColumns[$class])) { + Object::add_extension($class, "MySQLFulltextSearchable('{$defaultColumns[$class]}')"); + } else { + throw new Exception("MySQLFUlltextSearchable::enable() I don't know the default search columns for class '$class'"); + } + } + + Object::add_extension("ContentController", "ContentControllerSearchExtension"); + } + + function __construct($searchFields) { + if(is_array($searchFields)) $this->searchFields = implode(',', $searchFields); + else $this->searchFields = $searchFields; + parent::__construct(); + } + + function extraStatics($class = null, $extension = null) { + if($extension && preg_match('/\([\'"](.*)[\'"]\)/', $extension, $matches)) { + $searchFields = $matches[1]; + + return array( + 'indexes' => array( + "SearchFields" => Array( + 'type'=>'fulltext', + 'name'=>'SearchFields', + 'value'=> $searchFields + ), + ) + ); + } + } +} \ No newline at end of file diff --git a/tests/search/SearchFormTest.php b/tests/search/SearchFormTest.php index 7ea611aa9..90b170d88 100644 --- a/tests/search/SearchFormTest.php +++ b/tests/search/SearchFormTest.php @@ -12,6 +12,18 @@ class SearchFormTest extends FunctionalTest { protected $mockController; + protected $requiredExtensions = array( + "SiteTree" => array( + "MySQLFulltextSearchable('Title,MenuTitle,Content,MetaTitle,MetaDescription,MetaKeywords')", + ), + "File" => array( + "MySQLFulltextSearchable('Filename,Title,Content')", + ), + "ContentController" => array( + "ContentControllerSearchExtension", + ), + ); + function setUp() { parent::setUp(); diff --git a/tests/search/TranslatableSearchFormTest.php b/tests/search/TranslatableSearchFormTest.php index 44cc5043f..7a805191b 100644 --- a/tests/search/TranslatableSearchFormTest.php +++ b/tests/search/TranslatableSearchFormTest.php @@ -10,7 +10,16 @@ class TranslatableSearchFormTest extends FunctionalTest { protected $mockController; protected $requiredExtensions = array( - 'SiteTree' => array('Translatable'), + 'SiteTree' => array( + 'Translatable', + "MySQLFulltextSearchable('Title,MenuTitle,Content,MetaTitle,MetaDescription,MetaKeywords')", + ), + "File" => array( + "MySQLFulltextSearchable('Filename,Title,Content')", + ), + "ContentController" => array( + "ContentControllerSearchExtension", + ), ); function setUp() {