source: branches/z3950-branch/gsdl/src/recpt/documentaction.cpp@ 1342

Last change on this file since 1342 was 1342, checked in by johnmcp, 24 years ago

Relatively stable z39.50 implementation now, merged with the mgpp source.
(Still needs a decent interface and query language though...)

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 32.8 KB
Line 
1/**********************************************************************
2 *
3 * documentaction.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 <string.h>
27#include "documentaction.h"
28#include "browsetools.h"
29#include "OIDtools.h"
30#include "querytools.h"
31#include "unitool.h"
32#include "gsdltools.h"
33
34documentaction::documentaction () {
35 recpt = NULL;
36
37
38 // this action uses cgi variables "a", "d", "cl",
39 // "x", "gc", "gt", "gp", and "hl"
40 cgiarginfo arg_ainfo;
41 arg_ainfo.shortname = "a";
42 arg_ainfo.longname = "action";
43 arg_ainfo.multiplechar = true;
44 arg_ainfo.defaultstatus = cgiarginfo::weak;
45 arg_ainfo.argdefault = "p";
46 arg_ainfo.savedarginfo = cgiarginfo::must;
47 argsinfo.addarginfo (NULL, arg_ainfo);
48
49 arg_ainfo.shortname = "d";
50 arg_ainfo.longname = "document OID";
51 arg_ainfo.multiplechar = true;
52 arg_ainfo.defaultstatus = cgiarginfo::none;
53 arg_ainfo.argdefault = "";
54 arg_ainfo.savedarginfo = cgiarginfo::can;
55 argsinfo.addarginfo (NULL, arg_ainfo);
56
57 // whether or not a document should be retrieved from the
58 // library or the Web.
59 arg_ainfo.shortname = "il";
60 arg_ainfo.longname = "internal link preference";
61 arg_ainfo.multiplechar = false;
62 arg_ainfo.defaultstatus = cgiarginfo::weak;
63 arg_ainfo.argdefault = "l";
64 arg_ainfo.savedarginfo = cgiarginfo::must;
65 argsinfo.addarginfo (NULL, arg_ainfo);
66
67 arg_ainfo.shortname = "cl";
68 arg_ainfo.longname = "classification OID";
69 arg_ainfo.multiplechar = true;
70 arg_ainfo.defaultstatus = cgiarginfo::none;
71 arg_ainfo.argdefault = "";
72 arg_ainfo.savedarginfo = cgiarginfo::can;
73 argsinfo.addarginfo (NULL, arg_ainfo);
74
75 // in this action "gc" controls the expand/contract
76 // contents function
77 arg_ainfo.shortname = "gc";
78 arg_ainfo.longname = "expand contents";
79 arg_ainfo.multiplechar = false;
80 arg_ainfo.defaultstatus = cgiarginfo::weak;
81 arg_ainfo.argdefault = "0";
82 arg_ainfo.savedarginfo = cgiarginfo::can;
83 argsinfo.addarginfo (NULL, arg_ainfo);
84
85 // in this action "gt" controls the expand/contract
86 // text function 0 = not expanded, 1 = expand unless
87 // there are more than 10 sections containing text,
88 // 2 = expand all
89 arg_ainfo.shortname = "gt";
90 arg_ainfo.longname = "expand text";
91 arg_ainfo.multiplechar = false;
92 arg_ainfo.defaultstatus = cgiarginfo::weak;
93 arg_ainfo.argdefault = "0";
94 arg_ainfo.savedarginfo = cgiarginfo::can;
95 argsinfo.addarginfo (NULL, arg_ainfo);
96
97 // in this action "gp" is the "go to page" control
98 // used by the Book type of toc
99 arg_ainfo.shortname = "gp";
100 arg_ainfo.longname = "go to page";
101 arg_ainfo.multiplechar = true;
102 arg_ainfo.defaultstatus = cgiarginfo::none;
103 arg_ainfo.argdefault = "";
104 arg_ainfo.savedarginfo = cgiarginfo::mustnot;
105 argsinfo.addarginfo (NULL, arg_ainfo);
106
107 // in this action "hl" is the "highlighting on/
108 // highlighting off control
109 arg_ainfo.shortname = "hl";
110 arg_ainfo.longname = "highlighting on/off";
111 arg_ainfo.multiplechar = false;
112 arg_ainfo.defaultstatus = cgiarginfo::weak;
113 arg_ainfo.argdefault = "1";
114 arg_ainfo.savedarginfo = cgiarginfo::must;
115 argsinfo.addarginfo (NULL, arg_ainfo);
116
117 // "x" is 0 normally or 1 if page
118 // has been "detached"
119 arg_ainfo.shortname = "x";
120 arg_ainfo.longname = "detached page";
121 arg_ainfo.multiplechar = false;
122 arg_ainfo.defaultstatus = cgiarginfo::weak;
123 arg_ainfo.argdefault = "0";
124 arg_ainfo.savedarginfo = cgiarginfo::must;
125 argsinfo.addarginfo (NULL, arg_ainfo);
126
127 // f arg is set to 1 if document is to
128 // be displayed in a frame
129 arg_ainfo.shortname = "f";
130 arg_ainfo.longname = "frame";
131 arg_ainfo.multiplechar = false;
132 arg_ainfo.defaultstatus = cgiarginfo::weak;
133 arg_ainfo.argdefault = "0";
134 arg_ainfo.savedarginfo = cgiarginfo::mustnot;
135 argsinfo.addarginfo (NULL, arg_ainfo);
136
137 // fc arg is "1" if search bar is to be included (i.e. if "fc" == 1
138 // the httpdocument macro will include "&f=1"
139 arg_ainfo.shortname = "fc";
140 arg_ainfo.longname = "include search bar";
141 arg_ainfo.multiplechar = false;
142 arg_ainfo.defaultstatus = cgiarginfo::weak;
143 arg_ainfo.argdefault = "1";
144 arg_ainfo.savedarginfo = cgiarginfo::must;
145 argsinfo.addarginfo (NULL, arg_ainfo);
146}
147
148documentaction::~documentaction () {
149}
150
151bool documentaction::check_cgiargs (cgiargsinfoclass &argsinfo, cgiargsclass &args,
152 ostream &logout) {
153
154 // check gc argument
155 int arg_gc = args.getintarg("gc");
156 if (arg_gc != 0 && arg_gc != 1) {
157 logout << "Warning: \"gc\" argument out of range (" << arg_gc << ")\n";
158 cgiarginfo *gcinfo = argsinfo.getarginfo ("gc");
159 if (gcinfo != NULL) args["gc"] = gcinfo->argdefault;
160 }
161
162 // check gt argument (may be either 0, 1 or 2)
163 int arg_gt = args.getintarg("gt");
164 if (arg_gt != 0 && arg_gt != 1 && arg_gt != 2) {
165 logout << "Warning: \"gt\" argument out of range (" << arg_gt << ")\n";
166 cgiarginfo *gtinfo = argsinfo.getarginfo ("gt");
167 if (gtinfo != NULL) args["gt"] = gtinfo->argdefault;
168 }
169
170 // check hl argument
171 int arg_hl = args.getintarg("hl");
172 if (arg_hl != 0 && arg_hl != 1) {
173 logout << "Warning: \"hl\" argument out of range (" << arg_hl << ")\n";
174 cgiarginfo *hlinfo = argsinfo.getarginfo ("hl");
175 if (hlinfo != NULL) args["hl"] = hlinfo->argdefault;
176 }
177
178 // check x argument
179 int arg_x = args.getintarg("x");
180 if (arg_x != 0 && arg_x != 1) {
181 logout << "Warning: \"x\" argument out of range (" << arg_x << ")\n";
182 cgiarginfo *xinfo = argsinfo.getarginfo ("x");
183 if (xinfo != NULL) args["x"] = xinfo->argdefault;
184 }
185
186 return true;
187}
188
189void documentaction::get_cgihead_info (cgiargsclass &args, recptprotolistclass *protos,
190 response_t &response,text_t &response_data,
191 ostream &logout) {
192
193 if ((args["il"] == "w") && (!args["d"].empty())) {
194
195 recptproto* collectproto = protos->getrecptproto (args["c"], logout);
196 if (collectproto != NULL) {
197
198 text_tset metadata;
199 FilterResponse_t filt_response;
200 text_t top;
201
202 metadata.insert ("URL");
203
204 // get metadata for parent document
205 get_top (args["d"], top);
206 if (get_info (top, args["c"], metadata, false, collectproto, filt_response, logout)) {
207 text_t url = filt_response.docInfo[0].metadata["URL"].values[0];
208
209 response = location;
210 response_data = url;
211 return;
212 } else {
213 // error, no URL
214 logout << "Error: documentaction::get_cgihead_info failed on get_info" << endl;
215 }
216 }
217 }
218 response = content;
219 response_data = "text/html";
220}
221
222// set_widthtspace calculates how wide the spaces in the nav bar should
223// be and sets the appropriate macro
224void documentaction::set_spacemacro (displayclass &disp, FilterResponse_t &response) {
225
226 text_t width;
227 int twidth, swidth, iwidth = 0;
228
229 int numc = response.docInfo.size();
230 ResultDocInfo_tarray::iterator dochere = response.docInfo.begin();
231 ResultDocInfo_tarray::iterator docend = response.docInfo.end();
232
233 disp.expandstring ("Global", "_pagewidth_", width);
234 twidth = width.getint();
235
236 disp.expandstring ("query", "_searchwidth_", width);
237 iwidth += width.getint();
238
239
240
241 while (dochere != docend) {
242 const text_t &title = (*dochere).metadata["Title"].values[0];
243
244 disp.expandstring ("document", "_" + title + "width_", width);
245 if (width == ("_" + title + "width_"))
246 disp.expandstring ("document", "_defaultwidth_", width);
247 iwidth += width.getint();
248 dochere ++;
249 }
250 if ((twidth - iwidth) < numc) swidth = 2;
251 else {
252 swidth = twidth - iwidth;
253 if (numc > 0) swidth = swidth / numc;
254 }
255 disp.setmacro ("widthtspace", "Global", swidth);
256}
257
258// set_navbarmacros sets _navigationbar_, _javaimagesnavbar_ and _httpbrowseXXX_ macros
259// reponse contains 1 metadata field (Title)
260void documentaction::set_navbarmacros (displayclass &disp, FilterResponse_t &response,
261 cgiargsclass &args) {
262
263 text_t javaimagesnavbar, topparent;
264 text_t &arg_d = args["d"];
265 text_t navigationbar = "<!-- Navigation Bar -->\n";
266
267 get_top (args["cl"], topparent);
268 int numc = response.docInfo.size();
269 ResultDocInfo_tarray::iterator dochere = response.docInfo.begin();
270 ResultDocInfo_tarray::iterator docend = response.docInfo.end();
271
272 navigationbar += "<nobr>\n";
273 if (args["a"] == "q") {
274 navigationbar += "_icontabsearchgreen_";
275 } else {
276 navigationbar += "_imagesearch_";
277 javaimagesnavbar = "_javasearch_";
278 }
279
280
281 if (numc == 0) navigationbar += "_imagespacer_";
282
283 while (dochere != docend) {
284 text_t title = (*dochere).metadata["Title"].values[0];
285
286 bool unknown = false;
287
288 // test the _XXXwidth_ macro to see if image macros are
289 // defined for this type of classification - if not we'll
290 // just display the text
291 text_t tmpwidth;
292 disp.expandstring ("document", "_" + title + "width_", tmpwidth);
293 if (tmpwidth == ("_" + title + "width_")) unknown = true;
294
295 // if we're inside a document all the classification buttons should be enabled
296 if (arg_d.empty() && ((*dochere).OID == topparent)) {
297 if (unknown) navigationbar += "_imagespacer_&nbsp;" + title + "&nbsp;";
298 else navigationbar += "_imagespacer__icontab" + title + "green_";
299 } else {
300
301 // set the _httpbrowseXXX_ macro for this classification
302 if (unknown) navigationbar += "_imagespacer_&nbsp;<a href=\"_httpdocument_&cl=" +
303 (*dochere).OID + "\">" + title + "</a>&nbsp;";
304 else {
305 navigationbar += "_imagespacer__image" + title + "_";
306 disp.setmacro ("httpbrowse" + title, "Global", "_httpdocument_&cl=" + (*dochere).OID);
307 }
308 }
309 if (!unknown) javaimagesnavbar += "_java" + title + "_";
310 dochere ++;
311 }
312 navigationbar += "\n</nobr>\n";
313 navigationbar += "<!-- End of Navigation Bar -->\n";
314 disp.setmacro ("navigationbar", "Global", navigationbar);
315 if (args.getintarg("v") == 0)
316 disp.setmacro ("javaimagesnavbar", "Global", javaimagesnavbar);
317}
318
319// define all the macros which might be used by other actions
320// to produce pages.
321void documentaction::define_external_macros (displayclass &disp, cgiargsclass &args,
322 recptprotolistclass *protos, ostream &logout) {
323
324 // define_external_macros sets the following macros:
325
326 // _navigationbar_ this is the navigation bar containing the search button
327 // and any classification buttons - it goes at the top of
328 // most pages. for now we're assuming that there'll always
329 // be a search button - we should probably check that there
330 // is a query action before making this assumption
331
332 // _httpbrowseXXX_ the http macros for each classification (i.e. if there
333 // are Title and Creator classifications _httpbrowseTitle_
334 // and _httpbrowseCreator_ will be set
335
336 // _javaimagesnavbar_ this is the javascript code to shove in to make the
337 // flashy images used by _navigationbar_ work
338
339 // _widthtspace_ the width of the spacers between buttons in navigation
340 // bar
341
342 // _httpdocument_ has '&f=1' added if displaying document inside a frame
343
344 // _gsdltop_ macro to replace _top targets with
345
346 // _httppagehome_ overridden home url if html collections have own homepage
347
348 // must have a valid collection server to continue
349 text_t &collection = args["c"];
350 if (collection.empty()) return;
351 recptproto *collectproto = protos->getrecptproto (collection, logout);
352 if (collectproto == NULL) return;
353
354 if (recpt == NULL) {
355 logout << "ERROR (documentaction::define_external_macros): This action does not contain\n"
356 << " information about any receptionists. The method set_receptionist was\n"
357 << " probably not called from the module which instantiated this action.\n";
358 return;
359 }
360
361 outconvertclass text_t2ascii;
362 comerror_t err;
363 InfoFiltersResponse_t filterinfo;
364 FilterResponse_t response;
365 text_tset metadata;
366
367 // get info on current collection and load up formatinfo
368 // I'd prefer not to do this here as we're getting
369 // collection info every time (and probably also getting
370 // it in other places some of the time) - One day I'll
371 // fix it ... maybe - Stefan.
372 ColInfoResponse_t cinfo;
373
374 collectproto->get_collectinfo (collection, cinfo, err, logout);
375 load_formatinfo (cinfo.format, args.getintarg("gt"));
376 // ColInfoResponse_t *cinfo = recpt->get_collectinfo_ptr (collectproto, collection, logout);
377 // if (cinfo == NULL) {
378 // logout << "ERROR (documentaction::define_external_macros): get_collectinfo_ptr returned NULL\n";
379 // return;
380 // }
381 //load_formatinfo (cinfo->format, args.getintarg("gt"));
382
383 if (formatinfo.DocumentUseHTML) {
384
385 // frame stuff
386 if (args["fc"] == "1") {
387 text_t httpdocument;
388 disp.expandstring ("Global", "_httpdocument_", httpdocument);
389 httpdocument += "&f=1";
390 disp.setmacro ("httpdocument", "Global", httpdocument);
391 disp.setmacro ("gsdltop", "Global", "documenttop");
392 }
393 text_tmap::iterator it = cinfo.format.find ("homepage");
394 if (it != cinfo.format.end()) {
395 text_t httppagehome;
396 if (get_link (args, protos, (*it).second, httppagehome, logout))
397 disp.setmacro ("httppagehome", "Global", httppagehome);
398 }
399 }
400
401 // don't want navigation bar if page is 'detached'
402 if (!args.getintarg("x")) {
403
404 collectproto->get_filterinfo (collection, filterinfo, err, logout);
405 if (err == noError) {
406 // check that there's a browse filter
407 if (filterinfo.filterNames.find ("BrowseFilter") != filterinfo.filterNames.end()) {
408
409 metadata.insert ("Title");
410 bool getParents = false;
411 get_children ("", collection, metadata, getParents, collectproto, response, logout);
412
413 // calculate width of spacers and set _widthtspace_ macro
414 if (args.getintarg("v") == 0) set_spacemacro (disp, response);
415
416 // set _navigationbar_ and _javaimagesnavbar_ macros
417 set_navbarmacros (disp, response, args);
418
419 }
420 } else {
421 logout << text_t2ascii
422 << "Error (documentaction::define_external_macros()) in call to get_filterinfo() "
423 << get_comerror_string (err);
424 }
425 }
426}
427
428bool documentaction::get_link (cgiargsclass &args, recptprotolistclass *protos,
429 const text_t &inlink, text_t &outlink, ostream &logout) {
430
431 FilterResponse_t response;
432 text_tset metadata;
433 metadata.insert ("section");
434
435 // check current collection first
436 recptproto *collectproto = protos->getrecptproto (args["c"], logout);
437
438 if (get_info (inlink, args["c"], metadata, false, collectproto, response, logout)) {
439 if (!response.docInfo[0].metadata["section"].values[0].empty()) {
440 outlink = "_httpdocument_&d=" + response.docInfo[0].metadata["section"].values[0];
441 return true;
442 }
443 }
444
445 // check all the other enabled collections
446
447 if (args["ccs"] == "1" && !args["cc"].empty()) {
448 text_tarray collections;
449 splitchar (args["cc"].begin(), args["cc"].end(), ',', collections);
450
451 text_tarray::const_iterator col_here = collections.begin();
452 text_tarray::const_iterator col_end = collections.end();
453
454 while (col_here != col_end) {
455
456 // don't need to check current collection again
457 if (*col_here == args["c"]) {col_here ++; continue;}
458
459 collectproto = protos->getrecptproto (*col_here, logout);
460
461 if (get_info (inlink, *col_here, metadata, false, collectproto, response, logout)) {
462 if (!response.docInfo[0].metadata["section"].values[0].empty()) {
463 outlink = "_httpdocument_&c=" + *col_here + "&d=" +
464 response.docInfo[0].metadata["section"].values[0];
465 return true;
466 }
467 }
468 col_here ++;
469 }
470 }
471 return false;
472}
473
474void documentaction::load_formatinfo (const text_tmap &colformat, int gt) {
475
476 formatinfo.clear();
477 text_tmap::const_iterator format_here = colformat.begin();
478 text_tmap::const_iterator format_end = colformat.end();
479
480 while (format_here != format_end) {
481 if (((*format_here).first == "DocumentImages") &&
482 ((*format_here).second == "true"))
483 formatinfo.DocumentImages = true;
484 else if (((*format_here).first == "DocumentTitles") &&
485 ((*format_here).second == "false"))
486 formatinfo.DocumentTitles = false;
487 else if ((*format_here).first == "DocumentHeading")
488 formatinfo.DocumentHeading = (*format_here).second;
489 else if (((*format_here).first == "DocumentContents") &&
490 ((*format_here).second == "false"))
491 formatinfo.DocumentContents = false;
492 else if (((*format_here).first == "DocumentArrowsBottom") &&
493 ((*format_here).second == "false"))
494 formatinfo.DocumentArrowsBottom = false;
495 else if ((*format_here).first == "DocumentButtons")
496 splitchar ((*format_here).second.begin(), (*format_here).second.end(),
497 '|', formatinfo.DocumentButtons);
498 else if ((*format_here).first == "DocumentText")
499 formatinfo.DocumentText = (*format_here).second;
500 else if (((*format_here).first == "DocumentUseHTML") &&
501 ((*format_here).second == "true"))
502 formatinfo.DocumentUseHTML = true;
503 else
504 formatinfo.formatstrings[(*format_here).first] = (*format_here).second;
505
506 format_here ++;
507 }
508
509 // never want arrows when text is expanded
510 if (gt) formatinfo.DocumentArrowsBottom = false;
511}
512
513void documentaction::set_java_macros (cgiargsclass &args, displayclass &disp) {
514
515 text_t javaimagescontent = "_javaextras_";
516
517 int arg_gt = args.getintarg("gt");
518 int arg_gc = args.getintarg("gc");
519 int arg_hl = args.getintarg("hl");
520
521 text_tarray::const_iterator button_here = formatinfo.DocumentButtons.begin();
522 text_tarray::const_iterator button_end = formatinfo.DocumentButtons.end();
523
524 while (button_here != button_end) {
525 if (*button_here == "Detach")
526 javaimagescontent += "_javadetach_";
527 else if (*button_here == "Expand Text") {
528 if (arg_gt == 1)
529 javaimagescontent += "_javacontracttext__javacontinue_";
530 else if (arg_gt == 2)
531 javaimagescontent += "_javacontracttext_";
532 else
533 javaimagescontent += "_javaexpandtext_";
534 } else if (*button_here == "Expand Contents") {
535 if (arg_gc == 1)
536 javaimagescontent += "_javacontractcontents_";
537 else
538 javaimagescontent += "_javaexpandcontents_";
539 } else if (*button_here == "Highlight") {
540 if (arg_hl == 1)
541 javaimagescontent += "_javanohighlighting_";
542 else
543 javaimagescontent += "_javahighlighting_";
544 }
545 button_here ++;
546 }
547 disp.setmacro ("javaimagescontent", "document", javaimagescontent);
548}
549
550
551// define all the macros which are related to pages generated
552// by this action. we also load up the formatinfo structure
553// here (it's used in do_action as well as here)
554void documentaction::define_internal_macros (displayclass &disp, cgiargsclass &args,
555 recptprotolistclass *protos, ostream &logout) {
556
557 // define_internal_macros sets the following macros:
558
559 // _pagetitle_ the title to be displayed at the top of the browser window
560
561 // _imagethispage_ the title image to be displayed at top right of page
562
563 // _navarrows_ this may be overridden to "" when format option
564 // DocumentArrowsBottom is false
565
566 // _header_ the header macro is overridden if we're not at a top level
567 // classification to remove the title block
568
569 // _javaimagescontent_ this is the javascript code to shove in to make the
570 // flashy buttons work
571
572 // _thisOID_ the OID (directory) of the current document - this corresponds
573 // to the archivedir metadata element
574
575 // must have a valid collection server to continue
576 text_t &collection = args["c"];
577 if (collection.empty()) return;
578 recptproto *collectproto = protos->getrecptproto (collection, logout);
579 if (collectproto == NULL) return;
580
581 text_tset metadata;
582 FilterResponse_t response;
583 text_t &arg_d = args["d"];
584 text_t &arg_cl = args["cl"];
585
586 if (!formatinfo.DocumentArrowsBottom)
587 disp.setmacro("navarrows", "document", "");
588
589 metadata.insert ("Title");
590
591 bool fulltoc = false;
592
593 if (args["cl"] != "search") {
594 // see if there's a FullTOC string
595 text_t cl_top, full_toc;
596 get_top (arg_cl, cl_top);
597 if (get_formatstring (cl_top, "FullTOC", formatinfo.formatstrings, full_toc))
598 if (full_toc == "true") fulltoc = true;
599 }
600
601 if (!arg_d.empty() && !fulltoc) {
602 // we're at document level
603
604 metadata.insert ("archivedir");
605
606 OptionValue_tarray options;
607 // we need to know what the query was for the z3950proto
608 if (collectproto->get_protocol_name()=="z3950proto") {
609 OptionValue_t opt;
610 opt.name="Query";
611 opt.value=args["q"];
612 options.push_back(opt);
613 }
614
615 // get metadata for this document and it's parents
616 if (get_info (arg_d, collection, metadata, options,
617 true, collectproto, response, logout)) {
618 disp.setmacro ("header", "document", "_textheader_");
619
620 text_tarray pagetitlearray;
621 if (!response.docInfo[0].metadata["Title"].values[0].empty())
622 pagetitlearray.push_back (response.docInfo[0].metadata["Title"].values[0]);
623 if (args["gt"] != "1") {
624 MetadataInfo_t *parenttitle = response.docInfo[0].metadata["Title"].parent;
625 while (parenttitle != NULL) {
626 if (!parenttitle->values[0].empty())
627 pagetitlearray.push_back (parenttitle->values[0]);
628 parenttitle = parenttitle->parent;
629 }
630 }
631 reverse (pagetitlearray.begin(), pagetitlearray.end());
632 text_t pagetitle;
633 joinchar (pagetitlearray, ": ", pagetitle);
634 disp.setmacro ("pagetitle", "document", pagetitle);
635
636 if (is_top (arg_d))
637 disp.setmacro ("thisOID", "Global", dm_safe(response.docInfo[0].metadata["archivedir"].values[0]));
638 else {
639 MetadataInfo_t *parentad = response.docInfo[0].metadata["archivedir"].parent;
640 text_t thisOID;
641 while (parentad != NULL) {
642 thisOID = parentad->values[0];
643 parentad = parentad->parent;
644 }
645 disp.setmacro ("thisOID", "Global", dm_safe(thisOID));
646 }
647
648 if (args["u"] != "1")
649 set_java_macros (args, disp);
650
651 }
652 } else {
653 if (!arg_cl.empty()) {
654
655 // get metadata for top level classification
656 text_t classtop;
657 get_top (arg_cl, classtop);
658 if (get_info (classtop, collection, metadata, false, collectproto, response, logout)) {
659
660 text_t &title = response.docInfo[0].metadata["Title"].values[0];
661 bool unknown = false;
662
663 // test the _XXXwidth_ macro to see if image macros are
664 // defined for this type of classification - if not we'll
665 // just display the text
666 text_t tmp;
667 disp.expandstring ("document", "_" + title + "width_", tmp);
668 if (tmp == ("_" + title + "width_")) unknown = true;
669
670 if (unknown) {
671 disp.setmacro ("pagetitle", "document", title);
672 disp.setmacro ("imagethispage", "document", "<h2>" + title + "</h2>");
673 } else {
674 disp.setmacro ("pagetitle", "document", "_text" + title + "page_");
675 disp.setmacro ("imagethispage", "document", "_icon" + title + "page_");
676 }
677 }
678 }
679 }
680}
681
682
683bool documentaction::do_action (cgiargsclass &args, recptprotolistclass *protos,
684 browsermapclass *browsers, displayclass &disp,
685 outconvertclass &outconvert, ostream &textout,
686 ostream &logout) {
687
688
689 // must have a valid collection server
690 recptproto *collectproto = protos->getrecptproto (args["c"], logout);
691 if (collectproto == NULL) {
692 logout << "documentaction::do_action called with NULL collectproto\n";
693 textout << outconvert << disp << "_document:header_\n"
694 << "Error: Attempt to get document without setting collection\n"
695 << "_document:footer_\n";
696 } else {
697
698 text_t OID = args["d"];
699 if (OID.empty()) OID = args["cl"];
700 if (OID.empty()) {
701 textout << outconvert << disp << "Document contains no data_document:footer_\n";
702 return true;
703 }
704
705 if (formatinfo.DocumentUseHTML) {
706
707 if (!args["d"].empty()) {
708 if (args["f"] == "1") {
709 textout << outconvert << disp
710 << "<html><head></head>\n"
711 << "<frameset rows=\"68,*\" noresize border=0>\n"
712 << "<frame scrolling=no frameborder=0 src=\"_gwcgi_?e=_compressedoptions_&a=p&p=nav\">\n"
713 << "<frame name=\"documenttop\" frameborder=0 src=\"_gwcgi_?e=_compressedoptions_&a=d&d="
714 << args["d"] << "\">"
715 << "<noframes>\n"
716 << "<p>You must have a frame enabled browser to view this.</p>\n"
717 << "</noframes>\n"
718 << "</frameset>\n"
719 << "</html>\n";
720 } else {
721 output_document (OID, args, collectproto, disp, outconvert, textout, logout);
722 }
723 return true;
724 }
725 }
726
727 textout << outconvert << disp << "_document:header_\n"
728 << "_document:content_\n";
729
730 // output the table of contents
731 output_toc (args, browsers, formatinfo, collectproto,
732 disp, outconvert, textout, logout);
733
734 // output the document text
735 textout << "<p>\n";
736 output_document (OID, args, collectproto, disp, outconvert, textout, logout);
737
738 textout << outconvert << disp << "_document:footer_\n";
739 }
740 return true;
741}
742
743void documentaction::output_text (ResultDocInfo_t &docinfo, format_t *formatlistptr,
744 const TermInfo_tarray &terminfo, const text_t &OID,
745 bool highlight, int hastxt, int wanttext,
746 text_t &collection, recptproto *collectproto,
747 displayclass &disp, outconvertclass &outconvert,
748 ostream &textout, ostream &logout) {
749
750 DocumentRequest_t docrequest;
751 DocumentResponse_t docresponse;
752 comerror_t err;
753
754 if (hastxt == 1) {
755
756 if (wanttext) {
757 // get the text
758 docrequest.OID = OID;
759 collectproto->get_document (collection, docrequest, docresponse, err, logout);
760
761 // cut down on overhead by not using formattools if we only want the text
762 // (wanttext will equal 2 if we want text and other stuff too)
763 if (wanttext == 1)
764 if (highlight)
765 highlighttext(docresponse.doc, terminfo, disp, outconvert, textout, logout);
766 else
767 textout << outconvert << disp << docresponse.doc;
768 }
769
770 if (wanttext != 1) {
771 text_t doctext = get_formatted_string (docinfo, formatlistptr, docresponse.doc);
772
773 if (highlight)
774 highlighttext(doctext, terminfo, disp, outconvert, textout, logout);
775 else
776 textout << outconvert << disp << doctext;
777 }
778 }
779}
780
781
782void documentaction::output_document (const text_t &OID, cgiargsclass &args,
783 recptproto *collectproto, displayclass &disp,
784 outconvertclass &outconvert, ostream &textout,
785 ostream &logout) {
786 FilterResponse_t inforesponse;
787 FilterResponse_t queryresponse;
788 text_tset metadata;
789 bool getParents = false;
790 bool highlight = false;
791 int wanttext = 0;
792 int arg_gt = args.getintarg("gt");
793 text_t &collection = args["c"];
794
795 // if we have a query string and highlighting is turned on we need
796 // to redo the query to get the terms for highlighting
797 if (!args["q"].empty() && args.getintarg("hl")) {
798 FilterRequest_t request;
799 comerror_t err;
800 request.filterResultOptions = FRmatchTerms;
801 text_t formattedstring = args["q"];
802 format_querystring (formattedstring, args.getintarg("b"));
803 set_queryfilter_options (request, formattedstring, args);
804 collectproto->filter (args["c"], request, queryresponse, err, logout);
805 if (err != noError) {
806 outconvertclass text_t2ascii;
807 logout << text_t2ascii
808 << "documentaction::output_document: call to QueryFilter failed "
809 << "for " << args["c"] << " collection (" << get_comerror_string (err) << ")\n";
810 highlight = false;
811 } else {
812 highlight = true;
813 }
814 }
815
816 format_t *formatlistptr = new format_t();
817 parse_formatstring (formatinfo.DocumentText, formatlistptr, metadata, getParents);
818
819 metadata.insert ("hastxt");
820 metadata.insert ("haschildren");
821
822 if (formatinfo.DocumentText == "[Text]")
823 wanttext = 1;
824 else {
825 char *docformat = formatinfo.DocumentText.getcstr();
826 if (strstr (docformat, "[Text]") != NULL)
827 wanttext = 2;
828 delete docformat;
829 }
830
831 if (get_info (OID, collection, metadata, getParents, collectproto, inforesponse, logout)) {
832 int hastxt = inforesponse.docInfo[0].metadata["hastxt"].values[0].getint();
833 int haschildren = inforesponse.docInfo[0].metadata["haschildren"].values[0].getint();
834
835 if (arg_gt == 0) {
836 output_text (inforesponse.docInfo[0], formatlistptr, queryresponse.termInfo,
837 OID, highlight, hastxt, wanttext, collection, collectproto,
838 disp, outconvert, textout, logout);
839
840
841 } else {
842
843 ResultDocInfo_t thisdocinfo = inforesponse.docInfo[0];
844
845 // text is to be expanded
846 text_t exOID = OID;
847 if (haschildren != 1) exOID = get_parent (OID);
848 if (exOID.empty()) exOID = OID;
849
850 // if we're not in a document (i.e. we're in a top level classification)
851 // we need to pass "is_classify = true" to get_contents so that it
852 // doesn't recurse all the way through each document in the classification
853 bool is_classify = false;
854 if (args["d"].empty()) is_classify = true;
855
856 get_contents (exOID, is_classify, metadata, collection,
857 collectproto, inforesponse, logout);
858
859 ResultDocInfo_tarray::iterator sechere = inforesponse.docInfo.begin();
860 ResultDocInfo_tarray::iterator secend = inforesponse.docInfo.end();
861
862 if (arg_gt == 1) {
863 // check if there are more than 10 sections containing text to be expanded -
864 // if there are output warning message - this isn't a great way to do this
865 // since the sections may be very large or very small - one day I'll fix it
866 // -- Stefan.
867 int seccount = 0;
868 while (sechere != secend) {
869 int shastxt = (*sechere).metadata["hastxt"].values[0].getint();
870 if (shastxt == 1) seccount ++;
871 if (seccount > 10) break;
872 sechere ++;
873 }
874 if (seccount > 10) {
875 // more than 10 sections so output warning message and text
876 // for current section only
877 textout << outconvert << disp << "_document:textltwarning_";
878
879 output_text (thisdocinfo, formatlistptr, queryresponse.termInfo,
880 OID, highlight, hastxt, wanttext, collection,
881 collectproto, disp, outconvert, textout, logout);
882
883 }
884 else arg_gt = 2;
885 }
886
887 if (arg_gt == 2) {
888 // get the text for each section
889 sechere = inforesponse.docInfo.begin();
890 int count = 0;
891 while (sechere != secend) {
892 textout << outconvert << disp << "\n<p><a name=" << count << "></a>\n";
893
894 int shastxt = (*sechere).metadata["hastxt"].values[0].getint();
895
896 output_text (*sechere, formatlistptr, queryresponse.termInfo,
897 (*sechere).OID, highlight, shastxt, wanttext, collection,
898 collectproto, disp, outconvert, textout, logout);
899 count ++;
900 sechere ++;
901 }
902 }
903 }
904 }
905 delete formatlistptr;
906}
907
908// highlighttext highlights query terms in text string and outputs the resulting text string
909void documentaction::highlighttext(text_t &text, const TermInfo_tarray &terms,
910 displayclass &disp, outconvertclass &outconvert,
911 ostream &textout, ostream &/*logout*/) {
912
913 text_tmap allterms;
914 text_tmap::const_iterator it;
915
916 // first load all the term variations into a map
917 TermInfo_tarray::const_iterator this_term = terms.begin();
918 TermInfo_tarray::const_iterator last_term = terms.end();
919 while (this_term != last_term) {
920 text_tarray::const_iterator this_var = (*this_term).matchTerms.begin();
921 text_tarray::const_iterator last_var = (*this_term).matchTerms.end();
922 while (this_var != last_var) {
923 allterms[*this_var] = 1;
924 this_var ++;
925 }
926 this_term ++;
927 }
928
929 // get the text to start and end a hightlight
930 text_t starthighlight = "<b><u>";
931 text_t endhighlight = "</u></b>";
932 if (disp.isdefaultmacro("Global", "starthighlight"))
933 disp.expandstring("Global", "_starthighlight_", starthighlight);
934 if (disp.isdefaultmacro("Global", "endhighlight"))
935 disp.expandstring("Global", "_endhighlight_", endhighlight);
936
937
938 text_t::iterator here = text.begin();
939 text_t::iterator end = text.end();
940 text_t word, buffer;
941 while (here != end) {
942 if (is_unicode_letdig(*here)) {
943 // not word boundary
944 word.push_back(*here);
945 here++;
946
947 } else {
948 // found word boundary
949 // add last word if there was one
950 if (!word.empty()) {
951 it = allterms.find(word);
952 if (it != allterms.end()) {
953 word = starthighlight + word + endhighlight;
954 }
955 buffer += word;
956 word.clear();
957 }
958
959 if (*here == '<') {
960 // skip over rest of html tag
961 while ((here != end) && (*here != '>')) {
962 buffer.push_back(*here);
963 here++;
964 }
965 }
966
967 buffer.push_back(*here);
968 here++;
969
970 if (buffer.size() > 1024) {
971 textout << outconvert << disp << buffer;
972 buffer.clear();
973 }
974 }
975 }
976 textout << outconvert << disp << buffer;
977}
Note: See TracBrowser for help on using the repository browser.