Bump copyright year
[pazpar2-moved-to-github.git] / src / database.c
1 /* This file is part of Pazpar2.
2    Copyright (C) 2006-2013 Index Data
3
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
7 version.
8
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
12 for more details.
13
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
17
18 */
19
20 #if HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <assert.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <yaz/log.h>
28 #include <yaz/nmem.h>
29
30 #include "ppmutex.h"
31 #include "session.h"
32 #include "host.h"
33 #include "pazpar2_config.h"
34 #include "settings.h"
35 #include "http.h"
36 #include "database.h"
37
38 #include <sys/types.h>
39
40 enum pazpar2_database_criterion_type {
41     PAZPAR2_STRING_MATCH,
42     PAZPAR2_SUBSTRING_MATCH
43 };
44
45 struct database_criterion_value {
46     char *value;
47     struct database_criterion_value *next;
48 };
49
50 struct database_criterion {
51     char *name;
52     enum pazpar2_database_criterion_type type;
53     struct database_criterion_value *values;
54     struct database_criterion *next;
55 };
56
57 struct database *new_database(const char *id, NMEM nmem)
58 {
59     return new_database_inherit_settings(id, nmem, 0);
60 }
61 struct database *new_database_inherit_settings(const char *id, NMEM nmem, struct settings_array *service_settings)
62 {
63     struct database *db;
64     struct setting *idset;
65
66     db = nmem_malloc(nmem, sizeof(*db));
67     db->id = nmem_strdup(nmem, id);
68     db->next = 0;
69
70     if (service_settings && service_settings->num_settings > 0) {
71         yaz_log(YLOG_DEBUG, "copying settings from service to database %s settings", db->id);
72         db->num_settings = service_settings->num_settings;
73         db->settings = nmem_malloc(nmem, sizeof(*db->settings) * db->num_settings);
74         // Initialize database settings with service settings
75         memcpy(db->settings, service_settings->settings,  sizeof(*db->settings) * db->num_settings);
76     }
77     else {
78         yaz_log(YLOG_DEBUG, "No service settings to database %s ", db->id);
79         db->num_settings = PZ_MAX_EOF;
80         db->settings = nmem_malloc(nmem, sizeof(*db->settings) * db->num_settings);
81         memset(db->settings, 0, sizeof(*db->settings) * db->num_settings);
82     }
83     idset = nmem_malloc(nmem, sizeof(*idset));
84     idset->precedence = 0;
85     idset->name = "pz:id";
86     idset->target = idset->value = db->id;
87     idset->next = db->settings[PZ_ID];
88     db->settings[PZ_ID] = idset;
89
90     return db;
91 }
92
93 // Return a database structure by ID. Load and add to list if necessary
94 // new==1 just means we know it's not in the list
95 struct database *create_database_for_service(const char *id,
96                                              struct conf_service *service)
97 {
98     struct database *p;
99     for (p = service->databases; p; p = p->next)
100         if (!strcmp(p->id, id))
101             return p;
102
103     yaz_log(YLOG_DEBUG, "new database %s under service %s", id,
104        service->id ? service->id : "null");
105     p = new_database_inherit_settings(id, service->nmem, service->settings);
106     p->next = service->databases;
107     service->databases = p;
108
109     return p;
110 }
111
112 // This whole session_grep database thing should be moved elsewhere
113
114 int match_zurl(const char *zurl, const char *pattern)
115 {
116     int len;
117
118     if (!strcmp(pattern, "*"))
119         return 1;
120     else if (!strncmp(pattern, "*/", 2))   // host wildcard.. what the heck is that for?
121     {
122         char *db = strchr(zurl, '/');
123         if (!db)
124             return 0;
125         if (!strcmp(pattern + 2, db))
126             return 1;
127         else
128             return 0;
129     }
130     else if (*(pattern + (len = strlen(pattern) - 1)) == '*')  // db wildcard
131     {
132         if (!strncmp(pattern, zurl, len))
133             return 1;
134         else
135             return 0;
136     }
137     else if (!strcmp(pattern, zurl))
138         return 1;
139     else
140         return 0;
141 }
142
143 // This will be generalized at some point
144 static int match_criterion(struct setting **settings,
145                            struct conf_service *service,
146                            struct database_criterion *c)
147 {
148     int offset = settings_lookup_offset(service, c->name);
149     struct database_criterion_value *v;
150
151     if (offset < 0)
152     {
153         yaz_log(YLOG_WARN, "Criterion not found: %s", c->name);
154         return 0;
155     }
156     if (!settings[offset])
157         return 0;
158     for (v = c->values; v; v = v->next)
159     {
160         if (c->type == PAZPAR2_STRING_MATCH)
161         {
162             if (offset == PZ_ID)
163             {
164                 if (match_zurl(settings[offset]->value, v->value))
165                     break;
166             }
167             else
168             {
169                 if (!strcmp(settings[offset]->value, v->value))
170                     break;
171             }
172         }
173         else if (c->type == PAZPAR2_SUBSTRING_MATCH)
174         {
175             if (strstr(settings[offset]->value, v->value))
176                 break;
177         }
178     }
179     if (v)
180         return 1;
181     else
182         return 0;
183 }
184
185 // parses crit1=val1,crit2=val2|val3,...
186 static struct database_criterion *create_database_criterion(NMEM m,
187                                                             const char *buf)
188 {
189     struct database_criterion *res = 0;
190     char **values;
191     int num;
192     int i;
193
194     if (!buf || !*buf)
195         return 0;
196     nmem_strsplit(m, ",", buf,  &values, &num);
197     for (i = 0; i < num; i++)
198     {
199         char **subvalues;
200         int subnum;
201         int subi;
202         struct database_criterion *new = nmem_malloc(m, sizeof(*new));
203         char *eq;
204         if ((eq = strchr(values[i], '=')))
205             new->type = PAZPAR2_STRING_MATCH;
206         else if ((eq = strchr(values[i], '~')))
207             new->type = PAZPAR2_SUBSTRING_MATCH;
208         else
209         {
210             yaz_log(YLOG_WARN, "Missing equal-sign/tilde in filter");
211             return 0;
212         }
213         *(eq++) = '\0';
214         new->name = values[i];
215         nmem_strsplit(m, "|", eq, &subvalues, &subnum);
216         new->values = 0;
217         for (subi = 0; subi < subnum; subi++)
218         {
219             struct database_criterion_value *newv
220                 = nmem_malloc(m, sizeof(*newv));
221             newv->value = subvalues[subi];
222             newv->next = new->values;
223             new->values = newv;
224         }
225         new->next = res;
226         res = new;
227     }
228     return res;
229 }
230
231 static int database_match_criteria(struct setting **settings,
232                                    struct conf_service *service,
233                                    struct database_criterion *cl)
234 {
235     for (; cl; cl = cl->next)
236         if (!match_criterion(settings, service, cl))
237             break;
238     if (cl) // one of the criteria failed to match -- skip this db
239         return 0;
240     else
241         return 1;
242 }
243
244 // Cycles through databases, calling a handler function on the ones for
245 // which all criteria matched.
246 int session_grep_databases(struct session *se, const char *filter,
247                            void (*fun)(struct session *se, struct session_database *db))
248 {
249     struct session_database *p;
250     NMEM nmem = nmem_create();
251     int i = 0;
252     struct database_criterion *cl = create_database_criterion(nmem, filter);
253
254     for (p = se->databases; p; p = p->next)
255     {
256         if (p->settings && p->settings[PZ_ALLOW] && *p->settings[PZ_ALLOW]->value == '0')
257             continue;
258         if (!p->settings[PZ_NAME])
259             continue;
260         if (database_match_criteria(p->settings, se->service, cl))
261         {
262             (*fun)(se, p);
263             i++;
264         }
265     }
266     nmem_destroy(nmem);
267     return i;
268 }
269
270 int predef_grep_databases(void *context, struct conf_service *service,
271                           void (*fun)(void *context, struct database *db))
272 {
273     struct database *p;
274     int i = 0;
275
276     for (p = service->databases; p; p = p->next)
277         if (database_match_criteria(p->settings, service, 0))
278         {
279             (*fun)(context, p);
280             i++;
281         }
282     return i;
283 }
284
285 /*
286  * Local variables:
287  * c-basic-offset: 4
288  * c-file-style: "Stroustrup"
289  * indent-tabs-mode: nil
290  * End:
291  * vim: shiftwidth=4 tabstop=8 expandtab
292  */
293