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

Last change on this file since 12525 was 12488, checked in by mdewsnip, 18 years ago

Some XHTML compliance changes.

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