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

Last change on this file since 275 was 275, checked in by sjboddie, 25 years ago

got queryaction working

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 13.0 KB
Line 
1/**********************************************************************
2 *
3 * queryaction.cpp --
4 * Copyright (C) 1999 The New Zealand Digital Library Project
5 *
6 * PUT COPYRIGHT NOTICE HERE
7 *
8 * $Id: queryaction.cpp 275 1999-06-16 02:08:38Z sjboddie $
9 *
10 *********************************************************************/
11
12/*
13 $Log$
14 Revision 1.4 1999/06/16 02:08:38 sjboddie
15 got queryaction working
16
17 Revision 1.3 1999/03/25 03:06:45 sjboddie
18
19 altered receptionist slightly so it now passes *collectproto to
20 define_internal_macros and define_external_macros - need it
21 for browseaction
22
23 Revision 1.2 1999/03/03 20:26:50 rjmcnab
24
25 Modified stuff.
26
27 Revision 1.1 1999/02/28 22:45:21 rjmcnab
28
29 Initial revision.
30
31 */
32
33
34#include "queryaction.h"
35#include "querytools.h"
36
37queryaction::queryaction () {
38 // this action uses cgi variable "a"
39 cgiarginfo arg_ainfo;
40 arg_ainfo.shortname = "a";
41 arg_ainfo.longname = "action";
42 arg_ainfo.multiplechar = true;
43 arg_ainfo.defaultstatus = cgiarginfo::weak;
44 arg_ainfo.argdefault = "q";
45 arg_ainfo.savedarginfo = cgiarginfo::must;
46 argsinfo.addarginfo (NULL, arg_ainfo);
47
48 // "h"
49 arg_ainfo.shortname = "h";
50 arg_ainfo.longname = "main index";
51 arg_ainfo.multiplechar = true;
52 arg_ainfo.defaultstatus = cgiarginfo::weak;
53 arg_ainfo.argdefault = "";
54 arg_ainfo.savedarginfo = cgiarginfo::mustnot;
55 argsinfo.addarginfo (NULL, arg_ainfo);
56
57 // "j"
58 arg_ainfo.shortname = "j";
59 arg_ainfo.longname = "sub collection index";
60 arg_ainfo.multiplechar = true;
61 arg_ainfo.defaultstatus = cgiarginfo::weak;
62 arg_ainfo.argdefault = "";
63 arg_ainfo.savedarginfo = cgiarginfo::mustnot;
64 argsinfo.addarginfo (NULL, arg_ainfo);
65
66 // "n"
67 arg_ainfo.shortname = "n";
68 arg_ainfo.longname = "language index";
69 arg_ainfo.multiplechar = true;
70 arg_ainfo.defaultstatus = cgiarginfo::weak;
71 arg_ainfo.argdefault = "";
72 arg_ainfo.savedarginfo = cgiarginfo::mustnot;
73 argsinfo.addarginfo (NULL, arg_ainfo);
74
75 // "q"
76 arg_ainfo.shortname = "q";
77 arg_ainfo.longname = "query string";
78 arg_ainfo.multiplechar = true;
79 arg_ainfo.defaultstatus = cgiarginfo::weak;
80 arg_ainfo.argdefault = "";
81 arg_ainfo.savedarginfo = cgiarginfo::must;
82 argsinfo.addarginfo (NULL, arg_ainfo);
83
84 // "t"
85 arg_ainfo.shortname = "t";
86 arg_ainfo.longname = "search type";
87 arg_ainfo.multiplechar = false;
88 arg_ainfo.defaultstatus = cgiarginfo::weak;
89 arg_ainfo.argdefault = "0";
90 arg_ainfo.savedarginfo = cgiarginfo::must;
91 argsinfo.addarginfo (NULL, arg_ainfo);
92
93 // "k"
94 arg_ainfo.shortname = "k";
95 arg_ainfo.longname = "casefolding";
96 arg_ainfo.multiplechar = false;
97 arg_ainfo.defaultstatus = cgiarginfo::weak;
98 arg_ainfo.argdefault = "1";
99 arg_ainfo.savedarginfo = cgiarginfo::must;
100 argsinfo.addarginfo (NULL, arg_ainfo);
101
102 // "s"
103 arg_ainfo.shortname = "s";
104 arg_ainfo.longname = "stemming";
105 arg_ainfo.multiplechar = false;
106 arg_ainfo.defaultstatus = cgiarginfo::weak;
107 arg_ainfo.argdefault ="0";
108 arg_ainfo.savedarginfo = cgiarginfo::must;
109 argsinfo.addarginfo (NULL, arg_ainfo);
110
111 // "m"
112 arg_ainfo.shortname = "m";
113 arg_ainfo.longname = "maximum number of documents";
114 arg_ainfo.multiplechar = true;
115 arg_ainfo.defaultstatus = cgiarginfo::weak;
116 arg_ainfo.argdefault = "50";
117 arg_ainfo.savedarginfo = cgiarginfo::must;
118 argsinfo.addarginfo (NULL, arg_ainfo);
119
120 // "o"
121 arg_ainfo.shortname = "o";
122 arg_ainfo.longname = "hits per page";
123 arg_ainfo.multiplechar = true;
124 arg_ainfo.defaultstatus = cgiarginfo::weak;
125 arg_ainfo.argdefault = "20";
126 arg_ainfo.savedarginfo = cgiarginfo::must;
127 argsinfo.addarginfo (NULL, arg_ainfo);
128
129 // "r"
130 arg_ainfo.shortname = "r";
131 arg_ainfo.longname = "start results from";
132 arg_ainfo.multiplechar = true;
133 arg_ainfo.defaultstatus = cgiarginfo::weak;
134 arg_ainfo.argdefault = "1";
135 arg_ainfo.savedarginfo = cgiarginfo::must;
136 argsinfo.addarginfo (NULL, arg_ainfo);
137}
138
139void queryaction::configure (const text_t &key, const text_tarray &cfgline) {
140 action::configure (key, cfgline);
141}
142
143bool queryaction::init (ostream &logout) {
144 return action::init (logout);
145}
146
147bool queryaction::check_cgiargs (cgiargsinfoclass &argsinfo, cgiargsclass &args,
148 ostream &logout) {
149
150 // check t argument
151 int arg_t = args.getintarg("t");
152 if (arg_t != 0 && arg_t != 1) {
153 logout << "Warning: \"t\" argument out of range (" << arg_t << ")\n";
154 args["t"] = argsinfo["t"].argdefault;
155 }
156
157 // check k argument
158 int arg_k = args.getintarg("k");
159 if (arg_k != 0 && arg_k != 1) {
160 logout << "Warning: \"k\" argument out of range (" << arg_k << ")\n";
161 args["k"] = argsinfo["k"].argdefault;
162 }
163
164 // check s argument
165 int arg_s = args.getintarg("s");
166 if (arg_s != 0 && arg_s != 1) {
167 logout << "Warning: \"s\" argument out of range (" << arg_s << ")\n";
168 args["s"] = argsinfo["s"].argdefault;
169 }
170
171 // check m argument
172 int arg_m = args.getintarg("m");
173 if (arg_m < 0) {
174 logout << "Warning: \"m\" argument less than 0 (" << arg_m << ")\n";
175 args["m"] = argsinfo["m"].argdefault;
176 }
177
178 // check o argument
179 int arg_o = args.getintarg("o");
180 if (arg_o < 0) {
181 logout << "Warning: \"o\" argument less than 0 (" << arg_o << ")\n";
182 args["o"] = argsinfo["o"].argdefault;
183 }
184
185 // check r argument
186 int arg_r = args.getintarg("r");
187 if (arg_r < 1) {
188 logout << "Warning: \"r\" argument less than 1 (" << arg_r << ")\n";
189 args["r"] = argsinfo["r"].argdefault;
190 }
191
192 return true;
193}
194
195void queryaction::get_cgihead_info (cgiargsclass &/*args*/, response_t &response,
196 text_t &response_data, ostream &/*logout*/) {
197 response = content;
198 response_data = "text/html";
199}
200
201void queryaction::define_internal_macros (displayclass &/*disp*/, cgiargsclass &/*args*/,
202 recptproto */*collectproto*/, ostream &/*logout*/) {
203
204 // define_internal_macros doesn't set anything for this action. The following
205 // macros are set later though in define_query_macros (they can't be set until
206 // the query has been done).
207
208 // _freqmsg_ the term frequency string
209
210 // _quotedquery_ the part of the query string that was quoted for post-processing
211
212 // _resultline_ the "x documents matched the query" string
213
214 // _prevfirst_ these are used when setting up the links to previous/next
215 // _prevlast_ pages of results (_thisfirst_ and _thislast_ are used to set
216 // _nextfirst_ the 'results x-x for query: xxxx' string in the title bar)
217 // _nextlast_
218 // _thisfirst_
219 // _thislast_
220
221}
222
223// sets the selection box macros _hselection_, _jselection_, and _nselection_.
224// each option will need an _optionxoption_ macro (i.e. an _hselection_ macro
225// with options stx and ptx will need _optionhstx_ and _optionhptx_ macros)
226void queryaction::set_option_macro (const text_t &macroname, text_t current_value,
227 const FilterOption_t &option, displayclass &disp) {
228
229 if (option.validValues.size() < 2) return;
230
231 text_t macrovalue = "<select name=\"" + macroname + "\">\n";
232
233 if (current_value.empty()) current_value = option.defaultValue;
234
235 text_tarray::const_iterator thisvalue = option.validValues.begin();
236 text_tarray::const_iterator endvalue = option.validValues.end();
237
238 while (thisvalue != endvalue) {
239 macrovalue += "<option value=\"" + *thisvalue + "\"";
240 if (*thisvalue == current_value)
241 macrovalue += " selected";
242 macrovalue += ">_option" + macroname + *thisvalue + "_\n";
243 thisvalue ++;
244 }
245 macrovalue += "</select>\n";
246 disp.setmacro (macroname + "selection", "Global", macrovalue);
247}
248
249void queryaction::define_external_macros (displayclass &disp, cgiargsclass &args,
250 recptproto *collectproto, ostream &logout) {
251
252 // define_external_macros sets the following macros:
253
254 // some or all of these may not be required to be set
255 // _hselection_ the selection box for the main part of the index
256 // _jselection_ the selection box for the subcollection part of the index
257 // _nselection_ the selection box for the language part of the index
258
259
260 // can't do anything if collectproto is null (i.e. no collection was specified)
261 if (collectproto == NULL) return;
262
263 comerror_t err;
264 InfoFilterOptionsResponse_t response;
265 InfoFilterOptionsRequest_t request;
266 request.filterName = "QueryFilter";
267
268 collectproto->get_filteroptions (args["c"], request, response, err, logout);
269 if (err == noError) {
270
271 FilterOption_tmap::const_iterator it;
272 FilterOption_tmap::const_iterator end = response.filterOptions.end();
273
274 // _hselection_ (Index)
275 it = response.filterOptions.find ("Index");
276 if (it != end) set_option_macro ("h", args["h"], (*it).second, disp);
277
278 // _jselection_ (Subcollection)
279 it = response.filterOptions.find ("Subcollection");
280 if (it != end) set_option_macro ("j", args["j"], (*it).second, disp);
281
282 // _nselection_ (Language)
283 it = response.filterOptions.find ("Language");
284 if (it != end) set_option_macro ("n", args["n"], (*it).second, disp);
285 }
286}
287
288bool queryaction::do_action (cgiargsclass &args, recptproto *collectproto,
289 displayclass &disp, outconvertclass &outconvert,
290 ostream &textout, ostream &logout) {
291
292 if (collectproto == NULL) {
293 logout << "queryaction::do_action called with NULL collectproto\n";
294 textout << outconvert << disp << "_query:header_\n"
295 << "Error: Attempt to do query without setting collection\n"
296 << "_query:footer_\n";
297 } else {
298
299 text_t quotedstring;
300 FilterResponse_t response;
301
302 // do the query
303 FilterRequest_t request;
304 request.filterResultOptions = FROID | FRmetadata | FRtermFreq;
305 request.fields.push_back ("Title");
306 request.getParents = true;
307 if (!do_query (request, args, collectproto, quotedstring, response, logout))
308 return false;
309
310 // set macros
311 define_query_macros (args, disp, response, quotedstring);
312
313 // output the header
314 textout << outconvert << disp << "_query:header_\n_query:content_";
315
316 // output results
317 ResultDocInfo_tarray::const_iterator this_doc = response.docInfo.begin();
318 ResultDocInfo_tarray::const_iterator end_doc = response.docInfo.end();
319
320 textout << "<table cellspacing=4>\n";
321 while (this_doc != end_doc) {
322
323 text_t link = "<a href=\"_httpdocument_&d=" + (*this_doc).OID + "\">";
324
325 text_t title;
326 text_tarray::const_iterator this_title = (*this_doc).metadata[0].values.begin();
327 text_tarray::const_iterator end_title = (*this_doc).metadata[0].values.end();
328 while (this_title != end_title) {
329 if ((this_title + 1) == end_title)
330 title += link + *this_title + "</a>";
331 else
332 title += *this_title + ": ";
333 this_title ++;
334 }
335
336 textout << outconvert << disp << "<tr><td valign=top nowrap>" << link
337 << "_icontext_</a></td><td>" << title << "</td></tr>\n";
338
339 this_doc ++;
340 }
341 textout << "</table>\n";
342
343 // output the footer
344 textout << outconvert << disp << "_query:footer_";
345 }
346 return true;
347}
348
349// set_query_macros sets the macros that couldn't be set until the
350// query had been done. Those macros are _freqmsg_, _quotedquery_,
351// _resultline_, _nextfirst_, _nextlast_, _prevfirst_, _prevlast_,
352// _thisfirst_, and _thislast_
353void queryaction::define_query_macros (cgiargsclass &args, displayclass &disp,
354 const FilterResponse_t &response,
355 const text_t &quotedstring) {
356
357 int numdocs = response.numDocs;
358 int arg_m = args.getintarg("m");
359 if (numdocs > arg_m)
360 numdocs = arg_m;
361
362 // set up _freqmsg_ and _quotedquery_ macros
363 text_t freqmsg = "_textfreqmsg1_";
364 TermInfo_tarray::const_iterator this_term = response.termInfo.begin();
365 TermInfo_tarray::const_iterator end_term = response.termInfo.end();
366 while (this_term != end_term) {
367 freqmsg += (*this_term).term + ": " + (*this_term).freq;
368 if ((this_term + 1) != end_term)
369 freqmsg += ", ";
370 this_term ++;
371 }
372
373 disp.setmacro ("quotedquery", "query", quotedstring);
374 disp.setmacro ("freqmsg", "query", freqmsg);
375
376
377 // set up _resultline_ macro
378 text_t resline;
379
380 // can't use isApprox here as it will be false as long
381 // as numDocs < MAXDOCS (currently 200). If arg_m is less
382 // than MAXDOCS numDocs can be greater than arg_m while
383 // isApprox is false.
384 if (response.numDocs >= numdocs) resline = "_textmorethan_";
385 if (numdocs == 0) resline = "_textnodocs_";
386 else if (numdocs == 1) resline += "_text1doc_";
387 else resline += text_t(numdocs) + "_textlotsdocs_";
388
389 disp.setmacro("resultline", "query", resline);
390
391
392 int firstdoc = args.getintarg("r");
393 int hitsperpage = args.getintarg("o");
394
395 // set up _thisfirst_ and _thislast_ macros
396 disp.setmacro ("thisfirst", "query", firstdoc);
397 int thislast = firstdoc + (hitsperpage - 1);
398 if (thislast > numdocs) thislast = numdocs;
399 disp.setmacro ("thislast", "query", thislast);
400
401 // set up _prevfirst_ and _prevlast_ macros
402 if (firstdoc > 1) {
403 disp.setmacro ("prevlast", "query", firstdoc - 1);
404 int prevfirst = firstdoc - hitsperpage;
405 if (prevfirst < 1) prevfirst = 1;
406 disp.setmacro ("prevfirst", "query", prevfirst);
407 }
408
409 // set up _nextfirst_ and _nextlast_ macros
410 if (thislast < numdocs) {
411 disp.setmacro ("nextfirst", "query", thislast + 1);
412 int nextlast = thislast + hitsperpage;
413 if (nextlast > numdocs) nextlast = numdocs;
414 disp.setmacro ("nextlast", "query", nextlast);
415 }
416}
417
418
419
Note: See TracBrowser for help on using the repository browser.