+void yf::Zoom::Frontend::prepare_elements(BackendPtr b,
+ Odr_oid *preferredRecordSyntax,
+ const char *element_set_name,
+ bool &enable_pz2_retrieval,
+ bool &enable_pz2_transform,
+ bool &assume_marc8_charset)
+
+{
+ char oid_name_str[OID_STR_MAX];
+ const char *syntax_name = 0;
+
+ if (preferredRecordSyntax &&
+ !oid_oidcmp(preferredRecordSyntax, yaz_oid_recsyn_xml)
+ && element_set_name)
+ {
+ if (!strcmp(element_set_name, m_p->element_transform.c_str()))
+ {
+ enable_pz2_retrieval = true;
+ enable_pz2_transform = true;
+ }
+ else if (!strcmp(element_set_name, m_p->element_raw.c_str()))
+ {
+ enable_pz2_retrieval = true;
+ }
+ }
+
+ if (enable_pz2_retrieval)
+ {
+ std::string configured_request_syntax = b->sptr->request_syntax;
+ if (configured_request_syntax.length())
+ {
+ syntax_name = configured_request_syntax.c_str();
+ const Odr_oid *syntax_oid =
+ yaz_string_to_oid(yaz_oid_std(), CLASS_RECSYN, syntax_name);
+ if (!oid_oidcmp(syntax_oid, yaz_oid_recsyn_usmarc)
+ || !oid_oidcmp(syntax_oid, yaz_oid_recsyn_opac))
+ assume_marc8_charset = true;
+ }
+ }
+ else if (preferredRecordSyntax)
+ syntax_name =
+ yaz_oid_to_string_buf(preferredRecordSyntax, 0, oid_name_str);
+
+ if (b->sptr->sru.length())
+ syntax_name = "XML";
+
+ b->set_option("preferredRecordSyntax", syntax_name);
+
+ if (enable_pz2_retrieval)
+ {
+ element_set_name = 0;
+ if (b->sptr->element_set.length())
+ element_set_name = b->sptr->element_set.c_str();
+ }
+
+ b->set_option("elementSetName", element_set_name);
+
+}
+
+Z_Records *yf::Zoom::Frontend::get_records(Odr_int start,
+ Odr_int number_to_present,
+ int *error,
+ char **addinfo,
+ Odr_int *number_of_records_returned,
+ ODR odr,
+ BackendPtr b,
+ Odr_oid *preferredRecordSyntax,
+ const char *element_set_name)
+{
+ *number_of_records_returned = 0;
+ Z_Records *records = 0;
+ bool enable_pz2_retrieval = false; // whether target profile is used
+ bool enable_pz2_transform = false; // whether XSLT is used as well
+ bool assume_marc8_charset = false;
+
+ prepare_elements(b, preferredRecordSyntax,
+ element_set_name,
+ enable_pz2_retrieval,
+ enable_pz2_transform,
+ assume_marc8_charset);
+
+ if (start < 0 || number_to_present <=0)
+ return records;
+
+ if (number_to_present > 10000)
+ number_to_present = 10000;
+
+ ZOOM_record *recs = (ZOOM_record *)
+ odr_malloc(odr, (size_t) number_to_present * sizeof(*recs));
+
+ b->present(start, number_to_present, recs, error, addinfo, odr);
+
+ int i = 0;
+ if (!*error)
+ {
+ for (i = 0; i < number_to_present; i++)
+ if (!recs[i])
+ break;
+ }
+ if (i > 0)
+ { // only return records if no error and at least one record
+ char *odr_database = odr_strdup(odr,
+ b->m_frontend_database.c_str());
+ Z_NamePlusRecordList *npl = (Z_NamePlusRecordList *)
+ odr_malloc(odr, sizeof(*npl));
+ *number_of_records_returned = i;
+ npl->num_records = i;
+ npl->records = (Z_NamePlusRecord **)
+ odr_malloc(odr, i * sizeof(*npl->records));
+ for (i = 0; i < number_to_present; i++)
+ {
+ Z_NamePlusRecord *npr = 0;
+ const char *addinfo;
+ int sur_error = ZOOM_record_error(recs[i], 0 /* msg */,
+ &addinfo, 0 /* diagset */);
+
+ if (sur_error)
+ {
+ npr = zget_surrogateDiagRec(odr, odr_database, sur_error,
+ addinfo);
+ }
+ else if (enable_pz2_retrieval)
+ {
+ char rec_type_str[100];
+ const char *record_encoding = 0;
+
+ if (b->sptr->record_encoding.length())
+ record_encoding = b->sptr->record_encoding.c_str();
+ else if (assume_marc8_charset)
+ record_encoding = "marc8";
+
+ strcpy(rec_type_str, b->sptr->use_turbomarc ? "txml" : "xml");
+ if (record_encoding)
+ {
+ strcat(rec_type_str, "; charset=");
+ strcat(rec_type_str, record_encoding);
+ }
+
+ int rec_len;
+ xmlChar *xmlrec_buf = 0;
+ const char *rec_buf = ZOOM_record_get(recs[i], rec_type_str,
+ &rec_len);
+ if (!rec_buf && !npr)
+ {
+ std::string addinfo("ZOOM_record_get failed for type ");
+
+ addinfo += rec_type_str;
+ npr = zget_surrogateDiagRec(
+ odr, odr_database,
+ YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS,
+ addinfo.c_str());
+ }
+
+ if (rec_buf && b->xsp && enable_pz2_transform)
+ {
+ xmlDoc *rec_doc = xmlParseMemory(rec_buf, rec_len);
+ if (!rec_doc)
+ {
+ npr = zget_surrogateDiagRec(
+ odr, odr_database,
+ YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS,
+ "xml parse failed for record");
+ }
+ else
+ {
+ xmlDoc *rec_res =
+ xsltApplyStylesheet(b->xsp, rec_doc, 0);
+
+ if (rec_res)
+ {
+ xsltSaveResultToString(&xmlrec_buf, &rec_len,
+ rec_res, b->xsp);
+ rec_buf = (const char *) xmlrec_buf;
+
+ xmlFreeDoc(rec_res);
+ }
+ if (!rec_buf)
+ {
+ std::string addinfo;
+
+ addinfo = "xslt apply failed for "
+ + b->sptr->transform_xsl_fname;
+ npr = zget_surrogateDiagRec(
+ odr, odr_database,
+ YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS,
+ addinfo.c_str());
+ }
+ xmlFreeDoc(rec_doc);
+ }
+ }
+
+ if (rec_buf)
+ {
+ xmlDoc *doc = xmlParseMemory(rec_buf, rec_len);
+ std::string res =
+ mp::xml::url_recipe_handle(doc, b->sptr->urlRecipe);
+ if (res.length() && b->content_session_id.length())
+ {
+ size_t off = res.find_first_of("://");
+ if (off != std::string::npos)
+ {
+ char tmp[1024];
+ sprintf(tmp, "%s.%s/",
+ b->content_session_id.c_str(),
+ m_p->content_proxy_server.c_str());
+ res.insert(off + 3, tmp);
+ }
+ }
+ if (res.length())
+ {
+ xmlNode *ptr = xmlDocGetRootElement(doc);
+ while (ptr && ptr->type != XML_ELEMENT_NODE)
+ ptr = ptr->next;
+ xmlNode *c =
+ xmlNewChild(ptr, 0, BAD_CAST "generated-url", 0);
+ xmlNode * t = xmlNewText(BAD_CAST res.c_str());
+ xmlAddChild(c, t);
+
+ if (xmlrec_buf)
+ xmlFree(xmlrec_buf);
+
+ xmlDocDumpMemory(doc, &xmlrec_buf, &rec_len);
+ rec_buf = (const char *) xmlrec_buf;
+ }
+ xmlFreeDoc(doc);
+ }
+ if (!npr)
+ {
+ if (!rec_buf)
+ npr = zget_surrogateDiagRec(
+ odr, odr_database,
+ YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS,
+ rec_type_str);
+ else
+ {
+ npr = (Z_NamePlusRecord *)
+ odr_malloc(odr, sizeof(*npr));
+ npr->databaseName = odr_database;
+ npr->which = Z_NamePlusRecord_databaseRecord;
+ npr->u.databaseRecord =
+ z_ext_record_xml(odr, rec_buf, rec_len);
+ }
+ }
+ if (xmlrec_buf)
+ xmlFree(xmlrec_buf);
+ }
+ else
+ {
+ Z_External *ext =
+ (Z_External *) ZOOM_record_get(recs[i], "ext", 0);
+ if (ext)
+ {
+ npr = (Z_NamePlusRecord *) odr_malloc(odr, sizeof(*npr));
+ npr->databaseName = odr_database;
+ npr->which = Z_NamePlusRecord_databaseRecord;
+ npr->u.databaseRecord = ext;
+ }
+ else
+ {
+ npr = zget_surrogateDiagRec(
+ odr, odr_database,
+ YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS,
+ "ZOOM_record, type ext");
+ }
+ }
+ npl->records[i] = npr;
+ }
+ records = (Z_Records*) odr_malloc(odr, sizeof(*records));
+ records->which = Z_Records_DBOSD;
+ records->u.databaseOrSurDiagnostics = npl;
+ }
+ return records;
+}
+
+struct cql_node *yf::Zoom::Impl::convert_cql_fields(struct cql_node *cn,
+ ODR odr)
+{
+ struct cql_node *r = 0;
+ if (!cn)
+ return 0;
+ switch (cn->which)
+ {
+ case CQL_NODE_ST:
+ if (cn->u.st.index)
+ {
+ std::map<std::string,std::string>::const_iterator it;
+ it = fieldmap.find(cn->u.st.index);
+ if (it == fieldmap.end())
+ return cn;
+ if (it->second.length())
+ cn->u.st.index = odr_strdup(odr, it->second.c_str());
+ else
+ cn->u.st.index = 0;
+ }
+ break;
+ case CQL_NODE_BOOL:
+ r = convert_cql_fields(cn->u.boolean.left, odr);
+ if (!r)
+ r = convert_cql_fields(cn->u.boolean.right, odr);
+ break;
+ case CQL_NODE_SORT:
+ r = convert_cql_fields(cn->u.sort.search, odr);
+ break;
+ }
+ return r;
+}
+
+static void sort_pqf_type_7(WRBUF pqf_wrbuf, const char *sru_sortkeys)
+{
+ /* sortkey layour: path,schema,ascending,caseSensitive,missingValue */
+ /* see cql_sortby_to_sortkeys of YAZ. */
+ char **sortspec;
+ int num_sortspec = 0;
+ int i;
+ NMEM nmem = nmem_create();
+
+ if (sru_sortkeys)
+ nmem_strsplit_blank(nmem, sru_sortkeys, &sortspec, &num_sortspec);
+ if (num_sortspec > 0)
+ {
+ WRBUF w = wrbuf_alloc();
+ for (i = 0; i < num_sortspec; i++)
+ {
+ char **arg;
+ int num_arg;
+ int ascending = 1;
+ nmem_strsplitx(nmem, ",", sortspec[i], &arg, &num_arg, 0);
+
+ if (num_arg > 2 && arg[2][0])
+ ascending = atoi(arg[2]);
+
+ wrbuf_puts(w, "@or @attr 1=");
+ yaz_encode_pqf_term(w, arg[0], strlen(arg[0]));
+ wrbuf_printf(w, "@attr 7=%d %d ", ascending ? 1 : 2, i);
+ }
+ if (wrbuf_len(w))
+ {
+ wrbuf_puts(w, wrbuf_cstr(pqf_wrbuf));
+ wrbuf_rewind(pqf_wrbuf);
+ wrbuf_puts(pqf_wrbuf, wrbuf_cstr(w));
+ }
+ wrbuf_destroy(w);
+ }
+ nmem_destroy(nmem);
+}
+
+static void sort_via_cql(WRBUF cql_sortby, const char *sru_sortkeys)
+{
+ /* sortkey layour: path,schema,ascending,caseSensitive,missingValue */
+ /* see cql_sortby_to_sortkeys of YAZ. */
+ char **sortspec;
+ int num_sortspec = 0;
+ int i;
+ NMEM nmem = nmem_create();
+
+ if (sru_sortkeys)
+ nmem_strsplit_blank(nmem, sru_sortkeys, &sortspec, &num_sortspec);
+ if (num_sortspec > 0)
+ {
+ WRBUF w = wrbuf_alloc();
+ for (i = 0; i < num_sortspec; i++)
+ {
+ char **arg;
+ int num_arg;
+ int ascending = 1;
+ int case_sensitive = 0;
+ const char *missing = 0;
+ nmem_strsplitx(nmem, ",", sortspec[i], &arg, &num_arg, 0);
+
+ if (num_arg > 2 && arg[2][0])
+ ascending = atoi(arg[2]);
+ if (num_arg > 3 && arg[3][0])
+ case_sensitive = atoi(arg[3]);
+ if (num_arg > 4 && arg[4][0])
+ missing = arg[4];
+ if (i > 0)
+ wrbuf_puts(w, " ");
+ else
+ wrbuf_puts(w, " sortby ");
+ wrbuf_puts(w, arg[0]); /* field */
+ wrbuf_puts(w, "/");
+ wrbuf_puts(w, ascending ? "ascending" : "descending");
+ if (case_sensitive)
+ wrbuf_puts(w, "/respectCase");
+ if (missing)
+ {
+ if (!strcmp(missing, "omit"))
+ wrbuf_puts(w, "/missingOmit");
+ else if (!strcmp(missing, "abort"))
+ wrbuf_puts(w, "/missingFail");
+ else if (!strcmp(missing, "lowValue"))
+ wrbuf_puts(w, "/missingLow");
+ else if (!strcmp(missing, "highValue"))
+ wrbuf_puts(w, "/missingHigh");
+ }
+ }
+ if (wrbuf_len(w))
+ wrbuf_puts(cql_sortby, wrbuf_cstr(w));
+ wrbuf_destroy(w);
+ }
+ nmem_destroy(nmem);
+}
+
+#if YAZ_VERSIONL < 0x40206
+static void wrbuf_vp_puts(const char *buf, void *client_data)
+{
+ WRBUF b = (WRBUF) client_data;
+ wrbuf_puts(b, buf);
+}
+#endif
+