1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Adapter that will use MySQL's full text search features. |
4
|
|
|
* |
5
|
|
|
* @author Mark Guinn <[email protected]> |
6
|
|
|
* @date 11.13.2013 |
7
|
|
|
* @package shop_search |
8
|
|
|
* @subpackage adapters |
9
|
|
|
*/ |
10
|
|
|
class ShopSearchMysql extends Object implements ShopSearchAdapter |
|
|
|
|
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @param string $keywords |
14
|
|
|
* @param array $filters [optional] |
15
|
|
|
* @param array $facetSpec [optional] |
16
|
|
|
* @param int $start [optional] |
17
|
|
|
* @param int $limit [optional] |
18
|
|
|
* @param string $sort [optional] |
19
|
|
|
* @return ArrayData |
20
|
|
|
*/ |
21
|
|
|
public function searchFromVars($keywords, array $filters=array(), array $facetSpec=array(), $start=-1, $limit=-1, $sort='') |
22
|
|
|
{ |
23
|
|
|
$searchable = ShopSearch::get_searchable_classes(); |
24
|
|
|
$matches = new ArrayList; |
25
|
|
|
|
26
|
|
|
foreach ($searchable as $className) { |
27
|
|
|
$list = DataObject::get($className); |
28
|
|
|
|
29
|
|
|
// get searchable fields |
30
|
|
|
$keywordFields = $this->getSearchFields($className); |
31
|
|
|
|
32
|
|
|
// build the filter |
33
|
|
|
$filter = array(); |
34
|
|
|
|
35
|
|
|
// Use parametrized query if SilverStripe >= 3.2 |
36
|
|
|
if (SHOP_SEARCH_IS_SS32) { |
37
|
|
|
foreach ($keywordFields as $indexFields) { |
38
|
|
|
$filter[] = array("MATCH ($indexFields) AGAINST (?)" => $keywords); |
39
|
|
|
} |
40
|
|
|
$list = $list->whereAny($filter); |
41
|
|
|
} else { |
42
|
|
|
foreach ($keywordFields as $indexFields) { |
43
|
|
|
$filter[] = sprintf("MATCH ($indexFields) AGAINST ('%s')", Convert::raw2sql($keywords)); |
44
|
|
|
} |
45
|
|
|
// join all the filters with an "OR" statement |
46
|
|
|
$list = $list->where(implode(' OR ', $filter)); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
// add in any other filters |
50
|
|
|
$list = FacetHelper::inst()->addFiltersToDataList($list, $filters); |
51
|
|
|
|
52
|
|
|
// add any matches to the big list |
53
|
|
|
$matches->merge($list); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return new ArrayData(array( |
57
|
|
|
'Matches' => $matches, |
58
|
|
|
'Facets' => FacetHelper::inst()->buildFacets($matches, $facetSpec, (bool)Config::inst()->get('ShopSearch', 'auto_facet_attributes')), |
59
|
|
|
)); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param $className |
65
|
|
|
* @return array an array containing fields per index |
66
|
|
|
* @throws Exception |
67
|
|
|
*/ |
68
|
|
|
protected function getSearchFields($className) |
69
|
|
|
{ |
70
|
|
|
$indexes = Config::inst()->get($className, 'indexes'); |
71
|
|
|
|
72
|
|
|
$indexList = array(); |
73
|
|
|
foreach ($indexes as $name => $index) { |
|
|
|
|
74
|
|
|
if (is_array($index)) { |
75
|
|
|
if (!empty($index['type']) && $index['type'] == 'fulltext' && !empty($index['value'])) { |
76
|
|
|
$indexList[] = trim($index['value']); |
77
|
|
|
} |
78
|
|
|
} elseif (preg_match('/fulltext\((.+)\)/', $index, $m)) { |
79
|
|
|
$indexList[] = trim($m[1]); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if (count($indexList) === 0) { |
84
|
|
|
throw new Exception("Class $className does not appear to have any fulltext indexes"); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $indexList; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.