@@ -246,6 +246,9 @@ discard block |
||
246 | 246 | } |
247 | 247 | |
248 | 248 | // unpack 64-bit unsigned |
249 | +/** |
|
250 | + * @param string $v |
|
251 | + */ |
|
249 | 252 | function sphUnpackU64($v) |
250 | 253 | { |
251 | 254 | list($hi, $lo) = array_values(unpack("N*N*", $v)); |
@@ -312,6 +315,9 @@ discard block |
||
312 | 315 | } |
313 | 316 | |
314 | 317 | // unpack 64-bit signed |
318 | +/** |
|
319 | + * @param string $v |
|
320 | + */ |
|
315 | 321 | function sphUnpackI64($v) |
316 | 322 | { |
317 | 323 | list($hi, $lo) = array_values(unpack("N*N*", $v)); |
@@ -396,6 +402,9 @@ discard block |
||
396 | 402 | } |
397 | 403 | } |
398 | 404 | |
405 | +/** |
|
406 | + * @param integer $bit |
|
407 | + */ |
|
399 | 408 | function sphSetBit($flag, $bit, $on) |
400 | 409 | { |
401 | 410 | if ($on) |
@@ -566,6 +575,10 @@ discard block |
||
566 | 575 | } |
567 | 576 | |
568 | 577 | |
578 | + /** |
|
579 | + * @param string $data |
|
580 | + * @param integer $length |
|
581 | + */ |
|
569 | 582 | function _Send($handle, $data, $length) |
570 | 583 | { |
571 | 584 | if (feof($handle) || fwrite($handle, $data, $length) !== $length) |
@@ -669,6 +682,10 @@ discard block |
||
669 | 682 | } |
670 | 683 | |
671 | 684 | /// get and check response packet from searchd server |
685 | + |
|
686 | + /** |
|
687 | + * @param integer $client_ver |
|
688 | + */ |
|
672 | 689 | function _GetResponse($fp, $client_ver) |
673 | 690 | { |
674 | 691 | $response = ""; |
@@ -1257,6 +1274,11 @@ discard block |
||
1257 | 1274 | } |
1258 | 1275 | |
1259 | 1276 | /// parse and return search query (or queries) response |
1277 | + |
|
1278 | + /** |
|
1279 | + * @param string $response |
|
1280 | + * @param integer $nreqs |
|
1281 | + */ |
|
1260 | 1282 | function _ParseSearchResponse($response, $nreqs) |
1261 | 1283 | { |
1262 | 1284 | $p = 0; // current position |
@@ -1854,8 +1854,9 @@ |
||
1854 | 1854 | $this->error = 'already connected'; |
1855 | 1855 | return false; |
1856 | 1856 | } |
1857 | - if (!($fp = $this->connect())) |
|
1858 | - return false; |
|
1857 | + if (!($fp = $this->connect())) { |
|
1858 | + return false; |
|
1859 | + } |
|
1859 | 1860 | |
1860 | 1861 | // command, command version = 0, body length = 4, body = 1 |
1861 | 1862 | $req = pack('nnNN', SEARCHD_COMMAND_PERSIST, 0, 4, 1); |
@@ -140,1775 +140,1775 @@ |
||
140 | 140 | /// pack 64-bit signed |
141 | 141 | function sphPackI64($v) |
142 | 142 | { |
143 | - assert(is_numeric($v)); |
|
144 | - |
|
145 | - // x64 |
|
146 | - if (PHP_INT_SIZE >= 8) { |
|
147 | - $v = (int)$v; |
|
148 | - return pack('NN', $v >> 32, $v & 0xFFFFFFFF); |
|
149 | - } |
|
150 | - |
|
151 | - // x32, int |
|
152 | - if (is_int($v)) { |
|
153 | - return pack('NN', $v < 0 ? -1 : 0, $v); |
|
154 | - } |
|
155 | - |
|
156 | - // x32, bcmath |
|
157 | - if (function_exists('bcmul')) { |
|
158 | - if (bccomp($v, 0) == -1) { |
|
159 | - $v = bcadd('18446744073709551616', $v); |
|
160 | - } |
|
161 | - $h = bcdiv($v, '4294967296', 0); |
|
162 | - $l = bcmod($v, '4294967296'); |
|
163 | - return pack('NN', (float)$h, (float)$l); // conversion to float is intentional; int would lose 31st bit |
|
164 | - } |
|
165 | - |
|
166 | - // x32, no-bcmath |
|
167 | - $p = max(0, strlen($v) - 13); |
|
168 | - $lo = abs((float)substr($v, $p)); |
|
169 | - $hi = abs((float)substr($v, 0, $p)); |
|
170 | - |
|
171 | - $m = $lo + $hi * 1316134912.0; // (10 ^ 13) % (1 << 32) = 1316134912 |
|
172 | - $q = floor($m / 4294967296.0); |
|
173 | - $l = $m - ($q * 4294967296.0); |
|
174 | - $h = $hi * 2328.0 + $q; // (10 ^ 13) / (1 << 32) = 2328 |
|
175 | - |
|
176 | - if ($v < 0) { |
|
177 | - if ($l == 0) { |
|
178 | - $h = 4294967296.0 - $h; |
|
179 | - } else { |
|
180 | - $h = 4294967295.0 - $h; |
|
181 | - $l = 4294967296.0 - $l; |
|
182 | - } |
|
183 | - } |
|
184 | - return pack('NN', $h, $l); |
|
143 | + assert(is_numeric($v)); |
|
144 | + |
|
145 | + // x64 |
|
146 | + if (PHP_INT_SIZE >= 8) { |
|
147 | + $v = (int)$v; |
|
148 | + return pack('NN', $v >> 32, $v & 0xFFFFFFFF); |
|
149 | + } |
|
150 | + |
|
151 | + // x32, int |
|
152 | + if (is_int($v)) { |
|
153 | + return pack('NN', $v < 0 ? -1 : 0, $v); |
|
154 | + } |
|
155 | + |
|
156 | + // x32, bcmath |
|
157 | + if (function_exists('bcmul')) { |
|
158 | + if (bccomp($v, 0) == -1) { |
|
159 | + $v = bcadd('18446744073709551616', $v); |
|
160 | + } |
|
161 | + $h = bcdiv($v, '4294967296', 0); |
|
162 | + $l = bcmod($v, '4294967296'); |
|
163 | + return pack('NN', (float)$h, (float)$l); // conversion to float is intentional; int would lose 31st bit |
|
164 | + } |
|
165 | + |
|
166 | + // x32, no-bcmath |
|
167 | + $p = max(0, strlen($v) - 13); |
|
168 | + $lo = abs((float)substr($v, $p)); |
|
169 | + $hi = abs((float)substr($v, 0, $p)); |
|
170 | + |
|
171 | + $m = $lo + $hi * 1316134912.0; // (10 ^ 13) % (1 << 32) = 1316134912 |
|
172 | + $q = floor($m / 4294967296.0); |
|
173 | + $l = $m - ($q * 4294967296.0); |
|
174 | + $h = $hi * 2328.0 + $q; // (10 ^ 13) / (1 << 32) = 2328 |
|
175 | + |
|
176 | + if ($v < 0) { |
|
177 | + if ($l == 0) { |
|
178 | + $h = 4294967296.0 - $h; |
|
179 | + } else { |
|
180 | + $h = 4294967295.0 - $h; |
|
181 | + $l = 4294967296.0 - $l; |
|
182 | + } |
|
183 | + } |
|
184 | + return pack('NN', $h, $l); |
|
185 | 185 | } |
186 | 186 | |
187 | 187 | /// pack 64-bit unsigned |
188 | 188 | function sphPackU64($v) |
189 | 189 | { |
190 | - assert(is_numeric($v)); |
|
191 | - |
|
192 | - // x64 |
|
193 | - if (PHP_INT_SIZE >= 8) { |
|
194 | - assert($v >= 0); |
|
195 | - |
|
196 | - // x64, int |
|
197 | - if (is_int($v)) { |
|
198 | - return pack('NN', $v >> 32, $v & 0xFFFFFFFF); |
|
199 | - } |
|
200 | - |
|
201 | - // x64, bcmath |
|
202 | - if (function_exists('bcmul')) { |
|
203 | - $h = bcdiv($v, 4294967296, 0); |
|
204 | - $l = bcmod($v, 4294967296); |
|
205 | - return pack('NN', $h, $l); |
|
206 | - } |
|
207 | - |
|
208 | - // x64, no-bcmath |
|
209 | - $p = max(0, strlen($v) - 13); |
|
210 | - $lo = (int)substr($v, $p); |
|
211 | - $hi = (int)substr($v, 0, $p); |
|
212 | - |
|
213 | - $m = $lo + $hi * 1316134912; |
|
214 | - $l = $m % 4294967296; |
|
215 | - $h = $hi * 2328 + (int)($m / 4294967296); |
|
216 | - |
|
217 | - return pack('NN', $h, $l); |
|
218 | - } |
|
219 | - |
|
220 | - // x32, int |
|
221 | - if (is_int($v)) { |
|
222 | - return pack('NN', 0, $v); |
|
223 | - } |
|
224 | - |
|
225 | - // x32, bcmath |
|
226 | - if (function_exists('bcmul')) { |
|
227 | - $h = bcdiv($v, '4294967296', 0); |
|
228 | - $l = bcmod($v, '4294967296'); |
|
229 | - return pack('NN', (float)$h, (float)$l); // conversion to float is intentional; int would lose 31st bit |
|
230 | - } |
|
231 | - |
|
232 | - // x32, no-bcmath |
|
233 | - $p = max(0, strlen($v) - 13); |
|
234 | - $lo = (float)substr($v, $p); |
|
235 | - $hi = (float)substr($v, 0, $p); |
|
236 | - |
|
237 | - $m = $lo + $hi * 1316134912.0; |
|
238 | - $q = floor($m / 4294967296.0); |
|
239 | - $l = $m - ($q * 4294967296.0); |
|
240 | - $h = $hi * 2328.0 + $q; |
|
241 | - |
|
242 | - return pack('NN', $h, $l); |
|
190 | + assert(is_numeric($v)); |
|
191 | + |
|
192 | + // x64 |
|
193 | + if (PHP_INT_SIZE >= 8) { |
|
194 | + assert($v >= 0); |
|
195 | + |
|
196 | + // x64, int |
|
197 | + if (is_int($v)) { |
|
198 | + return pack('NN', $v >> 32, $v & 0xFFFFFFFF); |
|
199 | + } |
|
200 | + |
|
201 | + // x64, bcmath |
|
202 | + if (function_exists('bcmul')) { |
|
203 | + $h = bcdiv($v, 4294967296, 0); |
|
204 | + $l = bcmod($v, 4294967296); |
|
205 | + return pack('NN', $h, $l); |
|
206 | + } |
|
207 | + |
|
208 | + // x64, no-bcmath |
|
209 | + $p = max(0, strlen($v) - 13); |
|
210 | + $lo = (int)substr($v, $p); |
|
211 | + $hi = (int)substr($v, 0, $p); |
|
212 | + |
|
213 | + $m = $lo + $hi * 1316134912; |
|
214 | + $l = $m % 4294967296; |
|
215 | + $h = $hi * 2328 + (int)($m / 4294967296); |
|
216 | + |
|
217 | + return pack('NN', $h, $l); |
|
218 | + } |
|
219 | + |
|
220 | + // x32, int |
|
221 | + if (is_int($v)) { |
|
222 | + return pack('NN', 0, $v); |
|
223 | + } |
|
224 | + |
|
225 | + // x32, bcmath |
|
226 | + if (function_exists('bcmul')) { |
|
227 | + $h = bcdiv($v, '4294967296', 0); |
|
228 | + $l = bcmod($v, '4294967296'); |
|
229 | + return pack('NN', (float)$h, (float)$l); // conversion to float is intentional; int would lose 31st bit |
|
230 | + } |
|
231 | + |
|
232 | + // x32, no-bcmath |
|
233 | + $p = max(0, strlen($v) - 13); |
|
234 | + $lo = (float)substr($v, $p); |
|
235 | + $hi = (float)substr($v, 0, $p); |
|
236 | + |
|
237 | + $m = $lo + $hi * 1316134912.0; |
|
238 | + $q = floor($m / 4294967296.0); |
|
239 | + $l = $m - ($q * 4294967296.0); |
|
240 | + $h = $hi * 2328.0 + $q; |
|
241 | + |
|
242 | + return pack('NN', $h, $l); |
|
243 | 243 | } |
244 | 244 | |
245 | 245 | // unpack 64-bit unsigned |
246 | 246 | function sphUnpackU64($v) |
247 | 247 | { |
248 | - list($hi, $lo) = array_values(unpack('N*N*', $v)); |
|
249 | - |
|
250 | - if (PHP_INT_SIZE >= 8) { |
|
251 | - if ($hi < 0) { // because php 5.2.2 to 5.2.5 is totally fucked up again |
|
252 | - $hi += 1 << 32; |
|
253 | - } |
|
254 | - if ($lo < 0) { |
|
255 | - $lo += 1 << 32; |
|
256 | - } |
|
257 | - |
|
258 | - // x64, int |
|
259 | - if ($hi <= 2147483647) { |
|
260 | - return ($hi << 32) + $lo; |
|
261 | - } |
|
262 | - |
|
263 | - // x64, bcmath |
|
264 | - if (function_exists('bcmul')) { |
|
265 | - return bcadd($lo, bcmul($hi, '4294967296')); |
|
266 | - } |
|
267 | - |
|
268 | - // x64, no-bcmath |
|
269 | - $C = 100000; |
|
270 | - $h = ((int)($hi / $C) << 32) + (int)($lo / $C); |
|
271 | - $l = (($hi % $C) << 32) + ($lo % $C); |
|
272 | - if ($l > $C) { |
|
273 | - $h += (int)($l / $C); |
|
274 | - $l = $l % $C; |
|
275 | - } |
|
276 | - |
|
277 | - if ($h == 0) { |
|
278 | - return $l; |
|
279 | - } |
|
280 | - return sprintf('%d%05d', $h, $l); |
|
281 | - } |
|
282 | - |
|
283 | - // x32, int |
|
284 | - if ($hi == 0) { |
|
285 | - if ($lo > 0) { |
|
286 | - return $lo; |
|
287 | - } |
|
288 | - return sprintf('%u', $lo); |
|
289 | - } |
|
290 | - |
|
291 | - $hi = sprintf('%u', $hi); |
|
292 | - $lo = sprintf('%u', $lo); |
|
293 | - |
|
294 | - // x32, bcmath |
|
295 | - if (function_exists('bcmul')) { |
|
296 | - return bcadd($lo, bcmul($hi, '4294967296')); |
|
297 | - } |
|
298 | - |
|
299 | - // x32, no-bcmath |
|
300 | - $hi = (float)$hi; |
|
301 | - $lo = (float)$lo; |
|
302 | - |
|
303 | - $q = floor($hi / 10000000.0); |
|
304 | - $r = $hi - $q * 10000000.0; |
|
305 | - $m = $lo + $r * 4967296.0; |
|
306 | - $mq = floor($m / 10000000.0); |
|
307 | - $l = $m - $mq * 10000000.0; |
|
308 | - $h = $q * 4294967296.0 + $r * 429.0 + $mq; |
|
309 | - |
|
310 | - $h = sprintf('%.0f', $h); |
|
311 | - $l = sprintf('%07.0f', $l); |
|
312 | - if ($h == '0') { |
|
313 | - return sprintf('%.0f', (float)$l); |
|
314 | - } |
|
315 | - return $h . $l; |
|
248 | + list($hi, $lo) = array_values(unpack('N*N*', $v)); |
|
249 | + |
|
250 | + if (PHP_INT_SIZE >= 8) { |
|
251 | + if ($hi < 0) { // because php 5.2.2 to 5.2.5 is totally fucked up again |
|
252 | + $hi += 1 << 32; |
|
253 | + } |
|
254 | + if ($lo < 0) { |
|
255 | + $lo += 1 << 32; |
|
256 | + } |
|
257 | + |
|
258 | + // x64, int |
|
259 | + if ($hi <= 2147483647) { |
|
260 | + return ($hi << 32) + $lo; |
|
261 | + } |
|
262 | + |
|
263 | + // x64, bcmath |
|
264 | + if (function_exists('bcmul')) { |
|
265 | + return bcadd($lo, bcmul($hi, '4294967296')); |
|
266 | + } |
|
267 | + |
|
268 | + // x64, no-bcmath |
|
269 | + $C = 100000; |
|
270 | + $h = ((int)($hi / $C) << 32) + (int)($lo / $C); |
|
271 | + $l = (($hi % $C) << 32) + ($lo % $C); |
|
272 | + if ($l > $C) { |
|
273 | + $h += (int)($l / $C); |
|
274 | + $l = $l % $C; |
|
275 | + } |
|
276 | + |
|
277 | + if ($h == 0) { |
|
278 | + return $l; |
|
279 | + } |
|
280 | + return sprintf('%d%05d', $h, $l); |
|
281 | + } |
|
282 | + |
|
283 | + // x32, int |
|
284 | + if ($hi == 0) { |
|
285 | + if ($lo > 0) { |
|
286 | + return $lo; |
|
287 | + } |
|
288 | + return sprintf('%u', $lo); |
|
289 | + } |
|
290 | + |
|
291 | + $hi = sprintf('%u', $hi); |
|
292 | + $lo = sprintf('%u', $lo); |
|
293 | + |
|
294 | + // x32, bcmath |
|
295 | + if (function_exists('bcmul')) { |
|
296 | + return bcadd($lo, bcmul($hi, '4294967296')); |
|
297 | + } |
|
298 | + |
|
299 | + // x32, no-bcmath |
|
300 | + $hi = (float)$hi; |
|
301 | + $lo = (float)$lo; |
|
302 | + |
|
303 | + $q = floor($hi / 10000000.0); |
|
304 | + $r = $hi - $q * 10000000.0; |
|
305 | + $m = $lo + $r * 4967296.0; |
|
306 | + $mq = floor($m / 10000000.0); |
|
307 | + $l = $m - $mq * 10000000.0; |
|
308 | + $h = $q * 4294967296.0 + $r * 429.0 + $mq; |
|
309 | + |
|
310 | + $h = sprintf('%.0f', $h); |
|
311 | + $l = sprintf('%07.0f', $l); |
|
312 | + if ($h == '0') { |
|
313 | + return sprintf('%.0f', (float)$l); |
|
314 | + } |
|
315 | + return $h . $l; |
|
316 | 316 | } |
317 | 317 | |
318 | 318 | // unpack 64-bit signed |
319 | 319 | function sphUnpackI64($v) |
320 | 320 | { |
321 | - list($hi, $lo) = array_values(unpack('N*N*', $v)); |
|
322 | - |
|
323 | - // x64 |
|
324 | - if (PHP_INT_SIZE >= 8) { |
|
325 | - if ($hi < 0) { // because php 5.2.2 to 5.2.5 is totally fucked up again |
|
326 | - $hi += 1 << 32; |
|
327 | - } |
|
328 | - if ($lo < 0) { |
|
329 | - $lo += 1 << 32; |
|
330 | - } |
|
331 | - |
|
332 | - return ($hi << 32) + $lo; |
|
333 | - } |
|
334 | - |
|
335 | - if ($hi == 0) { // x32, int |
|
336 | - if ($lo > 0) { |
|
337 | - return $lo; |
|
338 | - } |
|
339 | - return sprintf('%u', $lo); |
|
340 | - } elseif ($hi == -1) { // x32, int |
|
341 | - if ($lo < 0) { |
|
342 | - return $lo; |
|
343 | - } |
|
344 | - return sprintf('%.0f', $lo - 4294967296.0); |
|
345 | - } |
|
346 | - |
|
347 | - $neg = ''; |
|
348 | - $c = 0; |
|
349 | - if ($hi < 0) { |
|
350 | - $hi = ~$hi; |
|
351 | - $lo = ~$lo; |
|
352 | - $c = 1; |
|
353 | - $neg = '-'; |
|
354 | - } |
|
355 | - |
|
356 | - $hi = sprintf('%u', $hi); |
|
357 | - $lo = sprintf('%u', $lo); |
|
358 | - |
|
359 | - // x32, bcmath |
|
360 | - if (function_exists('bcmul')) { |
|
361 | - return $neg . bcadd(bcadd($lo, bcmul($hi, '4294967296')), $c); |
|
362 | - } |
|
363 | - |
|
364 | - // x32, no-bcmath |
|
365 | - $hi = (float)$hi; |
|
366 | - $lo = (float)$lo; |
|
367 | - |
|
368 | - $q = floor($hi / 10000000.0); |
|
369 | - $r = $hi - $q * 10000000.0; |
|
370 | - $m = $lo + $r * 4967296.0; |
|
371 | - $mq = floor($m / 10000000.0); |
|
372 | - $l = $m - $mq * 10000000.0 + $c; |
|
373 | - $h = $q * 4294967296.0 + $r * 429.0 + $mq; |
|
374 | - if ($l == 10000000) { |
|
375 | - $l = 0; |
|
376 | - $h += 1; |
|
377 | - } |
|
378 | - |
|
379 | - $h = sprintf('%.0f', $h); |
|
380 | - $l = sprintf('%07.0f', $l); |
|
381 | - if ($h == '0') { |
|
382 | - return $neg . sprintf('%.0f', (float)$l); |
|
383 | - } |
|
384 | - return $neg . $h . $l; |
|
321 | + list($hi, $lo) = array_values(unpack('N*N*', $v)); |
|
322 | + |
|
323 | + // x64 |
|
324 | + if (PHP_INT_SIZE >= 8) { |
|
325 | + if ($hi < 0) { // because php 5.2.2 to 5.2.5 is totally fucked up again |
|
326 | + $hi += 1 << 32; |
|
327 | + } |
|
328 | + if ($lo < 0) { |
|
329 | + $lo += 1 << 32; |
|
330 | + } |
|
331 | + |
|
332 | + return ($hi << 32) + $lo; |
|
333 | + } |
|
334 | + |
|
335 | + if ($hi == 0) { // x32, int |
|
336 | + if ($lo > 0) { |
|
337 | + return $lo; |
|
338 | + } |
|
339 | + return sprintf('%u', $lo); |
|
340 | + } elseif ($hi == -1) { // x32, int |
|
341 | + if ($lo < 0) { |
|
342 | + return $lo; |
|
343 | + } |
|
344 | + return sprintf('%.0f', $lo - 4294967296.0); |
|
345 | + } |
|
346 | + |
|
347 | + $neg = ''; |
|
348 | + $c = 0; |
|
349 | + if ($hi < 0) { |
|
350 | + $hi = ~$hi; |
|
351 | + $lo = ~$lo; |
|
352 | + $c = 1; |
|
353 | + $neg = '-'; |
|
354 | + } |
|
355 | + |
|
356 | + $hi = sprintf('%u', $hi); |
|
357 | + $lo = sprintf('%u', $lo); |
|
358 | + |
|
359 | + // x32, bcmath |
|
360 | + if (function_exists('bcmul')) { |
|
361 | + return $neg . bcadd(bcadd($lo, bcmul($hi, '4294967296')), $c); |
|
362 | + } |
|
363 | + |
|
364 | + // x32, no-bcmath |
|
365 | + $hi = (float)$hi; |
|
366 | + $lo = (float)$lo; |
|
367 | + |
|
368 | + $q = floor($hi / 10000000.0); |
|
369 | + $r = $hi - $q * 10000000.0; |
|
370 | + $m = $lo + $r * 4967296.0; |
|
371 | + $mq = floor($m / 10000000.0); |
|
372 | + $l = $m - $mq * 10000000.0 + $c; |
|
373 | + $h = $q * 4294967296.0 + $r * 429.0 + $mq; |
|
374 | + if ($l == 10000000) { |
|
375 | + $l = 0; |
|
376 | + $h += 1; |
|
377 | + } |
|
378 | + |
|
379 | + $h = sprintf('%.0f', $h); |
|
380 | + $l = sprintf('%07.0f', $l); |
|
381 | + if ($h == '0') { |
|
382 | + return $neg . sprintf('%.0f', (float)$l); |
|
383 | + } |
|
384 | + return $neg . $h . $l; |
|
385 | 385 | } |
386 | 386 | |
387 | 387 | |
388 | 388 | function sphFixUint($value) |
389 | 389 | { |
390 | - if (PHP_INT_SIZE >= 8) { |
|
391 | - // x64 route, workaround broken unpack() in 5.2.2+ |
|
392 | - if ($value < 0) { |
|
393 | - $value += 1 << 32; |
|
394 | - } |
|
395 | - return $value; |
|
396 | - } else { |
|
397 | - // x32 route, workaround php signed/unsigned braindamage |
|
398 | - return sprintf('%u', $value); |
|
399 | - } |
|
390 | + if (PHP_INT_SIZE >= 8) { |
|
391 | + // x64 route, workaround broken unpack() in 5.2.2+ |
|
392 | + if ($value < 0) { |
|
393 | + $value += 1 << 32; |
|
394 | + } |
|
395 | + return $value; |
|
396 | + } else { |
|
397 | + // x32 route, workaround php signed/unsigned braindamage |
|
398 | + return sprintf('%u', $value); |
|
399 | + } |
|
400 | 400 | } |
401 | 401 | |
402 | 402 | function sphSetBit($flag, $bit, $on) |
403 | 403 | { |
404 | - if ($on) { |
|
405 | - $flag |= 1 << $bit; |
|
406 | - } else { |
|
407 | - $reset = 16777215 ^ (1 << $bit); |
|
408 | - $flag = $flag & $reset; |
|
409 | - } |
|
410 | - return $flag; |
|
404 | + if ($on) { |
|
405 | + $flag |= 1 << $bit; |
|
406 | + } else { |
|
407 | + $reset = 16777215 ^ (1 << $bit); |
|
408 | + $flag = $flag & $reset; |
|
409 | + } |
|
410 | + return $flag; |
|
411 | 411 | } |
412 | 412 | |
413 | 413 | |
414 | 414 | /// sphinx searchd client class |
415 | 415 | class SphinxClient |
416 | 416 | { |
417 | - protected $host = 'localhost'; ///< searchd host (default is 'localhost') |
|
418 | - protected $port = 9312; ///< searchd port (default is 9312) |
|
419 | - protected $offset = 0; ///< how many records to seek from result-set start (default is 0) |
|
420 | - protected $limit = 20; ///< how many records to return from result-set starting at offset (default is 20) |
|
421 | - protected $mode = SPH_MATCH_EXTENDED2; ///< query matching mode (default is SPH_MATCH_EXTENDED2) |
|
422 | - protected $weights = array(); ///< per-field weights (default is 1 for all fields) |
|
423 | - protected $sort = SPH_SORT_RELEVANCE; ///< match sorting mode (default is SPH_SORT_RELEVANCE) |
|
424 | - protected $sort_by = ''; ///< attribute to sort by (defualt is '') |
|
425 | - protected $min_id = 0; ///< min ID to match (default is 0, which means no limit) |
|
426 | - protected $max_id = 0; ///< max ID to match (default is 0, which means no limit) |
|
427 | - protected $filters = array(); ///< search filters |
|
428 | - protected $group_by = ''; ///< group-by attribute name |
|
429 | - protected $group_func = SPH_GROUPBY_DAY; ///< group-by function (to pre-process group-by attribute value with) |
|
430 | - protected $group_sort = '@group desc'; ///< group-by sorting clause (to sort groups in result set with) |
|
431 | - protected $group_distinct = ''; ///< group-by count-distinct attribute |
|
432 | - protected $max_matches = 1000; ///< max matches to retrieve |
|
433 | - protected $cutoff = 0; ///< cutoff to stop searching at (default is 0) |
|
434 | - protected $retry_count = 0; ///< distributed retries count |
|
435 | - protected $retry_delay = 0; ///< distributed retries delay |
|
436 | - protected $anchor = array(); ///< geographical anchor point |
|
437 | - protected $index_weights = array(); ///< per-index weights |
|
438 | - protected $ranker = SPH_RANK_PROXIMITY_BM25; ///< ranking mode (default is SPH_RANK_PROXIMITY_BM25) |
|
439 | - protected $rank_expr = ''; ///< ranking mode expression (for SPH_RANK_EXPR) |
|
440 | - protected $max_query_time = 0; ///< max query time, milliseconds (default is 0, do not limit) |
|
441 | - protected $field_weights = array(); ///< per-field-name weights |
|
442 | - protected $overrides = array(); ///< per-query attribute values overrides |
|
443 | - protected $select = '*'; ///< select-list (attributes or expressions, with optional aliases) |
|
444 | - protected $query_flags = 0; ///< per-query various flags |
|
445 | - protected $predicted_time = 0; ///< per-query max_predicted_time |
|
446 | - protected $outer_order_by = ''; ///< outer match sort by |
|
447 | - protected $outer_offset = 0; ///< outer offset |
|
448 | - protected $outer_limit = 0; ///< outer limit |
|
449 | - protected $has_outer = false; |
|
450 | - |
|
451 | - protected $error = ''; ///< last error message |
|
452 | - protected $warning = ''; ///< last warning message |
|
453 | - protected $conn_error = false; ///< connection error vs remote error flag |
|
454 | - |
|
455 | - protected $reqs = array(); ///< requests array for multi-query |
|
456 | - protected $mbenc = ''; ///< stored mbstring encoding |
|
457 | - protected $array_result = false; ///< whether $result['matches'] should be a hash or an array |
|
458 | - protected $timeout = 0; ///< connect timeout |
|
459 | - |
|
460 | - protected $path = false; |
|
461 | - protected $socket = false; |
|
462 | - |
|
463 | - ///////////////////////////////////////////////////////////////////////////// |
|
464 | - // common stuff |
|
465 | - ///////////////////////////////////////////////////////////////////////////// |
|
466 | - |
|
467 | - public function __construct() |
|
468 | - { |
|
469 | - $this->query_flags = sphSetBit(0, 6, true); // default idf=tfidf_normalized |
|
470 | - } |
|
471 | - |
|
472 | - public function __destruct() |
|
473 | - { |
|
474 | - if ($this->socket !== false) { |
|
475 | - fclose($this->socket); |
|
476 | - } |
|
477 | - } |
|
478 | - |
|
479 | - /// get last error message (string) |
|
480 | - public function getLastError() |
|
481 | - { |
|
482 | - return $this->error; |
|
483 | - } |
|
484 | - |
|
485 | - /// get last warning message (string) |
|
486 | - public function getLastWarning() |
|
487 | - { |
|
488 | - return $this->warning; |
|
489 | - } |
|
490 | - |
|
491 | - /// get last error flag (to tell network connection errors from searchd errors or broken responses) |
|
492 | - public function isConnectError() |
|
493 | - { |
|
494 | - return $this->conn_error; |
|
495 | - } |
|
496 | - |
|
497 | - /// set searchd host name (string) and port (integer) |
|
498 | - public function setServer($host, $port = 0) |
|
499 | - { |
|
500 | - assert(is_string($host)); |
|
501 | - if ($host[0] == '/') { |
|
502 | - $this->path = 'unix://' . $host; |
|
503 | - return; |
|
504 | - } |
|
505 | - if (substr($host, 0, 7) == 'unix://') { |
|
506 | - $this->path = $host; |
|
507 | - return; |
|
508 | - } |
|
509 | - |
|
510 | - $this->host = $host; |
|
511 | - $port = intval($port); |
|
512 | - assert(0 <= $port && $port < 65536); |
|
513 | - $this->port = $port == 0 ? 9312 : $port; |
|
514 | - $this->path = ''; |
|
515 | - } |
|
516 | - |
|
517 | - /// set server connection timeout (0 to remove) |
|
518 | - public function setConnectTimeout($timeout) |
|
519 | - { |
|
520 | - assert(is_numeric($timeout)); |
|
521 | - $this->timeout = $timeout; |
|
522 | - } |
|
523 | - |
|
524 | - |
|
525 | - protected function send($handle, $data, $length) |
|
526 | - { |
|
527 | - if (feof($handle) || fwrite($handle, $data, $length) !== $length) { |
|
528 | - $this->error = 'connection unexpectedly closed (timed out?)'; |
|
529 | - $this->conn_error = true; |
|
530 | - return false; |
|
531 | - } |
|
532 | - return true; |
|
533 | - } |
|
534 | - |
|
535 | - ///////////////////////////////////////////////////////////////////////////// |
|
536 | - |
|
537 | - /// enter mbstring workaround mode |
|
538 | - protected function mbPush() |
|
539 | - { |
|
540 | - $this->mbenc = ''; |
|
541 | - if (ini_get('mbstring.func_overload') & 2) { |
|
542 | - $this->mbenc = mb_internal_encoding(); |
|
543 | - mb_internal_encoding('latin1'); |
|
544 | - } |
|
545 | - } |
|
546 | - |
|
547 | - /// leave mbstring workaround mode |
|
548 | - protected function mbPop() |
|
549 | - { |
|
550 | - if ($this->mbenc) { |
|
551 | - mb_internal_encoding($this->mbenc); |
|
552 | - } |
|
553 | - } |
|
554 | - |
|
555 | - /// connect to searchd server |
|
556 | - protected function connect() |
|
557 | - { |
|
558 | - if ($this->socket !== false) { |
|
559 | - // we are in persistent connection mode, so we have a socket |
|
560 | - // however, need to check whether it's still alive |
|
561 | - if (!@feof($this->socket)) { |
|
562 | - return $this->socket; |
|
563 | - } |
|
564 | - |
|
565 | - // force reopen |
|
566 | - $this->socket = false; |
|
567 | - } |
|
568 | - |
|
569 | - $errno = 0; |
|
570 | - $errstr = ''; |
|
571 | - $this->conn_error = false; |
|
572 | - |
|
573 | - if ($this->path) { |
|
574 | - $host = $this->path; |
|
575 | - $port = 0; |
|
576 | - } else { |
|
577 | - $host = $this->host; |
|
578 | - $port = $this->port; |
|
579 | - } |
|
580 | - |
|
581 | - if ($this->timeout <= 0) { |
|
582 | - $fp = @fsockopen($host, $port, $errno, $errstr); |
|
583 | - } else { |
|
584 | - $fp = @fsockopen($host, $port, $errno, $errstr, $this->timeout); |
|
585 | - } |
|
586 | - |
|
587 | - if (!$fp) { |
|
588 | - if ($this->path) { |
|
589 | - $location = $this->path; |
|
590 | - } else { |
|
591 | - $location = "{$this->host}:{$this->port}"; |
|
592 | - } |
|
593 | - |
|
594 | - $errstr = trim($errstr); |
|
595 | - $this->error = "connection to $location failed (errno=$errno, msg=$errstr)"; |
|
596 | - $this->conn_error = true; |
|
597 | - return false; |
|
598 | - } |
|
599 | - |
|
600 | - // send my version |
|
601 | - // this is a subtle part. we must do it before (!) reading back from searchd. |
|
602 | - // because otherwise under some conditions (reported on FreeBSD for instance) |
|
603 | - // TCP stack could throttle write-write-read pattern because of Nagle. |
|
604 | - if (!$this->send($fp, pack('N', 1), 4)) { |
|
605 | - fclose($fp); |
|
606 | - $this->error = 'failed to send client protocol version'; |
|
607 | - return false; |
|
608 | - } |
|
609 | - |
|
610 | - // check version |
|
611 | - list(, $v) = unpack('N*', fread($fp, 4)); |
|
612 | - $v = (int)$v; |
|
613 | - if ($v < 1) { |
|
614 | - fclose($fp); |
|
615 | - $this->error = "expected searchd protocol version 1+, got version '$v'"; |
|
616 | - return false; |
|
617 | - } |
|
618 | - |
|
619 | - return $fp; |
|
620 | - } |
|
621 | - |
|
622 | - /// get and check response packet from searchd server |
|
623 | - protected function getResponse($fp, $client_ver) |
|
624 | - { |
|
625 | - $response = ''; |
|
626 | - $len = 0; |
|
627 | - |
|
628 | - $header = fread($fp, 8); |
|
629 | - if (strlen($header) == 8) { |
|
630 | - list($status, $ver, $len) = array_values(unpack('n2a/Nb', $header)); |
|
631 | - $left = $len; |
|
632 | - while ($left > 0 && !feof($fp)) { |
|
633 | - $chunk = fread($fp, min(8192, $left)); |
|
634 | - if ($chunk) { |
|
635 | - $response .= $chunk; |
|
636 | - $left -= strlen($chunk); |
|
637 | - } |
|
638 | - } |
|
639 | - } |
|
640 | - if ($this->socket === false) { |
|
641 | - fclose($fp); |
|
642 | - } |
|
643 | - |
|
644 | - // check response |
|
645 | - $read = strlen($response); |
|
646 | - if (!$response || $read != $len) { |
|
647 | - $this->error = $len |
|
648 | - ? "failed to read searchd response (status=$status, ver=$ver, len=$len, read=$read)" |
|
649 | - : 'received zero-sized searchd response'; |
|
650 | - return false; |
|
651 | - } |
|
652 | - |
|
653 | - // check status |
|
654 | - if ($status == SEARCHD_WARNING) { |
|
655 | - list(, $wlen) = unpack('N*', substr($response, 0, 4)); |
|
656 | - $this->warning = substr($response, 4, $wlen); |
|
657 | - return substr($response, 4 + $wlen); |
|
658 | - } |
|
659 | - if ($status == SEARCHD_ERROR) { |
|
660 | - $this->error = 'searchd error: ' . substr($response, 4); |
|
661 | - return false; |
|
662 | - } |
|
663 | - if ($status == SEARCHD_RETRY) { |
|
664 | - $this->error = 'temporary searchd error: ' . substr($response, 4); |
|
665 | - return false; |
|
666 | - } |
|
667 | - if ($status != SEARCHD_OK) { |
|
668 | - $this->error = "unknown status code '$status'"; |
|
669 | - return false; |
|
670 | - } |
|
671 | - |
|
672 | - // check version |
|
673 | - if ($ver < $client_ver) { |
|
674 | - $this->warning = sprintf( |
|
675 | - 'searchd command v.%d.%d older than client\'s v.%d.%d, some options might not work', |
|
676 | - $ver >> 8, |
|
677 | - $ver & 0xff, |
|
678 | - $client_ver >> 8, |
|
679 | - $client_ver & 0xff |
|
680 | - ); |
|
681 | - } |
|
682 | - |
|
683 | - return $response; |
|
684 | - } |
|
685 | - |
|
686 | - ///////////////////////////////////////////////////////////////////////////// |
|
687 | - // searching |
|
688 | - ///////////////////////////////////////////////////////////////////////////// |
|
689 | - |
|
690 | - /// set offset and count into result set, |
|
691 | - /// and optionally set max-matches and cutoff limits |
|
692 | - public function setLimits($offset, $limit, $max = 0, $cutoff = 0) |
|
693 | - { |
|
694 | - assert(is_int($offset)); |
|
695 | - assert(is_int($limit)); |
|
696 | - assert($offset >= 0); |
|
697 | - assert($limit > 0); |
|
698 | - assert($max >= 0); |
|
699 | - $this->offset = $offset; |
|
700 | - $this->limit = $limit; |
|
701 | - if ($max > 0) { |
|
702 | - $this->max_matches = $max; |
|
703 | - } |
|
704 | - if ($cutoff > 0) { |
|
705 | - $this->cutoff = $cutoff; |
|
706 | - } |
|
707 | - } |
|
708 | - |
|
709 | - /// set maximum query time, in milliseconds, per-index |
|
710 | - /// integer, 0 means 'do not limit' |
|
711 | - public function setMaxQueryTime($max) |
|
712 | - { |
|
713 | - assert(is_int($max)); |
|
714 | - assert($max >= 0); |
|
715 | - $this->max_query_time = $max; |
|
716 | - } |
|
717 | - |
|
718 | - /// set matching mode |
|
719 | - public function setMatchMode($mode) |
|
720 | - { |
|
721 | - trigger_error( |
|
722 | - 'DEPRECATED: Do not call this method or, even better, use SphinxQL instead of an API', |
|
723 | - E_USER_DEPRECATED |
|
724 | - ); |
|
725 | - assert( |
|
726 | - $mode == SPH_MATCH_ALL || |
|
727 | - $mode == SPH_MATCH_ANY || |
|
728 | - $mode == SPH_MATCH_PHRASE || |
|
729 | - $mode == SPH_MATCH_BOOLEAN || |
|
730 | - $mode == SPH_MATCH_EXTENDED || |
|
731 | - $mode == SPH_MATCH_FULLSCAN || |
|
732 | - $mode == SPH_MATCH_EXTENDED2 |
|
733 | - ); |
|
734 | - $this->mode = $mode; |
|
735 | - } |
|
736 | - |
|
737 | - /// set ranking mode |
|
738 | - public function setRankingMode($ranker, $rankexpr='') |
|
739 | - { |
|
740 | - assert($ranker === 0 || $ranker >= 1 && $ranker < SPH_RANK_TOTAL); |
|
741 | - assert(is_string($rankexpr)); |
|
742 | - $this->ranker = $ranker; |
|
743 | - $this->rank_expr = $rankexpr; |
|
744 | - } |
|
745 | - |
|
746 | - /// set matches sorting mode |
|
747 | - public function setSortMode($mode, $sortby = '') |
|
748 | - { |
|
749 | - assert ( |
|
750 | - $mode == SPH_SORT_RELEVANCE || |
|
751 | - $mode == SPH_SORT_ATTR_DESC || |
|
752 | - $mode == SPH_SORT_ATTR_ASC || |
|
753 | - $mode == SPH_SORT_TIME_SEGMENTS || |
|
754 | - $mode == SPH_SORT_EXTENDED || |
|
755 | - $mode == SPH_SORT_EXPR |
|
756 | - ); |
|
757 | - assert(is_string($sortby)); |
|
758 | - assert($mode == SPH_SORT_RELEVANCE || strlen($sortby) > 0); |
|
759 | - |
|
760 | - $this->sort = $mode; |
|
761 | - $this->sort_by = $sortby; |
|
762 | - } |
|
763 | - |
|
764 | - /** |
|
765 | - * Bind per-field weights by order |
|
766 | - * |
|
767 | - * @deprecated use setFieldWeights() instead |
|
768 | - */ |
|
769 | - public function setWeights() |
|
770 | - { |
|
771 | - throw new \RuntimeException('This method is now deprecated; please use setFieldWeights instead'); |
|
772 | - } |
|
773 | - |
|
774 | - /// bind per-field weights by name |
|
775 | - public function setFieldWeights($weights) |
|
776 | - { |
|
777 | - assert(is_array($weights)); |
|
778 | - foreach ($weights as $name => $weight) { |
|
779 | - assert(is_string($name)); |
|
780 | - assert(is_int($weight)); |
|
781 | - } |
|
782 | - $this->field_weights = $weights; |
|
783 | - } |
|
784 | - |
|
785 | - /// bind per-index weights by name |
|
786 | - public function setIndexWeights($weights) |
|
787 | - { |
|
788 | - assert(is_array($weights)); |
|
789 | - foreach ($weights as $index => $weight) { |
|
790 | - assert(is_string($index)); |
|
791 | - assert(is_int($weight)); |
|
792 | - } |
|
793 | - $this->index_weights = $weights; |
|
794 | - } |
|
795 | - |
|
796 | - /// set IDs range to match |
|
797 | - /// only match records if document ID is beetwen $min and $max (inclusive) |
|
798 | - public function setIDRange($min, $max) |
|
799 | - { |
|
800 | - assert(is_numeric($min)); |
|
801 | - assert(is_numeric($max)); |
|
802 | - assert($min <= $max); |
|
803 | - $this->min_id = $min; |
|
804 | - $this->max_id = $max; |
|
805 | - } |
|
806 | - |
|
807 | - /// set values set filter |
|
808 | - /// only match records where $attribute value is in given set |
|
809 | - public function setFilter($attribute, $values, $exclude = false) |
|
810 | - { |
|
811 | - assert(is_string($attribute)); |
|
812 | - assert(is_array($values)); |
|
813 | - assert(count($values)); |
|
814 | - |
|
815 | - if (is_array($values) && count($values)) { |
|
816 | - foreach ($values as $value) { |
|
817 | - assert(is_numeric($value)); |
|
818 | - } |
|
819 | - |
|
820 | - $this->filters[] = array( |
|
821 | - 'type' => SPH_FILTER_VALUES, |
|
822 | - 'attr' => $attribute, |
|
823 | - 'exclude' => $exclude, |
|
824 | - 'values' => $values |
|
825 | - ); |
|
826 | - } |
|
827 | - } |
|
828 | - |
|
829 | - /// set string filter |
|
830 | - /// only match records where $attribute value is equal |
|
831 | - public function setFilterString($attribute, $value, $exclude = false) |
|
832 | - { |
|
833 | - assert(is_string($attribute)); |
|
834 | - assert(is_string($value)); |
|
835 | - $this->filters[] = array( |
|
836 | - 'type' => SPH_FILTER_STRING, |
|
837 | - 'attr' => $attribute, |
|
838 | - 'exclude' => $exclude, |
|
839 | - 'value' => $value |
|
840 | - ); |
|
841 | - } |
|
842 | - |
|
843 | - /// set range filter |
|
844 | - /// only match records if $attribute value is beetwen $min and $max (inclusive) |
|
845 | - public function setFilterRange($attribute, $min, $max, $exclude = false) |
|
846 | - { |
|
847 | - assert(is_string($attribute)); |
|
848 | - assert(is_numeric($min)); |
|
849 | - assert(is_numeric($max)); |
|
850 | - assert($min <= $max); |
|
851 | - |
|
852 | - $this->filters[] = array( |
|
853 | - 'type' => SPH_FILTER_RANGE, |
|
854 | - 'attr' => $attribute, |
|
855 | - 'exclude' => $exclude, |
|
856 | - 'min' => $min, |
|
857 | - 'max' => $max |
|
858 | - ); |
|
859 | - } |
|
860 | - |
|
861 | - /// set float range filter |
|
862 | - /// only match records if $attribute value is beetwen $min and $max (inclusive) |
|
863 | - public function setFilterFloatRange($attribute, $min, $max, $exclude = false) |
|
864 | - { |
|
865 | - assert(is_string($attribute)); |
|
866 | - assert(is_float($min)); |
|
867 | - assert(is_float($max)); |
|
868 | - assert($min <= $max); |
|
869 | - |
|
870 | - $this->filters[] = array( |
|
871 | - 'type' => SPH_FILTER_FLOATRANGE, |
|
872 | - 'attr' => $attribute, |
|
873 | - 'exclude' => $exclude, |
|
874 | - 'min' => $min, |
|
875 | - 'max' => $max |
|
876 | - ); |
|
877 | - } |
|
878 | - |
|
879 | - /// setup anchor point for geosphere distance calculations |
|
880 | - /// required to use @geodist in filters and sorting |
|
881 | - /// latitude and longitude must be in radians |
|
882 | - public function setGeoAnchor($attrlat, $attrlong, $lat, $long) |
|
883 | - { |
|
884 | - assert(is_string($attrlat)); |
|
885 | - assert(is_string($attrlong)); |
|
886 | - assert(is_float($lat)); |
|
887 | - assert(is_float($long)); |
|
888 | - |
|
889 | - $this->anchor = array( |
|
890 | - 'attrlat' => $attrlat, |
|
891 | - 'attrlong' => $attrlong, |
|
892 | - 'lat' => $lat, |
|
893 | - 'long' => $long |
|
894 | - ); |
|
895 | - } |
|
896 | - |
|
897 | - /// set grouping attribute and function |
|
898 | - public function setGroupBy($attribute, $func, $groupsort = '@group desc') |
|
899 | - { |
|
900 | - assert(is_string($attribute)); |
|
901 | - assert(is_string($groupsort)); |
|
902 | - assert( |
|
903 | - $func == SPH_GROUPBY_DAY || |
|
904 | - $func == SPH_GROUPBY_WEEK || |
|
905 | - $func == SPH_GROUPBY_MONTH || |
|
906 | - $func == SPH_GROUPBY_YEAR || |
|
907 | - $func == SPH_GROUPBY_ATTR || |
|
908 | - $func == SPH_GROUPBY_ATTRPAIR |
|
909 | - ); |
|
910 | - |
|
911 | - $this->group_by = $attribute; |
|
912 | - $this->group_func = $func; |
|
913 | - $this->group_sort = $groupsort; |
|
914 | - } |
|
915 | - |
|
916 | - /// set count-distinct attribute for group-by queries |
|
917 | - public function setGroupDistinct($attribute) |
|
918 | - { |
|
919 | - assert(is_string($attribute)); |
|
920 | - $this->group_distinct = $attribute; |
|
921 | - } |
|
922 | - |
|
923 | - /// set distributed retries count and delay |
|
924 | - public function setRetries($count, $delay = 0) |
|
925 | - { |
|
926 | - assert(is_int($count) && $count >= 0); |
|
927 | - assert(is_int($delay) && $delay >= 0); |
|
928 | - $this->retry_count = $count; |
|
929 | - $this->retry_delay = $delay; |
|
930 | - } |
|
931 | - |
|
932 | - /// set result set format (hash or array; hash by default) |
|
933 | - /// PHP specific; needed for group-by-MVA result sets that may contain duplicate IDs |
|
934 | - public function setArrayResult($arrayresult) |
|
935 | - { |
|
936 | - assert(is_bool($arrayresult)); |
|
937 | - $this->array_result = $arrayresult; |
|
938 | - } |
|
939 | - |
|
940 | - /// set attribute values override |
|
941 | - /// there can be only one override per attribute |
|
942 | - /// $values must be a hash that maps document IDs to attribute values |
|
943 | - public function setOverride($attrname, $attrtype, $values) |
|
944 | - { |
|
945 | - trigger_error( |
|
946 | - 'DEPRECATED: Do not call this method. Use SphinxQL REMAP() function instead.', |
|
947 | - E_USER_DEPRECATED |
|
948 | - ); |
|
949 | - assert(is_string($attrname)); |
|
950 | - assert(in_array($attrtype, array( |
|
951 | - SPH_ATTR_INTEGER, |
|
952 | - SPH_ATTR_TIMESTAMP, |
|
953 | - SPH_ATTR_BOOL, |
|
954 | - SPH_ATTR_FLOAT, |
|
955 | - SPH_ATTR_BIGINT |
|
956 | - ))); |
|
957 | - assert(is_array($values)); |
|
958 | - |
|
959 | - $this->overrides[$attrname] = array( |
|
960 | - 'attr' => $attrname, |
|
961 | - 'type' => $attrtype, |
|
962 | - 'values' => $values |
|
963 | - ); |
|
964 | - } |
|
965 | - |
|
966 | - /// set select-list (attributes or expressions), SQL-like syntax |
|
967 | - public function setSelect($select) |
|
968 | - { |
|
969 | - assert(is_string($select)); |
|
970 | - $this->select = $select; |
|
971 | - } |
|
972 | - |
|
973 | - public function setQueryFlag($flag_name, $flag_value) |
|
974 | - { |
|
975 | - $known_names = array( |
|
976 | - 'reverse_scan', |
|
977 | - 'sort_method', |
|
978 | - 'max_predicted_time', |
|
979 | - 'boolean_simplify', |
|
980 | - 'idf', |
|
981 | - 'global_idf', |
|
982 | - 'low_priority' |
|
983 | - ); |
|
984 | - $flags = array ( |
|
985 | - 'reverse_scan' => array(0, 1), |
|
986 | - 'sort_method' => array('pq', 'kbuffer'), |
|
987 | - 'max_predicted_time' => array(0), |
|
988 | - 'boolean_simplify' => array(true, false), |
|
989 | - 'idf' => array ('normalized', 'plain', 'tfidf_normalized', 'tfidf_unnormalized'), |
|
990 | - 'global_idf' => array(true, false), |
|
991 | - 'low_priority' => array(true, false) |
|
992 | - ); |
|
993 | - |
|
994 | - assert(isset($flag_name, $known_names)); |
|
995 | - assert( |
|
996 | - in_array($flag_value, $flags[$flag_name], true) || |
|
997 | - ($flag_name == 'max_predicted_time' && is_int($flag_value) && $flag_value >= 0) |
|
998 | - ); |
|
999 | - |
|
1000 | - switch ($flag_name) { |
|
1001 | - case 'reverse_scan': |
|
1002 | - $this->query_flags = sphSetBit($this->query_flags, 0, $flag_value == 1); |
|
1003 | - break; |
|
1004 | - case 'sort_method': |
|
1005 | - $this->query_flags = sphSetBit($this->query_flags, 1, $flag_value == 'kbuffer'); |
|
1006 | - break; |
|
1007 | - case 'max_predicted_time': |
|
1008 | - $this->query_flags = sphSetBit($this->query_flags, 2, $flag_value > 0); |
|
1009 | - $this->predicted_time = (int)$flag_value; |
|
1010 | - break; |
|
1011 | - case 'boolean_simplify': |
|
1012 | - $this->query_flags = sphSetBit($this->query_flags, 3, $flag_value); |
|
1013 | - break; |
|
1014 | - case 'idf': |
|
1015 | - if ($flag_value == 'normalized' || $flag_value == 'plain') { |
|
1016 | - $this->query_flags = sphSetBit($this->query_flags, 4, $flag_value == 'plain'); |
|
1017 | - } |
|
1018 | - if ($flag_value == 'tfidf_normalized' || $flag_value == 'tfidf_unnormalized') { |
|
1019 | - $this->query_flags = sphSetBit($this->query_flags, 6, $flag_value == 'tfidf_normalized'); |
|
1020 | - } |
|
1021 | - break; |
|
1022 | - case 'global_idf': |
|
1023 | - $this->query_flags = sphSetBit($this->query_flags, 5, $flag_value); |
|
1024 | - break; |
|
1025 | - case 'low_priority': |
|
1026 | - $this->query_flags = sphSetBit($this->query_flags, 8, $flag_value); |
|
1027 | - break; |
|
1028 | - } |
|
1029 | - } |
|
1030 | - |
|
1031 | - /// set outer order by parameters |
|
1032 | - public function setOuterSelect($orderby, $offset, $limit) |
|
1033 | - { |
|
1034 | - assert(is_string($orderby)); |
|
1035 | - assert(is_int($offset)); |
|
1036 | - assert(is_int($limit)); |
|
1037 | - assert($offset >= 0); |
|
1038 | - assert($limit > 0); |
|
1039 | - |
|
1040 | - $this->outer_order_by = $orderby; |
|
1041 | - $this->outer_offset = $offset; |
|
1042 | - $this->outer_limit = $limit; |
|
1043 | - $this->has_outer = true; |
|
1044 | - } |
|
1045 | - |
|
1046 | - |
|
1047 | - ////////////////////////////////////////////////////////////////////////////// |
|
1048 | - |
|
1049 | - /// clear all filters (for multi-queries) |
|
1050 | - public function resetFilters() |
|
1051 | - { |
|
1052 | - $this->filters = array(); |
|
1053 | - $this->anchor = array(); |
|
1054 | - } |
|
1055 | - |
|
1056 | - /// clear groupby settings (for multi-queries) |
|
1057 | - public function resetGroupBy() |
|
1058 | - { |
|
1059 | - $this->group_by = ''; |
|
1060 | - $this->group_func = SPH_GROUPBY_DAY; |
|
1061 | - $this->group_sort = '@group desc'; |
|
1062 | - $this->group_distinct = ''; |
|
1063 | - } |
|
1064 | - |
|
1065 | - /// clear all attribute value overrides (for multi-queries) |
|
1066 | - public function resetOverrides() |
|
1067 | - { |
|
1068 | - $this->overrides = array(); |
|
1069 | - } |
|
1070 | - |
|
1071 | - public function resetQueryFlag() |
|
1072 | - { |
|
1073 | - $this->query_flags = sphSetBit(0, 6, true); // default idf=tfidf_normalized |
|
1074 | - $this->predicted_time = 0; |
|
1075 | - } |
|
1076 | - |
|
1077 | - public function resetOuterSelect() |
|
1078 | - { |
|
1079 | - $this->outer_order_by = ''; |
|
1080 | - $this->outer_offset = 0; |
|
1081 | - $this->outer_limit = 0; |
|
1082 | - $this->has_outer = false; |
|
1083 | - } |
|
1084 | - |
|
1085 | - ////////////////////////////////////////////////////////////////////////////// |
|
1086 | - |
|
1087 | - /// connect to searchd server, run given search query through given indexes, |
|
1088 | - /// and return the search results |
|
1089 | - public function query($query, $index = '*', $comment = '') |
|
1090 | - { |
|
1091 | - assert(empty($this->reqs)); |
|
1092 | - |
|
1093 | - $this->addQuery($query, $index, $comment); |
|
1094 | - $results = $this->runQueries(); |
|
1095 | - $this->reqs = array(); // just in case it failed too early |
|
1096 | - |
|
1097 | - if (!is_array($results)) { |
|
1098 | - return false; // probably network error; error message should be already filled |
|
1099 | - } |
|
1100 | - |
|
1101 | - $this->error = $results[0]['error']; |
|
1102 | - $this->warning = $results[0]['warning']; |
|
1103 | - if ($results[0]['status'] == SEARCHD_ERROR) { |
|
1104 | - return false; |
|
1105 | - } else { |
|
1106 | - return $results[0]; |
|
1107 | - } |
|
1108 | - } |
|
1109 | - |
|
1110 | - /// helper to pack floats in network byte order |
|
1111 | - protected function packFloat($f) |
|
1112 | - { |
|
1113 | - $t1 = pack('f', $f); // machine order |
|
1114 | - list(, $t2) = unpack('L*', $t1); // int in machine order |
|
1115 | - return pack('N', $t2); |
|
1116 | - } |
|
1117 | - |
|
1118 | - /// add query to multi-query batch |
|
1119 | - /// returns index into results array from RunQueries() call |
|
1120 | - public function addQuery($query, $index = '*', $comment = '') |
|
1121 | - { |
|
1122 | - // mbstring workaround |
|
1123 | - $this->mbPush(); |
|
1124 | - |
|
1125 | - // build request |
|
1126 | - $req = pack('NNNNN', $this->query_flags, $this->offset, $this->limit, $this->mode, $this->ranker); |
|
1127 | - if ($this->ranker == SPH_RANK_EXPR) { |
|
1128 | - $req .= pack('N', strlen($this->rank_expr)) . $this->rank_expr; |
|
1129 | - } |
|
1130 | - $req .= pack('N', $this->sort); // (deprecated) sort mode |
|
1131 | - $req .= pack('N', strlen($this->sort_by)) . $this->sort_by; |
|
1132 | - $req .= pack('N', strlen($query)) . $query; // query itself |
|
1133 | - $req .= pack('N', count($this->weights)); // weights |
|
1134 | - foreach ($this->weights as $weight) { |
|
1135 | - $req .= pack('N', (int)$weight); |
|
1136 | - } |
|
1137 | - $req .= pack('N', strlen($index)) . $index; // indexes |
|
1138 | - $req .= pack('N', 1); // id64 range marker |
|
1139 | - $req .= sphPackU64($this->min_id) . sphPackU64($this->max_id); // id64 range |
|
1140 | - |
|
1141 | - // filters |
|
1142 | - $req .= pack('N', count($this->filters)); |
|
1143 | - foreach ($this->filters as $filter) { |
|
1144 | - $req .= pack('N', strlen($filter['attr'])) . $filter['attr']; |
|
1145 | - $req .= pack('N', $filter['type']); |
|
1146 | - switch ($filter['type']) { |
|
1147 | - case SPH_FILTER_VALUES: |
|
1148 | - $req .= pack('N', count($filter['values'])); |
|
1149 | - foreach ($filter['values'] as $value) { |
|
1150 | - $req .= sphPackI64($value); |
|
1151 | - } |
|
1152 | - break; |
|
1153 | - case SPH_FILTER_RANGE: |
|
1154 | - $req .= sphPackI64($filter['min']) . sphPackI64($filter['max']); |
|
1155 | - break; |
|
1156 | - case SPH_FILTER_FLOATRANGE: |
|
1157 | - $req .= $this->packFloat($filter['min']) . $this->packFloat($filter['max']); |
|
1158 | - break; |
|
1159 | - case SPH_FILTER_STRING: |
|
1160 | - $req .= pack('N', strlen($filter['value'])) . $filter['value']; |
|
1161 | - break; |
|
1162 | - default: |
|
1163 | - assert(0 && 'internal error: unhandled filter type'); |
|
1164 | - } |
|
1165 | - $req .= pack('N', $filter['exclude']); |
|
1166 | - } |
|
1167 | - |
|
1168 | - // group-by clause, max-matches count, group-sort clause, cutoff count |
|
1169 | - $req .= pack('NN', $this->group_func, strlen($this->group_by)) . $this->group_by; |
|
1170 | - $req .= pack('N', $this->max_matches); |
|
1171 | - $req .= pack('N', strlen($this->group_sort)) . $this->group_sort; |
|
1172 | - $req .= pack('NNN', $this->cutoff, $this->retry_count, $this->retry_delay); |
|
1173 | - $req .= pack('N', strlen($this->group_distinct)) . $this->group_distinct; |
|
1174 | - |
|
1175 | - // anchor point |
|
1176 | - if (empty($this->anchor)) { |
|
1177 | - $req .= pack('N', 0); |
|
1178 | - } else { |
|
1179 | - $a =& $this->anchor; |
|
1180 | - $req .= pack('N', 1); |
|
1181 | - $req .= pack('N', strlen($a['attrlat'])) . $a['attrlat']; |
|
1182 | - $req .= pack('N', strlen($a['attrlong'])) . $a['attrlong']; |
|
1183 | - $req .= $this->packFloat($a['lat']) . $this->packFloat($a['long']); |
|
1184 | - } |
|
1185 | - |
|
1186 | - // per-index weights |
|
1187 | - $req .= pack('N', count($this->index_weights)); |
|
1188 | - foreach ($this->index_weights as $idx => $weight) { |
|
1189 | - $req .= pack('N', strlen($idx)) . $idx . pack('N', $weight); |
|
1190 | - } |
|
1191 | - |
|
1192 | - // max query time |
|
1193 | - $req .= pack('N', $this->max_query_time); |
|
1194 | - |
|
1195 | - // per-field weights |
|
1196 | - $req .= pack('N', count($this->field_weights)); |
|
1197 | - foreach ($this->field_weights as $field => $weight) { |
|
1198 | - $req .= pack('N', strlen($field)) . $field . pack('N', $weight); |
|
1199 | - } |
|
1200 | - |
|
1201 | - // comment |
|
1202 | - $req .= pack('N', strlen($comment)) . $comment; |
|
1203 | - |
|
1204 | - // attribute overrides |
|
1205 | - $req .= pack('N', count($this->overrides)); |
|
1206 | - foreach ($this->overrides as $key => $entry) { |
|
1207 | - $req .= pack('N', strlen($entry['attr'])) . $entry['attr']; |
|
1208 | - $req .= pack('NN', $entry['type'], count($entry['values'])); |
|
1209 | - foreach ($entry['values'] as $id => $val) { |
|
1210 | - assert(is_numeric($id)); |
|
1211 | - assert(is_numeric($val)); |
|
1212 | - |
|
1213 | - $req .= sphPackU64($id); |
|
1214 | - switch ($entry['type']) { |
|
1215 | - case SPH_ATTR_FLOAT: |
|
1216 | - $req .= $this->packFloat($val); |
|
1217 | - break; |
|
1218 | - case SPH_ATTR_BIGINT: |
|
1219 | - $req .= sphPackI64($val); |
|
1220 | - break; |
|
1221 | - default: |
|
1222 | - $req .= pack('N', $val); |
|
1223 | - break; |
|
1224 | - } |
|
1225 | - } |
|
1226 | - } |
|
1227 | - |
|
1228 | - // select-list |
|
1229 | - $req .= pack('N', strlen($this->select)) . $this->select; |
|
1230 | - |
|
1231 | - // max_predicted_time |
|
1232 | - if ($this->predicted_time > 0) { |
|
1233 | - $req .= pack('N', (int)$this->predicted_time); |
|
1234 | - } |
|
1235 | - |
|
1236 | - $req .= pack('N', strlen($this->outer_order_by)) . $this->outer_order_by; |
|
1237 | - $req .= pack('NN', $this->outer_offset, $this->outer_limit); |
|
1238 | - if ($this->has_outer) { |
|
1239 | - $req .= pack('N', 1); |
|
1240 | - } else { |
|
1241 | - $req .= pack('N', 0); |
|
1242 | - } |
|
1243 | - |
|
1244 | - // mbstring workaround |
|
1245 | - $this->mbPop(); |
|
1246 | - |
|
1247 | - // store request to requests array |
|
1248 | - $this->reqs[] = $req; |
|
1249 | - return count($this->reqs) - 1; |
|
1250 | - } |
|
1251 | - |
|
1252 | - /// connect to searchd, run queries batch, and return an array of result sets |
|
1253 | - public function runQueries() |
|
1254 | - { |
|
1255 | - if (empty($this->reqs)) { |
|
1256 | - $this->error = 'no queries defined, issue AddQuery() first'; |
|
1257 | - return false; |
|
1258 | - } |
|
1259 | - |
|
1260 | - // mbstring workaround |
|
1261 | - $this->mbPush(); |
|
1262 | - |
|
1263 | - if (!($fp = $this->connect())) { |
|
1264 | - $this->mbPop(); |
|
1265 | - return false; |
|
1266 | - } |
|
1267 | - |
|
1268 | - // send query, get response |
|
1269 | - $nreqs = count($this->reqs); |
|
1270 | - $req = join('', $this->reqs); |
|
1271 | - $len = 8 + strlen($req); |
|
1272 | - $req = pack('nnNNN', SEARCHD_COMMAND_SEARCH, VER_COMMAND_SEARCH, $len, 0, $nreqs) . $req; // add header |
|
1273 | - |
|
1274 | - if (!$this->send($fp, $req, $len + 8) || !($response = $this->getResponse($fp, VER_COMMAND_SEARCH))) { |
|
1275 | - $this->mbPop(); |
|
1276 | - return false; |
|
1277 | - } |
|
1278 | - |
|
1279 | - // query sent ok; we can reset reqs now |
|
1280 | - $this->reqs = array(); |
|
1281 | - |
|
1282 | - // parse and return response |
|
1283 | - return $this->parseSearchResponse($response, $nreqs); |
|
1284 | - } |
|
1285 | - |
|
1286 | - /// parse and return search query (or queries) response |
|
1287 | - protected function parseSearchResponse($response, $nreqs) |
|
1288 | - { |
|
1289 | - $p = 0; // current position |
|
1290 | - $max = strlen($response); // max position for checks, to protect against broken responses |
|
1291 | - |
|
1292 | - $results = array(); |
|
1293 | - for ($ires = 0; $ires < $nreqs && $p < $max; $ires++) { |
|
1294 | - $results[] = array(); |
|
1295 | - $result =& $results[$ires]; |
|
1296 | - |
|
1297 | - $result['error'] = ''; |
|
1298 | - $result['warning'] = ''; |
|
1299 | - |
|
1300 | - // extract status |
|
1301 | - list(, $status) = unpack('N*', substr($response, $p, 4)); |
|
1302 | - $p += 4; |
|
1303 | - $result['status'] = $status; |
|
1304 | - if ($status != SEARCHD_OK) { |
|
1305 | - list(, $len) = unpack('N*', substr($response, $p, 4)); |
|
1306 | - $p += 4; |
|
1307 | - $message = substr($response, $p, $len); |
|
1308 | - $p += $len; |
|
1309 | - |
|
1310 | - if ($status == SEARCHD_WARNING) { |
|
1311 | - $result['warning'] = $message; |
|
1312 | - } else { |
|
1313 | - $result['error'] = $message; |
|
1314 | - continue; |
|
1315 | - } |
|
1316 | - } |
|
1317 | - |
|
1318 | - // read schema |
|
1319 | - $fields = array(); |
|
1320 | - $attrs = array(); |
|
1321 | - |
|
1322 | - list(, $nfields) = unpack('N*', substr($response, $p, 4)); |
|
1323 | - $p += 4; |
|
1324 | - while ($nfields --> 0 && $p < $max) { |
|
1325 | - list(, $len) = unpack('N*', substr($response, $p, 4)); |
|
1326 | - $p += 4; |
|
1327 | - $fields[] = substr($response, $p, $len); |
|
1328 | - $p += $len; |
|
1329 | - } |
|
1330 | - $result['fields'] = $fields; |
|
1331 | - |
|
1332 | - list(, $nattrs) = unpack('N*', substr($response, $p, 4)); |
|
1333 | - $p += 4; |
|
1334 | - while ($nattrs --> 0 && $p < $max) { |
|
1335 | - list(, $len) = unpack('N*', substr($response, $p, 4)); |
|
1336 | - $p += 4; |
|
1337 | - $attr = substr($response, $p, $len); |
|
1338 | - $p += $len; |
|
1339 | - list(, $type) = unpack('N*', substr($response, $p, 4)); |
|
1340 | - $p += 4; |
|
1341 | - $attrs[$attr] = $type; |
|
1342 | - } |
|
1343 | - $result['attrs'] = $attrs; |
|
1344 | - |
|
1345 | - // read match count |
|
1346 | - list(, $count) = unpack('N*', substr($response, $p, 4)); |
|
1347 | - $p += 4; |
|
1348 | - list(, $id64) = unpack('N*', substr($response, $p, 4)); |
|
1349 | - $p += 4; |
|
1350 | - |
|
1351 | - // read matches |
|
1352 | - $idx = -1; |
|
1353 | - while ($count --> 0 && $p < $max) { |
|
1354 | - // index into result array |
|
1355 | - $idx++; |
|
1356 | - |
|
1357 | - // parse document id and weight |
|
1358 | - if ($id64) { |
|
1359 | - $doc = sphUnpackU64(substr($response, $p, 8)); |
|
1360 | - $p += 8; |
|
1361 | - list(,$weight) = unpack('N*', substr($response, $p, 4)); |
|
1362 | - $p += 4; |
|
1363 | - } else { |
|
1364 | - list($doc, $weight) = array_values(unpack('N*N*', substr($response, $p, 8))); |
|
1365 | - $p += 8; |
|
1366 | - $doc = sphFixUint($doc); |
|
1367 | - } |
|
1368 | - $weight = sprintf('%u', $weight); |
|
1369 | - |
|
1370 | - // create match entry |
|
1371 | - if ($this->array_result) { |
|
1372 | - $result['matches'][$idx] = array('id' => $doc, 'weight' => $weight); |
|
1373 | - } else { |
|
1374 | - $result['matches'][$doc]['weight'] = $weight; |
|
1375 | - } |
|
1376 | - |
|
1377 | - // parse and create attributes |
|
1378 | - $attrvals = array(); |
|
1379 | - foreach ($attrs as $attr => $type) { |
|
1380 | - // handle 64bit ints |
|
1381 | - if ($type == SPH_ATTR_BIGINT) { |
|
1382 | - $attrvals[$attr] = sphUnpackI64(substr($response, $p, 8)); |
|
1383 | - $p += 8; |
|
1384 | - continue; |
|
1385 | - } |
|
1386 | - |
|
1387 | - // handle floats |
|
1388 | - if ($type == SPH_ATTR_FLOAT) { |
|
1389 | - list(, $uval) = unpack('N*', substr($response, $p, 4)); |
|
1390 | - $p += 4; |
|
1391 | - list(, $fval) = unpack('f*', pack('L', $uval)); |
|
1392 | - $attrvals[$attr] = $fval; |
|
1393 | - continue; |
|
1394 | - } |
|
1395 | - |
|
1396 | - // handle everything else as unsigned ints |
|
1397 | - list(, $val) = unpack('N*', substr($response, $p, 4)); |
|
1398 | - $p += 4; |
|
1399 | - if ($type == SPH_ATTR_MULTI) { |
|
1400 | - $attrvals[$attr] = array(); |
|
1401 | - $nvalues = $val; |
|
1402 | - while ($nvalues --> 0 && $p < $max) { |
|
1403 | - list(, $val) = unpack('N*', substr($response, $p, 4)); |
|
1404 | - $p += 4; |
|
1405 | - $attrvals[$attr][] = sphFixUint($val); |
|
1406 | - } |
|
1407 | - } elseif ($type == SPH_ATTR_MULTI64) { |
|
1408 | - $attrvals[$attr] = array(); |
|
1409 | - $nvalues = $val; |
|
1410 | - while ($nvalues > 0 && $p < $max) { |
|
1411 | - $attrvals[$attr][] = sphUnpackI64(substr($response, $p, 8)); |
|
1412 | - $p += 8; |
|
1413 | - $nvalues -= 2; |
|
1414 | - } |
|
1415 | - } elseif ($type == SPH_ATTR_STRING) { |
|
1416 | - $attrvals[$attr] = substr($response, $p, $val); |
|
1417 | - $p += $val; |
|
1418 | - } elseif ($type == SPH_ATTR_FACTORS) { |
|
1419 | - $attrvals[$attr] = substr($response, $p, $val - 4); |
|
1420 | - $p += $val-4; |
|
1421 | - } else { |
|
1422 | - $attrvals[$attr] = sphFixUint($val); |
|
1423 | - } |
|
1424 | - } |
|
1425 | - |
|
1426 | - if ($this->array_result) { |
|
1427 | - $result['matches'][$idx]['attrs'] = $attrvals; |
|
1428 | - } else { |
|
1429 | - $result['matches'][$doc]['attrs'] = $attrvals; |
|
1430 | - } |
|
1431 | - } |
|
1432 | - |
|
1433 | - list($total, $total_found, $msecs, $words) = array_values(unpack('N*N*N*N*', substr($response, $p, 16))); |
|
1434 | - $result['total'] = sprintf('%u', $total); |
|
1435 | - $result['total_found'] = sprintf('%u', $total_found); |
|
1436 | - $result['time'] = sprintf('%.3f', $msecs / 1000); |
|
1437 | - $p += 16; |
|
1438 | - |
|
1439 | - while ($words --> 0 && $p < $max) { |
|
1440 | - list(, $len) = unpack('N*', substr($response, $p, 4)); |
|
1441 | - $p += 4; |
|
1442 | - $word = substr($response, $p, $len); |
|
1443 | - $p += $len; |
|
1444 | - list($docs, $hits) = array_values(unpack('N*N*', substr($response, $p, 8))); |
|
1445 | - $p += 8; |
|
1446 | - $result['words'][$word] = array ( |
|
1447 | - 'docs' => sprintf('%u', $docs), |
|
1448 | - 'hits' => sprintf('%u', $hits) |
|
1449 | - ); |
|
1450 | - } |
|
1451 | - } |
|
1452 | - |
|
1453 | - $this->mbPop(); |
|
1454 | - return $results; |
|
1455 | - } |
|
1456 | - |
|
1457 | - ///////////////////////////////////////////////////////////////////////////// |
|
1458 | - // excerpts generation |
|
1459 | - ///////////////////////////////////////////////////////////////////////////// |
|
1460 | - |
|
1461 | - /// connect to searchd server, and generate exceprts (snippets) |
|
1462 | - /// of given documents for given query. returns false on failure, |
|
1463 | - /// an array of snippets on success |
|
1464 | - public function buildExcerpts($docs, $index, $words, $opts = array()) |
|
1465 | - { |
|
1466 | - assert(is_array($docs)); |
|
1467 | - assert(is_string($index)); |
|
1468 | - assert(is_string($words)); |
|
1469 | - assert(is_array($opts)); |
|
1470 | - |
|
1471 | - $this->mbPush(); |
|
1472 | - |
|
1473 | - if (!($fp = $this->connect())) { |
|
1474 | - $this->mbPop(); |
|
1475 | - return false; |
|
1476 | - } |
|
1477 | - |
|
1478 | - ///////////////// |
|
1479 | - // fixup options |
|
1480 | - ///////////////// |
|
1481 | - |
|
1482 | - if (!isset($opts['before_match'])) { |
|
1483 | - $opts['before_match'] = '<b>'; |
|
1484 | - } |
|
1485 | - if (!isset($opts['after_match'])) { |
|
1486 | - $opts['after_match'] = '</b>'; |
|
1487 | - } |
|
1488 | - if (!isset($opts['chunk_separator'])) { |
|
1489 | - $opts['chunk_separator'] = ' ... '; |
|
1490 | - } |
|
1491 | - if (!isset($opts['limit'])) { |
|
1492 | - $opts['limit'] = 256; |
|
1493 | - } |
|
1494 | - if (!isset($opts['limit_passages'])) { |
|
1495 | - $opts['limit_passages'] = 0; |
|
1496 | - } |
|
1497 | - if (!isset($opts['limit_words'])) { |
|
1498 | - $opts['limit_words'] = 0; |
|
1499 | - } |
|
1500 | - if (!isset($opts['around'])) { |
|
1501 | - $opts['around'] = 5; |
|
1502 | - } |
|
1503 | - if (!isset($opts['exact_phrase'])) { |
|
1504 | - $opts['exact_phrase'] = false; |
|
1505 | - } |
|
1506 | - if (!isset($opts['single_passage'])) { |
|
1507 | - $opts['single_passage'] = false; |
|
1508 | - } |
|
1509 | - if (!isset($opts['use_boundaries'])) { |
|
1510 | - $opts['use_boundaries'] = false; |
|
1511 | - } |
|
1512 | - if (!isset($opts['weight_order'])) { |
|
1513 | - $opts['weight_order'] = false; |
|
1514 | - } |
|
1515 | - if (!isset($opts['query_mode'])) { |
|
1516 | - $opts['query_mode'] = false; |
|
1517 | - } |
|
1518 | - if (!isset($opts['force_all_words'])) { |
|
1519 | - $opts['force_all_words'] = false; |
|
1520 | - } |
|
1521 | - if (!isset($opts['start_passage_id'])) { |
|
1522 | - $opts['start_passage_id'] = 1; |
|
1523 | - } |
|
1524 | - if (!isset($opts['load_files'])) { |
|
1525 | - $opts['load_files'] = false; |
|
1526 | - } |
|
1527 | - if (!isset($opts['html_strip_mode'])) { |
|
1528 | - $opts['html_strip_mode'] = 'index'; |
|
1529 | - } |
|
1530 | - if (!isset($opts['allow_empty'])) { |
|
1531 | - $opts['allow_empty'] = false; |
|
1532 | - } |
|
1533 | - if (!isset($opts['passage_boundary'])) { |
|
1534 | - $opts['passage_boundary'] = 'none'; |
|
1535 | - } |
|
1536 | - if (!isset($opts['emit_zones'])) { |
|
1537 | - $opts['emit_zones'] = false; |
|
1538 | - } |
|
1539 | - if (!isset($opts['load_files_scattered'])) { |
|
1540 | - $opts['load_files_scattered'] = false; |
|
1541 | - } |
|
1542 | - |
|
1543 | - |
|
1544 | - ///////////////// |
|
1545 | - // build request |
|
1546 | - ///////////////// |
|
1547 | - |
|
1548 | - // v.1.2 req |
|
1549 | - $flags = 1; // remove spaces |
|
1550 | - if ($opts['exact_phrase']) { |
|
1551 | - $flags |= 2; |
|
1552 | - } |
|
1553 | - if ($opts['single_passage']) { |
|
1554 | - $flags |= 4; |
|
1555 | - } |
|
1556 | - if ($opts['use_boundaries']) { |
|
1557 | - $flags |= 8; |
|
1558 | - } |
|
1559 | - if ($opts['weight_order']) { |
|
1560 | - $flags |= 16; |
|
1561 | - } |
|
1562 | - if ($opts['query_mode']) { |
|
1563 | - $flags |= 32; |
|
1564 | - } |
|
1565 | - if ($opts['force_all_words']) { |
|
1566 | - $flags |= 64; |
|
1567 | - } |
|
1568 | - if ($opts['load_files']) { |
|
1569 | - $flags |= 128; |
|
1570 | - } |
|
1571 | - if ($opts['allow_empty']) { |
|
1572 | - $flags |= 256; |
|
1573 | - } |
|
1574 | - if ($opts['emit_zones']) { |
|
1575 | - $flags |= 512; |
|
1576 | - } |
|
1577 | - if ($opts['load_files_scattered']) { |
|
1578 | - $flags |= 1024; |
|
1579 | - } |
|
1580 | - $req = pack('NN', 0, $flags); // mode=0, flags=$flags |
|
1581 | - $req .= pack('N', strlen($index)) . $index; // req index |
|
1582 | - $req .= pack('N', strlen($words)) . $words; // req words |
|
1583 | - |
|
1584 | - // options |
|
1585 | - $req .= pack('N', strlen($opts['before_match'])) . $opts['before_match']; |
|
1586 | - $req .= pack('N', strlen($opts['after_match'])) . $opts['after_match']; |
|
1587 | - $req .= pack('N', strlen($opts['chunk_separator'])) . $opts['chunk_separator']; |
|
1588 | - $req .= pack('NN', (int)$opts['limit'], (int)$opts['around']); |
|
1589 | - $req .= pack('NNN', (int)$opts['limit_passages'], (int)$opts['limit_words'], (int)$opts['start_passage_id']); // v.1.2 |
|
1590 | - $req .= pack('N', strlen($opts['html_strip_mode'])) . $opts['html_strip_mode']; |
|
1591 | - $req .= pack('N', strlen($opts['passage_boundary'])) . $opts['passage_boundary']; |
|
1592 | - |
|
1593 | - // documents |
|
1594 | - $req .= pack('N', count($docs)); |
|
1595 | - foreach ($docs as $doc) { |
|
1596 | - assert(is_string($doc)); |
|
1597 | - $req .= pack('N', strlen($doc)) . $doc; |
|
1598 | - } |
|
1599 | - |
|
1600 | - //////////////////////////// |
|
1601 | - // send query, get response |
|
1602 | - //////////////////////////// |
|
1603 | - |
|
1604 | - $len = strlen($req); |
|
1605 | - $req = pack('nnN', SEARCHD_COMMAND_EXCERPT, VER_COMMAND_EXCERPT, $len) . $req; // add header |
|
1606 | - if (!$this->send($fp, $req, $len + 8) || !($response = $this->getResponse($fp, VER_COMMAND_EXCERPT))) { |
|
1607 | - $this->mbPop(); |
|
1608 | - return false; |
|
1609 | - } |
|
1610 | - |
|
1611 | - ////////////////// |
|
1612 | - // parse response |
|
1613 | - ////////////////// |
|
1614 | - |
|
1615 | - $pos = 0; |
|
1616 | - $res = array(); |
|
1617 | - $rlen = strlen($response); |
|
1618 | - $count = count($docs); |
|
1619 | - while ($count--) { |
|
1620 | - list(, $len) = unpack('N*', substr($response, $pos, 4)); |
|
1621 | - $pos += 4; |
|
1622 | - |
|
1623 | - if ($pos + $len > $rlen) { |
|
1624 | - $this->error = 'incomplete reply'; |
|
1625 | - $this->mbPop(); |
|
1626 | - return false; |
|
1627 | - } |
|
1628 | - $res[] = $len ? substr($response, $pos, $len) : ''; |
|
1629 | - $pos += $len; |
|
1630 | - } |
|
1631 | - |
|
1632 | - $this->mbPop(); |
|
1633 | - return $res; |
|
1634 | - } |
|
1635 | - |
|
1636 | - |
|
1637 | - ///////////////////////////////////////////////////////////////////////////// |
|
1638 | - // keyword generation |
|
1639 | - ///////////////////////////////////////////////////////////////////////////// |
|
1640 | - |
|
1641 | - /// connect to searchd server, and generate keyword list for a given query |
|
1642 | - /// returns false on failure, |
|
1643 | - /// an array of words on success |
|
1644 | - public function buildKeywords($query, $index, $hits) |
|
1645 | - { |
|
1646 | - assert(is_string($query)); |
|
1647 | - assert(is_string($index)); |
|
1648 | - assert(is_bool($hits)); |
|
1649 | - |
|
1650 | - $this->mbPush(); |
|
1651 | - |
|
1652 | - if (!($fp = $this->connect())) { |
|
1653 | - $this->mbPop(); |
|
1654 | - return false; |
|
1655 | - } |
|
1656 | - |
|
1657 | - ///////////////// |
|
1658 | - // build request |
|
1659 | - ///////////////// |
|
1660 | - |
|
1661 | - // v.1.0 req |
|
1662 | - $req = pack('N', strlen($query)) . $query; // req query |
|
1663 | - $req .= pack('N', strlen($index)) . $index; // req index |
|
1664 | - $req .= pack('N', (int)$hits); |
|
1665 | - |
|
1666 | - //////////////////////////// |
|
1667 | - // send query, get response |
|
1668 | - //////////////////////////// |
|
1669 | - |
|
1670 | - $len = strlen($req); |
|
1671 | - $req = pack('nnN', SEARCHD_COMMAND_KEYWORDS, VER_COMMAND_KEYWORDS, $len) . $req; // add header |
|
1672 | - if (!$this->send($fp, $req, $len + 8) || !($response = $this->getResponse($fp, VER_COMMAND_KEYWORDS))) { |
|
1673 | - $this->mbPop(); |
|
1674 | - return false; |
|
1675 | - } |
|
1676 | - |
|
1677 | - ////////////////// |
|
1678 | - // parse response |
|
1679 | - ////////////////// |
|
1680 | - |
|
1681 | - $pos = 0; |
|
1682 | - $res = array(); |
|
1683 | - $rlen = strlen($response); |
|
1684 | - list(, $nwords) = unpack('N*', substr($response, $pos, 4)); |
|
1685 | - $pos += 4; |
|
1686 | - for ($i = 0; $i < $nwords; $i++) { |
|
1687 | - list(, $len) = unpack('N*', substr($response, $pos, 4)); |
|
1688 | - $pos += 4; |
|
1689 | - $tokenized = $len ? substr($response, $pos, $len) : ''; |
|
1690 | - $pos += $len; |
|
1691 | - |
|
1692 | - list(, $len) = unpack('N*', substr($response, $pos, 4)); |
|
1693 | - $pos += 4; |
|
1694 | - $normalized = $len ? substr($response, $pos, $len) : ''; |
|
1695 | - $pos += $len; |
|
1696 | - |
|
1697 | - $res[] = array( |
|
1698 | - 'tokenized' => $tokenized, |
|
1699 | - 'normalized' => $normalized |
|
1700 | - ); |
|
1701 | - |
|
1702 | - if ($hits) { |
|
1703 | - list($ndocs, $nhits) = array_values(unpack('N*N*', substr($response, $pos, 8))); |
|
1704 | - $pos += 8; |
|
1705 | - $res[$i]['docs'] = $ndocs; |
|
1706 | - $res[$i]['hits'] = $nhits; |
|
1707 | - } |
|
1708 | - |
|
1709 | - if ($pos > $rlen) { |
|
1710 | - $this->error = 'incomplete reply'; |
|
1711 | - $this->mbPop(); |
|
1712 | - return false; |
|
1713 | - } |
|
1714 | - } |
|
1715 | - |
|
1716 | - $this->mbPop(); |
|
1717 | - return $res; |
|
1718 | - } |
|
1719 | - |
|
1720 | - public function escapeString($string) |
|
1721 | - { |
|
1722 | - $from = array('\\', '(',')','|','-','!','@','~','"','&', '/', '^', '$', '=', '<'); |
|
1723 | - $to = array('\\\\', '\(','\)','\|','\-','\!','\@','\~','\"', '\&', '\/', '\^', '\$', '\=', '\<'); |
|
1724 | - |
|
1725 | - return str_replace($from, $to, $string); |
|
1726 | - } |
|
1727 | - |
|
1728 | - ///////////////////////////////////////////////////////////////////////////// |
|
1729 | - // attribute updates |
|
1730 | - ///////////////////////////////////////////////////////////////////////////// |
|
1731 | - |
|
1732 | - /// batch update given attributes in given rows in given indexes |
|
1733 | - /// returns amount of updated documents (0 or more) on success, or -1 on failure |
|
1734 | - public function updateAttributes($index, $attrs, $values, $mva = false, $ignorenonexistent = false) |
|
1735 | - { |
|
1736 | - // verify everything |
|
1737 | - assert(is_string($index)); |
|
1738 | - assert(is_bool($mva)); |
|
1739 | - assert(is_bool($ignorenonexistent)); |
|
1740 | - |
|
1741 | - assert(is_array($attrs)); |
|
1742 | - foreach ($attrs as $attr) { |
|
1743 | - assert(is_string($attr)); |
|
1744 | - } |
|
1745 | - |
|
1746 | - assert(is_array($values)); |
|
1747 | - foreach ($values as $id => $entry) { |
|
1748 | - assert(is_numeric($id)); |
|
1749 | - assert(is_array($entry)); |
|
1750 | - assert(count($entry) == count($attrs)); |
|
1751 | - foreach ($entry as $v) { |
|
1752 | - if ($mva) { |
|
1753 | - assert(is_array($v)); |
|
1754 | - foreach ($v as $vv) { |
|
1755 | - assert(is_int($vv)); |
|
1756 | - } |
|
1757 | - } else { |
|
1758 | - assert(is_int($v)); |
|
1759 | - } |
|
1760 | - } |
|
1761 | - } |
|
1762 | - |
|
1763 | - // build request |
|
1764 | - $this->mbPush(); |
|
1765 | - $req = pack('N', strlen($index)) . $index; |
|
1766 | - |
|
1767 | - $req .= pack('N', count($attrs)); |
|
1768 | - $req .= pack('N', $ignorenonexistent ? 1 : 0); |
|
1769 | - foreach ($attrs as $attr) { |
|
1770 | - $req .= pack('N', strlen($attr)) . $attr; |
|
1771 | - $req .= pack('N', $mva ? 1 : 0); |
|
1772 | - } |
|
1773 | - |
|
1774 | - $req .= pack('N', count($values)); |
|
1775 | - foreach ($values as $id => $entry) { |
|
1776 | - $req .= sphPackU64($id); |
|
1777 | - foreach ($entry as $v) { |
|
1778 | - $req .= pack('N', $mva ? count($v) : $v); |
|
1779 | - if ($mva) { |
|
1780 | - foreach ($v as $vv) { |
|
1781 | - $req .= pack('N', $vv); |
|
1782 | - } |
|
1783 | - } |
|
1784 | - } |
|
1785 | - } |
|
1786 | - |
|
1787 | - // connect, send query, get response |
|
1788 | - if (!($fp = $this->connect())) { |
|
1789 | - $this->mbPop(); |
|
1790 | - return -1; |
|
1791 | - } |
|
1792 | - |
|
1793 | - $len = strlen($req); |
|
1794 | - $req = pack('nnN', SEARCHD_COMMAND_UPDATE, VER_COMMAND_UPDATE, $len) . $req; // add header |
|
1795 | - if (!$this->send($fp, $req, $len + 8)) { |
|
1796 | - $this->mbPop(); |
|
1797 | - return -1; |
|
1798 | - } |
|
1799 | - |
|
1800 | - if (!($response = $this->getResponse($fp, VER_COMMAND_UPDATE))) { |
|
1801 | - $this->mbPop(); |
|
1802 | - return -1; |
|
1803 | - } |
|
1804 | - |
|
1805 | - // parse response |
|
1806 | - list(, $updated) = unpack('N*', substr($response, 0, 4)); |
|
1807 | - $this->mbPop(); |
|
1808 | - return $updated; |
|
1809 | - } |
|
1810 | - |
|
1811 | - ///////////////////////////////////////////////////////////////////////////// |
|
1812 | - // persistent connections |
|
1813 | - ///////////////////////////////////////////////////////////////////////////// |
|
1814 | - |
|
1815 | - public function open() |
|
1816 | - { |
|
1817 | - if ($this->socket !== false) { |
|
1818 | - $this->error = 'already connected'; |
|
1819 | - return false; |
|
1820 | - } |
|
1821 | - if (!($fp = $this->connect())) |
|
1822 | - return false; |
|
1823 | - |
|
1824 | - // command, command version = 0, body length = 4, body = 1 |
|
1825 | - $req = pack('nnNN', SEARCHD_COMMAND_PERSIST, 0, 4, 1); |
|
1826 | - if (!$this->send($fp, $req, 12)) { |
|
1827 | - return false; |
|
1828 | - } |
|
1829 | - |
|
1830 | - $this->socket = $fp; |
|
1831 | - return true; |
|
1832 | - } |
|
1833 | - |
|
1834 | - public function close() |
|
1835 | - { |
|
1836 | - if ($this->socket === false) { |
|
1837 | - $this->error = 'not connected'; |
|
1838 | - return false; |
|
1839 | - } |
|
1840 | - |
|
1841 | - fclose($this->socket); |
|
1842 | - $this->socket = false; |
|
1843 | - |
|
1844 | - return true; |
|
1845 | - } |
|
1846 | - |
|
1847 | - ////////////////////////////////////////////////////////////////////////// |
|
1848 | - // status |
|
1849 | - ////////////////////////////////////////////////////////////////////////// |
|
1850 | - |
|
1851 | - public function status($session = false) |
|
1852 | - { |
|
1853 | - assert(is_bool($session)); |
|
1854 | - |
|
1855 | - $this->mbPush(); |
|
1856 | - if (!($fp = $this->connect())) { |
|
1857 | - $this->mbPop(); |
|
1858 | - return false; |
|
1859 | - } |
|
1860 | - |
|
1861 | - $req = pack('nnNN', SEARCHD_COMMAND_STATUS, VER_COMMAND_STATUS, 4, $session ? 0 : 1); // len=4, body=1 |
|
1862 | - if (!$this->send($fp, $req, 12) || !($response = $this->getResponse($fp, VER_COMMAND_STATUS))) { |
|
1863 | - $this->mbPop(); |
|
1864 | - return false; |
|
1865 | - } |
|
1866 | - |
|
1867 | - $res = substr($response, 4); // just ignore length, error handling, etc |
|
1868 | - $p = 0; |
|
1869 | - list($rows, $cols) = array_values(unpack('N*N*', substr($response, $p, 8))); |
|
1870 | - $p += 8; |
|
1871 | - |
|
1872 | - $res = array(); |
|
1873 | - for ($i = 0; $i < $rows; $i++) { |
|
1874 | - for ($j = 0; $j < $cols; $j++) { |
|
1875 | - list(, $len) = unpack('N*', substr($response, $p, 4)); |
|
1876 | - $p += 4; |
|
1877 | - $res[$i][] = substr($response, $p, $len); |
|
1878 | - $p += $len; |
|
1879 | - } |
|
1880 | - } |
|
1881 | - |
|
1882 | - $this->mbPop(); |
|
1883 | - return $res; |
|
1884 | - } |
|
1885 | - |
|
1886 | - ////////////////////////////////////////////////////////////////////////// |
|
1887 | - // flush |
|
1888 | - ////////////////////////////////////////////////////////////////////////// |
|
1889 | - |
|
1890 | - public function flushAttributes() |
|
1891 | - { |
|
1892 | - $this->mbPush(); |
|
1893 | - if (!($fp = $this->connect())) { |
|
1894 | - $this->mbPop(); |
|
1895 | - return -1; |
|
1896 | - } |
|
1897 | - |
|
1898 | - $req = pack('nnN', SEARCHD_COMMAND_FLUSHATTRS, VER_COMMAND_FLUSHATTRS, 0); // len=0 |
|
1899 | - if (!$this->send($fp, $req, 8) || !($response = $this->getResponse($fp, VER_COMMAND_FLUSHATTRS))) { |
|
1900 | - $this->mbPop(); |
|
1901 | - return -1; |
|
1902 | - } |
|
1903 | - |
|
1904 | - $tag = -1; |
|
1905 | - if (strlen($response) == 4) { |
|
1906 | - list(, $tag) = unpack('N*', $response); |
|
1907 | - } else { |
|
1908 | - $this->error = 'unexpected response length'; |
|
1909 | - } |
|
1910 | - |
|
1911 | - $this->mbPop(); |
|
1912 | - return $tag; |
|
1913 | - } |
|
417 | + protected $host = 'localhost'; ///< searchd host (default is 'localhost') |
|
418 | + protected $port = 9312; ///< searchd port (default is 9312) |
|
419 | + protected $offset = 0; ///< how many records to seek from result-set start (default is 0) |
|
420 | + protected $limit = 20; ///< how many records to return from result-set starting at offset (default is 20) |
|
421 | + protected $mode = SPH_MATCH_EXTENDED2; ///< query matching mode (default is SPH_MATCH_EXTENDED2) |
|
422 | + protected $weights = array(); ///< per-field weights (default is 1 for all fields) |
|
423 | + protected $sort = SPH_SORT_RELEVANCE; ///< match sorting mode (default is SPH_SORT_RELEVANCE) |
|
424 | + protected $sort_by = ''; ///< attribute to sort by (defualt is '') |
|
425 | + protected $min_id = 0; ///< min ID to match (default is 0, which means no limit) |
|
426 | + protected $max_id = 0; ///< max ID to match (default is 0, which means no limit) |
|
427 | + protected $filters = array(); ///< search filters |
|
428 | + protected $group_by = ''; ///< group-by attribute name |
|
429 | + protected $group_func = SPH_GROUPBY_DAY; ///< group-by function (to pre-process group-by attribute value with) |
|
430 | + protected $group_sort = '@group desc'; ///< group-by sorting clause (to sort groups in result set with) |
|
431 | + protected $group_distinct = ''; ///< group-by count-distinct attribute |
|
432 | + protected $max_matches = 1000; ///< max matches to retrieve |
|
433 | + protected $cutoff = 0; ///< cutoff to stop searching at (default is 0) |
|
434 | + protected $retry_count = 0; ///< distributed retries count |
|
435 | + protected $retry_delay = 0; ///< distributed retries delay |
|
436 | + protected $anchor = array(); ///< geographical anchor point |
|
437 | + protected $index_weights = array(); ///< per-index weights |
|
438 | + protected $ranker = SPH_RANK_PROXIMITY_BM25; ///< ranking mode (default is SPH_RANK_PROXIMITY_BM25) |
|
439 | + protected $rank_expr = ''; ///< ranking mode expression (for SPH_RANK_EXPR) |
|
440 | + protected $max_query_time = 0; ///< max query time, milliseconds (default is 0, do not limit) |
|
441 | + protected $field_weights = array(); ///< per-field-name weights |
|
442 | + protected $overrides = array(); ///< per-query attribute values overrides |
|
443 | + protected $select = '*'; ///< select-list (attributes or expressions, with optional aliases) |
|
444 | + protected $query_flags = 0; ///< per-query various flags |
|
445 | + protected $predicted_time = 0; ///< per-query max_predicted_time |
|
446 | + protected $outer_order_by = ''; ///< outer match sort by |
|
447 | + protected $outer_offset = 0; ///< outer offset |
|
448 | + protected $outer_limit = 0; ///< outer limit |
|
449 | + protected $has_outer = false; |
|
450 | + |
|
451 | + protected $error = ''; ///< last error message |
|
452 | + protected $warning = ''; ///< last warning message |
|
453 | + protected $conn_error = false; ///< connection error vs remote error flag |
|
454 | + |
|
455 | + protected $reqs = array(); ///< requests array for multi-query |
|
456 | + protected $mbenc = ''; ///< stored mbstring encoding |
|
457 | + protected $array_result = false; ///< whether $result['matches'] should be a hash or an array |
|
458 | + protected $timeout = 0; ///< connect timeout |
|
459 | + |
|
460 | + protected $path = false; |
|
461 | + protected $socket = false; |
|
462 | + |
|
463 | + ///////////////////////////////////////////////////////////////////////////// |
|
464 | + // common stuff |
|
465 | + ///////////////////////////////////////////////////////////////////////////// |
|
466 | + |
|
467 | + public function __construct() |
|
468 | + { |
|
469 | + $this->query_flags = sphSetBit(0, 6, true); // default idf=tfidf_normalized |
|
470 | + } |
|
471 | + |
|
472 | + public function __destruct() |
|
473 | + { |
|
474 | + if ($this->socket !== false) { |
|
475 | + fclose($this->socket); |
|
476 | + } |
|
477 | + } |
|
478 | + |
|
479 | + /// get last error message (string) |
|
480 | + public function getLastError() |
|
481 | + { |
|
482 | + return $this->error; |
|
483 | + } |
|
484 | + |
|
485 | + /// get last warning message (string) |
|
486 | + public function getLastWarning() |
|
487 | + { |
|
488 | + return $this->warning; |
|
489 | + } |
|
490 | + |
|
491 | + /// get last error flag (to tell network connection errors from searchd errors or broken responses) |
|
492 | + public function isConnectError() |
|
493 | + { |
|
494 | + return $this->conn_error; |
|
495 | + } |
|
496 | + |
|
497 | + /// set searchd host name (string) and port (integer) |
|
498 | + public function setServer($host, $port = 0) |
|
499 | + { |
|
500 | + assert(is_string($host)); |
|
501 | + if ($host[0] == '/') { |
|
502 | + $this->path = 'unix://' . $host; |
|
503 | + return; |
|
504 | + } |
|
505 | + if (substr($host, 0, 7) == 'unix://') { |
|
506 | + $this->path = $host; |
|
507 | + return; |
|
508 | + } |
|
509 | + |
|
510 | + $this->host = $host; |
|
511 | + $port = intval($port); |
|
512 | + assert(0 <= $port && $port < 65536); |
|
513 | + $this->port = $port == 0 ? 9312 : $port; |
|
514 | + $this->path = ''; |
|
515 | + } |
|
516 | + |
|
517 | + /// set server connection timeout (0 to remove) |
|
518 | + public function setConnectTimeout($timeout) |
|
519 | + { |
|
520 | + assert(is_numeric($timeout)); |
|
521 | + $this->timeout = $timeout; |
|
522 | + } |
|
523 | + |
|
524 | + |
|
525 | + protected function send($handle, $data, $length) |
|
526 | + { |
|
527 | + if (feof($handle) || fwrite($handle, $data, $length) !== $length) { |
|
528 | + $this->error = 'connection unexpectedly closed (timed out?)'; |
|
529 | + $this->conn_error = true; |
|
530 | + return false; |
|
531 | + } |
|
532 | + return true; |
|
533 | + } |
|
534 | + |
|
535 | + ///////////////////////////////////////////////////////////////////////////// |
|
536 | + |
|
537 | + /// enter mbstring workaround mode |
|
538 | + protected function mbPush() |
|
539 | + { |
|
540 | + $this->mbenc = ''; |
|
541 | + if (ini_get('mbstring.func_overload') & 2) { |
|
542 | + $this->mbenc = mb_internal_encoding(); |
|
543 | + mb_internal_encoding('latin1'); |
|
544 | + } |
|
545 | + } |
|
546 | + |
|
547 | + /// leave mbstring workaround mode |
|
548 | + protected function mbPop() |
|
549 | + { |
|
550 | + if ($this->mbenc) { |
|
551 | + mb_internal_encoding($this->mbenc); |
|
552 | + } |
|
553 | + } |
|
554 | + |
|
555 | + /// connect to searchd server |
|
556 | + protected function connect() |
|
557 | + { |
|
558 | + if ($this->socket !== false) { |
|
559 | + // we are in persistent connection mode, so we have a socket |
|
560 | + // however, need to check whether it's still alive |
|
561 | + if (!@feof($this->socket)) { |
|
562 | + return $this->socket; |
|
563 | + } |
|
564 | + |
|
565 | + // force reopen |
|
566 | + $this->socket = false; |
|
567 | + } |
|
568 | + |
|
569 | + $errno = 0; |
|
570 | + $errstr = ''; |
|
571 | + $this->conn_error = false; |
|
572 | + |
|
573 | + if ($this->path) { |
|
574 | + $host = $this->path; |
|
575 | + $port = 0; |
|
576 | + } else { |
|
577 | + $host = $this->host; |
|
578 | + $port = $this->port; |
|
579 | + } |
|
580 | + |
|
581 | + if ($this->timeout <= 0) { |
|
582 | + $fp = @fsockopen($host, $port, $errno, $errstr); |
|
583 | + } else { |
|
584 | + $fp = @fsockopen($host, $port, $errno, $errstr, $this->timeout); |
|
585 | + } |
|
586 | + |
|
587 | + if (!$fp) { |
|
588 | + if ($this->path) { |
|
589 | + $location = $this->path; |
|
590 | + } else { |
|
591 | + $location = "{$this->host}:{$this->port}"; |
|
592 | + } |
|
593 | + |
|
594 | + $errstr = trim($errstr); |
|
595 | + $this->error = "connection to $location failed (errno=$errno, msg=$errstr)"; |
|
596 | + $this->conn_error = true; |
|
597 | + return false; |
|
598 | + } |
|
599 | + |
|
600 | + // send my version |
|
601 | + // this is a subtle part. we must do it before (!) reading back from searchd. |
|
602 | + // because otherwise under some conditions (reported on FreeBSD for instance) |
|
603 | + // TCP stack could throttle write-write-read pattern because of Nagle. |
|
604 | + if (!$this->send($fp, pack('N', 1), 4)) { |
|
605 | + fclose($fp); |
|
606 | + $this->error = 'failed to send client protocol version'; |
|
607 | + return false; |
|
608 | + } |
|
609 | + |
|
610 | + // check version |
|
611 | + list(, $v) = unpack('N*', fread($fp, 4)); |
|
612 | + $v = (int)$v; |
|
613 | + if ($v < 1) { |
|
614 | + fclose($fp); |
|
615 | + $this->error = "expected searchd protocol version 1+, got version '$v'"; |
|
616 | + return false; |
|
617 | + } |
|
618 | + |
|
619 | + return $fp; |
|
620 | + } |
|
621 | + |
|
622 | + /// get and check response packet from searchd server |
|
623 | + protected function getResponse($fp, $client_ver) |
|
624 | + { |
|
625 | + $response = ''; |
|
626 | + $len = 0; |
|
627 | + |
|
628 | + $header = fread($fp, 8); |
|
629 | + if (strlen($header) == 8) { |
|
630 | + list($status, $ver, $len) = array_values(unpack('n2a/Nb', $header)); |
|
631 | + $left = $len; |
|
632 | + while ($left > 0 && !feof($fp)) { |
|
633 | + $chunk = fread($fp, min(8192, $left)); |
|
634 | + if ($chunk) { |
|
635 | + $response .= $chunk; |
|
636 | + $left -= strlen($chunk); |
|
637 | + } |
|
638 | + } |
|
639 | + } |
|
640 | + if ($this->socket === false) { |
|
641 | + fclose($fp); |
|
642 | + } |
|
643 | + |
|
644 | + // check response |
|
645 | + $read = strlen($response); |
|
646 | + if (!$response || $read != $len) { |
|
647 | + $this->error = $len |
|
648 | + ? "failed to read searchd response (status=$status, ver=$ver, len=$len, read=$read)" |
|
649 | + : 'received zero-sized searchd response'; |
|
650 | + return false; |
|
651 | + } |
|
652 | + |
|
653 | + // check status |
|
654 | + if ($status == SEARCHD_WARNING) { |
|
655 | + list(, $wlen) = unpack('N*', substr($response, 0, 4)); |
|
656 | + $this->warning = substr($response, 4, $wlen); |
|
657 | + return substr($response, 4 + $wlen); |
|
658 | + } |
|
659 | + if ($status == SEARCHD_ERROR) { |
|
660 | + $this->error = 'searchd error: ' . substr($response, 4); |
|
661 | + return false; |
|
662 | + } |
|
663 | + if ($status == SEARCHD_RETRY) { |
|
664 | + $this->error = 'temporary searchd error: ' . substr($response, 4); |
|
665 | + return false; |
|
666 | + } |
|
667 | + if ($status != SEARCHD_OK) { |
|
668 | + $this->error = "unknown status code '$status'"; |
|
669 | + return false; |
|
670 | + } |
|
671 | + |
|
672 | + // check version |
|
673 | + if ($ver < $client_ver) { |
|
674 | + $this->warning = sprintf( |
|
675 | + 'searchd command v.%d.%d older than client\'s v.%d.%d, some options might not work', |
|
676 | + $ver >> 8, |
|
677 | + $ver & 0xff, |
|
678 | + $client_ver >> 8, |
|
679 | + $client_ver & 0xff |
|
680 | + ); |
|
681 | + } |
|
682 | + |
|
683 | + return $response; |
|
684 | + } |
|
685 | + |
|
686 | + ///////////////////////////////////////////////////////////////////////////// |
|
687 | + // searching |
|
688 | + ///////////////////////////////////////////////////////////////////////////// |
|
689 | + |
|
690 | + /// set offset and count into result set, |
|
691 | + /// and optionally set max-matches and cutoff limits |
|
692 | + public function setLimits($offset, $limit, $max = 0, $cutoff = 0) |
|
693 | + { |
|
694 | + assert(is_int($offset)); |
|
695 | + assert(is_int($limit)); |
|
696 | + assert($offset >= 0); |
|
697 | + assert($limit > 0); |
|
698 | + assert($max >= 0); |
|
699 | + $this->offset = $offset; |
|
700 | + $this->limit = $limit; |
|
701 | + if ($max > 0) { |
|
702 | + $this->max_matches = $max; |
|
703 | + } |
|
704 | + if ($cutoff > 0) { |
|
705 | + $this->cutoff = $cutoff; |
|
706 | + } |
|
707 | + } |
|
708 | + |
|
709 | + /// set maximum query time, in milliseconds, per-index |
|
710 | + /// integer, 0 means 'do not limit' |
|
711 | + public function setMaxQueryTime($max) |
|
712 | + { |
|
713 | + assert(is_int($max)); |
|
714 | + assert($max >= 0); |
|
715 | + $this->max_query_time = $max; |
|
716 | + } |
|
717 | + |
|
718 | + /// set matching mode |
|
719 | + public function setMatchMode($mode) |
|
720 | + { |
|
721 | + trigger_error( |
|
722 | + 'DEPRECATED: Do not call this method or, even better, use SphinxQL instead of an API', |
|
723 | + E_USER_DEPRECATED |
|
724 | + ); |
|
725 | + assert( |
|
726 | + $mode == SPH_MATCH_ALL || |
|
727 | + $mode == SPH_MATCH_ANY || |
|
728 | + $mode == SPH_MATCH_PHRASE || |
|
729 | + $mode == SPH_MATCH_BOOLEAN || |
|
730 | + $mode == SPH_MATCH_EXTENDED || |
|
731 | + $mode == SPH_MATCH_FULLSCAN || |
|
732 | + $mode == SPH_MATCH_EXTENDED2 |
|
733 | + ); |
|
734 | + $this->mode = $mode; |
|
735 | + } |
|
736 | + |
|
737 | + /// set ranking mode |
|
738 | + public function setRankingMode($ranker, $rankexpr='') |
|
739 | + { |
|
740 | + assert($ranker === 0 || $ranker >= 1 && $ranker < SPH_RANK_TOTAL); |
|
741 | + assert(is_string($rankexpr)); |
|
742 | + $this->ranker = $ranker; |
|
743 | + $this->rank_expr = $rankexpr; |
|
744 | + } |
|
745 | + |
|
746 | + /// set matches sorting mode |
|
747 | + public function setSortMode($mode, $sortby = '') |
|
748 | + { |
|
749 | + assert ( |
|
750 | + $mode == SPH_SORT_RELEVANCE || |
|
751 | + $mode == SPH_SORT_ATTR_DESC || |
|
752 | + $mode == SPH_SORT_ATTR_ASC || |
|
753 | + $mode == SPH_SORT_TIME_SEGMENTS || |
|
754 | + $mode == SPH_SORT_EXTENDED || |
|
755 | + $mode == SPH_SORT_EXPR |
|
756 | + ); |
|
757 | + assert(is_string($sortby)); |
|
758 | + assert($mode == SPH_SORT_RELEVANCE || strlen($sortby) > 0); |
|
759 | + |
|
760 | + $this->sort = $mode; |
|
761 | + $this->sort_by = $sortby; |
|
762 | + } |
|
763 | + |
|
764 | + /** |
|
765 | + * Bind per-field weights by order |
|
766 | + * |
|
767 | + * @deprecated use setFieldWeights() instead |
|
768 | + */ |
|
769 | + public function setWeights() |
|
770 | + { |
|
771 | + throw new \RuntimeException('This method is now deprecated; please use setFieldWeights instead'); |
|
772 | + } |
|
773 | + |
|
774 | + /// bind per-field weights by name |
|
775 | + public function setFieldWeights($weights) |
|
776 | + { |
|
777 | + assert(is_array($weights)); |
|
778 | + foreach ($weights as $name => $weight) { |
|
779 | + assert(is_string($name)); |
|
780 | + assert(is_int($weight)); |
|
781 | + } |
|
782 | + $this->field_weights = $weights; |
|
783 | + } |
|
784 | + |
|
785 | + /// bind per-index weights by name |
|
786 | + public function setIndexWeights($weights) |
|
787 | + { |
|
788 | + assert(is_array($weights)); |
|
789 | + foreach ($weights as $index => $weight) { |
|
790 | + assert(is_string($index)); |
|
791 | + assert(is_int($weight)); |
|
792 | + } |
|
793 | + $this->index_weights = $weights; |
|
794 | + } |
|
795 | + |
|
796 | + /// set IDs range to match |
|
797 | + /// only match records if document ID is beetwen $min and $max (inclusive) |
|
798 | + public function setIDRange($min, $max) |
|
799 | + { |
|
800 | + assert(is_numeric($min)); |
|
801 | + assert(is_numeric($max)); |
|
802 | + assert($min <= $max); |
|
803 | + $this->min_id = $min; |
|
804 | + $this->max_id = $max; |
|
805 | + } |
|
806 | + |
|
807 | + /// set values set filter |
|
808 | + /// only match records where $attribute value is in given set |
|
809 | + public function setFilter($attribute, $values, $exclude = false) |
|
810 | + { |
|
811 | + assert(is_string($attribute)); |
|
812 | + assert(is_array($values)); |
|
813 | + assert(count($values)); |
|
814 | + |
|
815 | + if (is_array($values) && count($values)) { |
|
816 | + foreach ($values as $value) { |
|
817 | + assert(is_numeric($value)); |
|
818 | + } |
|
819 | + |
|
820 | + $this->filters[] = array( |
|
821 | + 'type' => SPH_FILTER_VALUES, |
|
822 | + 'attr' => $attribute, |
|
823 | + 'exclude' => $exclude, |
|
824 | + 'values' => $values |
|
825 | + ); |
|
826 | + } |
|
827 | + } |
|
828 | + |
|
829 | + /// set string filter |
|
830 | + /// only match records where $attribute value is equal |
|
831 | + public function setFilterString($attribute, $value, $exclude = false) |
|
832 | + { |
|
833 | + assert(is_string($attribute)); |
|
834 | + assert(is_string($value)); |
|
835 | + $this->filters[] = array( |
|
836 | + 'type' => SPH_FILTER_STRING, |
|
837 | + 'attr' => $attribute, |
|
838 | + 'exclude' => $exclude, |
|
839 | + 'value' => $value |
|
840 | + ); |
|
841 | + } |
|
842 | + |
|
843 | + /// set range filter |
|
844 | + /// only match records if $attribute value is beetwen $min and $max (inclusive) |
|
845 | + public function setFilterRange($attribute, $min, $max, $exclude = false) |
|
846 | + { |
|
847 | + assert(is_string($attribute)); |
|
848 | + assert(is_numeric($min)); |
|
849 | + assert(is_numeric($max)); |
|
850 | + assert($min <= $max); |
|
851 | + |
|
852 | + $this->filters[] = array( |
|
853 | + 'type' => SPH_FILTER_RANGE, |
|
854 | + 'attr' => $attribute, |
|
855 | + 'exclude' => $exclude, |
|
856 | + 'min' => $min, |
|
857 | + 'max' => $max |
|
858 | + ); |
|
859 | + } |
|
860 | + |
|
861 | + /// set float range filter |
|
862 | + /// only match records if $attribute value is beetwen $min and $max (inclusive) |
|
863 | + public function setFilterFloatRange($attribute, $min, $max, $exclude = false) |
|
864 | + { |
|
865 | + assert(is_string($attribute)); |
|
866 | + assert(is_float($min)); |
|
867 | + assert(is_float($max)); |
|
868 | + assert($min <= $max); |
|
869 | + |
|
870 | + $this->filters[] = array( |
|
871 | + 'type' => SPH_FILTER_FLOATRANGE, |
|
872 | + 'attr' => $attribute, |
|
873 | + 'exclude' => $exclude, |
|
874 | + 'min' => $min, |
|
875 | + 'max' => $max |
|
876 | + ); |
|
877 | + } |
|
878 | + |
|
879 | + /// setup anchor point for geosphere distance calculations |
|
880 | + /// required to use @geodist in filters and sorting |
|
881 | + /// latitude and longitude must be in radians |
|
882 | + public function setGeoAnchor($attrlat, $attrlong, $lat, $long) |
|
883 | + { |
|
884 | + assert(is_string($attrlat)); |
|
885 | + assert(is_string($attrlong)); |
|
886 | + assert(is_float($lat)); |
|
887 | + assert(is_float($long)); |
|
888 | + |
|
889 | + $this->anchor = array( |
|
890 | + 'attrlat' => $attrlat, |
|
891 | + 'attrlong' => $attrlong, |
|
892 | + 'lat' => $lat, |
|
893 | + 'long' => $long |
|
894 | + ); |
|
895 | + } |
|
896 | + |
|
897 | + /// set grouping attribute and function |
|
898 | + public function setGroupBy($attribute, $func, $groupsort = '@group desc') |
|
899 | + { |
|
900 | + assert(is_string($attribute)); |
|
901 | + assert(is_string($groupsort)); |
|
902 | + assert( |
|
903 | + $func == SPH_GROUPBY_DAY || |
|
904 | + $func == SPH_GROUPBY_WEEK || |
|
905 | + $func == SPH_GROUPBY_MONTH || |
|
906 | + $func == SPH_GROUPBY_YEAR || |
|
907 | + $func == SPH_GROUPBY_ATTR || |
|
908 | + $func == SPH_GROUPBY_ATTRPAIR |
|
909 | + ); |
|
910 | + |
|
911 | + $this->group_by = $attribute; |
|
912 | + $this->group_func = $func; |
|
913 | + $this->group_sort = $groupsort; |
|
914 | + } |
|
915 | + |
|
916 | + /// set count-distinct attribute for group-by queries |
|
917 | + public function setGroupDistinct($attribute) |
|
918 | + { |
|
919 | + assert(is_string($attribute)); |
|
920 | + $this->group_distinct = $attribute; |
|
921 | + } |
|
922 | + |
|
923 | + /// set distributed retries count and delay |
|
924 | + public function setRetries($count, $delay = 0) |
|
925 | + { |
|
926 | + assert(is_int($count) && $count >= 0); |
|
927 | + assert(is_int($delay) && $delay >= 0); |
|
928 | + $this->retry_count = $count; |
|
929 | + $this->retry_delay = $delay; |
|
930 | + } |
|
931 | + |
|
932 | + /// set result set format (hash or array; hash by default) |
|
933 | + /// PHP specific; needed for group-by-MVA result sets that may contain duplicate IDs |
|
934 | + public function setArrayResult($arrayresult) |
|
935 | + { |
|
936 | + assert(is_bool($arrayresult)); |
|
937 | + $this->array_result = $arrayresult; |
|
938 | + } |
|
939 | + |
|
940 | + /// set attribute values override |
|
941 | + /// there can be only one override per attribute |
|
942 | + /// $values must be a hash that maps document IDs to attribute values |
|
943 | + public function setOverride($attrname, $attrtype, $values) |
|
944 | + { |
|
945 | + trigger_error( |
|
946 | + 'DEPRECATED: Do not call this method. Use SphinxQL REMAP() function instead.', |
|
947 | + E_USER_DEPRECATED |
|
948 | + ); |
|
949 | + assert(is_string($attrname)); |
|
950 | + assert(in_array($attrtype, array( |
|
951 | + SPH_ATTR_INTEGER, |
|
952 | + SPH_ATTR_TIMESTAMP, |
|
953 | + SPH_ATTR_BOOL, |
|
954 | + SPH_ATTR_FLOAT, |
|
955 | + SPH_ATTR_BIGINT |
|
956 | + ))); |
|
957 | + assert(is_array($values)); |
|
958 | + |
|
959 | + $this->overrides[$attrname] = array( |
|
960 | + 'attr' => $attrname, |
|
961 | + 'type' => $attrtype, |
|
962 | + 'values' => $values |
|
963 | + ); |
|
964 | + } |
|
965 | + |
|
966 | + /// set select-list (attributes or expressions), SQL-like syntax |
|
967 | + public function setSelect($select) |
|
968 | + { |
|
969 | + assert(is_string($select)); |
|
970 | + $this->select = $select; |
|
971 | + } |
|
972 | + |
|
973 | + public function setQueryFlag($flag_name, $flag_value) |
|
974 | + { |
|
975 | + $known_names = array( |
|
976 | + 'reverse_scan', |
|
977 | + 'sort_method', |
|
978 | + 'max_predicted_time', |
|
979 | + 'boolean_simplify', |
|
980 | + 'idf', |
|
981 | + 'global_idf', |
|
982 | + 'low_priority' |
|
983 | + ); |
|
984 | + $flags = array ( |
|
985 | + 'reverse_scan' => array(0, 1), |
|
986 | + 'sort_method' => array('pq', 'kbuffer'), |
|
987 | + 'max_predicted_time' => array(0), |
|
988 | + 'boolean_simplify' => array(true, false), |
|
989 | + 'idf' => array ('normalized', 'plain', 'tfidf_normalized', 'tfidf_unnormalized'), |
|
990 | + 'global_idf' => array(true, false), |
|
991 | + 'low_priority' => array(true, false) |
|
992 | + ); |
|
993 | + |
|
994 | + assert(isset($flag_name, $known_names)); |
|
995 | + assert( |
|
996 | + in_array($flag_value, $flags[$flag_name], true) || |
|
997 | + ($flag_name == 'max_predicted_time' && is_int($flag_value) && $flag_value >= 0) |
|
998 | + ); |
|
999 | + |
|
1000 | + switch ($flag_name) { |
|
1001 | + case 'reverse_scan': |
|
1002 | + $this->query_flags = sphSetBit($this->query_flags, 0, $flag_value == 1); |
|
1003 | + break; |
|
1004 | + case 'sort_method': |
|
1005 | + $this->query_flags = sphSetBit($this->query_flags, 1, $flag_value == 'kbuffer'); |
|
1006 | + break; |
|
1007 | + case 'max_predicted_time': |
|
1008 | + $this->query_flags = sphSetBit($this->query_flags, 2, $flag_value > 0); |
|
1009 | + $this->predicted_time = (int)$flag_value; |
|
1010 | + break; |
|
1011 | + case 'boolean_simplify': |
|
1012 | + $this->query_flags = sphSetBit($this->query_flags, 3, $flag_value); |
|
1013 | + break; |
|
1014 | + case 'idf': |
|
1015 | + if ($flag_value == 'normalized' || $flag_value == 'plain') { |
|
1016 | + $this->query_flags = sphSetBit($this->query_flags, 4, $flag_value == 'plain'); |
|
1017 | + } |
|
1018 | + if ($flag_value == 'tfidf_normalized' || $flag_value == 'tfidf_unnormalized') { |
|
1019 | + $this->query_flags = sphSetBit($this->query_flags, 6, $flag_value == 'tfidf_normalized'); |
|
1020 | + } |
|
1021 | + break; |
|
1022 | + case 'global_idf': |
|
1023 | + $this->query_flags = sphSetBit($this->query_flags, 5, $flag_value); |
|
1024 | + break; |
|
1025 | + case 'low_priority': |
|
1026 | + $this->query_flags = sphSetBit($this->query_flags, 8, $flag_value); |
|
1027 | + break; |
|
1028 | + } |
|
1029 | + } |
|
1030 | + |
|
1031 | + /// set outer order by parameters |
|
1032 | + public function setOuterSelect($orderby, $offset, $limit) |
|
1033 | + { |
|
1034 | + assert(is_string($orderby)); |
|
1035 | + assert(is_int($offset)); |
|
1036 | + assert(is_int($limit)); |
|
1037 | + assert($offset >= 0); |
|
1038 | + assert($limit > 0); |
|
1039 | + |
|
1040 | + $this->outer_order_by = $orderby; |
|
1041 | + $this->outer_offset = $offset; |
|
1042 | + $this->outer_limit = $limit; |
|
1043 | + $this->has_outer = true; |
|
1044 | + } |
|
1045 | + |
|
1046 | + |
|
1047 | + ////////////////////////////////////////////////////////////////////////////// |
|
1048 | + |
|
1049 | + /// clear all filters (for multi-queries) |
|
1050 | + public function resetFilters() |
|
1051 | + { |
|
1052 | + $this->filters = array(); |
|
1053 | + $this->anchor = array(); |
|
1054 | + } |
|
1055 | + |
|
1056 | + /// clear groupby settings (for multi-queries) |
|
1057 | + public function resetGroupBy() |
|
1058 | + { |
|
1059 | + $this->group_by = ''; |
|
1060 | + $this->group_func = SPH_GROUPBY_DAY; |
|
1061 | + $this->group_sort = '@group desc'; |
|
1062 | + $this->group_distinct = ''; |
|
1063 | + } |
|
1064 | + |
|
1065 | + /// clear all attribute value overrides (for multi-queries) |
|
1066 | + public function resetOverrides() |
|
1067 | + { |
|
1068 | + $this->overrides = array(); |
|
1069 | + } |
|
1070 | + |
|
1071 | + public function resetQueryFlag() |
|
1072 | + { |
|
1073 | + $this->query_flags = sphSetBit(0, 6, true); // default idf=tfidf_normalized |
|
1074 | + $this->predicted_time = 0; |
|
1075 | + } |
|
1076 | + |
|
1077 | + public function resetOuterSelect() |
|
1078 | + { |
|
1079 | + $this->outer_order_by = ''; |
|
1080 | + $this->outer_offset = 0; |
|
1081 | + $this->outer_limit = 0; |
|
1082 | + $this->has_outer = false; |
|
1083 | + } |
|
1084 | + |
|
1085 | + ////////////////////////////////////////////////////////////////////////////// |
|
1086 | + |
|
1087 | + /// connect to searchd server, run given search query through given indexes, |
|
1088 | + /// and return the search results |
|
1089 | + public function query($query, $index = '*', $comment = '') |
|
1090 | + { |
|
1091 | + assert(empty($this->reqs)); |
|
1092 | + |
|
1093 | + $this->addQuery($query, $index, $comment); |
|
1094 | + $results = $this->runQueries(); |
|
1095 | + $this->reqs = array(); // just in case it failed too early |
|
1096 | + |
|
1097 | + if (!is_array($results)) { |
|
1098 | + return false; // probably network error; error message should be already filled |
|
1099 | + } |
|
1100 | + |
|
1101 | + $this->error = $results[0]['error']; |
|
1102 | + $this->warning = $results[0]['warning']; |
|
1103 | + if ($results[0]['status'] == SEARCHD_ERROR) { |
|
1104 | + return false; |
|
1105 | + } else { |
|
1106 | + return $results[0]; |
|
1107 | + } |
|
1108 | + } |
|
1109 | + |
|
1110 | + /// helper to pack floats in network byte order |
|
1111 | + protected function packFloat($f) |
|
1112 | + { |
|
1113 | + $t1 = pack('f', $f); // machine order |
|
1114 | + list(, $t2) = unpack('L*', $t1); // int in machine order |
|
1115 | + return pack('N', $t2); |
|
1116 | + } |
|
1117 | + |
|
1118 | + /// add query to multi-query batch |
|
1119 | + /// returns index into results array from RunQueries() call |
|
1120 | + public function addQuery($query, $index = '*', $comment = '') |
|
1121 | + { |
|
1122 | + // mbstring workaround |
|
1123 | + $this->mbPush(); |
|
1124 | + |
|
1125 | + // build request |
|
1126 | + $req = pack('NNNNN', $this->query_flags, $this->offset, $this->limit, $this->mode, $this->ranker); |
|
1127 | + if ($this->ranker == SPH_RANK_EXPR) { |
|
1128 | + $req .= pack('N', strlen($this->rank_expr)) . $this->rank_expr; |
|
1129 | + } |
|
1130 | + $req .= pack('N', $this->sort); // (deprecated) sort mode |
|
1131 | + $req .= pack('N', strlen($this->sort_by)) . $this->sort_by; |
|
1132 | + $req .= pack('N', strlen($query)) . $query; // query itself |
|
1133 | + $req .= pack('N', count($this->weights)); // weights |
|
1134 | + foreach ($this->weights as $weight) { |
|
1135 | + $req .= pack('N', (int)$weight); |
|
1136 | + } |
|
1137 | + $req .= pack('N', strlen($index)) . $index; // indexes |
|
1138 | + $req .= pack('N', 1); // id64 range marker |
|
1139 | + $req .= sphPackU64($this->min_id) . sphPackU64($this->max_id); // id64 range |
|
1140 | + |
|
1141 | + // filters |
|
1142 | + $req .= pack('N', count($this->filters)); |
|
1143 | + foreach ($this->filters as $filter) { |
|
1144 | + $req .= pack('N', strlen($filter['attr'])) . $filter['attr']; |
|
1145 | + $req .= pack('N', $filter['type']); |
|
1146 | + switch ($filter['type']) { |
|
1147 | + case SPH_FILTER_VALUES: |
|
1148 | + $req .= pack('N', count($filter['values'])); |
|
1149 | + foreach ($filter['values'] as $value) { |
|
1150 | + $req .= sphPackI64($value); |
|
1151 | + } |
|
1152 | + break; |
|
1153 | + case SPH_FILTER_RANGE: |
|
1154 | + $req .= sphPackI64($filter['min']) . sphPackI64($filter['max']); |
|
1155 | + break; |
|
1156 | + case SPH_FILTER_FLOATRANGE: |
|
1157 | + $req .= $this->packFloat($filter['min']) . $this->packFloat($filter['max']); |
|
1158 | + break; |
|
1159 | + case SPH_FILTER_STRING: |
|
1160 | + $req .= pack('N', strlen($filter['value'])) . $filter['value']; |
|
1161 | + break; |
|
1162 | + default: |
|
1163 | + assert(0 && 'internal error: unhandled filter type'); |
|
1164 | + } |
|
1165 | + $req .= pack('N', $filter['exclude']); |
|
1166 | + } |
|
1167 | + |
|
1168 | + // group-by clause, max-matches count, group-sort clause, cutoff count |
|
1169 | + $req .= pack('NN', $this->group_func, strlen($this->group_by)) . $this->group_by; |
|
1170 | + $req .= pack('N', $this->max_matches); |
|
1171 | + $req .= pack('N', strlen($this->group_sort)) . $this->group_sort; |
|
1172 | + $req .= pack('NNN', $this->cutoff, $this->retry_count, $this->retry_delay); |
|
1173 | + $req .= pack('N', strlen($this->group_distinct)) . $this->group_distinct; |
|
1174 | + |
|
1175 | + // anchor point |
|
1176 | + if (empty($this->anchor)) { |
|
1177 | + $req .= pack('N', 0); |
|
1178 | + } else { |
|
1179 | + $a =& $this->anchor; |
|
1180 | + $req .= pack('N', 1); |
|
1181 | + $req .= pack('N', strlen($a['attrlat'])) . $a['attrlat']; |
|
1182 | + $req .= pack('N', strlen($a['attrlong'])) . $a['attrlong']; |
|
1183 | + $req .= $this->packFloat($a['lat']) . $this->packFloat($a['long']); |
|
1184 | + } |
|
1185 | + |
|
1186 | + // per-index weights |
|
1187 | + $req .= pack('N', count($this->index_weights)); |
|
1188 | + foreach ($this->index_weights as $idx => $weight) { |
|
1189 | + $req .= pack('N', strlen($idx)) . $idx . pack('N', $weight); |
|
1190 | + } |
|
1191 | + |
|
1192 | + // max query time |
|
1193 | + $req .= pack('N', $this->max_query_time); |
|
1194 | + |
|
1195 | + // per-field weights |
|
1196 | + $req .= pack('N', count($this->field_weights)); |
|
1197 | + foreach ($this->field_weights as $field => $weight) { |
|
1198 | + $req .= pack('N', strlen($field)) . $field . pack('N', $weight); |
|
1199 | + } |
|
1200 | + |
|
1201 | + // comment |
|
1202 | + $req .= pack('N', strlen($comment)) . $comment; |
|
1203 | + |
|
1204 | + // attribute overrides |
|
1205 | + $req .= pack('N', count($this->overrides)); |
|
1206 | + foreach ($this->overrides as $key => $entry) { |
|
1207 | + $req .= pack('N', strlen($entry['attr'])) . $entry['attr']; |
|
1208 | + $req .= pack('NN', $entry['type'], count($entry['values'])); |
|
1209 | + foreach ($entry['values'] as $id => $val) { |
|
1210 | + assert(is_numeric($id)); |
|
1211 | + assert(is_numeric($val)); |
|
1212 | + |
|
1213 | + $req .= sphPackU64($id); |
|
1214 | + switch ($entry['type']) { |
|
1215 | + case SPH_ATTR_FLOAT: |
|
1216 | + $req .= $this->packFloat($val); |
|
1217 | + break; |
|
1218 | + case SPH_ATTR_BIGINT: |
|
1219 | + $req .= sphPackI64($val); |
|
1220 | + break; |
|
1221 | + default: |
|
1222 | + $req .= pack('N', $val); |
|
1223 | + break; |
|
1224 | + } |
|
1225 | + } |
|
1226 | + } |
|
1227 | + |
|
1228 | + // select-list |
|
1229 | + $req .= pack('N', strlen($this->select)) . $this->select; |
|
1230 | + |
|
1231 | + // max_predicted_time |
|
1232 | + if ($this->predicted_time > 0) { |
|
1233 | + $req .= pack('N', (int)$this->predicted_time); |
|
1234 | + } |
|
1235 | + |
|
1236 | + $req .= pack('N', strlen($this->outer_order_by)) . $this->outer_order_by; |
|
1237 | + $req .= pack('NN', $this->outer_offset, $this->outer_limit); |
|
1238 | + if ($this->has_outer) { |
|
1239 | + $req .= pack('N', 1); |
|
1240 | + } else { |
|
1241 | + $req .= pack('N', 0); |
|
1242 | + } |
|
1243 | + |
|
1244 | + // mbstring workaround |
|
1245 | + $this->mbPop(); |
|
1246 | + |
|
1247 | + // store request to requests array |
|
1248 | + $this->reqs[] = $req; |
|
1249 | + return count($this->reqs) - 1; |
|
1250 | + } |
|
1251 | + |
|
1252 | + /// connect to searchd, run queries batch, and return an array of result sets |
|
1253 | + public function runQueries() |
|
1254 | + { |
|
1255 | + if (empty($this->reqs)) { |
|
1256 | + $this->error = 'no queries defined, issue AddQuery() first'; |
|
1257 | + return false; |
|
1258 | + } |
|
1259 | + |
|
1260 | + // mbstring workaround |
|
1261 | + $this->mbPush(); |
|
1262 | + |
|
1263 | + if (!($fp = $this->connect())) { |
|
1264 | + $this->mbPop(); |
|
1265 | + return false; |
|
1266 | + } |
|
1267 | + |
|
1268 | + // send query, get response |
|
1269 | + $nreqs = count($this->reqs); |
|
1270 | + $req = join('', $this->reqs); |
|
1271 | + $len = 8 + strlen($req); |
|
1272 | + $req = pack('nnNNN', SEARCHD_COMMAND_SEARCH, VER_COMMAND_SEARCH, $len, 0, $nreqs) . $req; // add header |
|
1273 | + |
|
1274 | + if (!$this->send($fp, $req, $len + 8) || !($response = $this->getResponse($fp, VER_COMMAND_SEARCH))) { |
|
1275 | + $this->mbPop(); |
|
1276 | + return false; |
|
1277 | + } |
|
1278 | + |
|
1279 | + // query sent ok; we can reset reqs now |
|
1280 | + $this->reqs = array(); |
|
1281 | + |
|
1282 | + // parse and return response |
|
1283 | + return $this->parseSearchResponse($response, $nreqs); |
|
1284 | + } |
|
1285 | + |
|
1286 | + /// parse and return search query (or queries) response |
|
1287 | + protected function parseSearchResponse($response, $nreqs) |
|
1288 | + { |
|
1289 | + $p = 0; // current position |
|
1290 | + $max = strlen($response); // max position for checks, to protect against broken responses |
|
1291 | + |
|
1292 | + $results = array(); |
|
1293 | + for ($ires = 0; $ires < $nreqs && $p < $max; $ires++) { |
|
1294 | + $results[] = array(); |
|
1295 | + $result =& $results[$ires]; |
|
1296 | + |
|
1297 | + $result['error'] = ''; |
|
1298 | + $result['warning'] = ''; |
|
1299 | + |
|
1300 | + // extract status |
|
1301 | + list(, $status) = unpack('N*', substr($response, $p, 4)); |
|
1302 | + $p += 4; |
|
1303 | + $result['status'] = $status; |
|
1304 | + if ($status != SEARCHD_OK) { |
|
1305 | + list(, $len) = unpack('N*', substr($response, $p, 4)); |
|
1306 | + $p += 4; |
|
1307 | + $message = substr($response, $p, $len); |
|
1308 | + $p += $len; |
|
1309 | + |
|
1310 | + if ($status == SEARCHD_WARNING) { |
|
1311 | + $result['warning'] = $message; |
|
1312 | + } else { |
|
1313 | + $result['error'] = $message; |
|
1314 | + continue; |
|
1315 | + } |
|
1316 | + } |
|
1317 | + |
|
1318 | + // read schema |
|
1319 | + $fields = array(); |
|
1320 | + $attrs = array(); |
|
1321 | + |
|
1322 | + list(, $nfields) = unpack('N*', substr($response, $p, 4)); |
|
1323 | + $p += 4; |
|
1324 | + while ($nfields --> 0 && $p < $max) { |
|
1325 | + list(, $len) = unpack('N*', substr($response, $p, 4)); |
|
1326 | + $p += 4; |
|
1327 | + $fields[] = substr($response, $p, $len); |
|
1328 | + $p += $len; |
|
1329 | + } |
|
1330 | + $result['fields'] = $fields; |
|
1331 | + |
|
1332 | + list(, $nattrs) = unpack('N*', substr($response, $p, 4)); |
|
1333 | + $p += 4; |
|
1334 | + while ($nattrs --> 0 && $p < $max) { |
|
1335 | + list(, $len) = unpack('N*', substr($response, $p, 4)); |
|
1336 | + $p += 4; |
|
1337 | + $attr = substr($response, $p, $len); |
|
1338 | + $p += $len; |
|
1339 | + list(, $type) = unpack('N*', substr($response, $p, 4)); |
|
1340 | + $p += 4; |
|
1341 | + $attrs[$attr] = $type; |
|
1342 | + } |
|
1343 | + $result['attrs'] = $attrs; |
|
1344 | + |
|
1345 | + // read match count |
|
1346 | + list(, $count) = unpack('N*', substr($response, $p, 4)); |
|
1347 | + $p += 4; |
|
1348 | + list(, $id64) = unpack('N*', substr($response, $p, 4)); |
|
1349 | + $p += 4; |
|
1350 | + |
|
1351 | + // read matches |
|
1352 | + $idx = -1; |
|
1353 | + while ($count --> 0 && $p < $max) { |
|
1354 | + // index into result array |
|
1355 | + $idx++; |
|
1356 | + |
|
1357 | + // parse document id and weight |
|
1358 | + if ($id64) { |
|
1359 | + $doc = sphUnpackU64(substr($response, $p, 8)); |
|
1360 | + $p += 8; |
|
1361 | + list(,$weight) = unpack('N*', substr($response, $p, 4)); |
|
1362 | + $p += 4; |
|
1363 | + } else { |
|
1364 | + list($doc, $weight) = array_values(unpack('N*N*', substr($response, $p, 8))); |
|
1365 | + $p += 8; |
|
1366 | + $doc = sphFixUint($doc); |
|
1367 | + } |
|
1368 | + $weight = sprintf('%u', $weight); |
|
1369 | + |
|
1370 | + // create match entry |
|
1371 | + if ($this->array_result) { |
|
1372 | + $result['matches'][$idx] = array('id' => $doc, 'weight' => $weight); |
|
1373 | + } else { |
|
1374 | + $result['matches'][$doc]['weight'] = $weight; |
|
1375 | + } |
|
1376 | + |
|
1377 | + // parse and create attributes |
|
1378 | + $attrvals = array(); |
|
1379 | + foreach ($attrs as $attr => $type) { |
|
1380 | + // handle 64bit ints |
|
1381 | + if ($type == SPH_ATTR_BIGINT) { |
|
1382 | + $attrvals[$attr] = sphUnpackI64(substr($response, $p, 8)); |
|
1383 | + $p += 8; |
|
1384 | + continue; |
|
1385 | + } |
|
1386 | + |
|
1387 | + // handle floats |
|
1388 | + if ($type == SPH_ATTR_FLOAT) { |
|
1389 | + list(, $uval) = unpack('N*', substr($response, $p, 4)); |
|
1390 | + $p += 4; |
|
1391 | + list(, $fval) = unpack('f*', pack('L', $uval)); |
|
1392 | + $attrvals[$attr] = $fval; |
|
1393 | + continue; |
|
1394 | + } |
|
1395 | + |
|
1396 | + // handle everything else as unsigned ints |
|
1397 | + list(, $val) = unpack('N*', substr($response, $p, 4)); |
|
1398 | + $p += 4; |
|
1399 | + if ($type == SPH_ATTR_MULTI) { |
|
1400 | + $attrvals[$attr] = array(); |
|
1401 | + $nvalues = $val; |
|
1402 | + while ($nvalues --> 0 && $p < $max) { |
|
1403 | + list(, $val) = unpack('N*', substr($response, $p, 4)); |
|
1404 | + $p += 4; |
|
1405 | + $attrvals[$attr][] = sphFixUint($val); |
|
1406 | + } |
|
1407 | + } elseif ($type == SPH_ATTR_MULTI64) { |
|
1408 | + $attrvals[$attr] = array(); |
|
1409 | + $nvalues = $val; |
|
1410 | + while ($nvalues > 0 && $p < $max) { |
|
1411 | + $attrvals[$attr][] = sphUnpackI64(substr($response, $p, 8)); |
|
1412 | + $p += 8; |
|
1413 | + $nvalues -= 2; |
|
1414 | + } |
|
1415 | + } elseif ($type == SPH_ATTR_STRING) { |
|
1416 | + $attrvals[$attr] = substr($response, $p, $val); |
|
1417 | + $p += $val; |
|
1418 | + } elseif ($type == SPH_ATTR_FACTORS) { |
|
1419 | + $attrvals[$attr] = substr($response, $p, $val - 4); |
|
1420 | + $p += $val-4; |
|
1421 | + } else { |
|
1422 | + $attrvals[$attr] = sphFixUint($val); |
|
1423 | + } |
|
1424 | + } |
|
1425 | + |
|
1426 | + if ($this->array_result) { |
|
1427 | + $result['matches'][$idx]['attrs'] = $attrvals; |
|
1428 | + } else { |
|
1429 | + $result['matches'][$doc]['attrs'] = $attrvals; |
|
1430 | + } |
|
1431 | + } |
|
1432 | + |
|
1433 | + list($total, $total_found, $msecs, $words) = array_values(unpack('N*N*N*N*', substr($response, $p, 16))); |
|
1434 | + $result['total'] = sprintf('%u', $total); |
|
1435 | + $result['total_found'] = sprintf('%u', $total_found); |
|
1436 | + $result['time'] = sprintf('%.3f', $msecs / 1000); |
|
1437 | + $p += 16; |
|
1438 | + |
|
1439 | + while ($words --> 0 && $p < $max) { |
|
1440 | + list(, $len) = unpack('N*', substr($response, $p, 4)); |
|
1441 | + $p += 4; |
|
1442 | + $word = substr($response, $p, $len); |
|
1443 | + $p += $len; |
|
1444 | + list($docs, $hits) = array_values(unpack('N*N*', substr($response, $p, 8))); |
|
1445 | + $p += 8; |
|
1446 | + $result['words'][$word] = array ( |
|
1447 | + 'docs' => sprintf('%u', $docs), |
|
1448 | + 'hits' => sprintf('%u', $hits) |
|
1449 | + ); |
|
1450 | + } |
|
1451 | + } |
|
1452 | + |
|
1453 | + $this->mbPop(); |
|
1454 | + return $results; |
|
1455 | + } |
|
1456 | + |
|
1457 | + ///////////////////////////////////////////////////////////////////////////// |
|
1458 | + // excerpts generation |
|
1459 | + ///////////////////////////////////////////////////////////////////////////// |
|
1460 | + |
|
1461 | + /// connect to searchd server, and generate exceprts (snippets) |
|
1462 | + /// of given documents for given query. returns false on failure, |
|
1463 | + /// an array of snippets on success |
|
1464 | + public function buildExcerpts($docs, $index, $words, $opts = array()) |
|
1465 | + { |
|
1466 | + assert(is_array($docs)); |
|
1467 | + assert(is_string($index)); |
|
1468 | + assert(is_string($words)); |
|
1469 | + assert(is_array($opts)); |
|
1470 | + |
|
1471 | + $this->mbPush(); |
|
1472 | + |
|
1473 | + if (!($fp = $this->connect())) { |
|
1474 | + $this->mbPop(); |
|
1475 | + return false; |
|
1476 | + } |
|
1477 | + |
|
1478 | + ///////////////// |
|
1479 | + // fixup options |
|
1480 | + ///////////////// |
|
1481 | + |
|
1482 | + if (!isset($opts['before_match'])) { |
|
1483 | + $opts['before_match'] = '<b>'; |
|
1484 | + } |
|
1485 | + if (!isset($opts['after_match'])) { |
|
1486 | + $opts['after_match'] = '</b>'; |
|
1487 | + } |
|
1488 | + if (!isset($opts['chunk_separator'])) { |
|
1489 | + $opts['chunk_separator'] = ' ... '; |
|
1490 | + } |
|
1491 | + if (!isset($opts['limit'])) { |
|
1492 | + $opts['limit'] = 256; |
|
1493 | + } |
|
1494 | + if (!isset($opts['limit_passages'])) { |
|
1495 | + $opts['limit_passages'] = 0; |
|
1496 | + } |
|
1497 | + if (!isset($opts['limit_words'])) { |
|
1498 | + $opts['limit_words'] = 0; |
|
1499 | + } |
|
1500 | + if (!isset($opts['around'])) { |
|
1501 | + $opts['around'] = 5; |
|
1502 | + } |
|
1503 | + if (!isset($opts['exact_phrase'])) { |
|
1504 | + $opts['exact_phrase'] = false; |
|
1505 | + } |
|
1506 | + if (!isset($opts['single_passage'])) { |
|
1507 | + $opts['single_passage'] = false; |
|
1508 | + } |
|
1509 | + if (!isset($opts['use_boundaries'])) { |
|
1510 | + $opts['use_boundaries'] = false; |
|
1511 | + } |
|
1512 | + if (!isset($opts['weight_order'])) { |
|
1513 | + $opts['weight_order'] = false; |
|
1514 | + } |
|
1515 | + if (!isset($opts['query_mode'])) { |
|
1516 | + $opts['query_mode'] = false; |
|
1517 | + } |
|
1518 | + if (!isset($opts['force_all_words'])) { |
|
1519 | + $opts['force_all_words'] = false; |
|
1520 | + } |
|
1521 | + if (!isset($opts['start_passage_id'])) { |
|
1522 | + $opts['start_passage_id'] = 1; |
|
1523 | + } |
|
1524 | + if (!isset($opts['load_files'])) { |
|
1525 | + $opts['load_files'] = false; |
|
1526 | + } |
|
1527 | + if (!isset($opts['html_strip_mode'])) { |
|
1528 | + $opts['html_strip_mode'] = 'index'; |
|
1529 | + } |
|
1530 | + if (!isset($opts['allow_empty'])) { |
|
1531 | + $opts['allow_empty'] = false; |
|
1532 | + } |
|
1533 | + if (!isset($opts['passage_boundary'])) { |
|
1534 | + $opts['passage_boundary'] = 'none'; |
|
1535 | + } |
|
1536 | + if (!isset($opts['emit_zones'])) { |
|
1537 | + $opts['emit_zones'] = false; |
|
1538 | + } |
|
1539 | + if (!isset($opts['load_files_scattered'])) { |
|
1540 | + $opts['load_files_scattered'] = false; |
|
1541 | + } |
|
1542 | + |
|
1543 | + |
|
1544 | + ///////////////// |
|
1545 | + // build request |
|
1546 | + ///////////////// |
|
1547 | + |
|
1548 | + // v.1.2 req |
|
1549 | + $flags = 1; // remove spaces |
|
1550 | + if ($opts['exact_phrase']) { |
|
1551 | + $flags |= 2; |
|
1552 | + } |
|
1553 | + if ($opts['single_passage']) { |
|
1554 | + $flags |= 4; |
|
1555 | + } |
|
1556 | + if ($opts['use_boundaries']) { |
|
1557 | + $flags |= 8; |
|
1558 | + } |
|
1559 | + if ($opts['weight_order']) { |
|
1560 | + $flags |= 16; |
|
1561 | + } |
|
1562 | + if ($opts['query_mode']) { |
|
1563 | + $flags |= 32; |
|
1564 | + } |
|
1565 | + if ($opts['force_all_words']) { |
|
1566 | + $flags |= 64; |
|
1567 | + } |
|
1568 | + if ($opts['load_files']) { |
|
1569 | + $flags |= 128; |
|
1570 | + } |
|
1571 | + if ($opts['allow_empty']) { |
|
1572 | + $flags |= 256; |
|
1573 | + } |
|
1574 | + if ($opts['emit_zones']) { |
|
1575 | + $flags |= 512; |
|
1576 | + } |
|
1577 | + if ($opts['load_files_scattered']) { |
|
1578 | + $flags |= 1024; |
|
1579 | + } |
|
1580 | + $req = pack('NN', 0, $flags); // mode=0, flags=$flags |
|
1581 | + $req .= pack('N', strlen($index)) . $index; // req index |
|
1582 | + $req .= pack('N', strlen($words)) . $words; // req words |
|
1583 | + |
|
1584 | + // options |
|
1585 | + $req .= pack('N', strlen($opts['before_match'])) . $opts['before_match']; |
|
1586 | + $req .= pack('N', strlen($opts['after_match'])) . $opts['after_match']; |
|
1587 | + $req .= pack('N', strlen($opts['chunk_separator'])) . $opts['chunk_separator']; |
|
1588 | + $req .= pack('NN', (int)$opts['limit'], (int)$opts['around']); |
|
1589 | + $req .= pack('NNN', (int)$opts['limit_passages'], (int)$opts['limit_words'], (int)$opts['start_passage_id']); // v.1.2 |
|
1590 | + $req .= pack('N', strlen($opts['html_strip_mode'])) . $opts['html_strip_mode']; |
|
1591 | + $req .= pack('N', strlen($opts['passage_boundary'])) . $opts['passage_boundary']; |
|
1592 | + |
|
1593 | + // documents |
|
1594 | + $req .= pack('N', count($docs)); |
|
1595 | + foreach ($docs as $doc) { |
|
1596 | + assert(is_string($doc)); |
|
1597 | + $req .= pack('N', strlen($doc)) . $doc; |
|
1598 | + } |
|
1599 | + |
|
1600 | + //////////////////////////// |
|
1601 | + // send query, get response |
|
1602 | + //////////////////////////// |
|
1603 | + |
|
1604 | + $len = strlen($req); |
|
1605 | + $req = pack('nnN', SEARCHD_COMMAND_EXCERPT, VER_COMMAND_EXCERPT, $len) . $req; // add header |
|
1606 | + if (!$this->send($fp, $req, $len + 8) || !($response = $this->getResponse($fp, VER_COMMAND_EXCERPT))) { |
|
1607 | + $this->mbPop(); |
|
1608 | + return false; |
|
1609 | + } |
|
1610 | + |
|
1611 | + ////////////////// |
|
1612 | + // parse response |
|
1613 | + ////////////////// |
|
1614 | + |
|
1615 | + $pos = 0; |
|
1616 | + $res = array(); |
|
1617 | + $rlen = strlen($response); |
|
1618 | + $count = count($docs); |
|
1619 | + while ($count--) { |
|
1620 | + list(, $len) = unpack('N*', substr($response, $pos, 4)); |
|
1621 | + $pos += 4; |
|
1622 | + |
|
1623 | + if ($pos + $len > $rlen) { |
|
1624 | + $this->error = 'incomplete reply'; |
|
1625 | + $this->mbPop(); |
|
1626 | + return false; |
|
1627 | + } |
|
1628 | + $res[] = $len ? substr($response, $pos, $len) : ''; |
|
1629 | + $pos += $len; |
|
1630 | + } |
|
1631 | + |
|
1632 | + $this->mbPop(); |
|
1633 | + return $res; |
|
1634 | + } |
|
1635 | + |
|
1636 | + |
|
1637 | + ///////////////////////////////////////////////////////////////////////////// |
|
1638 | + // keyword generation |
|
1639 | + ///////////////////////////////////////////////////////////////////////////// |
|
1640 | + |
|
1641 | + /// connect to searchd server, and generate keyword list for a given query |
|
1642 | + /// returns false on failure, |
|
1643 | + /// an array of words on success |
|
1644 | + public function buildKeywords($query, $index, $hits) |
|
1645 | + { |
|
1646 | + assert(is_string($query)); |
|
1647 | + assert(is_string($index)); |
|
1648 | + assert(is_bool($hits)); |
|
1649 | + |
|
1650 | + $this->mbPush(); |
|
1651 | + |
|
1652 | + if (!($fp = $this->connect())) { |
|
1653 | + $this->mbPop(); |
|
1654 | + return false; |
|
1655 | + } |
|
1656 | + |
|
1657 | + ///////////////// |
|
1658 | + // build request |
|
1659 | + ///////////////// |
|
1660 | + |
|
1661 | + // v.1.0 req |
|
1662 | + $req = pack('N', strlen($query)) . $query; // req query |
|
1663 | + $req .= pack('N', strlen($index)) . $index; // req index |
|
1664 | + $req .= pack('N', (int)$hits); |
|
1665 | + |
|
1666 | + //////////////////////////// |
|
1667 | + // send query, get response |
|
1668 | + //////////////////////////// |
|
1669 | + |
|
1670 | + $len = strlen($req); |
|
1671 | + $req = pack('nnN', SEARCHD_COMMAND_KEYWORDS, VER_COMMAND_KEYWORDS, $len) . $req; // add header |
|
1672 | + if (!$this->send($fp, $req, $len + 8) || !($response = $this->getResponse($fp, VER_COMMAND_KEYWORDS))) { |
|
1673 | + $this->mbPop(); |
|
1674 | + return false; |
|
1675 | + } |
|
1676 | + |
|
1677 | + ////////////////// |
|
1678 | + // parse response |
|
1679 | + ////////////////// |
|
1680 | + |
|
1681 | + $pos = 0; |
|
1682 | + $res = array(); |
|
1683 | + $rlen = strlen($response); |
|
1684 | + list(, $nwords) = unpack('N*', substr($response, $pos, 4)); |
|
1685 | + $pos += 4; |
|
1686 | + for ($i = 0; $i < $nwords; $i++) { |
|
1687 | + list(, $len) = unpack('N*', substr($response, $pos, 4)); |
|
1688 | + $pos += 4; |
|
1689 | + $tokenized = $len ? substr($response, $pos, $len) : ''; |
|
1690 | + $pos += $len; |
|
1691 | + |
|
1692 | + list(, $len) = unpack('N*', substr($response, $pos, 4)); |
|
1693 | + $pos += 4; |
|
1694 | + $normalized = $len ? substr($response, $pos, $len) : ''; |
|
1695 | + $pos += $len; |
|
1696 | + |
|
1697 | + $res[] = array( |
|
1698 | + 'tokenized' => $tokenized, |
|
1699 | + 'normalized' => $normalized |
|
1700 | + ); |
|
1701 | + |
|
1702 | + if ($hits) { |
|
1703 | + list($ndocs, $nhits) = array_values(unpack('N*N*', substr($response, $pos, 8))); |
|
1704 | + $pos += 8; |
|
1705 | + $res[$i]['docs'] = $ndocs; |
|
1706 | + $res[$i]['hits'] = $nhits; |
|
1707 | + } |
|
1708 | + |
|
1709 | + if ($pos > $rlen) { |
|
1710 | + $this->error = 'incomplete reply'; |
|
1711 | + $this->mbPop(); |
|
1712 | + return false; |
|
1713 | + } |
|
1714 | + } |
|
1715 | + |
|
1716 | + $this->mbPop(); |
|
1717 | + return $res; |
|
1718 | + } |
|
1719 | + |
|
1720 | + public function escapeString($string) |
|
1721 | + { |
|
1722 | + $from = array('\\', '(',')','|','-','!','@','~','"','&', '/', '^', '$', '=', '<'); |
|
1723 | + $to = array('\\\\', '\(','\)','\|','\-','\!','\@','\~','\"', '\&', '\/', '\^', '\$', '\=', '\<'); |
|
1724 | + |
|
1725 | + return str_replace($from, $to, $string); |
|
1726 | + } |
|
1727 | + |
|
1728 | + ///////////////////////////////////////////////////////////////////////////// |
|
1729 | + // attribute updates |
|
1730 | + ///////////////////////////////////////////////////////////////////////////// |
|
1731 | + |
|
1732 | + /// batch update given attributes in given rows in given indexes |
|
1733 | + /// returns amount of updated documents (0 or more) on success, or -1 on failure |
|
1734 | + public function updateAttributes($index, $attrs, $values, $mva = false, $ignorenonexistent = false) |
|
1735 | + { |
|
1736 | + // verify everything |
|
1737 | + assert(is_string($index)); |
|
1738 | + assert(is_bool($mva)); |
|
1739 | + assert(is_bool($ignorenonexistent)); |
|
1740 | + |
|
1741 | + assert(is_array($attrs)); |
|
1742 | + foreach ($attrs as $attr) { |
|
1743 | + assert(is_string($attr)); |
|
1744 | + } |
|
1745 | + |
|
1746 | + assert(is_array($values)); |
|
1747 | + foreach ($values as $id => $entry) { |
|
1748 | + assert(is_numeric($id)); |
|
1749 | + assert(is_array($entry)); |
|
1750 | + assert(count($entry) == count($attrs)); |
|
1751 | + foreach ($entry as $v) { |
|
1752 | + if ($mva) { |
|
1753 | + assert(is_array($v)); |
|
1754 | + foreach ($v as $vv) { |
|
1755 | + assert(is_int($vv)); |
|
1756 | + } |
|
1757 | + } else { |
|
1758 | + assert(is_int($v)); |
|
1759 | + } |
|
1760 | + } |
|
1761 | + } |
|
1762 | + |
|
1763 | + // build request |
|
1764 | + $this->mbPush(); |
|
1765 | + $req = pack('N', strlen($index)) . $index; |
|
1766 | + |
|
1767 | + $req .= pack('N', count($attrs)); |
|
1768 | + $req .= pack('N', $ignorenonexistent ? 1 : 0); |
|
1769 | + foreach ($attrs as $attr) { |
|
1770 | + $req .= pack('N', strlen($attr)) . $attr; |
|
1771 | + $req .= pack('N', $mva ? 1 : 0); |
|
1772 | + } |
|
1773 | + |
|
1774 | + $req .= pack('N', count($values)); |
|
1775 | + foreach ($values as $id => $entry) { |
|
1776 | + $req .= sphPackU64($id); |
|
1777 | + foreach ($entry as $v) { |
|
1778 | + $req .= pack('N', $mva ? count($v) : $v); |
|
1779 | + if ($mva) { |
|
1780 | + foreach ($v as $vv) { |
|
1781 | + $req .= pack('N', $vv); |
|
1782 | + } |
|
1783 | + } |
|
1784 | + } |
|
1785 | + } |
|
1786 | + |
|
1787 | + // connect, send query, get response |
|
1788 | + if (!($fp = $this->connect())) { |
|
1789 | + $this->mbPop(); |
|
1790 | + return -1; |
|
1791 | + } |
|
1792 | + |
|
1793 | + $len = strlen($req); |
|
1794 | + $req = pack('nnN', SEARCHD_COMMAND_UPDATE, VER_COMMAND_UPDATE, $len) . $req; // add header |
|
1795 | + if (!$this->send($fp, $req, $len + 8)) { |
|
1796 | + $this->mbPop(); |
|
1797 | + return -1; |
|
1798 | + } |
|
1799 | + |
|
1800 | + if (!($response = $this->getResponse($fp, VER_COMMAND_UPDATE))) { |
|
1801 | + $this->mbPop(); |
|
1802 | + return -1; |
|
1803 | + } |
|
1804 | + |
|
1805 | + // parse response |
|
1806 | + list(, $updated) = unpack('N*', substr($response, 0, 4)); |
|
1807 | + $this->mbPop(); |
|
1808 | + return $updated; |
|
1809 | + } |
|
1810 | + |
|
1811 | + ///////////////////////////////////////////////////////////////////////////// |
|
1812 | + // persistent connections |
|
1813 | + ///////////////////////////////////////////////////////////////////////////// |
|
1814 | + |
|
1815 | + public function open() |
|
1816 | + { |
|
1817 | + if ($this->socket !== false) { |
|
1818 | + $this->error = 'already connected'; |
|
1819 | + return false; |
|
1820 | + } |
|
1821 | + if (!($fp = $this->connect())) |
|
1822 | + return false; |
|
1823 | + |
|
1824 | + // command, command version = 0, body length = 4, body = 1 |
|
1825 | + $req = pack('nnNN', SEARCHD_COMMAND_PERSIST, 0, 4, 1); |
|
1826 | + if (!$this->send($fp, $req, 12)) { |
|
1827 | + return false; |
|
1828 | + } |
|
1829 | + |
|
1830 | + $this->socket = $fp; |
|
1831 | + return true; |
|
1832 | + } |
|
1833 | + |
|
1834 | + public function close() |
|
1835 | + { |
|
1836 | + if ($this->socket === false) { |
|
1837 | + $this->error = 'not connected'; |
|
1838 | + return false; |
|
1839 | + } |
|
1840 | + |
|
1841 | + fclose($this->socket); |
|
1842 | + $this->socket = false; |
|
1843 | + |
|
1844 | + return true; |
|
1845 | + } |
|
1846 | + |
|
1847 | + ////////////////////////////////////////////////////////////////////////// |
|
1848 | + // status |
|
1849 | + ////////////////////////////////////////////////////////////////////////// |
|
1850 | + |
|
1851 | + public function status($session = false) |
|
1852 | + { |
|
1853 | + assert(is_bool($session)); |
|
1854 | + |
|
1855 | + $this->mbPush(); |
|
1856 | + if (!($fp = $this->connect())) { |
|
1857 | + $this->mbPop(); |
|
1858 | + return false; |
|
1859 | + } |
|
1860 | + |
|
1861 | + $req = pack('nnNN', SEARCHD_COMMAND_STATUS, VER_COMMAND_STATUS, 4, $session ? 0 : 1); // len=4, body=1 |
|
1862 | + if (!$this->send($fp, $req, 12) || !($response = $this->getResponse($fp, VER_COMMAND_STATUS))) { |
|
1863 | + $this->mbPop(); |
|
1864 | + return false; |
|
1865 | + } |
|
1866 | + |
|
1867 | + $res = substr($response, 4); // just ignore length, error handling, etc |
|
1868 | + $p = 0; |
|
1869 | + list($rows, $cols) = array_values(unpack('N*N*', substr($response, $p, 8))); |
|
1870 | + $p += 8; |
|
1871 | + |
|
1872 | + $res = array(); |
|
1873 | + for ($i = 0; $i < $rows; $i++) { |
|
1874 | + for ($j = 0; $j < $cols; $j++) { |
|
1875 | + list(, $len) = unpack('N*', substr($response, $p, 4)); |
|
1876 | + $p += 4; |
|
1877 | + $res[$i][] = substr($response, $p, $len); |
|
1878 | + $p += $len; |
|
1879 | + } |
|
1880 | + } |
|
1881 | + |
|
1882 | + $this->mbPop(); |
|
1883 | + return $res; |
|
1884 | + } |
|
1885 | + |
|
1886 | + ////////////////////////////////////////////////////////////////////////// |
|
1887 | + // flush |
|
1888 | + ////////////////////////////////////////////////////////////////////////// |
|
1889 | + |
|
1890 | + public function flushAttributes() |
|
1891 | + { |
|
1892 | + $this->mbPush(); |
|
1893 | + if (!($fp = $this->connect())) { |
|
1894 | + $this->mbPop(); |
|
1895 | + return -1; |
|
1896 | + } |
|
1897 | + |
|
1898 | + $req = pack('nnN', SEARCHD_COMMAND_FLUSHATTRS, VER_COMMAND_FLUSHATTRS, 0); // len=0 |
|
1899 | + if (!$this->send($fp, $req, 8) || !($response = $this->getResponse($fp, VER_COMMAND_FLUSHATTRS))) { |
|
1900 | + $this->mbPop(); |
|
1901 | + return -1; |
|
1902 | + } |
|
1903 | + |
|
1904 | + $tag = -1; |
|
1905 | + if (strlen($response) == 4) { |
|
1906 | + list(, $tag) = unpack('N*', $response); |
|
1907 | + } else { |
|
1908 | + $this->error = 'unexpected response length'; |
|
1909 | + } |
|
1910 | + |
|
1911 | + $this->mbPop(); |
|
1912 | + return $tag; |
|
1913 | + } |
|
1914 | 1914 | } |
@@ -33,82 +33,82 @@ discard block |
||
33 | 33 | ///////////////////////////////////////////////////////////////////////////// |
34 | 34 | |
35 | 35 | /// known searchd commands |
36 | -define('SEARCHD_COMMAND_SEARCH', 0); |
|
37 | -define('SEARCHD_COMMAND_EXCERPT', 1); |
|
38 | -define('SEARCHD_COMMAND_UPDATE', 2); |
|
39 | -define('SEARCHD_COMMAND_KEYWORDS', 3); |
|
40 | -define('SEARCHD_COMMAND_PERSIST', 4); |
|
41 | -define('SEARCHD_COMMAND_STATUS', 5); |
|
36 | +define('SEARCHD_COMMAND_SEARCH', 0); |
|
37 | +define('SEARCHD_COMMAND_EXCERPT', 1); |
|
38 | +define('SEARCHD_COMMAND_UPDATE', 2); |
|
39 | +define('SEARCHD_COMMAND_KEYWORDS', 3); |
|
40 | +define('SEARCHD_COMMAND_PERSIST', 4); |
|
41 | +define('SEARCHD_COMMAND_STATUS', 5); |
|
42 | 42 | define('SEARCHD_COMMAND_FLUSHATTRS', 7); |
43 | 43 | |
44 | 44 | /// current client-side command implementation versions |
45 | -define('VER_COMMAND_SEARCH', 0x11E); |
|
46 | -define('VER_COMMAND_EXCERPT', 0x104); |
|
47 | -define('VER_COMMAND_UPDATE', 0x103); |
|
48 | -define('VER_COMMAND_KEYWORDS', 0x100); |
|
49 | -define('VER_COMMAND_STATUS', 0x101); |
|
50 | -define('VER_COMMAND_QUERY', 0x100); |
|
45 | +define('VER_COMMAND_SEARCH', 0x11E); |
|
46 | +define('VER_COMMAND_EXCERPT', 0x104); |
|
47 | +define('VER_COMMAND_UPDATE', 0x103); |
|
48 | +define('VER_COMMAND_KEYWORDS', 0x100); |
|
49 | +define('VER_COMMAND_STATUS', 0x101); |
|
50 | +define('VER_COMMAND_QUERY', 0x100); |
|
51 | 51 | define('VER_COMMAND_FLUSHATTRS', 0x100); |
52 | 52 | |
53 | 53 | /// known searchd status codes |
54 | -define('SEARCHD_OK', 0); |
|
55 | -define('SEARCHD_ERROR', 1); |
|
56 | -define('SEARCHD_RETRY', 2); |
|
54 | +define('SEARCHD_OK', 0); |
|
55 | +define('SEARCHD_ERROR', 1); |
|
56 | +define('SEARCHD_RETRY', 2); |
|
57 | 57 | define('SEARCHD_WARNING', 3); |
58 | 58 | |
59 | 59 | /// known match modes |
60 | -define('SPH_MATCH_ALL', 0); |
|
61 | -define('SPH_MATCH_ANY', 1); |
|
62 | -define('SPH_MATCH_PHRASE', 2); |
|
63 | -define('SPH_MATCH_BOOLEAN', 3); |
|
64 | -define('SPH_MATCH_EXTENDED', 4); |
|
65 | -define('SPH_MATCH_FULLSCAN', 5); |
|
60 | +define('SPH_MATCH_ALL', 0); |
|
61 | +define('SPH_MATCH_ANY', 1); |
|
62 | +define('SPH_MATCH_PHRASE', 2); |
|
63 | +define('SPH_MATCH_BOOLEAN', 3); |
|
64 | +define('SPH_MATCH_EXTENDED', 4); |
|
65 | +define('SPH_MATCH_FULLSCAN', 5); |
|
66 | 66 | define('SPH_MATCH_EXTENDED2', 6); // extended engine V2 (TEMPORARY, WILL BE REMOVED) |
67 | 67 | |
68 | 68 | /// known ranking modes (ext2 only) |
69 | 69 | define('SPH_RANK_PROXIMITY_BM25', 0); ///< default mode, phrase proximity major factor and BM25 minor one |
70 | -define('SPH_RANK_BM25', 1); ///< statistical mode, BM25 ranking only (faster but worse quality) |
|
71 | -define('SPH_RANK_NONE', 2); ///< no ranking, all matches get a weight of 1 |
|
72 | -define('SPH_RANK_WORDCOUNT', 3); ///< simple word-count weighting, rank is a weighted sum of per-field keyword occurence counts |
|
73 | -define('SPH_RANK_PROXIMITY', 4); |
|
74 | -define('SPH_RANK_MATCHANY', 5); |
|
75 | -define('SPH_RANK_FIELDMASK', 6); |
|
76 | -define('SPH_RANK_SPH04', 7); |
|
77 | -define('SPH_RANK_EXPR', 8); |
|
78 | -define('SPH_RANK_TOTAL', 9); |
|
70 | +define('SPH_RANK_BM25', 1); ///< statistical mode, BM25 ranking only (faster but worse quality) |
|
71 | +define('SPH_RANK_NONE', 2); ///< no ranking, all matches get a weight of 1 |
|
72 | +define('SPH_RANK_WORDCOUNT', 3); ///< simple word-count weighting, rank is a weighted sum of per-field keyword occurence counts |
|
73 | +define('SPH_RANK_PROXIMITY', 4); |
|
74 | +define('SPH_RANK_MATCHANY', 5); |
|
75 | +define('SPH_RANK_FIELDMASK', 6); |
|
76 | +define('SPH_RANK_SPH04', 7); |
|
77 | +define('SPH_RANK_EXPR', 8); |
|
78 | +define('SPH_RANK_TOTAL', 9); |
|
79 | 79 | |
80 | 80 | /// known sort modes |
81 | -define('SPH_SORT_RELEVANCE', 0); |
|
82 | -define('SPH_SORT_ATTR_DESC', 1); |
|
83 | -define('SPH_SORT_ATTR_ASC', 2); |
|
81 | +define('SPH_SORT_RELEVANCE', 0); |
|
82 | +define('SPH_SORT_ATTR_DESC', 1); |
|
83 | +define('SPH_SORT_ATTR_ASC', 2); |
|
84 | 84 | define('SPH_SORT_TIME_SEGMENTS', 3); |
85 | -define('SPH_SORT_EXTENDED', 4); |
|
86 | -define('SPH_SORT_EXPR', 5); |
|
85 | +define('SPH_SORT_EXTENDED', 4); |
|
86 | +define('SPH_SORT_EXPR', 5); |
|
87 | 87 | |
88 | 88 | /// known filter types |
89 | -define('SPH_FILTER_VALUES', 0); |
|
90 | -define('SPH_FILTER_RANGE', 1); |
|
89 | +define('SPH_FILTER_VALUES', 0); |
|
90 | +define('SPH_FILTER_RANGE', 1); |
|
91 | 91 | define('SPH_FILTER_FLOATRANGE', 2); |
92 | -define('SPH_FILTER_STRING', 3); |
|
92 | +define('SPH_FILTER_STRING', 3); |
|
93 | 93 | |
94 | 94 | /// known attribute types |
95 | -define('SPH_ATTR_INTEGER', 1); |
|
95 | +define('SPH_ATTR_INTEGER', 1); |
|
96 | 96 | define('SPH_ATTR_TIMESTAMP', 2); |
97 | -define('SPH_ATTR_ORDINAL', 3); |
|
98 | -define('SPH_ATTR_BOOL', 4); |
|
99 | -define('SPH_ATTR_FLOAT', 5); |
|
100 | -define('SPH_ATTR_BIGINT', 6); |
|
101 | -define('SPH_ATTR_STRING', 7); |
|
102 | -define('SPH_ATTR_FACTORS', 1001); |
|
103 | -define('SPH_ATTR_MULTI', 0x40000001); |
|
104 | -define('SPH_ATTR_MULTI64', 0x40000002); |
|
97 | +define('SPH_ATTR_ORDINAL', 3); |
|
98 | +define('SPH_ATTR_BOOL', 4); |
|
99 | +define('SPH_ATTR_FLOAT', 5); |
|
100 | +define('SPH_ATTR_BIGINT', 6); |
|
101 | +define('SPH_ATTR_STRING', 7); |
|
102 | +define('SPH_ATTR_FACTORS', 1001); |
|
103 | +define('SPH_ATTR_MULTI', 0x40000001); |
|
104 | +define('SPH_ATTR_MULTI64', 0x40000002); |
|
105 | 105 | |
106 | 106 | /// known grouping functions |
107 | -define('SPH_GROUPBY_DAY', 0); |
|
108 | -define('SPH_GROUPBY_WEEK', 1); |
|
109 | -define('SPH_GROUPBY_MONTH', 2); |
|
110 | -define('SPH_GROUPBY_YEAR', 3); |
|
111 | -define('SPH_GROUPBY_ATTR', 4); |
|
107 | +define('SPH_GROUPBY_DAY', 0); |
|
108 | +define('SPH_GROUPBY_WEEK', 1); |
|
109 | +define('SPH_GROUPBY_MONTH', 2); |
|
110 | +define('SPH_GROUPBY_YEAR', 3); |
|
111 | +define('SPH_GROUPBY_ATTR', 4); |
|
112 | 112 | define('SPH_GROUPBY_ATTRPAIR', 5); |
113 | 113 | |
114 | 114 | // important properties of PHP's integers: |
@@ -143,19 +143,19 @@ discard block |
||
143 | 143 | assert(is_numeric($v)); |
144 | 144 | |
145 | 145 | // x64 |
146 | - if (PHP_INT_SIZE >= 8) { |
|
146 | + if (PHP_INT_SIZE>=8) { |
|
147 | 147 | $v = (int)$v; |
148 | 148 | return pack('NN', $v >> 32, $v & 0xFFFFFFFF); |
149 | 149 | } |
150 | 150 | |
151 | 151 | // x32, int |
152 | 152 | if (is_int($v)) { |
153 | - return pack('NN', $v < 0 ? -1 : 0, $v); |
|
153 | + return pack('NN', $v<0 ? -1 : 0, $v); |
|
154 | 154 | } |
155 | 155 | |
156 | 156 | // x32, bcmath |
157 | 157 | if (function_exists('bcmul')) { |
158 | - if (bccomp($v, 0) == -1) { |
|
158 | + if (bccomp($v, 0)==-1) { |
|
159 | 159 | $v = bcadd('18446744073709551616', $v); |
160 | 160 | } |
161 | 161 | $h = bcdiv($v, '4294967296', 0); |
@@ -173,8 +173,8 @@ discard block |
||
173 | 173 | $l = $m - ($q * 4294967296.0); |
174 | 174 | $h = $hi * 2328.0 + $q; // (10 ^ 13) / (1 << 32) = 2328 |
175 | 175 | |
176 | - if ($v < 0) { |
|
177 | - if ($l == 0) { |
|
176 | + if ($v<0) { |
|
177 | + if ($l==0) { |
|
178 | 178 | $h = 4294967296.0 - $h; |
179 | 179 | } else { |
180 | 180 | $h = 4294967295.0 - $h; |
@@ -190,8 +190,8 @@ discard block |
||
190 | 190 | assert(is_numeric($v)); |
191 | 191 | |
192 | 192 | // x64 |
193 | - if (PHP_INT_SIZE >= 8) { |
|
194 | - assert($v >= 0); |
|
193 | + if (PHP_INT_SIZE>=8) { |
|
194 | + assert($v>=0); |
|
195 | 195 | |
196 | 196 | // x64, int |
197 | 197 | if (is_int($v)) { |
@@ -247,16 +247,16 @@ discard block |
||
247 | 247 | { |
248 | 248 | list($hi, $lo) = array_values(unpack('N*N*', $v)); |
249 | 249 | |
250 | - if (PHP_INT_SIZE >= 8) { |
|
251 | - if ($hi < 0) { // because php 5.2.2 to 5.2.5 is totally fucked up again |
|
250 | + if (PHP_INT_SIZE>=8) { |
|
251 | + if ($hi<0) { // because php 5.2.2 to 5.2.5 is totally fucked up again |
|
252 | 252 | $hi += 1 << 32; |
253 | 253 | } |
254 | - if ($lo < 0) { |
|
254 | + if ($lo<0) { |
|
255 | 255 | $lo += 1 << 32; |
256 | 256 | } |
257 | 257 | |
258 | 258 | // x64, int |
259 | - if ($hi <= 2147483647) { |
|
259 | + if ($hi<=2147483647) { |
|
260 | 260 | return ($hi << 32) + $lo; |
261 | 261 | } |
262 | 262 | |
@@ -269,20 +269,20 @@ discard block |
||
269 | 269 | $C = 100000; |
270 | 270 | $h = ((int)($hi / $C) << 32) + (int)($lo / $C); |
271 | 271 | $l = (($hi % $C) << 32) + ($lo % $C); |
272 | - if ($l > $C) { |
|
272 | + if ($l>$C) { |
|
273 | 273 | $h += (int)($l / $C); |
274 | 274 | $l = $l % $C; |
275 | 275 | } |
276 | 276 | |
277 | - if ($h == 0) { |
|
277 | + if ($h==0) { |
|
278 | 278 | return $l; |
279 | 279 | } |
280 | 280 | return sprintf('%d%05d', $h, $l); |
281 | 281 | } |
282 | 282 | |
283 | 283 | // x32, int |
284 | - if ($hi == 0) { |
|
285 | - if ($lo > 0) { |
|
284 | + if ($hi==0) { |
|
285 | + if ($lo>0) { |
|
286 | 286 | return $lo; |
287 | 287 | } |
288 | 288 | return sprintf('%u', $lo); |
@@ -309,10 +309,10 @@ discard block |
||
309 | 309 | |
310 | 310 | $h = sprintf('%.0f', $h); |
311 | 311 | $l = sprintf('%07.0f', $l); |
312 | - if ($h == '0') { |
|
312 | + if ($h=='0') { |
|
313 | 313 | return sprintf('%.0f', (float)$l); |
314 | 314 | } |
315 | - return $h . $l; |
|
315 | + return $h.$l; |
|
316 | 316 | } |
317 | 317 | |
318 | 318 | // unpack 64-bit signed |
@@ -321,24 +321,24 @@ discard block |
||
321 | 321 | list($hi, $lo) = array_values(unpack('N*N*', $v)); |
322 | 322 | |
323 | 323 | // x64 |
324 | - if (PHP_INT_SIZE >= 8) { |
|
325 | - if ($hi < 0) { // because php 5.2.2 to 5.2.5 is totally fucked up again |
|
324 | + if (PHP_INT_SIZE>=8) { |
|
325 | + if ($hi<0) { // because php 5.2.2 to 5.2.5 is totally fucked up again |
|
326 | 326 | $hi += 1 << 32; |
327 | 327 | } |
328 | - if ($lo < 0) { |
|
328 | + if ($lo<0) { |
|
329 | 329 | $lo += 1 << 32; |
330 | 330 | } |
331 | 331 | |
332 | 332 | return ($hi << 32) + $lo; |
333 | 333 | } |
334 | 334 | |
335 | - if ($hi == 0) { // x32, int |
|
336 | - if ($lo > 0) { |
|
335 | + if ($hi==0) { // x32, int |
|
336 | + if ($lo>0) { |
|
337 | 337 | return $lo; |
338 | 338 | } |
339 | 339 | return sprintf('%u', $lo); |
340 | - } elseif ($hi == -1) { // x32, int |
|
341 | - if ($lo < 0) { |
|
340 | + } elseif ($hi==-1) { // x32, int |
|
341 | + if ($lo<0) { |
|
342 | 342 | return $lo; |
343 | 343 | } |
344 | 344 | return sprintf('%.0f', $lo - 4294967296.0); |
@@ -346,7 +346,7 @@ discard block |
||
346 | 346 | |
347 | 347 | $neg = ''; |
348 | 348 | $c = 0; |
349 | - if ($hi < 0) { |
|
349 | + if ($hi<0) { |
|
350 | 350 | $hi = ~$hi; |
351 | 351 | $lo = ~$lo; |
352 | 352 | $c = 1; |
@@ -358,7 +358,7 @@ discard block |
||
358 | 358 | |
359 | 359 | // x32, bcmath |
360 | 360 | if (function_exists('bcmul')) { |
361 | - return $neg . bcadd(bcadd($lo, bcmul($hi, '4294967296')), $c); |
|
361 | + return $neg.bcadd(bcadd($lo, bcmul($hi, '4294967296')), $c); |
|
362 | 362 | } |
363 | 363 | |
364 | 364 | // x32, no-bcmath |
@@ -371,25 +371,25 @@ discard block |
||
371 | 371 | $mq = floor($m / 10000000.0); |
372 | 372 | $l = $m - $mq * 10000000.0 + $c; |
373 | 373 | $h = $q * 4294967296.0 + $r * 429.0 + $mq; |
374 | - if ($l == 10000000) { |
|
374 | + if ($l==10000000) { |
|
375 | 375 | $l = 0; |
376 | 376 | $h += 1; |
377 | 377 | } |
378 | 378 | |
379 | 379 | $h = sprintf('%.0f', $h); |
380 | 380 | $l = sprintf('%07.0f', $l); |
381 | - if ($h == '0') { |
|
382 | - return $neg . sprintf('%.0f', (float)$l); |
|
381 | + if ($h=='0') { |
|
382 | + return $neg.sprintf('%.0f', (float)$l); |
|
383 | 383 | } |
384 | - return $neg . $h . $l; |
|
384 | + return $neg.$h.$l; |
|
385 | 385 | } |
386 | 386 | |
387 | 387 | |
388 | 388 | function sphFixUint($value) |
389 | 389 | { |
390 | - if (PHP_INT_SIZE >= 8) { |
|
390 | + if (PHP_INT_SIZE>=8) { |
|
391 | 391 | // x64 route, workaround broken unpack() in 5.2.2+ |
392 | - if ($value < 0) { |
|
392 | + if ($value<0) { |
|
393 | 393 | $value += 1 << 32; |
394 | 394 | } |
395 | 395 | return $value; |
@@ -471,7 +471,7 @@ discard block |
||
471 | 471 | |
472 | 472 | public function __destruct() |
473 | 473 | { |
474 | - if ($this->socket !== false) { |
|
474 | + if ($this->socket!==false) { |
|
475 | 475 | fclose($this->socket); |
476 | 476 | } |
477 | 477 | } |
@@ -498,19 +498,19 @@ discard block |
||
498 | 498 | public function setServer($host, $port = 0) |
499 | 499 | { |
500 | 500 | assert(is_string($host)); |
501 | - if ($host[0] == '/') { |
|
502 | - $this->path = 'unix://' . $host; |
|
501 | + if ($host[0]=='/') { |
|
502 | + $this->path = 'unix://'.$host; |
|
503 | 503 | return; |
504 | 504 | } |
505 | - if (substr($host, 0, 7) == 'unix://') { |
|
505 | + if (substr($host, 0, 7)=='unix://') { |
|
506 | 506 | $this->path = $host; |
507 | 507 | return; |
508 | 508 | } |
509 | 509 | |
510 | 510 | $this->host = $host; |
511 | 511 | $port = intval($port); |
512 | - assert(0 <= $port && $port < 65536); |
|
513 | - $this->port = $port == 0 ? 9312 : $port; |
|
512 | + assert(0<=$port && $port<65536); |
|
513 | + $this->port = $port==0 ? 9312 : $port; |
|
514 | 514 | $this->path = ''; |
515 | 515 | } |
516 | 516 | |
@@ -524,7 +524,7 @@ discard block |
||
524 | 524 | |
525 | 525 | protected function send($handle, $data, $length) |
526 | 526 | { |
527 | - if (feof($handle) || fwrite($handle, $data, $length) !== $length) { |
|
527 | + if (feof($handle) || fwrite($handle, $data, $length)!==$length) { |
|
528 | 528 | $this->error = 'connection unexpectedly closed (timed out?)'; |
529 | 529 | $this->conn_error = true; |
530 | 530 | return false; |
@@ -555,7 +555,7 @@ discard block |
||
555 | 555 | /// connect to searchd server |
556 | 556 | protected function connect() |
557 | 557 | { |
558 | - if ($this->socket !== false) { |
|
558 | + if ($this->socket!==false) { |
|
559 | 559 | // we are in persistent connection mode, so we have a socket |
560 | 560 | // however, need to check whether it's still alive |
561 | 561 | if (!@feof($this->socket)) { |
@@ -578,7 +578,7 @@ discard block |
||
578 | 578 | $port = $this->port; |
579 | 579 | } |
580 | 580 | |
581 | - if ($this->timeout <= 0) { |
|
581 | + if ($this->timeout<=0) { |
|
582 | 582 | $fp = @fsockopen($host, $port, $errno, $errstr); |
583 | 583 | } else { |
584 | 584 | $fp = @fsockopen($host, $port, $errno, $errstr, $this->timeout); |
@@ -610,7 +610,7 @@ discard block |
||
610 | 610 | // check version |
611 | 611 | list(, $v) = unpack('N*', fread($fp, 4)); |
612 | 612 | $v = (int)$v; |
613 | - if ($v < 1) { |
|
613 | + if ($v<1) { |
|
614 | 614 | fclose($fp); |
615 | 615 | $this->error = "expected searchd protocol version 1+, got version '$v'"; |
616 | 616 | return false; |
@@ -626,10 +626,10 @@ discard block |
||
626 | 626 | $len = 0; |
627 | 627 | |
628 | 628 | $header = fread($fp, 8); |
629 | - if (strlen($header) == 8) { |
|
629 | + if (strlen($header)==8) { |
|
630 | 630 | list($status, $ver, $len) = array_values(unpack('n2a/Nb', $header)); |
631 | 631 | $left = $len; |
632 | - while ($left > 0 && !feof($fp)) { |
|
632 | + while ($left>0 && !feof($fp)) { |
|
633 | 633 | $chunk = fread($fp, min(8192, $left)); |
634 | 634 | if ($chunk) { |
635 | 635 | $response .= $chunk; |
@@ -637,13 +637,13 @@ discard block |
||
637 | 637 | } |
638 | 638 | } |
639 | 639 | } |
640 | - if ($this->socket === false) { |
|
640 | + if ($this->socket===false) { |
|
641 | 641 | fclose($fp); |
642 | 642 | } |
643 | 643 | |
644 | 644 | // check response |
645 | 645 | $read = strlen($response); |
646 | - if (!$response || $read != $len) { |
|
646 | + if (!$response || $read!=$len) { |
|
647 | 647 | $this->error = $len |
648 | 648 | ? "failed to read searchd response (status=$status, ver=$ver, len=$len, read=$read)" |
649 | 649 | : 'received zero-sized searchd response'; |
@@ -651,26 +651,26 @@ discard block |
||
651 | 651 | } |
652 | 652 | |
653 | 653 | // check status |
654 | - if ($status == SEARCHD_WARNING) { |
|
654 | + if ($status==SEARCHD_WARNING) { |
|
655 | 655 | list(, $wlen) = unpack('N*', substr($response, 0, 4)); |
656 | 656 | $this->warning = substr($response, 4, $wlen); |
657 | 657 | return substr($response, 4 + $wlen); |
658 | 658 | } |
659 | - if ($status == SEARCHD_ERROR) { |
|
660 | - $this->error = 'searchd error: ' . substr($response, 4); |
|
659 | + if ($status==SEARCHD_ERROR) { |
|
660 | + $this->error = 'searchd error: '.substr($response, 4); |
|
661 | 661 | return false; |
662 | 662 | } |
663 | - if ($status == SEARCHD_RETRY) { |
|
664 | - $this->error = 'temporary searchd error: ' . substr($response, 4); |
|
663 | + if ($status==SEARCHD_RETRY) { |
|
664 | + $this->error = 'temporary searchd error: '.substr($response, 4); |
|
665 | 665 | return false; |
666 | 666 | } |
667 | - if ($status != SEARCHD_OK) { |
|
667 | + if ($status!=SEARCHD_OK) { |
|
668 | 668 | $this->error = "unknown status code '$status'"; |
669 | 669 | return false; |
670 | 670 | } |
671 | 671 | |
672 | 672 | // check version |
673 | - if ($ver < $client_ver) { |
|
673 | + if ($ver<$client_ver) { |
|
674 | 674 | $this->warning = sprintf( |
675 | 675 | 'searchd command v.%d.%d older than client\'s v.%d.%d, some options might not work', |
676 | 676 | $ver >> 8, |
@@ -693,15 +693,15 @@ discard block |
||
693 | 693 | { |
694 | 694 | assert(is_int($offset)); |
695 | 695 | assert(is_int($limit)); |
696 | - assert($offset >= 0); |
|
697 | - assert($limit > 0); |
|
698 | - assert($max >= 0); |
|
696 | + assert($offset>=0); |
|
697 | + assert($limit>0); |
|
698 | + assert($max>=0); |
|
699 | 699 | $this->offset = $offset; |
700 | 700 | $this->limit = $limit; |
701 | - if ($max > 0) { |
|
701 | + if ($max>0) { |
|
702 | 702 | $this->max_matches = $max; |
703 | 703 | } |
704 | - if ($cutoff > 0) { |
|
704 | + if ($cutoff>0) { |
|
705 | 705 | $this->cutoff = $cutoff; |
706 | 706 | } |
707 | 707 | } |
@@ -711,7 +711,7 @@ discard block |
||
711 | 711 | public function setMaxQueryTime($max) |
712 | 712 | { |
713 | 713 | assert(is_int($max)); |
714 | - assert($max >= 0); |
|
714 | + assert($max>=0); |
|
715 | 715 | $this->max_query_time = $max; |
716 | 716 | } |
717 | 717 | |
@@ -723,21 +723,21 @@ discard block |
||
723 | 723 | E_USER_DEPRECATED |
724 | 724 | ); |
725 | 725 | assert( |
726 | - $mode == SPH_MATCH_ALL || |
|
727 | - $mode == SPH_MATCH_ANY || |
|
728 | - $mode == SPH_MATCH_PHRASE || |
|
729 | - $mode == SPH_MATCH_BOOLEAN || |
|
730 | - $mode == SPH_MATCH_EXTENDED || |
|
731 | - $mode == SPH_MATCH_FULLSCAN || |
|
732 | - $mode == SPH_MATCH_EXTENDED2 |
|
726 | + $mode==SPH_MATCH_ALL || |
|
727 | + $mode==SPH_MATCH_ANY || |
|
728 | + $mode==SPH_MATCH_PHRASE || |
|
729 | + $mode==SPH_MATCH_BOOLEAN || |
|
730 | + $mode==SPH_MATCH_EXTENDED || |
|
731 | + $mode==SPH_MATCH_FULLSCAN || |
|
732 | + $mode==SPH_MATCH_EXTENDED2 |
|
733 | 733 | ); |
734 | 734 | $this->mode = $mode; |
735 | 735 | } |
736 | 736 | |
737 | 737 | /// set ranking mode |
738 | - public function setRankingMode($ranker, $rankexpr='') |
|
738 | + public function setRankingMode($ranker, $rankexpr = '') |
|
739 | 739 | { |
740 | - assert($ranker === 0 || $ranker >= 1 && $ranker < SPH_RANK_TOTAL); |
|
740 | + assert($ranker===0 || $ranker>=1 && $ranker<SPH_RANK_TOTAL); |
|
741 | 741 | assert(is_string($rankexpr)); |
742 | 742 | $this->ranker = $ranker; |
743 | 743 | $this->rank_expr = $rankexpr; |
@@ -746,16 +746,16 @@ discard block |
||
746 | 746 | /// set matches sorting mode |
747 | 747 | public function setSortMode($mode, $sortby = '') |
748 | 748 | { |
749 | - assert ( |
|
750 | - $mode == SPH_SORT_RELEVANCE || |
|
751 | - $mode == SPH_SORT_ATTR_DESC || |
|
752 | - $mode == SPH_SORT_ATTR_ASC || |
|
753 | - $mode == SPH_SORT_TIME_SEGMENTS || |
|
754 | - $mode == SPH_SORT_EXTENDED || |
|
755 | - $mode == SPH_SORT_EXPR |
|
749 | + assert( |
|
750 | + $mode==SPH_SORT_RELEVANCE || |
|
751 | + $mode==SPH_SORT_ATTR_DESC || |
|
752 | + $mode==SPH_SORT_ATTR_ASC || |
|
753 | + $mode==SPH_SORT_TIME_SEGMENTS || |
|
754 | + $mode==SPH_SORT_EXTENDED || |
|
755 | + $mode==SPH_SORT_EXPR |
|
756 | 756 | ); |
757 | 757 | assert(is_string($sortby)); |
758 | - assert($mode == SPH_SORT_RELEVANCE || strlen($sortby) > 0); |
|
758 | + assert($mode==SPH_SORT_RELEVANCE || strlen($sortby)>0); |
|
759 | 759 | |
760 | 760 | $this->sort = $mode; |
761 | 761 | $this->sort_by = $sortby; |
@@ -799,7 +799,7 @@ discard block |
||
799 | 799 | { |
800 | 800 | assert(is_numeric($min)); |
801 | 801 | assert(is_numeric($max)); |
802 | - assert($min <= $max); |
|
802 | + assert($min<=$max); |
|
803 | 803 | $this->min_id = $min; |
804 | 804 | $this->max_id = $max; |
805 | 805 | } |
@@ -847,7 +847,7 @@ discard block |
||
847 | 847 | assert(is_string($attribute)); |
848 | 848 | assert(is_numeric($min)); |
849 | 849 | assert(is_numeric($max)); |
850 | - assert($min <= $max); |
|
850 | + assert($min<=$max); |
|
851 | 851 | |
852 | 852 | $this->filters[] = array( |
853 | 853 | 'type' => SPH_FILTER_RANGE, |
@@ -865,7 +865,7 @@ discard block |
||
865 | 865 | assert(is_string($attribute)); |
866 | 866 | assert(is_float($min)); |
867 | 867 | assert(is_float($max)); |
868 | - assert($min <= $max); |
|
868 | + assert($min<=$max); |
|
869 | 869 | |
870 | 870 | $this->filters[] = array( |
871 | 871 | 'type' => SPH_FILTER_FLOATRANGE, |
@@ -900,12 +900,12 @@ discard block |
||
900 | 900 | assert(is_string($attribute)); |
901 | 901 | assert(is_string($groupsort)); |
902 | 902 | assert( |
903 | - $func == SPH_GROUPBY_DAY || |
|
904 | - $func == SPH_GROUPBY_WEEK || |
|
905 | - $func == SPH_GROUPBY_MONTH || |
|
906 | - $func == SPH_GROUPBY_YEAR || |
|
907 | - $func == SPH_GROUPBY_ATTR || |
|
908 | - $func == SPH_GROUPBY_ATTRPAIR |
|
903 | + $func==SPH_GROUPBY_DAY || |
|
904 | + $func==SPH_GROUPBY_WEEK || |
|
905 | + $func==SPH_GROUPBY_MONTH || |
|
906 | + $func==SPH_GROUPBY_YEAR || |
|
907 | + $func==SPH_GROUPBY_ATTR || |
|
908 | + $func==SPH_GROUPBY_ATTRPAIR |
|
909 | 909 | ); |
910 | 910 | |
911 | 911 | $this->group_by = $attribute; |
@@ -923,8 +923,8 @@ discard block |
||
923 | 923 | /// set distributed retries count and delay |
924 | 924 | public function setRetries($count, $delay = 0) |
925 | 925 | { |
926 | - assert(is_int($count) && $count >= 0); |
|
927 | - assert(is_int($delay) && $delay >= 0); |
|
926 | + assert(is_int($count) && $count>=0); |
|
927 | + assert(is_int($delay) && $delay>=0); |
|
928 | 928 | $this->retry_count = $count; |
929 | 929 | $this->retry_delay = $delay; |
930 | 930 | } |
@@ -981,12 +981,12 @@ discard block |
||
981 | 981 | 'global_idf', |
982 | 982 | 'low_priority' |
983 | 983 | ); |
984 | - $flags = array ( |
|
984 | + $flags = array( |
|
985 | 985 | 'reverse_scan' => array(0, 1), |
986 | 986 | 'sort_method' => array('pq', 'kbuffer'), |
987 | 987 | 'max_predicted_time' => array(0), |
988 | 988 | 'boolean_simplify' => array(true, false), |
989 | - 'idf' => array ('normalized', 'plain', 'tfidf_normalized', 'tfidf_unnormalized'), |
|
989 | + 'idf' => array('normalized', 'plain', 'tfidf_normalized', 'tfidf_unnormalized'), |
|
990 | 990 | 'global_idf' => array(true, false), |
991 | 991 | 'low_priority' => array(true, false) |
992 | 992 | ); |
@@ -994,29 +994,29 @@ discard block |
||
994 | 994 | assert(isset($flag_name, $known_names)); |
995 | 995 | assert( |
996 | 996 | in_array($flag_value, $flags[$flag_name], true) || |
997 | - ($flag_name == 'max_predicted_time' && is_int($flag_value) && $flag_value >= 0) |
|
997 | + ($flag_name=='max_predicted_time' && is_int($flag_value) && $flag_value>=0) |
|
998 | 998 | ); |
999 | 999 | |
1000 | 1000 | switch ($flag_name) { |
1001 | 1001 | case 'reverse_scan': |
1002 | - $this->query_flags = sphSetBit($this->query_flags, 0, $flag_value == 1); |
|
1002 | + $this->query_flags = sphSetBit($this->query_flags, 0, $flag_value==1); |
|
1003 | 1003 | break; |
1004 | 1004 | case 'sort_method': |
1005 | - $this->query_flags = sphSetBit($this->query_flags, 1, $flag_value == 'kbuffer'); |
|
1005 | + $this->query_flags = sphSetBit($this->query_flags, 1, $flag_value=='kbuffer'); |
|
1006 | 1006 | break; |
1007 | 1007 | case 'max_predicted_time': |
1008 | - $this->query_flags = sphSetBit($this->query_flags, 2, $flag_value > 0); |
|
1008 | + $this->query_flags = sphSetBit($this->query_flags, 2, $flag_value>0); |
|
1009 | 1009 | $this->predicted_time = (int)$flag_value; |
1010 | 1010 | break; |
1011 | 1011 | case 'boolean_simplify': |
1012 | 1012 | $this->query_flags = sphSetBit($this->query_flags, 3, $flag_value); |
1013 | 1013 | break; |
1014 | 1014 | case 'idf': |
1015 | - if ($flag_value == 'normalized' || $flag_value == 'plain') { |
|
1016 | - $this->query_flags = sphSetBit($this->query_flags, 4, $flag_value == 'plain'); |
|
1015 | + if ($flag_value=='normalized' || $flag_value=='plain') { |
|
1016 | + $this->query_flags = sphSetBit($this->query_flags, 4, $flag_value=='plain'); |
|
1017 | 1017 | } |
1018 | - if ($flag_value == 'tfidf_normalized' || $flag_value == 'tfidf_unnormalized') { |
|
1019 | - $this->query_flags = sphSetBit($this->query_flags, 6, $flag_value == 'tfidf_normalized'); |
|
1018 | + if ($flag_value=='tfidf_normalized' || $flag_value=='tfidf_unnormalized') { |
|
1019 | + $this->query_flags = sphSetBit($this->query_flags, 6, $flag_value=='tfidf_normalized'); |
|
1020 | 1020 | } |
1021 | 1021 | break; |
1022 | 1022 | case 'global_idf': |
@@ -1034,8 +1034,8 @@ discard block |
||
1034 | 1034 | assert(is_string($orderby)); |
1035 | 1035 | assert(is_int($offset)); |
1036 | 1036 | assert(is_int($limit)); |
1037 | - assert($offset >= 0); |
|
1038 | - assert($limit > 0); |
|
1037 | + assert($offset>=0); |
|
1038 | + assert($limit>0); |
|
1039 | 1039 | |
1040 | 1040 | $this->outer_order_by = $orderby; |
1041 | 1041 | $this->outer_offset = $offset; |
@@ -1100,7 +1100,7 @@ discard block |
||
1100 | 1100 | |
1101 | 1101 | $this->error = $results[0]['error']; |
1102 | 1102 | $this->warning = $results[0]['warning']; |
1103 | - if ($results[0]['status'] == SEARCHD_ERROR) { |
|
1103 | + if ($results[0]['status']==SEARCHD_ERROR) { |
|
1104 | 1104 | return false; |
1105 | 1105 | } else { |
1106 | 1106 | return $results[0]; |
@@ -1124,24 +1124,24 @@ discard block |
||
1124 | 1124 | |
1125 | 1125 | // build request |
1126 | 1126 | $req = pack('NNNNN', $this->query_flags, $this->offset, $this->limit, $this->mode, $this->ranker); |
1127 | - if ($this->ranker == SPH_RANK_EXPR) { |
|
1128 | - $req .= pack('N', strlen($this->rank_expr)) . $this->rank_expr; |
|
1127 | + if ($this->ranker==SPH_RANK_EXPR) { |
|
1128 | + $req .= pack('N', strlen($this->rank_expr)).$this->rank_expr; |
|
1129 | 1129 | } |
1130 | 1130 | $req .= pack('N', $this->sort); // (deprecated) sort mode |
1131 | - $req .= pack('N', strlen($this->sort_by)) . $this->sort_by; |
|
1132 | - $req .= pack('N', strlen($query)) . $query; // query itself |
|
1131 | + $req .= pack('N', strlen($this->sort_by)).$this->sort_by; |
|
1132 | + $req .= pack('N', strlen($query)).$query; // query itself |
|
1133 | 1133 | $req .= pack('N', count($this->weights)); // weights |
1134 | 1134 | foreach ($this->weights as $weight) { |
1135 | 1135 | $req .= pack('N', (int)$weight); |
1136 | 1136 | } |
1137 | - $req .= pack('N', strlen($index)) . $index; // indexes |
|
1137 | + $req .= pack('N', strlen($index)).$index; // indexes |
|
1138 | 1138 | $req .= pack('N', 1); // id64 range marker |
1139 | - $req .= sphPackU64($this->min_id) . sphPackU64($this->max_id); // id64 range |
|
1139 | + $req .= sphPackU64($this->min_id).sphPackU64($this->max_id); // id64 range |
|
1140 | 1140 | |
1141 | 1141 | // filters |
1142 | 1142 | $req .= pack('N', count($this->filters)); |
1143 | 1143 | foreach ($this->filters as $filter) { |
1144 | - $req .= pack('N', strlen($filter['attr'])) . $filter['attr']; |
|
1144 | + $req .= pack('N', strlen($filter['attr'])).$filter['attr']; |
|
1145 | 1145 | $req .= pack('N', $filter['type']); |
1146 | 1146 | switch ($filter['type']) { |
1147 | 1147 | case SPH_FILTER_VALUES: |
@@ -1151,13 +1151,13 @@ discard block |
||
1151 | 1151 | } |
1152 | 1152 | break; |
1153 | 1153 | case SPH_FILTER_RANGE: |
1154 | - $req .= sphPackI64($filter['min']) . sphPackI64($filter['max']); |
|
1154 | + $req .= sphPackI64($filter['min']).sphPackI64($filter['max']); |
|
1155 | 1155 | break; |
1156 | 1156 | case SPH_FILTER_FLOATRANGE: |
1157 | - $req .= $this->packFloat($filter['min']) . $this->packFloat($filter['max']); |
|
1157 | + $req .= $this->packFloat($filter['min']).$this->packFloat($filter['max']); |
|
1158 | 1158 | break; |
1159 | 1159 | case SPH_FILTER_STRING: |
1160 | - $req .= pack('N', strlen($filter['value'])) . $filter['value']; |
|
1160 | + $req .= pack('N', strlen($filter['value'])).$filter['value']; |
|
1161 | 1161 | break; |
1162 | 1162 | default: |
1163 | 1163 | assert(0 && 'internal error: unhandled filter type'); |
@@ -1166,27 +1166,27 @@ discard block |
||
1166 | 1166 | } |
1167 | 1167 | |
1168 | 1168 | // group-by clause, max-matches count, group-sort clause, cutoff count |
1169 | - $req .= pack('NN', $this->group_func, strlen($this->group_by)) . $this->group_by; |
|
1169 | + $req .= pack('NN', $this->group_func, strlen($this->group_by)).$this->group_by; |
|
1170 | 1170 | $req .= pack('N', $this->max_matches); |
1171 | - $req .= pack('N', strlen($this->group_sort)) . $this->group_sort; |
|
1171 | + $req .= pack('N', strlen($this->group_sort)).$this->group_sort; |
|
1172 | 1172 | $req .= pack('NNN', $this->cutoff, $this->retry_count, $this->retry_delay); |
1173 | - $req .= pack('N', strlen($this->group_distinct)) . $this->group_distinct; |
|
1173 | + $req .= pack('N', strlen($this->group_distinct)).$this->group_distinct; |
|
1174 | 1174 | |
1175 | 1175 | // anchor point |
1176 | 1176 | if (empty($this->anchor)) { |
1177 | 1177 | $req .= pack('N', 0); |
1178 | 1178 | } else { |
1179 | - $a =& $this->anchor; |
|
1179 | + $a = & $this->anchor; |
|
1180 | 1180 | $req .= pack('N', 1); |
1181 | - $req .= pack('N', strlen($a['attrlat'])) . $a['attrlat']; |
|
1182 | - $req .= pack('N', strlen($a['attrlong'])) . $a['attrlong']; |
|
1183 | - $req .= $this->packFloat($a['lat']) . $this->packFloat($a['long']); |
|
1181 | + $req .= pack('N', strlen($a['attrlat'])).$a['attrlat']; |
|
1182 | + $req .= pack('N', strlen($a['attrlong'])).$a['attrlong']; |
|
1183 | + $req .= $this->packFloat($a['lat']).$this->packFloat($a['long']); |
|
1184 | 1184 | } |
1185 | 1185 | |
1186 | 1186 | // per-index weights |
1187 | 1187 | $req .= pack('N', count($this->index_weights)); |
1188 | 1188 | foreach ($this->index_weights as $idx => $weight) { |
1189 | - $req .= pack('N', strlen($idx)) . $idx . pack('N', $weight); |
|
1189 | + $req .= pack('N', strlen($idx)).$idx.pack('N', $weight); |
|
1190 | 1190 | } |
1191 | 1191 | |
1192 | 1192 | // max query time |
@@ -1195,16 +1195,16 @@ discard block |
||
1195 | 1195 | // per-field weights |
1196 | 1196 | $req .= pack('N', count($this->field_weights)); |
1197 | 1197 | foreach ($this->field_weights as $field => $weight) { |
1198 | - $req .= pack('N', strlen($field)) . $field . pack('N', $weight); |
|
1198 | + $req .= pack('N', strlen($field)).$field.pack('N', $weight); |
|
1199 | 1199 | } |
1200 | 1200 | |
1201 | 1201 | // comment |
1202 | - $req .= pack('N', strlen($comment)) . $comment; |
|
1202 | + $req .= pack('N', strlen($comment)).$comment; |
|
1203 | 1203 | |
1204 | 1204 | // attribute overrides |
1205 | 1205 | $req .= pack('N', count($this->overrides)); |
1206 | 1206 | foreach ($this->overrides as $key => $entry) { |
1207 | - $req .= pack('N', strlen($entry['attr'])) . $entry['attr']; |
|
1207 | + $req .= pack('N', strlen($entry['attr'])).$entry['attr']; |
|
1208 | 1208 | $req .= pack('NN', $entry['type'], count($entry['values'])); |
1209 | 1209 | foreach ($entry['values'] as $id => $val) { |
1210 | 1210 | assert(is_numeric($id)); |
@@ -1226,14 +1226,14 @@ discard block |
||
1226 | 1226 | } |
1227 | 1227 | |
1228 | 1228 | // select-list |
1229 | - $req .= pack('N', strlen($this->select)) . $this->select; |
|
1229 | + $req .= pack('N', strlen($this->select)).$this->select; |
|
1230 | 1230 | |
1231 | 1231 | // max_predicted_time |
1232 | - if ($this->predicted_time > 0) { |
|
1232 | + if ($this->predicted_time>0) { |
|
1233 | 1233 | $req .= pack('N', (int)$this->predicted_time); |
1234 | 1234 | } |
1235 | 1235 | |
1236 | - $req .= pack('N', strlen($this->outer_order_by)) . $this->outer_order_by; |
|
1236 | + $req .= pack('N', strlen($this->outer_order_by)).$this->outer_order_by; |
|
1237 | 1237 | $req .= pack('NN', $this->outer_offset, $this->outer_limit); |
1238 | 1238 | if ($this->has_outer) { |
1239 | 1239 | $req .= pack('N', 1); |
@@ -1269,7 +1269,7 @@ discard block |
||
1269 | 1269 | $nreqs = count($this->reqs); |
1270 | 1270 | $req = join('', $this->reqs); |
1271 | 1271 | $len = 8 + strlen($req); |
1272 | - $req = pack('nnNNN', SEARCHD_COMMAND_SEARCH, VER_COMMAND_SEARCH, $len, 0, $nreqs) . $req; // add header |
|
1272 | + $req = pack('nnNNN', SEARCHD_COMMAND_SEARCH, VER_COMMAND_SEARCH, $len, 0, $nreqs).$req; // add header |
|
1273 | 1273 | |
1274 | 1274 | if (!$this->send($fp, $req, $len + 8) || !($response = $this->getResponse($fp, VER_COMMAND_SEARCH))) { |
1275 | 1275 | $this->mbPop(); |
@@ -1290,9 +1290,9 @@ discard block |
||
1290 | 1290 | $max = strlen($response); // max position for checks, to protect against broken responses |
1291 | 1291 | |
1292 | 1292 | $results = array(); |
1293 | - for ($ires = 0; $ires < $nreqs && $p < $max; $ires++) { |
|
1293 | + for ($ires = 0; $ires<$nreqs && $p<$max; $ires++) { |
|
1294 | 1294 | $results[] = array(); |
1295 | - $result =& $results[$ires]; |
|
1295 | + $result = & $results[$ires]; |
|
1296 | 1296 | |
1297 | 1297 | $result['error'] = ''; |
1298 | 1298 | $result['warning'] = ''; |
@@ -1301,13 +1301,13 @@ discard block |
||
1301 | 1301 | list(, $status) = unpack('N*', substr($response, $p, 4)); |
1302 | 1302 | $p += 4; |
1303 | 1303 | $result['status'] = $status; |
1304 | - if ($status != SEARCHD_OK) { |
|
1304 | + if ($status!=SEARCHD_OK) { |
|
1305 | 1305 | list(, $len) = unpack('N*', substr($response, $p, 4)); |
1306 | 1306 | $p += 4; |
1307 | 1307 | $message = substr($response, $p, $len); |
1308 | 1308 | $p += $len; |
1309 | 1309 | |
1310 | - if ($status == SEARCHD_WARNING) { |
|
1310 | + if ($status==SEARCHD_WARNING) { |
|
1311 | 1311 | $result['warning'] = $message; |
1312 | 1312 | } else { |
1313 | 1313 | $result['error'] = $message; |
@@ -1321,7 +1321,7 @@ discard block |
||
1321 | 1321 | |
1322 | 1322 | list(, $nfields) = unpack('N*', substr($response, $p, 4)); |
1323 | 1323 | $p += 4; |
1324 | - while ($nfields --> 0 && $p < $max) { |
|
1324 | + while ($nfields-->0 && $p<$max) { |
|
1325 | 1325 | list(, $len) = unpack('N*', substr($response, $p, 4)); |
1326 | 1326 | $p += 4; |
1327 | 1327 | $fields[] = substr($response, $p, $len); |
@@ -1331,7 +1331,7 @@ discard block |
||
1331 | 1331 | |
1332 | 1332 | list(, $nattrs) = unpack('N*', substr($response, $p, 4)); |
1333 | 1333 | $p += 4; |
1334 | - while ($nattrs --> 0 && $p < $max) { |
|
1334 | + while ($nattrs-->0 && $p<$max) { |
|
1335 | 1335 | list(, $len) = unpack('N*', substr($response, $p, 4)); |
1336 | 1336 | $p += 4; |
1337 | 1337 | $attr = substr($response, $p, $len); |
@@ -1350,7 +1350,7 @@ discard block |
||
1350 | 1350 | |
1351 | 1351 | // read matches |
1352 | 1352 | $idx = -1; |
1353 | - while ($count --> 0 && $p < $max) { |
|
1353 | + while ($count-->0 && $p<$max) { |
|
1354 | 1354 | // index into result array |
1355 | 1355 | $idx++; |
1356 | 1356 | |
@@ -1378,14 +1378,14 @@ discard block |
||
1378 | 1378 | $attrvals = array(); |
1379 | 1379 | foreach ($attrs as $attr => $type) { |
1380 | 1380 | // handle 64bit ints |
1381 | - if ($type == SPH_ATTR_BIGINT) { |
|
1381 | + if ($type==SPH_ATTR_BIGINT) { |
|
1382 | 1382 | $attrvals[$attr] = sphUnpackI64(substr($response, $p, 8)); |
1383 | 1383 | $p += 8; |
1384 | 1384 | continue; |
1385 | 1385 | } |
1386 | 1386 | |
1387 | 1387 | // handle floats |
1388 | - if ($type == SPH_ATTR_FLOAT) { |
|
1388 | + if ($type==SPH_ATTR_FLOAT) { |
|
1389 | 1389 | list(, $uval) = unpack('N*', substr($response, $p, 4)); |
1390 | 1390 | $p += 4; |
1391 | 1391 | list(, $fval) = unpack('f*', pack('L', $uval)); |
@@ -1396,28 +1396,28 @@ discard block |
||
1396 | 1396 | // handle everything else as unsigned ints |
1397 | 1397 | list(, $val) = unpack('N*', substr($response, $p, 4)); |
1398 | 1398 | $p += 4; |
1399 | - if ($type == SPH_ATTR_MULTI) { |
|
1399 | + if ($type==SPH_ATTR_MULTI) { |
|
1400 | 1400 | $attrvals[$attr] = array(); |
1401 | 1401 | $nvalues = $val; |
1402 | - while ($nvalues --> 0 && $p < $max) { |
|
1402 | + while ($nvalues-->0 && $p<$max) { |
|
1403 | 1403 | list(, $val) = unpack('N*', substr($response, $p, 4)); |
1404 | 1404 | $p += 4; |
1405 | 1405 | $attrvals[$attr][] = sphFixUint($val); |
1406 | 1406 | } |
1407 | - } elseif ($type == SPH_ATTR_MULTI64) { |
|
1407 | + } elseif ($type==SPH_ATTR_MULTI64) { |
|
1408 | 1408 | $attrvals[$attr] = array(); |
1409 | 1409 | $nvalues = $val; |
1410 | - while ($nvalues > 0 && $p < $max) { |
|
1410 | + while ($nvalues>0 && $p<$max) { |
|
1411 | 1411 | $attrvals[$attr][] = sphUnpackI64(substr($response, $p, 8)); |
1412 | 1412 | $p += 8; |
1413 | 1413 | $nvalues -= 2; |
1414 | 1414 | } |
1415 | - } elseif ($type == SPH_ATTR_STRING) { |
|
1415 | + } elseif ($type==SPH_ATTR_STRING) { |
|
1416 | 1416 | $attrvals[$attr] = substr($response, $p, $val); |
1417 | 1417 | $p += $val; |
1418 | - } elseif ($type == SPH_ATTR_FACTORS) { |
|
1418 | + } elseif ($type==SPH_ATTR_FACTORS) { |
|
1419 | 1419 | $attrvals[$attr] = substr($response, $p, $val - 4); |
1420 | - $p += $val-4; |
|
1420 | + $p += $val - 4; |
|
1421 | 1421 | } else { |
1422 | 1422 | $attrvals[$attr] = sphFixUint($val); |
1423 | 1423 | } |
@@ -1436,14 +1436,14 @@ discard block |
||
1436 | 1436 | $result['time'] = sprintf('%.3f', $msecs / 1000); |
1437 | 1437 | $p += 16; |
1438 | 1438 | |
1439 | - while ($words --> 0 && $p < $max) { |
|
1439 | + while ($words-->0 && $p<$max) { |
|
1440 | 1440 | list(, $len) = unpack('N*', substr($response, $p, 4)); |
1441 | 1441 | $p += 4; |
1442 | 1442 | $word = substr($response, $p, $len); |
1443 | 1443 | $p += $len; |
1444 | 1444 | list($docs, $hits) = array_values(unpack('N*N*', substr($response, $p, 8))); |
1445 | 1445 | $p += 8; |
1446 | - $result['words'][$word] = array ( |
|
1446 | + $result['words'][$word] = array( |
|
1447 | 1447 | 'docs' => sprintf('%u', $docs), |
1448 | 1448 | 'hits' => sprintf('%u', $hits) |
1449 | 1449 | ); |
@@ -1578,23 +1578,23 @@ discard block |
||
1578 | 1578 | $flags |= 1024; |
1579 | 1579 | } |
1580 | 1580 | $req = pack('NN', 0, $flags); // mode=0, flags=$flags |
1581 | - $req .= pack('N', strlen($index)) . $index; // req index |
|
1582 | - $req .= pack('N', strlen($words)) . $words; // req words |
|
1581 | + $req .= pack('N', strlen($index)).$index; // req index |
|
1582 | + $req .= pack('N', strlen($words)).$words; // req words |
|
1583 | 1583 | |
1584 | 1584 | // options |
1585 | - $req .= pack('N', strlen($opts['before_match'])) . $opts['before_match']; |
|
1586 | - $req .= pack('N', strlen($opts['after_match'])) . $opts['after_match']; |
|
1587 | - $req .= pack('N', strlen($opts['chunk_separator'])) . $opts['chunk_separator']; |
|
1585 | + $req .= pack('N', strlen($opts['before_match'])).$opts['before_match']; |
|
1586 | + $req .= pack('N', strlen($opts['after_match'])).$opts['after_match']; |
|
1587 | + $req .= pack('N', strlen($opts['chunk_separator'])).$opts['chunk_separator']; |
|
1588 | 1588 | $req .= pack('NN', (int)$opts['limit'], (int)$opts['around']); |
1589 | 1589 | $req .= pack('NNN', (int)$opts['limit_passages'], (int)$opts['limit_words'], (int)$opts['start_passage_id']); // v.1.2 |
1590 | - $req .= pack('N', strlen($opts['html_strip_mode'])) . $opts['html_strip_mode']; |
|
1591 | - $req .= pack('N', strlen($opts['passage_boundary'])) . $opts['passage_boundary']; |
|
1590 | + $req .= pack('N', strlen($opts['html_strip_mode'])).$opts['html_strip_mode']; |
|
1591 | + $req .= pack('N', strlen($opts['passage_boundary'])).$opts['passage_boundary']; |
|
1592 | 1592 | |
1593 | 1593 | // documents |
1594 | 1594 | $req .= pack('N', count($docs)); |
1595 | 1595 | foreach ($docs as $doc) { |
1596 | 1596 | assert(is_string($doc)); |
1597 | - $req .= pack('N', strlen($doc)) . $doc; |
|
1597 | + $req .= pack('N', strlen($doc)).$doc; |
|
1598 | 1598 | } |
1599 | 1599 | |
1600 | 1600 | //////////////////////////// |
@@ -1602,7 +1602,7 @@ discard block |
||
1602 | 1602 | //////////////////////////// |
1603 | 1603 | |
1604 | 1604 | $len = strlen($req); |
1605 | - $req = pack('nnN', SEARCHD_COMMAND_EXCERPT, VER_COMMAND_EXCERPT, $len) . $req; // add header |
|
1605 | + $req = pack('nnN', SEARCHD_COMMAND_EXCERPT, VER_COMMAND_EXCERPT, $len).$req; // add header |
|
1606 | 1606 | if (!$this->send($fp, $req, $len + 8) || !($response = $this->getResponse($fp, VER_COMMAND_EXCERPT))) { |
1607 | 1607 | $this->mbPop(); |
1608 | 1608 | return false; |
@@ -1620,7 +1620,7 @@ discard block |
||
1620 | 1620 | list(, $len) = unpack('N*', substr($response, $pos, 4)); |
1621 | 1621 | $pos += 4; |
1622 | 1622 | |
1623 | - if ($pos + $len > $rlen) { |
|
1623 | + if ($pos + $len>$rlen) { |
|
1624 | 1624 | $this->error = 'incomplete reply'; |
1625 | 1625 | $this->mbPop(); |
1626 | 1626 | return false; |
@@ -1659,8 +1659,8 @@ discard block |
||
1659 | 1659 | ///////////////// |
1660 | 1660 | |
1661 | 1661 | // v.1.0 req |
1662 | - $req = pack('N', strlen($query)) . $query; // req query |
|
1663 | - $req .= pack('N', strlen($index)) . $index; // req index |
|
1662 | + $req = pack('N', strlen($query)).$query; // req query |
|
1663 | + $req .= pack('N', strlen($index)).$index; // req index |
|
1664 | 1664 | $req .= pack('N', (int)$hits); |
1665 | 1665 | |
1666 | 1666 | //////////////////////////// |
@@ -1668,7 +1668,7 @@ discard block |
||
1668 | 1668 | //////////////////////////// |
1669 | 1669 | |
1670 | 1670 | $len = strlen($req); |
1671 | - $req = pack('nnN', SEARCHD_COMMAND_KEYWORDS, VER_COMMAND_KEYWORDS, $len) . $req; // add header |
|
1671 | + $req = pack('nnN', SEARCHD_COMMAND_KEYWORDS, VER_COMMAND_KEYWORDS, $len).$req; // add header |
|
1672 | 1672 | if (!$this->send($fp, $req, $len + 8) || !($response = $this->getResponse($fp, VER_COMMAND_KEYWORDS))) { |
1673 | 1673 | $this->mbPop(); |
1674 | 1674 | return false; |
@@ -1683,7 +1683,7 @@ discard block |
||
1683 | 1683 | $rlen = strlen($response); |
1684 | 1684 | list(, $nwords) = unpack('N*', substr($response, $pos, 4)); |
1685 | 1685 | $pos += 4; |
1686 | - for ($i = 0; $i < $nwords; $i++) { |
|
1686 | + for ($i = 0; $i<$nwords; $i++) { |
|
1687 | 1687 | list(, $len) = unpack('N*', substr($response, $pos, 4)); |
1688 | 1688 | $pos += 4; |
1689 | 1689 | $tokenized = $len ? substr($response, $pos, $len) : ''; |
@@ -1706,7 +1706,7 @@ discard block |
||
1706 | 1706 | $res[$i]['hits'] = $nhits; |
1707 | 1707 | } |
1708 | 1708 | |
1709 | - if ($pos > $rlen) { |
|
1709 | + if ($pos>$rlen) { |
|
1710 | 1710 | $this->error = 'incomplete reply'; |
1711 | 1711 | $this->mbPop(); |
1712 | 1712 | return false; |
@@ -1719,8 +1719,8 @@ discard block |
||
1719 | 1719 | |
1720 | 1720 | public function escapeString($string) |
1721 | 1721 | { |
1722 | - $from = array('\\', '(',')','|','-','!','@','~','"','&', '/', '^', '$', '=', '<'); |
|
1723 | - $to = array('\\\\', '\(','\)','\|','\-','\!','\@','\~','\"', '\&', '\/', '\^', '\$', '\=', '\<'); |
|
1722 | + $from = array('\\', '(', ')', '|', '-', '!', '@', '~', '"', '&', '/', '^', '$', '=', '<'); |
|
1723 | + $to = array('\\\\', '\(', '\)', '\|', '\-', '\!', '\@', '\~', '\"', '\&', '\/', '\^', '\$', '\=', '\<'); |
|
1724 | 1724 | |
1725 | 1725 | return str_replace($from, $to, $string); |
1726 | 1726 | } |
@@ -1747,7 +1747,7 @@ discard block |
||
1747 | 1747 | foreach ($values as $id => $entry) { |
1748 | 1748 | assert(is_numeric($id)); |
1749 | 1749 | assert(is_array($entry)); |
1750 | - assert(count($entry) == count($attrs)); |
|
1750 | + assert(count($entry)==count($attrs)); |
|
1751 | 1751 | foreach ($entry as $v) { |
1752 | 1752 | if ($mva) { |
1753 | 1753 | assert(is_array($v)); |
@@ -1762,12 +1762,12 @@ discard block |
||
1762 | 1762 | |
1763 | 1763 | // build request |
1764 | 1764 | $this->mbPush(); |
1765 | - $req = pack('N', strlen($index)) . $index; |
|
1765 | + $req = pack('N', strlen($index)).$index; |
|
1766 | 1766 | |
1767 | 1767 | $req .= pack('N', count($attrs)); |
1768 | 1768 | $req .= pack('N', $ignorenonexistent ? 1 : 0); |
1769 | 1769 | foreach ($attrs as $attr) { |
1770 | - $req .= pack('N', strlen($attr)) . $attr; |
|
1770 | + $req .= pack('N', strlen($attr)).$attr; |
|
1771 | 1771 | $req .= pack('N', $mva ? 1 : 0); |
1772 | 1772 | } |
1773 | 1773 | |
@@ -1791,7 +1791,7 @@ discard block |
||
1791 | 1791 | } |
1792 | 1792 | |
1793 | 1793 | $len = strlen($req); |
1794 | - $req = pack('nnN', SEARCHD_COMMAND_UPDATE, VER_COMMAND_UPDATE, $len) . $req; // add header |
|
1794 | + $req = pack('nnN', SEARCHD_COMMAND_UPDATE, VER_COMMAND_UPDATE, $len).$req; // add header |
|
1795 | 1795 | if (!$this->send($fp, $req, $len + 8)) { |
1796 | 1796 | $this->mbPop(); |
1797 | 1797 | return -1; |
@@ -1814,7 +1814,7 @@ discard block |
||
1814 | 1814 | |
1815 | 1815 | public function open() |
1816 | 1816 | { |
1817 | - if ($this->socket !== false) { |
|
1817 | + if ($this->socket!==false) { |
|
1818 | 1818 | $this->error = 'already connected'; |
1819 | 1819 | return false; |
1820 | 1820 | } |
@@ -1833,7 +1833,7 @@ discard block |
||
1833 | 1833 | |
1834 | 1834 | public function close() |
1835 | 1835 | { |
1836 | - if ($this->socket === false) { |
|
1836 | + if ($this->socket===false) { |
|
1837 | 1837 | $this->error = 'not connected'; |
1838 | 1838 | return false; |
1839 | 1839 | } |
@@ -1870,8 +1870,8 @@ discard block |
||
1870 | 1870 | $p += 8; |
1871 | 1871 | |
1872 | 1872 | $res = array(); |
1873 | - for ($i = 0; $i < $rows; $i++) { |
|
1874 | - for ($j = 0; $j < $cols; $j++) { |
|
1873 | + for ($i = 0; $i<$rows; $i++) { |
|
1874 | + for ($j = 0; $j<$cols; $j++) { |
|
1875 | 1875 | list(, $len) = unpack('N*', substr($response, $p, 4)); |
1876 | 1876 | $p += 4; |
1877 | 1877 | $res[$i][] = substr($response, $p, $len); |
@@ -1902,7 +1902,7 @@ discard block |
||
1902 | 1902 | } |
1903 | 1903 | |
1904 | 1904 | $tag = -1; |
1905 | - if (strlen($response) == 4) { |
|
1905 | + if (strlen($response)==4) { |
|
1906 | 1906 | list(, $tag) = unpack('N*', $response); |
1907 | 1907 | } else { |
1908 | 1908 | $this->error = 'unexpected response length'; |
@@ -7,7 +7,7 @@ discard block |
||
7 | 7 | $file = __DIR__.'/../vendor/autoload.php'; |
8 | 8 | |
9 | 9 | if (!file_exists($file)) { |
10 | - throw new RuntimeException('Install dependencies to run test suite. "php composer.phar install --dev"'); |
|
10 | + throw new RuntimeException('Install dependencies to run test suite. "php composer.phar install --dev"'); |
|
11 | 11 | } |
12 | 12 | |
13 | 13 | require_once __DIR__.'/../vendor/autoload.php'; |
@@ -18,13 +18,13 @@ discard block |
||
18 | 18 | |
19 | 19 | // for very old PHP versions, like at my home test server |
20 | 20 | if (is_array($argv) && !isset($_SERVER['argv'])) { |
21 | - $_SERVER['argv'] = $argv; |
|
21 | + $_SERVER['argv'] = $argv; |
|
22 | 22 | } |
23 | 23 | unset($_SERVER['argv'][0]); |
24 | 24 | |
25 | 25 | // build query |
26 | 26 | if (!is_array($_SERVER['argv']) || empty($_SERVER['argv'])) { |
27 | - print <<<EOF |
|
27 | + print <<<EOF |
|
28 | 28 | Usage: php -f test.php [OPTIONS] query words |
29 | 29 | |
30 | 30 | Options are: |
@@ -49,199 +49,199 @@ discard block |
||
49 | 49 | EOF; |
50 | 50 | } else { |
51 | 51 | |
52 | - $args = array(); |
|
53 | - foreach ($_SERVER['argv'] as $arg) { |
|
54 | - $args[] = $arg; |
|
55 | - } |
|
56 | - |
|
57 | - $cl = new SphinxClient(); |
|
58 | - |
|
59 | - $q = ''; |
|
60 | - $sql = ''; |
|
61 | - $mode = SPH_MATCH_ALL; |
|
62 | - $host = 'localhost'; |
|
63 | - $port = 9312; |
|
64 | - $index = '*'; |
|
65 | - $groupby = ''; |
|
66 | - $groupsort = '@group desc'; |
|
67 | - $filter = 'group_id'; |
|
68 | - $filtervals = array(); |
|
69 | - $distinct = ''; |
|
70 | - $sortby = ''; |
|
71 | - $sortexpr = ''; |
|
72 | - $limit = 20; |
|
73 | - $ranker = SPH_RANK_PROXIMITY_BM25; |
|
74 | - $select = ''; |
|
75 | - $count = count($args); |
|
76 | - |
|
77 | - for ($i = 0; $i < $count; $i++) { |
|
78 | - switch ($args[$i]) { |
|
79 | - case '-h': |
|
80 | - case '--host': |
|
81 | - $host = $args[++$i]; |
|
82 | - break; |
|
83 | - case '-p': |
|
84 | - case '--port': |
|
85 | - $port = (int)$args[++$i]; |
|
86 | - break; |
|
87 | - case '-i': |
|
88 | - case '--index': |
|
89 | - $index = $args[++$i]; |
|
90 | - break; |
|
91 | - case '-s': |
|
92 | - case '--sortby': |
|
93 | - $sortby = $args[++$i]; |
|
94 | - $sortexpr = ''; |
|
95 | - break; |
|
96 | - case '-S': |
|
97 | - case '--sortexpr': |
|
98 | - $sortexpr = $args[++$i]; |
|
99 | - $sortby = ''; |
|
100 | - break; |
|
101 | - case '-a': |
|
102 | - case '--any': |
|
103 | - $mode = SPH_MATCH_ANY; |
|
104 | - break; |
|
105 | - case '-b': |
|
106 | - case '--boolean': |
|
107 | - $mode = SPH_MATCH_BOOLEAN; |
|
108 | - break; |
|
109 | - case '-e': |
|
110 | - case '--extended': |
|
111 | - $mode = SPH_MATCH_EXTENDED; |
|
112 | - break; |
|
113 | - case '-e2': |
|
114 | - $mode = SPH_MATCH_EXTENDED2; |
|
115 | - break; |
|
116 | - case '-ph': |
|
117 | - case '--phrase': |
|
118 | - $mode = SPH_MATCH_PHRASE; |
|
119 | - break; |
|
120 | - case '-f': |
|
121 | - case '--filter': |
|
122 | - $filter = $args[++$i]; |
|
123 | - break; |
|
124 | - case '-v': |
|
125 | - case '--value': |
|
126 | - $filtervals[] = $args[++$i]; |
|
127 | - break; |
|
128 | - case '-g': |
|
129 | - case '--groupby': |
|
130 | - $groupby = $args[++$i]; |
|
131 | - break; |
|
132 | - case '-gs': |
|
133 | - case '--groupsort': |
|
134 | - $groupsort = $args[++$i]; |
|
135 | - break; |
|
136 | - case '-d': |
|
137 | - case '--distinct': |
|
138 | - $distinct = $args[++$i]; |
|
139 | - break; |
|
140 | - case '-l': |
|
141 | - case '--limit': |
|
142 | - $limit = (int)$args[++$i]; |
|
143 | - break; |
|
144 | - case '--select': |
|
145 | - $select = $args[++$i]; |
|
146 | - break; |
|
147 | - case '-fr': |
|
148 | - case '--filterrange': |
|
149 | - $cl->setFilterRange($args[++$i], $args[++$i], $args[++$i]); |
|
150 | - break; |
|
151 | - case '-r': |
|
152 | - switch (strtolower($args[++$i])) { |
|
153 | - case 'bm25': |
|
154 | - $ranker = SPH_RANK_BM25; |
|
155 | - break; |
|
156 | - case 'none': |
|
157 | - $ranker = SPH_RANK_NONE; |
|
158 | - break; |
|
159 | - case 'wordcount': |
|
160 | - $ranker = SPH_RANK_WORDCOUNT; |
|
161 | - break; |
|
162 | - case 'fieldmask': |
|
163 | - $ranker = SPH_RANK_FIELDMASK; |
|
164 | - break; |
|
165 | - case 'sph04': |
|
166 | - $ranker = SPH_RANK_SPH04; |
|
167 | - break; |
|
168 | - } |
|
169 | - break; |
|
170 | - default: |
|
171 | - $q .= $args[$i] . ' '; |
|
172 | - } |
|
173 | - } |
|
174 | - |
|
175 | - //////////// |
|
176 | - // do query |
|
177 | - //////////// |
|
178 | - |
|
179 | - $cl->setServer($host, $port); |
|
180 | - $cl->setConnectTimeout(1); |
|
181 | - $cl->setArrayResult(true); |
|
182 | - $cl->setMatchMode($mode); |
|
183 | - if (count($filtervals)) { |
|
184 | - $cl->setFilter($filter, $filtervals); |
|
185 | - } |
|
186 | - if ($groupby) { |
|
187 | - $cl->setGroupBy($groupby, SPH_GROUPBY_ATTR, $groupsort); |
|
188 | - } |
|
189 | - if ($sortby) { |
|
190 | - $cl->setSortMode(SPH_SORT_EXTENDED, $sortby); |
|
191 | - } |
|
192 | - if ($sortexpr) { |
|
193 | - $cl->setSortMode(SPH_SORT_EXPR, $sortexpr); |
|
194 | - } |
|
195 | - if ($distinct) { |
|
196 | - $cl->setGroupDistinct($distinct); |
|
197 | - } |
|
198 | - if ($select) { |
|
199 | - $cl->setSelect($select); |
|
200 | - } |
|
201 | - if ($limit) { |
|
202 | - $cl->setLimits(0, $limit, ($limit > 1000) ? $limit : 1000); |
|
203 | - } |
|
204 | - $cl->setRankingMode($ranker); |
|
205 | - $res = $cl->query($q, $index); |
|
206 | - |
|
207 | - //////////////// |
|
208 | - // print me out |
|
209 | - //////////////// |
|
210 | - |
|
211 | - if ($res === false) { |
|
212 | - printf('Query failed: %s.' . PHP_EOL, $cl->getLastError()); |
|
213 | - |
|
214 | - } else { |
|
215 | - if ($cl->getLastWarning()) { |
|
216 | - printf('WARNING: %s' . PHP_EOL . PHP_EOL, $cl->getLastWarning()); |
|
217 | - } |
|
218 | - |
|
219 | - print "Query '$q' retrieved {$res['total']} of {$res['total_found']} matches in {$res['time']} sec.\n"; |
|
220 | - print 'Query stats:' . PHP_EOL; |
|
221 | - if (is_array($res['words'])) { |
|
222 | - foreach ($res['words'] as $word => $info) { |
|
223 | - print " '$word' found {$info['hits']} times in {$info['docs']} documents\n"; |
|
224 | - } |
|
225 | - } |
|
226 | - print PHP_EOL; |
|
227 | - |
|
228 | - if (is_array($res['matches'])) { |
|
229 | - $n = 1; |
|
230 | - print 'Matches:' . PHP_EOL; |
|
231 | - foreach ($res['matches'] as $docinfo) { |
|
232 | - print "$n. doc_id={$docinfo['id']}, weight={$docinfo['weight']}"; |
|
233 | - foreach ($res['attrs'] as $attrname => $attrtype) { |
|
234 | - $value = $docinfo['attrs'][$attrname]; |
|
235 | - if ($attrtype == SPH_ATTR_MULTI || $attrtype == SPH_ATTR_MULTI64) { |
|
236 | - $value = '(' . join(',', $value) . ')'; |
|
237 | - } elseif ($attrtype == SPH_ATTR_TIMESTAMP) { |
|
238 | - $value = date('Y-m-d H:i:s', $value); |
|
239 | - } |
|
240 | - print ", $attrname=$value"; |
|
241 | - } |
|
242 | - print PHP_EOL; |
|
243 | - $n++; |
|
244 | - } |
|
245 | - } |
|
246 | - } |
|
52 | + $args = array(); |
|
53 | + foreach ($_SERVER['argv'] as $arg) { |
|
54 | + $args[] = $arg; |
|
55 | + } |
|
56 | + |
|
57 | + $cl = new SphinxClient(); |
|
58 | + |
|
59 | + $q = ''; |
|
60 | + $sql = ''; |
|
61 | + $mode = SPH_MATCH_ALL; |
|
62 | + $host = 'localhost'; |
|
63 | + $port = 9312; |
|
64 | + $index = '*'; |
|
65 | + $groupby = ''; |
|
66 | + $groupsort = '@group desc'; |
|
67 | + $filter = 'group_id'; |
|
68 | + $filtervals = array(); |
|
69 | + $distinct = ''; |
|
70 | + $sortby = ''; |
|
71 | + $sortexpr = ''; |
|
72 | + $limit = 20; |
|
73 | + $ranker = SPH_RANK_PROXIMITY_BM25; |
|
74 | + $select = ''; |
|
75 | + $count = count($args); |
|
76 | + |
|
77 | + for ($i = 0; $i < $count; $i++) { |
|
78 | + switch ($args[$i]) { |
|
79 | + case '-h': |
|
80 | + case '--host': |
|
81 | + $host = $args[++$i]; |
|
82 | + break; |
|
83 | + case '-p': |
|
84 | + case '--port': |
|
85 | + $port = (int)$args[++$i]; |
|
86 | + break; |
|
87 | + case '-i': |
|
88 | + case '--index': |
|
89 | + $index = $args[++$i]; |
|
90 | + break; |
|
91 | + case '-s': |
|
92 | + case '--sortby': |
|
93 | + $sortby = $args[++$i]; |
|
94 | + $sortexpr = ''; |
|
95 | + break; |
|
96 | + case '-S': |
|
97 | + case '--sortexpr': |
|
98 | + $sortexpr = $args[++$i]; |
|
99 | + $sortby = ''; |
|
100 | + break; |
|
101 | + case '-a': |
|
102 | + case '--any': |
|
103 | + $mode = SPH_MATCH_ANY; |
|
104 | + break; |
|
105 | + case '-b': |
|
106 | + case '--boolean': |
|
107 | + $mode = SPH_MATCH_BOOLEAN; |
|
108 | + break; |
|
109 | + case '-e': |
|
110 | + case '--extended': |
|
111 | + $mode = SPH_MATCH_EXTENDED; |
|
112 | + break; |
|
113 | + case '-e2': |
|
114 | + $mode = SPH_MATCH_EXTENDED2; |
|
115 | + break; |
|
116 | + case '-ph': |
|
117 | + case '--phrase': |
|
118 | + $mode = SPH_MATCH_PHRASE; |
|
119 | + break; |
|
120 | + case '-f': |
|
121 | + case '--filter': |
|
122 | + $filter = $args[++$i]; |
|
123 | + break; |
|
124 | + case '-v': |
|
125 | + case '--value': |
|
126 | + $filtervals[] = $args[++$i]; |
|
127 | + break; |
|
128 | + case '-g': |
|
129 | + case '--groupby': |
|
130 | + $groupby = $args[++$i]; |
|
131 | + break; |
|
132 | + case '-gs': |
|
133 | + case '--groupsort': |
|
134 | + $groupsort = $args[++$i]; |
|
135 | + break; |
|
136 | + case '-d': |
|
137 | + case '--distinct': |
|
138 | + $distinct = $args[++$i]; |
|
139 | + break; |
|
140 | + case '-l': |
|
141 | + case '--limit': |
|
142 | + $limit = (int)$args[++$i]; |
|
143 | + break; |
|
144 | + case '--select': |
|
145 | + $select = $args[++$i]; |
|
146 | + break; |
|
147 | + case '-fr': |
|
148 | + case '--filterrange': |
|
149 | + $cl->setFilterRange($args[++$i], $args[++$i], $args[++$i]); |
|
150 | + break; |
|
151 | + case '-r': |
|
152 | + switch (strtolower($args[++$i])) { |
|
153 | + case 'bm25': |
|
154 | + $ranker = SPH_RANK_BM25; |
|
155 | + break; |
|
156 | + case 'none': |
|
157 | + $ranker = SPH_RANK_NONE; |
|
158 | + break; |
|
159 | + case 'wordcount': |
|
160 | + $ranker = SPH_RANK_WORDCOUNT; |
|
161 | + break; |
|
162 | + case 'fieldmask': |
|
163 | + $ranker = SPH_RANK_FIELDMASK; |
|
164 | + break; |
|
165 | + case 'sph04': |
|
166 | + $ranker = SPH_RANK_SPH04; |
|
167 | + break; |
|
168 | + } |
|
169 | + break; |
|
170 | + default: |
|
171 | + $q .= $args[$i] . ' '; |
|
172 | + } |
|
173 | + } |
|
174 | + |
|
175 | + //////////// |
|
176 | + // do query |
|
177 | + //////////// |
|
178 | + |
|
179 | + $cl->setServer($host, $port); |
|
180 | + $cl->setConnectTimeout(1); |
|
181 | + $cl->setArrayResult(true); |
|
182 | + $cl->setMatchMode($mode); |
|
183 | + if (count($filtervals)) { |
|
184 | + $cl->setFilter($filter, $filtervals); |
|
185 | + } |
|
186 | + if ($groupby) { |
|
187 | + $cl->setGroupBy($groupby, SPH_GROUPBY_ATTR, $groupsort); |
|
188 | + } |
|
189 | + if ($sortby) { |
|
190 | + $cl->setSortMode(SPH_SORT_EXTENDED, $sortby); |
|
191 | + } |
|
192 | + if ($sortexpr) { |
|
193 | + $cl->setSortMode(SPH_SORT_EXPR, $sortexpr); |
|
194 | + } |
|
195 | + if ($distinct) { |
|
196 | + $cl->setGroupDistinct($distinct); |
|
197 | + } |
|
198 | + if ($select) { |
|
199 | + $cl->setSelect($select); |
|
200 | + } |
|
201 | + if ($limit) { |
|
202 | + $cl->setLimits(0, $limit, ($limit > 1000) ? $limit : 1000); |
|
203 | + } |
|
204 | + $cl->setRankingMode($ranker); |
|
205 | + $res = $cl->query($q, $index); |
|
206 | + |
|
207 | + //////////////// |
|
208 | + // print me out |
|
209 | + //////////////// |
|
210 | + |
|
211 | + if ($res === false) { |
|
212 | + printf('Query failed: %s.' . PHP_EOL, $cl->getLastError()); |
|
213 | + |
|
214 | + } else { |
|
215 | + if ($cl->getLastWarning()) { |
|
216 | + printf('WARNING: %s' . PHP_EOL . PHP_EOL, $cl->getLastWarning()); |
|
217 | + } |
|
218 | + |
|
219 | + print "Query '$q' retrieved {$res['total']} of {$res['total_found']} matches in {$res['time']} sec.\n"; |
|
220 | + print 'Query stats:' . PHP_EOL; |
|
221 | + if (is_array($res['words'])) { |
|
222 | + foreach ($res['words'] as $word => $info) { |
|
223 | + print " '$word' found {$info['hits']} times in {$info['docs']} documents\n"; |
|
224 | + } |
|
225 | + } |
|
226 | + print PHP_EOL; |
|
227 | + |
|
228 | + if (is_array($res['matches'])) { |
|
229 | + $n = 1; |
|
230 | + print 'Matches:' . PHP_EOL; |
|
231 | + foreach ($res['matches'] as $docinfo) { |
|
232 | + print "$n. doc_id={$docinfo['id']}, weight={$docinfo['weight']}"; |
|
233 | + foreach ($res['attrs'] as $attrname => $attrtype) { |
|
234 | + $value = $docinfo['attrs'][$attrname]; |
|
235 | + if ($attrtype == SPH_ATTR_MULTI || $attrtype == SPH_ATTR_MULTI64) { |
|
236 | + $value = '(' . join(',', $value) . ')'; |
|
237 | + } elseif ($attrtype == SPH_ATTR_TIMESTAMP) { |
|
238 | + $value = date('Y-m-d H:i:s', $value); |
|
239 | + } |
|
240 | + print ", $attrname=$value"; |
|
241 | + } |
|
242 | + print PHP_EOL; |
|
243 | + $n++; |
|
244 | + } |
|
245 | + } |
|
246 | + } |
|
247 | 247 | } |
248 | 248 | \ No newline at end of file |
@@ -74,7 +74,7 @@ discard block |
||
74 | 74 | $select = ''; |
75 | 75 | $count = count($args); |
76 | 76 | |
77 | - for ($i = 0; $i < $count; $i++) { |
|
77 | + for ($i = 0; $i<$count; $i++) { |
|
78 | 78 | switch ($args[$i]) { |
79 | 79 | case '-h': |
80 | 80 | case '--host': |
@@ -168,7 +168,7 @@ discard block |
||
168 | 168 | } |
169 | 169 | break; |
170 | 170 | default: |
171 | - $q .= $args[$i] . ' '; |
|
171 | + $q .= $args[$i].' '; |
|
172 | 172 | } |
173 | 173 | } |
174 | 174 | |
@@ -199,7 +199,7 @@ discard block |
||
199 | 199 | $cl->setSelect($select); |
200 | 200 | } |
201 | 201 | if ($limit) { |
202 | - $cl->setLimits(0, $limit, ($limit > 1000) ? $limit : 1000); |
|
202 | + $cl->setLimits(0, $limit, ($limit>1000) ? $limit : 1000); |
|
203 | 203 | } |
204 | 204 | $cl->setRankingMode($ranker); |
205 | 205 | $res = $cl->query($q, $index); |
@@ -208,16 +208,16 @@ discard block |
||
208 | 208 | // print me out |
209 | 209 | //////////////// |
210 | 210 | |
211 | - if ($res === false) { |
|
212 | - printf('Query failed: %s.' . PHP_EOL, $cl->getLastError()); |
|
211 | + if ($res===false) { |
|
212 | + printf('Query failed: %s.'.PHP_EOL, $cl->getLastError()); |
|
213 | 213 | |
214 | 214 | } else { |
215 | 215 | if ($cl->getLastWarning()) { |
216 | - printf('WARNING: %s' . PHP_EOL . PHP_EOL, $cl->getLastWarning()); |
|
216 | + printf('WARNING: %s'.PHP_EOL.PHP_EOL, $cl->getLastWarning()); |
|
217 | 217 | } |
218 | 218 | |
219 | 219 | print "Query '$q' retrieved {$res['total']} of {$res['total_found']} matches in {$res['time']} sec.\n"; |
220 | - print 'Query stats:' . PHP_EOL; |
|
220 | + print 'Query stats:'.PHP_EOL; |
|
221 | 221 | if (is_array($res['words'])) { |
222 | 222 | foreach ($res['words'] as $word => $info) { |
223 | 223 | print " '$word' found {$info['hits']} times in {$info['docs']} documents\n"; |
@@ -227,14 +227,14 @@ discard block |
||
227 | 227 | |
228 | 228 | if (is_array($res['matches'])) { |
229 | 229 | $n = 1; |
230 | - print 'Matches:' . PHP_EOL; |
|
230 | + print 'Matches:'.PHP_EOL; |
|
231 | 231 | foreach ($res['matches'] as $docinfo) { |
232 | 232 | print "$n. doc_id={$docinfo['id']}, weight={$docinfo['weight']}"; |
233 | 233 | foreach ($res['attrs'] as $attrname => $attrtype) { |
234 | 234 | $value = $docinfo['attrs'][$attrname]; |
235 | - if ($attrtype == SPH_ATTR_MULTI || $attrtype == SPH_ATTR_MULTI64) { |
|
236 | - $value = '(' . join(',', $value) . ')'; |
|
237 | - } elseif ($attrtype == SPH_ATTR_TIMESTAMP) { |
|
235 | + if ($attrtype==SPH_ATTR_MULTI || $attrtype==SPH_ATTR_MULTI64) { |
|
236 | + $value = '('.join(',', $value).')'; |
|
237 | + } elseif ($attrtype==SPH_ATTR_TIMESTAMP) { |
|
238 | 238 | $value = date('Y-m-d H:i:s', $value); |
239 | 239 | } |
240 | 240 | print ", $attrname=$value"; |