mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
API CHANGE: Made MySQL fulltext search optional, activated with MySQLFulltextSearchable::enable() (from r101044)
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@111569 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
ec067ab08a
commit
ce173efce3
@ -84,9 +84,6 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
|
||||
);
|
||||
|
||||
static $indexes = array(
|
||||
"SearchFields" => Array('type'=>'fulltext', 'name'=>'SearchFields', 'value'=>'Title, MenuTitle, Content, MetaTitle, MetaDescription, MetaKeywords'),
|
||||
//"TitleSearchFields" => Array('type'=>'fulltext', 'value'=>'Title'),
|
||||
//"ContentSearchFields" => Array('type'=>'fulltext', 'value'=>'Content'),
|
||||
"URLSegment" => true,
|
||||
);
|
||||
|
||||
|
@ -24,10 +24,6 @@ class File extends DataObject {
|
||||
"Sort" => "Int"
|
||||
);
|
||||
|
||||
static $indexes = array(
|
||||
"SearchFields" => Array('type'=>'fulltext','name'=>'SearchFields', 'value'=>'Filename,Title,Content'),
|
||||
);
|
||||
|
||||
static $has_one = array(
|
||||
"Parent" => "File",
|
||||
"Owner" => "Member"
|
||||
|
36
search/ContentControllerSearchExtension.php
Normal file
36
search/ContentControllerSearchExtension.php
Normal file
@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Extension to provide a search interface when applied to ContentController
|
||||
*/
|
||||
class ContentControllerSearchExtension extends Extension {
|
||||
/**
|
||||
* Site search form
|
||||
*/
|
||||
function SearchForm() {
|
||||
$searchText = isset($_REQUEST['Search']) ? $_REQUEST['Search'] : 'Search';
|
||||
$fields = new FieldSet(
|
||||
new TextField('Search', '', $searchText)
|
||||
);
|
||||
$actions = new FieldSet(
|
||||
new FormAction('results', 'Search')
|
||||
);
|
||||
return new SearchForm($this->owner, 'SearchForm', $fields, $actions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Process and render search results.
|
||||
*
|
||||
* @param array $data The raw request data submitted by user
|
||||
* @param SearchForm $form The form instance that was submitted
|
||||
* @param SS_HTTPRequest $request Request generated for this action
|
||||
*/
|
||||
function results($data, $form, $request) {
|
||||
$data = array(
|
||||
'Results' => $form->getResults(),
|
||||
'Query' => $form->getSearchQuery(),
|
||||
'Title' => 'Search Results'
|
||||
);
|
||||
return $this->owner->customise($data)->renderWith(array('Page_results', 'Page'));
|
||||
}
|
||||
}
|
51
search/MySQLFulltextSearchable.php
Normal file
51
search/MySQLFulltextSearchable.php
Normal file
@ -0,0 +1,51 @@
|
||||
<?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
|
||||
),
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
@ -12,6 +12,18 @@ class SearchFormTest extends FunctionalTest {
|
||||
|
||||
protected $mockController;
|
||||
|
||||
protected $requiredExtensions = array(
|
||||
"SiteTree" => array(
|
||||
"MySQLFulltextSearchable('Title,MenuTitle,Content,MetaTitle,MetaDescription,MetaKeywords')",
|
||||
),
|
||||
"File" => array(
|
||||
"MySQLFulltextSearchable('Filename,Title,Content')",
|
||||
),
|
||||
"ContentController" => array(
|
||||
"ContentControllerSearchExtension",
|
||||
),
|
||||
);
|
||||
|
||||
function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
|
@ -10,7 +10,16 @@ class TranslatableSearchFormTest extends FunctionalTest {
|
||||
protected $mockController;
|
||||
|
||||
protected $requiredExtensions = array(
|
||||
'SiteTree' => array('Translatable'),
|
||||
'SiteTree' => array(
|
||||
'Translatable',
|
||||
"MySQLFulltextSearchable('Title,MenuTitle,Content,MetaTitle,MetaDescription,MetaKeywords')",
|
||||
),
|
||||
"File" => array(
|
||||
"MySQLFulltextSearchable('Filename,Title,Content')",
|
||||
),
|
||||
"ContentController" => array(
|
||||
"ContentControllerSearchExtension",
|
||||
),
|
||||
);
|
||||
|
||||
function setUp() {
|
||||
|
Loading…
Reference in New Issue
Block a user