1 /* This file is part of Pazpar2.
2 Copyright (C) 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
25 #include <sys/types.h>
33 #include "pazpar2_config.h"
38 #include <sys/types.h>
40 enum pazpar2_database_criterion_type {
42 PAZPAR2_SUBSTRING_MATCH
45 struct database_criterion_value {
47 struct database_criterion_value *next;
50 struct database_criterion {
52 enum pazpar2_database_criterion_type type;
53 struct database_criterion_value *values;
54 struct database_criterion *next;
57 struct database *new_database(const char *id, NMEM nmem)
59 return new_database_inherit_settings(id, nmem, 0);
61 struct database *new_database_inherit_settings(const char *id, NMEM nmem, struct settings_array *service_settings)
64 struct setting *idset;
66 db = nmem_malloc(nmem, sizeof(*db));
67 db->id = nmem_strdup(nmem, id);
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);
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);
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;
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)
99 for (p = service->databases; p; p = p->next)
100 if (!strcmp(p->id, id))
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;
112 // This whole session_grep database thing should be moved elsewhere
114 int match_zurl(const char *zurl, const char *pattern)
118 if (!strcmp(pattern, "*"))
120 else if (!strncmp(pattern, "*/", 2)) // host wildcard.. what the heck is that for?
122 char *db = strchr(zurl, '/');
125 if (!strcmp(pattern + 2, db))
130 else if (*(pattern + (len = strlen(pattern) - 1)) == '*') // db wildcard
132 if (!strncmp(pattern, zurl, len))
137 else if (!strcmp(pattern, zurl))
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)
148 int offset = settings_lookup_offset(service, c->name);
149 struct database_criterion_value *v;
153 yaz_log(YLOG_WARN, "Criterion not found: %s", c->name);
156 if (!settings[offset])
158 for (v = c->values; v; v = v->next)
160 if (c->type == PAZPAR2_STRING_MATCH)
164 if (match_zurl(settings[offset]->value, v->value))
169 if (!strcmp(settings[offset]->value, v->value))
173 else if (c->type == PAZPAR2_SUBSTRING_MATCH)
175 if (strstr(settings[offset]->value, v->value))
185 // parses crit1=val1,crit2=val2|val3,...
186 static struct database_criterion *create_database_criterion(NMEM m,
189 struct database_criterion *res = 0;
196 nmem_strsplit(m, ",", buf, &values, &num);
197 for (i = 0; i < num; i++)
202 struct database_criterion *new = nmem_malloc(m, sizeof(*new));
204 for (eq = values[i]; *eq; eq++)
207 new->type = PAZPAR2_STRING_MATCH;
212 new->type = PAZPAR2_SUBSTRING_MATCH;
217 yaz_log(YLOG_WARN, "Missing equal-sign/tilde in filter");
221 new->name = values[i];
222 nmem_strsplit(m, "|", eq, &subvalues, &subnum);
224 for (subi = 0; subi < subnum; subi++)
226 struct database_criterion_value *newv
227 = nmem_malloc(m, sizeof(*newv));
228 newv->value = subvalues[subi];
229 newv->next = new->values;
238 static int database_match_criteria(struct setting **settings,
239 struct conf_service *service,
240 struct database_criterion *cl)
242 for (; cl; cl = cl->next)
243 if (!match_criterion(settings, service, cl))
245 if (cl) // one of the criteria failed to match -- skip this db
251 // Cycles through databases, calling a handler function on the ones for
252 // which all criteria matched.
253 int session_grep_databases(struct session *se, const char *filter,
254 void (*fun)(struct session *se, struct session_database *db))
256 struct session_database *p;
257 NMEM nmem = nmem_create();
259 struct database_criterion *cl = create_database_criterion(nmem, filter);
261 for (p = se->databases; p; p = p->next)
263 if (p->settings && p->settings[PZ_ALLOW] && *p->settings[PZ_ALLOW]->value == '0')
265 if (!p->settings[PZ_NAME])
267 if (database_match_criteria(p->settings, se->service, cl))
277 int predef_grep_databases(void *context, struct conf_service *service,
278 void (*fun)(void *context, struct database *db))
283 for (p = service->databases; p; p = p->next)
284 if (database_match_criteria(p->settings, service, 0))
295 * c-file-style: "Stroustrup"
296 * indent-tabs-mode: nil
298 * vim: shiftwidth=4 tabstop=8 expandtab