Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 100 lines (89 sloc) 3.399 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
<?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).
*
* @see http://doc.silverstripe.org/tutorial:4-site-search
*
* @package sapphire
* @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>
* 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}.
*/
static function enable($searchableClasses = array('SiteTree', 'File')) {
$defaultColumns = array(
'SiteTree' => 'Title,MenuTitle,Content,MetaTitle,MetaDescription,MetaKeywords',
'File' => 'Filename,Title,Content'
);

if(!is_array($searchableClasses)) $searchableClasses = array($searchableClasses);
foreach($searchableClasses as $class) {
if(!class_exists($class)) continue;

if(isset($defaultColumns[$class])) {
Object::add_extension($class, "FulltextSearchable('{$defaultColumns[$class]}')");
Object::add_static_var($class, 'create_table_options', array('MySQLDatabase' => 'ENGINE=MyISAM'));
} 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")){
Object::add_extension("ContentController", "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.
*/
function __construct($searchFields) {
if(is_array($searchFields)) $this->searchFields = implode(',', $searchFields);
else $this->searchFields = $searchFields;

parent::__construct();
}

function extraStatics($class = null, $extension = null) {
if($extension && preg_match('/\([\'"](.*)[\'"]\)/', $extension, $matches)) {
$searchFields = $matches[1];

return array(
'indexes' => array(
"SearchFields" => Array(
'type'=>'fulltext',
'name'=>'SearchFields',
'value'=> $searchFields
),
)
);
}
}

/**
* Shows all classes that had the {@link FulltextSearchable} extension applied through {@link enable()}.
*
* @return Array
*/
function get_searchable_classes() {
return self::$searchable_classes;
}

}
Something went wrong with that request. Please try again.