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

Last change on this file since 9948 was 9931, checked in by kjdon, 19 years ago

getting collectionmeta from teh colinforesponse now, not using a get_info via the protocol. this means that collmeta will be read from the config file not from the database - it will not require rebuilding for changes to take effect.

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