forked from bauerjj/SphinxSearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass.searchmodel.php
135 lines (110 loc) · 4.43 KB
/
class.searchmodel.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
if (!defined('APPLICATION'))
exit();
/*
* This overrides the model that ships with Vanilla. Used to place some hooks so the sphinx search plugin
* can superseede the factory search method
*/
class SearchModel extends Gdn_Model {
/// PROPERTIES ///
protected $_Parameters = array();
protected $_SearchSql = array();
protected $_SearchMode = 'match';
public $ForceSearchMode = '';
protected $_SearchText = '';
/// METHODS ///
public function AddSearch($Sql) {
$this->_SearchSql[] = $Sql;
}
/** Add the sql to perform a search.
*
* @param Gdn_SQLDriver $Sql
* @param string $Columns a comma seperated list of columns to search on.
*/
public function AddMatchSql($Sql, $Columns, $LikeRelavenceColumn = '') {
if ($this->_SearchMode == 'like') {
if ($LikeRelavenceColumn)
$Sql->Select($LikeRelavenceColumn, '', 'Relavence');
else
$Sql->Select(1, '', 'Relavence');
$Sql->BeginWhereGroup();
$ColumnsArray = explode(',', $Columns);
foreach ($ColumnsArray as $Column) {
$Column = trim($Column);
$Param = $this->Parameter();
$Sql->OrWhere("$Column like $Param", NULL, FALSE, FALSE);
}
$Sql->EndWhereGroup();
} else {
$Boolean = $this->_SearchMode == 'boolean' ? ' in boolean mode' : '';
$Param = $this->Parameter();
$Sql->Select($Columns, "match(%s) against($Param{$Boolean})", 'Relavence');
$Param = $this->Parameter();
$Sql->Where("match($Columns) against ($Param{$Boolean})", NULL, FALSE, FALSE);
}
}
public function Parameter() {
$Parameter = ':Search' . count($this->_Parameters);
$this->_Parameters[$Parameter] = '';
return $Parameter;
}
public function Reset() {
$this->_Parameters = array();
$this->_SearchSql = '';
}
public function Search($Search, $Offset = 0, $Limit = 20) {
/**
* Override the default search from running right here!
*/
if(true) // Force this to be true while the sphinxsearch plugin is enabled!
return FALSE; //sphinx is running...don't use default search
else {
// If there are no searches then return an empty array.
if (trim($Search) == '')
return array();
// Figure out the exact search mode.
if ($this->ForceSearchMode)
$SearchMode = $this->ForceSearchMode;
else
$SearchMode = strtolower(C('Garden.Search.Mode', 'matchboolean'));
if ($SearchMode == 'matchboolean') {
if (strpos($Search, '+') !== FALSE || strpos($Search, '-') !== FALSE)
$SearchMode = 'boolean';
else
$SearchMode = 'match';
} else {
$this->_SearchMode = $SearchMode;
}
$this->_SearchMode = $SearchMode;
$this->FireEvent('Search');
//print_r($this->_SearchSql);
if (count($this->_SearchSql) == 0)
return array();
// Perform the search by unioning all of the sql together.
$Sql = $this->SQL
->Select()
->From('_TBL_ s')
->OrderBy('s.DateInserted', 'desc')
->Limit($Limit, $Offset)
->GetSelect();
$Sql = str_replace($this->Database->DatabasePrefix . '_TBL_', "(\n" . implode("\nunion all\n", $this->_SearchSql) . "\n)", $Sql);
$this->EventArguments['Search'] = $Search;
$this->FireEvent('AfterBuildSearchQuery');
if ($this->_SearchMode == 'like')
$Search = '%' . $Search . '%';
foreach ($this->_Parameters as $Key => $Value) {
$this->_Parameters[$Key] = $Search;
}
$Result = $this->Database->Query($Sql, $this->_Parameters)->ResultArray();
$this->Reset();
$this->SQL->Reset();
foreach ($Result as $Key => $Value) {
if (isset($Value['Summary'])) {
$Value['Summary'] = Gdn_Format::Text(Gdn_Format::To($Value['Summary'], $Value['Format']));
$Result[$Key] = $Value;
}
}
return $Result;
}
}
}