-
Notifications
You must be signed in to change notification settings - Fork 3
/
class.Search.php
executable file
·263 lines (222 loc) · 7.98 KB
/
class.Search.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<?php
/***************************************************************************\
This file is part of RoomAllocation.
RoomAllocation 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.
RoomAllocation 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 RoomAllocation. If not, see <http://www.gnu.org/licenses/>.
\***************************************************************************/
?>
<?php
/**
* How the parsing works:
* - a b => query LIKE '%a%' AND query LIKE '%b%'
* - a:b -> a LIKE '%b%'
* - a:"b" -> a LIKE '%b%'
* - a:~b,c -> (a NOT LIKE '%b%' OR a LIKE '%c%')
* - a:b,c,d -> (a LIKE '%b%' OR a LIKE '%c%' OR a LIKE '%d%')
* - a:b,c a:d -> (a LIKE '%b%' OR a LIKE '%c%') AND (a LIKE '%d%')
* - x a:b,c,d y -> (a LIKE '%b%' OR a LIKE '%c%' OR a LIKE '%d%') AND (query LIKE '%x%' AND query LIKE '%y%')
*/
class Search{
const MIN_AMBIGUOUS_TOKEN_LENGTH = 2;
private $fields = array();
private $hooks = array();
private $hookData = array();
private $_lastSanitize = '';
private $_lastParse = array();
public function __construct( array $fields ){
$this->setFields( $fields );
}
/**
* @param {mixed} $val The query to parse. Can either be :
* - a string in which case it is parsed
* - an array resulted from Search::parse()
* @return {string} The SQL conditions for the given query
*/
public function getQuery( $val, $skipTableCheck = false ){
$tokens = Search::parse( $val );
if( !$tokens ){
return null;
}
$this->triggerHook( 'getQuery_before', $tokens, $skipTableCheck );
if( $skipTableCheck || !$this->getFields() ){
$tables = $this->getFields();
foreach( $tokens['strict'] as $k => $v ){
if( !in_array( $v, $tables ) ){
unset( $tokens['strict'][$k] );
}
}
}
$ambiguous = array();
foreach( $tokens['ambiguous'] as $v ){
if( strlen($v) >= Search::MIN_AMBIGUOUS_TOKEN_LENGTH ){
if( substr( $v, 0, 1 ) == '~' ){
$ambiguous[] = "query NOT LIKE '%".ltrim($v,'~')."%'";
} else {
$ambiguous[] = "query LIKE '%$v%'";
}
}
}
$ambiguous = '('.implode(' AND ', $ambiguous ).')';
$strict = array();
foreach( $tokens['strict'] as $k => $v ){
$column = array();
foreach( $v as $v2 ){
$field = array();
foreach( $v2 as $val ){
if( strlen($val) >= Search::MIN_AMBIGUOUS_TOKEN_LENGTH ){
if( substr( $val, 0, 1 ) == '~' ){
$field[] = "$k NOT LIKE '%".ltrim($val, '~')."%'";
} else {
$field[] = "$k LIKE '%$val%'";
}
}
}
$column[] = '('.implode( ' OR ', $field ).')';
}
$column = '('.implode( ' AND ', $column ).')';
$strict[] = $column;
}
$strict = implode(' AND ', $strict);
$raw = '';
if( isset( $tokens['raw'] ) ){
$raw = $tokens['raw'];
}
$this->triggerHook( 'getQuery_after', $strict, $ambiguous, $raw );
if( strlen($strict) <= 4 && strlen($ambiguous) <= 2 && strlen($raw) <= 2 ){
return null;
} else {
$return = '';
$and = false;
if( strlen( $strict ) > 4 ){
$return .= " $strict";
$and = true;
}
if( strlen( $ambiguous ) > 2 ){
$return .= ( $and ? ' AND ' : '' ) . " $ambiguous";
$and = true;
}
if( strlen( $raw ) > 2 ){
$return .= ( $and ? ' AND ' : '' ) . " $raw";
$and = true;
}
return $return;
}
}
public function parse( $str ){
$str = Search::sanitize( $str );
$this->triggerHook( 'parse_before', $str );
$quotes = array();
$p = 0;
$c = 0;
while( $p = strpos( $str, '"', $p ) ){
$p2 = strpos( $str, '"', $p+1 );
$token = '{{'.(++$c).'}}';
$quotes[ $token ] = substr( $str, $p+1, $p2-$p-1 );
$str = substr( $str, 0, $p ) . $token . substr( $str, $p2+1 );
}
$arr = explode( ' ', $str );
$ambiguous = array();
$strict = array();
$keys = array_keys( $quotes );
foreach( $arr as $v ){
if( strpos( $v, ':' ) !== false ){
$a = explode(':', $v);
if( !isset( $strict[ $a[0] ] ) ){
$strict[ $a[0] ] = array();
}
$a[1] = str_ireplace( $keys, $quotes, $a[1] );
$b = explode(',', $a[1]);
if( count( $b ) > 0 ){
$c = count( $strict[ $a[0] ] );
$strict[ $a[0] ][ $c ] = array();
for( $i=0; $i<count($b); ++$i ){
$strict[ $a[0] ][ $c ][] = trim( $b[ $i ] );
}
}
} else {
$ambiguous[] = $v;
}
}
$result = array(
'ambiguous' => $ambiguous,
'strict' => $strict
);
$this->triggerHook( 'parse_after', $result );
$this->_lastParse = $result;
return $result;
}
public function sanitize( $str ){
$this->triggerHook( 'sanitize_before', $str );
$str = str_replace("\n", " ", $str);
$str = trim( preg_replace('/\s\s+/', ' ', $str) );
$str = str_replace( array("'", "`", "\\"), '', $str );
// check for uneven number of "
if( count(explode('"', $str )) % 2 == 0 ){
$str = preg_replace('/^(.*)"([^"]*)$/', '$1$2', $str);
}
$str = preg_replace('/\s*(:\s*)+/', '$1', $str);
$str = preg_replace('/:"\s*([^"]*)\s*"\s*/', ':"$1" ', $str);
$str = preg_replace('/\b([^: ]+:([^: ]+|"[^: ]*")):+/', "$1 ", $str);
$str = preg_replace('/[: ]+([^: ]+:[^: ]+)\b/', " $1", $str);
$str = trim( $str );
$this->triggerHook( 'sanitize_after', $str );
$this->_lastSanitize = $str;
return $str;
}
public function getLastParse(){
return $this->_lastParse;
}
public function getLastSanitize(){
return $this->_lastSanitize;
}
private function triggerHook( $hookName, &$arg1, &$arg2 = null, &$arg3 = null ){
if( isset( $this->hooks[ $hookName ] ) ){
foreach( $this->hooks[ $hookName ] as $k => $function ){
$function( $this, $arg1, $arg2, $arg3 );
}
}
return $this;
}
public function setHookData( $name, array $data ){
$this->hookData[ $name ] = $data;
return $this;
}
public function getHookData( $name ){
if( isset( $this->hookData[ $name ] ) ){
return $this->hookData[ $name ];
}
return null;
}
public function addHook( $hookName, $name, $function ){
if( !isset( $this->hooks[ $hookName ] ) ){
$this->hooks[ $hookName ] = array();
}
$this->hooks[ $hookName ][ $name ] = $function;
return $this;
}
public function removeHook( $hookName, $name ){
if( isset( $this->hooks[ $hookName ] ) && isset( $this->hooks[ $hookName ][ $name ] ) ){
unset( $this->hooks[ $hookName ][ $name ] );
}
return $this;
}
public function getHooks(){
return $this->hooks;
}
public function setFields( array $val ){
$this->fields = $val;
return $this;
}
public function getFields(){
return $this->fields;
}
}
?>