[939] | 1 | /**********************************************************************
|
---|
| 2 | *
|
---|
| 3 | * historydb.cpp --
|
---|
| 4 | * Copyright (C) 1999 The New Zealand Digital Library Project
|
---|
| 5 | *
|
---|
| 6 | * A component of the Greenstone digital library software
|
---|
| 7 | * from the New Zealand Digital Library Project at the
|
---|
| 8 | * University of Waikato, New Zealand.
|
---|
| 9 | *
|
---|
| 10 | * This program is free software; you can redistribute it and/or modify
|
---|
| 11 | * it under the terms of the GNU General Public License as published by
|
---|
| 12 | * the Free Software Foundation; either version 2 of the License, or
|
---|
| 13 | * (at your option) any later version.
|
---|
| 14 | *
|
---|
| 15 | * This program is distributed in the hope that it will be useful,
|
---|
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 18 | * GNU General Public License for more details.
|
---|
| 19 | *
|
---|
| 20 | * You should have received a copy of the GNU General Public License
|
---|
| 21 | * along with this program; if not, write to the Free Software
|
---|
| 22 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 23 | *
|
---|
| 24 | *********************************************************************/
|
---|
[928] | 25 |
|
---|
| 26 | #include "historydb.h"
|
---|
| 27 | #include "fileutil.h"
|
---|
| 28 | #include "cgiutils.h"
|
---|
| 29 | #include "recptproto.h"
|
---|
| 30 | #include "OIDtools.h"
|
---|
| 31 |
|
---|
[1913] | 32 | #define MAX_RECORDS 20
|
---|
[928] | 33 |
|
---|
| 34 | // returns true on success (in which case historyinfo will contain
|
---|
| 35 | // the information for this history)
|
---|
[963] | 36 | bool get_history_info (const text_t &userid, text_tarray &historyinfo,
|
---|
| 37 | const text_t &gsdlhome, ostream &logout) {
|
---|
[939] | 38 |
|
---|
[963] | 39 | text_t historyfile = filename_cat(gsdlhome, "etc", "history.db");
|
---|
[928] | 40 |
|
---|
| 41 | bool result = false;
|
---|
| 42 | // open the history database
|
---|
| 43 | gdbmclass historydb;
|
---|
| 44 |
|
---|
| 45 | if (historydb.opendatabase(historyfile, GDBM_READER, 1000, true)) {
|
---|
| 46 | // get history list
|
---|
| 47 | text_t historyresult;
|
---|
[939] | 48 |
|
---|
[928] | 49 | historydb.getkeydata(userid, historyresult);
|
---|
| 50 |
|
---|
| 51 | if (historyresult != "") { // there are entries, process them
|
---|
[939] | 52 |
|
---|
[928] | 53 | splitchar(historyresult.begin(), historyresult.end(), '\n', historyinfo);
|
---|
| 54 | result = true;
|
---|
| 55 | }
|
---|
| 56 | historydb.closedatabase();
|
---|
| 57 |
|
---|
[939] | 58 | } else {
|
---|
| 59 | outconvertclass text_t2ascii;
|
---|
| 60 | logout << text_t2ascii << "couldn't open history database " << historyfile << "\n";
|
---|
| 61 | }
|
---|
[928] | 62 | return result;
|
---|
[939] | 63 | }
|
---|
[928] | 64 |
|
---|
| 65 |
|
---|
| 66 | // returns true on success
|
---|
[1913] | 67 | // changed to only save 20 records per user, numbers not included
|
---|
| 68 | // only save if there are already entries there, or if display=true
|
---|
| 69 | bool set_history_info (const text_t &userid, const text_t &history, const text_t &gsdlhome, bool display) {
|
---|
[928] | 70 |
|
---|
[963] | 71 | text_t historyfile = filename_cat(gsdlhome, "etc", "history.db");
|
---|
[928] | 72 |
|
---|
| 73 | bool result = false;
|
---|
| 74 | // open the history database
|
---|
| 75 | gdbmclass historydb;
|
---|
| 76 |
|
---|
[1913] | 77 | text_t oldhistoryresult;
|
---|
| 78 | text_t newhistoryresult;
|
---|
| 79 | int numentries=0;
|
---|
[928] | 80 |
|
---|
| 81 | if ( !historydb.opendatabase(historyfile, GDBM_READER, 1000, true)) {
|
---|
| 82 | // not created yet
|
---|
[1913] | 83 | oldhistoryresult="";
|
---|
| 84 | if (!display) return true; // dont need to save
|
---|
[928] | 85 | }
|
---|
| 86 | else {
|
---|
| 87 |
|
---|
| 88 | // get history list
|
---|
[1913] | 89 | if (! historydb.getkeydata(userid, oldhistoryresult)) {
|
---|
| 90 | oldhistoryresult="";
|
---|
| 91 | if (!display) return true; // dont need to save
|
---|
[928] | 92 | }
|
---|
| 93 | historydb.closedatabase();
|
---|
| 94 | }
|
---|
| 95 |
|
---|
[1913] | 96 | text_tarray entries;
|
---|
| 97 |
|
---|
| 98 | if (oldhistoryresult!="") {
|
---|
| 99 | splitchar(oldhistoryresult.begin(), oldhistoryresult.end(), '\n', entries);
|
---|
| 100 | numentries = entries.size();
|
---|
| 101 | if (numentries >= MAX_RECORDS) numentries = MAX_RECORDS-1;
|
---|
[928] | 102 | }
|
---|
| 103 |
|
---|
| 104 | // open for writing
|
---|
| 105 | if (!historydb.opendatabase(historyfile, GDBM_WRCREAT, 1000, true)) return false;
|
---|
| 106 |
|
---|
[1913] | 107 | // add on new linethe new record to the front of the list, then add the
|
---|
| 108 | // appropriate entries from the old stuff
|
---|
| 109 | newhistoryresult += history;
|
---|
| 110 | newhistoryresult += "\n";
|
---|
[9620] | 111 | for (int i=0; i<numentries;++i) {
|
---|
[1913] | 112 | newhistoryresult += entries[i]+"\n";
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | if (historydb.setinfo(userid, newhistoryresult))
|
---|
| 116 | result=true;
|
---|
| 117 |
|
---|
| 118 | historydb.closedatabase();
|
---|
| 119 | return result;
|
---|
[928] | 120 | }
|
---|
| 121 |
|
---|
| 122 | // deletes all a users history
|
---|
[963] | 123 | bool delete_all_history_info (const text_t &userid, const text_t &gsdlhome) {
|
---|
[939] | 124 |
|
---|
[963] | 125 | text_t historyfile = filename_cat(gsdlhome, "etc", "history.db");
|
---|
[928] | 126 |
|
---|
| 127 | // open the history database
|
---|
| 128 | gdbmclass historydb;
|
---|
| 129 |
|
---|
| 130 | if ( !historydb.opendatabase(historyfile, GDBM_WRITER, 1000, true)) return false;
|
---|
| 131 |
|
---|
| 132 | historydb.deletekey(userid);
|
---|
[939] | 133 | historydb.closedatabase();
|
---|
[928] | 134 | return true;
|
---|
| 135 |
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | // retrieves the value of one of the arguments
|
---|
[939] | 139 | void parse_saved_args(text_t &args, text_t key, text_t &value)
|
---|
[928] | 140 | {
|
---|
| 141 | text_t::iterator here = args.begin();
|
---|
| 142 | text_t::iterator end = args.end();
|
---|
| 143 | text_t::iterator it;
|
---|
| 144 | while (here != end) {
|
---|
| 145 | if(*here==key[0]) {
|
---|
| 146 | it=findchar(here, end, '=');
|
---|
| 147 | if (it==end) {
|
---|
| 148 | value=""; return;
|
---|
| 149 | }
|
---|
| 150 | text_t entry = substr(here, it);
|
---|
| 151 | if (entry==key) {
|
---|
| 152 | here=it+1;
|
---|
| 153 | it=findchar(here, end, '&');
|
---|
| 154 | value = substr(here, it);
|
---|
| 155 | return;
|
---|
| 156 | }
|
---|
| 157 | }
|
---|
[9620] | 158 | ++here;
|
---|
[928] | 159 | }// while
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | // retrieves the value of all of the arguments
|
---|
| 163 | void parse_saved_args(text_t &args, infodbclass &info)
|
---|
| 164 | {
|
---|
| 165 | text_t::iterator here = args.begin();
|
---|
| 166 | text_t::iterator end = args.end();
|
---|
| 167 | text_t::iterator it;
|
---|
| 168 | text_tarray values;
|
---|
| 169 |
|
---|
| 170 | splitchar(here, end, '&', values);
|
---|
| 171 |
|
---|
| 172 | text_tarray::iterator start = values.begin();
|
---|
| 173 | text_tarray::iterator stop = values.end();
|
---|
| 174 |
|
---|
| 175 | text_t key;
|
---|
| 176 | text_t value;
|
---|
| 177 | while(start!=stop) {
|
---|
| 178 |
|
---|
| 179 | here=(*start).begin();
|
---|
| 180 | end=(*start).end();
|
---|
| 181 | it=findchar(here, end, '=');
|
---|
| 182 | if (it!=end) {
|
---|
| 183 | key = substr(here, it);
|
---|
| 184 | value = substr(it+1, end);
|
---|
| 185 |
|
---|
| 186 | info[key]=value;
|
---|
| 187 | }
|
---|
[9620] | 188 | ++start;
|
---|
[928] | 189 | }
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 |
|
---|
[1913] | 193 | void split_saved_query(text_t &query, text_t &numdocs, text_t &cgiargs)
|
---|
[928] | 194 | {
|
---|
| 195 | text_t::iterator begin = query.begin();
|
---|
| 196 | text_t::iterator end = query.end();
|
---|
[939] | 197 |
|
---|
[1913] | 198 | while (*begin >='0'&& *begin <='9') { // get the digits for numdocs
|
---|
[939] | 199 | numdocs.push_back(*begin);
|
---|
[9620] | 200 | ++begin;
|
---|
[1913] | 201 | }
|
---|
| 202 |
|
---|
[928] | 203 | if (*begin == '+') { // get the + if there
|
---|
| 204 | numdocs.push_back(*begin);
|
---|
[9620] | 205 | ++begin;
|
---|
[928] | 206 | }
|
---|
[1913] | 207 | if (*begin == ':') { // have the old format - previous bit was record number
|
---|
| 208 | numdocs.clear();
|
---|
[9620] | 209 | ++begin;
|
---|
[928] | 210 |
|
---|
[1913] | 211 | while(*begin >='0' && *begin <='9') { // get the digits
|
---|
| 212 | numdocs.push_back(*begin);
|
---|
[9620] | 213 | ++begin;
|
---|
[1913] | 214 | }
|
---|
| 215 | if (*begin == '+') { // get the + if there
|
---|
| 216 | numdocs.push_back(*begin);
|
---|
[9620] | 217 | ++begin;
|
---|
[1913] | 218 | }
|
---|
| 219 | }
|
---|
[928] | 220 | cgiargs += (substr(begin, end)); // put rest of query into cgiargs
|
---|
| 221 |
|
---|
| 222 | }
|
---|
| 223 |
|
---|
[1913] | 224 | void format_user_info (text_t &historyargs, text_t &userinfo,
|
---|
| 225 | cgiargsclass &args,
|
---|
| 226 | recptprotolistclass *protos, ostream &logout)
|
---|
[928] | 227 | {
|
---|
| 228 | text_tset metadata;
|
---|
[1913] | 229 | userinfo.clear();
|
---|
| 230 |
|
---|
[928] | 231 | infodbclass argsinfo;
|
---|
[1913] | 232 | parse_saved_args(historyargs, argsinfo);
|
---|
[939] | 233 |
|
---|
[928] | 234 | text_t collect = argsinfo["c"];
|
---|
| 235 | if (collect=="") {
|
---|
| 236 | userinfo="";
|
---|
| 237 | return;
|
---|
| 238 | }
|
---|
[1913] | 239 |
|
---|
| 240 | if (collect != args["c"]) {
|
---|
| 241 | userinfo += collect+", ";
|
---|
[928] | 242 | }
|
---|
| 243 |
|
---|
[1913] | 244 | if (argsinfo["h"] != args["h"]) {
|
---|
| 245 |
|
---|
| 246 | recptproto *collectproto = protos->getrecptproto(collect,logout);
|
---|
| 247 | if (collectproto == NULL) {
|
---|
| 248 | userinfo="";
|
---|
| 249 | return;
|
---|
[928] | 250 | }
|
---|
[1913] | 251 | metadata.insert(argsinfo["h"]);
|
---|
| 252 | FilterResponse_t response;
|
---|
[939] | 253 |
|
---|
[7432] | 254 | get_info("collection", collect, args["l"], metadata, false, collectproto, response, logout);
|
---|
[1913] | 255 | text_t index = response.docInfo[0].metadata[argsinfo["h"]].values[0];
|
---|
| 256 | if (!index.empty()) {
|
---|
| 257 | userinfo += index+", ";
|
---|
[928] | 258 | }
|
---|
[1913] | 259 | }
|
---|
| 260 |
|
---|
| 261 | if (argsinfo["b"] != args["b"] || argsinfo["t"] != args["t"]) {
|
---|
| 262 | text_t mode;
|
---|
| 263 | if (argsinfo["b"]=="0") { // simple mode
|
---|
| 264 | if (argsinfo["t"]=="0") {
|
---|
| 265 | mode = " _texthallwords_";
|
---|
| 266 | }
|
---|
| 267 | else { // t=1
|
---|
| 268 | mode = " _texthsomewords_";
|
---|
| 269 | }
|
---|
| 270 |
|
---|
| 271 | }
|
---|
| 272 | else { // advanced mode
|
---|
| 273 | if (argsinfo["t"]=="0") {
|
---|
| 274 | mode = " _texthboolean_";
|
---|
| 275 | }
|
---|
| 276 | else {
|
---|
| 277 | mode = " _texthranked_";
|
---|
| 278 | }
|
---|
[928] | 279 | }
|
---|
[1913] | 280 | userinfo += mode+", ";
|
---|
[928] | 281 | }
|
---|
[939] | 282 |
|
---|
[1913] | 283 | if (argsinfo["k"] != args["k"]) {
|
---|
| 284 | text_t options;
|
---|
| 285 | if (argsinfo["k"]=="0") {
|
---|
| 286 | options = " _texthcaseoff_";
|
---|
| 287 | }
|
---|
| 288 | else {
|
---|
| 289 | options = " _texthcaseon_";
|
---|
| 290 | }
|
---|
| 291 | userinfo += options+", ";
|
---|
[928] | 292 | }
|
---|
[1913] | 293 |
|
---|
| 294 | if (argsinfo["s"] != args["s"]) {
|
---|
| 295 | text_t stem;
|
---|
| 296 | if (argsinfo["s"]=="0") {
|
---|
| 297 | stem = " _texthstemoff_";
|
---|
| 298 | }
|
---|
| 299 | else {
|
---|
| 300 | stem = " _texthstemon_";
|
---|
| 301 | }
|
---|
| 302 | userinfo += stem+", ";
|
---|
[928] | 303 | }
|
---|
| 304 |
|
---|
| 305 | }
|
---|