mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-09-05 01:01:17 +02:00
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.4@101044 467b73ca-7a2a-4603-9d3b-597d59a354a9
51 lines
1.5 KiB
PHP
51 lines
1.5 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Provides a simple search engine for your site based on the MySQL FULLTEXT index
|
|
*/
|
|
class MySQLFulltextSearchable extends DataObjectDecorator {
|
|
protected $searchFields;
|
|
|
|
/**
|
|
* Enable the default configuration of MySQL full-text searching on the given data classes.
|
|
*/
|
|
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(isset($defaultColumns[$class])) {
|
|
Object::add_extension($class, "MySQLFulltextSearchable('{$defaultColumns[$class]}')");
|
|
} else {
|
|
throw new Exception("MySQLFUlltextSearchable::enable() I don't know the default search columns for class '$class'");
|
|
}
|
|
}
|
|
|
|
Object::add_extension("ContentController", "ContentControllerSearchExtension");
|
|
}
|
|
|
|
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
|
|
),
|
|
)
|
|
);
|
|
}
|
|
}
|
|
} |