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

Last change on this file since 1991 was 1991, checked in by sjboddie, 23 years ago

Fixed a bug in DocumentUseHTML

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