source: trunk/gsdl/src/recpt/documentaction.cpp@ 1285

Last change on this file since 1285 was 1285, checked in by sjboddie, 24 years ago

Removed CVS logging information from source files

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 32.1 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 ColInfoResponse_t *cinfo = recpt->get_collectinfo_ptr (collectproto, collection, logout);
368 if (cinfo == NULL) {
369 logout << "ERROR (documentaction::define_external_macros): get_collectinfo_ptr returned NULL\n";
370 return;
371 }
372 load_formatinfo (cinfo->format, args.getintarg("gt"));
373
374 if (formatinfo.DocumentUseHTML) {
375
376 // frame stuff
377 if (args["fc"] == "1") {
378 text_t httpdocument;
379 disp.expandstring ("Global", "_httpdocument_", httpdocument);
380 httpdocument += "&f=1";
381 disp.setmacro ("httpdocument", "Global", httpdocument);
382 disp.setmacro ("gsdltop", "Global", "documenttop");
383 }
384 text_tmap::iterator it = cinfo->format.find ("homepage");
385 if (it != cinfo->format.end()) {
386 text_t httppagehome;
387 if (get_link (args, protos, (*it).second, httppagehome, logout))
388 disp.setmacro ("httppagehome", "Global", httppagehome);
389 }
390 }
391
392 // don't want navigation bar if page is 'detached'
393 if (!args.getintarg("x")) {
394
395 collectproto->get_filterinfo (collection, filterinfo, err, logout);
396 if (err == noError) {
397 // check that there's a browse filter
398 if (filterinfo.filterNames.find ("BrowseFilter") != filterinfo.filterNames.end()) {
399
400 metadata.insert ("Title");
401 bool getParents = false;
402 get_children ("", collection, metadata, getParents, collectproto, response, logout);
403
404 // calculate width of spacers and set _widthtspace_ macro
405 if (args.getintarg("v") == 0) set_spacemacro (disp, response);
406
407 // set _navigationbar_ and _javaimagesnavbar_ macros
408 set_navbarmacros (disp, response, args);
409
410 }
411 } else {
412 logout << text_t2ascii
413 << "Error (documentaction::define_external_macros()) in call to get_filterinfo() "
414 << get_comerror_string (err);
415 }
416 }
417}
418
419bool documentaction::get_link (cgiargsclass &args, recptprotolistclass *protos,
420 const text_t &inlink, text_t &outlink, ostream &logout) {
421
422 FilterResponse_t response;
423 text_tset metadata;
424 metadata.insert ("section");
425
426 // check current collection first
427 recptproto *collectproto = protos->getrecptproto (args["c"], logout);
428
429 if (get_info (inlink, args["c"], metadata, false, collectproto, response, logout)) {
430 if (!response.docInfo[0].metadata["section"].values[0].empty()) {
431 outlink = "_httpdocument_&d=" + response.docInfo[0].metadata["section"].values[0];
432 return true;
433 }
434 }
435
436 // check all the other enabled collections
437
438 if (args["ccs"] == "1" && !args["cc"].empty()) {
439 text_tarray collections;
440 splitchar (args["cc"].begin(), args["cc"].end(), ',', collections);
441
442 text_tarray::const_iterator col_here = collections.begin();
443 text_tarray::const_iterator col_end = collections.end();
444
445 while (col_here != col_end) {
446
447 // don't need to check current collection again
448 if (*col_here == args["c"]) {col_here ++; continue;}
449
450 collectproto = protos->getrecptproto (*col_here, logout);
451
452 if (get_info (inlink, *col_here, metadata, false, collectproto, response, logout)) {
453 if (!response.docInfo[0].metadata["section"].values[0].empty()) {
454 outlink = "_httpdocument_&c=" + *col_here + "&d=" +
455 response.docInfo[0].metadata["section"].values[0];
456 return true;
457 }
458 }
459 col_here ++;
460 }
461 }
462 return false;
463}
464
465void documentaction::load_formatinfo (const text_tmap &colformat, int gt) {
466
467 formatinfo.clear();
468 text_tmap::const_iterator format_here = colformat.begin();
469 text_tmap::const_iterator format_end = colformat.end();
470
471 while (format_here != format_end) {
472 if (((*format_here).first == "DocumentImages") &&
473 ((*format_here).second == "true"))
474 formatinfo.DocumentImages = true;
475 else if (((*format_here).first == "DocumentTitles") &&
476 ((*format_here).second == "false"))
477 formatinfo.DocumentTitles = false;
478 else if ((*format_here).first == "DocumentHeading")
479 formatinfo.DocumentHeading = (*format_here).second;
480 else if (((*format_here).first == "DocumentContents") &&
481 ((*format_here).second == "false"))
482 formatinfo.DocumentContents = false;
483 else if (((*format_here).first == "DocumentArrowsBottom") &&
484 ((*format_here).second == "false"))
485 formatinfo.DocumentArrowsBottom = false;
486 else if ((*format_here).first == "DocumentButtons")
487 splitchar ((*format_here).second.begin(), (*format_here).second.end(),
488 '|', formatinfo.DocumentButtons);
489 else if ((*format_here).first == "DocumentText")
490 formatinfo.DocumentText = (*format_here).second;
491 else if (((*format_here).first == "DocumentUseHTML") &&
492 ((*format_here).second == "true"))
493 formatinfo.DocumentUseHTML = true;
494 else
495 formatinfo.formatstrings[(*format_here).first] = (*format_here).second;
496
497 format_here ++;
498 }
499
500 // never want arrows when text is expanded
501 if (gt) formatinfo.DocumentArrowsBottom = false;
502}
503
504void documentaction::set_java_macros (cgiargsclass &args, displayclass &disp) {
505
506 text_t javaimagescontent = "_javaextras_";
507
508 int arg_gt = args.getintarg("gt");
509 int arg_gc = args.getintarg("gc");
510 int arg_hl = args.getintarg("hl");
511
512 text_tarray::const_iterator button_here = formatinfo.DocumentButtons.begin();
513 text_tarray::const_iterator button_end = formatinfo.DocumentButtons.end();
514
515 while (button_here != button_end) {
516 if (*button_here == "Detach")
517 javaimagescontent += "_javadetach_";
518 else if (*button_here == "Expand Text") {
519 if (arg_gt == 1)
520 javaimagescontent += "_javacontracttext__javacontinue_";
521 else if (arg_gt == 2)
522 javaimagescontent += "_javacontracttext_";
523 else
524 javaimagescontent += "_javaexpandtext_";
525 } else if (*button_here == "Expand Contents") {
526 if (arg_gc == 1)
527 javaimagescontent += "_javacontractcontents_";
528 else
529 javaimagescontent += "_javaexpandcontents_";
530 } else if (*button_here == "Highlight") {
531 if (arg_hl == 1)
532 javaimagescontent += "_javanohighlighting_";
533 else
534 javaimagescontent += "_javahighlighting_";
535 }
536 button_here ++;
537 }
538 disp.setmacro ("javaimagescontent", "document", javaimagescontent);
539}
540
541
542// define all the macros which are related to pages generated
543// by this action. we also load up the formatinfo structure
544// here (it's used in do_action as well as here)
545void documentaction::define_internal_macros (displayclass &disp, cgiargsclass &args,
546 recptprotolistclass *protos, ostream &logout) {
547
548 // define_internal_macros sets the following macros:
549
550 // _pagetitle_ the title to be displayed at the top of the browser window
551
552 // _imagethispage_ the title image to be displayed at top right of page
553
554 // _navarrows_ this may be overridden to "" when format option
555 // DocumentArrowsBottom is false
556
557 // _header_ the header macro is overridden if we're not at a top level
558 // classification to remove the title block
559
560 // _javaimagescontent_ this is the javascript code to shove in to make the
561 // flashy buttons work
562
563 // _thisOID_ the OID (directory) of the current document - this corresponds
564 // to the archivedir metadata element
565
566 // must have a valid collection server to continue
567 text_t &collection = args["c"];
568 if (collection.empty()) return;
569 recptproto *collectproto = protos->getrecptproto (collection, logout);
570 if (collectproto == NULL) return;
571
572 text_tset metadata;
573 FilterResponse_t response;
574 text_t &arg_d = args["d"];
575 text_t &arg_cl = args["cl"];
576
577 if (!formatinfo.DocumentArrowsBottom)
578 disp.setmacro("navarrows", "document", "");
579
580 metadata.insert ("Title");
581
582 bool fulltoc = false;
583
584 if (args["cl"] != "search") {
585 // see if there's a FullTOC string
586 text_t cl_top, full_toc;
587 get_top (arg_cl, cl_top);
588 if (get_formatstring (cl_top, "FullTOC", formatinfo.formatstrings, full_toc))
589 if (full_toc == "true") fulltoc = true;
590 }
591
592 if (!arg_d.empty() && !fulltoc) {
593 // we're at document level
594
595 metadata.insert ("archivedir");
596
597 // get metadata for this document and it's parents
598 if (get_info (arg_d, collection, metadata, true, collectproto, response, logout)) {
599 disp.setmacro ("header", "document", "_textheader_");
600
601 text_tarray pagetitlearray;
602 if (!response.docInfo[0].metadata["Title"].values[0].empty())
603 pagetitlearray.push_back (response.docInfo[0].metadata["Title"].values[0]);
604 if (args["gt"] != "1") {
605 MetadataInfo_t *parenttitle = response.docInfo[0].metadata["Title"].parent;
606 while (parenttitle != NULL) {
607 if (!parenttitle->values[0].empty())
608 pagetitlearray.push_back (parenttitle->values[0]);
609 parenttitle = parenttitle->parent;
610 }
611 }
612 reverse (pagetitlearray.begin(), pagetitlearray.end());
613 text_t pagetitle;
614 joinchar (pagetitlearray, ": ", pagetitle);
615 disp.setmacro ("pagetitle", "document", pagetitle);
616
617 if (is_top (arg_d))
618 disp.setmacro ("thisOID", "Global", dm_safe(response.docInfo[0].metadata["archivedir"].values[0]));
619 else {
620 MetadataInfo_t *parentad = response.docInfo[0].metadata["archivedir"].parent;
621 text_t thisOID;
622 while (parentad != NULL) {
623 thisOID = parentad->values[0];
624 parentad = parentad->parent;
625 }
626 disp.setmacro ("thisOID", "Global", dm_safe(thisOID));
627 }
628
629 if (args["u"] != "1")
630 set_java_macros (args, disp);
631
632 }
633 } else {
634 if (!arg_cl.empty()) {
635
636 // get metadata for top level classification
637 text_t classtop;
638 get_top (arg_cl, classtop);
639 if (get_info (classtop, collection, metadata, false, collectproto, response, logout)) {
640
641 text_t &title = response.docInfo[0].metadata["Title"].values[0];
642 bool unknown = false;
643
644 // test the _XXXwidth_ macro to see if image macros are
645 // defined for this type of classification - if not we'll
646 // just display the text
647 text_t tmp;
648 disp.expandstring ("document", "_" + title + "width_", tmp);
649 if (tmp == ("_" + title + "width_")) unknown = true;
650
651 if (unknown) {
652 disp.setmacro ("pagetitle", "document", title);
653 disp.setmacro ("imagethispage", "document", "<h2>" + title + "</h2>");
654 } else {
655 disp.setmacro ("pagetitle", "document", "_text" + title + "page_");
656 disp.setmacro ("imagethispage", "document", "_icon" + title + "page_");
657 }
658 }
659 }
660 }
661}
662
663
664bool documentaction::do_action (cgiargsclass &args, recptprotolistclass *protos,
665 browsermapclass *browsers, displayclass &disp,
666 outconvertclass &outconvert, ostream &textout,
667 ostream &logout) {
668
669
670 // must have a valid collection server
671 recptproto *collectproto = protos->getrecptproto (args["c"], logout);
672 if (collectproto == NULL) {
673 logout << "documentaction::do_action called with NULL collectproto\n";
674 textout << outconvert << disp << "_document:header_\n"
675 << "Error: Attempt to get document without setting collection\n"
676 << "_document:footer_\n";
677 } else {
678
679 text_t OID = args["d"];
680 if (OID.empty()) OID = args["cl"];
681 if (OID.empty()) {
682 textout << outconvert << disp << "Document contains no data_document:footer_\n";
683 return true;
684 }
685
686 if (formatinfo.DocumentUseHTML) {
687
688 if (!args["d"].empty()) {
689 if (args["f"] == "1") {
690 textout << outconvert << disp
691 << "<html><head></head>\n"
692 << "<frameset rows=\"68,*\" noresize border=0>\n"
693 << "<frame scrolling=no frameborder=0 src=\"_gwcgi_?e=_compressedoptions_&a=p&p=nav\">\n"
694 << "<frame name=\"documenttop\" frameborder=0 src=\"_gwcgi_?e=_compressedoptions_&a=d&d="
695 << args["d"] << "\">"
696 << "<noframes>\n"
697 << "<p>You must have a frame enabled browser to view this.</p>\n"
698 << "</noframes>\n"
699 << "</frameset>\n"
700 << "</html>\n";
701 } else {
702 output_document (OID, args, collectproto, disp, outconvert, textout, logout);
703 }
704 return true;
705 }
706 }
707
708 textout << outconvert << disp << "_document:header_\n"
709 << "_document:content_\n";
710
711 // output the table of contents
712 output_toc (args, browsers, formatinfo, collectproto,
713 disp, outconvert, textout, logout);
714
715 // output the document text
716 textout << "<p>\n";
717 output_document (OID, args, collectproto, disp, outconvert, textout, logout);
718
719 textout << outconvert << disp << "_document:footer_\n";
720 }
721 return true;
722}
723
724void documentaction::output_text (ResultDocInfo_t &docinfo, format_t *formatlistptr,
725 const TermInfo_tarray &terminfo, const text_t &OID,
726 bool highlight, int hastxt, int wanttext,
727 text_t &collection, recptproto *collectproto,
728 displayclass &disp, outconvertclass &outconvert,
729 ostream &textout, ostream &logout) {
730
731 DocumentRequest_t docrequest;
732 DocumentResponse_t docresponse;
733 comerror_t err;
734
735 if (hastxt == 1) {
736
737 if (wanttext) {
738 // get the text
739 docrequest.OID = OID;
740 collectproto->get_document (collection, docrequest, docresponse, err, logout);
741
742 // cut down on overhead by not using formattools if we only want the text
743 // (wanttext will equal 2 if we want text and other stuff too)
744 if (wanttext == 1)
745 if (highlight)
746 highlighttext(docresponse.doc, terminfo, disp, outconvert, textout, logout);
747 else
748 textout << outconvert << disp << docresponse.doc;
749 }
750
751 if (wanttext != 1) {
752 text_t doctext = get_formatted_string (docinfo, formatlistptr, docresponse.doc);
753
754 if (highlight)
755 highlighttext(doctext, terminfo, disp, outconvert, textout, logout);
756 else
757 textout << outconvert << disp << doctext;
758 }
759 }
760}
761
762
763void documentaction::output_document (const text_t &OID, cgiargsclass &args,
764 recptproto *collectproto, displayclass &disp,
765 outconvertclass &outconvert, ostream &textout,
766 ostream &logout) {
767 FilterResponse_t inforesponse;
768 FilterResponse_t queryresponse;
769 text_tset metadata;
770 bool getParents = false;
771 bool highlight = false;
772 int wanttext = 0;
773 int arg_gt = args.getintarg("gt");
774 text_t &collection = args["c"];
775
776 // if we have a query string and highlighting is turned on we need
777 // to redo the query to get the terms for highlighting
778 if (!args["q"].empty() && args.getintarg("hl")) {
779 FilterRequest_t request;
780 comerror_t err;
781 request.filterResultOptions = FRmatchTerms;
782 text_t formattedstring = args["q"];
783 format_querystring (formattedstring, args.getintarg("b"));
784 set_queryfilter_options (request, formattedstring, args);
785 collectproto->filter (args["c"], request, queryresponse, err, logout);
786 if (err != noError) {
787 outconvertclass text_t2ascii;
788 logout << text_t2ascii
789 << "documentaction::output_document: call to QueryFilter failed "
790 << "for " << args["c"] << " collection (" << get_comerror_string (err) << ")\n";
791 highlight = false;
792 } else {
793 highlight = true;
794 }
795 }
796
797 format_t *formatlistptr = new format_t();
798 parse_formatstring (formatinfo.DocumentText, formatlistptr, metadata, getParents);
799
800 metadata.insert ("hastxt");
801 metadata.insert ("haschildren");
802
803 if (formatinfo.DocumentText == "[Text]")
804 wanttext = 1;
805 else {
806 char *docformat = formatinfo.DocumentText.getcstr();
807 if (strstr (docformat, "[Text]") != NULL)
808 wanttext = 2;
809 delete docformat;
810 }
811
812 if (get_info (OID, collection, metadata, getParents, collectproto, inforesponse, logout)) {
813 int hastxt = inforesponse.docInfo[0].metadata["hastxt"].values[0].getint();
814 int haschildren = inforesponse.docInfo[0].metadata["haschildren"].values[0].getint();
815
816 if (arg_gt == 0) {
817 output_text (inforesponse.docInfo[0], formatlistptr, queryresponse.termInfo,
818 OID, highlight, hastxt, wanttext, collection, collectproto,
819 disp, outconvert, textout, logout);
820
821
822 } else {
823
824 ResultDocInfo_t thisdocinfo = inforesponse.docInfo[0];
825
826 // text is to be expanded
827 text_t exOID = OID;
828 if (haschildren != 1) exOID = get_parent (OID);
829 if (exOID.empty()) exOID = OID;
830
831 // if we're not in a document (i.e. we're in a top level classification)
832 // we need to pass "is_classify = true" to get_contents so that it
833 // doesn't recurse all the way through each document in the classification
834 bool is_classify = false;
835 if (args["d"].empty()) is_classify = true;
836
837 get_contents (exOID, is_classify, metadata, collection,
838 collectproto, inforesponse, logout);
839
840 ResultDocInfo_tarray::iterator sechere = inforesponse.docInfo.begin();
841 ResultDocInfo_tarray::iterator secend = inforesponse.docInfo.end();
842
843 if (arg_gt == 1) {
844 // check if there are more than 10 sections containing text to be expanded -
845 // if there are output warning message - this isn't a great way to do this
846 // since the sections may be very large or very small - one day I'll fix it
847 // -- Stefan.
848 int seccount = 0;
849 while (sechere != secend) {
850 int shastxt = (*sechere).metadata["hastxt"].values[0].getint();
851 if (shastxt == 1) seccount ++;
852 if (seccount > 10) break;
853 sechere ++;
854 }
855 if (seccount > 10) {
856 // more than 10 sections so output warning message and text
857 // for current section only
858 textout << outconvert << disp << "_document:textltwarning_";
859
860 output_text (thisdocinfo, formatlistptr, queryresponse.termInfo,
861 OID, highlight, hastxt, wanttext, collection,
862 collectproto, disp, outconvert, textout, logout);
863
864 }
865 else arg_gt = 2;
866 }
867
868 if (arg_gt == 2) {
869 // get the text for each section
870 sechere = inforesponse.docInfo.begin();
871 int count = 0;
872 while (sechere != secend) {
873 textout << outconvert << disp << "\n<p><a name=" << count << "></a>\n";
874
875 int shastxt = (*sechere).metadata["hastxt"].values[0].getint();
876
877 output_text (*sechere, formatlistptr, queryresponse.termInfo,
878 (*sechere).OID, highlight, shastxt, wanttext, collection,
879 collectproto, disp, outconvert, textout, logout);
880 count ++;
881 sechere ++;
882 }
883 }
884 }
885 }
886 delete formatlistptr;
887}
888
889// highlighttext highlights query terms in text string and outputs the resulting text string
890void documentaction::highlighttext(text_t &text, const TermInfo_tarray &terms,
891 displayclass &disp, outconvertclass &outconvert,
892 ostream &textout, ostream &/*logout*/) {
893
894 text_tmap allterms;
895 text_tmap::const_iterator it;
896
897 // first load all the term variations into a map
898 TermInfo_tarray::const_iterator this_term = terms.begin();
899 TermInfo_tarray::const_iterator last_term = terms.end();
900 while (this_term != last_term) {
901 text_tarray::const_iterator this_var = (*this_term).matchTerms.begin();
902 text_tarray::const_iterator last_var = (*this_term).matchTerms.end();
903 while (this_var != last_var) {
904 allterms[*this_var] = 1;
905 this_var ++;
906 }
907 this_term ++;
908 }
909
910 // get the text to start and end a hightlight
911 text_t starthighlight = "<b><u>";
912 text_t endhighlight = "</u></b>";
913 if (disp.isdefaultmacro("Global", "starthighlight"))
914 disp.expandstring("Global", "_starthighlight_", starthighlight);
915 if (disp.isdefaultmacro("Global", "endhighlight"))
916 disp.expandstring("Global", "_endhighlight_", endhighlight);
917
918
919 text_t::iterator here = text.begin();
920 text_t::iterator end = text.end();
921 text_t word, buffer;
922 while (here != end) {
923 if (is_unicode_letdig(*here)) {
924 // not word boundary
925 word.push_back(*here);
926 here++;
927
928 } else {
929 // found word boundary
930 // add last word if there was one
931 if (!word.empty()) {
932 it = allterms.find(word);
933 if (it != allterms.end()) {
934 word = starthighlight + word + endhighlight;
935 }
936 buffer += word;
937 word.clear();
938 }
939
940 if (*here == '<') {
941 // skip over rest of html tag
942 while ((here != end) && (*here != '>')) {
943 buffer.push_back(*here);
944 here++;
945 }
946 }
947
948 buffer.push_back(*here);
949 here++;
950
951 if (buffer.size() > 1024) {
952 textout << outconvert << disp << buffer;
953 buffer.clear();
954 }
955 }
956 }
957 textout << outconvert << disp << buffer;
958}
Note: See TracBrowser for help on using the repository browser.