source: trunk/gsdl/src/recpt/queryaction.cpp@ 1717

Last change on this file since 1717 was 1717, checked in by nzdl, 23 years ago

Fixed a couple of minor bugs in cross-collection searching

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 39.7 KB
Line 
1/**********************************************************************
2 *
3 * queryaction.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 "queryaction.h"
27#include "querytools.h"
28#include "formattools.h"
29#include "cgiutils.h"
30#include "OIDtools.h"
31//#include "infodbclass.h"
32#include "fileutil.h"
33#include "text_t.h"
34#include "historydb.h"
35
36void colinfo_t::clear () {
37 formatlistptr = NULL;
38 browserptr = NULL;
39}
40
41void QueryResult_t::clear() {
42 doc.clear();
43 collection.clear();
44}
45
46queryaction::queryaction () {
47
48 recpt = NULL;
49 num_phrases = 0;
50
51 // this action uses cgi variable "a"
52 cgiarginfo arg_ainfo;
53 arg_ainfo.shortname = "a";
54 arg_ainfo.longname = "action";
55 arg_ainfo.multiplechar = true;
56 arg_ainfo.defaultstatus = cgiarginfo::weak;
57 arg_ainfo.argdefault = "q";
58 arg_ainfo.savedarginfo = cgiarginfo::must;
59 argsinfo.addarginfo (NULL, arg_ainfo);
60
61 // "b" - 0 = simple, 1 = advanced
62 arg_ainfo.shortname = "b";
63 arg_ainfo.longname = "query mode";
64 arg_ainfo.multiplechar = false;
65 arg_ainfo.defaultstatus = cgiarginfo::weak;
66 arg_ainfo.argdefault = "0";
67 arg_ainfo.savedarginfo = cgiarginfo::must;
68 argsinfo.addarginfo (NULL, arg_ainfo);
69
70 // "h"
71 arg_ainfo.shortname = "h";
72 arg_ainfo.longname = "main index";
73 arg_ainfo.multiplechar = true;
74 arg_ainfo.defaultstatus = cgiarginfo::weak;
75 arg_ainfo.argdefault = "";
76 arg_ainfo.savedarginfo = cgiarginfo::must;
77 argsinfo.addarginfo (NULL, arg_ainfo);
78
79 // "h2"
80 arg_ainfo.shortname = "h2";
81 arg_ainfo.longname = "main index for second query";
82 arg_ainfo.multiplechar = true;
83 arg_ainfo.defaultstatus = cgiarginfo::weak;
84 arg_ainfo.argdefault = "";
85 arg_ainfo.savedarginfo = cgiarginfo::must;
86 argsinfo.addarginfo (NULL, arg_ainfo);
87
88 // "j"
89 arg_ainfo.shortname = "j";
90 arg_ainfo.longname = "sub collection index";
91 arg_ainfo.multiplechar = true;
92 arg_ainfo.defaultstatus = cgiarginfo::weak;
93 arg_ainfo.argdefault = "";
94 arg_ainfo.savedarginfo = cgiarginfo::must;
95 argsinfo.addarginfo (NULL, arg_ainfo);
96
97 // "j2"
98 arg_ainfo.shortname = "j2";
99 arg_ainfo.longname = "sub collection index for second query";
100 arg_ainfo.multiplechar = true;
101 arg_ainfo.defaultstatus = cgiarginfo::weak;
102 arg_ainfo.argdefault = "";
103 arg_ainfo.savedarginfo = cgiarginfo::must;
104 argsinfo.addarginfo (NULL, arg_ainfo);
105
106 // "n"
107 arg_ainfo.shortname = "n";
108 arg_ainfo.longname = "language index";
109 arg_ainfo.multiplechar = true;
110 arg_ainfo.defaultstatus = cgiarginfo::weak;
111 arg_ainfo.argdefault = "";
112 arg_ainfo.savedarginfo = cgiarginfo::must;
113 argsinfo.addarginfo (NULL, arg_ainfo);
114
115 // "n2"
116 arg_ainfo.shortname = "n2";
117 arg_ainfo.longname = "language index for second query";
118 arg_ainfo.multiplechar = true;
119 arg_ainfo.defaultstatus = cgiarginfo::weak;
120 arg_ainfo.argdefault = "";
121 arg_ainfo.savedarginfo = cgiarginfo::must;
122 argsinfo.addarginfo (NULL, arg_ainfo);
123
124 // "q"
125 arg_ainfo.shortname = "q";
126 arg_ainfo.longname = "query string";
127 arg_ainfo.multiplechar = true;
128 arg_ainfo.defaultstatus = cgiarginfo::weak;
129 arg_ainfo.argdefault = "";
130 arg_ainfo.savedarginfo = cgiarginfo::must;
131 argsinfo.addarginfo (NULL, arg_ainfo);
132
133 // "q2"
134 arg_ainfo.shortname = "q2";
135 arg_ainfo.longname = "query string for second query";
136 arg_ainfo.multiplechar = true;
137 arg_ainfo.defaultstatus = cgiarginfo::weak;
138 arg_ainfo.argdefault = "";
139 arg_ainfo.savedarginfo = cgiarginfo::must;
140 argsinfo.addarginfo (NULL, arg_ainfo);
141
142 // "cq2" ""=don't combine, "and", "or", "not"
143 arg_ainfo.shortname = "cq2";
144 arg_ainfo.longname = "combine queries";
145 arg_ainfo.multiplechar = true;
146 arg_ainfo.defaultstatus = cgiarginfo::weak;
147 arg_ainfo.argdefault = "";
148 arg_ainfo.savedarginfo = cgiarginfo::must;
149 argsinfo.addarginfo (NULL, arg_ainfo);
150
151 // "t" - 1 = ranked 0 = boolean
152 arg_ainfo.shortname = "t";
153 arg_ainfo.longname = "search type";
154 arg_ainfo.multiplechar = false;
155 arg_ainfo.defaultstatus = cgiarginfo::weak;
156 arg_ainfo.argdefault = "1";
157 arg_ainfo.savedarginfo = cgiarginfo::must;
158 argsinfo.addarginfo (NULL, arg_ainfo);
159
160 // "k"
161 arg_ainfo.shortname = "k";
162 arg_ainfo.longname = "casefolding";
163 arg_ainfo.multiplechar = false;
164 arg_ainfo.defaultstatus = cgiarginfo::weak;
165 arg_ainfo.argdefault = "1";
166 arg_ainfo.savedarginfo = cgiarginfo::must;
167 argsinfo.addarginfo (NULL, arg_ainfo);
168
169 // "s"
170 arg_ainfo.shortname = "s";
171 arg_ainfo.longname = "stemming";
172 arg_ainfo.multiplechar = false;
173 arg_ainfo.defaultstatus = cgiarginfo::weak;
174 arg_ainfo.argdefault ="0";
175 arg_ainfo.savedarginfo = cgiarginfo::must;
176 argsinfo.addarginfo (NULL, arg_ainfo);
177
178 // "m"
179 arg_ainfo.shortname = "m";
180 arg_ainfo.longname = "maximum number of documents";
181 arg_ainfo.multiplechar = true;
182 arg_ainfo.defaultstatus = cgiarginfo::weak;
183 arg_ainfo.argdefault = "50";
184 arg_ainfo.savedarginfo = cgiarginfo::must;
185 argsinfo.addarginfo (NULL, arg_ainfo);
186
187 // "o"
188 arg_ainfo.shortname = "o";
189 arg_ainfo.longname = "hits per page";
190 arg_ainfo.multiplechar = true;
191 arg_ainfo.defaultstatus = cgiarginfo::weak;
192 arg_ainfo.argdefault = "20";
193 arg_ainfo.savedarginfo = cgiarginfo::must;
194 argsinfo.addarginfo (NULL, arg_ainfo);
195
196 // "r"
197 arg_ainfo.shortname = "r";
198 arg_ainfo.longname = "start results from";
199 arg_ainfo.multiplechar = true;
200 arg_ainfo.defaultstatus = cgiarginfo::weak;
201 arg_ainfo.argdefault = "1";
202 arg_ainfo.savedarginfo = cgiarginfo::must;
203 argsinfo.addarginfo (NULL, arg_ainfo);
204
205 // "ccs"
206 arg_ainfo.shortname = "ccs";
207 arg_ainfo.longname = "cross collection searching";
208 arg_ainfo.multiplechar = false;
209 arg_ainfo.defaultstatus = cgiarginfo::weak;
210 arg_ainfo.argdefault = "0";
211 arg_ainfo.savedarginfo = cgiarginfo::must;
212 argsinfo.addarginfo (NULL, arg_ainfo);
213
214 // "ccp"
215 arg_ainfo.shortname = "ccp";
216 arg_ainfo.longname = "cross collection page";
217 arg_ainfo.multiplechar = false;
218 arg_ainfo.defaultstatus = cgiarginfo::weak;
219 arg_ainfo.argdefault = "0";
220 arg_ainfo.savedarginfo = cgiarginfo::must;
221 argsinfo.addarginfo (NULL, arg_ainfo);
222
223 // "cc"
224 arg_ainfo.shortname = "cc";
225 arg_ainfo.longname = "collections to search";
226 arg_ainfo.multiplechar = true;
227 arg_ainfo.multiplevalue = true;
228 arg_ainfo.defaultstatus = cgiarginfo::weak;
229 arg_ainfo.argdefault = "";
230 arg_ainfo.savedarginfo = cgiarginfo::must;
231 argsinfo.addarginfo (NULL, arg_ainfo);
232
233 // "hd" history display - search history only displayed when
234 // this var set.
235 arg_ainfo.shortname = "hd";
236 arg_ainfo.longname = "history display";
237 arg_ainfo.multiplechar = false;
238 arg_ainfo.multiplevalue = false;
239 arg_ainfo.defaultstatus = cgiarginfo::weak;
240 arg_ainfo.argdefault = "0";
241 arg_ainfo.savedarginfo = cgiarginfo::must;
242 argsinfo.addarginfo (NULL, arg_ainfo);
243
244
245 // "hs" save - set to 1 in query form, so only save when submit
246 // query
247 // 0 = no save 1 = save
248 arg_ainfo.shortname = "hs";
249 arg_ainfo.longname = "history save";
250 arg_ainfo.multiplechar = false;
251 arg_ainfo.defaultstatus = cgiarginfo::weak;
252 arg_ainfo.argdefault = "0";
253 arg_ainfo.savedarginfo = cgiarginfo::mustnot;
254 argsinfo.addarginfo (NULL, arg_ainfo);
255
256
257 // "hcl" compress the list (show only the last 5)
258 // 0 = expand, 1 = compress
259
260 arg_ainfo.shortname = "hcl";
261 arg_ainfo.longname = "history compress list";
262 arg_ainfo.multiplechar = false;
263 arg_ainfo.defaultstatus = cgiarginfo::weak;
264 arg_ainfo.argdefault = "1";
265 arg_ainfo.savedarginfo = cgiarginfo::must;
266 argsinfo.addarginfo (NULL, arg_ainfo);
267
268
269 // new arg for granularity, for mgpp collections
270 arg_ainfo.shortname = "g";
271 arg_ainfo.longname = "granularity";
272 arg_ainfo.multiplechar = true;
273 arg_ainfo.defaultstatus = cgiarginfo::weak;
274 arg_ainfo.argdefault = "Document";
275 arg_ainfo.savedarginfo = cgiarginfo::must;
276 argsinfo.addarginfo (NULL, arg_ainfo);
277
278 // "ds" - start date
279 arg_ainfo.shortname = "ds";
280 arg_ainfo.longname = "start date";
281 arg_ainfo.multiplechar = true;
282 arg_ainfo.defaultstatus = cgiarginfo::weak;
283 arg_ainfo.argdefault = "";
284 arg_ainfo.savedarginfo = cgiarginfo::must;
285 argsinfo.addarginfo (NULL, arg_ainfo);
286
287 // "de" - end date
288 arg_ainfo.shortname = "de";
289 arg_ainfo.longname = "end date";
290 arg_ainfo.multiplechar = true;
291 arg_ainfo.defaultstatus = cgiarginfo::weak;
292 arg_ainfo.argdefault = "";
293 arg_ainfo.savedarginfo = cgiarginfo::must;
294 argsinfo.addarginfo (NULL, arg_ainfo);
295
296 // "dsbc" - whether or not start date is prechristian
297 arg_ainfo.shortname = "dsbc";
298 arg_ainfo.longname = "start date bc";
299 arg_ainfo.multiplechar = false;
300 arg_ainfo.defaultstatus = cgiarginfo::weak;
301 arg_ainfo.argdefault = "0";
302 arg_ainfo.savedarginfo = cgiarginfo::must;
303 argsinfo.addarginfo (NULL, arg_ainfo);
304
305 // "debc" - whether or not end date is prechristian
306 arg_ainfo.shortname = "debc";
307 arg_ainfo.longname = "end date bc";
308 arg_ainfo.multiplechar = false;
309 arg_ainfo.defaultstatus = cgiarginfo::weak;
310 arg_ainfo.argdefault = "0";
311 arg_ainfo.savedarginfo = cgiarginfo::must;
312 argsinfo.addarginfo (NULL, arg_ainfo);
313
314}
315
316void queryaction::configure (const text_t &key, const text_tarray &cfgline) {
317 action::configure (key, cfgline);
318}
319
320bool queryaction::init (ostream &logout) {
321 return action::init (logout);
322}
323
324bool queryaction::check_cgiargs (cgiargsinfoclass &argsinfo, cgiargsclass &args,
325 ostream &logout) {
326
327 // check t argument
328 int arg_t = args.getintarg("t");
329 if (arg_t != 0 && arg_t != 1) {
330 logout << "Warning: \"t\" argument out of range (" << arg_t << ")\n";
331 cgiarginfo *tinfo = argsinfo.getarginfo ("t");
332 if (tinfo != NULL) args["t"] = tinfo->argdefault;
333 }
334
335 // check k argument
336 int arg_k = args.getintarg("k");
337 if (arg_k != 0 && arg_k != 1) {
338 logout << "Warning: \"k\" argument out of range (" << arg_k << ")\n";
339 cgiarginfo *kinfo = argsinfo.getarginfo ("k");
340 if (kinfo != NULL) args["k"] = kinfo->argdefault;
341 }
342
343 // check s argument
344 int arg_s = args.getintarg("s");
345 if (arg_s != 0 && arg_s != 1) {
346 logout << "Warning: \"s\" argument out of range (" << arg_s << ")\n";
347 cgiarginfo *sinfo = argsinfo.getarginfo ("s");
348 if (sinfo != NULL) args["s"] = sinfo->argdefault;
349 }
350
351 // check m argument
352 int arg_m = args.getintarg("m");
353 if (arg_m < -1) {
354 logout << "Warning: \"m\" argument less than -1 (" << arg_m << ")\n";
355 cgiarginfo *minfo = argsinfo.getarginfo ("m");
356 if (minfo != NULL) args["m"] = minfo->argdefault;
357 }
358
359 // check o argument
360 int arg_o = args.getintarg("o");
361 if (arg_o < -1) {
362 logout << "Warning: \"o\" argument less than -1 (" << arg_o << ")\n";
363 cgiarginfo *oinfo = argsinfo.getarginfo ("o");
364 if (oinfo != NULL) args["o"] = oinfo->argdefault;
365 }
366
367 // check r argument
368 int arg_r = args.getintarg("r");
369 if (arg_r < 1) {
370 logout << "Warning: \"r\" argument less than 1 (" << arg_r << ")\n";
371 cgiarginfo *rinfo = argsinfo.getarginfo ("r");
372 if (rinfo != NULL) args["r"] = rinfo->argdefault;
373 }
374 //check hd argument
375 int arg_hd = args.getintarg("hd");
376 if (arg_hd !=0 && arg_hd !=1) {
377 logout << "Warning: \"hd\" argument out of range (" << arg_hd << ")\n";
378 cgiarginfo *hdinfo = argsinfo.getarginfo ("hd");
379 if (hdinfo != NULL) args["hd"] = hdinfo->argdefault;
380 }
381 //check hs argument
382 int arg_hs = args.getintarg("hs");
383 if (arg_hs !=0 && arg_hs !=1) {
384 logout << "Warning: \"hs\" argument out of range (" << arg_hs << ")\n";
385 cgiarginfo *hsinfo = argsinfo.getarginfo ("hs");
386 if (hsinfo != NULL) args["hs"] = hsinfo->argdefault;
387 }
388
389 // chech hcl argument
390 int arg_hcl = args.getintarg("hcl");
391 if (arg_hcl !=0 && arg_hcl !=1) {
392 logout << "Warning: \"hcl\" argument out of range (" << arg_hcl << ")\n";
393 cgiarginfo *hclinfo = argsinfo.getarginfo ("hcl");
394 if (hclinfo != NULL) args["hcl"] = hclinfo->argdefault;
395 }
396
397 return true;
398}
399
400void queryaction::get_cgihead_info (cgiargsclass &/*args*/, recptprotolistclass * /*protos*/,
401 response_t &response, text_t &response_data,
402 ostream &/*logout*/) {
403 response = content;
404 response_data = "text/html";
405}
406
407void queryaction::define_internal_macros (displayclass &disp, cgiargsclass &args,
408 recptprotolistclass * /*protos*/,
409 ostream &/*logout*/) {
410
411 // define_internal_macros sets the following macros:
412
413 // _quotedquery_ the part of the query string that was quoted for post-processing
414
415
416
417 // The following macros are set later (in define_query_macros) as they can't be set until
418 // the query has been done.
419
420 // _freqmsg_ the term frequency string
421
422 // _resultline_ the "x documents matched the query" string
423
424 // _prevfirst_ these are used when setting up the links to previous/next
425 // _prevlast_ pages of results (_thisfirst_ and _thislast_ are used to set
426 // _nextfirst_ the 'results x-x for query: xxxx' string in the title bar)
427 // _nextlast_
428 // _thisfirst_
429 // _thislast_
430
431
432 // get the quoted bits of the query string and set _quotedquery_
433 text_tarray phrases;
434 get_phrases (args["q"], phrases);
435 num_phrases = phrases.size();
436 text_tarray::const_iterator phere = phrases.begin();
437 text_tarray::const_iterator pend = phrases.end();
438 bool first = true;
439 text_t quotedquery;
440 while (phere != pend) {
441 if (!first)
442 if ((phere +1) == pend) quotedquery += " and ";
443 else quotedquery += ", ";
444
445 quotedquery += "\"" + *phere + "\"";
446 first = false;
447 phere ++;
448 }
449 if (args.getintarg("s")) quotedquery += "_textstemon_";
450 disp.setmacro ("quotedquery", "query", quotedquery);
451}
452
453// sets the selection box macros _hselection_, _jselection_, and _nselection_.
454void queryaction::set_option_macro (const text_t &macroname, text_t current_value,
455 const FilterOption_t &option, displayclass &disp) {
456
457 if (option.validValues.empty()) return;
458 else if (option.validValues.size() == 1) {
459 disp.setmacro (macroname + "selection", "Global", "_" + option.defaultValue + "_");
460 return;
461 }
462 if (option.validValues.size() < 2) return;
463
464 text_t macrovalue = "<select name=\"" + macroname + "\">\n";
465
466 if (current_value.empty()) current_value = option.defaultValue;
467
468 text_tarray::const_iterator thisvalue = option.validValues.begin();
469 text_tarray::const_iterator endvalue = option.validValues.end();
470
471 while (thisvalue != endvalue) {
472 macrovalue += "<option value=\"" + *thisvalue + "\"";
473 if (*thisvalue == current_value)
474 macrovalue += " selected";
475 macrovalue += ">_" + *thisvalue + "_\n";
476 thisvalue ++;
477 }
478 macrovalue += "</select>\n";
479 disp.setmacro (macroname + "selection", "Global", macrovalue);
480}
481
482void queryaction::define_external_macros (displayclass &disp, cgiargsclass &args,
483 recptprotolistclass *protos, ostream &logout) {
484
485 // define_external_macros sets the following macros:
486
487 // some or all of these may not be required to be set
488 // _hselection_, _h2selection_ the selection box for the main part of the index
489 // _jselection_, _j2selection_ the selection box for the subcollection part of the index
490 // _nselection_, _n2selection_ the selection box for the language part of the index
491 // _cq2selection the selection box for combining two queries
492
493
494 // can't do anything if collectproto is null (i.e. no collection was specified)
495 recptproto *collectproto = protos->getrecptproto (args["c"], logout);
496 if (collectproto == NULL) return;
497
498 comerror_t err;
499 InfoFilterOptionsResponse_t response;
500 InfoFilterOptionsRequest_t request;
501 request.filterName = "QueryFilter";
502
503 collectproto->get_filteroptions (args["c"], request, response, err, logout);
504 if (err == noError) {
505
506 FilterOption_tmap::const_iterator it;
507 FilterOption_tmap::const_iterator end = response.filterOptions.end();
508
509 // _hselection_ and _h2selection_ (Index)
510 it = response.filterOptions.find ("Index");
511 if (it != end) set_option_macro ("h", args["h"], (*it).second, disp);
512 if (it != end) set_option_macro ("h2", args["h2"], (*it).second, disp);
513
514 // _jselection_ and _j2selection_ (Subcollection)
515 it = response.filterOptions.find ("Subcollection");
516 if (it != end) set_option_macro ("j", args["j"], (*it).second, disp);
517 if (it != end) set_option_macro ("j2", args["j2"], (*it).second, disp);
518
519 // _nselection_ and _n2selection_ (Language)
520 it = response.filterOptions.find ("Language");
521 if (it != end) set_option_macro ("n", args["n"], (*it).second, disp);
522 if (it != end) set_option_macro ("n2", args["n2"], (*it).second, disp);
523
524 // _cq2selection_ (CombineQuery)
525 it = response.filterOptions.find ("CombineQuery");
526 if (it != end) set_option_macro ("cq2", args["cq2"], (*it).second, disp);
527
528 // _gselection_ (Level) // for mgpp collections
529 it = response.filterOptions.find("Level");
530 if (it!=end) set_option_macro("g", args["g"], (*it).second, disp);
531
532 // define_history_macros(disp, args, protos, logout);
533 }
534} // define external macros
535
536void queryaction::define_history_macros (displayclass &disp, cgiargsclass &args,
537 recptprotolistclass *protos, ostream &logout) {
538
539 // defines the following macros
540
541 // _searchhistorylist_
542
543 text_t historylist;
544 int arghd = args.getintarg("hd");
545 if (arghd != 1) {
546 historylist="";
547 }
548 else {
549 historylist = "<!-- Search History List -->\n";
550
551 text_t userid = args["z"];
552 text_tarray entries;
553 if (get_history_info (userid, entries, gsdlhome, logout)) {
554 int count = 1;
555 text_tarray::iterator here = entries.begin();
556 text_tarray::iterator end = entries.end();
557 int size=(int)entries.size();
558 if (args["hcl"]==1&&size>5) { // compress the list
559 here = end-5;
560 count=size-4;
561 }
562 historylist += "<table align=center width=500 border=0>\n";
563 historylist += "<tr><td width=300 align=center>";
564 historylist += "<a href=\"_httpclearhistory_\">_textclearhistory_</a></td>\n";
565
566 if (size>5&&args["hcl"]==1) { //compress the list, show the expand button
567
568 historylist += "<td><a href=\"_gwcgi_?e=_compressedoptions_&a=q&hcl=0\">_textexpand_</a></td>\n";
569 }
570 else if (size >5 && args["hcl"]==0) { // expand the list, show contract button
571 historylist += "<td><a href=\"_gwcgi_?e=_compressedoptions_&a=q&hcl=1\">_textcontract_</a></td>\n";
572 }
573 historylist += "</table>\n";
574 historylist += "<table align=center width=500 border=1>\n<tr><th colspan=4 align=center>";
575 historylist += "_textsearchhistory_</th></tr>\n";
576 historylist += "<tr><th width=40>#</th>\n<th width=340>_textquery_</th>\n";
577 historylist += "<th width=60>_textresults_</th><th width=60>_textview_</th></tr>\n";
578
579 while (here !=end ) {
580 text_t c;
581 text_t query;
582 text_t numdocs;
583 text_t cgiargs;
584 text_t userinfo;
585 split_saved_query(*here,c, numdocs, cgiargs);
586 parse_saved_args(cgiargs, "q", query); // get query string out
587 decode_cgi_arg(query); // un cgisafe it
588
589 format_user_info(cgiargs, userinfo, protos, logout);
590
591 historylist += "<tr> <td width=40 align=center>"+c+"</td>\n";
592 historylist += "<td width=340 align=left>"+query+"</td><td width=60 align=center>"+numdocs+"</td>\n";
593 historylist += "<td width=60 align=center><a href=\"_gwcgi_?e=_compressedoptions_&";
594 historylist += *here+"\"><img name=\"display\" src=\"_httpicondisplay_\" width=_widthdisplay_ ";
595 historylist += "height=_heightdisplay_ border=\"0\" alt=\"" + userinfo +"\"></a></td></tr>\n";
596 here++;
597 count++;
598 }
599 historylist+="</table>\n\n";
600
601 } // if
602 else {
603 historylist += "_textnohistory_";
604 }
605 historylist += "<p><! ---- end of history list ----->\n";
606 } // else display list
607 disp.setmacro("searchhistorylist", "query", historylist);
608
609} // define history macros
610
611void queryaction::output_ccp (cgiargsclass &args, recptprotolistclass *protos,
612 displayclass &disp, outconvertclass &outconvert,
613 ostream &textout, ostream &logout) {
614
615 ColInfoResponse_t *cinfo = NULL;
616 comerror_t err;
617 InfoFilterOptionsResponse_t fresponse;
618 InfoFilterOptionsRequest_t frequest;
619 frequest.filterName = "QueryFilter";
620
621 text_t &index = args["h"];
622 text_t &subcollection = args["j"];
623 text_t &language = args["n"];
624
625 text_tset collections;
626 text_t arg_cc = args["cc"];
627 decode_cgi_arg (arg_cc);
628 splitchar (arg_cc.begin(), arg_cc.end(), ',', collections);
629
630 textout << outconvert << disp << "_query:header_\n"
631 << "<center>_navigationbar_</center><br>\n"
632 << "<form name=QueryForm method=get action=\"_gwcgi_\">\n"
633 << "<input type=hidden name=a value=\"q\">\n"
634 << "<input type=hidden name=e value=\"_compressedoptions_\">\n"
635 << "<input type=hidden name=ccp value=\"1\">\n"
636 << "<center><table width=_pagewidth_><tr valign=top>\n"
637 << "<td>Select collections to search for \"" << args["q"]
638 << "\" <i>(index=" << index << " subcollection=" << subcollection
639 << " language=" << language << ")</i></td>\n"
640 << "<td><input type=\"submit\" value=\"_query:textbeginsearch_\"></td>\n"
641 << "</tr></table></center>\n"
642 << "<center><table width=_pagewidth_>\n"
643 << "<tr><td>\n";
644
645 recptprotolistclass::iterator rprotolist_here = protos->begin();
646 recptprotolistclass::iterator rprotolist_end = protos->end();
647 while (rprotolist_here != rprotolist_end) {
648 if ((*rprotolist_here).p != NULL) {
649
650 text_tarray collist;
651 (*rprotolist_here).p->get_collection_list (collist, err, logout);
652 if (err == noError) {
653 text_tarray::iterator collist_here = collist.begin();
654 text_tarray::iterator collist_end = collist.end();
655 while (collist_here != collist_end) {
656
657 cinfo = recpt->get_collectinfo_ptr ((*rprotolist_here).p, *collist_here, logout);
658 // if (err == noError && cinfo.isPublic && (cinfo.buildDate > 0)) {
659 if (cinfo != NULL && (cinfo->buildDate > 0)) {
660
661 (*rprotolist_here).p->get_filteroptions (*collist_here, frequest, fresponse, err, logout);
662 if (err == noError) {
663
664 FilterOption_tmap::const_iterator it;
665 FilterOption_tmap::const_iterator end = fresponse.filterOptions.end();
666 if (!index.empty()) {
667 it = fresponse.filterOptions.find ("Index");
668 if (it == end) {collist_here ++; continue;}
669 text_tarray::const_iterator there = (*it).second.validValues.begin();
670 text_tarray::const_iterator tend = (*it).second.validValues.end();
671 while (there != tend) {
672 if (*there == index) break;
673 there ++;
674 }
675 if (there == tend) {collist_here++; continue;}
676 }
677 if (!subcollection.empty()) {
678 it = fresponse.filterOptions.find ("Subcollection");
679 if (it == end) {collist_here++; continue;}
680 text_tarray::const_iterator there = (*it).second.validValues.begin();
681 text_tarray::const_iterator tend = (*it).second.validValues.end();
682 while (there != tend) {
683 if (*there == subcollection) break;
684 there ++;
685 }
686 if (there == tend) {collist_here++; continue;}
687 }
688 if (!language.empty()) {
689 it = fresponse.filterOptions.find ("Language");
690 if (it == end) {collist_here++; continue;}
691 text_tarray::const_iterator there = (*it).second.validValues.begin();
692 text_tarray::const_iterator tend = (*it).second.validValues.end();
693 while (there != tend) {
694 if (*there == language) break;
695 there ++;
696 }
697 if (there == tend) {collist_here++; continue;}
698 }
699
700 // we've got a matching collection
701 textout << "<input type=checkbox";
702
703 text_tset::const_iterator t = collections.find (*collist_here);
704 if (t != collections.end()) textout << " checked";
705
706 textout << outconvert
707 << " name=cc value=\"" << *collist_here << "\">";
708
709 if (!cinfo->collectionmeta["collectionname"].empty())
710 textout << outconvert << disp << cinfo->collectionmeta["collectionname"];
711 else
712 textout << outconvert << *collist_here;
713
714 textout << "<br>\n";
715
716 }
717 }
718 collist_here ++;
719 }
720 }
721 }
722 rprotolist_here ++;
723 }
724 textout << outconvert << disp
725 << "</td></tr></table></center>\n"
726 << "</form>\n"
727 << "_query:footer_\n";
728
729}
730
731bool queryaction::do_action (cgiargsclass &args, recptprotolistclass *protos,
732 browsermapclass *browsers, displayclass &disp,
733 outconvertclass &outconvert, ostream &textout,
734 ostream &logout) {
735
736 if (recpt == NULL) {
737 logout << "ERROR (queryaction::do_action): This action does not contain information\n"
738 << " about any receptionists. The method set_receptionist was probably\n"
739 << " not called from the module which instantiated this action.\n";
740 return true;
741 }
742
743
744 if (args["ccs"] == "1") {
745 if (!args["cc"].empty()) {
746 // query the selected collections
747 text_t::const_iterator b = args["cc"].begin();
748 text_t::const_iterator e = args["cc"].end();
749 if (findchar (b, e, ',') != e) {
750 if (!search_multiple_collections (args, protos, browsers, disp, outconvert,
751 textout, logout)) return false;
752 return true;
753 } else {
754 if (!search_single_collection (args, args["cc"], protos, browsers, disp,
755 outconvert, textout, logout)) return false;
756 return true;
757 }
758 }
759 }
760
761 // simply query the current collection
762 if (!search_single_collection (args, args["c"], protos, browsers, disp,
763 outconvert, textout, logout)) return false;
764 return true;
765}
766
767bool queryaction::search_multiple_collections (cgiargsclass &args, recptprotolistclass *protos,
768 browsermapclass *browsers, displayclass &disp,
769 outconvertclass &outconvert, ostream &textout,
770 ostream &logout) {
771
772 text_tarray collections;
773
774 text_t arg_cc = args["cc"];
775 decode_cgi_arg (arg_cc);
776 splitchar (arg_cc.begin(), arg_cc.end(), ',', collections);
777
778 if (collections.empty()) {
779 logout << "queryaction::search_multiple_collections: No collections "
780 << "set for doing multiple query - will search current collection\n";
781 textout << outconvert << disp << "_query:textwarningnocollections_\n";
782 return search_single_collection (args, args["c"], protos, browsers, disp,
783 outconvert, textout, logout);
784 }
785
786 // queryaction uses "VList" browser to display results,
787 // a queries clasification is "Search"
788 text_t browsertype = "VList";
789 text_t classification = "Search";
790
791 QueryResult_tset results;
792 map<text_t, colinfo_t, lttext_t> colinfomap;
793
794 ColInfoResponse_t *cinfo = NULL;
795 recptproto *collectproto = NULL;
796 comerror_t err;
797 FilterRequest_t request;
798 FilterResponse_t response;
799 request.filterResultOptions = FROID | FRmetadata | FRtermFreq | FRranking;
800 text_t formattedstring = args["q"];
801 text_t freqmsg = "_textfreqmsg1_";
802 int numdocs = 0;
803 isapprox isApprox = Exact;
804
805 format_querystring (formattedstring, args.getintarg("b"));
806 set_queryfilter_options (request, formattedstring, args);
807
808 // need to retrieve maxdocs matches for each collection
809 // (will eventually want to tidy this up, do so caching etc.)
810 OptionValue_t option;
811 option.name = "StartResults";
812 option.value = "1";
813 request.filterOptions.push_back (option);
814
815 option.name = "EndResults";
816 option.value = args["m"];
817 request.filterOptions.push_back (option);
818
819 text_tarray::iterator col_here = collections.begin();
820 text_tarray::iterator col_end = collections.end();
821
822 map<text_t, int, lttext_t> termfreqs;
823 while (col_here != col_end) {
824
825 request.fields.erase (request.fields.begin(), request.fields.end());
826 request.getParents = false;
827
828 collectproto = protos->getrecptproto (*col_here, logout);
829 if (collectproto == NULL) {
830 logout << outconvert << "queryaction::search_multiple_collections: " << *col_here
831 << " collection has a NULL collectproto, ignoring\n";
832 col_here ++;
833 continue;
834 }
835 cinfo = recpt->get_collectinfo_ptr (collectproto, *col_here, logout);
836 if (cinfo == NULL) {
837 logout << "ERROR (query_action::search_multiple_collections): get_collectinfo_ptr returned NULL\n";
838 col_here ++;
839 continue;
840 }
841
842 browserclass *bptr = browsers->getbrowser (browsertype);
843
844 // get the formatstring if there is one
845 text_t formatstring;
846 if (!get_formatstring (classification, browsertype,
847 cinfo->format, formatstring))
848 formatstring = bptr->get_default_formatstring();
849
850 bptr->load_metadata_defaults (request.fields);
851
852 format_t *formatlistptr = new format_t();
853 parse_formatstring (formatstring, formatlistptr, request.fields, request.getParents);
854
855 colinfo_t thiscolinfo;
856 thiscolinfo.formatlistptr = formatlistptr;
857 thiscolinfo.browserptr = bptr;
858 colinfomap[*col_here] = thiscolinfo;
859
860 // do the query
861 collectproto->filter (*col_here, request, response, err, logout);
862 if (err != noError) {
863 outconvertclass text_t2ascii;
864 logout << text_t2ascii
865 << "queryaction::search_multiple_collections: call to QueryFilter failed "
866 << "for " << *col_here << " collection (" << get_comerror_string (err) << ")\n";
867 return false;
868 }
869
870 if (isApprox == Exact)
871 isApprox = response.isApprox;
872 else if (isApprox == MoreThan)
873 if (response.isApprox == Approximate)
874 isApprox = response.isApprox;
875
876 TermInfo_tarray::const_iterator this_term = response.termInfo.begin();
877 TermInfo_tarray::const_iterator end_term = response.termInfo.end();
878 while (this_term != end_term) {
879 termfreqs[(*this_term).term] += (*this_term).freq;
880 if ((col_here+1) == col_end) {
881 freqmsg += (*this_term).term + ": " + termfreqs[(*this_term).term];
882 if ((this_term+1) != end_term) freqmsg += ", ";
883 }
884 this_term ++;
885 }
886
887 if (response.numDocs > 0) {
888 numdocs += response.numDocs;
889
890 QueryResult_t thisresult;
891 thisresult.collection = *col_here;
892 ResultDocInfo_tarray::iterator doc_here = response.docInfo.begin();
893 ResultDocInfo_tarray::iterator doc_end = response.docInfo.end();
894 while (doc_here != doc_end) {
895 thisresult.doc = *doc_here;
896 results.insert (thisresult);
897 doc_here ++;
898 }
899 }
900 col_here ++;
901 }
902
903 disp.setmacro ("freqmsg", "query", freqmsg);
904
905 int maxdocs = args.getintarg("m");
906 int firstdoc = args.getintarg("r");
907 int hitsperpage = args.getintarg("o");
908 if (num_phrases > 0) isApprox = Exact;
909 if (maxdocs == -1) maxdocs = numdocs;
910 else if (numdocs > maxdocs) {
911 numdocs = maxdocs;
912 isApprox = MoreThan;
913 }
914 if (hitsperpage == -1) hitsperpage = numdocs;
915
916 text_t resline;
917 if (isApprox == Approximate) resline = "_textapprox_";
918 else if (isApprox == MoreThan) resline = "_textmorethan_";
919
920 if (numdocs == 0) resline = "_textnodocs_";
921 else if (numdocs == 1) resline += "_text1doc_";
922 else resline += text_t(numdocs) + " _textlotsdocs_";
923 disp.setmacro("resultline", "query", resline);
924
925 QueryResult_tset::iterator res_here = results.begin();
926 QueryResult_tset::iterator res_end = results.end();
927 text_tset metadata; // empty !!
928 bool getParents = false; // don't care !!
929 bool use_table;
930 ResultDocInfo_t thisdoc;
931 format_t *formatlistptr = NULL;
932 browserclass *browserptr = NULL;
933
934 // set up _thisfirst_ and _thislast_ macros
935 disp.setmacro ("thisfirst", "query", firstdoc);
936 int thislast = firstdoc + (hitsperpage - 1);
937 if (thislast > numdocs) thislast = numdocs;
938 disp.setmacro ("thislast", "query", thislast);
939
940 // set up _prevfirst_ and _prevlast_ macros
941 if (firstdoc > 1) {
942 disp.setmacro ("prevlast", "query", firstdoc - 1);
943 int prevfirst = firstdoc - hitsperpage;
944 if (prevfirst < 1) prevfirst = 1;
945 disp.setmacro ("prevfirst", "query", prevfirst);
946 }
947
948 // set up _nextfirst_ and _nextlast_ macros
949 if (thislast < numdocs) {
950 disp.setmacro ("nextfirst", "query", thislast + 1);
951 int nextlast = thislast + hitsperpage;
952 if (nextlast > numdocs) nextlast = numdocs;
953 disp.setmacro ("nextlast", "query", nextlast);
954 }
955
956 textout << outconvert << disp << "_query:header_\n"
957 << "_query:content_";
958
959 int count = 1;
960
961 // output results
962 while (res_here != res_end) {
963 if (count < firstdoc) {count ++; res_here ++; continue;}
964 if (count > thislast) break;
965 formatlistptr = colinfomap[(*res_here).collection].formatlistptr;
966 browserptr = colinfomap[(*res_here).collection].browserptr;
967 thisdoc = (*res_here).doc;
968 use_table = is_table_content (formatlistptr);
969
970 collectproto = protos->getrecptproto ((*res_here).collection, logout);
971 if (collectproto == NULL) {
972 logout << outconvert << "queryaction::search_multiple_collections: " << (*res_here).collection
973 << " collection has a NULL collectproto, ignoring results\n";
974 res_here ++;
975 continue;
976 }
977
978 browserptr->output_section_group (thisdoc, args, (*res_here).collection, 0,
979 formatlistptr, use_table, metadata, getParents,
980 collectproto, disp, outconvert, textout, logout);
981 // textout << outconvert << "(ranking: " << (*res_here).doc.ranking << ")\n";
982 res_here ++;
983 count ++;
984 }
985
986 textout << outconvert << disp << "_query:footer_";
987
988 // clean up the format_t pointers
989 map<text_t, colinfo_t, lttext_t>::iterator here = colinfomap.begin();
990 map<text_t, colinfo_t, lttext_t>::iterator end = colinfomap.end();
991 while (here != end) {
992 delete ((*here).second.formatlistptr);
993 here ++;
994 }
995 return true;
996}
997
998bool queryaction::search_single_collection (cgiargsclass &args, const text_t &collection,
999 recptprotolistclass *protos, browsermapclass *browsers,
1000 displayclass &disp, outconvertclass &outconvert,
1001 ostream &textout, ostream &logout) {
1002
1003 recptproto *collectproto = protos->getrecptproto (collection, logout);
1004 if (collectproto == NULL) {
1005 logout << outconvert << "queryaction::search_single_collection: " << collection
1006 << " collection has a NULL collectproto\n";
1007 return false;
1008 }
1009
1010 // queryaction uses "VList" browser to display results,
1011 // a queries clasification is "Search"
1012 text_t browsertype = "VList";
1013 text_t classification = "Search";
1014
1015 comerror_t err;
1016 ColInfoResponse_t *cinfo = recpt->get_collectinfo_ptr (collectproto, collection, logout);
1017
1018 if (cinfo == NULL) {
1019 logout << "ERROR (query_action::search_single_collection): get_collectinfo_ptr returned NULL\n";
1020 return false;
1021 }
1022
1023 browserclass *bptr = browsers->getbrowser (browsertype);
1024
1025 // get the formatstring if there is one
1026 text_t formatstring;
1027 if (!get_formatstring (classification, browsertype,
1028 cinfo->format, formatstring))
1029 formatstring = bptr->get_default_formatstring();
1030
1031 FilterRequest_t request;
1032 FilterResponse_t response;
1033 bptr->set_filter_options (request, args);
1034 bptr->load_metadata_defaults (request.fields);
1035
1036 format_t *formatlistptr = new format_t();
1037 parse_formatstring (formatstring, formatlistptr, request.fields, request.getParents);
1038
1039 // do the query
1040 request.filterResultOptions = FROID | FRmetadata | FRtermFreq;
1041 text_t formattedstring = args["q"];
1042 if (!combine_query (args["z"], formattedstring, gsdlhome)) {
1043 args["q"]=formattedstring;
1044 }
1045
1046 format_querystring (formattedstring, args.getintarg("b"));
1047 //add_ands(formattedstring, args.getintarg("t"));
1048 add_dates(formattedstring, args.getintarg("ds"), args.getintarg("de"),
1049 args.getintarg("dsbc"), args.getintarg("debc"));
1050 set_queryfilter_options (request, formattedstring, args);
1051 collectproto->filter (collection, request, response, err, logout);
1052 if (err != noError) {
1053 outconvertclass text_t2ascii;
1054 logout << text_t2ascii
1055 << "queryaction::search_single_collections: call to QueryFilter failed "
1056 << "for " << collection << " collection (" << get_comerror_string (err) << ")\n";
1057 return false;
1058 }
1059
1060
1061 define_query_macros (args, disp, response);
1062
1063 // save the query if appropriate
1064 if (!save_search_history(args, response))
1065 logout << "save failed";
1066
1067 define_history_macros (disp, args, protos, logout);
1068
1069 textout << outconvert << disp << "_query:header_\n"
1070 << "_query:content_";
1071
1072 // output the results
1073 bool use_table = is_table_content (formatlistptr);
1074 bptr->output_section_group (response, args, collection, 0, formatlistptr,
1075 use_table, request.fields, request.getParents,
1076 collectproto, disp, outconvert, textout, logout);
1077
1078
1079 textout << outconvert << disp << "_query:footer_";
1080
1081 delete (formatlistptr);
1082
1083 return true;
1084}
1085
1086// define_query_macros sets the macros that couldn't be set until the
1087// query had been done. Those macros are _freqmsg_, _quotedquery_,
1088// _resultline_, _nextfirst_, _nextlast_, _prevfirst_, _prevlast_,
1089// _thisfirst_, and _thislast_
1090void queryaction::define_query_macros (cgiargsclass &args, displayclass &disp,
1091 const FilterResponse_t &response) {
1092 // set up _freqmsg_ and _quotedquery_ macros
1093 text_t freqmsg = "_textfreqmsg1_";
1094 TermInfo_tarray::const_iterator this_term = response.termInfo.begin();
1095 TermInfo_tarray::const_iterator end_term = response.termInfo.end();
1096 while (this_term != end_term) {
1097 char *term = (*this_term).term.getcstr();
1098 if(term[0]<'0' || term[0] >'9'){
1099 freqmsg += (*this_term).term + ": " + (*this_term).freq;
1100 if ((this_term + 1) != end_term)
1101 freqmsg += ", ";
1102 }
1103 delete term;
1104 this_term ++;
1105 }
1106 disp.setmacro ("freqmsg", "query", freqmsg);
1107
1108 // set up _resultline_ macro
1109 text_t resline;
1110 int maxdocs = args.getintarg("m");
1111 int numdocs = response.numDocs;
1112 isapprox isApprox = response.isApprox;
1113 if (num_phrases > 0) isApprox = Exact;
1114 if (maxdocs == -1) maxdocs = numdocs;
1115 else if (numdocs > maxdocs) {
1116 numdocs = maxdocs;
1117 isApprox = MoreThan;
1118 }
1119
1120 if (isApprox == Approximate) resline = "_textapprox_";
1121 else if (isApprox == MoreThan) resline = "_textmorethan_";
1122
1123 if (numdocs == 0) resline = "_textnodocs_";
1124 else if (numdocs == 1) resline += "_text1doc_";
1125 else resline += text_t(numdocs) + " _textlotsdocs_";
1126
1127 disp.setmacro("resultline", "query", resline);
1128
1129 int firstdoc = args.getintarg("r");
1130 int hitsperpage = args.getintarg("o");
1131 if (hitsperpage == -1) hitsperpage = numdocs;
1132
1133 // set up _thisfirst_ and _thislast_ macros
1134 disp.setmacro ("thisfirst", "query", firstdoc);
1135 int thislast = firstdoc + (hitsperpage - 1);
1136 if (thislast > numdocs) thislast = numdocs;
1137 disp.setmacro ("thislast", "query", thislast);
1138
1139 // set up _prevfirst_ and _prevlast_ macros
1140 if (firstdoc > 1) {
1141 disp.setmacro ("prevlast", "query", firstdoc - 1);
1142 int prevfirst = firstdoc - hitsperpage;
1143 if (prevfirst < 1) prevfirst = 1;
1144 disp.setmacro ("prevfirst", "query", prevfirst);
1145 }
1146
1147 // set up _nextfirst_ and _nextlast_ macros
1148 if (thislast < numdocs) {
1149 disp.setmacro ("nextfirst", "query", thislast + 1);
1150 int nextlast = thislast + hitsperpage;
1151 if (nextlast > numdocs) nextlast = numdocs;
1152 disp.setmacro ("nextlast", "query", nextlast);
1153 }
1154}
1155
1156bool queryaction::save_search_history (cgiargsclass &args, const FilterResponse_t &response)
1157{
1158
1159 if (args["q"]=="") return true; // null query, dont save
1160 if (args["hs"]=="0") return true; // only save when submit query
1161
1162 // get userid
1163 text_t userid = args["z"];
1164
1165 // the number of docs goes on the front of the query string
1166 int numdocs= response.numDocs;
1167 text_t query = text_t(numdocs);
1168 if (response.isApprox==MoreThan) { // there were more docs found
1169 query.push_back('+');
1170 }
1171 query += "a=q";
1172 query += "&c="+args["c"];
1173 query += "&h="+args["h"];
1174 query += "&t="+args["t"];
1175 query += "&b="+args["b"];
1176 query += "&j="+args["j"];
1177 query += "&n="+args["n"];
1178 query += "&s="+args["s"];
1179 query += "&k="+args["k"];
1180
1181 text_t qstring = args["q"];
1182 text_t formattedquery =cgi_safe(qstring);
1183 query += "&q="+formattedquery;
1184
1185 if (set_history_info(userid, query, gsdlhome)) return true;
1186 else return false;
1187
1188
1189}
1190
1191
1192
1193
1194
1195
Note: See TracBrowser for help on using the repository browser.