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

Last change on this file since 22980 was 22980, checked in by kjdon, 14 years ago

when setting up a cgiarg, parameters not defined get left at their previous value. So setting cc arg multiplevalue=true ended up setting all following args to multiplevalue=true. But for fqk and fqs we don't want this, and presumably for the other args too. So I have moved cc arg to the bottom. But we should go through and explicitly set all args multiplevalue parameters to true or false so there is no confusion

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