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).
|
|
|
|
*
|
|
|
|
* @see http://doc.silverstripe.org/tutorial:4-site-search
|
2010-10-19 05:54:51 +02:00
|
|
|
*
|
2010-10-13 05:53:12 +02:00
|
|
|
* @package sapphire
|
|
|
|
* @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])) {
|
2011-10-29 05:48:09 +02:00
|
|
|
if(DB::getConn()->getDatabaseServer() == 'mysql') {
|
|
|
|
Object::add_static_var($class, 'create_table_options', array('MySQLDatabase' => 'ENGINE=MyISAM'), true);
|
|
|
|
}
|
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
|
|
|
|
2010-10-04 06:32:48 +02:00
|
|
|
function extraStatics($class = null, $extension = null) {
|
|
|
|
if($extension && preg_match('/\([\'"](.*)[\'"]\)/', $extension, $matches)) {
|
|
|
|
$searchFields = $matches[1];
|
2010-10-19 05:54:51 +02:00
|
|
|
|
2010-10-04 06:32:48 +02:00
|
|
|
return array(
|
|
|
|
'indexes' => array(
|
|
|
|
"SearchFields" => Array(
|
2010-10-19 05:54:51 +02:00
|
|
|
'type'=>'fulltext',
|
|
|
|
'name'=>'SearchFields',
|
2010-10-04 06:32:48 +02:00
|
|
|
'value'=> $searchFields
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2010-10-19 06:53:36 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Shows all classes that had the {@link FulltextSearchable} extension applied through {@link enable()}.
|
|
|
|
*
|
|
|
|
* @return Array
|
|
|
|
*/
|
|
|
|
function get_searchable_classes() {
|
|
|
|
return self::$searchable_classes;
|
|
|
|
}
|
|
|
|
|
2011-10-29 06:12:02 +02:00
|
|
|
}
|