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

Last change on this file since 2745 was 2745, checked in by kjm18, 23 years ago

changes for gselection for mgpp queries, and some form query parsing changes

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