2010-10-04 04:32:48 +00:00
|
|
|
<?php
|
2016-06-15 16:03:16 +12:00
|
|
|
|
2016-08-19 10:51:35 +12:00
|
|
|
namespace SilverStripe\ORM\Search;
|
|
|
|
|
2017-03-29 00:20:39 +13:00
|
|
|
use SilverStripe\Assets\File;
|
|
|
|
use SilverStripe\CMS\Model\SiteTree;
|
2016-08-19 10:51:35 +12:00
|
|
|
use SilverStripe\Core\Config\Config;
|
|
|
|
use SilverStripe\CMS\Controllers\ContentController;
|
2016-06-15 16:03:16 +12:00
|
|
|
use SilverStripe\ORM\Connect\MySQLSchemaManager;
|
|
|
|
use SilverStripe\ORM\DataExtension;
|
2016-08-19 10:51:35 +12:00
|
|
|
use Exception;
|
2016-08-05 10:32:13 +12:00
|
|
|
|
2010-10-04 04:32:48 +00:00
|
|
|
/**
|
2010-10-19 04:53:36 +00: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 17:55:13 +13:00
|
|
|
* as well as the {@link ContentControllerSearchExtension} to {@link ContentController}
|
|
|
|
* (if the 'cms' module is available as well).
|
2010-10-19 04:53:36 +00:00
|
|
|
* (this means you can use $SearchForm in your template without changing your own implementation).
|
2014-08-15 18:53:05 +12: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 18:53:05 +12:00
|
|
|
*
|
2014-02-07 15:10:44 +13:00
|
|
|
* @see http://doc.silverstripe.org/framework/en/tutorials/4-site-search
|
2010-10-04 04:32:48 +00:00
|
|
|
*/
|
2016-11-29 12:31:16 +13:00
|
|
|
class FulltextSearchable extends DataExtension
|
|
|
|
{
|
2010-10-19 04:53:36 +00:00
|
|
|
|
2016-11-29 12:31:16 +13:00
|
|
|
/**
|
|
|
|
* Comma-separated list of database column names
|
|
|
|
* that can be searched on. Used for generation of the database index defintions.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $searchFields;
|
2014-08-15 18:53:05 +12:00
|
|
|
|
2016-11-29 12:31:16 +13:00
|
|
|
/**
|
|
|
|
* @var array List of class names
|
|
|
|
*/
|
|
|
|
protected static $searchable_classes;
|
2010-10-19 03:54:51 +00:00
|
|
|
|
2016-11-29 12:31:16 +13:00
|
|
|
/**
|
|
|
|
* 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>
|
|
|
|
* MyObject::add_extension("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}.
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
2017-03-29 00:20:39 +13:00
|
|
|
public static function enable($searchableClasses = [SiteTree::class, File::class])
|
|
|
|
{
|
2020-04-20 18:58:09 +01:00
|
|
|
$defaultColumns = [
|
2017-05-18 17:59:57 +01:00
|
|
|
SiteTree::class => ['Title','MenuTitle','Content','MetaDescription'],
|
|
|
|
File::class => ['Name','Title'],
|
2020-04-20 18:58:09 +01:00
|
|
|
];
|
2010-10-19 03:54:51 +00:00
|
|
|
|
2016-11-29 12:31:16 +13:00
|
|
|
if (!is_array($searchableClasses)) {
|
2020-04-20 18:58:09 +01:00
|
|
|
$searchableClasses = [$searchableClasses];
|
2016-11-29 12:31:16 +13:00
|
|
|
}
|
|
|
|
foreach ($searchableClasses as $class) {
|
|
|
|
if (!class_exists($class)) {
|
|
|
|
continue;
|
|
|
|
}
|
2014-08-15 18:53:05 +12:00
|
|
|
|
2016-11-29 12:31:16 +13:00
|
|
|
if (isset($defaultColumns[$class])) {
|
2017-05-18 17:59:57 +01:00
|
|
|
$class::add_extension(sprintf('%s(%s)', static::class, "'" . implode("','", $defaultColumns[$class]) . "''"));
|
2016-11-29 12:31:16 +13:00
|
|
|
} else {
|
|
|
|
throw new Exception(
|
|
|
|
"FulltextSearchable::enable() I don't know the default search columns for class '$class'"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self::$searchable_classes = $searchableClasses;
|
|
|
|
if (class_exists("SilverStripe\\CMS\\Controllers\\ContentController")) {
|
|
|
|
ContentController::add_extension("SilverStripe\\CMS\\Search\\ContentControllerSearchExtension");
|
|
|
|
}
|
|
|
|
}
|
2010-10-19 03:54:51 +00:00
|
|
|
|
2016-11-29 12:31:16 +13: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.
|
|
|
|
*/
|
2020-04-20 18:58:09 +01:00
|
|
|
public function __construct($searchFields = [])
|
2016-11-29 12:31:16 +13:00
|
|
|
{
|
2017-06-23 15:13:55 +12:00
|
|
|
parent::__construct();
|
2016-11-29 12:31:16 +13:00
|
|
|
if (is_array($searchFields)) {
|
|
|
|
$this->searchFields = $searchFields;
|
2017-05-18 17:59:57 +01:00
|
|
|
} else {
|
|
|
|
$this->searchFields = explode(',', $searchFields);
|
|
|
|
foreach ($this->searchFields as &$field) {
|
|
|
|
$field = trim($field);
|
|
|
|
}
|
2016-11-29 12:31:16 +13:00
|
|
|
}
|
|
|
|
}
|
2010-10-19 03:54:51 +00:00
|
|
|
|
2016-11-29 12:31:16 +13:00
|
|
|
public static function get_extra_config($class, $extensionClass, $args)
|
|
|
|
{
|
2020-04-20 18:58:09 +01:00
|
|
|
return [
|
|
|
|
'indexes' => [
|
|
|
|
'SearchFields' => [
|
2016-11-29 12:31:16 +13:00
|
|
|
'type' => 'fulltext',
|
|
|
|
'name' => 'SearchFields',
|
2017-05-18 17:59:57 +01:00
|
|
|
'columns' => $args,
|
2020-04-20 18:58:09 +01:00
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
2016-11-29 12:31:16 +13:00
|
|
|
}
|
2014-08-15 18:53:05 +12:00
|
|
|
|
2016-11-29 12:31:16 +13:00
|
|
|
/**
|
|
|
|
* Shows all classes that had the {@link FulltextSearchable} extension applied through {@link enable()}.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function get_searchable_classes()
|
|
|
|
{
|
|
|
|
return self::$searchable_classes;
|
|
|
|
}
|
2011-10-29 17:12:02 +13:00
|
|
|
}
|