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
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function enable($searchableClasses = array('SiteTree', 'File')) {
|
2010-10-04 06:32:48 +02:00
|
|
|
$defaultColumns = array(
|
2012-09-11 02:53:08 +02:00
|
|
|
'SiteTree' => '"Title","MenuTitle","Content","MetaTitle","MetaDescription","MetaKeywords"',
|
2012-09-25 02:31:50 +02:00
|
|
|
'File' => '"Title","Filename","Content"'
|
2010-10-04 06:32:48 +02:00
|
|
|
);
|
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.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function __construct($searchFields = array()) {
|
2012-09-11 02:53:08 +02:00
|
|
|
if(is_array($searchFields)) $this->searchFields = '"'.implode('","', $searchFields).'"';
|
2010-10-04 06:32:48 +02:00
|
|
|
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-09-19 12:07:39 +02:00
|
|
|
public static function get_extra_config($class, $extensionClass, $args) {
|
2012-08-22 23:29:13 +02:00
|
|
|
return array(
|
|
|
|
'indexes' => array(
|
|
|
|
'SearchFields' => array(
|
|
|
|
'type' => 'fulltext',
|
|
|
|
'name' => 'SearchFields',
|
|
|
|
'value' => $args[0]
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
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-09-19 12:07:39 +02:00
|
|
|
public 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
|
|
|
}
|