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

Last change on this file since 1285 was 1285, checked in by sjboddie, 24 years ago

Removed CVS logging information from source files

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 37.4 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
270}
271
272void queryaction::configure (const text_t &key, const text_tarray &cfgline) {
273 action::configure (key, cfgline);
274}
275
276bool queryaction::init (ostream &logout) {
277 return action::init (logout);
278}
279
280bool queryaction::check_cgiargs (cgiargsinfoclass &argsinfo, cgiargsclass &args,
281 ostream &logout) {
282
283 // check t argument
284 int arg_t = args.getintarg("t");
285 if (arg_t != 0 && arg_t != 1) {
286 logout << "Warning: \"t\" argument out of range (" << arg_t << ")\n";
287 cgiarginfo *tinfo = argsinfo.getarginfo ("t");
288 if (tinfo != NULL) args["t"] = tinfo->argdefault;
289 }
290
291 // check k argument
292 int arg_k = args.getintarg("k");
293 if (arg_k != 0 && arg_k != 1) {
294 logout << "Warning: \"k\" argument out of range (" << arg_k << ")\n";
295 cgiarginfo *kinfo = argsinfo.getarginfo ("k");
296 if (kinfo != NULL) args["k"] = kinfo->argdefault;
297 }
298
299 // check s argument
300 int arg_s = args.getintarg("s");
301 if (arg_s != 0 && arg_s != 1) {
302 logout << "Warning: \"s\" argument out of range (" << arg_s << ")\n";
303 cgiarginfo *sinfo = argsinfo.getarginfo ("s");
304 if (sinfo != NULL) args["s"] = sinfo->argdefault;
305 }
306
307 // check m argument
308 int arg_m = args.getintarg("m");
309 if (arg_m < -1) {
310 logout << "Warning: \"m\" argument less than -1 (" << arg_m << ")\n";
311 cgiarginfo *minfo = argsinfo.getarginfo ("m");
312 if (minfo != NULL) args["m"] = minfo->argdefault;
313 }
314
315 // check o argument
316 int arg_o = args.getintarg("o");
317 if (arg_o < -1) {
318 logout << "Warning: \"o\" argument less than -1 (" << arg_o << ")\n";
319 cgiarginfo *oinfo = argsinfo.getarginfo ("o");
320 if (oinfo != NULL) args["o"] = oinfo->argdefault;
321 }
322
323 // check r argument
324 int arg_r = args.getintarg("r");
325 if (arg_r < 1) {
326 logout << "Warning: \"r\" argument less than 1 (" << arg_r << ")\n";
327 cgiarginfo *rinfo = argsinfo.getarginfo ("r");
328 if (rinfo != NULL) args["r"] = rinfo->argdefault;
329 }
330 //check hd argument
331 int arg_hd = args.getintarg("hd");
332 if (arg_hd !=0 && arg_hd !=1) {
333 logout << "Warning: \"hd\" argument out of range (" << arg_hd << ")\n";
334 cgiarginfo *hdinfo = argsinfo.getarginfo ("hd");
335 if (hdinfo != NULL) args["hd"] = hdinfo->argdefault;
336 }
337 //check hs argument
338 int arg_hs = args.getintarg("hs");
339 if (arg_hs !=0 && arg_hs !=1) {
340 logout << "Warning: \"hs\" argument out of range (" << arg_hs << ")\n";
341 cgiarginfo *hsinfo = argsinfo.getarginfo ("hs");
342 if (hsinfo != NULL) args["hs"] = hsinfo->argdefault;
343 }
344
345 // chech hcl argument
346 int arg_hcl = args.getintarg("hcl");
347 if (arg_hcl !=0 && arg_hcl !=1) {
348 logout << "Warning: \"hcl\" argument out of range (" << arg_hcl << ")\n";
349 cgiarginfo *hclinfo = argsinfo.getarginfo ("hcl");
350 if (hclinfo != NULL) args["hcl"] = hclinfo->argdefault;
351 }
352
353 return true;
354}
355
356void queryaction::get_cgihead_info (cgiargsclass &/*args*/, recptprotolistclass * /*protos*/,
357 response_t &response, text_t &response_data,
358 ostream &/*logout*/) {
359 response = content;
360 response_data = "text/html";
361}
362
363void queryaction::define_internal_macros (displayclass &disp, cgiargsclass &args,
364 recptprotolistclass * /*protos*/,
365 ostream &/*logout*/) {
366
367 // define_internal_macros sets the following macros:
368
369 // _quotedquery_ the part of the query string that was quoted for post-processing
370
371
372
373 // The following macros are set later (in define_query_macros) as they can't be set until
374 // the query has been done.
375
376 // _freqmsg_ the term frequency string
377
378 // _resultline_ the "x documents matched the query" string
379
380 // _prevfirst_ these are used when setting up the links to previous/next
381 // _prevlast_ pages of results (_thisfirst_ and _thislast_ are used to set
382 // _nextfirst_ the 'results x-x for query: xxxx' string in the title bar)
383 // _nextlast_
384 // _thisfirst_
385 // _thislast_
386
387
388 // get the quoted bits of the query string and set _quotedquery_
389 text_tarray phrases;
390 get_phrases (args["q"], phrases);
391 num_phrases = phrases.size();
392 text_tarray::const_iterator phere = phrases.begin();
393 text_tarray::const_iterator pend = phrases.end();
394 bool first = true;
395 text_t quotedquery;
396 while (phere != pend) {
397 if (!first)
398 if ((phere +1) == pend) quotedquery += " and ";
399 else quotedquery += ", ";
400
401 quotedquery += "\"" + *phere + "\"";
402 first = false;
403 phere ++;
404 }
405 if (args.getintarg("s")) quotedquery += "_textstemon_";
406 disp.setmacro ("quotedquery", "query", quotedquery);
407}
408
409// sets the selection box macros _hselection_, _jselection_, and _nselection_.
410void queryaction::set_option_macro (const text_t &macroname, text_t current_value,
411 const FilterOption_t &option, displayclass &disp) {
412
413 if (option.validValues.empty()) return;
414 else if (option.validValues.size() == 1) {
415 disp.setmacro (macroname + "selection", "Global", "_" + option.defaultValue + "_");
416 return;
417 }
418 if (option.validValues.size() < 2) return;
419
420 text_t macrovalue = "<select name=\"" + macroname + "\">\n";
421
422 if (current_value.empty()) current_value = option.defaultValue;
423
424 text_tarray::const_iterator thisvalue = option.validValues.begin();
425 text_tarray::const_iterator endvalue = option.validValues.end();
426
427 while (thisvalue != endvalue) {
428 macrovalue += "<option value=\"" + *thisvalue + "\"";
429 if (*thisvalue == current_value)
430 macrovalue += " selected";
431 macrovalue += ">_" + *thisvalue + "_\n";
432 thisvalue ++;
433 }
434 macrovalue += "</select>\n";
435 disp.setmacro (macroname + "selection", "Global", macrovalue);
436}
437
438void queryaction::define_external_macros (displayclass &disp, cgiargsclass &args,
439 recptprotolistclass *protos, ostream &logout) {
440
441 // define_external_macros sets the following macros:
442
443 // some or all of these may not be required to be set
444 // _hselection_, _h2selection_ the selection box for the main part of the index
445 // _jselection_, _j2selection_ the selection box for the subcollection part of the index
446 // _nselection_, _n2selection_ the selection box for the language part of the index
447 // _cq2selection the selection box for combining two queries
448
449
450 // can't do anything if collectproto is null (i.e. no collection was specified)
451 recptproto *collectproto = protos->getrecptproto (args["c"], logout);
452 if (collectproto == NULL) return;
453
454 comerror_t err;
455 InfoFilterOptionsResponse_t response;
456 InfoFilterOptionsRequest_t request;
457 request.filterName = "QueryFilter";
458
459 collectproto->get_filteroptions (args["c"], request, response, err, logout);
460 if (err == noError) {
461
462 FilterOption_tmap::const_iterator it;
463 FilterOption_tmap::const_iterator end = response.filterOptions.end();
464
465 // _hselection_ and _h2selection_ (Index)
466 it = response.filterOptions.find ("Index");
467 if (it != end) set_option_macro ("h", args["h"], (*it).second, disp);
468 if (it != end) set_option_macro ("h2", args["h2"], (*it).second, disp);
469
470 // _jselection_ and _j2selection_ (Subcollection)
471 it = response.filterOptions.find ("Subcollection");
472 if (it != end) set_option_macro ("j", args["j"], (*it).second, disp);
473 if (it != end) set_option_macro ("j2", args["j2"], (*it).second, disp);
474
475 // _nselection_ and _n2selection_ (Language)
476 it = response.filterOptions.find ("Language");
477 if (it != end) set_option_macro ("n", args["n"], (*it).second, disp);
478 if (it != end) set_option_macro ("n2", args["n2"], (*it).second, disp);
479
480 // _cq2selection_ (CombineQuery)
481 it = response.filterOptions.find ("CombineQuery");
482 if (it != end) set_option_macro ("cq2", args["cq2"], (*it).second, disp);
483
484 // define_history_macros(disp, args, protos, logout);
485 }
486} // define external macros
487
488void queryaction::define_history_macros (displayclass &disp, cgiargsclass &args,
489 recptprotolistclass *protos, ostream &logout) {
490
491 // defines the following macros
492
493 // _searchhistorylist_
494
495 text_t historylist;
496 int arghd = args.getintarg("hd");
497 if (arghd != 1) {
498 historylist="";
499 }
500 else {
501 historylist = "<!-- Search History List -->\n";
502
503 text_t userid = args["z"];
504 text_tarray entries;
505 if (get_history_info (userid, entries, gsdlhome, logout)) {
506 int count = 1;
507 text_tarray::iterator here = entries.begin();
508 text_tarray::iterator end = entries.end();
509 int size=(int)entries.size();
510 if (args["hcl"]==1&&size>5) { // compress the list
511 here = end-5;
512 count=size-4;
513 }
514 historylist += "<table align=center width=500 border=0>\n";
515 historylist += "<tr><td width=300 align=center>";
516 historylist += "<a href=\"_httpclearhistory_\">_textclearhistory_</a></td>\n";
517
518 if (size>5&&args["hcl"]==1) { //compress the list, show the expand button
519
520 historylist += "<td><a href=\"_gwcgi_?e=_compressedoptions_&a=q&hcl=0\">_textexpand_</a></td>\n";
521 }
522 else if (size >5 && args["hcl"]==0) { // expand the list, show contract button
523 historylist += "<td><a href=\"_gwcgi_?e=_compressedoptions_&a=q&hcl=1\">_textcontract_</a></td>\n";
524 }
525 historylist += "</table>\n";
526 historylist += "<table align=center width=500 border=1>\n<tr><th colspan=4 align=center>";
527 historylist += "_textsearchhistory_</th></tr>\n";
528 historylist += "<tr><th width=40>#</th>\n<th width=340>_textquery_</th>\n";
529 historylist += "<th width=60>_textresults_</th><th width=60>_textview_</th></tr>\n";
530
531 while (here !=end ) {
532 text_t c;
533 text_t query;
534 text_t numdocs;
535 text_t cgiargs;
536 text_t userinfo;
537 split_saved_query(*here,c, numdocs, cgiargs);
538 parse_saved_args(cgiargs, "q", query); // get query string out
539 decode_cgi_arg(query); // un cgisafe it
540
541 format_user_info(cgiargs, userinfo, protos, logout);
542
543 historylist += "<tr> <td width=40 align=center>"+c+"</td>\n";
544 historylist += "<td width=340 align=left>"+query+"</td><td width=60 align=center>"+numdocs+"</td>\n";
545 historylist += "<td width=60 align=center><a href=\"_gwcgi_?e=_compressedoptions_&";
546 historylist += *here+"\"><img name=\"display\" src=\"_httpicondisplay_\" width=_widthdisplay_ ";
547 historylist += "height=_heightdisplay_ border=\"0\" alt=\"" + userinfo +"\"></a></td></tr>\n";
548 here++;
549 count++;
550 }
551 historylist+="</table>\n\n";
552
553 } // if
554 else {
555 historylist += "_textnohistory_";
556 }
557 historylist += "<p><! ---- end of history list ----->\n";
558 } // else display list
559 disp.setmacro("searchhistorylist", "query", historylist);
560
561} // define history macros
562
563void queryaction::output_ccp (cgiargsclass &args, recptprotolistclass *protos,
564 displayclass &disp, outconvertclass &outconvert,
565 ostream &textout, ostream &logout) {
566
567 ColInfoResponse_t *cinfo = NULL;
568 comerror_t err;
569 InfoFilterOptionsResponse_t fresponse;
570 InfoFilterOptionsRequest_t frequest;
571 frequest.filterName = "QueryFilter";
572
573 text_t &index = args["h"];
574 text_t &subcollection = args["j"];
575 text_t &language = args["n"];
576
577 text_tset collections;
578 text_t arg_cc = args["cc"];
579 decode_cgi_arg (arg_cc);
580 splitchar (arg_cc.begin(), arg_cc.end(), ',', collections);
581
582 textout << outconvert << disp << "_query:header_\n"
583 << "<center>_navigationbar_</center><br>\n"
584 << "<form name=QueryForm method=get action=\"_gwcgi_\">\n"
585 << "<input type=hidden name=a value=\"q\">\n"
586 << "<input type=hidden name=e value=\"_compressedoptions_\">\n"
587 << "<input type=hidden name=ccp value=\"1\">\n"
588 << "<center><table width=_pagewidth_><tr valign=top>\n"
589 << "<td>Select collections to search for \"" << args["q"]
590 << "\" <i>(index=" << index << " subcollection=" << subcollection
591 << " language=" << language << ")</i></td>\n"
592 << "<td><input type=\"submit\" value=\"_query:textbeginsearch_\"></td>\n"
593 << "</tr></table></center>\n"
594 << "<center><table width=_pagewidth_>\n"
595 << "<tr><td>\n";
596
597 recptprotolistclass::iterator rprotolist_here = protos->begin();
598 recptprotolistclass::iterator rprotolist_end = protos->end();
599 while (rprotolist_here != rprotolist_end) {
600 if ((*rprotolist_here).p != NULL) {
601
602 text_tarray collist;
603 (*rprotolist_here).p->get_collection_list (collist, err, logout);
604 if (err == noError) {
605 text_tarray::iterator collist_here = collist.begin();
606 text_tarray::iterator collist_end = collist.end();
607 while (collist_here != collist_end) {
608
609 cinfo = recpt->get_collectinfo_ptr ((*rprotolist_here).p, *collist_here, logout);
610 // if (err == noError && cinfo.isPublic && (cinfo.buildDate > 0)) {
611 if (cinfo != NULL && (cinfo->buildDate > 0)) {
612
613 (*rprotolist_here).p->get_filteroptions (*collist_here, frequest, fresponse, err, logout);
614 if (err == noError) {
615
616 FilterOption_tmap::const_iterator it;
617 FilterOption_tmap::const_iterator end = fresponse.filterOptions.end();
618 if (!index.empty()) {
619 it = fresponse.filterOptions.find ("Index");
620 if (it == end) {collist_here ++; continue;}
621 text_tarray::const_iterator there = (*it).second.validValues.begin();
622 text_tarray::const_iterator tend = (*it).second.validValues.end();
623 while (there != tend) {
624 if (*there == index) break;
625 there ++;
626 }
627 if (there == tend) {collist_here++; continue;}
628 }
629 if (!subcollection.empty()) {
630 it = fresponse.filterOptions.find ("Subcollection");
631 if (it == end) {collist_here++; continue;}
632 text_tarray::const_iterator there = (*it).second.validValues.begin();
633 text_tarray::const_iterator tend = (*it).second.validValues.end();
634 while (there != tend) {
635 if (*there == subcollection) break;
636 there ++;
637 }
638 if (there == tend) {collist_here++; continue;}
639 }
640 if (!language.empty()) {
641 it = fresponse.filterOptions.find ("Language");
642 if (it == end) {collist_here++; continue;}
643 text_tarray::const_iterator there = (*it).second.validValues.begin();
644 text_tarray::const_iterator tend = (*it).second.validValues.end();
645 while (there != tend) {
646 if (*there == language) break;
647 there ++;
648 }
649 if (there == tend) {collist_here++; continue;}
650 }
651
652 // we've got a matching collection
653 textout << "<input type=checkbox";
654
655 text_tset::const_iterator t = collections.find (*collist_here);
656 if (t != collections.end()) textout << " checked";
657
658 textout << outconvert
659 << " name=cc value=\"" << *collist_here << "\">";
660
661 if (!cinfo->collectionmeta["collectionname"].empty())
662 textout << outconvert << disp << cinfo->collectionmeta["collectionname"];
663 else
664 textout << outconvert << *collist_here;
665
666 textout << "<br>\n";
667
668 }
669 }
670 collist_here ++;
671 }
672 }
673 }
674 rprotolist_here ++;
675 }
676 textout << outconvert << disp
677 << "</td></tr></table></center>\n"
678 << "</form>\n"
679 << "_query:footer_\n";
680
681}
682
683bool queryaction::do_action (cgiargsclass &args, recptprotolistclass *protos,
684 browsermapclass *browsers, displayclass &disp,
685 outconvertclass &outconvert, ostream &textout,
686 ostream &logout) {
687
688 if (recpt == NULL) {
689 logout << "ERROR (queryaction::do_action): This action does not contain information\n"
690 << " about any receptionists. The method set_receptionist was probably\n"
691 << " not called from the module which instantiated this action.\n";
692 return true;
693 }
694
695
696 if (args["ccs"] == "1") {
697 if (!args["cc"].empty()) {
698 // query the selected collections
699 text_t::const_iterator b = args["cc"].begin();
700 text_t::const_iterator e = args["cc"].end();
701 if (findchar (b, e, ',') != e) {
702 if (!search_multiple_collections (args, protos, browsers, disp, outconvert,
703 textout, logout)) return false;
704 return true;
705 } else {
706 if (!search_single_collection (args, args["cc"], protos, browsers, disp,
707 outconvert, textout, logout)) return false;
708 return true;
709 }
710 }
711 }
712
713 // simply query the current collection
714 if (!search_single_collection (args, args["c"], protos, browsers, disp,
715 outconvert, textout, logout)) return false;
716 return true;
717}
718
719bool queryaction::search_multiple_collections (cgiargsclass &args, recptprotolistclass *protos,
720 browsermapclass *browsers, displayclass &disp,
721 outconvertclass &outconvert, ostream &textout,
722 ostream &logout) {
723
724 text_tarray collections;
725
726 text_t arg_cc = args["cc"];
727 decode_cgi_arg (arg_cc);
728 splitchar (arg_cc.begin(), arg_cc.end(), ',', collections);
729
730 if (collections.empty()) {
731 logout << "queryaction::search_multiple_collections: No collections "
732 << "set for doing multiple query - will search current collection\n";
733 textout << outconvert << disp << "_query:textwarningnocollections_\n";
734 return search_single_collection (args, args["c"], protos, browsers, disp,
735 outconvert, textout, logout);
736 }
737
738 // queryaction uses "VList" browser to display results,
739 // a queries clasification is "Search"
740 text_t browsertype = "VList";
741 text_t classification = "Search";
742
743 QueryResult_tset results;
744 map<text_t, colinfo_t, lttext_t> colinfomap;
745
746 ColInfoResponse_t *cinfo = NULL;
747 comerror_t err;
748 FilterRequest_t request;
749 FilterResponse_t response;
750 request.filterResultOptions = FROID | FRmetadata | FRtermFreq | FRranking;
751 text_t formattedstring = args["q"];
752 text_t freqmsg = "_textfreqmsg1_";
753 int numdocs = 0;
754 isapprox isApprox = Exact;
755
756 format_querystring (formattedstring, args.getintarg("b"));
757 set_queryfilter_options (request, formattedstring, args);
758
759 // need to retrieve maxdocs matches for each collection
760 // (will eventually want to tidy this up, do so caching etc.)
761 OptionValue_t option;
762 option.name = "StartResults";
763 option.value = "1";
764 request.filterOptions.push_back (option);
765
766 option.name = "EndResults";
767 option.value = args["m"];
768 request.filterOptions.push_back (option);
769
770 text_tarray::iterator col_here = collections.begin();
771 text_tarray::iterator col_end = collections.end();
772
773 map<text_t, int, lttext_t> termfreqs;
774 while (col_here != col_end) {
775
776 request.fields.erase (request.fields.begin(), request.fields.end());
777 request.getParents = false;
778
779 recptproto *collectproto = protos->getrecptproto (*col_here, logout);
780 if (collectproto == NULL) {
781 logout << outconvert << "queryaction::search_multiple_collections: " << *col_here
782 << " collection has a NULL collectproto, ignoring\n";
783 col_here ++;
784 continue;
785 }
786 cinfo = recpt->get_collectinfo_ptr (collectproto, *col_here, logout);
787 if (cinfo == NULL) {
788 logout << "ERROR (query_action::search_multiple_collections): get_collectinfo_ptr returned NULL\n";
789 col_here ++;
790 continue;
791 }
792
793 browserclass *bptr = browsers->getbrowser (browsertype);
794
795 // get the formatstring if there is one
796 text_t formatstring;
797 if (!get_formatstring (classification, browsertype,
798 cinfo->format, formatstring))
799 formatstring = bptr->get_default_formatstring();
800
801 bptr->load_metadata_defaults (request.fields);
802
803 format_t *formatlistptr = new format_t();
804 parse_formatstring (formatstring, formatlistptr, request.fields, request.getParents);
805
806 colinfo_t thiscolinfo;
807 thiscolinfo.formatlistptr = formatlistptr;
808 thiscolinfo.browserptr = bptr;
809 colinfomap[*col_here] = thiscolinfo;
810
811 // do the query
812 collectproto->filter (*col_here, request, response, err, logout);
813 if (err != noError) {
814 outconvertclass text_t2ascii;
815 logout << text_t2ascii
816 << "queryaction::search_multiple_collections: call to QueryFilter failed "
817 << "for " << *col_here << " collection (" << get_comerror_string (err) << ")\n";
818 return false;
819 }
820
821 if (isApprox == Exact)
822 isApprox = response.isApprox;
823 else if (isApprox == MoreThan)
824 if (response.isApprox == Approximate)
825 isApprox = response.isApprox;
826
827 TermInfo_tarray::const_iterator this_term = response.termInfo.begin();
828 TermInfo_tarray::const_iterator end_term = response.termInfo.end();
829 while (this_term != end_term) {
830 termfreqs[(*this_term).term] += (*this_term).freq;
831 if ((col_here+1) == col_end) {
832 freqmsg += (*this_term).term + ": " + termfreqs[(*this_term).term];
833 if ((this_term+1) != end_term) freqmsg += ", ";
834 }
835 this_term ++;
836 }
837
838 if (response.numDocs > 0) {
839 numdocs += response.numDocs;
840
841 QueryResult_t thisresult;
842 thisresult.collection = *col_here;
843 ResultDocInfo_tarray::iterator doc_here = response.docInfo.begin();
844 ResultDocInfo_tarray::iterator doc_end = response.docInfo.end();
845 while (doc_here != doc_end) {
846 thisresult.doc = *doc_here;
847 results.insert (thisresult);
848 doc_here ++;
849 }
850 }
851 col_here ++;
852 }
853
854 disp.setmacro ("freqmsg", "query", freqmsg);
855
856 text_t resline;
857 if (num_phrases > 0) isApprox = Exact;
858 if (isApprox == Approximate) resline = "_textapprox_";
859 else if (isApprox == MoreThan) resline = "_textmorethan_";
860
861 if (numdocs == 0) resline = "_textnodocs_";
862 else if (numdocs == 1) resline += "_text1doc_";
863 else resline += text_t(numdocs) + " _textlotsdocs_";
864 disp.setmacro("resultline", "query", resline);
865
866 QueryResult_tset::iterator res_here = results.begin();
867 QueryResult_tset::iterator res_end = results.end();
868 text_tset metadata; // empty !!
869 bool getParents = false; // don't care !!
870 recptproto *collectproto = NULL;
871 bool use_table;
872 ResultDocInfo_t thisdoc;
873 format_t *formatlistptr = NULL;
874 browserclass *browserptr = NULL;
875
876 int maxdocs = args.getintarg("m");
877 int firstdoc = args.getintarg("r");
878 int hitsperpage = args.getintarg("o");
879 if (numdocs > maxdocs) numdocs = maxdocs;
880 if (hitsperpage == -1) hitsperpage = numdocs;
881
882 // set up _thisfirst_ and _thislast_ macros
883 disp.setmacro ("thisfirst", "query", firstdoc);
884 int thislast = firstdoc + (hitsperpage - 1);
885 if (thislast > numdocs) thislast = numdocs;
886 disp.setmacro ("thislast", "query", thislast);
887
888 // set up _prevfirst_ and _prevlast_ macros
889 if (firstdoc > 1) {
890 disp.setmacro ("prevlast", "query", firstdoc - 1);
891 int prevfirst = firstdoc - hitsperpage;
892 if (prevfirst < 1) prevfirst = 1;
893 disp.setmacro ("prevfirst", "query", prevfirst);
894 }
895
896 // set up _nextfirst_ and _nextlast_ macros
897 if (thislast < numdocs) {
898 disp.setmacro ("nextfirst", "query", thislast + 1);
899 int nextlast = thislast + hitsperpage;
900 if (nextlast > numdocs) nextlast = numdocs;
901 disp.setmacro ("nextlast", "query", nextlast);
902 }
903
904 textout << outconvert << disp << "_query:header_\n"
905 << "_query:content_";
906
907 int count = 1;
908
909 // output results
910 while (res_here != res_end) {
911 if (count < firstdoc) {count ++; res_here ++; continue;}
912 if (count > thislast) break;
913 formatlistptr = colinfomap[(*res_here).collection].formatlistptr;
914 browserptr = colinfomap[(*res_here).collection].browserptr;
915 thisdoc = (*res_here).doc;
916 use_table = is_table_content (formatlistptr);
917 browserptr->output_section_group (thisdoc, args, (*res_here).collection, 0,
918 formatlistptr, use_table, metadata, getParents,
919 collectproto, disp, outconvert, textout, logout);
920 // textout << outconvert << "(ranking: " << (*res_here).doc.ranking << ")\n";
921 res_here ++;
922 count ++;
923 }
924
925 textout << outconvert << disp << "_query:footer_";
926
927 // clean up the format_t pointers
928 map<text_t, colinfo_t, lttext_t>::iterator here = colinfomap.begin();
929 map<text_t, colinfo_t, lttext_t>::iterator end = colinfomap.end();
930 while (here != end) {
931 delete ((*here).second.formatlistptr);
932 here ++;
933 }
934 return true;
935}
936
937bool queryaction::search_single_collection (cgiargsclass &args, const text_t &collection,
938 recptprotolistclass *protos, browsermapclass *browsers,
939 displayclass &disp, outconvertclass &outconvert,
940 ostream &textout, ostream &logout) {
941
942 recptproto *collectproto = protos->getrecptproto (collection, logout);
943 if (collectproto == NULL) {
944 logout << outconvert << "queryaction::search_single_collection: " << collection
945 << " collection has a NULL collectproto\n";
946 return false;
947 }
948
949 // queryaction uses "VList" browser to display results,
950 // a queries clasification is "Search"
951 text_t browsertype = "VList";
952 text_t classification = "Search";
953
954 comerror_t err;
955 ColInfoResponse_t *cinfo = recpt->get_collectinfo_ptr (collectproto, collection, logout);
956
957 if (cinfo == NULL) {
958 logout << "ERROR (query_action::search_single_collection): get_collectinfo_ptr returned NULL\n";
959 return false;
960 }
961
962 browserclass *bptr = browsers->getbrowser (browsertype);
963
964 // get the formatstring if there is one
965 text_t formatstring;
966 if (!get_formatstring (classification, browsertype,
967 cinfo->format, formatstring))
968 formatstring = bptr->get_default_formatstring();
969
970 FilterRequest_t request;
971 FilterResponse_t response;
972 bptr->set_filter_options (request, args);
973 bptr->load_metadata_defaults (request.fields);
974
975 format_t *formatlistptr = new format_t();
976 parse_formatstring (formatstring, formatlistptr, request.fields, request.getParents);
977
978 // do the query
979 request.filterResultOptions = FROID | FRmetadata | FRtermFreq;
980 text_t formattedstring = args["q"];
981 if (!combine_query (args["z"], formattedstring, gsdlhome)) {
982 args["q"]=formattedstring;
983 }
984 format_querystring (formattedstring, args.getintarg("b"));
985 set_queryfilter_options (request, formattedstring, args);
986 collectproto->filter (collection, request, response, err, logout);
987 if (err != noError) {
988 outconvertclass text_t2ascii;
989 logout << text_t2ascii
990 << "queryaction::search_single_collections: call to QueryFilter failed "
991 << "for " << collection << " collection (" << get_comerror_string (err) << ")\n";
992 return false;
993 }
994
995
996 define_query_macros (args, disp, response);
997
998 // save the query if appropriate
999 if (!save_search_history(args, response))
1000 logout << "save failed";
1001
1002 define_history_macros (disp, args, protos, logout);
1003
1004 textout << outconvert << disp << "_query:header_\n"
1005 << "_query:content_";
1006
1007 // output the results
1008 bool use_table = is_table_content (formatlistptr);
1009 bptr->output_section_group (response, args, collection, 0, formatlistptr,
1010 use_table, request.fields, request.getParents,
1011 collectproto, disp, outconvert, textout, logout);
1012
1013
1014 textout << outconvert << disp << "_query:footer_";
1015
1016 delete (formatlistptr);
1017
1018 return true;
1019}
1020
1021// define_query_macros sets the macros that couldn't be set until the
1022// query had been done. Those macros are _freqmsg_, _quotedquery_,
1023// _resultline_, _nextfirst_, _nextlast_, _prevfirst_, _prevlast_,
1024// _thisfirst_, and _thislast_
1025void queryaction::define_query_macros (cgiargsclass &args, displayclass &disp,
1026 const FilterResponse_t &response) {
1027
1028 // set up _freqmsg_ and _quotedquery_ macros
1029 text_t freqmsg = "_textfreqmsg1_";
1030 TermInfo_tarray::const_iterator this_term = response.termInfo.begin();
1031 TermInfo_tarray::const_iterator end_term = response.termInfo.end();
1032 while (this_term != end_term) {
1033 freqmsg += (*this_term).term + ": " + (*this_term).freq;
1034 if ((this_term + 1) != end_term)
1035 freqmsg += ", ";
1036 this_term ++;
1037 }
1038
1039 disp.setmacro ("freqmsg", "query", freqmsg);
1040
1041
1042 // set up _resultline_ macro
1043 text_t resline;
1044 int maxdocs = args.getintarg("m");
1045 int numdocs = response.numDocs;
1046 if (maxdocs == -1) maxdocs = response.numDocs;
1047 isapprox isApprox = response.isApprox;
1048
1049 // if there were phrases (post-processing) we're not going to include
1050 // those documents that didn't match
1051 if (num_phrases > 0) isApprox = Exact;
1052
1053 if (isApprox == Approximate) resline = "_textapprox_";
1054 else if (isApprox == MoreThan) resline = "_textmorethan_";
1055
1056 if (numdocs == 0) resline = "_textnodocs_";
1057 else if (numdocs == 1) resline += "_text1doc_";
1058 else resline += text_t(numdocs) + " _textlotsdocs_";
1059
1060 disp.setmacro("resultline", "query", resline);
1061
1062 int firstdoc = args.getintarg("r");
1063 int hitsperpage = args.getintarg("o");
1064 if (hitsperpage == -1) hitsperpage = numdocs;
1065
1066 // set up _thisfirst_ and _thislast_ macros
1067 disp.setmacro ("thisfirst", "query", firstdoc);
1068 int thislast = firstdoc + (hitsperpage - 1);
1069 if (thislast > numdocs) thislast = numdocs;
1070 disp.setmacro ("thislast", "query", thislast);
1071
1072 // set up _prevfirst_ and _prevlast_ macros
1073 if (firstdoc > 1) {
1074 disp.setmacro ("prevlast", "query", firstdoc - 1);
1075 int prevfirst = firstdoc - hitsperpage;
1076 if (prevfirst < 1) prevfirst = 1;
1077 disp.setmacro ("prevfirst", "query", prevfirst);
1078 }
1079
1080 // set up _nextfirst_ and _nextlast_ macros
1081 if (thislast < numdocs) {
1082 disp.setmacro ("nextfirst", "query", thislast + 1);
1083 int nextlast = thislast + hitsperpage;
1084 if (nextlast > numdocs) nextlast = numdocs;
1085 disp.setmacro ("nextlast", "query", nextlast);
1086 }
1087}
1088
1089bool queryaction::save_search_history (cgiargsclass &args, const FilterResponse_t &response)
1090{
1091 if (args["q"]=="") return true; // null query, dont save
1092 if (args["hs"]=="0") return true; // only save when submit query
1093
1094 // get userid
1095 text_t userid = args["z"];
1096
1097 // the number of docs goes on the front of the query string
1098 int numdocs= response.numDocs;
1099 text_t query = text_t(numdocs);
1100 if (response.isApprox==MoreThan) { // there were more docs found
1101 query.push_back('+');
1102 }
1103 query += "a=q";
1104 query += "&c="+args["c"];
1105 query += "&h="+args["h"];
1106 query += "&t="+args["t"];
1107 query += "&b="+args["b"];
1108 query += "&j="+args["j"];
1109 query += "&n="+args["n"];
1110 query += "&s="+args["s"];
1111 query += "&k="+args["k"];
1112
1113 text_t qstring = args["q"];
1114 text_t formattedquery =cgi_safe(qstring);
1115 query += "&q="+formattedquery;
1116
1117 if (set_history_info(userid, query, gsdlhome)) return true;
1118 else return false;
1119
1120
1121}
Note: See TracBrowser for help on using the repository browser.