silverstripe-framework/search/FulltextSearchable.php
Marcus Nyeholt 82495f5a7e BUGFIX Versioned's constructor doesn't provide suitable defaults. Previously a bug/feature in singleton, where it would pass null,true as params to strong_create, which would then get passed through as params to Versioned's constructor, meant that the code still executed fine (as was set to something that wasn't an array, so the null and true were instead taken as args). The fact that the usage of singleton(Versioned) never really used the classes code, purely for value lookup, meant that this never propagated errors. I've now switched singleton() to use the injector for retrieving values, which means these dud values are no longer passed through
CHANGE Given that Config::inst is an implementation of the singleton pattern itself, I've removed the extra call to singleton(). A side effect of this is that it gets around a possibly nasty circular reference with the dependency injector (which relies on the config object); in future, this dependency structure should really be structured from the DI directly.

MINOR Change singleton and strong_create to use dependency injector

BUGFIX: Provide default constructor values for classes (fixes issues when used in 'singleton' scenario during dev/build in particular)

MINOR Clear out injector state when resetting db schema during tests (a follow on from changing singleton() calls to use the injector underneath)
2012-05-23 21:10:04 +10:00

98 lines
3.4 KiB
PHP

<?php
/**
* 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}
* (if the 'cms' module is available as well).
* (this means you can use $SearchForm in your template without changing your own implementation).
*
* CAUTION: Will make all files in your /assets folder searchable by file name
* unless "File" is excluded from FulltextSearchable::enable().
*
* @see http://doc.silverstripe.org/tutorial:4-site-search
*
* @package framework
* @subpackage search
*/
class FulltextSearchable extends DataExtension {
/**
* @var String Comma-separated list of database column names
* that can be searched on. Used for generation of the database index defintions.
*/
protected $searchFields;
/**
* @var Array List of class names
*/
protected static $searchable_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')) {
$defaultColumns = array(
'SiteTree' => 'Title,MenuTitle,Content,MetaTitle,MetaDescription,MetaKeywords',
'File' => 'Filename,Title,Content'
);
if(!is_array($searchableClasses)) $searchableClasses = array($searchableClasses);
foreach($searchableClasses as $class) {
if(!class_exists($class)) continue;
if(isset($defaultColumns[$class])) {
Config::inst()->update($class, 'create_table_options', array('MySQLDatabase' => 'ENGINE=MyISAM'));
Object::add_extension($class, "FulltextSearchable('{$defaultColumns[$class]}')");
} else {
throw new Exception("FulltextSearchable::enable() I don't know the default search columns for class '$class'");
}
}
self::$searchable_classes = $searchableClasses;
if(class_exists("ContentController")){
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 = array()) {
if(is_array($searchFields)) $this->searchFields = implode(',', $searchFields);
else $this->searchFields = $searchFields;
parent::__construct();
}
static function add_to_class($class, $extensionClass, $args = null) {
Config::inst()->update($class, 'indexes', array('SearchFields' => array(
'type' => 'fulltext',
'name' => 'SearchFields',
'value' => $args[0]
)));
parent::add_to_class($class, $extensionClass, $args);
}
/**
* Shows all classes that had the {@link FulltextSearchable} extension applied through {@link enable()}.
*
* @return Array
*/
static function get_searchable_classes() {
return self::$searchable_classes;
}
}