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

Last change on this file since 15432 was 15432, checked in by mdewsnip, 16 years ago

(Adding new DB support) Changing a few "infodbclass.h" to "gdbmclass.h".

  • Property svn:keywords set to Author Date Id Revision
File size: 8.0 KB
RevLine 
[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"
[15432]31#include "gdbmclass.h"
[928]32
[1913]33#define MAX_RECORDS 20
[11751]34#define HIST_SEP ';'
[928]35
36// returns true on success (in which case historyinfo will contain
37// the information for this history)
[963]38bool get_history_info (const text_t &userid, text_tarray &historyinfo,
39 const text_t &gsdlhome, ostream &logout) {
[939]40
[963]41 text_t historyfile = filename_cat(gsdlhome, "etc", "history.db");
[928]42
43 bool result = false;
44 // open the history database
45 gdbmclass historydb;
46
47 if (historydb.opendatabase(historyfile, GDBM_READER, 1000, true)) {
48 // get history list
49 text_t historyresult;
[939]50
[928]51 historydb.getkeydata(userid, historyresult);
52
53 if (historyresult != "") { // there are entries, process them
[939]54
[928]55 splitchar(historyresult.begin(), historyresult.end(), '\n', historyinfo);
56 result = true;
57 }
58 historydb.closedatabase();
59
[939]60 } else {
61 outconvertclass text_t2ascii;
62 logout << text_t2ascii << "couldn't open history database " << historyfile << "\n";
63 }
[928]64 return result;
[939]65}
[928]66
67
68// returns true on success
[1913]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) {
[928]72
[963]73 text_t historyfile = filename_cat(gsdlhome, "etc", "history.db");
[928]74
75 bool result = false;
76 // open the history database
77 gdbmclass historydb;
78
[1913]79 text_t oldhistoryresult;
80 text_t newhistoryresult;
81 int numentries=0;
[928]82
83 if ( !historydb.opendatabase(historyfile, GDBM_READER, 1000, true)) {
84 // not created yet
[1913]85 oldhistoryresult="";
86 if (!display) return true; // dont need to save
[928]87 }
88 else {
89
90 // get history list
[1913]91 if (! historydb.getkeydata(userid, oldhistoryresult)) {
92 oldhistoryresult="";
93 if (!display) return true; // dont need to save
[928]94 }
95 historydb.closedatabase();
96 }
97
[1913]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;
[928]104 }
105
106 // open for writing
107 if (!historydb.opendatabase(historyfile, GDBM_WRCREAT, 1000, true)) return false;
108
[1913]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";
[9620]113 for (int i=0; i<numentries;++i) {
[1913]114 newhistoryresult += entries[i]+"\n";
115 }
116
117 if (historydb.setinfo(userid, newhistoryresult))
118 result=true;
119
120 historydb.closedatabase();
121 return result;
[928]122}
123
124// deletes all a users history
[963]125bool delete_all_history_info (const text_t &userid, const text_t &gsdlhome) {
[939]126
[963]127 text_t historyfile = filename_cat(gsdlhome, "etc", "history.db");
[928]128
129 // open the history database
130 gdbmclass historydb;
131
132 if ( !historydb.opendatabase(historyfile, GDBM_WRITER, 1000, true)) return false;
133
134 historydb.deletekey(userid);
[939]135 historydb.closedatabase();
[928]136 return true;
137
138}
139
140// retrieves the value of one of the arguments
[939]141void parse_saved_args(text_t &args, text_t key, text_t &value)
[928]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;
[11751]155 it=findchar(here, end, HIST_SEP);
[928]156 value = substr(here, it);
157 return;
158 }
159 }
[9620]160 ++here;
[928]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
[11751]172 splitchar(here, end, HIST_SEP, values);
[928]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 }
[9620]190 ++start;
[928]191 }
192}
193
194
[1913]195void split_saved_query(text_t &query, text_t &numdocs, text_t &cgiargs)
[928]196{
197 text_t::iterator begin = query.begin();
198 text_t::iterator end = query.end();
[939]199
[1913]200 while (*begin >='0'&& *begin <='9') { // get the digits for numdocs
[939]201 numdocs.push_back(*begin);
[9620]202 ++begin;
[1913]203 }
204
[928]205 if (*begin == '+') { // get the + if there
206 numdocs.push_back(*begin);
[9620]207 ++begin;
[928]208 }
[1913]209 if (*begin == ':') { // have the old format - previous bit was record number
210 numdocs.clear();
[9620]211 ++begin;
[928]212
[1913]213 while(*begin >='0' && *begin <='9') { // get the digits
214 numdocs.push_back(*begin);
[9620]215 ++begin;
[1913]216 }
217 if (*begin == '+') { // get the + if there
218 numdocs.push_back(*begin);
[9620]219 ++begin;
[1913]220 }
221 }
[928]222 cgiargs += (substr(begin, end)); // put rest of query into cgiargs
223
224}
225
[1913]226void format_user_info (text_t &historyargs, text_t &userinfo,
227 cgiargsclass &args,
228 recptprotolistclass *protos, ostream &logout)
[928]229{
230 text_tset metadata;
[1913]231 userinfo.clear();
232
[928]233 infodbclass argsinfo;
[1913]234 parse_saved_args(historyargs, argsinfo);
[928]235 text_t collect = argsinfo["c"];
236 if (collect=="") {
237 userinfo="";
238 return;
239 }
[11751]240
[1913]241 if (collect != args["c"]) {
242 userinfo += collect+", ";
[928]243 }
244
[1913]245 if (argsinfo["h"] != args["h"]) {
246
247 recptproto *collectproto = protos->getrecptproto(collect,logout);
248 if (collectproto == NULL) {
249 userinfo="";
250 return;
[928]251 }
[1913]252 metadata.insert(argsinfo["h"]);
253 FilterResponse_t response;
[939]254
[7432]255 get_info("collection", collect, args["l"], metadata, false, collectproto, response, logout);
[1913]256 text_t index = response.docInfo[0].metadata[argsinfo["h"]].values[0];
257 if (!index.empty()) {
258 userinfo += index+", ";
[928]259 }
[1913]260 }
261
262 if (argsinfo["b"] != args["b"] || argsinfo["t"] != args["t"]) {
263 if (argsinfo["b"]=="0") { // simple mode
264 if (argsinfo["t"]=="0") {
[11751]265 userinfo += " _texthallwords_, ";
[1913]266 }
[11751]267 else if (argsinfo["t"]=="1"){
268 userinfo += " _texthsomewords_, ";
[1913]269 }
270
271 }
[11751]272 else if (argsinfo["b"] == "1") { // advanced mode
[1913]273 if (argsinfo["t"]=="0") {
[11751]274 userinfo += " _texthboolean_, ";
[1913]275 }
[11751]276 else if (argsinfo["t"]=="1") {
277 userinfo += " _texthranked_, ";
[1913]278 }
[928]279 }
280 }
[939]281
[11751]282 if (argsinfo["k"] != "" && argsinfo["k"] != args["k"]) {
[1913]283 text_t options;
[11751]284 // the text translations are the wrong way round
285 if (argsinfo["k"]=="1") {
286 userinfo += " _texthcaseoff_, ";
[1913]287 }
[11751]288 else if (argsinfo["k"]=="0") {
289 userinfo += " _texthcaseon_, ";
[1913]290 }
[928]291 }
[1913]292
[11751]293 if (argsinfo["s"] != "" && argsinfo["s"] != args["s"]) {
[1913]294 if (argsinfo["s"]=="0") {
[11751]295 userinfo += " _texthstemoff_, ";
[1913]296 }
[11751]297 else if (argsinfo["s"]=="1") {
298 userinfo += " _texthstemon_, ";
[1913]299 }
[928]300 }
301
302}
Note: See TracBrowser for help on using the repository browser.