2 +----------------------------------------------------------------------+
4 +----------------------------------------------------------------------+
5 | Copyright (c) 1997-2011 The PHP Group |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt. |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
15 | Author: Adam Dickmeiss <adam@indexdata.dk> |
16 +----------------------------------------------------------------------+
28 #include "ext/standard/info.h"
31 #include <yaz/yaz-version.h>
34 #error YAZ version 3.0 or later must be used.
35 #elif YAZ_VERSIONL < 0x030000
36 #error YAZ version 3.0 or later must be used.
44 #include <yaz/proto.h>
45 #include <yaz/marcdisp.h>
46 #include <yaz/yaz-util.h>
47 #include <yaz/yaz-ccl.h>
48 #include <yaz/oid_db.h>
51 #ifndef ODR_INT_PRINTF
52 #define ODR_INT_PRINTF %d
57 #define GET_PARM1(a) zend_parse_parameters(1 TSRMLS_CC , "z", a)
58 #define GET_PARM2(a,b) zend_parse_parameters(2 TSRMLS_CC, "zz", a, b)
59 #define GET_PARM3(a,b,c) zend_parse_parameters(3 TSRMLS_CC, "zzz", a, b, c)
60 #define GET_PARM4(a,b,c,d) zend_parse_parameters(4 TSRMLS_CC, "zzzz", a, b, c, d)
62 typedef struct Yaz_AssociationInfo *Yaz_Association;
64 struct Yaz_AssociationInfo {
66 ZOOM_connection zoom_conn;
67 ZOOM_resultset zoom_set;
68 ZOOM_scanset zoom_scan;
69 ZOOM_package zoom_package;
78 static Yaz_Association yaz_association_mk()
80 Yaz_Association p = xmalloc(sizeof(*p));
82 p->zoom_conn = ZOOM_connection_create(0);
86 ZOOM_connection_option_set(p->zoom_conn, "implementationName", "PHP");
87 ZOOM_connection_option_set(p->zoom_conn, "async", "1");
92 p->bibset = ccl_qual_mk();
97 static void yaz_association_destroy(Yaz_Association p)
103 ZOOM_resultset_destroy(p->zoom_set);
104 ZOOM_scanset_destroy(p->zoom_scan);
105 ZOOM_package_destroy(p->zoom_package);
106 ZOOM_connection_destroy(p->zoom_conn);
107 xfree(p->sort_criteria);
108 ccl_qual_rm(&p->bibset);
112 static MUTEX_T yaz_mutex;
115 ZEND_DECLARE_MODULE_GLOBALS(yaz);
117 static Yaz_Association *shared_associations;
118 static int order_associations;
122 #ifdef COMPILE_DL_YAZ
126 #ifdef ZEND_BEGIN_ARG_INFO
127 ZEND_BEGIN_ARG_INFO(first_argument_force_ref, 0)
128 ZEND_ARG_PASS_INFO(1)
131 ZEND_BEGIN_ARG_INFO(second_argument_force_ref, 0)
132 ZEND_ARG_PASS_INFO(0)
133 ZEND_ARG_PASS_INFO(1)
136 ZEND_BEGIN_ARG_INFO(third_argument_force_ref, 0)
137 ZEND_ARG_PASS_INFO(0)
138 ZEND_ARG_PASS_INFO(0)
139 ZEND_ARG_PASS_INFO(1)
142 static unsigned char first_argument_force_ref[] = {
144 static unsigned char second_argument_force_ref[] = {
145 2, BYREF_NONE, BYREF_FORCE };
146 static unsigned char third_argument_force_ref[] = {
147 3, BYREF_NONE, BYREF_NONE, BYREF_FORCE };
151 zend_function_entry yaz_functions [] = {
152 PHP_FE(yaz_connect, NULL)
153 PHP_FE(yaz_close, NULL)
154 PHP_FE(yaz_search, NULL)
155 PHP_FE(yaz_wait, first_argument_force_ref)
156 PHP_FE(yaz_errno, NULL)
157 PHP_FE(yaz_error, NULL)
158 PHP_FE(yaz_addinfo, NULL)
159 PHP_FE(yaz_hits, second_argument_force_ref)
160 PHP_FE(yaz_record, NULL)
161 PHP_FE(yaz_syntax, NULL)
162 PHP_FE(yaz_element, NULL)
163 PHP_FE(yaz_range, NULL)
164 PHP_FE(yaz_itemorder, NULL)
165 PHP_FE(yaz_es_result, NULL)
166 PHP_FE(yaz_scan, NULL)
167 PHP_FE(yaz_scan_result, second_argument_force_ref)
168 PHP_FE(yaz_present, NULL)
169 PHP_FE(yaz_ccl_conf, NULL)
170 PHP_FE(yaz_ccl_parse, third_argument_force_ref)
171 PHP_FE(yaz_database, NULL)
172 PHP_FE(yaz_sort, NULL)
173 PHP_FE(yaz_schema, NULL)
174 PHP_FE(yaz_set_option, NULL)
175 PHP_FE(yaz_get_option, NULL)
180 static void get_assoc(INTERNAL_FUNCTION_PARAMETERS, zval *id, Yaz_Association *assocp)
182 Yaz_Association *as = 0;
186 tsrm_mutex_lock(yaz_mutex);
189 ZEND_FETCH_RESOURCE(as, Yaz_Association *, &id, -1, "YAZ link", le_link);
191 if (as && *as && (*as)->order == YAZSG(assoc_seq) && (*as)->in_use) {
195 tsrm_mutex_unlock(yaz_mutex);
197 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid YAZ handle");
201 static void release_assoc(Yaz_Association assoc)
205 tsrm_mutex_unlock(yaz_mutex);
210 static const char *array_lookup_string(HashTable *ht, const char *idx)
214 if (ht && zend_hash_find(ht, (char *) idx, strlen(idx) + 1, (void **) &pvalue) == SUCCESS) {
215 SEPARATE_ZVAL(pvalue);
216 convert_to_string(*pvalue);
217 return (*pvalue)->value.str.val;
222 static long *array_lookup_long(HashTable *ht, const char *idx)
226 if (ht && zend_hash_find(ht, (char *) idx, strlen(idx) + 1, (void **) &pvalue) == SUCCESS) {
227 SEPARATE_ZVAL(pvalue);
228 convert_to_long(*pvalue);
229 return &(*pvalue)->value.lval;
234 static long *array_lookup_bool(HashTable *ht, const char *idx)
238 if (ht && zend_hash_find(ht, (char *) idx, strlen(idx) + 1, (void **) &pvalue) == SUCCESS) {
239 SEPARATE_ZVAL(pvalue);
240 convert_to_boolean(*pvalue);
241 return &(*pvalue)->value.lval;
246 static const char *option_get(Yaz_Association as, const char *name)
251 return ZOOM_connection_option_get(as->zoom_conn, name);
254 static int option_get_int(Yaz_Association as, const char *name, int def)
258 v = ZOOM_connection_option_get(as->zoom_conn, name);
267 static void option_set(Yaz_Association as, const char *name, const char *value)
270 ZOOM_connection_option_set(as->zoom_conn, name, value);
274 static void option_set_int(Yaz_Association as, const char *name, int v)
280 ZOOM_connection_option_set(as->zoom_conn, name, s);
284 static int strcmp_null(const char *s1, const char *s2)
286 if (s1 == 0 && s2 == 0) {
289 if (s1 == 0 || s2 == 0) {
292 return strcmp(s1, s2);
295 /* {{{ proto resource yaz_connect(string zurl [, array options])
296 Create target with given zurl. Returns positive id if successful. */
297 PHP_FUNCTION(yaz_connect)
302 const char *sru_str = 0, *sru_version_str = 0;
303 const char *user_str = 0, *group_str = 0, *pass_str = 0;
304 const char *cookie_str = 0, *proxy_str = 0;
305 const char *charset_str = 0;
306 const char *client_IP = 0;
307 const char *otherInfo[3];
308 const char *maximumRecordSize = 0;
309 const char *preferredMessageSize = 0;
312 zval *zurl, *user = 0;
314 int max_links = YAZSG(max_links);
316 otherInfo[0] = otherInfo[1] = otherInfo[2] = 0;
318 if (ZEND_NUM_ARGS() == 1) {
319 if (GET_PARM1(&zurl) == FAILURE) {
322 } else if (ZEND_NUM_ARGS() == 2) {
323 if (GET_PARM2(&zurl, &user) == FAILURE) {
327 if (Z_TYPE_PP(&user) == IS_ARRAY) {
328 long *persistent_val;
330 HashTable *ht = Z_ARRVAL_PP(&user);
332 sru_str = array_lookup_string(ht, "sru");
333 sru_version_str = array_lookup_string(ht, "sru_version");
334 user_str = array_lookup_string(ht, "user");
335 group_str = array_lookup_string(ht, "group");
336 pass_str = array_lookup_string(ht, "password");
337 cookie_str = array_lookup_string(ht, "cookie");
338 proxy_str = array_lookup_string(ht, "proxy");
339 charset_str = array_lookup_string(ht, "charset");
340 persistent_val = array_lookup_bool(ht, "persistent");
341 if (persistent_val) {
342 persistent = *persistent_val;
344 piggyback_val = array_lookup_bool(ht, "piggyback");
346 piggyback = *piggyback_val;
349 array_lookup_string(ht, "maximumRecordSize");
350 preferredMessageSize =
351 array_lookup_string(ht, "preferredMessageSize");
352 otherInfo[0] = array_lookup_string(ht, "otherInfo0");
353 otherInfo[1] = array_lookup_string(ht, "otherInfo1");
354 otherInfo[2] = array_lookup_string(ht, "otherInfo2");
355 } else if (Z_TYPE_PP(&user) == IS_STRING) {
356 convert_to_string_ex(&user);
357 if (*user->value.str.val)
358 user_str = user->value.str.val;
363 convert_to_string_ex(&zurl);
364 zurl_str = zurl->value.str.val;
365 for (cp = zurl_str; *cp && strchr("\t\n ", *cp); cp++);
367 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Empty zurl");
370 /* see if we have it already ... */
372 tsrm_mutex_lock(yaz_mutex);
374 for (i = 0; i < max_links; i++) {
375 as = shared_associations[i];
376 if (persistent && as && !as->in_use &&
377 !strcmp_null(option_get(as, "host"), zurl_str) &&
378 !strcmp_null(option_get(as, "proxy"), proxy_str) &&
379 !strcmp_null(option_get(as, "sru"), sru_str) &&
380 !strcmp_null(option_get(as, "sru_version"), sru_version_str) &&
381 !strcmp_null(option_get(as, "user"), user_str) &&
382 !strcmp_null(option_get(as, "group"), group_str) &&
383 !strcmp_null(option_get(as, "pass"), pass_str) &&
384 !strcmp_null(option_get(as, "cookie"), cookie_str) &&
385 !strcmp_null(option_get(as, "charset"), charset_str))
388 if (i == max_links) {
389 /* we didn't have it (or already in use) */
391 int min_order = 2000000000;
393 /* find completely free slot or the oldest one */
394 for (i = 0; i < max_links && shared_associations[i]; i++) {
395 as = shared_associations[i];
396 if (persistent && !as->in_use && as->order < min_order) {
397 min_order = as->order;
402 if (i == max_links) {
407 tsrm_mutex_unlock(yaz_mutex);
409 sprintf(msg, "No YAZ handles available. max_links=%d",
411 php_error_docref(NULL TSRMLS_CC, E_WARNING,
412 "No YAZ handles available. max_links=%ld",
414 RETURN_LONG(0); /* no free slot */
415 } else { /* "best" free slot */
416 yaz_association_destroy(shared_associations[i]);
419 shared_associations[i] = as = yaz_association_mk();
421 option_set(as, "proxy", proxy_str);
422 option_set(as, "sru", sru_str);
423 option_set(as, "sru_version", sru_version_str);
424 option_set(as, "user", user_str);
425 option_set(as, "group", group_str);
426 option_set(as, "pass", pass_str);
427 option_set(as, "cookie", cookie_str);
428 option_set(as, "charset", charset_str);
430 if (maximumRecordSize)
431 option_set(as, "maximumRecordSize", maximumRecordSize);
432 if (preferredMessageSize)
433 option_set(as, "preferredMessageSize", preferredMessageSize);
434 option_set(as, "otherInfo0", otherInfo[0]);
435 option_set(as, "otherInfo1", otherInfo[1]);
436 option_set(as, "otherInfo2", otherInfo[2]);
437 option_set(as, "clientIP", client_IP);
438 option_set(as, "piggyback", piggyback ? "1" : "0");
439 option_set_int(as, "start", 0);
440 option_set_int(as, "count", 0);
441 ZOOM_connection_connect(as->zoom_conn, zurl_str, 0);
443 as->persistent = persistent;
444 as->order = YAZSG(assoc_seq);
445 as->time_stamp = time(0);
449 ZOOM_resultset_destroy(as->zoom_set);
453 tsrm_mutex_unlock(yaz_mutex);
456 ZEND_REGISTER_RESOURCE(return_value, &shared_associations[i], le_link);
457 as->zval_resource = Z_LVAL_P(return_value);
461 /* {{{ proto bool yaz_close(resource id)
462 Destory and close target */
463 PHP_FUNCTION(yaz_close)
468 if (ZEND_NUM_ARGS() != 1) {
471 if (GET_PARM1(&id) == FAILURE) {
474 get_assoc(INTERNAL_FUNCTION_PARAM_PASSTHRU, id, &p);
479 zend_list_delete(id->value.lval);
485 /* {{{ proto bool yaz_search(resource id, string type, string query)
486 Specify query of type for search - returns true if successful */
487 PHP_FUNCTION(yaz_search)
489 char *query_str, *type_str;
490 zval *id, *type, *query;
493 if (ZEND_NUM_ARGS() == 3) {
494 if (GET_PARM3(&id, &type, &query) == FAILURE) {
501 get_assoc(INTERNAL_FUNCTION_PARAM_PASSTHRU, id, &p);
506 convert_to_string_ex(&type);
507 type_str = type->value.str.val;
508 convert_to_string_ex(&query);
509 query_str = query->value.str.val;
511 ZOOM_resultset_destroy(p->zoom_set);
516 if (!strcmp(type_str, "rpn")) {
517 ZOOM_query q = ZOOM_query_create();
518 if (ZOOM_query_prefix(q, query_str) == 0)
520 if (p->sort_criteria) {
521 ZOOM_query_sortby(q, p->sort_criteria);
523 xfree(p->sort_criteria);
524 p->sort_criteria = 0;
525 p->zoom_set = ZOOM_connection_search(p->zoom_conn, q);
528 ZOOM_query_destroy(q);
530 else if (!strcmp(type_str, "cql")) {
531 ZOOM_query q = ZOOM_query_create();
532 if (ZOOM_query_cql(q, query_str) == 0)
534 if (p->sort_criteria) {
535 ZOOM_query_sortby(q, p->sort_criteria);
537 xfree(p->sort_criteria);
538 p->sort_criteria = 0;
539 p->zoom_set = ZOOM_connection_search(p->zoom_conn, q);
542 ZOOM_query_destroy(q);
546 php_error_docref(NULL TSRMLS_CC, E_WARNING,
547 "Invalid query type %s", type_str);
553 /* {{{ proto bool yaz_present(resource id)
555 PHP_FUNCTION(yaz_present)
560 if (ZEND_NUM_ARGS() != 1) {
563 if (GET_PARM1(&id) == FAILURE) {
567 get_assoc(INTERNAL_FUNCTION_PARAM_PASSTHRU, id, &p);
573 size_t start = option_get_int(p, "start", 0);
574 size_t count = option_get_int(p, "count", 0);
576 ZOOM_resultset_records(p->zoom_set, 0 /* recs */, start, count);
584 /* {{{ proto bool yaz_wait([array options])
586 PHP_FUNCTION(yaz_wait)
588 zval *pval_options = 0;
591 ZOOM_connection conn_ar[MAX_ASSOC];
592 Yaz_Association conn_as[MAX_ASSOC];
595 if (ZEND_NUM_ARGS() == 1) {
597 long *event_bool = 0;
598 HashTable *options_ht = 0;
599 if (GET_PARM1(&pval_options) == FAILURE) {
602 if (Z_TYPE_PP(&pval_options) != IS_ARRAY) {
603 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Expected array parameter");
606 options_ht = Z_ARRVAL_PP(&pval_options);
607 val = array_lookup_long(options_ht, "timeout");
611 event_bool = array_lookup_bool(options_ht, "event");
612 if (event_bool && *event_bool)
616 tsrm_mutex_lock(yaz_mutex);
618 for (i = 0; i<YAZSG(max_links); i++) {
619 Yaz_Association p = shared_associations[i];
620 if (p && p->order == YAZSG(assoc_seq)) {
623 sprintf(str, "%d", timeout);
624 ZOOM_connection_option_set(p->zoom_conn, "timeout", str);
626 conn_ar[no++] = p->zoom_conn;
630 tsrm_mutex_unlock(yaz_mutex);
633 long ev = ZOOM_event(no, conn_ar);
637 Yaz_Association p = conn_as[ev-1];
638 int event_code = ZOOM_connection_last_event(p->zoom_conn);
640 add_assoc_long(pval_options, "connid", ev);
642 add_assoc_long(pval_options, "eventcode", event_code);
644 zend_list_addref(p->zval_resource);
645 Z_LVAL_P(return_value) = p->zval_resource;
646 Z_TYPE_P(return_value) = IS_RESOURCE;
652 while (ZOOM_event(no, conn_ar))
659 /* {{{ proto int yaz_errno(resource id)
660 Return last error number (>0 for bib-1 diagnostic, <0 for other error, 0 for no error */
661 PHP_FUNCTION(yaz_errno)
666 if (ZEND_NUM_ARGS() != 1 || GET_PARM1(&id) == FAILURE) {
669 get_assoc(INTERNAL_FUNCTION_PARAM_PASSTHRU, id, &p);
673 RETVAL_LONG(ZOOM_connection_errcode(p->zoom_conn));
678 /* {{{ proto string yaz_error(resource id)
679 Return last error message */
680 PHP_FUNCTION(yaz_error)
685 if (ZEND_NUM_ARGS() != 1 || GET_PARM1(&id) == FAILURE) {
689 get_assoc(INTERNAL_FUNCTION_PARAM_PASSTHRU, id, &p);
691 int code = ZOOM_connection_errcode(p->zoom_conn);
692 const char *msg = ZOOM_connection_errmsg(p->zoom_conn);
697 return_value->value.str.len = strlen(msg);
698 return_value->value.str.val = estrndup(msg, return_value->value.str.len);
699 return_value->type = IS_STRING;
705 /* {{{ proto string yaz_addinfo(resource id)
706 Return additional info for last error (empty string if none) */
707 PHP_FUNCTION(yaz_addinfo)
712 if (ZEND_NUM_ARGS() != 1 || GET_PARM1(&id) == FAILURE) {
716 get_assoc(INTERNAL_FUNCTION_PARAM_PASSTHRU, id, &p);
718 const char *addinfo = ZOOM_connection_addinfo(p->zoom_conn);
720 return_value->value.str.len = strlen(addinfo);
721 return_value->value.str.val = estrndup(addinfo, return_value->value.str.len);
722 return_value->type = IS_STRING;
728 /* {{{ proto int yaz_hits(resource id [, array searchresult])
729 Return number of hits (result count) for last search */
730 PHP_FUNCTION(yaz_hits)
732 zval *id, *searchresult = 0;
735 if (ZEND_NUM_ARGS() == 1) {
736 if (GET_PARM1(&id) == FAILURE) {
739 } else if (ZEND_NUM_ARGS() == 2) {
740 if (GET_PARM2(&id, &searchresult) == FAILURE) {
743 if (array_init(searchresult) == FAILURE) {
744 php_error_docref(NULL TSRMLS_CC, E_WARNING,
745 "Could not initialize search result array");
752 get_assoc(INTERNAL_FUNCTION_PARAM_PASSTHRU, id, &p);
753 if (p && p->zoom_set) {
754 RETVAL_LONG(ZOOM_resultset_size(p->zoom_set));
758 ZOOM_resultset_option_get(p->zoom_set, "resultSetStatus");
760 add_assoc_string(searchresult, "resultSetStatus",
766 ZOOM_resultset_option_get(p->zoom_set, "searchresult.size");
770 if (sz_str && *sz_str)
772 for (i = 0; i<sz; i++)
775 const char *opt_value;
778 MAKE_STD_ZVAL(zval_element);
779 array_init(zval_element);
780 add_next_index_zval(searchresult, zval_element);
782 sprintf(opt_name, "searchresult.%d.id", i);
783 opt_value = ZOOM_resultset_option_get(p->zoom_set, opt_name);
785 add_assoc_string(zval_element, "id",
786 (char *) opt_value, 1);
788 sprintf(opt_name, "searchresult.%d.count", i);
789 opt_value = ZOOM_resultset_option_get(p->zoom_set, opt_name);
791 add_assoc_long(zval_element, "count", atoi(opt_value));
793 sprintf(opt_name, "searchresult.%d.subquery.term", i);
794 opt_value = ZOOM_resultset_option_get(p->zoom_set, opt_name);
796 add_assoc_string(zval_element, "subquery.term",
797 (char *) opt_value, 1);
799 sprintf(opt_name, "searchresult.%d.interpretation.term", i);
800 opt_value = ZOOM_resultset_option_get(p->zoom_set, opt_name);
802 add_assoc_string(zval_element, "interpretation.term",
803 (char *) opt_value, 1);
805 sprintf(opt_name, "searchresult.%d.recommendation.term", i);
806 opt_value = ZOOM_resultset_option_get(p->zoom_set, opt_name);
808 add_assoc_string(zval_element, "recommendation.term",
809 (char *) opt_value, 1);
820 static Z_GenericRecord *marc_to_grs1(const char *buf, ODR o)
824 int indicator_length;
825 int identifier_length;
827 int length_data_entry;
829 int length_implementation;
830 int max_elements = 256;
831 Z_GenericRecord *r = odr_malloc(o, sizeof(*r));
832 r->elements = odr_malloc(o, sizeof(*r->elements) * max_elements);
835 record_length = atoi_n(buf, 5);
836 if (record_length < 25) {
839 indicator_length = atoi_n(buf + 10, 1);
840 identifier_length = atoi_n(buf + 11, 1);
841 base_address = atoi_n(buf + 12, 5);
843 length_data_entry = atoi_n(buf + 20, 1);
844 length_starting = atoi_n(buf + 21, 1);
845 length_implementation = atoi_n(buf + 22, 1);
847 for (entry_p = 24; buf[entry_p] != ISO2709_FS; ) {
848 entry_p += 3 + length_data_entry + length_starting;
849 if (entry_p >= record_length) {
855 Z_TaggedElement *tag;
856 tag = r->elements[r->num_elements++] = odr_malloc(o, sizeof(*tag));
857 tag->tagType = odr_malloc(o, sizeof(*tag->tagType));
859 tag->tagOccurrence = 0;
861 tag->appliedVariant = 0;
862 tag->tagValue = odr_malloc(o, sizeof(*tag->tagValue));
863 tag->tagValue->which = Z_StringOrNumeric_string;
864 tag->tagValue->u.string = odr_strdup(o, "leader");
866 tag->content = odr_malloc(o, sizeof(*tag->content));
867 tag->content->which = Z_ElementData_string;
868 tag->content->u.string = odr_strdupn(o, buf, 24);
870 base_address = entry_p + 1;
871 for (entry_p = 24; buf[entry_p] != ISO2709_FS; ) {
872 Z_TaggedElement *tag;
878 int identifier_flag = 1;
880 memcpy(tag_str, buf+entry_p, 3);
884 if ((r->num_elements + 1) >= max_elements) {
885 Z_TaggedElement **tmp = r->elements;
887 /* double array space, throw away old buffer (nibble memory) */
888 r->elements = odr_malloc(o, sizeof(*r->elements) * (max_elements *= 2));
889 memcpy(r->elements, tmp, r->num_elements * sizeof(*tmp));
891 tag = r->elements[r->num_elements++] = odr_malloc(o, sizeof(*tag));
892 tag->tagType = odr_malloc(o, sizeof(*tag->tagType));
894 tag->tagOccurrence = 0;
896 tag->appliedVariant = 0;
897 tag->tagValue = odr_malloc(o, sizeof(*tag->tagValue));
898 tag->tagValue->which = Z_StringOrNumeric_string;
899 tag->tagValue->u.string = odr_strdup(o, tag_str);
901 tag->content = odr_malloc(o, sizeof(*tag->content));
902 tag->content->which = Z_ElementData_subtree;
904 tag->content->u.subtree = odr_malloc(o, sizeof(*tag->content->u.subtree));
905 tag->content->u.subtree->elements = odr_malloc(o, sizeof(*r->elements));
906 tag->content->u.subtree->num_elements = 1;
908 tag = tag->content->u.subtree->elements[0] = odr_malloc(o, sizeof(**tag->content->u.subtree->elements));
910 tag->tagType = odr_malloc(o, sizeof(*tag->tagType));
912 tag->tagOccurrence = 0;
914 tag->appliedVariant = 0;
915 tag->tagValue = odr_malloc(o, sizeof(*tag->tagValue));
916 tag->tagValue->which = Z_StringOrNumeric_string;
917 tag->content = odr_malloc(o, sizeof(*tag->content));
919 data_length = atoi_n(buf + entry_p, length_data_entry);
920 entry_p += length_data_entry;
921 data_offset = atoi_n(buf + entry_p, length_starting);
922 entry_p += length_starting;
923 i = data_offset + base_address;
924 end_offset = i + data_length - 1;
926 if (indicator_length > 0 && indicator_length < 5) {
927 if (buf[i + indicator_length] != ISO2709_IDFS) {
930 } else if (!memcmp(tag_str, "00", 2)) {
934 if (identifier_flag && indicator_length) {
936 tag->tagValue->u.string = odr_malloc(o, indicator_length + 1);
937 memcpy(tag->tagValue->u.string, buf + i, indicator_length);
938 tag->tagValue->u.string[indicator_length] = '\0';
939 i += indicator_length;
941 tag->content->which = Z_ElementData_subtree;
943 tag->content->u.subtree = odr_malloc(o, sizeof(*tag->content->u.subtree));
944 tag->content->u.subtree->elements = odr_malloc(o, 256 * sizeof(*r->elements));
945 tag->content->u.subtree->num_elements = 0;
947 while (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS && i < end_offset) {
950 Z_TaggedElement *parent_tag = tag;
951 Z_TaggedElement *tag = odr_malloc(o, sizeof(*tag));
953 if (parent_tag->content->u.subtree->num_elements < 256) {
954 parent_tag->content->u.subtree->elements[
955 parent_tag->content->u.subtree->num_elements++] = tag;
958 tag->tagType = odr_malloc(o, sizeof(*tag->tagType));
960 tag->tagOccurrence = 0;
962 tag->appliedVariant = 0;
963 tag->tagValue = odr_malloc(o, sizeof(*tag->tagValue));
964 tag->tagValue->which = Z_StringOrNumeric_string;
967 tag->tagValue->u.string = odr_malloc(o, identifier_length);
968 memcpy(tag->tagValue->u.string, buf + i + 1, identifier_length - 1);
969 tag->tagValue->u.string[identifier_length - 1] = '\0';
970 i += identifier_length;
973 tag->content = odr_malloc(o, sizeof(*tag->content));
974 tag->content->which = Z_ElementData_string;
977 while ( buf[i] != ISO2709_RS &&
978 buf[i] != ISO2709_IDFS &&
979 buf[i] != ISO2709_FS && i < end_offset) {
983 tag->content->u.string = odr_malloc(o, i - i0 + 1);
984 memcpy(tag->content->u.string, buf + i0, i - i0);
985 tag->content->u.string[i - i0] = '\0';
990 tag->tagValue->u.string = "@";
991 tag->content->which = Z_ElementData_string;
993 while (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS && i < end_offset) {
996 tag->content->u.string = odr_malloc(o, i - i0 +1);
997 memcpy(tag->content->u.string, buf + i0, i - i0);
998 tag->content->u.string[i-i0] = '\0';
1011 static struct cvt_handle *cvt_open(const char *to, const char *from)
1013 ODR o = odr_createmem(ODR_ENCODE);
1015 struct cvt_handle *cvt = odr_malloc(o, sizeof(*cvt));
1018 cvt->buf = odr_malloc(o, cvt->size);
1021 cvt->cd = yaz_iconv_open(to, from);
1025 static void cvt_close(struct cvt_handle *cvt)
1028 yaz_iconv_close(cvt->cd);
1029 odr_destroy(cvt->odr);
1032 static const char *cvt_string(const char *input, struct cvt_handle *cvt)
1037 size_t inbytesleft = strlen(input);
1038 const char *inp = input;
1039 size_t outbytesleft = cvt->size - 1;
1040 char *outp = cvt->buf;
1041 size_t r = yaz_iconv(cvt->cd, (char**) &inp, &inbytesleft,
1042 &outp, &outbytesleft);
1043 if (r == (size_t) (-1)) {
1044 int e = yaz_iconv_error(cvt->cd);
1045 if (e != YAZ_ICONV_E2BIG || cvt->size > 200000)
1050 cvt->size = cvt->size * 2 + 30;
1051 cvt->buf = (char*) odr_malloc(cvt->odr, cvt->size);
1053 cvt->buf[outp - cvt->buf] = '\0';
1060 static void retval_array3_grs1(zval *return_value, Z_GenericRecord *p,
1061 struct cvt_handle *cvt)
1067 struct tag_list *next;
1069 NMEM nmem = nmem_create();
1071 array_init(return_value);
1072 for (i = 0; i<p->num_elements; i++)
1074 struct tag_list *tl;
1077 Z_TaggedElement *e = p->elements[i];
1078 char tagstr[32], *tag = 0;
1080 if (e->tagValue->which == Z_StringOrNumeric_numeric)
1082 sprintf(tagstr, ODR_INT_PRINTF, *e->tagValue->u.numeric);
1085 else if (e->tagValue->which == Z_StringOrNumeric_string)
1086 tag = e->tagValue->u.string, zval_element;
1091 for (tl = all_tags; tl; tl = tl->next)
1092 if (!strcmp(tl->tag, tag))
1095 zval_list = tl->zval_list;
1098 MAKE_STD_ZVAL(zval_list);
1099 array_init(zval_list);
1100 add_assoc_zval(return_value, tag, zval_list);
1102 tl = nmem_malloc(nmem, sizeof(*tl));
1103 tl->tag = nmem_strdup(nmem, tag);
1104 tl->zval_list = zval_list;
1105 tl->next = all_tags;
1108 MAKE_STD_ZVAL(zval_element);
1109 array_init(zval_element);
1110 add_next_index_zval(zval_list, zval_element);
1111 if (e->content->which == Z_ElementData_subtree)
1113 /* we have a subtree. Move to first child */
1114 Z_GenericRecord *sub = e->content->u.subtree;
1115 if (sub->num_elements >= 1)
1116 e = sub->elements[0];
1122 const char *tag = 0;
1123 if (e->tagValue->which == Z_StringOrNumeric_numeric)
1125 sprintf(tagstr, ODR_INT_PRINTF, *e->tagValue->u.numeric);
1128 else if (e->tagValue->which == Z_StringOrNumeric_string)
1129 tag = e->tagValue->u.string;
1130 if (tag && e->content->which == Z_ElementData_subtree)
1133 Z_GenericRecord *sub = e->content->u.subtree;
1135 for (i = 0; tag[i]; i++)
1140 sprintf(ind_idx, "ind%d", i+1);
1141 ind_val[0] = tag[i];
1144 add_assoc_string(zval_element, ind_idx, ind_val, 1);
1146 for (i = 0; i<sub->num_elements; i++)
1148 Z_TaggedElement *e = sub->elements[i];
1149 const char *tag = 0;
1150 if (e->tagValue->which == Z_StringOrNumeric_numeric)
1152 sprintf(tagstr, ODR_INT_PRINTF, *e->tagValue->u.numeric);
1155 else if (e->tagValue->which == Z_StringOrNumeric_string)
1156 tag = e->tagValue->u.string, zval_element;
1158 if (tag && e->content->which == Z_ElementData_string)
1160 const char *v = cvt_string(e->content->u.string, cvt);
1161 add_assoc_string(zval_element, (char*) tag, (char*) v,
1166 else if (tag && e->content->which == Z_ElementData_string)
1168 /* Leader or control field */
1169 const char *v = cvt_string(e->content->u.string, cvt);
1170 ZVAL_STRING(zval_element, (char*) v, 1);
1177 static void retval_array2_grs1(zval *return_value, Z_GenericRecord *p,
1178 struct cvt_handle *cvt)
1182 array_init(return_value);
1184 for (i = 0; i<p->num_elements; i++)
1188 Z_TaggedElement *e = p->elements[i];
1190 MAKE_STD_ZVAL(zval_element);
1191 array_init(zval_element);
1194 add_assoc_long(zval_element, "tagType", (long) *e->tagType);
1196 if (e->tagValue->which == Z_StringOrNumeric_string)
1197 add_assoc_string(zval_element, "tag", e->tagValue->u.string, 1);
1198 else if (e->tagValue->which == Z_StringOrNumeric_numeric)
1199 add_assoc_long(zval_element, "tag", (long) *e->tagValue->u.numeric);
1201 switch (e->content->which) {
1202 case Z_ElementData_string:
1205 const char *v = cvt_string(e->content->u.string, cvt);
1206 add_assoc_string(zval_element, "content", (char*) v, 1);
1209 case Z_ElementData_numeric:
1210 add_assoc_long(zval_element, "content", (long) *e->content->u.numeric);
1212 case Z_ElementData_trueOrFalse:
1213 add_assoc_bool(zval_element, "content",*e->content->u.trueOrFalse);
1215 case Z_ElementData_subtree:
1216 MAKE_STD_ZVAL(zval_sub);
1217 retval_array2_grs1(zval_sub, e->content->u.subtree, cvt);
1218 add_assoc_zval(zval_element, "content", zval_sub);
1220 add_next_index_zval(return_value, zval_element);
1224 static void retval_array1_grs1(zval *return_value, Z_GenericRecord *p,
1225 struct cvt_handle *cvt)
1227 Z_GenericRecord *grs[20];
1231 array_init(return_value);
1235 while (level >= 0) {
1237 Z_TaggedElement *e = 0;
1238 Z_GenericRecord *p = grs[level];
1243 if (eno[level] >= p->num_elements) {
1250 for (i = 0; i <= level; i++) {
1252 e = grs[i]->elements[eno[i]];
1255 tag_type = (long) *e->tagType;
1257 taglen = strlen(tag);
1258 sprintf(tag + taglen, "(%ld,", tag_type);
1259 taglen = strlen(tag);
1261 if (e->tagValue->which == Z_StringOrNumeric_string) {
1262 int len = strlen(e->tagValue->u.string);
1264 memcpy(tag + taglen, e->tagValue->u.string, len);
1265 tag[taglen+len] = '\0';
1266 } else if (e->tagValue->which == Z_StringOrNumeric_numeric) {
1267 sprintf(tag + taglen, ODR_INT_PRINTF, *e->tagValue->u.numeric);
1269 taglen = strlen(tag);
1270 strcpy(tag + taglen, ")");
1273 ALLOC_ZVAL(my_zval);
1274 array_init(my_zval);
1275 INIT_PZVAL(my_zval);
1277 add_next_index_string(my_zval, tag, 1);
1279 switch (e->content->which) {
1280 case Z_ElementData_string:
1283 const char *v = cvt_string(e->content->u.string, cvt);
1284 add_next_index_string(my_zval, (char*) v, 1);
1287 case Z_ElementData_numeric:
1288 add_next_index_long(my_zval, (long) *e->content->u.numeric);
1290 case Z_ElementData_trueOrFalse:
1291 add_next_index_long(my_zval, *e->content->u.trueOrFalse);
1293 case Z_ElementData_subtree:
1296 grs[level] = e->content->u.subtree;
1300 zend_hash_next_index_insert(return_value->value.ht, (void *) &my_zval, sizeof(zval *), NULL);
1305 static void ext_grs1(zval *return_value, char type_args[][60],
1307 void (*array_func)(zval *, Z_GenericRecord *,
1308 struct cvt_handle *))
1310 Z_External *ext = (Z_External *) ZOOM_record_get(r, "ext", 0);
1311 if (ext && ext->which == Z_External_OPAC)
1312 ext = ext->u.opac->bibliographicRecord;
1314 struct cvt_handle *cvt = 0;
1315 if (type_args[2][0])
1316 cvt = cvt_open(type_args[3], type_args[2]);
1318 cvt = cvt_open(0, 0);
1320 if (ext->which == Z_External_grs1) {
1321 retval_array1_grs1(return_value, ext->u.grs1, cvt);
1322 } else if (ext->which == Z_External_octet) {
1323 Z_GenericRecord *rec = 0;
1324 if (yaz_oid_is_iso2709(ext->direct_reference))
1326 char *buf = (char *) (ext->u.octet_aligned->buf);
1327 rec = marc_to_grs1(buf, cvt->odr);
1330 (*array_func)(return_value, rec, cvt);
1338 /* {{{ proto string yaz_record(resource id, int pos, string type)
1339 Return record information at given result set position */
1340 PHP_FUNCTION(yaz_record)
1342 zval *pval_id, *pval_pos, *pval_type;
1347 if (ZEND_NUM_ARGS() != 3) {
1350 if (GET_PARM3( &pval_id, &pval_pos, &pval_type) == FAILURE) {
1354 get_assoc(INTERNAL_FUNCTION_PARAM_PASSTHRU, pval_id, &p);
1356 convert_to_long_ex(&pval_pos);
1357 pos = pval_pos->value.lval;
1358 convert_to_string_ex(&pval_type);
1359 type = pval_type->value.str.val;
1361 if (p && p->zoom_set) {
1363 char type_args[4][60]; /* 0; 1=2,3 (1 is assumed charset) */
1364 type_args[0][0] = 0;
1365 type_args[1][0] = 0;
1366 type_args[2][0] = 0;
1367 type_args[3][0] = 0;
1368 sscanf(type, "%59[^;];%59[^=]=%59[^,],%59[^,]", type_args[0],
1369 type_args[1], type_args[2], type_args[3]);
1370 r = ZOOM_resultset_record(p->zoom_set, pos-1);
1371 if (!strcmp(type_args[0], "string")) {
1375 if (!strcmp(type_args[0], "array") ||
1376 !strcmp(type_args[0], "array1"))
1378 ext_grs1(return_value, type_args, r, retval_array1_grs1);
1379 } else if (!strcmp(type_args[0], "array2")) {
1380 ext_grs1(return_value, type_args, r, retval_array2_grs1);
1381 } else if (!strcmp(type_args[0], "array3")) {
1382 ext_grs1(return_value, type_args, r, retval_array3_grs1);
1385 const char *info = ZOOM_record_get(r, type, &rlen);
1387 return_value->value.str.len = (rlen > 0) ? rlen : 0;
1388 return_value->value.str.val =
1389 estrndup(info, return_value->value.str.len);
1390 return_value->type = IS_STRING;
1394 php_error_docref(NULL TSRMLS_CC, E_WARNING,
1395 "Bad yaz_record type %s - or unable "
1396 "to return record with type given", type);
1405 /* {{{ proto void yaz_syntax(resource id, string syntax)
1406 Set record syntax for retrieval */
1407 PHP_FUNCTION(yaz_syntax)
1409 zval *pval_id, *pval_syntax;
1412 if (ZEND_NUM_ARGS() != 2 ||
1413 GET_PARM2(&pval_id, &pval_syntax) == FAILURE) {
1417 get_assoc(INTERNAL_FUNCTION_PARAM_PASSTHRU, pval_id, &p);
1418 convert_to_string_ex(&pval_syntax);
1419 option_set(p, "preferredRecordSyntax", pval_syntax->value.str.val);
1424 /* {{{ proto void yaz_element(resource id, string elementsetname)
1425 Set Element-Set-Name for retrieval */
1426 PHP_FUNCTION(yaz_element)
1428 zval *pval_id, *pval_element;
1431 if (ZEND_NUM_ARGS() != 2 ||
1432 GET_PARM2(&pval_id, &pval_element) == FAILURE) {
1436 get_assoc(INTERNAL_FUNCTION_PARAM_PASSTHRU, pval_id, &p);
1438 convert_to_string_ex(&pval_element);
1439 option_set(p, "elementSetName", pval_element->value.str.val);
1444 /* {{{ proto void yaz_schema(resource id, string schema)
1445 Set Schema for retrieval */
1446 PHP_FUNCTION(yaz_schema)
1448 zval *pval_id, *pval_element;
1451 if (ZEND_NUM_ARGS() != 2 ||
1452 GET_PARM2(&pval_id, &pval_element) == FAILURE) {
1456 get_assoc(INTERNAL_FUNCTION_PARAM_PASSTHRU, pval_id, &p);
1457 convert_to_string_ex(&pval_element);
1458 option_set(p, "schema", pval_element->value.str.val);
1463 /* {{{ proto void yaz_set_option(resource id, mixed options)
1464 Set Option(s) for connection */
1465 PHP_FUNCTION(yaz_set_option)
1467 zval *pval_ar, *pval_name, *pval_val, *pval_id;
1470 if (ZEND_NUM_ARGS() == 2) {
1471 if (GET_PARM2(&pval_id, &pval_ar) == FAILURE) {
1474 if (Z_TYPE_PP(&pval_ar) != IS_ARRAY) {
1477 get_assoc(INTERNAL_FUNCTION_PARAM_PASSTHRU, pval_id, &p);
1483 ht = Z_ARRVAL_PP(&pval_ar);
1484 for(zend_hash_internal_pointer_reset_ex(ht, &pos);
1485 zend_hash_get_current_data_ex(ht, (void**) &ent, &pos) == SUCCESS;
1486 zend_hash_move_forward_ex(ht, &pos)
1490 #if PHP_API_VERSION > 20010101
1491 int type = zend_hash_get_current_key_ex(ht, &key, 0, &idx, 0, &pos);
1493 int type = zend_hash_get_current_key_ex(ht, &key, 0, &idx, &pos);
1495 if (type != HASH_KEY_IS_STRING || Z_TYPE_PP(ent) != IS_STRING) {
1498 option_set(p, key, (*ent)->value.str.val);
1502 } else if (ZEND_NUM_ARGS() == 3) {
1503 if (GET_PARM3( &pval_id, &pval_name, &pval_val) == FAILURE) {
1506 get_assoc (INTERNAL_FUNCTION_PARAM_PASSTHRU, pval_id, &p);
1507 convert_to_string_ex(&pval_name);
1508 convert_to_string_ex(&pval_val);
1509 option_set(p, pval_name->value.str.val, pval_val->value.str.val);
1518 /* {{{ proto string yaz_get_option(resource id, string name)
1519 Set Option(s) for connection */
1520 PHP_FUNCTION(yaz_get_option)
1522 zval *pval_id, *pval_name;
1525 if (ZEND_NUM_ARGS() != 2) {
1528 if (GET_PARM2(&pval_id, &pval_name) == FAILURE) {
1532 get_assoc(INTERNAL_FUNCTION_PARAM_PASSTHRU, pval_id, &p);
1534 const char *name_str, *v;
1535 convert_to_string_ex(&pval_name);
1536 name_str = pval_name->value.str.val;
1538 v = option_get(p, name_str);
1542 return_value->value.str.len = strlen(v);
1543 return_value->value.str.val = estrndup(v, return_value->value.str.len);
1544 return_value->type = IS_STRING;
1552 /* {{{ proto void yaz_range(resource id, int start, int number)
1553 Set result set start point and number of records to request */
1554 PHP_FUNCTION(yaz_range)
1556 zval *pval_id, *pval_start, *pval_number;
1560 if (ZEND_NUM_ARGS() != 3 ||
1561 GET_PARM3( &pval_id, &pval_start, &pval_number) == FAILURE) {
1565 get_assoc(INTERNAL_FUNCTION_PARAM_PASSTHRU, pval_id, &p);
1566 convert_to_long_ex(&pval_start);
1567 convert_to_long_ex(&pval_number);
1568 start = pval_start->value.lval;
1569 count = pval_number->value.lval;
1570 option_set_int(p, "start", start > 0 ? start - 1 : 0);
1571 option_set_int(p, "count", pval_number->value.lval);
1576 /* {{{ proto void yaz_sort(resource id, string sortspec)
1577 Set result set sorting criteria */
1578 PHP_FUNCTION(yaz_sort)
1580 zval *pval_id, *pval_criteria;
1583 if (ZEND_NUM_ARGS() != 2 ||
1584 GET_PARM2(&pval_id, &pval_criteria) == FAILURE) {
1588 get_assoc(INTERNAL_FUNCTION_PARAM_PASSTHRU, pval_id, &p);
1590 convert_to_string_ex(&pval_criteria);
1591 xfree(p->sort_criteria);
1592 p->sort_criteria = xstrdup(pval_criteria->value.str.val);
1594 ZOOM_resultset_sort(p->zoom_set, "yaz",
1595 pval_criteria->value.str.val);
1601 const char *ill_array_lookup(void *handle, const char *name)
1603 return array_lookup_string((HashTable *) handle, name);
1606 /* {{{ proto void yaz_itemorder(resource id, array package)
1607 Sends Item Order request */
1608 PHP_FUNCTION(yaz_itemorder)
1610 zval *pval_id, *pval_package;
1613 if (ZEND_NUM_ARGS() != 2 ||
1614 GET_PARM2(&pval_id, &pval_package) == FAILURE) {
1617 if (Z_TYPE_PP(&pval_package) != IS_ARRAY) {
1618 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Expected array parameter");
1622 get_assoc(INTERNAL_FUNCTION_PARAM_PASSTHRU, pval_id, &p);
1624 ZOOM_options options = ZOOM_options_create();
1626 ZOOM_options_set_callback(options,
1627 ill_array_lookup, Z_ARRVAL_PP(&pval_package));
1628 ZOOM_package_destroy(p->zoom_package);
1629 p->zoom_package = ZOOM_connection_package(p->zoom_conn, options);
1630 ZOOM_package_send(p->zoom_package, "itemorder");
1631 ZOOM_options_set_callback(options, 0, 0);
1632 ZOOM_options_destroy(options);
1638 /* {{{ proto void yaz_es(resource id, string type, array package)
1639 Sends Extended Services Request */
1640 PHP_FUNCTION(yaz_es)
1642 zval *pval_id, *pval_type, *pval_package;
1645 if (ZEND_NUM_ARGS() != 3 ||
1646 GET_PARM3( &pval_id, &pval_type, &pval_package) == FAILURE) {
1649 if (Z_TYPE_PP(&pval_type) != IS_STRING) {
1650 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Expected string parameter");
1654 if (Z_TYPE_PP(&pval_package) != IS_ARRAY) {
1655 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Expected array parameter");
1659 get_assoc(INTERNAL_FUNCTION_PARAM_PASSTHRU, pval_id, &p);
1661 ZOOM_options options = ZOOM_options_create();
1663 ZOOM_options_set_callback(options, ill_array_lookup,
1664 Z_ARRVAL_PP(&pval_package));
1665 ZOOM_package_destroy(p->zoom_package);
1666 p->zoom_package = ZOOM_connection_package(p->zoom_conn, options);
1667 ZOOM_package_send(p->zoom_package, pval_type->value.str.val);
1668 ZOOM_options_set_callback(options, 0, 0);
1669 ZOOM_options_destroy(options);
1675 /* {{{ proto void yaz_scan(resource id, type, query [, flags])
1676 Sends Scan Request */
1677 PHP_FUNCTION(yaz_scan)
1679 zval *pval_id, *pval_type, *pval_query, *pval_flags = 0;
1680 HashTable *flags_ht = 0;
1683 if (ZEND_NUM_ARGS() == 3) {
1684 if (GET_PARM3( &pval_id, &pval_type, &pval_query) == FAILURE) {
1687 } else if (ZEND_NUM_ARGS() == 4) {
1688 if (GET_PARM4(&pval_id, &pval_type, &pval_query, &pval_flags)
1692 if (Z_TYPE_PP(&pval_flags) != IS_ARRAY) {
1693 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Bad flags parameter");
1696 flags_ht = Z_ARRVAL_PP(&pval_flags);
1701 convert_to_string_ex(&pval_type);
1702 convert_to_string_ex(&pval_query);
1704 get_assoc(INTERNAL_FUNCTION_PARAM_PASSTHRU, pval_id, &p);
1705 ZOOM_scanset_destroy(p->zoom_scan);
1708 option_set(p, "number", array_lookup_string(flags_ht, "number"));
1709 option_set(p, "position", array_lookup_string(flags_ht, "position"));
1710 option_set(p, "stepSize", array_lookup_string(flags_ht, "stepsize"));
1711 p->zoom_scan = ZOOM_connection_scan(p->zoom_conn,
1712 Z_STRVAL_PP(&pval_query));
1718 /* {{{ proto array yaz_es_result(resource id)
1719 Inspects Extended Services Result */
1720 PHP_FUNCTION(yaz_es_result)
1725 if (ZEND_NUM_ARGS() != 1 || GET_PARM1(&pval_id) == FAILURE) {
1729 array_init(return_value);
1731 get_assoc(INTERNAL_FUNCTION_PARAM_PASSTHRU, pval_id, &p);
1732 if (p && p->zoom_package) {
1733 const char *str = ZOOM_package_option_get(p->zoom_package,
1737 add_assoc_string(return_value, "targetReference", (char *) str, 1);
1739 str = ZOOM_package_option_get(p->zoom_package,
1742 add_assoc_string(return_value, "xmlUpdateDoc", (char *) str, 1);
1749 /* {{{ proto array yaz_scan_result(resource id [, array options])
1750 Inspects Scan Result */
1751 PHP_FUNCTION(yaz_scan_result)
1753 zval *pval_id, *pval_opt = 0;
1756 if (ZEND_NUM_ARGS() == 2) {
1757 if (GET_PARM2(&pval_id, &pval_opt) == FAILURE) {
1760 } else if (ZEND_NUM_ARGS() == 1) {
1761 if (GET_PARM1(&pval_id) == FAILURE) {
1768 array_init(return_value);
1770 if (pval_opt && array_init(pval_opt) == FAILURE) {
1774 get_assoc(INTERNAL_FUNCTION_PARAM_PASSTHRU, pval_id, &p);
1775 if (p && p->zoom_scan) {
1777 /* ZOOM_scanset_term changed from YAZ 3 to YAZ 4 */
1778 #if YAZ_VERSIONL >= 0x040000
1783 int size = ZOOM_scanset_size(p->zoom_scan);
1785 for (pos = 0; pos < size; pos++) {
1786 const char *term = ZOOM_scanset_term(p->zoom_scan, pos, &occ, &len);
1789 ALLOC_ZVAL(my_zval);
1790 array_init(my_zval);
1791 INIT_PZVAL(my_zval);
1793 add_next_index_string(my_zval, "term", 1);
1796 add_next_index_stringl(my_zval, (char*) term, len, 1);
1798 add_next_index_string(my_zval, "?", 1);
1800 add_next_index_long(my_zval, occ);
1802 term = ZOOM_scanset_display_term(p->zoom_scan, pos, &occ, &len);
1805 add_next_index_stringl(my_zval, (char*) term, len, 1);
1807 add_next_index_string(my_zval, "?", 1);
1810 zend_hash_next_index_insert(return_value->value.ht, (void *) &my_zval, sizeof(zval *), NULL);
1816 add_assoc_long(pval_opt, "number", size);
1818 v = ZOOM_scanset_option_get(p->zoom_scan, "stepSize");
1820 add_assoc_long(pval_opt, "stepsize", atoi(v));
1822 v = ZOOM_scanset_option_get(p->zoom_scan, "position");
1824 add_assoc_long(pval_opt, "position", atoi(v));
1826 v = ZOOM_scanset_option_get(p->zoom_scan, "scanStatus");
1828 add_assoc_long(pval_opt, "status", atoi(v));
1836 /* {{{ proto void yaz_ccl_conf(resource id, array package)
1837 Configure CCL package */
1838 PHP_FUNCTION(yaz_ccl_conf)
1840 zval *pval_id, *pval_package;
1843 if (ZEND_NUM_ARGS() != 2 ||
1844 GET_PARM2(&pval_id, &pval_package) == FAILURE) {
1848 if (Z_TYPE_PP(&pval_package) != IS_ARRAY) {
1849 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Expected array parameter");
1853 get_assoc(INTERNAL_FUNCTION_PARAM_PASSTHRU, pval_id, &p);
1855 HashTable *ht = Z_ARRVAL_PP(&pval_package);
1860 ccl_qual_rm(&p->bibset);
1861 p->bibset = ccl_qual_mk();
1863 for(zend_hash_internal_pointer_reset_ex(ht, &pos);
1864 zend_hash_get_current_data_ex(ht, (void**) &ent, &pos) == SUCCESS;
1865 zend_hash_move_forward_ex(ht, &pos)
1868 #if PHP_API_VERSION > 20010101
1869 int type = zend_hash_get_current_key_ex(ht, &key, 0, &idx, 0, &pos);
1871 int type = zend_hash_get_current_key_ex(ht, &key, 0, &idx, &pos);
1873 if (type != HASH_KEY_IS_STRING || Z_TYPE_PP(ent) != IS_STRING) {
1876 ccl_qual_fitem(p->bibset, (*ent)->value.str.val, key);
1883 /* {{{ proto bool yaz_ccl_parse(resource id, string query, array res)
1884 Parse a CCL query */
1885 PHP_FUNCTION(yaz_ccl_parse)
1887 zval *pval_id, *pval_query, *pval_res = 0;
1890 if (ZEND_NUM_ARGS() != 3 || GET_PARM3( &pval_id, &pval_query, &pval_res)
1895 zval_dtor(pval_res);
1896 array_init(pval_res);
1897 convert_to_string_ex(&pval_query);
1899 get_assoc(INTERNAL_FUNCTION_PARAM_PASSTHRU, pval_id, &p);
1901 const char *query_str = pval_query->value.str.val;
1902 struct ccl_rpn_node *rpn;
1905 CCL_parser ccl_parser = ccl_parser_create(p->bibset);
1907 rpn = ccl_parser_find_str(ccl_parser, query_str);
1909 error_code = ccl_parser_get_error(ccl_parser, &error_pos);
1910 add_assoc_long(pval_res, "errorcode", error_code);
1914 add_assoc_string(pval_res, "errorstring",
1915 (char *) ccl_err_msg(error_code), 1);
1916 add_assoc_long(pval_res, "errorpos", error_pos);
1921 WRBUF wrbuf_pqf = wrbuf_alloc();
1922 ccl_stop_words_t csw = ccl_stop_words_create();
1923 int r = ccl_stop_words_tree(csw, p->bibset, &rpn);
1927 /* stop words were removed. Return stopwords info */
1928 zval *zval_stopwords;
1931 MAKE_STD_ZVAL(zval_stopwords);
1932 array_init(zval_stopwords);
1933 for (idx = 0; ; idx++)
1935 zval *zval_stopword;
1939 if (!ccl_stop_words_info(csw, idx, &qname, &term))
1942 MAKE_STD_ZVAL(zval_stopword);
1943 array_init(zval_stopword);
1945 add_assoc_string(zval_stopword, "field", (char *) qname, 1);
1946 add_assoc_string(zval_stopword, "term", (char *) term, 1);
1947 add_next_index_zval(zval_stopwords, zval_stopword);
1949 add_assoc_zval(pval_res, "stopwords", zval_stopwords);
1951 ccl_pquery(wrbuf_pqf, rpn);
1952 add_assoc_stringl(pval_res, "rpn",
1953 wrbuf_buf(wrbuf_pqf), wrbuf_len(wrbuf_pqf), 1);
1954 wrbuf_destroy(wrbuf_pqf);
1955 ccl_stop_words_destroy(csw);
1958 ccl_rpn_delete(rpn);
1966 /* {{{ proto bool yaz_database (resource id, string databases)
1967 Specify the databases within a session */
1968 PHP_FUNCTION(yaz_database)
1970 zval *pval_id, *pval_database;
1973 if (ZEND_NUM_ARGS() != 2 ||
1974 GET_PARM2(&pval_id, &pval_database) == FAILURE) {
1978 convert_to_string_ex(&pval_database);
1979 get_assoc(INTERNAL_FUNCTION_PARAM_PASSTHRU, pval_id, &p);
1980 option_set(p, "databaseName", pval_database->value.str.val);
1986 /* {{{ php_yaz_init_globals
1988 static void php_yaz_init_globals(zend_yaz_globals *yaz_globals)
1990 yaz_globals->assoc_seq = 0;
1991 yaz_globals->max_links = 100;
1992 yaz_globals->keepalive = 120;
1993 yaz_globals->log_file = NULL;
1994 yaz_globals->log_mask = NULL;
1998 static void yaz_close_session(Yaz_Association *as TSRMLS_DC)
2000 if (*as && (*as)->order == YAZSG(assoc_seq)) {
2001 if ((*as)->persistent) {
2004 yaz_association_destroy(*as);
2010 static void yaz_close_link(zend_rsrc_list_entry *rsrc TSRMLS_DC)
2012 Yaz_Association *as = (Yaz_Association *) rsrc->ptr;
2013 yaz_close_session(as TSRMLS_CC);
2016 /* {{{ PHP_INI_BEGIN
2019 #if PHP_MAJOR_VERSION >= 5
2020 STD_PHP_INI_ENTRY("yaz.max_links", "100", PHP_INI_ALL, OnUpdateLong, max_links, zend_yaz_globals, yaz_globals)
2022 STD_PHP_INI_ENTRY("yaz.max_links", "100", PHP_INI_ALL, OnUpdateInt, max_links, zend_yaz_globals, yaz_globals)
2024 #if PHP_MAJOR_VERSION >= 5
2025 STD_PHP_INI_ENTRY("yaz.keepalive", "120", PHP_INI_ALL, OnUpdateLong, keepalive, zend_yaz_globals, yaz_globals)
2027 STD_PHP_INI_ENTRY("yaz.keepalive", "120", PHP_INI_ALL, OnUpdateInt, keepalive, zend_yaz_globals, yaz_globals)
2029 STD_PHP_INI_ENTRY("yaz.log_file", NULL, PHP_INI_ALL, OnUpdateString, log_file, zend_yaz_globals, yaz_globals)
2030 STD_PHP_INI_ENTRY("yaz.log_mask", NULL, PHP_INI_ALL, OnUpdateString, log_mask, zend_yaz_globals, yaz_globals)
2034 PHP_MINIT_FUNCTION(yaz)
2040 yaz_mutex = tsrm_mutex_alloc();
2043 ZEND_INIT_MODULE_GLOBALS(yaz, php_yaz_init_globals, NULL);
2045 REGISTER_INI_ENTRIES();
2047 REGISTER_LONG_CONSTANT("ZOOM_EVENT_NONE", ZOOM_EVENT_NONE,
2048 CONST_CS|CONST_PERSISTENT);
2049 REGISTER_LONG_CONSTANT("ZOOM_EVENT_CONNECT", ZOOM_EVENT_CONNECT,
2050 CONST_CS|CONST_PERSISTENT);
2051 REGISTER_LONG_CONSTANT("ZOOM_EVENT_SEND_DATA", ZOOM_EVENT_SEND_DATA,
2052 CONST_CS|CONST_PERSISTENT);
2053 REGISTER_LONG_CONSTANT("ZOOM_EVENT_RECV_DATA", ZOOM_EVENT_RECV_DATA,
2054 CONST_CS|CONST_PERSISTENT);
2055 REGISTER_LONG_CONSTANT("ZOOM_EVENT_TIMEOUT", ZOOM_EVENT_TIMEOUT,
2056 CONST_CS|CONST_PERSISTENT);
2057 REGISTER_LONG_CONSTANT("ZOOM_EVENT_UNKNOWN", ZOOM_EVENT_UNKNOWN,
2058 CONST_CS|CONST_PERSISTENT);
2059 REGISTER_LONG_CONSTANT("ZOOM_EVENT_SEND_APDU", ZOOM_EVENT_SEND_APDU,
2060 CONST_CS|CONST_PERSISTENT);
2061 REGISTER_LONG_CONSTANT("ZOOM_EVENT_RECV_APDU", ZOOM_EVENT_RECV_APDU,
2062 CONST_CS|CONST_PERSISTENT);
2063 REGISTER_LONG_CONSTANT("ZOOM_EVENT_RECV_RECORD", ZOOM_EVENT_RECV_RECORD,
2064 CONST_CS|CONST_PERSISTENT);
2065 REGISTER_LONG_CONSTANT("ZOOM_EVENT_RECV_SEARCH", ZOOM_EVENT_RECV_SEARCH,
2066 CONST_CS|CONST_PERSISTENT);
2068 fname = YAZSG(log_file);
2069 mask = YAZSG(log_mask);
2070 if (fname && *fname)
2072 yaz_log_init_file(fname);
2075 yaz_log_init_level(yaz_log_mask_str(mask));
2078 yaz_log_init_level(0);
2080 le_link = zend_register_list_destructors_ex(yaz_close_link, 0, "YAZ link", module_number);
2082 order_associations = 1;
2083 shared_associations = xmalloc(sizeof(*shared_associations) * MAX_ASSOC);
2084 for (i = 0; i < MAX_ASSOC; i++) {
2085 shared_associations[i] = 0;
2090 PHP_MSHUTDOWN_FUNCTION(yaz)
2094 if (shared_associations) {
2095 for (i = 0; i < MAX_ASSOC; i++) {
2096 yaz_association_destroy (shared_associations[i]);
2098 xfree(shared_associations);
2099 shared_associations = 0;
2102 tsrm_mutex_free(yaz_mutex);
2105 yaz_log_init_file(0);
2107 UNREGISTER_INI_ENTRIES();
2112 PHP_MINFO_FUNCTION(yaz)
2114 char version_str[20];
2116 strcpy(version_str, "unknown");
2117 yaz_version(version_str, 0);
2118 php_info_print_table_start();
2119 php_info_print_table_row(2, "YAZ Support", "enabled");
2120 php_info_print_table_row(2, "PHP/YAZ Version", PHP_YAZ_VERSION);
2121 php_info_print_table_row(2, "YAZ Version", version_str);
2122 php_info_print_table_row(2, "Compiled with YAZ version", YAZ_VERSION);
2123 php_info_print_table_end();
2126 PHP_RSHUTDOWN_FUNCTION(yaz)
2132 tsrm_mutex_lock(yaz_mutex);
2134 for (i = 0; i < YAZSG(max_links); i++) {
2135 Yaz_Association *as = shared_associations + i;
2138 if (now - (*as)->time_stamp > YAZSG(keepalive))
2140 yaz_association_destroy(*as);
2146 tsrm_mutex_unlock(yaz_mutex);
2151 PHP_RINIT_FUNCTION(yaz)
2155 sprintf(pidstr, "%ld", (long) getpid());
2157 tsrm_mutex_lock(yaz_mutex);
2159 YAZSG(assoc_seq) = order_associations++;
2161 tsrm_mutex_unlock(yaz_mutex);
2163 yaz_log_init_prefix(pidstr);
2167 zend_module_entry yaz_module_entry = {
2168 #if ZEND_MODULE_API_NO >= 20010901
2169 STANDARD_MODULE_HEADER,
2178 #if ZEND_MODULE_API_NO >= 20010901
2181 STANDARD_MODULE_PROPERTIES
2192 * vim600: sw=4 ts=4 fdm=marker
2193 * vim<600: sw=4 ts=4