-
Notifications
You must be signed in to change notification settings - Fork 0
/
sphinx.c
2195 lines (1825 loc) · 59 KB
/
sphinx.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2008 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Antony Dovgal <tony at daylessday.org> |
| Based on Sphinx PHP API by Andrew Aksyonoff <shodan at shodan.ru> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "ext/standard/file.h"
#include "zend_operators.h"
#include "php_sphinx.h"
#include <sphinxclient.h>
static zend_class_entry *ce_sphinx_client;
static zend_object_handlers php_sphinx_client_handlers;
static zend_object_handlers cannot_be_cloned;
typedef struct _php_sphinx_client {
zend_object std;
struct sphinx_obj {
sphinx_client *sphinx;
} *obj;
zend_bool array_result;
zend_bool is_persistent;
zend_bool is_pristine;
} php_sphinx_client;
static int le_sphinx_client;
#ifdef COMPILE_DL_SPHINX
ZEND_GET_MODULE(sphinx)
#endif
#ifndef E_RECOVERABLE_ERROR
#define E_RECOVERABLE_ERROR E_WARNING
#endif
#define SPHINX_CONST(name) REGISTER_LONG_CONSTANT(#name, name, CONST_CS | CONST_PERSISTENT)
#define SPHINX_INITIALIZED(c) \
if (!(c) || !(c)->obj || !(c)->obj->sphinx) { \
php_error_docref(NULL TSRMLS_CC, E_WARNING, "using uninitialized SphinxClient object"); \
RETURN_FALSE; \
}
static void php_sphinx_destroy(struct sphinx_obj *obj, zend_bool persistent TSRMLS_DC) /* {{{ */
{
if (obj->sphinx) {
sphinx_destroy(obj->sphinx);
}
pefree(obj, persistent);
}
/* }}} */
ZEND_RSRC_DTOR_FUNC(php_sphinx_dtor)
{
if (rsrc->ptr) {
struct sphinx_obj *sphinx = (struct sphinx_obj *)rsrc->ptr;
php_sphinx_destroy(sphinx, 1 TSRMLS_CC);
rsrc->ptr = NULL;
}
}
static void php_sphinx_client_obj_dtor(php_sphinx_client *c TSRMLS_DC) /* {{{ */
{
if (c->obj && !c->is_persistent) {
php_sphinx_destroy(c->obj, 0 TSRMLS_CC);
} else if (c->obj && c->obj->sphinx) {
// Cleanup our sphinx object, if our request left it in an incompatible state we want to clean it for the next requests
sphinx_cleanup(c->obj->sphinx);
}
c->obj = NULL;
zend_object_std_dtor(&c->std TSRMLS_CC);
efree(c);
}
/* }}} */
static zend_object_value php_sphinx_client_new(zend_class_entry *ce TSRMLS_DC) /* {{{ */
{
php_sphinx_client *c;
zend_object_value retval;
#if PHP_VERSION_ID < 50399
zval *tmp;
#endif
c = ecalloc(1, sizeof(*c));
zend_object_std_init(&c->std, ce TSRMLS_CC);
ALLOC_HASHTABLE(c->std.properties);
zend_hash_init(c->std.properties, 0, NULL, ZVAL_PTR_DTOR, 0);
#if PHP_VERSION_ID < 50399
zend_hash_copy(c->std.properties, &ce->default_properties, (copy_ctor_func_t) zval_add_ref, (void *) &tmp, sizeof(zval *));
#else
object_properties_init(&c->std, ce);
#endif
retval.handle = zend_objects_store_put(c, (zend_objects_store_dtor_t)zend_objects_destroy_object, (zend_objects_free_object_storage_t)php_sphinx_client_obj_dtor, NULL TSRMLS_CC);
retval.handlers = &php_sphinx_client_handlers;
return retval;
}
/* }}} */
#if PHP_MAJOR_VERSION >= 5 && PHP_MINOR_VERSION >= 4
static zval *php_sphinx_client_read_property(zval *object, zval *member, int type, const zend_literal *key TSRMLS_DC) /* {{{ */
#else
static zval *php_sphinx_client_read_property(zval *object, zval *member, int type TSRMLS_DC) /* {{{ */
#endif
{
php_sphinx_client *c;
zval tmp_member;
zval *retval;
zend_object_handlers *std_hnd;
c = (php_sphinx_client *)zend_object_store_get_object(object TSRMLS_CC);
if (member->type != IS_STRING) {
tmp_member = *member;
zval_copy_ctor(&tmp_member);
convert_to_string(&tmp_member);
member = &tmp_member;
}
/* XXX we can either create retval ourselves (for custom properties) or use standard handlers */
std_hnd = zend_get_std_object_handlers();
#if PHP_MAJOR_VERSION >= 5 && PHP_MINOR_VERSION >= 4
retval = std_hnd->read_property(object, member, type, key TSRMLS_CC);
#else
retval = std_hnd->read_property(object, member, type TSRMLS_CC);
#endif
if (member == &tmp_member) {
zval_dtor(member);
}
return retval;
}
/* }}} */
static HashTable *php_sphinx_client_get_properties(zval *object TSRMLS_DC) /* {{{ */
{
php_sphinx_client *c;
const char *warning, *error;
zval *tmp;
c = (php_sphinx_client *)zend_objects_get_address(object TSRMLS_CC);
error = sphinx_error(c->obj->sphinx);
MAKE_STD_ZVAL(tmp);
ZVAL_STRING(tmp, (char *)error, 1);
zend_hash_update(c->std.properties, "error", sizeof("error"), (void *)&tmp, sizeof(zval *), NULL);
warning = sphinx_warning(c->obj->sphinx);
MAKE_STD_ZVAL(tmp);
ZVAL_STRING(tmp, (char *)warning, 1);
zend_hash_update(c->std.properties, "warning", sizeof("warning"), (void *)&tmp, sizeof(zval *), NULL);
MAKE_STD_ZVAL(tmp);
ZVAL_BOOL(tmp, c->is_persistent);
zend_hash_update(c->std.properties, "is_persistent", sizeof("is_persistent"), (void *)&tmp, sizeof(zval *), NULL);
MAKE_STD_ZVAL(tmp);
ZVAL_BOOL(tmp, c->is_pristine);
zend_hash_update(c->std.properties, "is_pristine", sizeof("is_pristine"), (void *)&tmp, sizeof(zval *), NULL);
return c->std.properties;
}
/* }}} */
#ifdef TONY_200807015
static inline void php_sphinx_error(php_sphinx_client *c TSRMLS_DC) /* {{{ */
{
const char *err;
err = sphinx_error(c->obj->sphinx);
if (!err || err[0] == '\0') {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "unknown error");
} else {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", err);
}
}
/* }}} */
#endif
static void php_sphinx_result_to_array(php_sphinx_client *c, sphinx_result *result, zval **array TSRMLS_DC) /* {{{ */
{
zval *tmp, *tmp_element, *sub_element, *sub_sub_element;
int i, j;
array_init(*array);
/* error */
if (!result->error) {
add_assoc_string_ex(*array, "error", sizeof("error"), "", 1);
} else {
add_assoc_string_ex(*array, "error", sizeof("error"), (char *)(result->error), 1);
}
/* warning */
if (!result->warning) {
add_assoc_string_ex(*array, "warning", sizeof("warning"), "", 1);
} else {
add_assoc_string_ex(*array, "warning", sizeof("warning"), (char *)result->warning, 1);
}
/* status */
add_assoc_long_ex(*array, "status", sizeof("status"), result->status);
switch(result->status) {
case SEARCHD_OK:
/* ok, continue reading data */
break;
case SEARCHD_WARNING:
/* this seems to be safe, too */
break;
default:
/* libsphinxclient doesn't nullify the data
in case of error, so it's not safe to continue. */
return;
}
/* fields */
MAKE_STD_ZVAL(tmp);
array_init(tmp);
for (i = 0; i < result->num_fields; i++) {
add_next_index_string(tmp, result->fields[i], 1);
}
add_assoc_zval_ex(*array, "fields", sizeof("fields"), tmp);
/* attrs */
MAKE_STD_ZVAL(tmp);
array_init(tmp);
for (i = 0; i < result->num_attrs; i++) {
#if SIZEOF_LONG == 8
add_assoc_long_ex(tmp, result->attr_names[i], strlen(result->attr_names[i]) + 1, result->attr_types[i]);
#else
double float_value;
char buf[128];
float_value = (double)result->attr_types[i];
slprintf(buf, sizeof(buf), "%.0f", float_value);
add_assoc_string_ex(tmp, result->attr_names[i], strlen(result->attr_names[i]) + 1, buf, 1);
#endif
}
add_assoc_zval_ex(*array, "attrs", sizeof("attrs"), tmp);
/* matches */
if (result->num_matches) {
MAKE_STD_ZVAL(tmp);
array_init(tmp);
for (i = 0; i < result->num_matches; i++) {
MAKE_STD_ZVAL(tmp_element);
array_init(tmp_element);
if (c->array_result) {
/* id */
#if SIZEOF_LONG == 8
add_assoc_long_ex(tmp_element, "id", sizeof("id"), sphinx_get_id(result, i));
#else
double float_id;
char buf[128];
float_id = (double)sphinx_get_id(result, i);
slprintf(buf, sizeof(buf), "%.0f", float_id);
add_assoc_string_ex(tmp_element, "id", sizeof("id"), buf, 1);
#endif
}
/* weight */
add_assoc_long_ex(tmp_element, "weight", sizeof("weight"), sphinx_get_weight(result, i));
/* attrs */
MAKE_STD_ZVAL(sub_element);
array_init(sub_element);
for (j = 0; j < result->num_attrs; j++) {
#if SIZEOF_LONG != 8
double float_value;
char buf[128];
#endif
MAKE_STD_ZVAL(sub_sub_element);
switch(result->attr_types[j]) {
case SPH_ATTR_MULTI | SPH_ATTR_INTEGER:
{
int k;
unsigned int *mva = sphinx_get_mva(result, i, j);
unsigned int tmp, num;
array_init(sub_sub_element);
if (!mva) {
break;
}
memcpy(&num, mva, sizeof(unsigned int));
for (k = 1; k <= num; k++) {
mva++;
memcpy(&tmp, mva, sizeof(unsigned int));
#if SIZEOF_LONG == 8
add_next_index_long(sub_sub_element, tmp);
#else
float_value = (double)tmp;
slprintf(buf, sizeof(buf), "%.0f", float_value);
add_next_index_string(sub_sub_element, buf, 1);
#endif
}
} break;
case SPH_ATTR_FLOAT:
ZVAL_DOUBLE(sub_sub_element, sphinx_get_float(result, i, j));
break;
#if LIBSPHINX_VERSION_ID >= 110
case SPH_ATTR_STRING:
ZVAL_STRING(sub_sub_element, sphinx_get_string(result, i, j), 1);
break;
#endif
default:
#if SIZEOF_LONG == 8
ZVAL_LONG(sub_sub_element, sphinx_get_int(result, i, j));
#else
float_value = (double)sphinx_get_int(result, i, j);
slprintf(buf, sizeof(buf), "%.0f", float_value);
ZVAL_STRING(sub_sub_element, buf, 1);
#endif
break;
}
add_assoc_zval(sub_element, result->attr_names[j], sub_sub_element);
}
add_assoc_zval_ex(tmp_element, "attrs", sizeof("attrs"), sub_element);
if (c->array_result) {
add_next_index_zval(tmp, tmp_element);
} else {
#if SIZEOF_LONG == 8
add_index_zval(tmp, sphinx_get_id(result, i), tmp_element);
#else
char buf[128];
double float_id;
int buf_len;
float_id = (double)sphinx_get_id(result, i);
buf_len = slprintf(buf, sizeof(buf), "%.0f", float_id);
add_assoc_zval_ex(tmp, buf, buf_len + 1, tmp_element);
#endif
}
}
add_assoc_zval_ex(*array, "matches", sizeof("matches"), tmp);
}
/* total */
add_assoc_long_ex(*array, "total", sizeof("total"), result->total);
/* total_found */
add_assoc_long_ex(*array, "total_found", sizeof("total_found"), result->total_found);
/* time */
add_assoc_double_ex(*array, "time", sizeof("time"), (double)result->time_msec/1000.0);
/* words */
if (result->num_words) {
MAKE_STD_ZVAL(tmp);
array_init(tmp);
for (i = 0; i < result->num_words; i++) {
MAKE_STD_ZVAL(sub_element);
array_init(sub_element);
add_assoc_long_ex(sub_element, "docs", sizeof("docs"), result->words[i].docs);
add_assoc_long_ex(sub_element, "hits", sizeof("hits"), result->words[i].hits);
add_assoc_zval_ex(tmp, (char *)result->words[i].word, strlen(result->words[i].word) + 1, sub_element);
}
add_assoc_zval_ex(*array, "words", sizeof("words"), tmp);
}
}
/* }}} */
/* {{{ proto void SphinxClient::__construct([string persistent_id]) */
static PHP_METHOD(SphinxClient, __construct)
{
zval *object = getThis();
struct sphinx_obj *sphinx_obj = NULL;
php_sphinx_client *c;
char *persistent_id = NULL, *plist_key = NULL;
int persistent_id_len, plist_key_len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s!", &persistent_id, &persistent_id_len) == FAILURE) {
ZVAL_NULL(object);
return;
}
c = (php_sphinx_client *)zend_object_store_get_object(object TSRMLS_CC);
c->is_pristine = 0;
c->is_persistent = 0;
if (persistent_id && *persistent_id) {
zend_rsrc_list_entry *le = NULL;
c->is_persistent = 1;
plist_key_len = spprintf(&plist_key, 0, "sphinx:id=%s", persistent_id);
plist_key_len += 1;
if (zend_hash_find(&EG(persistent_list), plist_key, plist_key_len, (void *)&le) == SUCCESS) {
if (le->type == le_sphinx_client) {
sphinx_obj = (struct sphinx_obj *) le->ptr;
}
}
c->obj = sphinx_obj;
}
if (!sphinx_obj) {
sphinx_obj = pecalloc(1, sizeof(*sphinx_obj), c->is_persistent);
if (sphinx_obj == NULL) {
if (plist_key) {
efree(plist_key);
}
php_error_docref(NULL TSRMLS_CC, E_ERROR, "could not allocate memory for sphinx object");
/* not reached */
}
sphinx_obj->sphinx = sphinx_create(1 /* copy string args */);
sphinx_set_connect_timeout(sphinx_obj->sphinx, FG(default_socket_timeout));
if (c->is_persistent) {
zend_rsrc_list_entry le;
le.type = le_sphinx_client;
le.ptr = sphinx_obj;
if (zend_hash_update(&EG(persistent_list), plist_key, plist_key_len, (void *)&le, sizeof(le), NULL) == FAILURE) {
if (plist_key) {
efree(plist_key);
}
php_error_docref(NULL TSRMLS_CC, E_ERROR, "could not register persistent entry");
/* not reached */
}
}
c->obj = sphinx_obj;
c->is_pristine = 1;
}
if (plist_key) {
efree(plist_key);
}
}
/* }}} */
/* {{{ proto bool SphinxClient::setServer(string server, int port) */
static PHP_METHOD(SphinxClient, setServer)
{
php_sphinx_client *c;
long port;
char *server;
int server_len, res;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl", &server, &server_len, &port) == FAILURE) {
return;
}
c = (php_sphinx_client *)zend_object_store_get_object(getThis() TSRMLS_CC);
SPHINX_INITIALIZED(c)
res = sphinx_set_server(c->obj->sphinx, server, (int)port);
if (!res) {
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool SphinxClient::setLimits(int offset, int limit[, int max_matches[, int cutoff]]) */
static PHP_METHOD(SphinxClient, setLimits)
{
php_sphinx_client *c;
long offset, limit, max_matches = 0, cutoff = 0;
int res;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll|ll", &offset, &limit, &max_matches, &cutoff) == FAILURE) {
return;
}
c = (php_sphinx_client *)zend_object_store_get_object(getThis() TSRMLS_CC);
SPHINX_INITIALIZED(c)
res = sphinx_set_limits(c->obj->sphinx, (int)offset, (int)limit, (int)max_matches, (int)cutoff);
if (!res) {
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool SphinxClient::setMatchMode(int mode) */
static PHP_METHOD(SphinxClient, setMatchMode)
{
php_sphinx_client *c;
long mode;
int res;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &mode) == FAILURE) {
return;
}
c = (php_sphinx_client *)zend_object_store_get_object(getThis() TSRMLS_CC);
SPHINX_INITIALIZED(c)
res = sphinx_set_match_mode(c->obj->sphinx, mode);
if (!res) {
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool SphinxClient::setIndexWeights(array weights) */
static PHP_METHOD(SphinxClient, setIndexWeights)
{
php_sphinx_client *c;
zval *weights, **item;
int num_weights, res = 0, i;
int *index_weights;
char **index_names;
char *string_key;
unsigned int string_key_len;
unsigned long int num_key;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &weights) == FAILURE) {
return;
}
c = (php_sphinx_client *)zend_object_store_get_object(getThis() TSRMLS_CC);
SPHINX_INITIALIZED(c)
num_weights = zend_hash_num_elements(Z_ARRVAL_P(weights));
if (!num_weights) {
/* check for empty array and return false right away */
RETURN_FALSE;
}
index_names = safe_emalloc(num_weights, sizeof(char *), 0);
index_weights = safe_emalloc(num_weights, sizeof(int), 0);
/* reset num_weights, we'll reuse it count _real_ number of entries */
num_weights = 0;
for (zend_hash_internal_pointer_reset(Z_ARRVAL_P(weights));
zend_hash_get_current_data(Z_ARRVAL_P(weights), (void **)&item) != FAILURE;
zend_hash_move_forward(Z_ARRVAL_P(weights))) {
if (zend_hash_get_current_key_ex(Z_ARRVAL_P(weights), &string_key, &string_key_len, &num_key, 0, NULL) != HASH_KEY_IS_STRING) {
/* if the key is not string.. well.. you're screwed */
break;
}
convert_to_long_ex(item);
index_names[num_weights] = estrndup(string_key, string_key_len);
index_weights[num_weights] = Z_LVAL_PP(item);
num_weights++;
}
if (num_weights) {
res = sphinx_set_index_weights(c->obj->sphinx, num_weights, (const char **)index_names, index_weights);
}
for (i = 0; i != num_weights; i++) {
efree(index_names[i]);
}
efree(index_names);
efree(index_weights);
if (!res) {
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
#if LIBSPHINX_VERSION_ID >= 99
/* {{{ proto bool SphinxClient::setSelect(string clause) */
static PHP_METHOD(SphinxClient, setSelect)
{
php_sphinx_client *c;
char *clause;
int clause_len, res;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &clause, &clause_len) == FAILURE) {
return;
}
c = (php_sphinx_client *)zend_object_store_get_object(getThis() TSRMLS_CC);
SPHINX_INITIALIZED(c)
res = sphinx_set_select(c->obj->sphinx, clause);
if (!res) {
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
#endif
/* {{{ proto bool SphinxClient::setIDRange(int min, int max) */
static PHP_METHOD(SphinxClient, setIDRange)
{
php_sphinx_client *c;
long min, max;
int res;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll", &min, &max) == FAILURE) {
return;
}
c = (php_sphinx_client *)zend_object_store_get_object(getThis() TSRMLS_CC);
SPHINX_INITIALIZED(c)
res = sphinx_set_id_range(c->obj->sphinx, (sphinx_uint64_t)min, (sphinx_uint64_t)max);
if (!res) {
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool SphinxClient::setFilter(string attribute, array values[, bool exclude]) */
static PHP_METHOD(SphinxClient, setFilter)
{
php_sphinx_client *c;
zval *values, **item;
char *attribute;
int attribute_len, num_values, i = 0, res;
zend_bool exclude = 0;
sphinx_int64_t *u_values;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sa|b", &attribute, &attribute_len, &values, &exclude) == FAILURE) {
return;
}
c = (php_sphinx_client *)zend_object_store_get_object(getThis() TSRMLS_CC);
SPHINX_INITIALIZED(c)
num_values = zend_hash_num_elements(Z_ARRVAL_P(values));
if (!num_values) {
RETURN_FALSE;
}
u_values = safe_emalloc(num_values, sizeof(sphinx_int64_t), 0);
for (zend_hash_internal_pointer_reset(Z_ARRVAL_P(values));
zend_hash_get_current_data(Z_ARRVAL_P(values), (void **) &item) != FAILURE;
zend_hash_move_forward(Z_ARRVAL_P(values))) {
convert_to_double_ex(item);
u_values[i] = (sphinx_int64_t)Z_DVAL_PP(item);
i++;
}
res = sphinx_add_filter(c->obj->sphinx, attribute, num_values, u_values, exclude ? 1 : 0);
efree(u_values);
if (!res) {
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool SphinxClient::setFilterRange(string attribute, int min, int max[, bool exclude]) */
static PHP_METHOD(SphinxClient, setFilterRange)
{
php_sphinx_client *c;
char *attribute;
int attribute_len, res;
long min, max;
zend_bool exclude = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sll|b", &attribute, &attribute_len, &min, &max, &exclude ) == FAILURE) {
return;
}
c = (php_sphinx_client *)zend_object_store_get_object(getThis() TSRMLS_CC);
SPHINX_INITIALIZED(c)
res = sphinx_add_filter_range(c->obj->sphinx, attribute, min, max, exclude);
if (!res) {
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool SphinxClient::setFilterFloatRange(string attribute, float min, float max[, bool exclude]) */
static PHP_METHOD(SphinxClient, setFilterFloatRange)
{
php_sphinx_client *c;
char *attribute;
int attribute_len, res;
double min, max;
zend_bool exclude = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sdd|b", &attribute, &attribute_len, &min, &max, &exclude) == FAILURE) {
return;
}
c = (php_sphinx_client *)zend_object_store_get_object(getThis() TSRMLS_CC);
SPHINX_INITIALIZED(c)
res = sphinx_add_filter_float_range(c->obj->sphinx, attribute, min, max, exclude);
if (!res) {
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool SphinxClient::setGeoAnchor(string attrlat, string attrlong, float latitude, float longitude) */
static PHP_METHOD(SphinxClient, setGeoAnchor)
{
php_sphinx_client *c;
char *attrlat, *attrlong;
int attrlat_len, attrlong_len, res;
double latitude, longitude;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ssdd", &attrlat, &attrlat_len, &attrlong, &attrlong_len, &latitude, &longitude) == FAILURE) {
return;
}
c = (php_sphinx_client *)zend_object_store_get_object(getThis() TSRMLS_CC);
SPHINX_INITIALIZED(c)
res = sphinx_set_geoanchor(c->obj->sphinx, attrlat, attrlong, latitude, longitude);
if (!res) {
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool SphinxClient::setGroupBy(string attribute, int func[, string groupsort]) */
static PHP_METHOD(SphinxClient, setGroupBy)
{
php_sphinx_client *c;
char *attribute, *groupsort = NULL;
int attribute_len, groupsort_len, func, res;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl|s", &attribute, &attribute_len, &func, &groupsort, &groupsort_len) == FAILURE) {
return;
}
if (groupsort == NULL) {
groupsort = "@group desc";
}
c = (php_sphinx_client *)zend_object_store_get_object(getThis() TSRMLS_CC);
SPHINX_INITIALIZED(c)
res = sphinx_set_groupby(c->obj->sphinx, attribute, func, groupsort);
if (!res) {
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool SphinxClient::setGroupDistinct(string attribute) */
static PHP_METHOD(SphinxClient, setGroupDistinct)
{
php_sphinx_client *c;
char *attribute;
int attribute_len, res;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &attribute, &attribute_len) == FAILURE) {
return;
}
c = (php_sphinx_client *)zend_object_store_get_object(getThis() TSRMLS_CC);
SPHINX_INITIALIZED(c)
res = sphinx_set_groupby_distinct(c->obj->sphinx, attribute);
if (!res) {
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool SphinxClient::setRetries(int count[, int delay]) */
static PHP_METHOD(SphinxClient, setRetries)
{
php_sphinx_client *c;
long count, delay = 0;
int res;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|l", &count, &delay) == FAILURE) {
return;
}
c = (php_sphinx_client *)zend_object_store_get_object(getThis() TSRMLS_CC);
SPHINX_INITIALIZED(c)
res = sphinx_set_retries(c->obj->sphinx, (int)count, (int)delay);
if (!res) {
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool SphinxClient::setMaxQueryTime(int qtime) */
static PHP_METHOD(SphinxClient, setMaxQueryTime)
{
php_sphinx_client *c;
long qtime;
int res;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &qtime) == FAILURE) {
return;
}
c = (php_sphinx_client *)zend_object_store_get_object(getThis() TSRMLS_CC);
SPHINX_INITIALIZED(c)
res = sphinx_set_max_query_time(c->obj->sphinx, (int)qtime);
if (!res) {
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
#ifdef HAVE_3ARG_SPHINX_SET_RANKING_MODE
/* {{{ proto bool SphinxClient::setRankingMode(int ranker[, string ranking_expression]) */
static PHP_METHOD(SphinxClient, setRankingMode)
{
php_sphinx_client *c;
long ranker;
int res, rank_expr_len;
char *rank_expr = NULL;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|s", &ranker, &rank_expr, &rank_expr_len) == FAILURE) {
return;
}
c = (php_sphinx_client *)zend_object_store_get_object(getThis() TSRMLS_CC);
SPHINX_INITIALIZED(c)
res = sphinx_set_ranking_mode(c->obj->sphinx, (int)ranker, rank_expr);
if (!res) {
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
#else
/* {{{ proto bool SphinxClient::setRankingMode(int ranker) */
static PHP_METHOD(SphinxClient, setRankingMode)
{
php_sphinx_client *c;
long ranker;
int res;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &ranker) == FAILURE) {
return;
}
c = (php_sphinx_client *)zend_object_store_get_object(getThis() TSRMLS_CC);
SPHINX_INITIALIZED(c)
res = sphinx_set_ranking_mode(c->obj->sphinx, (int)ranker);
if (!res) {
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
#endif
/* {{{ proto bool SphinxClient::setFieldWeights(array weights) */
static PHP_METHOD(SphinxClient, setFieldWeights)
{
php_sphinx_client *c;
zval *weights, **item;
int num_weights, res = 0, i;
int *field_weights;
char **field_names;
char *string_key;
unsigned int string_key_len;
unsigned long int num_key;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &weights) == FAILURE) {
return;
}
c = (php_sphinx_client *)zend_object_store_get_object(getThis() TSRMLS_CC);
SPHINX_INITIALIZED(c)
num_weights = zend_hash_num_elements(Z_ARRVAL_P(weights));
if (!num_weights) {
/* check for empty array and return false right away */
RETURN_FALSE;
}
field_names = safe_emalloc(num_weights, sizeof(char *), 0);
field_weights = safe_emalloc(num_weights, sizeof(int), 0);
/* reset num_weights, we'll reuse it count _real_ number of entries */
num_weights = 0;
for (zend_hash_internal_pointer_reset(Z_ARRVAL_P(weights));
zend_hash_get_current_data(Z_ARRVAL_P(weights), (void **)&item) != FAILURE;
zend_hash_move_forward(Z_ARRVAL_P(weights))) {
if (zend_hash_get_current_key_ex(Z_ARRVAL_P(weights), &string_key, &string_key_len, &num_key, 0, NULL) != HASH_KEY_IS_STRING) {
/* if the key is not string.. well.. you're screwed */
break;
}
convert_to_long_ex(item);
field_names[num_weights] = estrndup(string_key, string_key_len);
field_weights[num_weights] = Z_LVAL_PP(item);
num_weights++;
}
if (num_weights) {
res = sphinx_set_field_weights(c->obj->sphinx, num_weights, (const char **) field_names, field_weights);
}
for (i = 0; i != num_weights; i++) {
efree(field_names[i]);
}
efree(field_names);
efree(field_weights);
if (!res) {
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool SphinxClient::setSortMode(int mode[, string sortby]) */
static PHP_METHOD(SphinxClient, setSortMode)
{
php_sphinx_client *c;
long mode;
char *sortby = NULL;
int sortby_len, res;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|s", &mode, &sortby, &sortby_len) == FAILURE) {
return;
}