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

Last change on this file since 1328 was 1328, checked in by kjm18, 24 years ago

added new cgiarg - g for granularity - for mgpp collections. also defines
_gselection_ macro.

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