1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2010 Index Data
3 * See the file LICENSE for details.
7 * \brief Implements SRW/SRU utilities.
13 #include <yaz/matchstr.h>
14 #include <yaz/yaz-iconv.h>
16 #include <yaz/facet.h>
21 #include <libxml/parser.h>
22 #include <libxml/tree.h>
24 #define SOLR_MAX_PARAMETERS 100
26 const char *xml_node_attribute_value_get(xmlNodePtr ptr, const char *node_name, const char *attribute_name) {
28 struct _xmlAttr *attr;
29 // check if the node name matches
30 if (strcmp((const char*) ptr->name, node_name))
32 // check if the attribute name and return the value
33 for (attr = ptr->properties; attr; attr = attr->next)
34 if (attr->children && attr->children->type == XML_TEXT_NODE) {
35 if (!strcmp((const char *) attr->name, attribute_name))
36 return (const char *) attr->children->content;
42 static int match_xml_node_attribute(xmlNodePtr ptr, const char *node_name, const char *attribute_name, const char *value)
44 const char *attribute_value;
45 // check if the node name matches
46 if (strcmp((const char*) ptr->name, node_name))
48 attribute_value = xml_node_attribute_value_get(ptr, node_name, attribute_name);
49 if (attribute_value && !strcmp(attribute_value, value))
54 static void yaz_solr_decode_result_docs(ODR o, xmlNodePtr ptr, Odr_int start, Z_SRW_searchRetrieveResponse *sr) {
60 for (node = ptr->children; node; node = node->next)
61 if (node->type == XML_ELEMENT_NODE)
64 sr->records = odr_malloc(o, sizeof(*sr->records) * sr->num_records);
66 for (node = ptr->children; node; node = node->next)
68 if (node->type == XML_ELEMENT_NODE)
70 Z_SRW_record *record = sr->records + i;
71 xmlBufferPtr buf = xmlBufferCreate();
72 xmlNode *tmp = xmlCopyNode(node, 1);
74 xmlNodeDump(buf, tmp->doc, tmp, 0, 0);
78 record->recordSchema = 0;
79 record->recordPacking = Z_SRW_recordPacking_XML;
80 record->recordData_len = buf->use;
81 record->recordData_buf = odr_malloc(o, buf->use + 1);
82 memcpy(record->recordData_buf, buf->content, buf->use);
83 record->recordData_buf[buf->use] = '\0';
84 // TODO Solve the real problem in zoom-sru, that doesnt work with 0-based indexes.
85 // Work-around: Making the recordPosition 1-based.
86 record->recordPosition = odr_intdup(o, start + offset + 1);
96 static void yaz_solr_decode_result(ODR o, xmlNodePtr ptr, Z_SRW_searchRetrieveResponse *sr) {
98 struct _xmlAttr *attr;
99 for (attr = ptr->properties; attr; attr = attr->next)
100 if (attr->children && attr->children->type == XML_TEXT_NODE) {
101 if (!strcmp((const char *) attr->name, "numFound")) {
102 sr->numberOfRecords = odr_intdup(o, odr_atoi(
103 (const char *) attr->children->content));
104 } else if (!strcmp((const char *) attr->name, "start")) {
105 start = odr_atoi((const char *) attr->children->content);
108 yaz_solr_decode_result_docs(o, ptr, start, sr);
111 static Z_AttributeList *yaz_solr_use_atttribute_create(ODR o, const char *name) {
117 static const char *get_facet_term_count(xmlNodePtr node, int *freq) {
122 Z_FacetField *yaz_solr_decode_facet_field(ODR o, xmlNodePtr ptr, Z_SRW_searchRetrieveResponse *sr)
125 const char* name = xml_node_attribute_value_get(ptr, "lst", "name");
126 Z_AttributeList *list = yaz_solr_use_atttribute_create(o, name);
127 Z_FacetField *facet_field;
131 for (node = ptr->children; node; node = node->next) {
134 facet_field = facet_field_create(o, list, num_terms);
136 for (node = ptr->children; node; node = node->next) {
138 const char *term = get_facet_term_count(node, &count);
139 facet_field_term_set(o, facet_field, facet_term_create(o, term_create(o, term), count), index);
145 static void yaz_solr_decode_facet_counts(ODR o, xmlNodePtr root, Z_SRW_searchRetrieveResponse *sr) {
147 for (ptr = root->children; ptr; ptr = ptr->next)
149 if (match_xml_node_attribute(ptr, "lst", "name", "facet_fields"))
152 Z_FacetList *facet_list;
154 for (node = ptr->children; node; node= node->next)
158 facet_list = facet_list_create(o, num_facets);
160 for (node = ptr->children; node; node= node->next)
162 facet_list_field_set(o, facet_list, yaz_solr_decode_facet_field(o, node, sr), num_facets);
165 sr->facetList = facet_list;
171 static void yaz_solr_decode_facets(ODR o, xmlNodePtr ptr, Z_SRW_searchRetrieveResponse *sr) {
172 if (match_xml_node_attribute(ptr, "lst", "name", "facet_counts"))
173 yaz_solr_decode_facet_counts(o, ptr->children, sr);
177 int yaz_solr_decode_response(ODR o, Z_HTTP_Response *hres, Z_SRW_PDU **pdup)
180 const char *content_buf = hres->content_buf;
181 int content_len = hres->content_len;
182 xmlDocPtr doc = xmlParseMemory(content_buf, content_len);
185 Z_SRW_PDU *pdu = yaz_srw_get(o, Z_SRW_searchRetrieve_response);
186 Z_SRW_searchRetrieveResponse *sr = pdu->u.response;
194 xmlNodePtr root = xmlDocGetRootElement(doc);
199 else if (strcmp((const char *) root->name, "response"))
205 /** look for result (required) and facets node (optional) */
208 for (ptr = root->children; ptr; ptr = ptr->next)
210 if (ptr->type == XML_ELEMENT_NODE &&
211 !strcmp((const char *) ptr->name, "result"))
212 rc_result = yaz_solr_decode_result(o, ptr, sr);
213 if (match_xml_node_attribute(ptr, "lst", "name", "facet_counts"))
214 rc_facets = yaz_solr_decode_facet_counts(o, ptr, sr);
216 ret = rc_result + rc_facets;
229 static void yaz_solr_encode_facet_field(ODR encode, char **name, char **value, int *i, Z_FacetField *facet_field, int *limit) {
230 Z_AttributeList *attribute_list = facet_field->attributes;
231 struct yaz_facet_attr attr_values;
232 yaz_facet_attr_init(&attr_values);
233 yaz_facet_attr_get_z_attributes(attribute_list, &attr_values);
234 // TODO do we want to support server decided
235 if (!attr_values.errcode && attr_values.useattr) {
236 yaz_add_name_value_str(encode, name, value, i, "facet.field", (char *) attr_values.useattr);
237 // TODO max(attr_values, *limit);
238 if (attr_values.limit > 0 && attr_values.limit > *limit) {
239 *limit = attr_values.limit;
244 static void yaz_solr_encode_facet_list(ODR encode, char **name, char **value, int *i, Z_FacetList *facet_list, int *limit) {
247 for (index = 0; index < facet_list->num; index++) {
248 yaz_solr_encode_facet_field(encode, name, value, i, facet_list->elements[index], limit);
254 int yaz_solr_encode_request(Z_HTTP_Request *hreq, Z_SRW_PDU *srw_pdu,
255 ODR encode, const char *charset)
257 const char *solr_op = 0;
258 //TODO Change. not a nice hard coded, unchecked limit.
259 char *name[SOLR_MAX_PARAMETERS], *value[SOLR_MAX_PARAMETERS];
264 z_HTTP_header_add_basic_auth(encode, &hreq->headers,
265 srw_pdu->username, srw_pdu->password);
267 switch (srw_pdu->which)
269 case Z_SRW_searchRetrieve_request: {
270 Z_SRW_searchRetrieveRequest *request = srw_pdu->u.request;
272 switch(srw_pdu->u.request->query_type)
274 case Z_SRW_query_type_pqf:
275 yaz_add_name_value_str(encode, name, value, &i,
276 "q", request->query.pqf);
278 case Z_SRW_query_type_cql:
279 yaz_add_name_value_str(encode, name, value, &i,
280 "q", request->query.cql);
285 if (srw_pdu->u.request->startRecord)
287 Odr_int start = *request->startRecord - 1;
288 yaz_add_name_value_int(encode, name, value, &i,
291 yaz_add_name_value_int(encode, name, value, &i,
292 "rows", request->maximumRecords);
293 yaz_add_name_value_str(encode, name, value, &i,
294 "fl", request->recordSchema);
296 if (request->facetList) {
297 Z_FacetList *facet_list = request->facetList;
300 yaz_add_name_value_str(encode, name, value, &i, "facet", "true");
301 yaz_solr_encode_facet_list(encode, name, value, &i, facet_list, &limit);
303 yaz_add_name_value_int(encode, name, value, &i, "facet.limit", &olimit);
312 yaz_array_to_uri(&uri_args, encode, name, value);
314 hreq->method = "GET";
317 odr_malloc(encode, strlen(hreq->path) +
318 strlen(uri_args) + strlen(solr_op) + 4);
320 sprintf(path, "%s/%s?%s", hreq->path, solr_op, uri_args);
323 z_HTTP_header_add_content_type(encode, &hreq->headers,
324 "text/xml", charset);
332 * c-file-style: "Stroustrup"
333 * indent-tabs-mode: nil
335 * vim: shiftwidth=4 tabstop=8 expandtab