-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass.searchmodel.php
executable file
·207 lines (171 loc) · 6.75 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php if (!defined('APPLICATION')) exit();
/*
Copyright 2008, 2009 Vanilla Forums Inc.
This file is part of Garden.
Garden is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
Garden is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with Garden. If not, see <http://www.gnu.org/licenses/>.
Contact Vanilla Forums Inc. at support [at] vanillaforums [dot] com
*/
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);
$First = TRUE;
foreach ($ColumnsArray as $Column) {
$Column = trim($Column);
$Param = $this->Parameter();
if ($First) {
$Sql->Where("$Column like $Param", NULL, FALSE, FALSE);
$First = FALSE;
} else {
$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 = '';
}
// Converts a Google search phrase such as [eggs bacon "scrambled eggs" -omelette]
// into a boolean mode equivalent [+eggs +bacon +"scrambled eggs" -omelette]
// Prefixes all terms (words or quoted phrases) with a plus,
// unless they already have a plus or minus prefix.
public function ConvertGoogleToBoolean($s) {
$inQuotes = false;
$nextIsTermStart = true;
for ($i = 0; $i < strlen($s); $i++) {
$c = substr($s, $i, 1);
if (!$inQuotes) {
if ($c==' ') {
// Term separator
$nextIsTermStart = true;
} else {
// Everything other than space is considered a term character
if ($nextIsTermStart) {
// This is the first character in the term, including a quote
if ($c!='+' && $c!='-') {
// Insert s +, unless there is already a + or -
$s = substr($s, 0, $i) . '+' . substr($s, $i);
$i++;
}
$nextIsTermStart = false;
}
}
}
if ($c == '"') $inQuotes = !$inQuotes;
}
return $s;
}
public function Search($Search, $Offset = 0, $Limit = 20, $CategoryFilter = 0) {
// 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 == 'google') {
$SearchMode = 'boolean';
$Search = $this->ConvertGoogleToBoolean($Search);
}
if ($SearchMode == 'matchboolean') {
if (strpos($Search, '+') !== FALSE || strpos($Search, '-') !== FALSE)
$SearchMode = 'boolean';
else
$SearchMode = 'match';
} else {
$this->_SearchMode = $SearchMode;
}
if ($ForceDatabaseEngine = C('Database.ForceStorageEngine')) {
if (strcasecmp($ForceDatabaseEngine, 'myisam') != 0)
$SearchMode = 'like';
}
if (strlen($Search) <= 4)
$SearchMode = 'like';
$this->_SearchMode = $SearchMode;
$this->FireEvent('Search');
if(count($this->_SearchSql) == 0)
return array();
// Perform the search by unioning all of the sql together.
if($CategoryFilter > 0) {
$Sql = $this->SQL
->Select()
->From('_TBL_ s')
->OrderBy('s.DateInserted', 'desc')
->Where(array("CategoryID"=>'1'))
->Limit($Limit, $Offset)
->GetSelect();
}
else {
$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;
}
$Parameters= $this->_Parameters;
if($CategoryFilter > 0) {
$Parameters[":CategoryID"]=$CategoryFilter;
}
$this->Reset();
$this->SQL->Reset();
$Result = $this->Database->Query($Sql, $Parameters)->ResultArray();
foreach ($Result as $Key => $Value) {
if (isset($Value['Summary'])) {
$Value['Summary'] = Condense(Gdn_Format::To($Value['Summary'], $Value['Format']));
$Result[$Key] = $Value;
}
switch ($Value['RecordType']) {
case 'Discussion':
$Discussion = ArrayTranslate($Value, array('PrimaryID' => 'DiscussionID', 'Title' => 'Name', 'CategoryID'));
$Result[$Key]['Url'] = DiscussionUrl($Discussion, 1);
break;
}
}
return $Result;
}
}