1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2009 Index Data
4 Pazpar2 is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27 #include <libxml/parser.h>
28 #include <libxml/tree.h>
29 #include <libxslt/xslt.h>
30 #include <libxslt/transform.h>
31 #include <libxslt/xsltutils.h>
33 #include <yaz/yaz-util.h>
35 #include <yaz/snprintf.h>
36 #include <yaz/tpath.h>
39 #define USE_POSIX_GLOB 1
41 #define USE_POSIX_GLOB 0
48 #include <sys/types.h>
53 #include "pazpar2_config.h"
60 NMEM nmem; /* for conf_config and servers memory */
61 struct conf_server *servers;
66 static char *parse_settings(struct conf_config *config,
67 NMEM nmem, xmlNode *node);
69 static struct conf_targetprofiles *parse_targetprofiles(NMEM nmem,
72 static void conf_metadata_assign(NMEM nmem,
73 struct conf_metadata * metadata,
75 enum conf_metadata_type type,
76 enum conf_metadata_merge merge,
77 enum conf_setting_type setting,
82 enum conf_metadata_mergekey mt)
84 assert(nmem && metadata && name);
86 metadata->name = nmem_strdup(nmem, name);
88 metadata->type = type;
90 // enforcing that type_year is always range_merge
91 if (metadata->type == Metadata_type_year)
92 metadata->merge = Metadata_merge_range;
94 metadata->merge = merge;
96 metadata->setting = setting;
97 metadata->brief = brief;
98 metadata->termlist = termlist;
99 metadata->rank = rank;
100 metadata->sortkey_offset = sortkey_offset;
101 metadata->mergekey = mt;
105 static void conf_sortkey_assign(NMEM nmem,
106 struct conf_sortkey * sortkey,
108 enum conf_sortkey_type type)
110 assert(nmem && sortkey && name);
112 sortkey->name = nmem_strdup(nmem, name);
113 sortkey->type = type;
117 struct conf_service *conf_service_create(struct conf_config *config,
118 int num_metadata, int num_sortkeys,
119 const char *service_id)
121 struct conf_service * service = 0;
122 NMEM nmem = nmem_create();
124 service = nmem_malloc(nmem, sizeof(struct conf_service));
125 service->nmem = nmem;
127 service->settings = 0;
128 service->databases = 0;
129 service->targetprofiles = 0;
130 service->config = config;
132 service->id = service_id ? nmem_strdup(nmem, service_id) : 0;
133 service->num_metadata = num_metadata;
134 service->metadata = 0;
135 if (service->num_metadata)
138 sizeof(struct conf_metadata) * service->num_metadata);
139 service->num_sortkeys = num_sortkeys;
140 service->sortkeys = 0;
141 if (service->num_sortkeys)
144 sizeof(struct conf_sortkey) * service->num_sortkeys);
145 service->dictionary = 0;
149 struct conf_metadata* conf_service_add_metadata(struct conf_service *service,
152 enum conf_metadata_type type,
153 enum conf_metadata_merge merge,
154 enum conf_setting_type setting,
159 enum conf_metadata_mergekey mt)
161 struct conf_metadata * md = 0;
163 if (!service || !service->metadata || !service->num_metadata
164 || field_id < 0 || !(field_id < service->num_metadata))
167 md = service->metadata + field_id;
168 conf_metadata_assign(service->nmem, md, name, type, merge, setting,
169 brief, termlist, rank, sortkey_offset,
175 struct conf_sortkey * conf_service_add_sortkey(struct conf_service *service,
178 enum conf_sortkey_type type)
180 struct conf_sortkey * sk = 0;
182 if (!service || !service->sortkeys || !service->num_sortkeys
183 || field_id < 0 || !(field_id < service->num_sortkeys))
186 //sk = &((service->sortkeys)[field_id]);
187 sk = service->sortkeys + field_id;
188 conf_sortkey_assign(service->nmem, sk, name, type);
194 int conf_service_metadata_field_id(struct conf_service *service,
199 if (!service || !service->metadata || !service->num_metadata)
202 for(i = 0; i < service->num_metadata; i++) {
203 if (!strcmp(name, (service->metadata[i]).name))
211 int conf_service_sortkey_field_id(struct conf_service *service,
216 if (!service || !service->sortkeys || !service->num_sortkeys)
219 for(i = 0; i < service->num_sortkeys; i++) {
220 if (!strcmp(name, (service->sortkeys[i]).name))
227 static void conf_dir_path(struct conf_config *config, WRBUF w, const char *src)
229 if (config->confdir && wrbuf_len(config->confdir) > 0 &&
230 !yaz_is_abspath(src))
232 wrbuf_printf(w, "%s/%s", wrbuf_cstr(config->confdir), src);
238 static void service_destroy(struct conf_service *service)
242 pp2_charset_destroy(service->relevance_pct);
243 pp2_charset_destroy(service->sort_pct);
244 pp2_charset_destroy(service->mergekey_pct);
245 nmem_destroy(service->nmem);
249 static struct conf_service *service_create(struct conf_config *config,
250 xmlNode *node, const char *service_id)
256 struct conf_service *service = 0;
257 int num_metadata = 0;
258 int num_sortkeys = 0;
260 // count num_metadata and num_sortkeys
261 for (n = node->children; n; n = n->next)
262 if (n->type == XML_ELEMENT_NODE && !strcmp((const char *)
263 n->name, "metadata"))
265 xmlChar *sortkey = xmlGetProp(n, (xmlChar *) "sortkey");
267 if (sortkey && strcmp((const char *) sortkey, "no"))
272 service = conf_service_create(config,
273 num_metadata, num_sortkeys, service_id);
275 for (n = node->children; n; n = n->next)
277 if (n->type != XML_ELEMENT_NODE)
279 if (!strcmp((const char *) n->name, "settings"))
281 if (service->settings)
283 yaz_log(YLOG_FATAL, "Can't repeat 'settings'");
286 service->settings = parse_settings(config, service->nmem, n);
287 if (!service->settings)
290 else if (!strcmp((const char *) n->name, (const char *) "targetprofiles"))
292 if (service->targetprofiles)
294 yaz_log(YLOG_FATAL, "Can't repeat targetprofiles");
297 if (!(service->targetprofiles =
298 parse_targetprofiles(service->nmem, n)))
301 else if (!strcmp((const char *) n->name, (const char *) "metadata"))
303 xmlChar *xml_name = xmlGetProp(n, (xmlChar *) "name");
304 xmlChar *xml_brief = xmlGetProp(n, (xmlChar *) "brief");
305 xmlChar *xml_sortkey = xmlGetProp(n, (xmlChar *) "sortkey");
306 xmlChar *xml_merge = xmlGetProp(n, (xmlChar *) "merge");
307 xmlChar *xml_type = xmlGetProp(n, (xmlChar *) "type");
308 xmlChar *xml_termlist = xmlGetProp(n, (xmlChar *) "termlist");
309 xmlChar *xml_rank = xmlGetProp(n, (xmlChar *) "rank");
310 xmlChar *xml_setting = xmlGetProp(n, (xmlChar *) "setting");
311 xmlChar *xml_mergekey = xmlGetProp(n, (xmlChar *) "mergekey");
313 enum conf_metadata_type type = Metadata_type_generic;
314 enum conf_metadata_merge merge = Metadata_merge_no;
315 enum conf_setting_type setting = Metadata_setting_no;
316 enum conf_sortkey_type sk_type = Metadata_sortkey_relevance;
317 enum conf_metadata_mergekey mergekey_type = Metadata_mergekey_no;
321 int sortkey_offset = 0;
323 // now do the parsing logic
326 yaz_log(YLOG_FATAL, "Must specify name in metadata element");
331 if (!strcmp((const char *) xml_brief, "yes"))
333 else if (strcmp((const char *) xml_brief, "no"))
335 yaz_log(YLOG_FATAL, "metadata/brief must be yes or no");
344 if (!strcmp((const char *) xml_termlist, "yes"))
346 else if (strcmp((const char *) xml_termlist, "no"))
348 yaz_log(YLOG_FATAL, "metadata/termlist must be yes or no");
356 rank = atoi((const char *) xml_rank);
362 if (!strcmp((const char *) xml_type, "generic"))
363 type = Metadata_type_generic;
364 else if (!strcmp((const char *) xml_type, "year"))
365 type = Metadata_type_year;
366 else if (!strcmp((const char *) xml_type, "date"))
367 type = Metadata_type_date;
371 "Unknown value for metadata/type: %s", xml_type);
376 type = Metadata_type_generic;
380 if (!strcmp((const char *) xml_merge, "no"))
381 merge = Metadata_merge_no;
382 else if (!strcmp((const char *) xml_merge, "unique"))
383 merge = Metadata_merge_unique;
384 else if (!strcmp((const char *) xml_merge, "longest"))
385 merge = Metadata_merge_longest;
386 else if (!strcmp((const char *) xml_merge, "range"))
387 merge = Metadata_merge_range;
388 else if (!strcmp((const char *) xml_merge, "all"))
389 merge = Metadata_merge_all;
393 "Unknown value for metadata/merge: %s", xml_merge);
398 merge = Metadata_merge_no;
402 if (!strcmp((const char *) xml_setting, "no"))
403 setting = Metadata_setting_no;
404 else if (!strcmp((const char *) xml_setting, "postproc"))
405 setting = Metadata_setting_postproc;
406 else if (!strcmp((const char *) xml_setting, "parameter"))
407 setting = Metadata_setting_parameter;
411 "Unknown value for medadata/setting: %s", xml_setting);
416 // add a sortkey if so specified
417 if (xml_sortkey && strcmp((const char *) xml_sortkey, "no"))
419 if (merge == Metadata_merge_no)
422 "Can't specify sortkey on a non-merged field");
425 if (!strcmp((const char *) xml_sortkey, "numeric"))
426 sk_type = Metadata_sortkey_numeric;
427 else if (!strcmp((const char *) xml_sortkey, "skiparticle"))
428 sk_type = Metadata_sortkey_skiparticle;
432 "Unknown sortkey in metadata element: %s",
436 sortkey_offset = sk_node;
438 conf_service_add_sortkey(service, sk_node,
439 (const char *) xml_name, sk_type);
446 if (xml_mergekey && strcmp((const char *) xml_mergekey, "no"))
448 mergekey_type = Metadata_mergekey_yes;
452 // metadata known, assign values
453 conf_service_add_metadata(service, md_node,
454 (const char *) xml_name,
455 type, merge, setting,
456 brief, termlist, rank, sortkey_offset,
461 xmlFree(xml_sortkey);
464 xmlFree(xml_termlist);
466 xmlFree(xml_setting);
471 yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
478 static char *parse_settings(struct conf_config *config,
479 NMEM nmem, xmlNode *node)
481 xmlChar *src = xmlGetProp(node, (xmlChar *) "src");
486 WRBUF w = wrbuf_alloc();
487 conf_dir_path(config, w, (const char *) src);
488 r = nmem_strdup(nmem, wrbuf_cstr(w));
493 yaz_log(YLOG_FATAL, "Must specify src in targetprofile");
500 static struct conf_server *parse_server(struct conf_config *config,
501 NMEM nmem, xmlNode *node)
504 struct conf_server *server = nmem_malloc(nmem, sizeof(struct conf_server));
508 server->proxy_host = 0;
509 server->proxy_port = 0;
511 server->proxy_addr = 0;
514 server->server_settings = 0;
515 server->relevance_pct = 0;
516 server->sort_pct = 0;
517 server->mergekey_pct = 0;
519 for (n = node->children; n; n = n->next)
521 if (n->type != XML_ELEMENT_NODE)
523 if (!strcmp((const char *) n->name, "listen"))
525 xmlChar *port = xmlGetProp(n, (xmlChar *) "port");
526 xmlChar *host = xmlGetProp(n, (xmlChar *) "host");
528 server->port = atoi((const char *) port);
530 server->host = nmem_strdup(nmem, (const char *) host);
534 else if (!strcmp((const char *) n->name, "proxy"))
536 xmlChar *port = xmlGetProp(n, (xmlChar *) "port");
537 xmlChar *host = xmlGetProp(n, (xmlChar *) "host");
538 xmlChar *myurl = xmlGetProp(n, (xmlChar *) "myurl");
540 server->proxy_port = atoi((const char *) port);
542 server->proxy_host = nmem_strdup(nmem, (const char *) host);
544 server->myurl = nmem_strdup(nmem, (const char *) myurl);
549 else if (!strcmp((const char *) n->name, "settings"))
551 if (server->server_settings)
553 yaz_log(YLOG_FATAL, "Can't repeat 'settings'");
556 if (!(server->server_settings = parse_settings(config, nmem, n)))
559 else if (!strcmp((const char *) n->name, "relevance"))
561 server->relevance_pct = pp2_charset_create_xml(n);
562 if (!server->relevance_pct)
565 else if (!strcmp((const char *) n->name, "sort"))
567 server->sort_pct = pp2_charset_create_xml(n);
568 if (!server->sort_pct)
571 else if (!strcmp((const char *) n->name, "mergekey"))
573 server->mergekey_pct = pp2_charset_create_xml(n);
574 if (!server->mergekey_pct)
577 else if (!strcmp((const char *) n->name, "service"))
579 char *service_id = (char *)
580 xmlGetProp(n, (xmlChar *) "id");
582 struct conf_service **sp = &server->service;
583 for (; *sp; sp = &(*sp)->next)
584 if ((*sp)->id && service_id &&
585 0 == strcmp((*sp)->id, service_id))
587 yaz_log(YLOG_FATAL, "Duplicate service: %s", service_id);
590 else if (!(*sp)->id && !service_id)
592 yaz_log(YLOG_FATAL, "Duplicate unnamed service");
596 if (*sp) /* service already exist */
600 struct conf_service *s = service_create(config, n, service_id);
603 if (server->relevance_pct)
605 s->relevance_pct = server->relevance_pct;
606 pp2_charset_incref(s->relevance_pct);
609 s->relevance_pct = pp2_charset_create(0);
611 if (server->sort_pct)
614 s->sort_pct = server->sort_pct;
615 pp2_charset_incref(s->sort_pct);
618 s->sort_pct = pp2_charset_create(0);
620 if (server->mergekey_pct)
622 s->mergekey_pct = server->mergekey_pct;
623 pp2_charset_incref(s->mergekey_pct);
626 s->mergekey_pct = pp2_charset_create(0);
634 yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
641 xsltStylesheet *conf_load_stylesheet(struct conf_config *config,
644 WRBUF w = wrbuf_alloc();
647 conf_dir_path(config, w, fname);
648 s = xsltParseStylesheetFile((xmlChar *) wrbuf_cstr(w));
653 static struct conf_targetprofiles *parse_targetprofiles(NMEM nmem,
656 struct conf_targetprofiles *r = nmem_malloc(nmem, sizeof(*r));
657 xmlChar *type = xmlGetProp(node, (xmlChar *) "type");
658 xmlChar *src = xmlGetProp(node, (xmlChar *) "src");
660 memset(r, 0, sizeof(*r));
664 if (!strcmp((const char *) type, "local"))
665 r->type = Targetprofiles_local;
668 yaz_log(YLOG_FATAL, "Unknown targetprofile type");
674 yaz_log(YLOG_FATAL, "Must specify type for targetprofile");
679 r->src = nmem_strdup(nmem, (const char *) src);
682 yaz_log(YLOG_FATAL, "Must specify src in targetprofile");
690 struct conf_service *locate_service(struct conf_server *server,
691 const char *service_id)
693 struct conf_service *s = server->service;
694 for (; s; s = s->next)
695 if (s->id && service_id && 0 == strcmp(s->id, service_id))
697 else if (!s->id && !service_id)
703 static int parse_config(struct conf_config *config, xmlNode *root)
707 for (n = root->children; n; n = n->next)
709 if (n->type != XML_ELEMENT_NODE)
711 if (!strcmp((const char *) n->name, "server"))
713 struct conf_server *tmp = parse_server(config, config->nmem, n);
716 tmp->next = config->servers;
717 config->servers = tmp;
719 else if (!strcmp((const char *) n->name, "targetprofiles"))
721 yaz_log(YLOG_FATAL, "targetprofiles unsupported here. Must be part of service");
727 yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
734 static int process_config_includes(struct conf_config *config, xmlNode *n);
736 static int config_include_one(struct conf_config *config, xmlNode **sib,
740 if (stat(path, &st) < 0)
742 yaz_log(YLOG_FATAL|YLOG_ERRNO, "stat %s", path);
747 if ((st.st_mode & S_IFMT) == S_IFREG)
749 xmlDoc *doc = xmlParseFile(path);
750 yaz_log(YLOG_LOG, "processing include path=%s", path);
753 xmlNodePtr t = xmlDocGetRootElement(doc);
754 int ret = process_config_includes(config, t);
755 *sib = xmlAddNextSibling(*sib, xmlCopyNode(t, 1));
762 yaz_log(YLOG_FATAL, "Could not parse %s", path);
770 static int config_include_src(struct conf_config *config, xmlNode **np,
773 int ret = 0; /* return code. OK so far */
774 WRBUF w = wrbuf_alloc();
775 xmlNodePtr sib; /* our sibling that we append */
776 xmlNodePtr c; /* tmp node */
778 wrbuf_printf(w, " begin include src=\"%s\" ", src);
780 /* replace include element with a 'begin' comment */
781 sib = xmlNewComment((const xmlChar *) wrbuf_cstr(w));
782 xmlReplaceNode(*np, sib);
787 conf_dir_path(config, w, src);
792 glob(wrbuf_cstr(w), 0 /* flags */, 0 /* errfunc */, &glob_res);
794 for (i = 0; ret == 0 && i < glob_res.gl_pathc; i++)
796 const char *path = glob_res.gl_pathv[i];
797 ret = config_include_one(config, &sib, path);
802 ret = config_include_one(config, &sib, wrbuf_cstr(w));
805 wrbuf_printf(w, " end include src=\"%s\" ", src);
806 c = xmlNewComment((const xmlChar *) wrbuf_cstr(w));
807 sib = xmlAddNextSibling(sib, c);
814 static int process_config_includes(struct conf_config *config, xmlNode *n)
816 for (; n; n = n->next)
818 if (n->type == XML_ELEMENT_NODE)
820 if (!strcmp((const char *) n->name, "include"))
822 xmlChar *src = xmlGetProp(n, (xmlChar *) "src");
825 int ret = config_include_src(config, &n,
835 if (process_config_includes(config, n->children))
843 struct conf_config *config_create(const char *fname, int verbose)
845 xmlDoc *doc = xmlParseFile(fname);
849 NMEM nmem = nmem_create();
850 struct conf_config *config = nmem_malloc(nmem, sizeof(struct conf_config));
852 xmlSubstituteEntitiesDefault(1);
853 xmlLoadExtDtdDefaultValue = 1;
856 yaz_log(YLOG_FATAL, "Failed to read %s", fname);
863 config->confdir = wrbuf_alloc();
864 if ((p = strrchr(fname,
873 wrbuf_write(config->confdir, fname, len);
875 wrbuf_puts(config->confdir, "");
877 n = xmlDocGetRootElement(doc);
878 r = process_config_includes(config, n);
883 yaz_log(YLOG_LOG, "Configuration %s after include processing",
885 xmlDocFormatDump(yaz_log_file(), doc, 0);
887 r = parse_config(config, n);
893 config_destroy(config);
899 void server_destroy(struct conf_server *server)
901 struct conf_service *s = server->service;
904 struct conf_service *s_next = s->next;
908 pp2_charset_destroy(server->relevance_pct);
909 pp2_charset_destroy(server->sort_pct);
910 pp2_charset_destroy(server->mergekey_pct);
913 void config_destroy(struct conf_config *config)
917 struct conf_server *server = config->servers;
920 struct conf_server *s_next = server->next;
921 server_destroy(server);
924 wrbuf_destroy(config->confdir);
925 nmem_destroy(config->nmem);
929 void config_read_settings(struct conf_config *config)
931 struct conf_service *s = config->servers->service;
932 for (;s ; s = s->next)
936 settings_read(s, s->settings);
937 else if (config->servers->server_settings)
938 settings_read(s, config->servers->server_settings);
940 yaz_log(YLOG_WARN, "No settings for service");
944 void config_stop_listeners(struct conf_config *conf)
946 struct conf_server *ser;
947 for (ser = conf->servers; ser; ser = ser->next)
948 http_close_server(ser);
951 int config_start_listeners(struct conf_config *conf,
952 const char *listener_override)
954 struct conf_server *ser;
955 for (ser = conf->servers; ser; ser = ser->next)
957 WRBUF w = wrbuf_alloc();
959 if (listener_override)
961 wrbuf_puts(w, listener_override);
962 listener_override = 0; /* only first server is overriden */
967 wrbuf_puts(w, ser->host);
972 wrbuf_printf(w, "%d", ser->port);
975 r = http_init(wrbuf_cstr(w), ser);
981 if (ser->proxy_host || ser->proxy_port)
984 wrbuf_puts(w, ser->proxy_host);
989 wrbuf_printf(w, "%d", ser->proxy_port);
993 http_set_proxyaddr(wrbuf_cstr(w), ser);
1002 * c-file-style: "Stroustrup"
1003 * indent-tabs-mode: nil
1005 * vim: shiftwidth=4 tabstop=8 expandtab