silverstripe-framework/search/FulltextSearchable.php

112 lines
3.7 KiB
PHP
Raw Normal View History

<?php
use SilverStripe\ORM\Connect\MySQLSchemaManager;
use SilverStripe\ORM\DataExtension;
use SilverStripe\CMS\Controllers\ContentController;
/**
* Provides a simple search engine for your site based on the MySQL FULLTEXT index.
* Adds the {@link FulltextSearchable} extension to data classes,
* as well as the {@link ContentControllerSearchExtension} to {@link ContentController}
* (if the 'cms' module is available as well).
* (this means you can use $SearchForm in your template without changing your own implementation).
2014-08-15 08:53:05 +02: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
*
* @see http://doc.silverstripe.org/framework/en/tutorials/4-site-search
*
* @package framework
* @subpackage search
*/
class FulltextSearchable extends DataExtension {
/**
* 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 08:53:05 +02:00
/**
* @var array List of class names
*/
protected static $searchable_classes;
/**
* 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>
2016-03-15 12:49:12 +01:00
* MyObject::add_extension("FulltextSearchable('MySearchableField,MyOtherField')");
* </code>
2014-08-15 08:53:05 +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
*
* @param array $searchableClasses The extension will be applied to all DataObject subclasses
* listed here. Default: {@link SiteTree} and {@link File}.
* @throws Exception
*/
public static function enable($searchableClasses = array('SilverStripe\\CMS\\Model\\SiteTree', 'File')) {
$defaultColumns = array(
'SilverStripe\\CMS\\Model\\SiteTree' => '"Title","MenuTitle","Content","MetaDescription"',
2015-10-15 05:32:24 +02:00
'File' => '"Name","Title"'
);
if(!is_array($searchableClasses)) $searchableClasses = array($searchableClasses);
foreach($searchableClasses as $class) {
if(!class_exists($class)) continue;
2014-08-15 08:53:05 +02:00
if(isset($defaultColumns[$class])) {
Config::inst()->update(
$class, 'create_table_options', array(MySQLSchemaManager::ID => 'ENGINE=MyISAM')
);
$class::add_extension("FulltextSearchable('{$defaultColumns[$class]}')");
} 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");
}
}
/**
* @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.
*/
public function __construct($searchFields = array()) {
if(is_array($searchFields)) $this->searchFields = '"'.implode('","', $searchFields).'"';
else $this->searchFields = $searchFields;
2014-08-15 08:53:05 +02:00
parent::__construct();
}
public static function get_extra_config($class, $extensionClass, $args) {
return array(
'indexes' => array(
'SearchFields' => array(
'type' => 'fulltext',
'name' => 'SearchFields',
'value' => $args[0]
)
)
);
}
/**
* Shows all classes that had the {@link FulltextSearchable} extension applied through {@link enable()}.
2014-08-15 08:53:05 +02:00
*
* @return array
*/
public static function get_searchable_classes() {
return self::$searchable_classes;
}
2014-08-15 08:53:05 +02:00
}