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