enforcing that years type metadata is always range merged
[pazpar2-moved-to-github.git] / src / config.c
1 /* $Id: config.c,v 1.32 2007-04-27 09:38:13 marc Exp $
2    Copyright (c) 2006-2007, Index Data.
3
4 This file is part of Pazpar2.
5
6 Pazpar2 is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
9 version.
10
11 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Pazpar2; see the file LICENSE.  If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA.
20  */
21
22 /* $Id: config.c,v 1.32 2007-04-27 09:38:13 marc Exp $ */
23
24 #include <string.h>
25
26 #include <libxml/parser.h>
27 #include <libxml/tree.h>
28 #include <libxslt/xslt.h>
29 #include <libxslt/transform.h>
30 #include <libxslt/xsltutils.h>
31
32 #if HAVE_CONFIG_H
33 #include <cconfig.h>
34 #endif
35
36 #include <yaz/yaz-util.h>
37 #include <yaz/nmem.h>
38
39 #define CONFIG_NOEXTERNS
40 #include "config.h"
41
42 static NMEM nmem = 0;
43 static char confdir[256] = ".";
44
45 struct conf_config *config = 0;
46
47
48 struct conf_metadata * conf_metadata_assign(NMEM nmem, 
49                                             struct conf_metadata * metadata,
50                                             const char *name,
51                                             enum conf_metadata_type type,
52                                             enum conf_metadata_merge merge,
53                                             int brief,
54                                             int termlist,
55                                             int rank,
56                                             int sortkey_offset)
57 {
58     if (!nmem || !metadata || !name)
59         return 0;
60     
61     metadata->name = nmem_strdup(nmem, name);
62     metadata->type = type;
63
64     // enforcing that years are always range merged
65     if (metadata->type == Metadata_type_year)
66         metadata->merge = Metadata_merge_range;
67     else
68         metadata->merge = merge;
69
70     metadata->brief = brief;   
71     metadata->termlist = termlist;
72     metadata->rank = rank;    
73     metadata->sortkey_offset = sortkey_offset;
74     return metadata;
75 }
76
77
78 struct conf_sortkey * conf_sortkey_assign(NMEM nmem, 
79                                           struct conf_sortkey * sortkey,
80                                           const char *name,
81                                           enum conf_sortkey_type type)
82 {
83     if (!nmem || !sortkey || !name)
84         return 0;
85     
86     sortkey->name = nmem_strdup(nmem, name);
87     sortkey->type = type;
88
89     return sortkey;
90 }
91
92
93 struct conf_service * conf_service_create(NMEM nmem,
94                                           int num_metadata, int num_sortkeys)
95 {
96     struct conf_service * service = 0;
97
98     //assert(nmem);
99     
100     service = nmem_malloc(nmem, sizeof(struct conf_service));
101
102     service->num_metadata = num_metadata;
103     service->metadata = 0;
104     if (service->num_metadata)
105       service->metadata 
106           = nmem_malloc(nmem, 
107                         sizeof(struct conf_metadata) * service->num_metadata);
108     service->num_sortkeys = num_sortkeys;
109     service->sortkeys = 0;
110     if (service->num_sortkeys)
111         service->sortkeys 
112             = nmem_malloc(nmem, 
113                           sizeof(struct conf_sortkey) * service->num_sortkeys);
114     return service; 
115 }
116
117 struct conf_metadata* conf_service_add_metadata(NMEM nmem, 
118                                                 struct conf_service *service,
119                                                 int field_id,
120                                                 const char *name,
121                                                 enum conf_metadata_type type,
122                                                 enum conf_metadata_merge merge,
123                                                 int brief,
124                                                 int termlist,
125                                                 int rank,
126                                                 int sortkey_offset)
127 {
128     struct conf_metadata * md = 0;
129
130     if (!service || !service->metadata || !service->num_metadata
131         || field_id < 0  || !(field_id < service->num_metadata))
132         return 0;
133
134     //md = &((service->metadata)[field_id]);
135     md = service->metadata + field_id;
136     md = conf_metadata_assign(nmem, md, name, type, merge, 
137                              brief, termlist, rank, sortkey_offset);
138     return md;
139 }
140
141
142 struct conf_sortkey * conf_service_add_sortkey(NMEM nmem,
143                                                struct conf_service *service,
144                                                int field_id,
145                                                const char *name,
146                                                enum conf_sortkey_type type)
147 {
148     struct conf_sortkey * sk = 0;
149
150     if (!service || !service->sortkeys || !service->num_sortkeys
151         || field_id < 0  || !(field_id < service->num_sortkeys))
152         return 0;
153
154     //sk = &((service->sortkeys)[field_id]);
155     sk = service->sortkeys + field_id;
156     sk = conf_sortkey_assign(nmem, sk, name, type);
157
158     return sk;
159 }
160
161
162 int conf_service_metadata_field_id(struct conf_service *service,
163                                    const char * name)
164 {
165     int i = 0;
166
167     if (!service || !service->metadata || !service->num_metadata)
168         return -1;
169
170     for(i = 0; i < service->num_metadata; i++) {
171         if (!strcmp(name, (service->metadata[i]).name))
172             return i;
173     }
174    
175     return -1;
176 };
177
178
179 int conf_service_sortkey_field_id(struct conf_service *service,
180                                   const char * name)
181 {
182     int i = 0;
183
184     if (!service || !service->sortkeys || !service->num_sortkeys)
185         return -1;
186
187     for(i = 0; i < service->num_sortkeys; i++) {
188         if (!strcmp(name, (service->sortkeys[i]).name))
189             return i;
190     }
191    
192     return -1;
193 };
194
195
196
197 /* Code to parse configuration file */
198 /* ==================================================== */
199
200 static struct conf_service *parse_service(xmlNode *node)
201 {
202     xmlNode *n;
203     int md_node = 0;
204     int sk_node = 0;
205
206     struct conf_service *service = 0;
207     int num_metadata = 0;
208     int num_sortkeys = 0;
209     
210     // count num_metadata and num_sortkeys
211     for (n = node->children; n; n = n->next)
212         if (n->type == XML_ELEMENT_NODE && !strcmp((const char *)
213                                                    n->name, "metadata"))
214         {
215             xmlChar *sortkey = xmlGetProp(n, (xmlChar *) "sortkey");
216             num_metadata++;
217             if (sortkey && strcmp((const char *) sortkey, "no"))
218                 num_sortkeys++;
219             xmlFree(sortkey);
220         }
221
222     service = conf_service_create(nmem, num_metadata, num_sortkeys);    
223
224     for (n = node->children; n; n = n->next)
225     {
226         if (n->type != XML_ELEMENT_NODE)
227             continue;
228         if (!strcmp((const char *) n->name, (const char *) "metadata"))
229         {
230             xmlChar *xml_name = xmlGetProp(n, (xmlChar *) "name");
231             xmlChar *xml_brief = xmlGetProp(n, (xmlChar *) "brief");
232             xmlChar *xml_sortkey = xmlGetProp(n, (xmlChar *) "sortkey");
233             xmlChar *xml_merge = xmlGetProp(n, (xmlChar *) "merge");
234             xmlChar *xml_type = xmlGetProp(n, (xmlChar *) "type");
235             xmlChar *xml_termlist = xmlGetProp(n, (xmlChar *) "termlist");
236             xmlChar *xml_rank = xmlGetProp(n, (xmlChar *) "rank");
237
238             enum conf_metadata_type type = Metadata_type_generic;
239             enum conf_metadata_merge merge = Metadata_merge_no;
240             int brief = 0;
241             int termlist = 0;
242             int rank = 0;
243             int sortkey_offset = 0;
244             enum conf_sortkey_type sk_type = Metadata_sortkey_relevance;
245             
246             // now do the parsing logic
247             if (!xml_name)
248             {
249                 yaz_log(YLOG_FATAL, "Must specify name in metadata element");
250                 return 0;
251             }
252             if (xml_brief)
253             {
254                 if (!strcmp((const char *) xml_brief, "yes"))
255                     brief = 1;
256                  else if (strcmp((const char *) xml_brief, "no"))
257                 {
258                     yaz_log(YLOG_FATAL, "metadata/brief must be yes or no");
259                     return 0;
260                 }
261             }
262             else
263                 brief = 0;
264
265             if (xml_termlist)
266             {
267                 if (!strcmp((const char *) xml_termlist, "yes"))
268                     termlist = 1;
269                 else if (strcmp((const char *) xml_termlist, "no"))
270                 {
271                     yaz_log(YLOG_FATAL, "metadata/termlist must be yes or no");
272                     return 0;
273                 }
274             }
275             else
276                 termlist = 0;
277
278             if (xml_rank)
279                 rank = atoi((const char *) xml_rank);
280             else
281                 rank = 0;
282
283             if (xml_type)
284             {
285                 if (!strcmp((const char *) xml_type, "generic"))
286                     type = Metadata_type_generic;
287                 else if (!strcmp((const char *) xml_type, "year"))
288                     type = Metadata_type_year;
289                 else
290                 {
291                     yaz_log(YLOG_FATAL, "Unknown value for metadata/type: %s", xml_type);
292                     return 0;
293                 }
294             }
295             else
296                 type = Metadata_type_generic;
297
298             if (xml_merge)
299             {
300                 if (!strcmp((const char *) xml_merge, "no"))
301                     merge = Metadata_merge_no;
302                 else if (!strcmp((const char *) xml_merge, "unique"))
303                     merge = Metadata_merge_unique;
304                 else if (!strcmp((const char *) xml_merge, "longest"))
305                     merge = Metadata_merge_longest;
306                 else if (!strcmp((const char *) xml_merge, "range"))
307                     merge = Metadata_merge_range;
308                 else if (!strcmp((const char *) xml_merge, "all"))
309                     merge = Metadata_merge_all;
310                 else
311                 {
312                     yaz_log(YLOG_FATAL, 
313                             "Unknown value for metadata/merge: %s", xml_merge);
314                     return 0;
315                 }
316             }
317             else
318                 merge = Metadata_merge_no;
319
320             // add a sortkey if so specified
321             if (xml_sortkey && strcmp((const char *) xml_sortkey, "no"))
322             {
323                 if (merge == Metadata_merge_no)
324                 {
325                     yaz_log(YLOG_FATAL, 
326                             "Can't specify sortkey on a non-merged field");
327                     return 0;
328                 }
329                 if (!strcmp((const char *) xml_sortkey, "numeric"))
330                     sk_type = Metadata_sortkey_numeric;
331                 else if (!strcmp((const char *) xml_sortkey, "skiparticle"))
332                     sk_type = Metadata_sortkey_skiparticle;
333                 else
334                 {
335                     yaz_log(YLOG_FATAL,
336                             "Unknown sortkey in metadata element: %s", 
337                             xml_sortkey);
338                     return 0;
339                 }
340                 sortkey_offset = sk_node;
341
342                 conf_service_add_sortkey(nmem, service, sk_node,
343                                          (const char *) xml_name, sk_type);
344                 
345                 sk_node++;
346             }
347             else
348                 sortkey_offset = -1;
349
350             // metadata known, assign values
351             conf_service_add_metadata(nmem, service, md_node,
352                                       (const char *) xml_name,
353                                       type, merge,
354                                       brief, termlist, rank, sortkey_offset);
355
356             xmlFree(xml_name);
357             xmlFree(xml_brief);
358             xmlFree(xml_sortkey);
359             xmlFree(xml_merge);
360             xmlFree(xml_type);
361             xmlFree(xml_termlist);
362             xmlFree(xml_rank);
363             md_node++;
364         }
365         else
366         {
367             yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
368             return 0;
369         }
370     }
371     return service;
372 }
373
374 static char *parse_settings(xmlNode *node)
375 {
376     xmlChar *src = xmlGetProp(node, (xmlChar *) "src");
377     char *r;
378
379     if (src)
380         r = nmem_strdup(nmem, (const char *) src);
381     else
382     {
383         yaz_log(YLOG_FATAL, "Must specify src in targetprofile");
384         return 0;
385     }
386     xmlFree(src);
387     return r;
388 }
389
390 static struct conf_server *parse_server(xmlNode *node)
391 {
392     xmlNode *n;
393     struct conf_server *r = nmem_malloc(nmem, sizeof(struct conf_server));
394
395     r->host = 0;
396     r->port = 0;
397     r->proxy_host = 0;
398     r->proxy_port = 0;
399     r->myurl = 0;
400     r->zproxy_host = 0;
401     r->zproxy_port = 0;
402     r->service = 0;
403     r->next = 0;
404     r->settings = 0;
405
406     for (n = node->children; n; n = n->next)
407     {
408         if (n->type != XML_ELEMENT_NODE)
409             continue;
410         if (!strcmp((const char *) n->name, "listen"))
411         {
412             xmlChar *port = xmlGetProp(n, (xmlChar *) "port");
413             xmlChar *host = xmlGetProp(n, (xmlChar *) "host");
414             if (port)
415                 r->port = atoi((const char *) port);
416             if (host)
417                 r->host = nmem_strdup(nmem, (const char *) host);
418             xmlFree(port);
419             xmlFree(host);
420         }
421         else if (!strcmp((const char *) n->name, "proxy"))
422         {
423             xmlChar *port = xmlGetProp(n, (xmlChar *) "port");
424             xmlChar *host = xmlGetProp(n, (xmlChar *) "host");
425             xmlChar *myurl = xmlGetProp(n, (xmlChar *) "myurl");
426             if (port)
427                 r->proxy_port = atoi((const char *) port);
428             if (host)
429                 r->proxy_host = nmem_strdup(nmem, (const char *) host);
430             if (myurl)
431                 r->myurl = nmem_strdup(nmem, (const char *) myurl);
432 #ifdef GAGA
433             else
434             {
435                 yaz_log(YLOG_FATAL, "Must specify @myurl for proxy");
436                 return 0;
437             }
438 #endif
439             xmlFree(port);
440             xmlFree(host);
441             xmlFree(myurl);
442         }
443         else if (!strcmp((const char *) n->name, "zproxy"))
444         {
445             xmlChar *port = 0;
446             xmlChar *host = 0;
447
448             port = xmlGetProp(n, (xmlChar *) "port");
449             host = xmlGetProp(n, (xmlChar *) "host");
450
451             if (port)
452                 r->zproxy_port = atoi((const char *) port);
453             if (host)
454                 r->zproxy_host = nmem_strdup(nmem, (const char *) host);
455
456             xmlFree(port);
457             xmlFree(host);
458         }
459         else if (!strcmp((const char *) n->name, "settings"))
460         {
461             if (r->settings)
462             {
463                 yaz_log(YLOG_FATAL, "Can't repeat 'settings'");
464                 return 0;
465             }
466             if (!(r->settings = parse_settings(n)))
467                 return 0;
468         }
469         else if (!strcmp((const char *) n->name, "service"))
470         {
471             struct conf_service *s = parse_service(n);
472             if (!s)
473                 return 0;
474             r->service = s;
475         }
476         else
477         {
478             yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
479             return 0;
480         }
481     }
482     return r;
483 }
484
485 xsltStylesheet *conf_load_stylesheet(const char *fname)
486 {
487     char path[256];
488     sprintf(path, "%s/%s", confdir, fname);
489     return xsltParseStylesheetFile((xmlChar *) path);
490 }
491
492 static struct conf_targetprofiles *parse_targetprofiles(xmlNode *node)
493 {
494     struct conf_targetprofiles *r = nmem_malloc(nmem, sizeof(*r));
495     xmlChar *type = xmlGetProp(node, (xmlChar *) "type");
496     xmlChar *src = xmlGetProp(node, (xmlChar *) "src");
497
498     memset(r, 0, sizeof(*r));
499
500     if (type)
501     {
502         if (!strcmp((const char *) type, "local"))
503             r->type = Targetprofiles_local;
504         else
505         {
506             yaz_log(YLOG_FATAL, "Unknown targetprofile type");
507             return 0;
508         }
509     }
510     else
511     {
512         yaz_log(YLOG_FATAL, "Must specify type for targetprofile");
513         return 0;
514     }
515
516     if (src)
517         r->src = nmem_strdup(nmem, (const char *) src);
518     else
519     {
520         yaz_log(YLOG_FATAL, "Must specify src in targetprofile");
521         return 0;
522     }
523     xmlFree(type);
524     xmlFree(src);
525     return r;
526 }
527
528 static struct conf_config *parse_config(xmlNode *root)
529 {
530     xmlNode *n;
531     struct conf_config *r = nmem_malloc(nmem, sizeof(struct conf_config));
532
533     r->servers = 0;
534     r->targetprofiles = 0;
535
536     for (n = root->children; n; n = n->next)
537     {
538         if (n->type != XML_ELEMENT_NODE)
539             continue;
540         if (!strcmp((const char *) n->name, "server"))
541         {
542             struct conf_server *tmp = parse_server(n);
543             if (!tmp)
544                 return 0;
545             tmp->next = r->servers;
546             r->servers = tmp;
547         }
548         else if (!strcmp((const char *) n->name, "targetprofiles"))
549         {
550             // It would be fun to be able to fix this sometime
551             if (r->targetprofiles)
552             {
553                 yaz_log(YLOG_FATAL, "Can't repeat targetprofiles");
554                 return 0;
555             }
556             if (!(r->targetprofiles = parse_targetprofiles(n)))
557                 return 0;
558         }
559         else
560         {
561             yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
562             return 0;
563         }
564     }
565     return r;
566 }
567
568 int read_config(const char *fname)
569 {
570     xmlDoc *doc = xmlParseFile(fname);
571     const char *p;
572
573     if (!nmem)  // Initialize
574     {
575         nmem = nmem_create();
576         xmlSubstituteEntitiesDefault(1);
577         xmlLoadExtDtdDefaultValue = 1;
578     }
579     if (!doc)
580     {
581         yaz_log(YLOG_FATAL, "Failed to read %s", fname);
582         exit(1);
583     }
584     if ((p = strrchr(fname, '/')))
585     {
586         int len = p - fname;
587         strncpy(confdir, fname, len);
588         confdir[len] = '\0';
589     }
590     config = parse_config(xmlDocGetRootElement(doc));
591     xmlFreeDoc(doc);
592
593     if (config)
594         return 1;
595     else
596         return 0;
597 }
598
599
600 /*
601  * Local variables:
602  * c-basic-offset: 4
603  * indent-tabs-mode: nil
604  * End:
605  * vim: shiftwidth=4 tabstop=8 expandtab
606  */