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

Last change on this file since 11153 was 11005, checked in by kjdon, 18 years ago

get_phrases now comes from phrases.h

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