source: main/tags/2.50-cdrom/gsdl/src/recpt/queryaction.cpp@ 26494

Last change on this file since 26494 was 7067, checked in by mdewsnip, 20 years ago

Added in the code from pageaction where if the collection specified by the c variable does not exist, you get a nice page rather than a view of the error log.

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