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

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

Changed the new "fuzziness" argument to take an integer instead of a float, since dots just make things more difficult.

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