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

Last change on this file since 22651 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
RevLine 
[174]1/**********************************************************************
2 *
3 * queryaction.cpp --
4 * Copyright (C) 1999 The New Zealand Digital Library Project
5 *
[533]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.
[174]9 *
[533]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 *
[174]24 *********************************************************************/
25
26#include "queryaction.h"
[275]27#include "querytools.h"
[347]28#include "formattools.h"
[757]29#include "cgiutils.h"
[772]30#include "OIDtools.h"
[928]31#include "fileutil.h"
32#include "text_t.h"
33#include "historydb.h"
[1915]34#include "htmlutils.h" // for html_safe in do_action
35#include "gsdltools.h"
[11005]36#include "phrases.h" // for get_phrases
[3176]37#include <stdlib.h> // for strtol
[3204]38#include <assert.h>
[174]39
[757]40
[22046]41queryaction::queryaction ()
42 : basequeryaction()
43{
44 num_phrases = 0;
[757]45
[22046]46 cgiarginfo arg_ainfo;
[403]47
[174]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);
[4755]56
[8029]57 // "ct" - 0 = mg, 1 = mgpp, 2=lucene
[1915]58 arg_ainfo.shortname = "ct";
59 arg_ainfo.longname = "collection type";
[12000]60 arg_ainfo.multiplechar = true; // can be empty or single char
[1915]61 arg_ainfo.defaultstatus = cgiarginfo::weak;
[11988]62 arg_ainfo.argdefault = g_EmptyText;
[1915]63 arg_ainfo.savedarginfo = cgiarginfo::must;
64 argsinfo.addarginfo (NULL, arg_ainfo);
65
[470]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
[174]75 // "h"
76 arg_ainfo.shortname = "h";
77 arg_ainfo.longname = "main index";
78 arg_ainfo.multiplechar = true;
79 arg_ainfo.defaultstatus = cgiarginfo::weak;
[7441]80 arg_ainfo.argdefault = g_EmptyText;
[295]81 arg_ainfo.savedarginfo = cgiarginfo::must;
[174]82 argsinfo.addarginfo (NULL, arg_ainfo);
83
[349]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;
[7441]89 arg_ainfo.argdefault = g_EmptyText;
[349]90 arg_ainfo.savedarginfo = cgiarginfo::must;
91 argsinfo.addarginfo (NULL, arg_ainfo);
92
[174]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;
[7441]98 arg_ainfo.argdefault = g_EmptyText;
[295]99 arg_ainfo.savedarginfo = cgiarginfo::must;
[174]100 argsinfo.addarginfo (NULL, arg_ainfo);
101
[349]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;
[7441]107 arg_ainfo.argdefault = g_EmptyText;
[349]108 arg_ainfo.savedarginfo = cgiarginfo::must;
109 argsinfo.addarginfo (NULL, arg_ainfo);
110
[174]111 // "n"
112 arg_ainfo.shortname = "n";
113 arg_ainfo.longname = "language index";
114 arg_ainfo.multiplechar = true;
115 arg_ainfo.defaultstatus = cgiarginfo::weak;
[7441]116 arg_ainfo.argdefault = g_EmptyText;
[295]117 arg_ainfo.savedarginfo = cgiarginfo::must;
[174]118 argsinfo.addarginfo (NULL, arg_ainfo);
119
[349]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;
[7441]125 arg_ainfo.argdefault = g_EmptyText;
[349]126 arg_ainfo.savedarginfo = cgiarginfo::must;
127 argsinfo.addarginfo (NULL, arg_ainfo);
128
[1915]129
[284]130 // "t" - 1 = ranked 0 = boolean
[174]131 arg_ainfo.shortname = "t";
132 arg_ainfo.longname = "search type";
133 arg_ainfo.multiplechar = false;
134 arg_ainfo.defaultstatus = cgiarginfo::weak;
[284]135 arg_ainfo.argdefault = "1";
[174]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
[12866]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
[174]157 // "s"
158 arg_ainfo.shortname = "s";
159 arg_ainfo.longname = "stemming";
160 arg_ainfo.multiplechar = false;
161 arg_ainfo.defaultstatus = cgiarginfo::weak;
[12866]162 arg_ainfo.argdefault = "0";
[174]163 arg_ainfo.savedarginfo = cgiarginfo::must;
164 argsinfo.addarginfo (NULL, arg_ainfo);
165
[12866]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
[174]193
[757]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;
[776]216 arg_ainfo.multiplevalue = true;
[757]217 arg_ainfo.defaultstatus = cgiarginfo::weak;
[7441]218 arg_ainfo.argdefault = g_EmptyText;
[757]219 arg_ainfo.savedarginfo = cgiarginfo::must;
220 argsinfo.addarginfo (NULL, arg_ainfo);
221
[928]222
[2745]223 // "g" - new arg for granularity, for mgpp collections
[1328]224 arg_ainfo.shortname = "g";
225 arg_ainfo.longname = "granularity";
226 arg_ainfo.multiplechar = true;
227 arg_ainfo.defaultstatus = cgiarginfo::weak;
[7441]228 arg_ainfo.argdefault = g_EmptyText;
[1328]229 arg_ainfo.savedarginfo = cgiarginfo::must;
230 argsinfo.addarginfo (NULL, arg_ainfo);
[928]231
[1915]232 // "ds" - start date
[1373]233 arg_ainfo.shortname = "ds";
234 arg_ainfo.longname = "start date";
235 arg_ainfo.multiplechar = true;
236 arg_ainfo.defaultstatus = cgiarginfo::weak;
[7441]237 arg_ainfo.argdefault = g_EmptyText;
[1373]238 arg_ainfo.savedarginfo = cgiarginfo::must;
239 argsinfo.addarginfo (NULL, arg_ainfo);
[1328]240
[1373]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;
[7441]246 arg_ainfo.argdefault = g_EmptyText;
[1373]247 arg_ainfo.savedarginfo = cgiarginfo::must;
248 argsinfo.addarginfo (NULL, arg_ainfo);
[1328]249
[1373]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
[1915]268 // "qt" - 0 = text, 1 = form
269 arg_ainfo.shortname = "qt";
270 arg_ainfo.longname = "query type";
[12000]271 arg_ainfo.multiplechar = true; // can be empty or single char
[1915]272 arg_ainfo.defaultstatus = cgiarginfo::weak;
[11988]273 arg_ainfo.argdefault = g_EmptyText;
[1915]274 arg_ainfo.savedarginfo = cgiarginfo::must;
275 argsinfo.addarginfo (NULL, arg_ainfo);
276
[5531]277 // "qto" - 1 = text only, 2 = form only, 3 = text and form
[4755]278 arg_ainfo.shortname = "qto";
279 arg_ainfo.longname = "query type options";
[12000]280 arg_ainfo.multiplechar = true; // can be empty or single char
[4755]281 arg_ainfo.defaultstatus = cgiarginfo::weak;
[11988]282 arg_ainfo.argdefault = g_EmptyText;
[4755]283 arg_ainfo.savedarginfo = cgiarginfo::must;
284 argsinfo.addarginfo (NULL, arg_ainfo);
[12768]285
[1915]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;
[7441]302 arg_ainfo.argdefault = g_EmptyText;
[3145]303 arg_ainfo.savedarginfo = cgiarginfo::must;
[1915]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;
[7441]313 arg_ainfo.argdefault = g_EmptyText;
[3145]314 arg_ainfo.savedarginfo = cgiarginfo::must;
[1915]315 argsinfo.addarginfo (NULL, arg_ainfo);
316
317
[12276]318 // "sf" - Sort field. Set to field to be used for sorting search reult
319 // set (only implemented for lucene collections at present).
[22046]320 arg_ainfo.shortname = "sqlsf";
321 arg_ainfo.longname = "sql sort field";
[12276]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);
[12388]327
[22046]328
[174]329}
330
[22046]331queryaction::~queryaction ()
332{
333}
334
[174]335void queryaction::configure (const text_t &key, const text_tarray &cfgline) {
[22046]336 basequeryaction::configure (key, cfgline);
[174]337}
338
339bool queryaction::init (ostream &logout) {
[22046]340 return basequeryaction::init (logout);
[174]341}
342
[275]343bool queryaction::check_cgiargs (cgiargsinfoclass &argsinfo, cgiargsclass &args,
[22046]344 recptprotolistclass* protos, ostream &logout) {
[275]345
[174]346 // check t argument
[275]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";
[284]350 cgiarginfo *tinfo = argsinfo.getarginfo ("t");
351 if (tinfo != NULL) args["t"] = tinfo->argdefault;
[275]352 }
[174]353
354 // check k argument
[275]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";
[284]358 cgiarginfo *kinfo = argsinfo.getarginfo ("k");
359 if (kinfo != NULL) args["k"] = kinfo->argdefault;
[275]360 }
[174]361
362 // check s argument
[275]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";
[284]366 cgiarginfo *sinfo = argsinfo.getarginfo ("s");
367 if (sinfo != NULL) args["s"] = sinfo->argdefault;
[275]368 }
[174]369
370
[1915]371 // check ct argument
372 int arg_ct = args.getintarg("ct");
[10361]373 if (arg_ct < 0 || arg_ct > 2) {
[1915]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;
[928]377 }
378
[1915]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
[22046]411 return basequeryaction::check_cgiargs(argsinfo,args,protos,logout);
[174]412
413}
414
[757]415void queryaction::define_internal_macros (displayclass &disp, cgiargsclass &args,
[1915]416 recptprotolistclass * protos,
[22046]417 ostream &logout)
418{
419 basequeryaction::define_internal_macros(disp,args,protos,logout);
[275]420
[2769]421 define_query_interface(disp, args, protos, logout);
[174]422}
423
[2769]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 }
[11988]446
[2769]447 text_tmap::iterator check = colinfo->format.find("QueryInterface");
448 if(check != colinfo->format.end()){
449 if((*check).second=="DateSearch"){
[5633]450 text_t current = "_datesearch_";
451 disp.setmacro("optdatesearch","query",current);
[2769]452 }
453 }
454}
455
456
[275]457
[757]458void queryaction::define_external_macros (displayclass &disp, cgiargsclass &args,
459 recptprotolistclass *protos, ostream &logout) {
[275]460
461 // define_external_macros sets the following macros:
462
463 // some or all of these may not be required to be set
[356]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
[275]468
[4755]469 // _gselection_, the selection box forlevels (mgpp)
470 // _fqfselection_, the selection box for index/fields (mgpp)
[275]471 // can't do anything if collectproto is null (i.e. no collection was specified)
[757]472 recptproto *collectproto = protos->getrecptproto (args["c"], logout);
[275]473 if (collectproto == NULL) return;
474
[11988]475 ColInfoResponse_t *colinfo = recpt->get_collectinfo_ptr(collectproto,
476 args["c"],
477 logout);
478 set_query_type_args(colinfo, args);
[12866]479 set_stem_index_args(colinfo, args);
[11988]480
[275]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) {
[356]488
[275]489 FilterOption_tmap::const_iterator it;
490 FilterOption_tmap::const_iterator end = response.filterOptions.end();
[356]491
492 // _hselection_ and _h2selection_ (Index)
[275]493 it = response.filterOptions.find ("Index");
[4937]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);
[356]496
497 // _jselection_ and _j2selection_ (Subcollection)
[275]498 it = response.filterOptions.find ("Subcollection");
[4937]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);
[356]501
502 // _nselection_ and _n2selection_ (Language)
503 it = response.filterOptions.find ("Language");
[4937]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);
[275]506
[356]507 // _cq2selection_ (CombineQuery)
508 it = response.filterOptions.find ("CombineQuery");
[4937]509 if (it != end) set_option_macro ("cq2", args["cq2"], true,false, (*it).second, disp);
[928]510
[8029]511 if ((args["ct"] == "1") || (args["ct"] == "2")) { // mgpp/lucene collections
[4780]512 // _gselection_ (Level)
513 it = response.filterOptions.find("Level");
514 if (it!=end) {
[4937]515 set_option_macro("g", args["g"], false, false, (*it).second, disp);
[4780]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");
[4937]522 if (it!=end) {
[12786]523 bool form_search = false;
524 if (args["qto"]=="2" || args["qt"]=="1") {
525 form_search = true;
[4937]526 }
[12786]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 }
[4937]532 }
[4780]533 }
[20481]534
535 // add a queryterms macro for plain version of search terms
[20601]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"]));
[20481]551 }
[20601]552
[4780]553 }
554} // define external macros
[12786]555
[22046]556
[12786]557void queryaction::set_sfselection_macro(text_t current_value,
558 const FilterOption_t &option,
559 displayclass &disp) {
560
[13587]561 // we need at least one option here to continue
562 if (option.validValues.size() < 1) {
[12786]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
[4780]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;
[275]605 }
[4780]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();
[928]609
[4780]610 bool has_paras = false;
611 while (thisvalue != endvalue) {
[4809]612 if (*thisvalue == "Para") {
[4780]613 has_paras = true;
614 break;
615 }
[9620]616 ++thisvalue;
[4780]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;
[4809]623 if (option.validValues[0] == "Para") {
[4780]624 opt = 1;
625 }
[7433]626 disp.setmacro ("gformselection", displayclass::defaultpackage, "_"+option.validValues[opt]+"_");
[4780]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) {
[4809]638 if (*thisvalue != "Para") {
[4780]639 macrovalue += "<option value=\"" + *thisvalue + "\"";
640 if (*thisvalue == current_value)
641 macrovalue += " selected";
642 macrovalue += ">_" + *thisvalue + "_\n";
643 }
[9620]644 ++thisvalue;
[4780]645 }
646 macrovalue += "</select>\n";
[7433]647 disp.setmacro ("gformselection", displayclass::defaultpackage, macrovalue);
[4780]648}
[22046]649
[1915]650void queryaction::define_form_macros (displayclass &disp, cgiargsclass &args,
[22046]651 recptprotolistclass *protos,
652 ostream &logout)
653{
[1915]654
655 // defines the following macros
656 // _regformlist_
657 // _advformlist_
658
[8029]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
[1915]660 return; // dont need these macros
661
662 text_t form = "";
663 int argfqn = args.getintarg("fqn");
664
[12768]665 if (args["b"] == "1") { // advanced form
[12786]666 form += "_firstadvformelement_\n";
[9620]667 for (int i=1; i<argfqn; ++i) {
[12786]668 form += "_advformelement_\n";
[1915]669 }
670 disp.setmacro("advformlist", "query", form);
671 }
672 else { // simple form
[9620]673 for (int i=0; i<argfqn; ++i) {
[12786]674 form += "_regformelement_\n";
[1915]675 }
676 disp.setmacro("regformlist", "query", form);
677 }
678
679}
680
[928]681void queryaction::output_ccp (cgiargsclass &args, recptprotolistclass *protos,
682 displayclass &disp, outconvertclass &outconvert,
683 ostream &textout, ostream &logout) {
684
[1270]685 ColInfoResponse_t *cinfo = NULL;
[928]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"
[12488]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"
[928]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"
[12488]713 << "<center><table width=\"_pagewidth_\">\n"
[928]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
[1270]728 cinfo = recpt->get_collectinfo_ptr ((*rprotolist_here).p, *collist_here, logout);
[928]729 // if (err == noError && cinfo.isPublic && (cinfo.buildDate > 0)) {
[1270]730 if (cinfo != NULL && (cinfo->buildDate > 0)) {
[928]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");
[9620]739 if (it == end) {++collist_here; continue;}
[928]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;
[9620]744 ++there;
[928]745 }
[9620]746 if (there == tend) {++collist_here; continue;}
[928]747 }
748 if (!subcollection.empty()) {
749 it = fresponse.filterOptions.find ("Subcollection");
[9620]750 if (it == end) {++collist_here; continue;}
[928]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;
[9620]755 ++there;
[928]756 }
[9620]757 if (there == tend) {++collist_here; continue;}
[928]758 }
759 if (!language.empty()) {
760 it = fresponse.filterOptions.find ("Language");
[9620]761 if (it == end) {++collist_here; continue;}
[928]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;
[9620]766 ++there;
[928]767 }
[9620]768 if (there == tend) {++collist_here; continue;}
[928]769 }
770
771 // we've got a matching collection
[12488]772 textout << outconvert << "<input type=\"checkbox\"";
[928]773
774 text_tset::const_iterator t = collections.find (*collist_here);
[3671]775 if (t != collections.end()) textout << outconvert << " checked";
[9931]776
777 text_t collectionname = cinfo->get_collectionmeta("collectionname", args["l"]);
778 if (collectionname.empty()) {
779 collectionname = *collist_here;
780 }
781 textout << outconvert << disp
[12488]782 << " name=\"cc\" value=\"" << *collist_here << "\">"
[9931]783 << collectionname << "<br>\n";
[928]784
785
786 }
787 }
[9620]788 ++collist_here;
[928]789 }
790 }
791 }
[9620]792 ++rprotolist_here;
[928]793 }
794 textout << outconvert << disp
795 << "</td></tr></table></center>\n"
796 << "</form>\n"
797 << "_query:footer_\n";
798
[174]799}
[275]800
[757]801bool queryaction::do_action (cgiargsclass &args, recptprotolistclass *protos,
802 browsermapclass *browsers, displayclass &disp,
[421]803 outconvertclass &outconvert, ostream &textout,
804 ostream &logout) {
[757]805
[1270]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
[865]813 if (args["ccs"] == "1") {
814 if (!args["cc"].empty()) {
[757]815 // query the selected collections
[865]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 }
[337]828 }
[421]829
[800]830 // simply query the current collection
[865]831 if (!search_single_collection (args, args["c"], protos, browsers, disp,
832 outconvert, textout, logout)) return false;
[757]833 return true;
834}
835
[22046]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
[757]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";
[865]874 return search_single_collection (args, args["c"], protos, browsers, disp,
[757]875 outconvert, textout, logout);
[447]876 }
877
[757]878 // queryaction uses "VList" browser to display results,
879 // a queries clasification is "Search"
880 text_t browsertype = "VList";
881 text_t classification = "Search";
[337]882
[757]883 QueryResult_tset results;
884 map<text_t, colinfo_t, lttext_t> colinfomap;
[275]885
[1270]886 ColInfoResponse_t *cinfo = NULL;
[1695]887 recptproto *collectproto = NULL;
[757]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;
[800]894 isapprox isApprox = Exact;
[772]895
[6584]896 // what to do about segmentation for multiple colls??
897 bool segment = false;
[4200]898 text_t formattedstring = "";
[6584]899 get_formatted_query_string(formattedstring, segment, args, disp, logout);
[4200]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 }
[4220]910 bool syntax_error = false;
[4200]911
[757]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
[865]928 map<text_t, int, lttext_t> termfreqs;
[10789]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
[757]962 while (col_here != col_end) {
963
[1695]964 collectproto = protos->getrecptproto (*col_here, logout);
[757]965 if (collectproto == NULL) {
966 logout << outconvert << "queryaction::search_multiple_collections: " << *col_here
967 << " collection has a NULL collectproto, ignoring\n";
[9620]968 ++col_here;
[757]969 continue;
970 }
[1270]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";
[9620]974 ++col_here;
[1270]975 continue;
976 }
[757]977
[10789]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
[757]985 // get the formatstring if there is one
[10789]986 if (!get_formatstring (classification, browsertype,
987 cinfo->format, formatstring)) {
988 formatstring = bptr->get_default_formatstring();
989 }
[757]990
[10789]991 formatlistptr = new format_t();
992 parse_formatstring (formatstring, formatlistptr, request.fields, request.getParents);
993 }
994
[757]995 colinfo_t thiscolinfo;
996 thiscolinfo.formatlistptr = formatlistptr;
997 thiscolinfo.browserptr = bptr;
998 colinfomap[*col_here] = thiscolinfo;
999
[275]1000 // do the query
[757]1001 collectproto->filter (*col_here, request, response, err, logout);
[4220]1002 if (err != noError && err != syntaxError) {
[757]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";
[275]1007 return false;
[757]1008 }
1009
[4220]1010 if (err == syntaxError) {
1011 syntax_error = true;
1012 freqmsg = "_textinvalidquery_";
1013 // assume the syntax will be invalid for all colls
1014 break;
1015 }
[12421]1016 if (response.error_message == "TOO_MANY_CLAUSES") {
1017 freqmsg = "_textlucenetoomanyclauses_";
1018 break;
1019 }
[800]1020 if (isApprox == Exact)
1021 isApprox = response.isApprox;
1022 else if (isApprox == MoreThan)
1023 if (response.isApprox == Approximate)
1024 isApprox = response.isApprox;
1025
[757]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) {
[865]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 }
[9620]1034 ++this_term;
[757]1035 }
[12380]1036
[757]1037 if (response.numDocs > 0) {
1038 numdocs += response.numDocs;
[13366]1039
[757]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);
[9620]1047 ++doc_here;
[447]1048 }
[347]1049 }
[9620]1050 ++col_here;
[4200]1051 } // for each coll
[13366]1052
1053 text_t numdocs_t = numdocs;
1054 args["nmd"] = numdocs_t;
[347]1055
[800]1056 disp.setmacro ("freqmsg", "query", freqmsg);
[349]1057
[4200]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);
[1690]1062
[4200]1063 textout << outconvert << disp << "_query:header_\n"
1064 << "_query:content_";
[757]1065
[4220]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) {
[9620]1085 if (count < firstdoc) {++count; ++res_here; continue;}
[4220]1086 if (count > thislast) break;
[13366]1087
[4220]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";
[9620]1097 ++res_here;
[4220]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";
[9620]1105 ++res_here;
1106 ++count;
[1695]1107 }
[757]1108 }
1109 textout << outconvert << disp << "_query:footer_";
[349]1110
[757]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);
[9620]1116 ++here;
[757]1117 }
[275]1118 return true;
1119}
[757]1120
[865]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) {
[757]1125
[865]1126 recptproto *collectproto = protos->getrecptproto (collection, logout);
[757]1127 if (collectproto == NULL) {
[865]1128 logout << outconvert << "queryaction::search_single_collection: " << collection
[757]1129 << " collection has a NULL collectproto\n";
[7067]1130
1131 // Display the "this collection is not installed on this system" page
[7433]1132 disp.setmacro("cvariable", displayclass::defaultpackage, collection);
[7067]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;
[757]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;
[1270]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 }
[757]1152
[6584]1153 bool segment = cinfo->isSegmented;
[757]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,
[4200]1159 cinfo->format, formatstring)) {
[757]1160 formatstring = bptr->get_default_formatstring();
[4200]1161 }
[757]1162 FilterRequest_t request;
1163 FilterResponse_t response;
[13366]1164
[17936]1165 text_t hits_per_page_old = args["o"];
1166 text_t start_results_from_old = args["r"];
[13366]1167
[17936]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") {
[17938]1172 args["r"] = args["ifln"]; // the document number we want
[17936]1173 args["o"] = "1";
[13366]1174 }
1175
[757]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;
[1915]1184 text_t formattedstring = "";
[6584]1185 get_formatted_query_string(formattedstring, segment, args, disp, logout);
[13366]1186
1187
[12421]1188 if (!formattedstring.empty()) { // do the query
[4200]1189 // note! formattedstring is in unicode! mg and mgpp must convert!
1190 set_queryfilter_options (request, formattedstring, args);
[13366]1191
[4200]1192 collectproto->filter (collection, request, response, err, logout);
[13366]1193
[9698]1194 if (err != noError) {
[4200]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";
[9698]1199
[4200]1200 }
[13280]1201
1202 // Perform the "I'm feeling lucky" trick if the "ifl" argument is set
[9698]1203 if (err == noError && !args["ifl"].empty()) {
[13366]1204 //Restore the "r" and "o" arg
[17936]1205 args["r"] = start_results_from_old;
1206 args["o"] = hits_per_page_old;
1207
[13366]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();
[17936]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
[17938]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 }
[17936]1242 }
1243
[17938]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";
[17936]1248 textout << flush;
1249
1250 return true;
1251 }
1252 }
1253
[13280]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";
[13366]1258
[7594]1259 }
1260
[9698]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 }
[12421]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 }
[4200]1280 }
[17938]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
[4200]1286 define_history_macros (disp, args, protos, logout);
[12421]1287
[4200]1288 textout << outconvert << disp << "_query:header_\n"
1289 << "_query:content_";
1290
[9698]1291 if (err == noError) {
1292 // output the results
[13366]1293 text_t numdocs_t = response.numDocs;
1294 args["nmd"] = numdocs_t;
[9698]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 }
[4200]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,
[6584]1312 bool segment,
[4200]1313 cgiargsclass &args,
1314 displayclass &disp,
1315 ostream &logout) {
[4755]1316 if (args["qt"]=="0" && args["qto"] != "2") { // normal text search
[1915]1317 formattedstring = args["q"];
[7197]1318 // remove & | ! for simple search,do segmentation if necessary
[6584]1319 format_querystring (formattedstring, args.getintarg("b"), segment);
[8357]1320 if (args["ct"]!=0) { // mgpp and lucene - need to add in tag info if appropriate
[12786]1321 format_field_info(formattedstring, args["fqf"], args.getintarg("ct"),
1322 args.getintarg("t"), args.getintarg("b"));
[4755]1323 }
[8029]1324
[1915]1325 add_dates(formattedstring, args.getintarg("ds"), args.getintarg("de"),
[8029]1326 args.getintarg("dsbc"), args.getintarg("debc"),
1327 args.getintarg("ct"));
[7199]1328 args["q"] = formattedstring;
[8357]1329
[928]1330 }
[4755]1331 else if (args["qt"]=="1" || args["qto"]=="2"){ // form search
[1347]1332
[12768]1333 if (args["b"]=="1" && args["fqa"]=="1") { // explicit query
[1915]1334 formattedstring = args["q"];
1335 }
1336 else { // form search
[12768]1337 if (args["b"]=="0") { // regular form
[12786]1338 parse_reg_query_form(formattedstring, args, segment);
[1915]1339 }
1340 else { // advanced form
[12786]1341 parse_adv_query_form(formattedstring, args, segment);
[1915]1342 }
[7199]1343 args["q"] = formattedstring;
1344
[3159]1345 // reset the cgiargfqv macro - need to escape any quotes in it
1346 disp.setmacro("cgiargfqv", "query", escape_quotes(args["fqv"]));
[7199]1347
1348 // also reset the _cgiargq_ macro as it has changed now
[7433]1349 disp.setmacro("cgiargq", displayclass::defaultpackage, html_safe(args["q"]));
[20481]1350
[7199]1351 // reset the compressed options to include the q arg
1352 text_t compressedoptions = recpt->get_compressed_arg(args, logout);
1353 if (!compressedoptions.empty()) {
[7433]1354 disp.setmacro ("compressedoptions", displayclass::defaultpackage, dm_safe(compressedoptions));
[7199]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)
[13463]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
[7433]1364 disp.setmacro ("decodedcompressedoptions", displayclass::defaultpackage, dm_safe(compressedoptions));
[7199]1365 }
1366 } // form search
1367 } // args["qt"]=1
1368 else {
[22046]1369 logout << "ERROR (queryaction::get_formatted_query_string): querytype not defined\n";
[757]1370 }
[4200]1371}
[928]1372
[757]1373
[298]1374// define_query_macros sets the macros that couldn't be set until the
[4200]1375// query had been done. Those macros are
[275]1376// _resultline_, _nextfirst_, _nextlast_, _prevfirst_, _prevlast_,
[9698]1377// _thisfirst_, and _thislast_ and _quotedquery_
[4200]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,
[22046]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
[800]1386 if (num_phrases > 0) isApprox = Exact;
[403]1387
[22046]1388 basequeryaction::define_query_macros(args,disp,numdocs,isApprox);
[275]1389
[7197]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;
[9620]1406 ++phere;
[7197]1407 }
1408 if (args.getintarg("s") && !quotedquery.empty()) quotedquery += "_textstemon_";
1409 disp.setmacro ("quotedquery", "query", quotedquery);
1410 }
1411
[275]1412}
1413
[4200]1414// should this change for cross coll search??
1415bool queryaction::save_search_history (cgiargsclass &args, int numdocs,
1416 isapprox isApprox) {
[928]1417 if (args["q"]=="") return true; // null query, dont save
[4200]1418 if (args["hs"]=="0") return true; // only save when submit query pressed
1419
[928]1420 // get userid
1421 text_t userid = args["z"];
[275]1422
[928]1423 // the number of docs goes on the front of the query string
1424 text_t query = text_t(numdocs);
[4200]1425 if (isApprox==MoreThan) { // there were more docs found
[928]1426 query.push_back('+');
1427 }
[1915]1428 query += "c="+args["c"];
[11751]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"];
[1915]1437
[928]1438 text_t qstring = args["q"];
[10262]1439 //text_t formattedquery =cgi_safe(qstring);
[10873]1440 //query += "&amp;q="+formattedquery;
[11751]1441 query += ";q="+qstring;
[1915]1442 bool display=false;
1443 int hd = args.getintarg("hd");
1444 if (hd > 0) display=true;
[15589]1445 if (set_history_info(userid, query, dbhome, display)) return true;
[928]1446 else return false;
1447}
[1373]1448
Note: See TracBrowser for help on using the repository browser.