root/gsdl/trunk/runtime-src/src/recpt/historydb.cpp @ 19062

Revision 19062, 8.0 KB (checked in by kjdon, 10 years ago)

all gdbm files (key, users, history, argdb) now use gdb extension instead of db

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