[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"
|
---|
[15418] | 30 | #include "recptprototools.h"
|
---|
[928] | 31 |
|
---|
[1913] | 32 | #define MAX_RECORDS 20
|
---|
[11751] | 33 | #define HIST_SEP ';'
|
---|
[928] | 34 |
|
---|
| 35 | // returns true on success (in which case historyinfo will contain
|
---|
| 36 | // the information for this history)
|
---|
[963] | 37 | bool get_history_info (const text_t &userid, text_tarray &historyinfo,
|
---|
| 38 | const text_t &gsdlhome, ostream &logout) {
|
---|
[939] | 39 |
|
---|
[963] | 40 | text_t historyfile = filename_cat(gsdlhome, "etc", "history.db");
|
---|
[928] | 41 |
|
---|
| 42 | bool result = false;
|
---|
| 43 | // open the history database
|
---|
| 44 | gdbmclass historydb;
|
---|
| 45 |
|
---|
| 46 | if (historydb.opendatabase(historyfile, GDBM_READER, 1000, true)) {
|
---|
| 47 | // get history list
|
---|
| 48 | text_t historyresult;
|
---|
[939] | 49 |
|
---|
[928] | 50 | historydb.getkeydata(userid, historyresult);
|
---|
| 51 |
|
---|
| 52 | if (historyresult != "") { // there are entries, process them
|
---|
[939] | 53 |
|
---|
[928] | 54 | splitchar(historyresult.begin(), historyresult.end(), '\n', historyinfo);
|
---|
| 55 | result = true;
|
---|
| 56 | }
|
---|
| 57 | historydb.closedatabase();
|
---|
| 58 |
|
---|
[939] | 59 | } else {
|
---|
| 60 | outconvertclass text_t2ascii;
|
---|
| 61 | logout << text_t2ascii << "couldn't open history database " << historyfile << "\n";
|
---|
| 62 | }
|
---|
[928] | 63 | return result;
|
---|
[939] | 64 | }
|
---|
[928] | 65 |
|
---|
| 66 |
|
---|
| 67 | // returns true on success
|
---|
[1913] | 68 | // changed to only save 20 records per user, numbers not included
|
---|
| 69 | // only save if there are already entries there, or if display=true
|
---|
| 70 | bool set_history_info (const text_t &userid, const text_t &history, const text_t &gsdlhome, bool display) {
|
---|
[928] | 71 |
|
---|
[963] | 72 | text_t historyfile = filename_cat(gsdlhome, "etc", "history.db");
|
---|
[928] | 73 |
|
---|
| 74 | bool result = false;
|
---|
| 75 | // open the history database
|
---|
| 76 | gdbmclass historydb;
|
---|
| 77 |
|
---|
[1913] | 78 | text_t oldhistoryresult;
|
---|
| 79 | text_t newhistoryresult;
|
---|
| 80 | int numentries=0;
|
---|
[928] | 81 |
|
---|
| 82 | if ( !historydb.opendatabase(historyfile, GDBM_READER, 1000, true)) {
|
---|
| 83 | // not created yet
|
---|
[1913] | 84 | oldhistoryresult="";
|
---|
| 85 | if (!display) return true; // dont need to save
|
---|
[928] | 86 | }
|
---|
| 87 | else {
|
---|
| 88 |
|
---|
| 89 | // get history list
|
---|
[1913] | 90 | if (! historydb.getkeydata(userid, oldhistoryresult)) {
|
---|
| 91 | oldhistoryresult="";
|
---|
| 92 | if (!display) return true; // dont need to save
|
---|
[928] | 93 | }
|
---|
| 94 | historydb.closedatabase();
|
---|
| 95 | }
|
---|
| 96 |
|
---|
[1913] | 97 | text_tarray entries;
|
---|
| 98 |
|
---|
| 99 | if (oldhistoryresult!="") {
|
---|
| 100 | splitchar(oldhistoryresult.begin(), oldhistoryresult.end(), '\n', entries);
|
---|
| 101 | numentries = entries.size();
|
---|
| 102 | if (numentries >= MAX_RECORDS) numentries = MAX_RECORDS-1;
|
---|
[928] | 103 | }
|
---|
| 104 |
|
---|
| 105 | // open for writing
|
---|
| 106 | if (!historydb.opendatabase(historyfile, GDBM_WRCREAT, 1000, true)) return false;
|
---|
| 107 |
|
---|
[1913] | 108 | // add on new linethe new record to the front of the list, then add the
|
---|
| 109 | // appropriate entries from the old stuff
|
---|
| 110 | newhistoryresult += history;
|
---|
| 111 | newhistoryresult += "\n";
|
---|
[9620] | 112 | for (int i=0; i<numentries;++i) {
|
---|
[1913] | 113 | newhistoryresult += entries[i]+"\n";
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | if (historydb.setinfo(userid, newhistoryresult))
|
---|
| 117 | result=true;
|
---|
| 118 |
|
---|
| 119 | historydb.closedatabase();
|
---|
| 120 | return result;
|
---|
[928] | 121 | }
|
---|
| 122 |
|
---|
| 123 | // deletes all a users history
|
---|
[963] | 124 | bool delete_all_history_info (const text_t &userid, const text_t &gsdlhome) {
|
---|
[939] | 125 |
|
---|
[963] | 126 | text_t historyfile = filename_cat(gsdlhome, "etc", "history.db");
|
---|
[928] | 127 |
|
---|
| 128 | // open the history database
|
---|
| 129 | gdbmclass historydb;
|
---|
| 130 |
|
---|
| 131 | if ( !historydb.opendatabase(historyfile, GDBM_WRITER, 1000, true)) return false;
|
---|
| 132 |
|
---|
| 133 | historydb.deletekey(userid);
|
---|
[939] | 134 | historydb.closedatabase();
|
---|
[928] | 135 | return true;
|
---|
| 136 |
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | // retrieves the value of one of the arguments
|
---|
[939] | 140 | void parse_saved_args(text_t &args, text_t key, text_t &value)
|
---|
[928] | 141 | {
|
---|
| 142 | text_t::iterator here = args.begin();
|
---|
| 143 | text_t::iterator end = args.end();
|
---|
| 144 | text_t::iterator it;
|
---|
| 145 | while (here != end) {
|
---|
| 146 | if(*here==key[0]) {
|
---|
| 147 | it=findchar(here, end, '=');
|
---|
| 148 | if (it==end) {
|
---|
| 149 | value=""; return;
|
---|
| 150 | }
|
---|
| 151 | text_t entry = substr(here, it);
|
---|
| 152 | if (entry==key) {
|
---|
| 153 | here=it+1;
|
---|
[11751] | 154 | it=findchar(here, end, HIST_SEP);
|
---|
[928] | 155 | value = substr(here, it);
|
---|
| 156 | return;
|
---|
| 157 | }
|
---|
| 158 | }
|
---|
[9620] | 159 | ++here;
|
---|
[928] | 160 | }// while
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | // retrieves the value of all of the arguments
|
---|
| 164 | void parse_saved_args(text_t &args, infodbclass &info)
|
---|
| 165 | {
|
---|
| 166 | text_t::iterator here = args.begin();
|
---|
| 167 | text_t::iterator end = args.end();
|
---|
| 168 | text_t::iterator it;
|
---|
| 169 | text_tarray values;
|
---|
| 170 |
|
---|
[11751] | 171 | splitchar(here, end, HIST_SEP, values);
|
---|
[928] | 172 |
|
---|
| 173 | text_tarray::iterator start = values.begin();
|
---|
| 174 | text_tarray::iterator stop = values.end();
|
---|
| 175 |
|
---|
| 176 | text_t key;
|
---|
| 177 | text_t value;
|
---|
| 178 | while(start!=stop) {
|
---|
| 179 |
|
---|
| 180 | here=(*start).begin();
|
---|
| 181 | end=(*start).end();
|
---|
| 182 | it=findchar(here, end, '=');
|
---|
| 183 | if (it!=end) {
|
---|
| 184 | key = substr(here, it);
|
---|
| 185 | value = substr(it+1, end);
|
---|
| 186 |
|
---|
| 187 | info[key]=value;
|
---|
| 188 | }
|
---|
[9620] | 189 | ++start;
|
---|
[928] | 190 | }
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 |
|
---|
[1913] | 194 | void split_saved_query(text_t &query, text_t &numdocs, text_t &cgiargs)
|
---|
[928] | 195 | {
|
---|
| 196 | text_t::iterator begin = query.begin();
|
---|
| 197 | text_t::iterator end = query.end();
|
---|
[939] | 198 |
|
---|
[1913] | 199 | while (*begin >='0'&& *begin <='9') { // get the digits for numdocs
|
---|
[939] | 200 | numdocs.push_back(*begin);
|
---|
[9620] | 201 | ++begin;
|
---|
[1913] | 202 | }
|
---|
| 203 |
|
---|
[928] | 204 | if (*begin == '+') { // get the + if there
|
---|
| 205 | numdocs.push_back(*begin);
|
---|
[9620] | 206 | ++begin;
|
---|
[928] | 207 | }
|
---|
[1913] | 208 | if (*begin == ':') { // have the old format - previous bit was record number
|
---|
| 209 | numdocs.clear();
|
---|
[9620] | 210 | ++begin;
|
---|
[928] | 211 |
|
---|
[1913] | 212 | while(*begin >='0' && *begin <='9') { // get the digits
|
---|
| 213 | numdocs.push_back(*begin);
|
---|
[9620] | 214 | ++begin;
|
---|
[1913] | 215 | }
|
---|
| 216 | if (*begin == '+') { // get the + if there
|
---|
| 217 | numdocs.push_back(*begin);
|
---|
[9620] | 218 | ++begin;
|
---|
[1913] | 219 | }
|
---|
| 220 | }
|
---|
[928] | 221 | cgiargs += (substr(begin, end)); // put rest of query into cgiargs
|
---|
| 222 |
|
---|
| 223 | }
|
---|
| 224 |
|
---|
[1913] | 225 | void format_user_info (text_t &historyargs, text_t &userinfo,
|
---|
| 226 | cgiargsclass &args,
|
---|
| 227 | recptprotolistclass *protos, ostream &logout)
|
---|
[928] | 228 | {
|
---|
| 229 | text_tset metadata;
|
---|
[1913] | 230 | userinfo.clear();
|
---|
| 231 |
|
---|
[928] | 232 | infodbclass argsinfo;
|
---|
[1913] | 233 | parse_saved_args(historyargs, argsinfo);
|
---|
[928] | 234 | text_t collect = argsinfo["c"];
|
---|
| 235 | if (collect=="") {
|
---|
| 236 | userinfo="";
|
---|
| 237 | return;
|
---|
| 238 | }
|
---|
[11751] | 239 |
|
---|
[1913] | 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 | if (argsinfo["b"]=="0") { // simple mode
|
---|
| 263 | if (argsinfo["t"]=="0") {
|
---|
[11751] | 264 | userinfo += " _texthallwords_, ";
|
---|
[1913] | 265 | }
|
---|
[11751] | 266 | else if (argsinfo["t"]=="1"){
|
---|
| 267 | userinfo += " _texthsomewords_, ";
|
---|
[1913] | 268 | }
|
---|
| 269 |
|
---|
| 270 | }
|
---|
[11751] | 271 | else if (argsinfo["b"] == "1") { // advanced mode
|
---|
[1913] | 272 | if (argsinfo["t"]=="0") {
|
---|
[11751] | 273 | userinfo += " _texthboolean_, ";
|
---|
[1913] | 274 | }
|
---|
[11751] | 275 | else if (argsinfo["t"]=="1") {
|
---|
| 276 | userinfo += " _texthranked_, ";
|
---|
[1913] | 277 | }
|
---|
[928] | 278 | }
|
---|
| 279 | }
|
---|
[939] | 280 |
|
---|
[11751] | 281 | if (argsinfo["k"] != "" && argsinfo["k"] != args["k"]) {
|
---|
[1913] | 282 | text_t options;
|
---|
[11751] | 283 | // the text translations are the wrong way round
|
---|
| 284 | if (argsinfo["k"]=="1") {
|
---|
| 285 | userinfo += " _texthcaseoff_, ";
|
---|
[1913] | 286 | }
|
---|
[11751] | 287 | else if (argsinfo["k"]=="0") {
|
---|
| 288 | userinfo += " _texthcaseon_, ";
|
---|
[1913] | 289 | }
|
---|
[928] | 290 | }
|
---|
[1913] | 291 |
|
---|
[11751] | 292 | if (argsinfo["s"] != "" && argsinfo["s"] != args["s"]) {
|
---|
[1913] | 293 | if (argsinfo["s"]=="0") {
|
---|
[11751] | 294 | userinfo += " _texthstemoff_, ";
|
---|
[1913] | 295 | }
|
---|
[11751] | 296 | else if (argsinfo["s"]=="1") {
|
---|
| 297 | userinfo += " _texthstemon_, ";
|
---|
[1913] | 298 | }
|
---|
[928] | 299 | }
|
---|
| 300 |
|
---|
| 301 | }
|
---|