Implemented pz:allow (turn access to targets on or off)
[pazpar2-moved-to-github.git] / src / database.c
index 525ed2a..639e8f2 100644 (file)
@@ -1,4 +1,23 @@
-/* $Id: database.c,v 1.7 2007-04-08 20:52:09 quinn Exp $ */
+/* $Id: database.c,v 1.10 2007-04-11 04:33:41 quinn Exp $
+   Copyright (c) 2006-2007, Index Data.
+
+This file is part of Pazpar2.
+
+Pazpar2 is free software; you can redistribute it and/or modify it under
+the terms of the GNU General Public License as published by the Free
+Software Foundation; either version 2, or (at your option) any later
+version.
+
+Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+for more details.
+
+You should have received a copy of the GNU General Public License
+along with Pazpar2; see the file LICENSE.  If not, write to the
+Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
+02111-1307, USA.
+ */
 
 #include <libxml/parser.h>
 #include <libxml/tree.h>
@@ -165,7 +184,9 @@ struct database *find_database(const char *id, int new)
     return load_database(id);
 }
 
-static int match_zurl(const char *zurl, const char *pattern)
+// This whole session_grep database thing should be moved to pazpar2.c
+
+int match_zurl(const char *zurl, const char *pattern)
 {
     if (!strcmp(pattern, "*"))
         return 1;
@@ -186,24 +207,44 @@ static int match_zurl(const char *zurl, const char *pattern)
 }
 
 // This will be generalized at some point
-static int match_criterion(struct database *db, struct database_criterion *c)
+static int match_criterion(struct setting **settings, struct database_criterion *c)
 {
-    if (!strcmp(c->name, "id"))
+    int offset = settings_offset(c->name);
+    struct database_criterion_value *v;
+
+    if (offset < 0)
     {
-        struct database_criterion_value *v;
-        for (v = c->values; v; v = v->next)
-            if (match_zurl(db->url, v->value))
-                return 1;
+        yaz_log(YLOG_WARN, "Criterion not found: %s", c->name);
         return 0;
     }
-    else
+    if (!settings[offset])
         return 0;
+    for (v = c->values; v; v = v->next)
+    {
+        if (offset == PZ_ID)
+        {
+            if (match_zurl(settings[offset]->value, v->value))
+                return 1;
+            else
+                return 0;
+        }
+        else 
+        {
+            if (!strcmp(settings[offset]->value, v->value))
+                return 1;
+            else
+                return 0;
+        }
+    }
+    return 0;
 }
 
-int database_match_criteria(struct database *db, struct database_criterion *cl)
+int database_match_criteria(struct setting **settings, struct database_criterion *cl)
 {
+    if (settings[PZ_ALLOW] && *settings[PZ_ALLOW]->value == '0')
+        return 0;
     for (; cl; cl = cl->next)
-        if (!match_criterion(db, cl))
+        if (!match_criterion(settings, cl))
             break;
     if (cl) // one of the criteria failed to match -- skip this db
         return 0;
@@ -213,20 +254,33 @@ int database_match_criteria(struct database *db, struct database_criterion *cl)
 
 // Cycles through databases, calling a handler function on the ones for
 // which all criteria matched.
+int session_grep_databases(struct session *se, struct database_criterion *cl,
+        void (*fun)(void *context, struct session_database *db))
+{
+    struct session_database *p;
+    int i = 0;
+
+    for (p = se->databases; p; p = p->next)
+        if (database_match_criteria(p->settings, cl))
+        {
+            (*fun)(se, p);
+            i++;
+        }
+    return i;
+}
+
 int grep_databases(void *context, struct database_criterion *cl,
         void (*fun)(void *context, struct database *db))
 {
     struct database *p;
-    int i;
+    int i = 0;
 
     for (p = databases; p; p = p->next)
-    {
-        if (database_match_criteria(p, cl))
+        if (database_match_criteria(p->settings, cl))
         {
             (*fun)(context, p);
             i++;
         }
-    }
     return i;
 }