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

Last change on this file since 3152 was 3152, checked in by jrm21, 22 years ago

Don't set _quotedquery_ (used to display post-processing information) if we aren't using mg. Eg mgpp shouldn't display this.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 48.6 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::must;
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::must;
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::must;
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::must;
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::must;
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::must;
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 if (args["ct"]==0) { // mg queries only, not mgpp
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.empty()) quotedquery += "_textstemon_";
575 disp.setmacro ("quotedquery", "query", quotedquery);
576 }
577
578 define_form_macros(disp, args, protos, logout);
579
580 define_query_interface(disp, args, protos, logout);
581
582}
583
584void queryaction::define_query_interface(displayclass &disp,
585 cgiargsclass &args,
586 recptprotolistclass * protos,
587 ostream &logout){
588 text_t collection = args["c"];
589
590 //check that the protocol is alive
591 recptproto* colproto = protos->getrecptproto (collection, logout);
592 if(colproto == NULL) {
593 logout << "ERROR: Null collection protocol trying to query"
594 << collection.getcstr() << "\n";
595 return;
596 }
597
598 //check the collection is responding/in place
599 ColInfoResponse_t *colinfo = recpt->get_collectinfo_ptr(colproto, collection,
600 logout);
601 if(colinfo == NULL){
602 logout << "ERROR: Null returned for get_collectinfo_ptr on "
603 << collection.getcstr() << "in queryaction::define_query_interface\n";
604 return;
605 }
606
607
608 text_tmap::iterator check = colinfo->format.find("QueryInterface");
609 if(check != colinfo->format.end()){
610 if((*check).second=="DateSearch"){
611 text_t current = "_basicqueryform_ _datesearch_";
612
613 disp.setmacro("queryformcontent","query",current);
614 }
615 }
616}
617
618
619// sets the selection box macros _hselection_, _jselection_, and _nselection_.
620void queryaction::set_option_macro (const text_t &macroname, text_t current_value,
621 const FilterOption_t &option, displayclass &disp) {
622
623 if (option.validValues.empty()) return;
624 else if (option.validValues.size() == 1) {
625 disp.setmacro (macroname + "selection", "Global", "_" + option.defaultValue + "_");
626 return;
627 }
628 if (option.validValues.size() < 2) return;
629
630 text_t macrovalue = "<select name=\"" + macroname + "\">\n";
631
632 if (current_value.empty()) current_value = option.defaultValue;
633
634 text_tarray::const_iterator thisvalue = option.validValues.begin();
635 text_tarray::const_iterator endvalue = option.validValues.end();
636
637 while (thisvalue != endvalue) {
638 macrovalue += "<option value=\"" + *thisvalue + "\"";
639 if (*thisvalue == current_value)
640 macrovalue += " selected";
641 macrovalue += ">_" + *thisvalue + "_\n";
642 thisvalue ++;
643 }
644 macrovalue += "</select>\n";
645 disp.setmacro (macroname + "selection", "Global", macrovalue);
646}
647
648// sets the selection box macro _fqfselection_.
649void queryaction::set_fqfselection_macro (const FilterOption_t &option,
650 displayclass &disp) {
651
652 if (option.validValues.empty()) return;
653 text_t macrovalue = "";
654
655 if (option.validValues.size() == 1) {
656 macrovalue = "_ZZ_";
657 }
658 else {
659 macrovalue += "<select name=\"fqf\" onChange=\"updatefqf();\">\n";
660
661 text_tarray::const_iterator thisvalue = option.validValues.begin();
662 text_tarray::const_iterator endvalue = option.validValues.end();
663
664 while (thisvalue != endvalue) {
665 macrovalue += "<option value=\"" + *thisvalue + "\">_";
666 macrovalue += *thisvalue + "_\n";
667 thisvalue ++;
668 }
669 macrovalue += "</select>\n";
670 }
671 disp.setmacro ("fqfselection", "query", macrovalue);
672
673}
674// sets the selection box macros _gselection_.
675void queryaction::set_gselection_macro (text_t current_value,
676 const FilterOption_t &option,
677 displayclass &disp) {
678
679 if (option.validValues.size() <= 1) {
680 return;
681 }
682
683 text_t macrovalue = "<select name=\"g\">\n";
684
685 if (current_value.empty()) current_value = option.defaultValue;
686
687 text_tarray::const_iterator thisvalue = option.validValues.begin();
688 text_tarray::const_iterator endvalue = option.validValues.end();
689
690 while (thisvalue != endvalue) {
691 macrovalue += "<option value=\"" + *thisvalue + "\"";
692 if (*thisvalue == current_value)
693 macrovalue += " selected";
694 macrovalue += ">_" + *thisvalue + "_\n";
695 thisvalue ++;
696 }
697 macrovalue += "</select>\n";
698 disp.setmacro ("gselection", "Global", macrovalue);
699}
700
701void queryaction::define_external_macros (displayclass &disp, cgiargsclass &args,
702 recptprotolistclass *protos, ostream &logout) {
703
704 // define_external_macros sets the following macros:
705
706 // some or all of these may not be required to be set
707 // _hselection_, _h2selection_ the selection box for the main part of the index
708 // _jselection_, _j2selection_ the selection box for the subcollection part of the index
709 // _nselection_, _n2selection_ the selection box for the language part of the index
710 // _cq2selection the selection box for combining two queries
711
712
713 // can't do anything if collectproto is null (i.e. no collection was specified)
714 recptproto *collectproto = protos->getrecptproto (args["c"], logout);
715 if (collectproto == NULL) return;
716
717 comerror_t err;
718 InfoFilterOptionsResponse_t response;
719 InfoFilterOptionsRequest_t request;
720 request.filterName = "QueryFilter";
721
722 collectproto->get_filteroptions (args["c"], request, response, err, logout);
723 if (err == noError) {
724
725 FilterOption_tmap::const_iterator it;
726 FilterOption_tmap::const_iterator end = response.filterOptions.end();
727
728 // _hselection_ and _h2selection_ (Index)
729 it = response.filterOptions.find ("Index");
730 if (it != end) set_option_macro ("h", args["h"], (*it).second, disp);
731 if (it != end) set_option_macro ("h2", args["h2"], (*it).second, disp);
732
733 // _jselection_ and _j2selection_ (Subcollection)
734 it = response.filterOptions.find ("Subcollection");
735 if (it != end) set_option_macro ("j", args["j"], (*it).second, disp);
736 if (it != end) set_option_macro ("j2", args["j2"], (*it).second, disp);
737
738 // _nselection_ and _n2selection_ (Language)
739 it = response.filterOptions.find ("Language");
740 if (it != end) set_option_macro ("n", args["n"], (*it).second, disp);
741 if (it != end) set_option_macro ("n2", args["n2"], (*it).second, disp);
742
743 // _cq2selection_ (CombineQuery)
744 it = response.filterOptions.find ("CombineQuery");
745 if (it != end) set_option_macro ("cq2", args["cq2"], (*it).second, disp);
746
747 // _gselection_ (Level) // for mgpp collections
748 it = response.filterOptions.find("Level");
749 if (it!=end) set_gselection_macro(args["g"], (*it).second, disp);
750
751 // should probably go into define_internal_macros
752 // _fqfselection_
753 it = response.filterOptions.find("IndexField");
754 if (it!=end) {
755 set_fqfselection_macro((*it).second, disp);
756 }
757 }
758} // define external macros
759
760void queryaction::define_form_macros (displayclass &disp, cgiargsclass &args,
761 recptprotolistclass *protos, ostream &logout) {
762
763 // defines the following macros
764 // _regformlist_
765 // _advformlist_
766
767 if (args["ct"]!="1" || args["qt"]!="1")
768 return; // dont need these macros
769
770 // mgpp & form query only needs the macros defined
771 text_t form = "";
772 int argfqn = args.getintarg("fqn");
773
774 if (args["b"] == "1") { // advanced form
775 form += "<tr>_firstadvformelement_</tr>\n";
776 for (int i=1; i<argfqn; i++) {
777 form += "<tr>_advformelement_</tr>\n";
778 }
779 disp.setmacro("advformlist", "query", form);
780 }
781 else { // simple form
782 for (int i=0; i<argfqn; i++) {
783 form += "<tr>_regformelement_</tr>\n";
784 }
785 disp.setmacro("regformlist", "query", form);
786 }
787
788}
789
790void queryaction::define_history_macros (displayclass &disp, cgiargsclass &args,
791 recptprotolistclass *protos, ostream &logout) {
792
793 // defines the following macros
794
795 // _searchhistorylist_
796
797 text_t historylist;
798 int arghd = args.getintarg("hd");
799 if (arghd == 0) {
800 historylist="";
801 }
802 else {
803 historylist = "<!-- Search History List -->\n";
804
805 text_t userid = args["z"];
806 text_tarray entries;
807 if (get_history_info (userid, entries, gdbmhome, logout)) {
808 int count = 1;
809 text_tarray::iterator here = entries.begin();
810 text_tarray::iterator end = entries.end();
811 int numrecords=(int)entries.size();
812 if (numrecords>arghd) { // only display some of them
813 numrecords = arghd;
814 }
815 historylist += "<form name=\"HistoryForm\"><table width=537>\n";
816
817 for (int i=0; i<numrecords;i++) {
818 text_t query;
819 text_t numdocs;
820 text_t cgiargs;
821 text_t userinfo;
822 text_t escquery;
823 split_saved_query(entries[i],numdocs,cgiargs);
824 parse_saved_args(cgiargs, "q", query); // get query string out
825 decode_cgi_arg(query); // un cgisafe it
826 escquery = escape_quotes(query); // escape the quotes and newlines
827 text_t histvalue = "histvalue";
828 histvalue += i;
829 disp.setmacro(histvalue, "query", escquery);
830 format_user_info(cgiargs, userinfo, args, protos, logout);
831
832 historylist += "<tr><td align=right>_imagehistbutton_(";
833 historylist += i;
834 historylist += ")</td>\n";
835 historylist += "<td><nobr><table border=1 cellspacing=0 ";
836 historylist += "cellpadding=0><tr><td width=365 align=left>"
837 + query
838 + "</td></tr></table></td><td width=110 align=center><small>"
839 + numdocs;
840 if (numdocs == 1) historylist += " _texthresult_";
841 else historylist += " _texthresults_";
842 if (!userinfo.empty()) {
843 historylist += "<br>( "+userinfo+" )</small></td>\n";
844 }
845 }
846 historylist+="</table></form>\n\n";
847
848 } // if get history info
849 else {
850 historylist += "_textnohistory_";
851 }
852 historylist += "<p><! ---- end of history list ----->\n";
853 } // else display list
854 disp.setmacro("searchhistorylist", "query", historylist);
855
856} // define history macros
857
858void queryaction::output_ccp (cgiargsclass &args, recptprotolistclass *protos,
859 displayclass &disp, outconvertclass &outconvert,
860 ostream &textout, ostream &logout) {
861
862 ColInfoResponse_t *cinfo = NULL;
863 comerror_t err;
864 InfoFilterOptionsResponse_t fresponse;
865 InfoFilterOptionsRequest_t frequest;
866 frequest.filterName = "QueryFilter";
867
868 text_t &index = args["h"];
869 text_t &subcollection = args["j"];
870 text_t &language = args["n"];
871
872 text_tset collections;
873 text_t arg_cc = args["cc"];
874 decode_cgi_arg (arg_cc);
875 splitchar (arg_cc.begin(), arg_cc.end(), ',', collections);
876
877 textout << outconvert << disp << "_query:header_\n"
878 << "<center>_navigationbar_</center><br>\n"
879 << "<form name=QueryForm method=get action=\"_gwcgi_\">\n"
880 << "<input type=hidden name=a value=\"q\">\n"
881 << "<input type=hidden name=site value=\"_cgiargsite_\"\n"
882 << "<input type=hidden name=e value=\"_compressedoptions_\">\n"
883 << "<input type=hidden name=ccp value=\"1\">\n"
884 << "<center><table width=_pagewidth_><tr valign=top>\n"
885 << "<td>Select collections to search for \"" << args["q"]
886 << "\" <i>(index=" << index << " subcollection=" << subcollection
887 << " language=" << language << ")</i></td>\n"
888 << "<td><input type=\"submit\" value=\"_query:textbeginsearch_\"></td>\n"
889 << "</tr></table></center>\n"
890 << "<center><table width=_pagewidth_>\n"
891 << "<tr><td>\n";
892
893 recptprotolistclass::iterator rprotolist_here = protos->begin();
894 recptprotolistclass::iterator rprotolist_end = protos->end();
895 while (rprotolist_here != rprotolist_end) {
896 if ((*rprotolist_here).p != NULL) {
897
898 text_tarray collist;
899 (*rprotolist_here).p->get_collection_list (collist, err, logout);
900 if (err == noError) {
901 text_tarray::iterator collist_here = collist.begin();
902 text_tarray::iterator collist_end = collist.end();
903 while (collist_here != collist_end) {
904
905 cinfo = recpt->get_collectinfo_ptr ((*rprotolist_here).p, *collist_here, logout);
906 // if (err == noError && cinfo.isPublic && (cinfo.buildDate > 0)) {
907 if (cinfo != NULL && (cinfo->buildDate > 0)) {
908
909 (*rprotolist_here).p->get_filteroptions (*collist_here, frequest, fresponse, err, logout);
910 if (err == noError) {
911
912 FilterOption_tmap::const_iterator it;
913 FilterOption_tmap::const_iterator end = fresponse.filterOptions.end();
914 if (!index.empty()) {
915 it = fresponse.filterOptions.find ("Index");
916 if (it == end) {collist_here ++; continue;}
917 text_tarray::const_iterator there = (*it).second.validValues.begin();
918 text_tarray::const_iterator tend = (*it).second.validValues.end();
919 while (there != tend) {
920 if (*there == index) break;
921 there ++;
922 }
923 if (there == tend) {collist_here++; continue;}
924 }
925 if (!subcollection.empty()) {
926 it = fresponse.filterOptions.find ("Subcollection");
927 if (it == end) {collist_here++; continue;}
928 text_tarray::const_iterator there = (*it).second.validValues.begin();
929 text_tarray::const_iterator tend = (*it).second.validValues.end();
930 while (there != tend) {
931 if (*there == subcollection) break;
932 there ++;
933 }
934 if (there == tend) {collist_here++; continue;}
935 }
936 if (!language.empty()) {
937 it = fresponse.filterOptions.find ("Language");
938 if (it == end) {collist_here++; continue;}
939 text_tarray::const_iterator there = (*it).second.validValues.begin();
940 text_tarray::const_iterator tend = (*it).second.validValues.end();
941 while (there != tend) {
942 if (*there == language) break;
943 there ++;
944 }
945 if (there == tend) {collist_here++; continue;}
946 }
947
948 // we've got a matching collection
949 textout << "<input type=checkbox";
950
951 text_tset::const_iterator t = collections.find (*collist_here);
952 if (t != collections.end()) textout << " checked";
953
954 textout << outconvert
955 << " name=cc value=\"" << *collist_here << "\">";
956
957 if (!cinfo->collectionmeta["collectionname"].empty())
958 textout << outconvert << disp << cinfo->collectionmeta["collectionname"];
959 else
960 textout << outconvert << *collist_here;
961
962 textout << "<br>\n";
963
964 }
965 }
966 collist_here ++;
967 }
968 }
969 }
970 rprotolist_here ++;
971 }
972 textout << outconvert << disp
973 << "</td></tr></table></center>\n"
974 << "</form>\n"
975 << "_query:footer_\n";
976
977}
978
979bool queryaction::do_action (cgiargsclass &args, recptprotolistclass *protos,
980 browsermapclass *browsers, displayclass &disp,
981 outconvertclass &outconvert, ostream &textout,
982 ostream &logout) {
983
984 if (recpt == NULL) {
985 logout << "ERROR (queryaction::do_action): This action does not contain information\n"
986 << " about any receptionists. The method set_receptionist was probably\n"
987 << " not called from the module which instantiated this action.\n";
988 return true;
989 }
990
991
992
993 if (args["ccs"] == "1") {
994 if (!args["cc"].empty()) {
995 // query the selected collections
996 text_t::const_iterator b = args["cc"].begin();
997 text_t::const_iterator e = args["cc"].end();
998 if (findchar (b, e, ',') != e) {
999 if (!search_multiple_collections (args, protos, browsers, disp, outconvert,
1000 textout, logout)) return false;
1001 return true;
1002 } else {
1003 if (!search_single_collection (args, args["cc"], protos, browsers, disp,
1004 outconvert, textout, logout)) return false;
1005 return true;
1006 }
1007 }
1008 }
1009
1010 // simply query the current collection
1011 if (!search_single_collection (args, args["c"], protos, browsers, disp,
1012 outconvert, textout, logout)) return false;
1013 return true;
1014}
1015
1016bool queryaction::search_multiple_collections (cgiargsclass &args, recptprotolistclass *protos,
1017 browsermapclass *browsers, displayclass &disp,
1018 outconvertclass &outconvert, ostream &textout,
1019 ostream &logout) {
1020
1021 text_tarray collections;
1022
1023 text_t arg_cc = args["cc"];
1024 decode_cgi_arg (arg_cc);
1025 splitchar (arg_cc.begin(), arg_cc.end(), ',', collections);
1026
1027 if (collections.empty()) {
1028 logout << "queryaction::search_multiple_collections: No collections "
1029 << "set for doing multiple query - will search current collection\n";
1030 textout << outconvert << disp << "_query:textwarningnocollections_\n";
1031 return search_single_collection (args, args["c"], protos, browsers, disp,
1032 outconvert, textout, logout);
1033 }
1034
1035 // queryaction uses "VList" browser to display results,
1036 // a queries clasification is "Search"
1037 text_t browsertype = "VList";
1038 text_t classification = "Search";
1039
1040 QueryResult_tset results;
1041 map<text_t, colinfo_t, lttext_t> colinfomap;
1042
1043 ColInfoResponse_t *cinfo = NULL;
1044 recptproto *collectproto = NULL;
1045 comerror_t err;
1046 FilterRequest_t request;
1047 FilterResponse_t response;
1048 request.filterResultOptions = FROID | FRmetadata | FRtermFreq | FRranking;
1049 text_t formattedstring = args["q"];
1050 text_t freqmsg = "_textfreqmsg1_";
1051 int numdocs = 0;
1052 isapprox isApprox = Exact;
1053
1054 format_querystring (formattedstring, args.getintarg("b"));
1055 set_queryfilter_options (request, formattedstring, args);
1056
1057 // need to retrieve maxdocs matches for each collection
1058 // (will eventually want to tidy this up, do so caching etc.)
1059 OptionValue_t option;
1060 option.name = "StartResults";
1061 option.value = "1";
1062 request.filterOptions.push_back (option);
1063
1064 option.name = "EndResults";
1065 option.value = args["m"];
1066 request.filterOptions.push_back (option);
1067
1068 text_tarray::iterator col_here = collections.begin();
1069 text_tarray::iterator col_end = collections.end();
1070
1071 map<text_t, int, lttext_t> termfreqs;
1072 while (col_here != col_end) {
1073
1074 request.fields.erase (request.fields.begin(), request.fields.end());
1075 request.getParents = false;
1076
1077 collectproto = protos->getrecptproto (*col_here, logout);
1078 if (collectproto == NULL) {
1079 logout << outconvert << "queryaction::search_multiple_collections: " << *col_here
1080 << " collection has a NULL collectproto, ignoring\n";
1081 col_here ++;
1082 continue;
1083 }
1084 cinfo = recpt->get_collectinfo_ptr (collectproto, *col_here, logout);
1085 if (cinfo == NULL) {
1086 logout << "ERROR (query_action::search_multiple_collections): get_collectinfo_ptr returned NULL\n";
1087 col_here ++;
1088 continue;
1089 }
1090
1091 browserclass *bptr = browsers->getbrowser (browsertype);
1092
1093 // get the formatstring if there is one
1094 text_t formatstring;
1095 if (!get_formatstring (classification, browsertype,
1096 cinfo->format, formatstring))
1097 formatstring = bptr->get_default_formatstring();
1098
1099 bptr->load_metadata_defaults (request.fields);
1100
1101 format_t *formatlistptr = new format_t();
1102 parse_formatstring (formatstring, formatlistptr, request.fields, request.getParents);
1103
1104 colinfo_t thiscolinfo;
1105 thiscolinfo.formatlistptr = formatlistptr;
1106 thiscolinfo.browserptr = bptr;
1107 colinfomap[*col_here] = thiscolinfo;
1108
1109 // do the query
1110 collectproto->filter (*col_here, request, response, err, logout);
1111 if (err != noError) {
1112 outconvertclass text_t2ascii;
1113 logout << text_t2ascii
1114 << "queryaction::search_multiple_collections: call to QueryFilter failed "
1115 << "for " << *col_here << " collection (" << get_comerror_string (err) << ")\n";
1116 return false;
1117 }
1118
1119 if (isApprox == Exact)
1120 isApprox = response.isApprox;
1121 else if (isApprox == MoreThan)
1122 if (response.isApprox == Approximate)
1123 isApprox = response.isApprox;
1124
1125 TermInfo_tarray::const_iterator this_term = response.termInfo.begin();
1126 TermInfo_tarray::const_iterator end_term = response.termInfo.end();
1127 while (this_term != end_term) {
1128 termfreqs[(*this_term).term] += (*this_term).freq;
1129 if ((col_here+1) == col_end) {
1130 freqmsg += (*this_term).term + ": " + termfreqs[(*this_term).term];
1131 if ((this_term+1) != end_term) freqmsg += ", ";
1132 }
1133 this_term ++;
1134 }
1135
1136 if (response.numDocs > 0) {
1137 numdocs += response.numDocs;
1138
1139 QueryResult_t thisresult;
1140 thisresult.collection = *col_here;
1141 ResultDocInfo_tarray::iterator doc_here = response.docInfo.begin();
1142 ResultDocInfo_tarray::iterator doc_end = response.docInfo.end();
1143 while (doc_here != doc_end) {
1144 thisresult.doc = *doc_here;
1145 results.insert (thisresult);
1146 doc_here ++;
1147 }
1148 }
1149 col_here ++;
1150 }
1151
1152 disp.setmacro ("freqmsg", "query", freqmsg);
1153
1154 int maxdocs = args.getintarg("m");
1155 int firstdoc = args.getintarg("r");
1156 int hitsperpage = args.getintarg("o");
1157 if (num_phrases > 0) isApprox = Exact;
1158 if (maxdocs == -1) maxdocs = numdocs;
1159 else if (numdocs > maxdocs) {
1160 numdocs = maxdocs;
1161 isApprox = MoreThan;
1162 }
1163 if (hitsperpage == -1) hitsperpage = numdocs;
1164
1165 text_t resline;
1166 if (isApprox == Approximate) resline = "_textapprox_";
1167 else if (isApprox == MoreThan) resline = "_textmorethan_";
1168
1169 if (numdocs == 0) resline = "_textnodocs_";
1170 else if (numdocs == 1) resline += "_text1doc_";
1171 else resline += text_t(numdocs) + " _textlotsdocs_";
1172 disp.setmacro("resultline", "query", resline);
1173
1174 QueryResult_tset::iterator res_here = results.begin();
1175 QueryResult_tset::iterator res_end = results.end();
1176 text_tset metadata; // empty !!
1177 bool getParents = false; // don't care !!
1178 bool use_table;
1179 ResultDocInfo_t thisdoc;
1180 format_t *formatlistptr = NULL;
1181 browserclass *browserptr = NULL;
1182
1183 // set up _thisfirst_ and _thislast_ macros
1184 disp.setmacro ("thisfirst", "query", firstdoc);
1185 int thislast = firstdoc + (hitsperpage - 1);
1186 if (thislast > numdocs) thislast = numdocs;
1187 disp.setmacro ("thislast", "query", thislast);
1188
1189 // set up _prevfirst_ and _prevlast_ macros
1190 if (firstdoc > 1) {
1191 disp.setmacro ("prevlast", "query", firstdoc - 1);
1192 int prevfirst = firstdoc - hitsperpage;
1193 if (prevfirst < 1) prevfirst = 1;
1194 disp.setmacro ("prevfirst", "query", prevfirst);
1195 }
1196
1197 // set up _nextfirst_ and _nextlast_ macros
1198 if (thislast < numdocs) {
1199 disp.setmacro ("nextfirst", "query", thislast + 1);
1200 int nextlast = thislast + hitsperpage;
1201 if (nextlast > numdocs) nextlast = numdocs;
1202 disp.setmacro ("nextlast", "query", nextlast);
1203 }
1204
1205 textout << outconvert << disp << "_query:header_\n"
1206 << "_query:content_";
1207
1208 int count = 1;
1209
1210 // output results
1211 while (res_here != res_end) {
1212 if (count < firstdoc) {count ++; res_here ++; continue;}
1213 if (count > thislast) break;
1214 formatlistptr = colinfomap[(*res_here).collection].formatlistptr;
1215 browserptr = colinfomap[(*res_here).collection].browserptr;
1216 thisdoc = (*res_here).doc;
1217 use_table = is_table_content (formatlistptr);
1218
1219 collectproto = protos->getrecptproto ((*res_here).collection, logout);
1220 if (collectproto == NULL) {
1221 logout << outconvert << "queryaction::search_multiple_collections: " << (*res_here).collection
1222 << " collection has a NULL collectproto, ignoring results\n";
1223 res_here ++;
1224 continue;
1225 }
1226
1227 browserptr->output_section_group (thisdoc, args, (*res_here).collection, 0,
1228 formatlistptr, use_table, metadata, getParents,
1229 collectproto, disp, outconvert, textout, logout);
1230 // textout << outconvert << "(ranking: " << (*res_here).doc.ranking << ")\n";
1231 res_here ++;
1232 count ++;
1233 }
1234
1235 textout << outconvert << disp << "_query:footer_";
1236
1237 // clean up the format_t pointers
1238 map<text_t, colinfo_t, lttext_t>::iterator here = colinfomap.begin();
1239 map<text_t, colinfo_t, lttext_t>::iterator end = colinfomap.end();
1240 while (here != end) {
1241 delete ((*here).second.formatlistptr);
1242 here ++;
1243 }
1244 return true;
1245}
1246
1247bool queryaction::search_single_collection (cgiargsclass &args, const text_t &collection,
1248 recptprotolistclass *protos, browsermapclass *browsers,
1249 displayclass &disp, outconvertclass &outconvert,
1250 ostream &textout, ostream &logout) {
1251
1252 recptproto *collectproto = protos->getrecptproto (collection, logout);
1253 if (collectproto == NULL) {
1254 logout << outconvert << "queryaction::search_single_collection: " << collection
1255 << " collection has a NULL collectproto\n";
1256 return false;
1257 }
1258
1259 // queryaction uses "VList" browser to display results,
1260 // a queries clasification is "Search"
1261 text_t browsertype = "VList";
1262 text_t classification = "Search";
1263
1264 comerror_t err;
1265 ColInfoResponse_t *cinfo = recpt->get_collectinfo_ptr (collectproto, collection, logout);
1266
1267 if (cinfo == NULL) {
1268 logout << "ERROR (query_action::search_single_collection): get_collectinfo_ptr returned NULL\n";
1269 return false;
1270 }
1271
1272 browserclass *bptr = browsers->getbrowser (browsertype);
1273
1274 // get the formatstring if there is one
1275 text_t formatstring;
1276 if (!get_formatstring (classification, browsertype,
1277 cinfo->format, formatstring))
1278 formatstring = bptr->get_default_formatstring();
1279
1280 FilterRequest_t request;
1281 FilterResponse_t response;
1282 bptr->set_filter_options (request, args);
1283 bptr->load_metadata_defaults (request.fields);
1284
1285 format_t *formatlistptr = new format_t();
1286 parse_formatstring (formatstring, formatlistptr, request.fields, request.getParents);
1287
1288 // do the query
1289 request.filterResultOptions = FROID | FRmetadata | FRtermFreq;
1290 text_t formattedstring = "";
1291
1292 if (args["qt"]=="0") { // normal text search
1293 formattedstring = args["q"];
1294 format_querystring (formattedstring, args.getintarg("b"));
1295 add_dates(formattedstring, args.getintarg("ds"), args.getintarg("de"),
1296 args.getintarg("dsbc"), args.getintarg("debc"));
1297 }
1298 else if (args["qt"]=="1" ){ // form search
1299
1300 if (args["b"]=="1" && args["fqa"]=="1") { // explicit query
1301 formattedstring = args["q"];
1302 }
1303 else { // form search
1304 if (args["b"]=="0") { // regular form
1305 parse_reg_query_form(formattedstring, args);
1306 }
1307 else { // advanced form
1308 parse_adv_query_form(formattedstring, args);
1309 }
1310 args["q"] = formattedstring;
1311
1312 // also reset the _cgiargq_ macro as it has changed now
1313 disp.setmacro("cgiargq", "Global", html_safe(args["q"]));
1314
1315 // reset the compressed options to include the q arg
1316 text_t compressedoptions = recpt->get_compressed_arg(args, logout);
1317 if (!compressedoptions.empty()) {
1318 disp.setmacro ("compressedoptions", "Global", dm_safe(compressedoptions));
1319 // need a decoded version of compressedoptions for use within forms
1320 // as browsers encode values from forms before sending to server
1321 // (e.g. %25 becomes %2525)
1322 decode_cgi_arg (compressedoptions);
1323 disp.setmacro ("decodedcompressedoptions", "Global", dm_safe(compressedoptions));
1324 }
1325 } // form search
1326 } // args["qt"]=1
1327 else {
1328 logout << "ERROR (query_action::search_single_collection): querytype not defined\n";
1329 return false;
1330 }
1331
1332 if (!formattedstring.empty()) { // do the query
1333 // note! formattedstring is in unicode! mg and mgpp must convert!
1334 set_queryfilter_options (request, formattedstring, args);
1335 collectproto->filter (collection, request, response, err, logout);
1336 if (err != noError) {
1337 outconvertclass text_t2ascii;
1338 logout << text_t2ascii
1339 << "queryaction::search_single_collections: call to QueryFilter failed "
1340 << "for " << collection << " collection (" << get_comerror_string (err) << ")\n";
1341 return false;
1342 }
1343
1344 define_query_macros (args, disp, response);
1345
1346 // save the query if appropriate
1347 if (!save_search_history(args, response))
1348 logout << "save failed";
1349 }
1350
1351 define_history_macros (disp, args, protos, logout);
1352
1353 textout << outconvert << disp << "_query:header_\n"
1354 << "_query:content_";
1355
1356 // output the results
1357 bool use_table = is_table_content (formatlistptr);
1358 bptr->output_section_group (response, args, collection, 0, formatlistptr,
1359 use_table, request.fields, request.getParents,
1360 collectproto, disp, outconvert, textout, logout);
1361
1362
1363 textout << outconvert << disp << "_query:footer_";
1364
1365 delete (formatlistptr);
1366
1367 return true;
1368}
1369
1370// define_query_macros sets the macros that couldn't be set until the
1371// query had been done. Those macros are _freqmsg_, _quotedquery_,
1372// _resultline_, _nextfirst_, _nextlast_, _prevfirst_, _prevlast_,
1373// _thisfirst_, and _thislast_
1374void queryaction::define_query_macros (cgiargsclass &args, displayclass &disp,
1375 const FilterResponse_t &response) {
1376 // set up _freqmsg_ and _quotedquery_ macros
1377 text_t freqmsg = "_textfreqmsg1_";
1378 TermInfo_tarray::const_iterator this_term = response.termInfo.begin();
1379 TermInfo_tarray::const_iterator end_term = response.termInfo.end();
1380 while (this_term != end_term) {
1381 char *term = (*this_term).term.getcstr();
1382 if(term[0]<'0' || term[0] >'9'){
1383 freqmsg += (*this_term).term + ": " + (*this_term).freq;
1384 if ((this_term + 1) != end_term)
1385 freqmsg += ", ";
1386 }
1387 delete term;
1388 this_term ++;
1389 }
1390 disp.setmacro ("freqmsg", "query", freqmsg);
1391
1392 // set up _resultline_ macro
1393 text_t resline;
1394 int maxdocs = args.getintarg("m");
1395 int numdocs = response.numDocs;
1396 isapprox isApprox = response.isApprox;
1397 if (num_phrases > 0) isApprox = Exact;
1398 if (maxdocs == -1) maxdocs = numdocs;
1399 else if (numdocs > maxdocs) {
1400 numdocs = maxdocs;
1401 isApprox = MoreThan;
1402 }
1403
1404 if (isApprox == Approximate) resline = "_textapprox_";
1405 else if (isApprox == MoreThan) resline = "_textmorethan_";
1406
1407 if (numdocs == 0) resline = "_textnodocs_";
1408 else if (numdocs == 1) resline += "_text1doc_";
1409 else resline += text_t(numdocs) + " _textlotsdocs_";
1410
1411 disp.setmacro("resultline", "query", resline);
1412
1413 int firstdoc = args.getintarg("r");
1414 int hitsperpage = args.getintarg("o");
1415 if (hitsperpage == -1) hitsperpage = numdocs;
1416
1417 // set up _thisfirst_ and _thislast_ macros
1418 disp.setmacro ("thisfirst", "query", firstdoc);
1419 int thislast = firstdoc + (hitsperpage - 1);
1420 if (thislast > numdocs) thislast = numdocs;
1421 disp.setmacro ("thislast", "query", thislast);
1422
1423 // set up _prevfirst_ and _prevlast_ macros
1424 if (firstdoc > 1) {
1425 disp.setmacro ("prevlast", "query", firstdoc - 1);
1426 int prevfirst = firstdoc - hitsperpage;
1427 if (prevfirst < 1) prevfirst = 1;
1428 disp.setmacro ("prevfirst", "query", prevfirst);
1429 }
1430
1431 // set up _nextfirst_ and _nextlast_ macros
1432 if (thislast < numdocs) {
1433 disp.setmacro ("nextfirst", "query", thislast + 1);
1434 int nextlast = thislast + hitsperpage;
1435 if (nextlast > numdocs) nextlast = numdocs;
1436 disp.setmacro ("nextlast", "query", nextlast);
1437 }
1438}
1439
1440bool queryaction::save_search_history (cgiargsclass &args, const FilterResponse_t &response)
1441{
1442 if (args["q"]=="") return true; // null query, dont save
1443 if (args["hs"]=="0") return true; // only save when submit query
1444 // displayed
1445 // get userid
1446 text_t userid = args["z"];
1447
1448 // the number of docs goes on the front of the query string
1449 int numdocs= response.numDocs;
1450 text_t query = text_t(numdocs);
1451 if (response.isApprox==MoreThan) { // there were more docs found
1452 query.push_back('+');
1453 }
1454 query += "c="+args["c"];
1455 query += "&h="+args["h"];
1456 query += "&t="+args["t"];
1457 query += "&b="+args["b"];
1458 query += "&j="+args["j"];
1459 query += "&n="+args["n"];
1460 query += "&s="+args["s"];
1461 query += "&k="+args["k"];
1462 query += "&g="+args["g"];
1463
1464 text_t qstring = args["q"];
1465 text_t formattedquery =cgi_safe(qstring);
1466 query += "&q="+formattedquery;
1467
1468 bool display=false;
1469 int hd = args.getintarg("hd");
1470 if (hd > 0) display=true;
1471 if (set_history_info(userid, query, gdbmhome, display)) return true;
1472 else return false;
1473
1474
1475}
1476
1477
1478
Note: See TracBrowser for help on using the repository browser.