2010-10-04 06:32:48 +02:00
|
|
|
<?php
|
2016-06-15 06:03:16 +02:00
|
|
|
|
|
|
|
use SilverStripe\ORM\Connect\MySQLSchemaManager;
|
|
|
|
use SilverStripe\ORM\DataExtension;
|
2016-08-05 00:32:13 +02:00
|
|
|
use SilverStripe\CMS\Controllers\ContentController;
|
|
|
|
|
2010-10-04 06:32:48 +02:00
|
|
|
/**
|
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).
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
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().
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2014-02-07 03:10:44 +01:00
|
|
|
* @see http://doc.silverstripe.org/framework/en/tutorials/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
|
|
|
|
|
|
|
/**
|
2016-08-05 00:32:13 +02:00
|
|
|
* Comma-separated list of database column names
|
|
|
|
* that can be searched on. Used for generation of the database index defintions.
|
|
|
|
*
|
|
|
|
* @var string
|
2010-10-19 06:53:36 +02:00
|
|
|
*/
|
2010-10-04 06:32:48 +02:00
|
|
|
protected $searchFields;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-19 06:53:36 +02:00
|
|
|
/**
|
2016-08-05 00:32:13 +02:00
|
|
|
* @var array List of class names
|
2010-10-19 06:53:36 +02:00
|
|
|
*/
|
|
|
|
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>
|
2016-03-15 12:49:12 +01:00
|
|
|
* MyObject::add_extension("FulltextSearchable('MySearchableField,MyOtherField')");
|
2010-10-19 06:53:36 +02:00
|
|
|
* </code>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-19 06:53:36 +02:00
|
|
|
* Caution: This is a wrapper method that should only be used in _config.php,
|
|
|
|
* and only be called once in your code.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2016-08-05 00:32:13 +02:00
|
|
|
* @param array $searchableClasses The extension will be applied to all DataObject subclasses
|
2010-10-19 06:53:36 +02:00
|
|
|
* listed here. Default: {@link SiteTree} and {@link File}.
|
2016-08-05 00:32:13 +02:00
|
|
|
* @throws Exception
|
2010-10-04 06:32:48 +02:00
|
|
|
*/
|
2016-08-05 00:32:13 +02:00
|
|
|
public static function enable($searchableClasses = array('SilverStripe\\CMS\\Model\\SiteTree', 'File')) {
|
2010-10-04 06:32:48 +02:00
|
|
|
$defaultColumns = array(
|
2016-08-05 00:32:13 +02:00
|
|
|
'SilverStripe\\CMS\\Model\\SiteTree' => '"Title","MenuTitle","Content","MetaDescription"',
|
2015-10-15 05:32:24 +02:00
|
|
|
'File' => '"Name","Title"'
|
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;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-04 06:32:48 +02:00
|
|
|
if(isset($defaultColumns[$class])) {
|
2013-06-21 00:32:08 +02:00
|
|
|
Config::inst()->update(
|
|
|
|
$class, 'create_table_options', array(MySQLSchemaManager::ID => 'ENGINE=MyISAM')
|
|
|
|
);
|
2012-10-09 05:55:37 +02:00
|
|
|
$class::add_extension("FulltextSearchable('{$defaultColumns[$class]}')");
|
2010-10-04 06:32:48 +02:00
|
|
|
} else {
|
2012-09-26 23:34:00 +02:00
|
|
|
throw new Exception(
|
2013-06-21 00:32:08 +02:00
|
|
|
"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;
|
2016-08-05 00:32:13 +02:00
|
|
|
if(class_exists("SilverStripe\\CMS\\Controllers\\ContentController")){
|
|
|
|
ContentController::add_extension("SilverStripe\\CMS\\Search\\ContentControllerSearchExtension");
|
2011-03-18 03:01:09 +01:00
|
|
|
}
|
2010-10-04 06:32:48 +02:00
|
|
|
}
|
2010-10-19 05:54:51 +02:00
|
|
|
|
2010-10-19 06:53:36 +02:00
|
|
|
/**
|
2016-08-05 00:32:13 +02:00
|
|
|
* @param array|string $searchFields Comma-separated list (or array) of database column names
|
2010-10-19 06:53:36 +02:00
|
|
|
* 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;
|
2014-08-15 08:53:05 +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()}.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2016-08-05 00:32:13 +02:00
|
|
|
* @return array
|
2010-10-19 06:53:36 +02:00
|
|
|
*/
|
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;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-10-29 06:12:02 +02:00
|
|
|
}
|