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

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

Changes necessary to support new sql-query action

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