2008-08-06 05:28:25 +02:00
|
|
|
<?php
|
2016-06-15 06:03:16 +02:00
|
|
|
|
|
|
|
use SilverStripe\ORM\DataQuery;
|
|
|
|
use SilverStripe\ORM\DataObject;
|
2008-08-09 08:40:50 +02:00
|
|
|
/**
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-08-09 08:40:50 +02:00
|
|
|
* @subpackage search
|
|
|
|
*/
|
|
|
|
|
2008-08-06 05:28:25 +02:00
|
|
|
/**
|
2008-08-09 06:38:44 +02:00
|
|
|
* Filters by full-text matching on the given field.
|
|
|
|
*
|
|
|
|
* Full-text indexes are only available with MyISAM tables. The following column types are
|
|
|
|
* supported:
|
|
|
|
* - Char
|
|
|
|
* - Varchar
|
|
|
|
* - Text
|
|
|
|
*
|
|
|
|
* To enable full-text matching on fields, you also need to add an index to the
|
|
|
|
* database table, using the {$indexes} hash in your DataObject subclass:
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-08-09 06:38:44 +02:00
|
|
|
* <code>
|
2014-05-25 02:46:07 +02:00
|
|
|
* private static $indexes = array(
|
2008-08-09 06:38:44 +02:00
|
|
|
* 'SearchFields' => 'fulltext(Name, Title, Description)'
|
|
|
|
* );
|
|
|
|
* </code>
|
2008-08-06 05:28:25 +02:00
|
|
|
*
|
2014-05-25 02:46:07 +02:00
|
|
|
* @todo Add support for databases besides MySQL
|
2008-08-06 05:28:25 +02:00
|
|
|
*/
|
2008-08-09 05:54:55 +02:00
|
|
|
class FulltextFilter extends SearchFilter {
|
2008-08-09 06:06:52 +02:00
|
|
|
|
2012-09-06 09:54:44 +02:00
|
|
|
protected function applyOne(DataQuery $query) {
|
2014-05-25 02:46:07 +02:00
|
|
|
$this->model = $query->applyRelation($this->relation);
|
2013-06-21 00:32:08 +02:00
|
|
|
$predicate = sprintf("MATCH (%s) AGAINST (?)", $this->getDbName());
|
|
|
|
return $query->where(array($predicate => $this->getValue()));
|
2008-08-09 06:06:52 +02:00
|
|
|
}
|
2008-08-09 06:38:44 +02:00
|
|
|
|
2012-09-06 09:54:44 +02:00
|
|
|
protected function excludeOne(DataQuery $query) {
|
2014-05-25 02:46:07 +02:00
|
|
|
$this->model = $query->applyRelation($this->relation);
|
2013-06-21 00:32:08 +02:00
|
|
|
$predicate = sprintf("NOT MATCH (%s) AGAINST (?)", $this->getDbName());
|
|
|
|
return $query->where(array($predicate => $this->getValue()));
|
2012-09-06 09:54:44 +02:00
|
|
|
}
|
|
|
|
|
2008-08-11 02:03:57 +02:00
|
|
|
public function isEmpty() {
|
2012-09-06 09:54:44 +02:00
|
|
|
return $this->getValue() === array() || $this->getValue() === null || $this->getValue() === '';
|
2008-08-11 02:03:57 +02:00
|
|
|
}
|
2014-05-25 02:46:07 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This implementation allows for a list of columns to be passed into MATCH() instead of just one.
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* <code>
|
|
|
|
* MyDataObject::get()->filter('SearchFields:fulltext', 'search term')
|
|
|
|
* </code>
|
|
|
|
*
|
2016-05-25 07:09:29 +02:00
|
|
|
* @throws Exception
|
2014-05-25 02:46:07 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getDbName() {
|
|
|
|
$indexes = Config::inst()->get($this->model, "indexes");
|
|
|
|
if(is_array($indexes) && array_key_exists($this->getName(), $indexes)) {
|
|
|
|
$index = $indexes[$this->getName()];
|
|
|
|
if(is_array($index) && array_key_exists("value", $index)) {
|
2015-08-05 08:41:44 +02:00
|
|
|
return $this->prepareColumns($index['value']);
|
2014-05-25 02:46:07 +02:00
|
|
|
} else {
|
|
|
|
// Parse a fulltext string (eg. fulltext ("ColumnA", "ColumnB")) to figure out which columns
|
|
|
|
// we need to search.
|
2015-01-29 00:48:46 +01:00
|
|
|
if(preg_match('/^fulltext\s+\((.+)\)$/i', $index, $matches)) {
|
2015-08-05 08:41:44 +02:00
|
|
|
return $this->prepareColumns($matches[1]);
|
2014-05-25 02:46:07 +02:00
|
|
|
} else {
|
2016-05-25 07:09:29 +02:00
|
|
|
throw new Exception(sprintf(
|
|
|
|
"Invalid fulltext index format for '%s' on '%s'",
|
|
|
|
$this->getName(),
|
|
|
|
$this->model
|
|
|
|
));
|
2014-05-25 02:46:07 +02:00
|
|
|
}
|
|
|
|
}
|
2015-01-29 00:48:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return parent::getDbName();
|
2014-05-25 02:46:07 +02:00
|
|
|
}
|
2015-10-06 17:58:22 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds table identifier to the every column.
|
|
|
|
* Columns must have table identifier to prevent duplicate column name error.
|
|
|
|
*
|
2016-05-25 07:09:29 +02:00
|
|
|
* @param array $columns
|
2015-10-06 17:58:22 +02:00
|
|
|
* @return string
|
2016-05-25 07:09:29 +02:00
|
|
|
*/
|
2015-10-06 17:58:22 +02:00
|
|
|
protected function prepareColumns($columns) {
|
|
|
|
$cols = preg_split('/"?\s*,\s*"?/', trim($columns, '(") '));
|
2016-05-25 07:09:29 +02:00
|
|
|
$table = DataObject::getSchema()->tableForField($this->model, current($cols));
|
|
|
|
$cols = array_map(function($col) use ($table) {
|
|
|
|
return sprintf('"%s"."%s"', $table, $col);
|
2015-10-06 17:58:22 +02:00
|
|
|
}, $cols);
|
|
|
|
return implode(',', $cols);
|
2015-08-05 08:41:44 +02:00
|
|
|
}
|
2014-05-25 02:46:07 +02:00
|
|
|
|
2008-08-06 05:28:25 +02:00
|
|
|
}
|