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

Last change on this file since 1888 was 1888, checked in by dmm9, 23 years ago

using mgpp's some/all search

  • 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_?_optsite_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_?_optsite_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_?_optsite_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=site value=\"_cgiargsite_\"\n"
635 << "<input type=hidden name=e value=\"_compressedoptions_\">\n"
636 << "<input type=hidden name=ccp value=\"1\">\n"
637 << "<center><table width=_pagewidth_><tr valign=top>\n"
638 << "<td>Select collections to search for \"" << args["q"]
639 << "\" <i>(index=" << index << " subcollection=" << subcollection
640 << " language=" << language << ")</i></td>\n"
641 << "<td><input type=\"submit\" value=\"_query:textbeginsearch_\"></td>\n"
642 << "</tr></table></center>\n"
643 << "<center><table width=_pagewidth_>\n"
644 << "<tr><td>\n";
645
646 recptprotolistclass::iterator rprotolist_here = protos->begin();
647 recptprotolistclass::iterator rprotolist_end = protos->end();
648 while (rprotolist_here != rprotolist_end) {
649 if ((*rprotolist_here).p != NULL) {
650
651 text_tarray collist;
652 (*rprotolist_here).p->get_collection_list (collist, err, logout);
653 if (err == noError) {
654 text_tarray::iterator collist_here = collist.begin();
655 text_tarray::iterator collist_end = collist.end();
656 while (collist_here != collist_end) {
657
658 cinfo = recpt->get_collectinfo_ptr ((*rprotolist_here).p, *collist_here, logout);
659 // if (err == noError && cinfo.isPublic && (cinfo.buildDate > 0)) {
660 if (cinfo != NULL && (cinfo->buildDate > 0)) {
661
662 (*rprotolist_here).p->get_filteroptions (*collist_here, frequest, fresponse, err, logout);
663 if (err == noError) {
664
665 FilterOption_tmap::const_iterator it;
666 FilterOption_tmap::const_iterator end = fresponse.filterOptions.end();
667 if (!index.empty()) {
668 it = fresponse.filterOptions.find ("Index");
669 if (it == end) {collist_here ++; continue;}
670 text_tarray::const_iterator there = (*it).second.validValues.begin();
671 text_tarray::const_iterator tend = (*it).second.validValues.end();
672 while (there != tend) {
673 if (*there == index) break;
674 there ++;
675 }
676 if (there == tend) {collist_here++; continue;}
677 }
678 if (!subcollection.empty()) {
679 it = fresponse.filterOptions.find ("Subcollection");
680 if (it == end) {collist_here++; continue;}
681 text_tarray::const_iterator there = (*it).second.validValues.begin();
682 text_tarray::const_iterator tend = (*it).second.validValues.end();
683 while (there != tend) {
684 if (*there == subcollection) break;
685 there ++;
686 }
687 if (there == tend) {collist_here++; continue;}
688 }
689 if (!language.empty()) {
690 it = fresponse.filterOptions.find ("Language");
691 if (it == end) {collist_here++; continue;}
692 text_tarray::const_iterator there = (*it).second.validValues.begin();
693 text_tarray::const_iterator tend = (*it).second.validValues.end();
694 while (there != tend) {
695 if (*there == language) break;
696 there ++;
697 }
698 if (there == tend) {collist_here++; continue;}
699 }
700
701 // we've got a matching collection
702 textout << "<input type=checkbox";
703
704 text_tset::const_iterator t = collections.find (*collist_here);
705 if (t != collections.end()) textout << " checked";
706
707 textout << outconvert
708 << " name=cc value=\"" << *collist_here << "\">";
709
710 if (!cinfo->collectionmeta["collectionname"].empty())
711 textout << outconvert << disp << cinfo->collectionmeta["collectionname"];
712 else
713 textout << outconvert << *collist_here;
714
715 textout << "<br>\n";
716
717 }
718 }
719 collist_here ++;
720 }
721 }
722 }
723 rprotolist_here ++;
724 }
725 textout << outconvert << disp
726 << "</td></tr></table></center>\n"
727 << "</form>\n"
728 << "_query:footer_\n";
729
730}
731
732bool queryaction::do_action (cgiargsclass &args, recptprotolistclass *protos,
733 browsermapclass *browsers, displayclass &disp,
734 outconvertclass &outconvert, ostream &textout,
735 ostream &logout) {
736
737 if (recpt == NULL) {
738 logout << "ERROR (queryaction::do_action): This action does not contain information\n"
739 << " about any receptionists. The method set_receptionist was probably\n"
740 << " not called from the module which instantiated this action.\n";
741 return true;
742 }
743
744
745 if (args["ccs"] == "1") {
746 if (!args["cc"].empty()) {
747 // query the selected collections
748 text_t::const_iterator b = args["cc"].begin();
749 text_t::const_iterator e = args["cc"].end();
750 if (findchar (b, e, ',') != e) {
751 if (!search_multiple_collections (args, protos, browsers, disp, outconvert,
752 textout, logout)) return false;
753 return true;
754 } else {
755 if (!search_single_collection (args, args["cc"], protos, browsers, disp,
756 outconvert, textout, logout)) return false;
757 return true;
758 }
759 }
760 }
761
762 // simply query the current collection
763 if (!search_single_collection (args, args["c"], protos, browsers, disp,
764 outconvert, textout, logout)) return false;
765 return true;
766}
767
768bool queryaction::search_multiple_collections (cgiargsclass &args, recptprotolistclass *protos,
769 browsermapclass *browsers, displayclass &disp,
770 outconvertclass &outconvert, ostream &textout,
771 ostream &logout) {
772
773 text_tarray collections;
774
775 text_t arg_cc = args["cc"];
776 decode_cgi_arg (arg_cc);
777 splitchar (arg_cc.begin(), arg_cc.end(), ',', collections);
778
779 if (collections.empty()) {
780 logout << "queryaction::search_multiple_collections: No collections "
781 << "set for doing multiple query - will search current collection\n";
782 textout << outconvert << disp << "_query:textwarningnocollections_\n";
783 return search_single_collection (args, args["c"], protos, browsers, disp,
784 outconvert, textout, logout);
785 }
786
787 // queryaction uses "VList" browser to display results,
788 // a queries clasification is "Search"
789 text_t browsertype = "VList";
790 text_t classification = "Search";
791
792 QueryResult_tset results;
793 map<text_t, colinfo_t, lttext_t> colinfomap;
794
795 ColInfoResponse_t *cinfo = NULL;
796 recptproto *collectproto = NULL;
797 comerror_t err;
798 FilterRequest_t request;
799 FilterResponse_t response;
800 request.filterResultOptions = FROID | FRmetadata | FRtermFreq | FRranking;
801 text_t formattedstring = args["q"];
802 text_t freqmsg = "_textfreqmsg1_";
803 int numdocs = 0;
804 isapprox isApprox = Exact;
805
806 format_querystring (formattedstring, args.getintarg("b"));
807 set_queryfilter_options (request, formattedstring, args);
808
809 // need to retrieve maxdocs matches for each collection
810 // (will eventually want to tidy this up, do so caching etc.)
811 OptionValue_t option;
812 option.name = "StartResults";
813 option.value = "1";
814 request.filterOptions.push_back (option);
815
816 option.name = "EndResults";
817 option.value = args["m"];
818 request.filterOptions.push_back (option);
819
820 text_tarray::iterator col_here = collections.begin();
821 text_tarray::iterator col_end = collections.end();
822
823 map<text_t, int, lttext_t> termfreqs;
824 while (col_here != col_end) {
825
826 request.fields.erase (request.fields.begin(), request.fields.end());
827 request.getParents = false;
828
829 collectproto = protos->getrecptproto (*col_here, logout);
830 if (collectproto == NULL) {
831 logout << outconvert << "queryaction::search_multiple_collections: " << *col_here
832 << " collection has a NULL collectproto, ignoring\n";
833 col_here ++;
834 continue;
835 }
836 cinfo = recpt->get_collectinfo_ptr (collectproto, *col_here, logout);
837 if (cinfo == NULL) {
838 logout << "ERROR (query_action::search_multiple_collections): get_collectinfo_ptr returned NULL\n";
839 col_here ++;
840 continue;
841 }
842
843 browserclass *bptr = browsers->getbrowser (browsertype);
844
845 // get the formatstring if there is one
846 text_t formatstring;
847 if (!get_formatstring (classification, browsertype,
848 cinfo->format, formatstring))
849 formatstring = bptr->get_default_formatstring();
850
851 bptr->load_metadata_defaults (request.fields);
852
853 format_t *formatlistptr = new format_t();
854 parse_formatstring (formatstring, formatlistptr, request.fields, request.getParents);
855
856 colinfo_t thiscolinfo;
857 thiscolinfo.formatlistptr = formatlistptr;
858 thiscolinfo.browserptr = bptr;
859 colinfomap[*col_here] = thiscolinfo;
860
861 // do the query
862 collectproto->filter (*col_here, request, response, err, logout);
863 if (err != noError) {
864 outconvertclass text_t2ascii;
865 logout << text_t2ascii
866 << "queryaction::search_multiple_collections: call to QueryFilter failed "
867 << "for " << *col_here << " collection (" << get_comerror_string (err) << ")\n";
868 return false;
869 }
870
871 if (isApprox == Exact)
872 isApprox = response.isApprox;
873 else if (isApprox == MoreThan)
874 if (response.isApprox == Approximate)
875 isApprox = response.isApprox;
876
877 TermInfo_tarray::const_iterator this_term = response.termInfo.begin();
878 TermInfo_tarray::const_iterator end_term = response.termInfo.end();
879 while (this_term != end_term) {
880 termfreqs[(*this_term).term] += (*this_term).freq;
881 if ((col_here+1) == col_end) {
882 freqmsg += (*this_term).term + ": " + termfreqs[(*this_term).term];
883 if ((this_term+1) != end_term) freqmsg += ", ";
884 }
885 this_term ++;
886 }
887
888 if (response.numDocs > 0) {
889 numdocs += response.numDocs;
890
891 QueryResult_t thisresult;
892 thisresult.collection = *col_here;
893 ResultDocInfo_tarray::iterator doc_here = response.docInfo.begin();
894 ResultDocInfo_tarray::iterator doc_end = response.docInfo.end();
895 while (doc_here != doc_end) {
896 thisresult.doc = *doc_here;
897 results.insert (thisresult);
898 doc_here ++;
899 }
900 }
901 col_here ++;
902 }
903
904 disp.setmacro ("freqmsg", "query", freqmsg);
905
906 int maxdocs = args.getintarg("m");
907 int firstdoc = args.getintarg("r");
908 int hitsperpage = args.getintarg("o");
909 if (num_phrases > 0) isApprox = Exact;
910 if (maxdocs == -1) maxdocs = numdocs;
911 else if (numdocs > maxdocs) {
912 numdocs = maxdocs;
913 isApprox = MoreThan;
914 }
915 if (hitsperpage == -1) hitsperpage = numdocs;
916
917 text_t resline;
918 if (isApprox == Approximate) resline = "_textapprox_";
919 else if (isApprox == MoreThan) resline = "_textmorethan_";
920
921 if (numdocs == 0) resline = "_textnodocs_";
922 else if (numdocs == 1) resline += "_text1doc_";
923 else resline += text_t(numdocs) + " _textlotsdocs_";
924 disp.setmacro("resultline", "query", resline);
925
926 QueryResult_tset::iterator res_here = results.begin();
927 QueryResult_tset::iterator res_end = results.end();
928 text_tset metadata; // empty !!
929 bool getParents = false; // don't care !!
930 bool use_table;
931 ResultDocInfo_t thisdoc;
932 format_t *formatlistptr = NULL;
933 browserclass *browserptr = NULL;
934
935 // set up _thisfirst_ and _thislast_ macros
936 disp.setmacro ("thisfirst", "query", firstdoc);
937 int thislast = firstdoc + (hitsperpage - 1);
938 if (thislast > numdocs) thislast = numdocs;
939 disp.setmacro ("thislast", "query", thislast);
940
941 // set up _prevfirst_ and _prevlast_ macros
942 if (firstdoc > 1) {
943 disp.setmacro ("prevlast", "query", firstdoc - 1);
944 int prevfirst = firstdoc - hitsperpage;
945 if (prevfirst < 1) prevfirst = 1;
946 disp.setmacro ("prevfirst", "query", prevfirst);
947 }
948
949 // set up _nextfirst_ and _nextlast_ macros
950 if (thislast < numdocs) {
951 disp.setmacro ("nextfirst", "query", thislast + 1);
952 int nextlast = thislast + hitsperpage;
953 if (nextlast > numdocs) nextlast = numdocs;
954 disp.setmacro ("nextlast", "query", nextlast);
955 }
956
957 textout << outconvert << disp << "_query:header_\n"
958 << "_query:content_";
959
960 int count = 1;
961
962 // output results
963 while (res_here != res_end) {
964 if (count < firstdoc) {count ++; res_here ++; continue;}
965 if (count > thislast) break;
966 formatlistptr = colinfomap[(*res_here).collection].formatlistptr;
967 browserptr = colinfomap[(*res_here).collection].browserptr;
968 thisdoc = (*res_here).doc;
969 use_table = is_table_content (formatlistptr);
970
971 collectproto = protos->getrecptproto ((*res_here).collection, logout);
972 if (collectproto == NULL) {
973 logout << outconvert << "queryaction::search_multiple_collections: " << (*res_here).collection
974 << " collection has a NULL collectproto, ignoring results\n";
975 res_here ++;
976 continue;
977 }
978
979 browserptr->output_section_group (thisdoc, args, (*res_here).collection, 0,
980 formatlistptr, use_table, metadata, getParents,
981 collectproto, disp, outconvert, textout, logout);
982 // textout << outconvert << "(ranking: " << (*res_here).doc.ranking << ")\n";
983 res_here ++;
984 count ++;
985 }
986
987 textout << outconvert << disp << "_query:footer_";
988
989 // clean up the format_t pointers
990 map<text_t, colinfo_t, lttext_t>::iterator here = colinfomap.begin();
991 map<text_t, colinfo_t, lttext_t>::iterator end = colinfomap.end();
992 while (here != end) {
993 delete ((*here).second.formatlistptr);
994 here ++;
995 }
996 return true;
997}
998
999bool queryaction::search_single_collection (cgiargsclass &args, const text_t &collection,
1000 recptprotolistclass *protos, browsermapclass *browsers,
1001 displayclass &disp, outconvertclass &outconvert,
1002 ostream &textout, ostream &logout) {
1003
1004 recptproto *collectproto = protos->getrecptproto (collection, logout);
1005 if (collectproto == NULL) {
1006 logout << outconvert << "queryaction::search_single_collection: " << collection
1007 << " collection has a NULL collectproto\n";
1008 return false;
1009 }
1010
1011 // queryaction uses "VList" browser to display results,
1012 // a queries clasification is "Search"
1013 text_t browsertype = "VList";
1014 text_t classification = "Search";
1015
1016 comerror_t err;
1017 ColInfoResponse_t *cinfo = recpt->get_collectinfo_ptr (collectproto, collection, logout);
1018
1019 if (cinfo == NULL) {
1020 logout << "ERROR (query_action::search_single_collection): get_collectinfo_ptr returned NULL\n";
1021 return false;
1022 }
1023
1024 browserclass *bptr = browsers->getbrowser (browsertype);
1025
1026 // get the formatstring if there is one
1027 text_t formatstring;
1028 if (!get_formatstring (classification, browsertype,
1029 cinfo->format, formatstring))
1030 formatstring = bptr->get_default_formatstring();
1031
1032 FilterRequest_t request;
1033 FilterResponse_t response;
1034 bptr->set_filter_options (request, args);
1035 bptr->load_metadata_defaults (request.fields);
1036
1037 format_t *formatlistptr = new format_t();
1038 parse_formatstring (formatstring, formatlistptr, request.fields, request.getParents);
1039
1040 // do the query
1041 request.filterResultOptions = FROID | FRmetadata | FRtermFreq;
1042 text_t formattedstring = args["q"];
1043 if (!combine_query (args["z"], formattedstring, gsdlhome)) {
1044 args["q"]=formattedstring;
1045 }
1046
1047 format_querystring (formattedstring, args.getintarg("b"));
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.