source: trunk/gsdl/src/recpt/historydb.cpp@ 1913

Last change on this file since 1913 was 1913, checked in by kjm18, 23 years ago

history revamp. new format: numdocs args. query num no longer saved, so
#x in a query no longer references previous queries. deletion of history no
longer provided, max saved queries per user set in MAX_RECORDS (20).
For new user, history only saved once history display has been turned on.

  • Property svn:keywords set to Author Date Id Revision
File size: 7.7 KB
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 "OIDtools.h"
31
32#define MAX_RECORDS 20
33
34// returns true on success (in which case historyinfo will contain
35// the information for this history)
36bool get_history_info (const text_t &userid, text_tarray &historyinfo,
37 const text_t &gsdlhome, ostream &logout) {
38
39 text_t historyfile = filename_cat(gsdlhome, "etc", "history.db");
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;
48
49 historydb.getkeydata(userid, historyresult);
50
51 if (historyresult != "") { // there are entries, process them
52
53 splitchar(historyresult.begin(), historyresult.end(), '\n', historyinfo);
54 result = true;
55 }
56 historydb.closedatabase();
57
58 } else {
59 outconvertclass text_t2ascii;
60 logout << text_t2ascii << "couldn't open history database " << historyfile << "\n";
61 }
62 return result;
63}
64
65
66// returns true on success
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
69bool set_history_info (const text_t &userid, const text_t &history, const text_t &gsdlhome, bool display) {
70
71 text_t historyfile = filename_cat(gsdlhome, "etc", "history.db");
72
73 bool result = false;
74 // open the history database
75 gdbmclass historydb;
76
77 text_t oldhistoryresult;
78 text_t newhistoryresult;
79 int numentries=0;
80
81 if ( !historydb.opendatabase(historyfile, GDBM_READER, 1000, true)) {
82 // not created yet
83 oldhistoryresult="";
84 if (!display) return true; // dont need to save
85 }
86 else {
87
88 // get history list
89 if (! historydb.getkeydata(userid, oldhistoryresult)) {
90 oldhistoryresult="";
91 if (!display) return true; // dont need to save
92 }
93 historydb.closedatabase();
94 }
95
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;
102 }
103
104 // open for writing
105 if (!historydb.opendatabase(historyfile, GDBM_WRCREAT, 1000, true)) return false;
106
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";
111 for (int i=0; i<numentries;i++) {
112 newhistoryresult += entries[i]+"\n";
113 }
114
115 if (historydb.setinfo(userid, newhistoryresult))
116 result=true;
117
118 historydb.closedatabase();
119 return result;
120}
121
122// deletes all a users history
123bool delete_all_history_info (const text_t &userid, const text_t &gsdlhome) {
124
125 text_t historyfile = filename_cat(gsdlhome, "etc", "history.db");
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);
133 historydb.closedatabase();
134 return true;
135
136}
137
138// retrieves the value of one of the arguments
139void parse_saved_args(text_t &args, text_t key, text_t &value)
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 }
158 here++;
159 }// while
160}
161
162// retrieves the value of all of the arguments
163void 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 }
188 start++;
189 }
190}
191
192
193void split_saved_query(text_t &query, text_t &numdocs, text_t &cgiargs)
194{
195 text_t::iterator begin = query.begin();
196 text_t::iterator end = query.end();
197
198 while (*begin >='0'&& *begin <='9') { // get the digits for numdocs
199 numdocs.push_back(*begin);
200 begin++;
201 }
202
203 if (*begin == '+') { // get the + if there
204 numdocs.push_back(*begin);
205 begin++;
206 }
207 if (*begin == ':') { // have the old format - previous bit was record number
208 numdocs.clear();
209 begin++;
210
211 while(*begin >='0' && *begin <='9') { // get the digits
212 numdocs.push_back(*begin);
213 begin++;
214 }
215 if (*begin == '+') { // get the + if there
216 numdocs.push_back(*begin);
217 begin++;
218 }
219 }
220 cgiargs += (substr(begin, end)); // put rest of query into cgiargs
221
222}
223
224void format_user_info (text_t &historyargs, text_t &userinfo,
225 cgiargsclass &args,
226 recptprotolistclass *protos, ostream &logout)
227{
228 text_tset metadata;
229 userinfo.clear();
230
231 infodbclass argsinfo;
232 parse_saved_args(historyargs, argsinfo);
233
234 text_t collect = argsinfo["c"];
235 if (collect=="") {
236 userinfo="";
237 return;
238 }
239
240 if (collect != args["c"]) {
241 userinfo += collect+", ";
242 }
243
244 if (argsinfo["h"] != args["h"]) {
245
246 recptproto *collectproto = protos->getrecptproto(collect,logout);
247 if (collectproto == NULL) {
248 userinfo="";
249 return;
250 }
251 metadata.insert(argsinfo["h"]);
252 FilterResponse_t response;
253
254 get_info("collection", collect, metadata, false, collectproto, response, logout);
255 text_t index = response.docInfo[0].metadata[argsinfo["h"]].values[0];
256 if (!index.empty()) {
257 userinfo += index+", ";
258 }
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 }
279 }
280 userinfo += mode+", ";
281 }
282
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+", ";
292 }
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+", ";
303 }
304
305}
Note: See TracBrowser for help on using the repository browser.