+// returns a xmalloced SOLR query corresponding to the pquery in client
+// TODO Could prob. be merge with the similar make_cqlquery
+static char *make_solrquery(struct client *cl)
+{
+ solr_transform_t sqlt = solr_transform_create();
+ Z_RPNQuery *zquery;
+ char *r;
+ WRBUF wrb = wrbuf_alloc();
+ int status;
+ ODR odr_out = odr_createmem(ODR_ENCODE);
+
+ zquery = p_query_rpn(odr_out, cl->pquery);
+ if (zquery == 0) {
+ yaz_log(YLOG_WARN, "Failed to generate RPN from PQF: %s", cl->pquery);
+ return 0;
+ }
+ yaz_log(YLOG_LOG, "PQF: %s", cl->pquery);
+ if ((status = solr_transform_rpn2solr_wrbuf(sqlt, wrb, zquery)))
+ {
+ yaz_log(YLOG_WARN, "Failed to generate SOLR query from PQF %s, code=%d", cl->pquery, status);
+ r = 0;
+ }
+ else
+ {
+ r = xstrdup(wrbuf_cstr(wrb));
+ }
+ wrbuf_destroy(wrb);
+ odr_destroy(odr_out);
+ solr_transform_close(sqlt);
+ return r;
+}
+
+const char *client_get_facet_limit_local(struct client *cl,
+ struct session_database *sdb,
+ int *l,
+ NMEM nmem, int *num, char ***values)
+{
+ const char *name = 0;
+ const char *value = 0;
+ for (; (name = facet_limits_get(cl->facet_limits, *l, &value)); (*l)++)
+ {
+ struct setting *s = 0;
+
+ for (s = sdb->settings[PZ_LIMITMAP]; s; s = s->next)
+ {
+ const char *p = strchr(s->name + 3, ':');
+ if (p && !strcmp(p + 1, name) && s->value &&
+ !strncmp(s->value, "local:", 6))
+ {
+ const char *cp = s->value + 6;
+ while (*cp == ' ')
+ cp++;
+
+ nmem_strsplit_escape2(nmem, "|", value, values,
+ num, 1, '\\', 1);
+ (*l)++;
+ return *cp ? cp : name;
+ }
+ }
+ }
+ return 0;
+}
+
+static int apply_limit(struct session_database *sdb,
+ facet_limits_t facet_limits,
+ WRBUF w_pqf, WRBUF w_ccl)
+{
+ int ret = 0;
+ int i = 0;
+ const char *name;
+ const char *value;
+ NMEM nmem_tmp = nmem_create();
+ for (i = 0; (name = facet_limits_get(facet_limits, i, &value)); i++)
+ {
+ struct setting *s = 0;
+
+ for (s = sdb->settings[PZ_LIMITMAP]; s; s = s->next)
+ {
+ const char *p = strchr(s->name + 3, ':');
+ if (p && !strcmp(p + 1, name) && s->value)
+ {
+ char **values = 0;
+ int i, num = 0;
+ nmem_strsplit_escape2(nmem_tmp, "|", value, &values,
+ &num, 1, '\\', 1);
+
+ if (!strncmp(s->value, "rpn:", 4))
+ {
+ const char *pqf = s->value + 4;
+
+ wrbuf_puts(w_pqf, "@and ");
+ wrbuf_puts(w_pqf, pqf);
+ wrbuf_puts(w_pqf, " ");
+ for (i = 0; i < num; i++)
+ {
+ if (i < num - 1)
+ wrbuf_puts(w_pqf, "@or ");
+ yaz_encode_pqf_term(w_pqf, values[i],
+ strlen(values[i]));
+ }
+ }
+ else if (!strncmp(s->value, "ccl:", 4))
+ {
+ const char *ccl = s->value + 4;
+
+ wrbuf_puts(w_ccl, " and (");
+
+ for (i = 0; i < num; i++)
+ {
+ if (i)
+ wrbuf_puts(w_ccl, " or ");
+ wrbuf_puts(w_ccl, ccl);
+ wrbuf_puts(w_ccl, "=\"");
+ wrbuf_puts(w_ccl, values[i]);
+ wrbuf_puts(w_ccl, "\"");
+ }
+ wrbuf_puts(w_ccl, ")");
+
+ }
+ else if (!strncmp(s->value, "local:", 6)) {
+ /* no operation */
+ }
+ else
+ {
+ yaz_log(YLOG_WARN, "Target %s: Bad limitmap '%s'",
+ sdb->database->id, s->value);
+ ret = -1; /* bad limitmap */
+ }
+ break;
+ }
+ }
+ nmem_reset(nmem_tmp);
+ if (!s)
+ {
+ yaz_log(YLOG_WARN, "Target %s: limit %s used, but no limitmap defined",
+ (sdb->database ? sdb->database->id : "<no id>"), name);
+ }
+ }
+ nmem_destroy(nmem_tmp);
+ return ret;
+}
+