2010-10-04 06:32:48 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2010-10-19 06:53:36 +02:00
|
|
|
* Provides a simple search engine for your site based on the MySQL FULLTEXT index.
|
|
|
|
* Adds the {@link FulltextSearchable} extension to data classes,
|
2011-03-29 06:55:13 +02:00
|
|
|
* as well as the {@link ContentControllerSearchExtension} to {@link ContentController}
|
|
|
|
* (if the 'cms' module is available as well).
|
2010-10-19 06:53:36 +02:00
|
|
|
* (this means you can use $SearchForm in your template without changing your own implementation).
|
|
|
|
*
|
2011-12-03 12:00:56 +01:00
|
|
|
* CAUTION: Will make all files in your /assets folder searchable by file name
|
|
|
|
* unless "File" is excluded from FulltextSearchable::enable().
|
|
|
|
*
|
2010-10-19 06:53:36 +02:00
|
|
|
* @see http://doc.silverstripe.org/tutorial:4-site-search
|
2010-10-19 05:54:51 +02:00
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2010-10-13 05:53:12 +02:00
|
|
|
* @subpackage search
|
2010-10-04 06:32:48 +02:00
|
|
|
*/
|
2011-04-15 11:35:30 +02:00
|
|
|
class FulltextSearchable extends DataExtension {
|
2010-10-19 06:53:36 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var String Comma-separated list of database column names
|
|
|
|
* that can be searched on. Used for generation of the database index defintions.
|
|
|
|
*/
|
2010-10-04 06:32:48 +02:00
|
|
|
protected $searchFields;
|
2010-10-19 06:53:36 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Array List of class names
|
|
|
|
*/
|
|
|
|
protected static $searchable_classes;
|
2010-10-19 05:54:51 +02:00
|
|
|
|
2010-10-04 06:32:48 +02:00
|
|
|
/**
|
|
|
|
* Enable the default configuration of MySQL full-text searching on the given data classes.
|
2010-10-19 06:53:36 +02:00
|
|
|
* 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}.
|
2010-10-04 06:32:48 +02:00
|
|
|
*/
|
|
|
|
static function enable($searchableClasses = array('SiteTree', 'File')) {
|
|
|
|
$defaultColumns = array(
|
|
|
|
'SiteTree' => 'Title,MenuTitle,Content,MetaTitle,MetaDescription,MetaKeywords',
|
|
|
|
'File' => 'Filename,Title,Content'
|
|
|
|
);
|
2010-10-19 05:54:51 +02:00
|
|
|
|
2010-10-04 06:32:48 +02:00
|
|
|
if(!is_array($searchableClasses)) $searchableClasses = array($searchableClasses);
|
|
|
|
foreach($searchableClasses as $class) {
|
2011-03-23 04:32:24 +01:00
|
|
|
if(!class_exists($class)) continue;
|
|
|
|
|
2010-10-04 06:32:48 +02:00
|
|
|
if(isset($defaultColumns[$class])) {
|
2012-03-25 09:38:29 +02:00
|
|
|
Config::inst()->update($class, 'create_table_options', array('MySQLDatabase' => 'ENGINE=MyISAM'));
|
2010-10-13 03:26:51 +02:00
|
|
|
Object::add_extension($class, "FulltextSearchable('{$defaultColumns[$class]}')");
|
2010-10-04 06:32:48 +02:00
|
|
|
} else {
|
2010-10-13 03:26:51 +02:00
|
|
|
throw new Exception("FulltextSearchable::enable() I don't know the default search columns for class '$class'");
|
2010-10-04 06:32:48 +02:00
|
|
|
}
|
|
|
|
}
|
2010-10-19 06:53:36 +02:00
|
|
|
self::$searchable_classes = $searchableClasses;
|
2011-03-18 03:01:09 +01:00
|
|
|
if(class_exists("ContentController")){
|
|
|
|
Object::add_extension("ContentController", "ContentControllerSearchExtension");
|
|
|
|
}
|
2010-10-04 06:32:48 +02:00
|
|
|
}
|
2010-10-19 05:54:51 +02:00
|
|
|
|
2010-10-19 06:53:36 +02:00
|
|
|
/**
|
|
|
|
* @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.
|
|
|
|
*/
|
2010-10-04 06:32:48 +02:00
|
|
|
function __construct($searchFields) {
|
|
|
|
if(is_array($searchFields)) $this->searchFields = implode(',', $searchFields);
|
|
|
|
else $this->searchFields = $searchFields;
|
2010-10-19 06:53:36 +02:00
|
|
|
|
2010-10-04 06:32:48 +02:00
|
|
|
parent::__construct();
|
|
|
|
}
|
2010-10-19 05:54:51 +02:00
|
|
|
|
2012-04-12 02:06:02 +02:00
|
|
|
static function add_to_class($class, $extensionClass, $args = null) {
|
2012-03-12 04:14:47 +01:00
|
|
|
Config::inst()->update($class, 'indexes', array('SearchFields' => array(
|
|
|
|
'type' => 'fulltext',
|
|
|
|
'name' => 'SearchFields',
|
|
|
|
'value' => $args[0]
|
|
|
|
)));
|
2010-10-19 05:54:51 +02:00
|
|
|
|
2012-03-12 04:14:47 +01:00
|
|
|
parent::add_to_class($class, $extensionClass, $args);
|
2010-10-04 06:32:48 +02:00
|
|
|
}
|
2012-03-12 04:14:47 +01:00
|
|
|
|
2010-10-19 06:53:36 +02:00
|
|
|
/**
|
|
|
|
* Shows all classes that had the {@link FulltextSearchable} extension applied through {@link enable()}.
|
|
|
|
*
|
|
|
|
* @return Array
|
|
|
|
*/
|
2012-04-12 02:06:02 +02:00
|
|
|
static function get_searchable_classes() {
|
2010-10-19 06:53:36 +02:00
|
|
|
return self::$searchable_classes;
|
|
|
|
}
|
|
|
|
|
2011-10-29 06:12:02 +02:00
|
|
|
}
|