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

Last change on this file since 3159 was 3159, checked in by kjdon, 22 years ago

in mgpp form query, field list now has a blank line after All Fields and
TextOnly (which may or may not be present), before the 'real' metadata.

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