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 | #define HIST_SEP ';'
|
---|
34 |
|
---|
35 | // returns true on success (in which case historyinfo will contain
|
---|
36 | // the information for this history)
|
---|
37 | bool get_history_info (const text_t &userid, text_tarray &historyinfo,
|
---|
38 | const text_t &gsdlhome, ostream &logout) {
|
---|
39 |
|
---|
40 | text_t historyfile = filename_cat(gsdlhome, "etc", "history.db");
|
---|
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;
|
---|
49 |
|
---|
50 | historydb.getkeydata(userid, historyresult);
|
---|
51 |
|
---|
52 | if (historyresult != "") { // there are entries, process them
|
---|
53 |
|
---|
54 | splitchar(historyresult.begin(), historyresult.end(), '\n', historyinfo);
|
---|
55 | result = true;
|
---|
56 | }
|
---|
57 | historydb.closedatabase();
|
---|
58 |
|
---|
59 | } else {
|
---|
60 | outconvertclass text_t2ascii;
|
---|
61 | logout << text_t2ascii << "couldn't open history database " << historyfile << "\n";
|
---|
62 | }
|
---|
63 | return result;
|
---|
64 | }
|
---|
65 |
|
---|
66 |
|
---|
67 | // returns true on success
|
---|
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) {
|
---|
71 |
|
---|
72 | text_t historyfile = filename_cat(gsdlhome, "etc", "history.db");
|
---|
73 |
|
---|
74 | bool result = false;
|
---|
75 | // open the history database
|
---|
76 | gdbmclass historydb;
|
---|
77 |
|
---|
78 | text_t oldhistoryresult;
|
---|
79 | text_t newhistoryresult;
|
---|
80 | int numentries=0;
|
---|
81 |
|
---|
82 | if ( !historydb.opendatabase(historyfile, GDBM_READER, 1000, true)) {
|
---|
83 | // not created yet
|
---|
84 | oldhistoryresult="";
|
---|
85 | if (!display) return true; // dont need to save
|
---|
86 | }
|
---|
87 | else {
|
---|
88 |
|
---|
89 | // get history list
|
---|
90 | if (! historydb.getkeydata(userid, oldhistoryresult)) {
|
---|
91 | oldhistoryresult="";
|
---|
92 | if (!display) return true; // dont need to save
|
---|
93 | }
|
---|
94 | historydb.closedatabase();
|
---|
95 | }
|
---|
96 |
|
---|
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;
|
---|
103 | }
|
---|
104 |
|
---|
105 | // open for writing
|
---|
106 | if (!historydb.opendatabase(historyfile, GDBM_WRCREAT, 1000, true)) return false;
|
---|
107 |
|
---|
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";
|
---|
112 | for (int i=0; i<numentries;++i) {
|
---|
113 | newhistoryresult += entries[i]+"\n";
|
---|
114 | }
|
---|
115 |
|
---|
116 | if (historydb.setinfo(userid, newhistoryresult))
|
---|
117 | result=true;
|
---|
118 |
|
---|
119 | historydb.closedatabase();
|
---|
120 | return result;
|
---|
121 | }
|
---|
122 |
|
---|
123 | // deletes all a users history
|
---|
124 | bool delete_all_history_info (const text_t &userid, const text_t &gsdlhome) {
|
---|
125 |
|
---|
126 | text_t historyfile = filename_cat(gsdlhome, "etc", "history.db");
|
---|
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);
|
---|
134 | historydb.closedatabase();
|
---|
135 | return true;
|
---|
136 |
|
---|
137 | }
|
---|
138 |
|
---|
139 | // retrieves the value of one of the arguments
|
---|
140 | void parse_saved_args(text_t &args, text_t key, text_t &value)
|
---|
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;
|
---|
154 | it=findchar(here, end, HIST_SEP);
|
---|
155 | value = substr(here, it);
|
---|
156 | return;
|
---|
157 | }
|
---|
158 | }
|
---|
159 | ++here;
|
---|
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 |
|
---|
171 | splitchar(here, end, HIST_SEP, values);
|
---|
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 | }
|
---|
189 | ++start;
|
---|
190 | }
|
---|
191 | }
|
---|
192 |
|
---|
193 |
|
---|
194 | void split_saved_query(text_t &query, text_t &numdocs, text_t &cgiargs)
|
---|
195 | {
|
---|
196 | text_t::iterator begin = query.begin();
|
---|
197 | text_t::iterator end = query.end();
|
---|
198 |
|
---|
199 | while (*begin >='0'&& *begin <='9') { // get the digits for numdocs
|
---|
200 | numdocs.push_back(*begin);
|
---|
201 | ++begin;
|
---|
202 | }
|
---|
203 |
|
---|
204 | if (*begin == '+') { // get the + if there
|
---|
205 | numdocs.push_back(*begin);
|
---|
206 | ++begin;
|
---|
207 | }
|
---|
208 | if (*begin == ':') { // have the old format - previous bit was record number
|
---|
209 | numdocs.clear();
|
---|
210 | ++begin;
|
---|
211 |
|
---|
212 | while(*begin >='0' && *begin <='9') { // get the digits
|
---|
213 | numdocs.push_back(*begin);
|
---|
214 | ++begin;
|
---|
215 | }
|
---|
216 | if (*begin == '+') { // get the + if there
|
---|
217 | numdocs.push_back(*begin);
|
---|
218 | ++begin;
|
---|
219 | }
|
---|
220 | }
|
---|
221 | cgiargs += (substr(begin, end)); // put rest of query into cgiargs
|
---|
222 |
|
---|
223 | }
|
---|
224 |
|
---|
225 | void format_user_info (text_t &historyargs, text_t &userinfo,
|
---|
226 | cgiargsclass &args,
|
---|
227 | recptprotolistclass *protos, ostream &logout)
|
---|
228 | {
|
---|
229 | text_tset metadata;
|
---|
230 | userinfo.clear();
|
---|
231 |
|
---|
232 | infodbclass argsinfo;
|
---|
233 | parse_saved_args(historyargs, argsinfo);
|
---|
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, args["l"], 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 | if (argsinfo["b"]=="0") { // simple mode
|
---|
263 | if (argsinfo["t"]=="0") {
|
---|
264 | userinfo += " _texthallwords_, ";
|
---|
265 | }
|
---|
266 | else if (argsinfo["t"]=="1"){
|
---|
267 | userinfo += " _texthsomewords_, ";
|
---|
268 | }
|
---|
269 |
|
---|
270 | }
|
---|
271 | else if (argsinfo["b"] == "1") { // advanced mode
|
---|
272 | if (argsinfo["t"]=="0") {
|
---|
273 | userinfo += " _texthboolean_, ";
|
---|
274 | }
|
---|
275 | else if (argsinfo["t"]=="1") {
|
---|
276 | userinfo += " _texthranked_, ";
|
---|
277 | }
|
---|
278 | }
|
---|
279 | }
|
---|
280 |
|
---|
281 | if (argsinfo["k"] != "" && argsinfo["k"] != args["k"]) {
|
---|
282 | text_t options;
|
---|
283 | // the text translations are the wrong way round
|
---|
284 | if (argsinfo["k"]=="1") {
|
---|
285 | userinfo += " _texthcaseoff_, ";
|
---|
286 | }
|
---|
287 | else if (argsinfo["k"]=="0") {
|
---|
288 | userinfo += " _texthcaseon_, ";
|
---|
289 | }
|
---|
290 | }
|
---|
291 |
|
---|
292 | if (argsinfo["s"] != "" && argsinfo["s"] != args["s"]) {
|
---|
293 | if (argsinfo["s"]=="0") {
|
---|
294 | userinfo += " _texthstemoff_, ";
|
---|
295 | }
|
---|
296 | else if (argsinfo["s"]=="1") {
|
---|
297 | userinfo += " _texthstemon_, ";
|
---|
298 | }
|
---|
299 | }
|
---|
300 |
|
---|
301 | }
|
---|