source: main/trunk/greenstone2/runtime-src/src/recpt/queryaction.cpp@ 22693

Last change on this file since 22693 was 22693, checked in by davidb, 14 years ago

'multiplevalue' appears to be left undefined -- in general for all args being set. In the case of 'sqlsf' this lead to a conflict-warning message. The current fix, sorts out the problem in this one case, but looks like a more general solution should be implemented. This could be as simple as ensuring sensible default values in its constructor

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