2010-03-15 06:14:31 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Provides a simple search engine for your site based on the MySQL FULLTEXT index
|
2010-10-01 03:08:08 +02:00
|
|
|
*
|
2010-04-23 03:30:45 +02:00
|
|
|
* @package sapphire
|
|
|
|
* @subpackage search
|
2010-03-15 06:14:31 +01:00
|
|
|
*/
|
2010-04-01 02:56:13 +02:00
|
|
|
class FulltextSearchable extends DataObjectDecorator {
|
2010-03-15 06:14:31 +01:00
|
|
|
protected $searchFields;
|
2010-10-01 03:08:08 +02:00
|
|
|
static $searchableClasses;
|
|
|
|
|
2010-03-15 06:14:31 +01:00
|
|
|
/**
|
|
|
|
* 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'
|
|
|
|
);
|
2010-10-01 03:08:08 +02:00
|
|
|
|
2010-03-15 06:14:31 +01:00
|
|
|
if(!is_array($searchableClasses)) $searchableClasses = array($searchableClasses);
|
|
|
|
foreach($searchableClasses as $class) {
|
|
|
|
if(isset($defaultColumns[$class])) {
|
2010-04-01 02:56:13 +02:00
|
|
|
Object::add_extension($class, "FulltextSearchable('{$defaultColumns[$class]}')");
|
2010-03-15 06:14:31 +01:00
|
|
|
} else {
|
2010-04-01 02:56:13 +02:00
|
|
|
throw new Exception("FulltextSearchable::enable() I don't know the default search columns for class '$class'");
|
2010-03-15 06:14:31 +01:00
|
|
|
}
|
|
|
|
}
|
2010-10-01 03:08:08 +02:00
|
|
|
self::$searchableClasses = $searchableClasses;
|
|
|
|
|
2010-03-15 06:14:31 +01:00
|
|
|
Object::add_extension("ContentController", "ContentControllerSearchExtension");
|
|
|
|
}
|
2010-10-01 03:08:08 +02:00
|
|
|
|
2010-03-15 06:14:31 +01:00
|
|
|
function __construct($searchFields) {
|
|
|
|
if(is_array($searchFields)) $this->searchFields = implode(',', $searchFields);
|
|
|
|
else $this->searchFields = $searchFields;
|
|
|
|
parent::__construct();
|
|
|
|
}
|
2010-10-01 03:08:08 +02:00
|
|
|
|
2010-03-15 06:14:31 +01:00
|
|
|
function extraStatics($class = null, $extension = null) {
|
|
|
|
if($extension && preg_match('/\([\'"](.*)[\'"]\)/', $extension, $matches)) {
|
|
|
|
$searchFields = $matches[1];
|
2010-10-01 03:08:08 +02:00
|
|
|
|
2010-03-15 06:14:31 +01:00
|
|
|
return array(
|
|
|
|
'indexes' => array(
|
|
|
|
"SearchFields" => Array(
|
2010-10-01 03:08:08 +02:00
|
|
|
'type'=>'fulltext',
|
|
|
|
'name'=>'SearchFields',
|
2010-03-15 06:14:31 +01:00
|
|
|
'value'=> $searchFields
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|