mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-08-28 13:36:12 +02:00
# Conflicts: # .travis.yml # admin/css/ie7.css # admin/css/ie7.css.map # admin/css/ie8.css.map # admin/css/screen.css # admin/css/screen.css.map # admin/javascript/LeftAndMain.js # admin/scss/_style.scss # admin/scss/_uitheme.scss # control/HTTPRequest.php # core/Object.php # css/AssetUploadField.css # css/AssetUploadField.css.map # css/ConfirmedPasswordField.css.map # css/Form.css.map # css/GridField.css.map # css/TreeDropdownField.css.map # css/UploadField.css # css/UploadField.css.map # css/debug.css.map # dev/Debug.php # docs/en/00_Getting_Started/00_Server_Requirements.md # docs/en/02_Developer_Guides/06_Testing/00_Unit_Testing.md # docs/en/02_Developer_Guides/06_Testing/index.md # docs/en/02_Developer_Guides/14_Files/02_Images.md # docs/en/02_Developer_Guides/15_Customising_the_Admin_Interface/How_Tos/Extend_CMS_Interface.md # filesystem/File.php # filesystem/Folder.php # filesystem/GD.php # filesystem/Upload.php # forms/ToggleField.php # forms/Validator.php # javascript/lang/en_GB.js # javascript/lang/fr.js # javascript/lang/src/en.js # javascript/lang/src/fr.js # model/Image.php # model/UnsavedRelationList.php # model/Versioned.php # model/connect/MySQLDatabase.php # model/fieldtypes/DBField.php # model/fieldtypes/Enum.php # scss/AssetUploadField.scss # scss/UploadField.scss # templates/email/ChangePasswordEmail.ss # templates/forms/DropdownField.ss # tests/behat/features/bootstrap/SilverStripe/Framework/Test/Behaviour/CmsFormsContext.php # tests/behat/features/bootstrap/SilverStripe/Framework/Test/Behaviour/CmsUiContext.php # tests/forms/EnumFieldTest.php # tests/security/MemberTest.php # tests/security/MemberTest.yml # tests/security/SecurityTest.php
104 lines
3.4 KiB
PHP
104 lines
3.4 KiB
PHP
<?php
|
|
/**
|
|
* 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).
|
|
*
|
|
* CAUTION: Will make all files in your /assets folder searchable by file name
|
|
* unless "File" is excluded from FulltextSearchable::enable().
|
|
*
|
|
* @see http://doc.silverstripe.org/framework/en/tutorials/4-site-search
|
|
*
|
|
* @package framework
|
|
* @subpackage search
|
|
*/
|
|
class FulltextSearchable extends DataExtension {
|
|
|
|
/**
|
|
* @var String Comma-separated list of database column names
|
|
* that can be searched on. Used for generation of the database index defintions.
|
|
*/
|
|
protected $searchFields;
|
|
|
|
/**
|
|
* @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>
|
|
* 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}.
|
|
*/
|
|
public static function enable($searchableClasses = array('SiteTree', 'File')) {
|
|
$defaultColumns = array(
|
|
'SiteTree' => '"Title","MenuTitle","Content","MetaDescription"',
|
|
'File' => '"Name","Title"'
|
|
);
|
|
|
|
if(!is_array($searchableClasses)) $searchableClasses = array($searchableClasses);
|
|
foreach($searchableClasses as $class) {
|
|
if(!class_exists($class)) continue;
|
|
|
|
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("ContentController")){
|
|
ContentController::add_extension("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;
|
|
|
|
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()}.
|
|
*
|
|
* @return Array
|
|
*/
|
|
public static function get_searchable_classes() {
|
|
return self::$searchable_classes;
|
|
}
|
|
|
|
}
|