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 node */
206 for (ptr = root->children; ptr; ptr = ptr->next)
208 if (ptr->type == XML_ELEMENT_NODE &&
209 !strcmp((const char *) ptr->name, "result"))
210 yaz_solr_decode_result(o, ptr, sr);
211 if (match_xml_node_attribute(ptr, "lst", "name", "facet_counts"))
212 yaz_solr_decode_facet_counts(o, ptr, sr);
230 static void yaz_solr_encode_facet_field(ODR encode, char **name, char **value, int *i, Z_FacetField *facet_field, int *limit) {
231 Z_AttributeList *attribute_list = facet_field->attributes;
232 struct yaz_facet_attr attr_values;
233 yaz_facet_attr_init(&attr_values);
234 yaz_facet_attr_get_z_attributes(attribute_list, &attr_values);
235 // TODO do we want to support server decided
236 if (!attr_values.errcode && attr_values.useattr) {
237 yaz_add_name_value_str(encode, name, value, i, "facet.field", (char *) attr_values.useattr);
238 // TODO max(attr_values, *limit);
239 if (attr_values.limit > 0 && attr_values.limit > *limit) {
240 *limit = attr_values.limit;
245 static void yaz_solr_encode_facet_list(ODR encode, char **name, char **value, int *i, Z_FacetList *facet_list, int *limit) {
248 for (index = 0; index < facet_list->num; index++) {
249 yaz_solr_encode_facet_field(encode, name, value, i, facet_list->elements[index], limit);
255 int yaz_solr_encode_request(Z_HTTP_Request *hreq, Z_SRW_PDU *srw_pdu,
256 ODR encode, const char *charset)
258 const char *solr_op = 0;
259 //TODO Change. not a nice hard coded, unchecked limit.
260 char *name[SOLR_MAX_PARAMETERS], *value[SOLR_MAX_PARAMETERS];
265 z_HTTP_header_add_basic_auth(encode, &hreq->headers,
266 srw_pdu->username, srw_pdu->password);
268 switch (srw_pdu->which)
270 case Z_SRW_searchRetrieve_request: {
271 Z_SRW_searchRetrieveRequest *request = srw_pdu->u.request;
273 switch(srw_pdu->u.request->query_type)
275 case Z_SRW_query_type_pqf:
276 yaz_add_name_value_str(encode, name, value, &i,
277 "q", request->query.pqf);
279 case Z_SRW_query_type_cql:
280 yaz_add_name_value_str(encode, name, value, &i,
281 "q", request->query.cql);
286 if (srw_pdu->u.request->startRecord)
288 Odr_int start = *request->startRecord - 1;
289 yaz_add_name_value_int(encode, name, value, &i,
292 yaz_add_name_value_int(encode, name, value, &i,
293 "rows", request->maximumRecords);
294 yaz_add_name_value_str(encode, name, value, &i,
295 "fl", request->recordSchema);
297 if (request->facetList) {
298 Z_FacetList *facet_list = request->facetList;
301 yaz_add_name_value_str(encode, name, value, &i, "facet", "true");
302 yaz_solr_encode_facet_list(encode, name, value, &i, facet_list, &limit);
304 yaz_add_name_value_int(encode, name, value, &i, "facet.limit", &olimit);
313 yaz_array_to_uri(&uri_args, encode, name, value);
315 hreq->method = "GET";
318 odr_malloc(encode, strlen(hreq->path) +
319 strlen(uri_args) + strlen(solr_op) + 4);
321 sprintf(path, "%s/%s?%s", hreq->path, solr_op, uri_args);
324 z_HTTP_header_add_content_type(encode, &hreq->headers,
325 "text/xml", charset);
333 * c-file-style: "Stroustrup"
334 * indent-tabs-mode: nil
336 * vim: shiftwidth=4 tabstop=8 expandtab