-
Notifications
You must be signed in to change notification settings - Fork 7
/
php_yaz.c
2646 lines (2361 loc) · 65.5 KB
/
php_yaz.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
/* This file is part of PHP YAZ.
* Copyright (C) Index Data 2004-2018
* See the file LICENSE for details.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#if HAVE_YAZ
#include "ext/standard/info.h"
#include <yaz/yaz-version.h>
#include "php_yaz.h"
/* for PHP 8+ */
#ifndef TSRMLS_CC
#define TSRMLS_CC
#define TSRMLS_DC
#endif
#ifndef YAZ_VERSIONL
#error YAZ version 3.0.2 or later must be used.
#elif YAZ_VERSIONL < 0x030020
#error YAZ version 3.0.2 or later must be used.
#endif
#ifdef PHP_WIN32
#include <process.h>
#endif
#include <yaz/log.h>
#include <yaz/proto.h>
#include <yaz/marcdisp.h>
#include <yaz/yaz-util.h>
#include <yaz/yaz-ccl.h>
#include <yaz/cql.h>
#include <yaz/oid_db.h>
#include <yaz/zoom.h>
#include <yaz/pquery.h>
#if YAZ_VERSIONL >= 0x050100
#include <yaz/rpn2cql.h>
#endif
#ifndef ODR_INT_PRINTF
#define ODR_INT_PRINTF "%d"
#endif
#define MAX_ASSOC 200
typedef struct Yaz_AssociationInfo *Yaz_Association;
#if PHP_API_VERSION >= 20150101
typedef size_t zend_size_t;
#define ADD_ASSOC_STRING(x, y, z) add_assoc_string(x, y, z)
#define ADD_NEXT_INDEX_STRING(x, y) add_next_index_string(x, y)
#define ADD_NEXT_INDEX_STRINGl(x, y, z) add_next_index_stringl(x, y, z)
#else
#define ADD_ASSOC_STRING(x, y, z) add_assoc_string(x, y, z, 1)
#define ADD_NEXT_INDEX_STRING(x, y) add_next_index_string(x, y, 1)
#define ADD_NEXT_INDEX_STRINGl(x, y, z) add_next_index_stringl(x, y, z, 1)
typedef int zend_size_t;
typedef long zend_long;
#endif
struct Yaz_AssociationInfo {
CCL_bibset bibset;
#if YAZ_VERSIONL >= 0x050100
cql_transform_t ct;
#endif
ZOOM_connection zoom_conn;
ZOOM_resultset zoom_set;
ZOOM_scanset zoom_scan;
ZOOM_package zoom_package;
char *sort_criteria;
int persistent;
int in_use;
int order;
#if PHP_API_VERSION >= 20150101
zend_resource *zval_resource;
#else
int zval_resource;
#endif
time_t time_stamp;
};
static Yaz_Association yaz_association_mk()
{
Yaz_Association p = xmalloc(sizeof(*p));
p->zoom_conn = ZOOM_connection_create(0);
p->zoom_set = 0;
p->zoom_scan = 0;
p->zoom_package = 0;
ZOOM_connection_option_set(p->zoom_conn, "implementationName", "PHP");
ZOOM_connection_option_set(p->zoom_conn, "async", "1");
p->sort_criteria = 0;
p->in_use = 0;
p->order = 0;
p->persistent = 0;
p->bibset = ccl_qual_mk();
#if YAZ_VERSIONL >= 0x050100
p->ct = cql_transform_create();
#endif
p->time_stamp = 0;
return p;
}
static void yaz_association_destroy(Yaz_Association p)
{
if (!p) {
return;
}
#if YAZ_VERSIONL >= 0x050100
cql_transform_close(p->ct);
#endif
ZOOM_resultset_destroy(p->zoom_set);
ZOOM_scanset_destroy(p->zoom_scan);
ZOOM_package_destroy(p->zoom_package);
ZOOM_connection_destroy(p->zoom_conn);
xfree(p->sort_criteria);
ccl_qual_rm(&p->bibset);
}
#ifdef ZTS
static MUTEX_T yaz_mutex;
#endif
ZEND_DECLARE_MODULE_GLOBALS(yaz);
static Yaz_Association *shared_associations;
static int order_associations;
static int le_link;
#ifdef COMPILE_DL_YAZ
ZEND_GET_MODULE(yaz)
#endif
ZEND_BEGIN_ARG_INFO_EX(arginfo_yaz_connect, 0, 0, 1)
ZEND_ARG_INFO(0, url)
ZEND_ARG_INFO(0, options)
ZEND_END_ARG_INFO();
ZEND_BEGIN_ARG_INFO_EX(arginfo_yaz_close, 0, 0, 1)
ZEND_ARG_INFO(0, id)
ZEND_END_ARG_INFO();
#define arginfo_yaz_present arginfo_yaz_close
#define arginfo_yaz_errno arginfo_yaz_close
#define arginfo_yaz_error arginfo_yaz_close
#define arginfo_yaz_addinfo arginfo_yaz_close
#define arginfo_yaz_es_result arginfo_yaz_close
ZEND_BEGIN_ARG_INFO_EX(arginfo_yaz_search, 0, 0, 3)
ZEND_ARG_INFO(0, id)
ZEND_ARG_INFO(0, type)
ZEND_ARG_INFO(0, query)
ZEND_END_ARG_INFO();
ZEND_BEGIN_ARG_INFO_EX(arginfo_yaz_wait, 0, 0, 0)
ZEND_ARG_INFO(1, options)
ZEND_END_ARG_INFO();
ZEND_BEGIN_ARG_INFO_EX(arginfo_yaz_hits, 0, 0, 1)
ZEND_ARG_INFO(0, id)
ZEND_ARG_INFO(1, searchresult)
ZEND_END_ARG_INFO();
ZEND_BEGIN_ARG_INFO_EX(arginfo_yaz_record, 0, 0, 3)
ZEND_ARG_INFO(0, id)
ZEND_ARG_INFO(0, pos)
ZEND_ARG_INFO(0, type)
ZEND_END_ARG_INFO();
ZEND_BEGIN_ARG_INFO_EX(arginfo_yaz_syntax, 0, 0, 2)
ZEND_ARG_INFO(0, id)
ZEND_ARG_INFO(0, syntax)
ZEND_END_ARG_INFO();
ZEND_BEGIN_ARG_INFO_EX(arginfo_yaz_element, 0, 0, 2)
ZEND_ARG_INFO(0, id)
ZEND_ARG_INFO(0, elementsetname)
ZEND_END_ARG_INFO();
ZEND_BEGIN_ARG_INFO_EX(arginfo_yaz_schema, 0, 0, 2)
ZEND_ARG_INFO(0, id)
ZEND_ARG_INFO(0, schema)
ZEND_END_ARG_INFO();
ZEND_BEGIN_ARG_INFO_EX(arginfo_yaz_set_option, 0, 0, 2)
ZEND_ARG_INFO(0, id)
ZEND_ARG_INFO(0, options_or_name)
ZEND_ARG_INFO(0, value)
ZEND_END_ARG_INFO();
ZEND_BEGIN_ARG_INFO_EX(arginfo_yaz_get_option, 0, 0, 2)
ZEND_ARG_INFO(0, id)
ZEND_ARG_INFO(0, name)
ZEND_END_ARG_INFO();
ZEND_BEGIN_ARG_INFO_EX(arginfo_yaz_range, 0, 0, 3)
ZEND_ARG_INFO(0, id)
ZEND_ARG_INFO(0, start)
ZEND_ARG_INFO(0, number)
ZEND_END_ARG_INFO();
ZEND_BEGIN_ARG_INFO_EX(arginfo_yaz_sort, 0, 0, 2)
ZEND_ARG_INFO(0, id)
ZEND_ARG_INFO(0, sortspec)
ZEND_END_ARG_INFO();
ZEND_BEGIN_ARG_INFO_EX(arginfo_yaz_itemorder, 0, 0, 2)
ZEND_ARG_INFO(0, id)
ZEND_ARG_INFO(0, package)
ZEND_END_ARG_INFO();
#define arginfo_yaz_ccl_conf arginfo_yaz_itemorder
#define arginfo_yaz_cql_conf arginfo_yaz_itemorder
ZEND_BEGIN_ARG_INFO_EX(arginfo_yaz_es, 0, 0, 3)
ZEND_ARG_INFO(0, id)
ZEND_ARG_INFO(0, type)
ZEND_ARG_INFO(0, package)
ZEND_END_ARG_INFO();
ZEND_BEGIN_ARG_INFO_EX(arginfo_yaz_scan, 0, 0, 3)
ZEND_ARG_INFO(0, id)
ZEND_ARG_INFO(0, type)
ZEND_ARG_INFO(0, query)
ZEND_ARG_INFO(0, flags)
ZEND_END_ARG_INFO();
ZEND_BEGIN_ARG_INFO_EX(arginfo_yaz_scan_result, 0, 0, 1)
ZEND_ARG_INFO(0, id)
ZEND_ARG_INFO(1, options)
ZEND_END_ARG_INFO();
ZEND_BEGIN_ARG_INFO_EX(arginfo_yaz_ccl_parse, 0, 0, 3)
ZEND_ARG_INFO(0, id)
ZEND_ARG_INFO(0, query)
ZEND_ARG_INFO(1, result)
ZEND_END_ARG_INFO();
ZEND_BEGIN_ARG_INFO_EX(arginfo_yaz_cql_parse, 0, 0, 4)
ZEND_ARG_INFO(0, id)
ZEND_ARG_INFO(0, cql)
ZEND_ARG_INFO(1, result)
ZEND_ARG_INFO(0, rev)
ZEND_END_ARG_INFO();
ZEND_BEGIN_ARG_INFO_EX(arginfo_databases, 0, 0, 2)
ZEND_ARG_INFO(0, id)
ZEND_ARG_INFO(0, databases)
ZEND_END_ARG_INFO();
zend_function_entry yaz_functions [] = {
PHP_FE(yaz_connect, arginfo_yaz_connect)
PHP_FE(yaz_close, arginfo_yaz_close)
PHP_FE(yaz_search, arginfo_yaz_search)
PHP_FE(yaz_wait, arginfo_yaz_wait)
PHP_FE(yaz_errno, arginfo_yaz_errno)
PHP_FE(yaz_error, arginfo_yaz_error)
PHP_FE(yaz_addinfo, arginfo_yaz_addinfo)
PHP_FE(yaz_hits, arginfo_yaz_hits)
PHP_FE(yaz_record, arginfo_yaz_record)
PHP_FE(yaz_syntax, arginfo_yaz_syntax)
PHP_FE(yaz_element, arginfo_yaz_element)
PHP_FE(yaz_range, arginfo_yaz_range)
PHP_FE(yaz_itemorder, arginfo_yaz_itemorder)
PHP_FE(yaz_es_result, arginfo_yaz_es_result)
PHP_FE(yaz_scan, arginfo_yaz_scan)
PHP_FE(yaz_scan_result, arginfo_yaz_scan_result)
PHP_FE(yaz_present, arginfo_yaz_present)
PHP_FE(yaz_ccl_conf, arginfo_yaz_ccl_conf)
PHP_FE(yaz_ccl_parse, arginfo_yaz_ccl_parse)
#if YAZ_VERSIONL >= 0x050100
PHP_FE(yaz_cql_parse, arginfo_yaz_cql_parse)
PHP_FE(yaz_cql_conf, arginfo_yaz_cql_conf)
#endif
PHP_FE(yaz_database, arginfo_databases)
PHP_FE(yaz_sort, arginfo_yaz_sort)
PHP_FE(yaz_schema, arginfo_yaz_schema)
PHP_FE(yaz_set_option, arginfo_yaz_set_option)
PHP_FE(yaz_get_option, arginfo_yaz_get_option)
PHP_FE(yaz_es, arginfo_yaz_es)
#ifdef PHP_FE_END
PHP_FE_END
#else
{NULL, NULL, NULL}
#endif
};
static void get_assoc(INTERNAL_FUNCTION_PARAMETERS, zval *id, Yaz_Association *assocp)
{
Yaz_Association *as = 0;
*assocp = 0;
#ifdef ZTS
tsrm_mutex_lock(yaz_mutex);
#endif
#if PHP_API_VERSION >= 20150101
as = (Yaz_Association *) zend_fetch_resource(Z_RES_P(id),
"YAZ link", le_link);
#else
ZEND_FETCH_RESOURCE(as, Yaz_Association *, &id, -1, "YAZ link", le_link);
#endif
if (as && *as && (*as)->order == YAZSG(assoc_seq) && (*as)->in_use) {
*assocp = *as;
} else {
#ifdef ZTS
tsrm_mutex_unlock(yaz_mutex);
#endif
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid YAZ handle");
}
}
static void release_assoc(Yaz_Association assoc)
{
#ifdef ZTS
if (assoc) {
tsrm_mutex_unlock(yaz_mutex);
}
#endif
}
static const char *array_lookup_string(HashTable *ht, const char *idx)
{
#if PHP_API_VERSION >= 20150101
zval *zv;
zend_string *str = zend_string_init(idx, strlen(idx), 0);
zv = zend_hash_find(ht, str);
zend_string_release(str);
if (zv) {
convert_to_string(zv);
if (Z_TYPE_P(zv) == IS_STRING)
return ZSTR_VAL(zv->value.str);
}
#else
zval **pvalue;
if (ht && zend_hash_find(ht, (char *) idx, strlen(idx) + 1, (void **) &pvalue) == SUCCESS) {
SEPARATE_ZVAL(pvalue);
convert_to_string(*pvalue);
return (*pvalue)->value.str.val;
}
#endif
return 0;
}
static zend_long *array_lookup_long(HashTable *ht, const char *idx)
{
#if PHP_API_VERSION >= 20150101
zval *zv;
zend_string *str = zend_string_init(idx, strlen(idx), 0);
zv = zend_hash_find(ht, str);
zend_string_release(str);
if (zv) {
convert_to_long(zv);
if (Z_TYPE_P(zv) == IS_LONG)
return &zv->value.lval;
}
#else
zval **pvalue;
if (ht && zend_hash_find(ht, (char *) idx, strlen(idx) + 1, (void **) &pvalue) == SUCCESS) {
SEPARATE_ZVAL(pvalue);
convert_to_long(*pvalue);
return &(*pvalue)->value.lval;
}
#endif
return 0;
}
static long *array_lookup_bool(HashTable *ht, const char *idx)
{
#if PHP_API_VERSION >= 20150101
zval *zv;
static long l_true = 1;
static long l_false = 0;
zend_string *str = zend_string_init(idx, strlen(idx), 0);
zv = zend_hash_find(ht, str);
zend_string_release(str);
if (zv) {
convert_to_boolean(zv);
if (Z_TYPE_P(zv) == IS_TRUE)
return &l_true;
if (Z_TYPE_P(zv) == IS_FALSE)
return &l_false;
}
#else
zval **pvalue;
if (ht && zend_hash_find(ht, (char *) idx, strlen(idx) + 1, (void **) &pvalue) == SUCCESS) {
SEPARATE_ZVAL(pvalue);
convert_to_boolean(*pvalue);
return &(*pvalue)->value.lval;
}
#endif
return 0;
}
static const char *option_get(Yaz_Association as, const char *name)
{
if (!as) {
return 0;
}
return ZOOM_connection_option_get(as->zoom_conn, name);
}
static int option_get_int(Yaz_Association as, const char *name, int def)
{
const char *v;
v = ZOOM_connection_option_get(as->zoom_conn, name);
if (!v) {
return def;
}
return atoi(v);
}
static void option_set(Yaz_Association as, const char *name, const char *value)
{
if (as && value) {
ZOOM_connection_option_set(as->zoom_conn, name, value);
}
}
static void option_set_int(Yaz_Association as, const char *name, int v)
{
if (as) {
char s[30];
sprintf(s, "%d", v);
ZOOM_connection_option_set(as->zoom_conn, name, s);
}
}
static int strcmp_null(const char *s1, const char *s2)
{
if (s1 == 0 && s2 == 0) {
return 0;
}
if (s1 == 0 || s2 == 0) {
return -1;
}
return strcmp(s1, s2);
}
/* {{{ proto resource yaz_connect(string zurl [, array options])
Create target with given zurl. Returns positive id if successful. */
PHP_FUNCTION(yaz_connect)
{
int i;
char *cp;
char *zurl_str;
zend_size_t zurl_len;
const char *sru_str = 0, *sru_version_str = 0;
const char *user_str = 0, *group_str = 0, *pass_str = 0;
const char *cookie_str = 0, *proxy_str = 0;
const char *charset_str = 0;
const char *client_IP = 0;
const char *otherInfo[3];
const char *maximumRecordSize = 0;
const char *preferredMessageSize = 0;
int persistent = 1;
int piggyback = 1;
Yaz_Association as = 0;
int max_links = YAZSG(max_links);
otherInfo[0] = otherInfo[1] = otherInfo[2] = 0;
if (ZEND_NUM_ARGS() == 1) {
if (zend_parse_parameters(1 TSRMLS_CC, "s", &zurl_str, &zurl_len)
== FAILURE) {
WRONG_PARAM_COUNT;
}
} else if (ZEND_NUM_ARGS() == 2) {
zval *user = 0;
if (zend_parse_parameters(2 TSRMLS_CC, "sz", &zurl_str, &zurl_len,
&user) == FAILURE) {
WRONG_PARAM_COUNT;
}
if (Z_TYPE_P(user) == IS_ARRAY) {
long *persistent_val;
long *piggyback_val;
#if PHP_API_VERSION >= 20150101
HashTable *ht = Z_ARRVAL_P(user);
#else
HashTable *ht = Z_ARRVAL_PP(&user);
#endif
sru_str = array_lookup_string(ht, "sru");
sru_version_str = array_lookup_string(ht, "sru_version");
user_str = array_lookup_string(ht, "user");
group_str = array_lookup_string(ht, "group");
pass_str = array_lookup_string(ht, "password");
cookie_str = array_lookup_string(ht, "cookie");
proxy_str = array_lookup_string(ht, "proxy");
charset_str = array_lookup_string(ht, "charset");
persistent_val = array_lookup_bool(ht, "persistent");
if (persistent_val) {
persistent = *persistent_val;
}
piggyback_val = array_lookup_bool(ht, "piggyback");
if (piggyback_val) {
piggyback = *piggyback_val;
}
maximumRecordSize =
array_lookup_string(ht, "maximumRecordSize");
preferredMessageSize =
array_lookup_string(ht, "preferredMessageSize");
otherInfo[0] = array_lookup_string(ht, "otherInfo0");
otherInfo[1] = array_lookup_string(ht, "otherInfo1");
otherInfo[2] = array_lookup_string(ht, "otherInfo2");
} else if (Z_TYPE_P(user) == IS_STRING) {
#if PHP_API_VERSION >= 20150110
user_str = ZSTR_VAL(user->value.str);
#else
user_str = user->value.str.val;
#endif
}
} else {
WRONG_PARAM_COUNT;
}
for (cp = zurl_str; *cp && strchr("\t\n ", *cp); cp++);
if (!*cp) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Empty zurl");
RETURN_LONG(0);
}
/* see if we have it already ... */
#ifdef ZTS
tsrm_mutex_lock(yaz_mutex);
#endif
for (i = 0; i < max_links; i++) {
as = shared_associations[i];
if (persistent && as && !as->in_use &&
!strcmp_null(option_get(as, "host"), zurl_str) &&
!strcmp_null(option_get(as, "proxy"), proxy_str) &&
!strcmp_null(option_get(as, "sru"), sru_str) &&
!strcmp_null(option_get(as, "sru_version"), sru_version_str) &&
!strcmp_null(option_get(as, "user"), user_str) &&
!strcmp_null(option_get(as, "group"), group_str) &&
!strcmp_null(option_get(as, "pass"), pass_str) &&
!strcmp_null(option_get(as, "cookie"), cookie_str) &&
!strcmp_null(option_get(as, "charset"), charset_str))
break;
}
if (i == max_links) {
/* we didn't have it (or already in use) */
int i0 = -1;
int min_order = 2000000000;
/* find completely free slot or the oldest one */
for (i = 0; i < max_links && shared_associations[i]; i++) {
as = shared_associations[i];
if (persistent && !as->in_use && as->order < min_order) {
min_order = as->order;
i0 = i;
}
}
if (i == max_links) {
i = i0;
if (i == -1) {
char msg[80];
#ifdef ZTS
tsrm_mutex_unlock(yaz_mutex);
#endif
sprintf(msg, "No YAZ handles available. max_links=%d",
max_links);
php_error_docref(NULL TSRMLS_CC, E_WARNING,
"No YAZ handles available. max_links=%ld",
(long) max_links);
RETURN_LONG(0); /* no free slot */
} else { /* "best" free slot */
yaz_association_destroy(shared_associations[i]);
}
}
shared_associations[i] = as = yaz_association_mk();
option_set(as, "proxy", proxy_str);
option_set(as, "sru", sru_str);
option_set(as, "sru_version", sru_version_str);
option_set(as, "user", user_str);
option_set(as, "group", group_str);
option_set(as, "pass", pass_str);
option_set(as, "cookie", cookie_str);
option_set(as, "charset", charset_str);
}
if (maximumRecordSize)
option_set(as, "maximumRecordSize", maximumRecordSize);
if (preferredMessageSize)
option_set(as, "preferredMessageSize", preferredMessageSize);
option_set(as, "otherInfo0", otherInfo[0]);
option_set(as, "otherInfo1", otherInfo[1]);
option_set(as, "otherInfo2", otherInfo[2]);
option_set(as, "clientIP", client_IP);
option_set(as, "piggyback", piggyback ? "1" : "0");
option_set_int(as, "start", 0);
option_set_int(as, "count", 0);
ZOOM_connection_connect(as->zoom_conn, zurl_str, 0);
as->in_use = 1;
as->persistent = persistent;
as->order = YAZSG(assoc_seq);
as->time_stamp = time(0);
if (as->zoom_set)
{
ZOOM_resultset_destroy(as->zoom_set);
as->zoom_set = 0;
}
#ifdef ZTS
tsrm_mutex_unlock(yaz_mutex);
#endif
#if PHP_API_VERSION >= 20150101
RETURN_RES(zend_register_resource(&shared_associations[i], le_link));
as->zval_resource = Z_RES_P(return_value);
#else
ZEND_REGISTER_RESOURCE(return_value, &shared_associations[i], le_link);
as->zval_resource = Z_LVAL_P(return_value);
#endif
}
/* }}} */
/* {{{ proto bool yaz_close(resource id)
Destroy and close target */
PHP_FUNCTION(yaz_close)
{
Yaz_Association p;
zval *id;
if (ZEND_NUM_ARGS() != 1 || zend_parse_parameters(1 TSRMLS_CC, "z", &id)
== FAILURE) {
WRONG_PARAM_COUNT;
}
get_assoc(INTERNAL_FUNCTION_PARAM_PASSTHRU, id, &p);
if (!p) {
RETURN_FALSE;
}
release_assoc(p);
#if PHP_API_VERSION >= 20150101
zend_list_close(Z_RES_P(id));
#else
zend_list_delete(id->value.lval);
#endif
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool yaz_search(resource id, string type, string query)
Specify query of type for search - returns true if successful */
PHP_FUNCTION(yaz_search)
{
char *query_str, *type_str;
zend_size_t query_len, type_len;
zval *id;
Yaz_Association p;
if (ZEND_NUM_ARGS() != 3 ||
zend_parse_parameters(3 TSRMLS_CC, "zss", &id,
&type_str, &type_len,
&query_str, &query_len) == FAILURE) {
WRONG_PARAM_COUNT;
}
get_assoc(INTERNAL_FUNCTION_PARAM_PASSTHRU, id, &p);
if (!p) {
RETURN_FALSE;
}
ZOOM_resultset_destroy(p->zoom_set);
p->zoom_set = 0;
RETVAL_FALSE;
if (!strcmp(type_str, "rpn")) {
ZOOM_query q = ZOOM_query_create();
if (ZOOM_query_prefix(q, query_str) == 0)
{
if (p->sort_criteria) {
ZOOM_query_sortby(q, p->sort_criteria);
}
xfree(p->sort_criteria);
p->sort_criteria = 0;
p->zoom_set = ZOOM_connection_search(p->zoom_conn, q);
RETVAL_TRUE;
}
ZOOM_query_destroy(q);
}
else if (!strcmp(type_str, "cql")) {
ZOOM_query q = ZOOM_query_create();
if (ZOOM_query_cql(q, query_str) == 0)
{
if (p->sort_criteria) {
ZOOM_query_sortby(q, p->sort_criteria);
}
xfree(p->sort_criteria);
p->sort_criteria = 0;
p->zoom_set = ZOOM_connection_search(p->zoom_conn, q);
RETVAL_TRUE;
}
ZOOM_query_destroy(q);
}
else
{
php_error_docref(NULL TSRMLS_CC, E_WARNING,
"Invalid query type %s", type_str);
}
release_assoc(p);
}
/* }}} */
/* {{{ proto bool yaz_present(resource id)
Retrieve records */
PHP_FUNCTION(yaz_present)
{
zval *id;
Yaz_Association p;
if (ZEND_NUM_ARGS() != 1 || zend_parse_parameters(1 TSRMLS_CC, "z", &id)
== FAILURE) {
WRONG_PARAM_COUNT;
}
get_assoc(INTERNAL_FUNCTION_PARAM_PASSTHRU, id, &p);
if (!p) {
RETURN_FALSE;
}
if (p->zoom_set) {
size_t start = option_get_int(p, "start", 0);
size_t count = option_get_int(p, "count", 0);
if (count > 0) {
ZOOM_resultset_records(p->zoom_set, 0 /* recs */, start, count);
}
}
release_assoc(p);
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool yaz_wait([array options])
Process events. */
PHP_FUNCTION(yaz_wait)
{
zval *pval_options = 0;
int event_mode = 0;
int no = 0;
ZOOM_connection conn_ar[MAX_ASSOC];
Yaz_Association conn_as[MAX_ASSOC];
int i, timeout = 15;
if (ZEND_NUM_ARGS() == 1) {
zend_long *val = 0;
long *event_bool = 0;
HashTable *options_ht = 0;
if (zend_parse_parameters(1 TSRMLS_CC, "a/", &pval_options) ==
FAILURE) {
WRONG_PARAM_COUNT;
}
#if PHP_API_VERSION >= 20150101
options_ht = Z_ARRVAL_P(pval_options);
#else
options_ht = Z_ARRVAL_PP(&pval_options);
#endif
val = array_lookup_long(options_ht, "timeout");
if (val) {
timeout = *val;
}
event_bool = array_lookup_bool(options_ht, "event");
if (event_bool && *event_bool)
event_mode = 1;
}
else if (ZEND_NUM_ARGS() > 1) {
WRONG_PARAM_COUNT;
}
#ifdef ZTS
tsrm_mutex_lock(yaz_mutex);
#endif
for (i = 0; i<YAZSG(max_links); i++) {
Yaz_Association p = shared_associations[i];
if (p && p->order == YAZSG(assoc_seq)) {
char str[20];
sprintf(str, "%d", timeout);
ZOOM_connection_option_set(p->zoom_conn, "timeout", str);
conn_as[no] = p;
conn_ar[no++] = p->zoom_conn;
}
}
#ifdef ZTS
tsrm_mutex_unlock(yaz_mutex);
#endif
if (event_mode) {
long ev = ZOOM_event(no, conn_ar);
if (ev <= 0) {
RETURN_FALSE;
} else {
Yaz_Association p = conn_as[ev-1];
int event_code = ZOOM_connection_last_event(p->zoom_conn);
if (pval_options) {
add_assoc_long(pval_options, "connid", ev);
add_assoc_long(pval_options, "eventcode", event_code);
}
#if PHP_API_VERSION >= 20150101
Z_RES_P(return_value) = p->zval_resource;
#else
Z_TYPE_P(return_value) = IS_RESOURCE;
zend_list_addref(p->zval_resource);
Z_LVAL_P(return_value) = p->zval_resource;
#endif
return;
}
}
if (no) {
while (ZOOM_event(no, conn_ar))
;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto int yaz_errno(resource id)
Return last error number (>0 for bib-1 diagnostic, <0 for other error, 0 for no error */
PHP_FUNCTION(yaz_errno)
{
zval *id;
Yaz_Association p;
if (ZEND_NUM_ARGS() != 1 || zend_parse_parameters(1 TSRMLS_CC, "z", &id)
== FAILURE) {
WRONG_PARAM_COUNT;
}
get_assoc(INTERNAL_FUNCTION_PARAM_PASSTHRU, id, &p);
if (!p) {
RETURN_LONG(0);
}
RETVAL_LONG(ZOOM_connection_errcode(p->zoom_conn));
release_assoc(p);
}
/* }}} */
/* {{{ proto string yaz_error(resource id)
Return last error message */
PHP_FUNCTION(yaz_error)
{
zval *id;
Yaz_Association p;
if (ZEND_NUM_ARGS() != 1 || zend_parse_parameters(1 TSRMLS_CC, "z", &id)
== FAILURE) {
WRONG_PARAM_COUNT;
}
get_assoc(INTERNAL_FUNCTION_PARAM_PASSTHRU, id, &p);
if (p) {
int code = ZOOM_connection_errcode(p->zoom_conn);
const char *msg = ZOOM_connection_errmsg(p->zoom_conn);
if (!code) {
msg = "";
}
#if PHP_API_VERSION >= 20150101
zend_string *str = zend_string_init(msg, strlen(msg), 0);
RETURN_STR(str);
#else
return_value->value.str.len = strlen(msg);
return_value->value.str.val = estrndup(msg, return_value->value.str.len);
return_value->type = IS_STRING;
#endif
}
release_assoc(p);
}
/* }}} */
/* {{{ proto string yaz_addinfo(resource id)
Return additional info for last error (empty string if none) */
PHP_FUNCTION(yaz_addinfo)
{
zval *id;
Yaz_Association p;
if (ZEND_NUM_ARGS() != 1 || zend_parse_parameters(1 TSRMLS_CC, "z", &id)
== FAILURE) {
WRONG_PARAM_COUNT;
}
get_assoc(INTERNAL_FUNCTION_PARAM_PASSTHRU, id, &p);
if (p) {
const char *addinfo = ZOOM_connection_addinfo(p->zoom_conn);
#if PHP_API_VERSION >= 20150101
zend_string *str = zend_string_init(addinfo, strlen(addinfo), 0);
RETURN_STR(str);
#else
return_value->value.str.len = strlen(addinfo);
return_value->value.str.val = estrndup(addinfo, return_value->value.str.len);
return_value->type = IS_STRING;
#endif
}
release_assoc(p);
}
/* }}} */
/* {{{ proto int yaz_hits(resource id [, array searchresult])
Return number of hits (result count) for last search */
PHP_FUNCTION(yaz_hits)
{
zval *id, *searchresult = 0;
Yaz_Association p;
if (ZEND_NUM_ARGS() == 1) {
if (zend_parse_parameters(1 TSRMLS_CC, "z", &id) == FAILURE) {
WRONG_PARAM_COUNT;
}
} else if (ZEND_NUM_ARGS() == 2) {
if (zend_parse_parameters(2 TSRMLS_CC, "zz/", &id, &searchresult)
== FAILURE) {
WRONG_PARAM_COUNT;
}
} else {
WRONG_PARAM_COUNT;
}
if (searchresult) {
array_init(searchresult);
}
get_assoc(INTERNAL_FUNCTION_PARAM_PASSTHRU, id, &p);
if (p && p->zoom_set) {
RETVAL_LONG(ZOOM_resultset_size(p->zoom_set));
if (searchresult)
{
const char *str =
ZOOM_resultset_option_get(p->zoom_set, "resultSetStatus");
if (str)
ADD_ASSOC_STRING(searchresult, "resultSetStatus",
(char *) str);
}
if (searchresult)
{
const char *sz_str =
ZOOM_resultset_option_get(p->zoom_set, "searchresult.size");
int i, sz = 0;
if (sz_str && *sz_str)
sz = atoi(sz_str);
for (i = 0; i<sz; i++)
{
char opt_name[80];
const char *opt_value;
zval *zval_element;
#if PHP_API_VERSION >= 20150101
zval zval_element0;
zval_element = &zval_element0;
#else
MAKE_STD_ZVAL(zval_element);
#endif
array_init(zval_element);
add_next_index_zval(searchresult, zval_element);
sprintf(opt_name, "searchresult.%d.id", i);
opt_value = ZOOM_resultset_option_get(p->zoom_set, opt_name);
if (opt_value)
ADD_ASSOC_STRING(zval_element, "id", (char *) opt_value);
sprintf(opt_name, "searchresult.%d.count", i);
opt_value = ZOOM_resultset_option_get(p->zoom_set, opt_name);
if (opt_value)
add_assoc_long(zval_element, "count", atoi(opt_value));
sprintf(opt_name, "searchresult.%d.subquery.term", i);
opt_value = ZOOM_resultset_option_get(p->zoom_set, opt_name);
if (opt_value)
ADD_ASSOC_STRING(zval_element, "subquery.term",
(char *) opt_value);
sprintf(opt_name, "searchresult.%d.interpretation.term", i);
opt_value = ZOOM_resultset_option_get(p->zoom_set, opt_name);
if (opt_value)
ADD_ASSOC_STRING(zval_element, "interpretation.term",
(char *) opt_value);