MINOR Added FulltextSearchable::get_searchable_classes() in order to introspect currently searchable classes, added FulltextSearchableTest, added documentation

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.4@111789 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Ingo Schommer 2010-10-07 06:49:40 +00:00 committed by Sam Minnee
parent 25aa5d3c8a
commit feefb11dae
3 changed files with 88 additions and 4 deletions

View File

@ -28,7 +28,7 @@ class ContentControllerSearchExtension extends Extension {
new FormAction('results', _t('SearchForm.GO', 'Go')) new FormAction('results', _t('SearchForm.GO', 'Go'))
); );
$form = new SearchForm($this->owner, 'SearchForm', $fields, $actions); $form = new SearchForm($this->owner, 'SearchForm', $fields, $actions);
$form->classesToSearch(FulltextSearchable::$searchableClasses); $form->classesToSearch(FulltextSearchable::get_searchable_classes());
return $form; return $form;
} }

View File

@ -1,16 +1,41 @@
<?php <?php
/** /**
* Provides a simple search engine for your site based on the MySQL FULLTEXT index * Provides a simple search engine for your site based on the MySQL FULLTEXT index.
* Adds the {@link FulltextSearchable} extension to data classes,
* as well as the {@link ContentControllerSearchExtension} to {@link ContentController}.
* (this means you can use $SearchForm in your template without changing your own implementation).
*
* @see http://doc.silverstripe.org/tutorial:4-site-search
* *
* @package sapphire * @package sapphire
* @subpackage search * @subpackage search
*/ */
class FulltextSearchable extends DataObjectDecorator { class FulltextSearchable extends DataObjectDecorator {
/**
* @var String Comma-separated list of database column names
* that can be searched on. Used for generation of the database index defintions.
*/
protected $searchFields; protected $searchFields;
static $searchableClasses;
/**
* @var Array List of class names
*/
protected static $searchable_classes;
/** /**
* Enable the default configuration of MySQL full-text searching on the given data classes. * Enable the default configuration of MySQL full-text searching on the given data classes.
* It can be used to limit the searched classes, but not to add your own classes.
* For this purpose, please use {@link Object::add_extension()} directly:
* <code>
* Object::add_extension('MyObject', "FulltextSearchable('MySearchableField,'MyOtherField')");
* </code>
*
* Caution: This is a wrapper method that should only be used in _config.php,
* and only be called once in your code.
*
* @param Array $searchableClasses The extension will be applied to all DataObject subclasses
* listed here. Default: {@link SiteTree} and {@link File}.
*/ */
static function enable($searchableClasses = array('SiteTree', 'File')) { static function enable($searchableClasses = array('SiteTree', 'File')) {
$defaultColumns = array( $defaultColumns = array(
@ -26,14 +51,19 @@ class FulltextSearchable extends DataObjectDecorator {
throw new Exception("FulltextSearchable::enable() I don't know the default search columns for class '$class'"); throw new Exception("FulltextSearchable::enable() I don't know the default search columns for class '$class'");
} }
} }
self::$searchableClasses = $searchableClasses; self::$searchable_classes = $searchableClasses;
Object::add_extension("ContentController", "ContentControllerSearchExtension"); Object::add_extension("ContentController", "ContentControllerSearchExtension");
} }
/**
* @param Array|String $searchFields Comma-separated list (or array) of database column names
* that can be searched on. Used for generation of the database index defintions.
*/
function __construct($searchFields) { function __construct($searchFields) {
if(is_array($searchFields)) $this->searchFields = implode(',', $searchFields); if(is_array($searchFields)) $this->searchFields = implode(',', $searchFields);
else $this->searchFields = $searchFields; else $this->searchFields = $searchFields;
parent::__construct(); parent::__construct();
} }
@ -52,4 +82,14 @@ class FulltextSearchable extends DataObjectDecorator {
); );
} }
} }
/**
* Shows all classes that had the {@link FulltextSearchable} extension applied through {@link enable()}.
*
* @return Array
*/
function get_searchable_classes() {
return self::$searchable_classes;
}
} }

View File

@ -0,0 +1,44 @@
<?php
/**
* @package sapphire
* @subpackage tests
*/
class FulltextSearchableTest extends SapphireTest {
function setUp() {
parent::setUp();
$this->orig['File_searchable'] = Object::has_extension('File', 'FulltextSearchable');
$this->orig['SiteTree_searchable'] = Object::has_extension('SiteTree', 'FulltextSearchable');
// TODO This shouldn't need all arguments included
Object::remove_extension('File', 'FulltextSearchable(\'Filename,Title,Content\')');
Object::remove_extension('SiteTree', 'FulltextSearchable(\'Title,MenuTitle,Content,MetaTitle,MetaDescription,MetaKeywords\')');
}
function tearDown() {
// TODO This shouldn't need all arguments included
if($this->orig['File_searchable']) Object::add_extension('File', 'FulltextSearchable(\'Filename,Title,Content\')');
if($this->orig['SiteTree_searchable']) Object::add_extension('SiteTree', 'FulltextSearchable(\'Title,MenuTitle,Content,MetaTitle,MetaDescription,MetaKeywords\')');
parent::tearDown();
}
function testEnable() {
FulltextSearchable::enable();
$this->assertTrue(Object::has_extension('SiteTree', 'FulltextSearchable'));
$this->assertTrue(Object::has_extension('File', 'FulltextSearchable'));
}
function testEnableWithCustomClasses() {
FulltextSearchable::enable(array('SiteTree'));
$this->assertTrue(Object::has_extension('SiteTree', 'FulltextSearchable'));
// TODO This shouldn't need all arguments included
Object::remove_extension('File', 'FulltextSearchable(\'Filename,Title,Content\')');
$this->assertFalse(Object::has_extension('File', 'FulltextSearchable'));
}
}