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

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

added function define_query_interface for dynamic query interface changes

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