source: gsdl/trunk/runtime-src/src/recpt/depositoraction.cpp@ 19821

Last change on this file since 19821 was 19821, checked in by mdewsnip, 12 years ago

Commented out all occurrences of

#define _XOPEN_SOURCE_EXTENDED 1

This was allegedly added for compilation on Solaris, but it just causes errors for me (on the NLNZ Solaris machines).

  • Property svn:keywords set to Author Date Id Revision
File size: 31.3 KB
Line 
1/**********************************************************************
2 *
3 * depositoraction.cpp --
4 * Copyright (C) 2000 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 "gsdl_modules_cfg.h"
27#ifdef GSDL_USE_DEPOSITOR_ACTION
28
29// note that the collectoraction relies on having direct access to a
30// collections configuration file. this breaks the separation between
31// receptionist and collection server and so is not suitable (at least
32// in its current form) for use when collection servers are separate
33// from the receptionist (e.g. when using the CORBA protocol).
34
35// following line required to get fstream.filedesc() on darwin (Mac OS X)
36#define _STREAM_COMPAT 1
37// required for utsname on solaris???
38#define _XOPEN_SOURCE 1
39// This was added for Solaris, but it makes things worse on Solaris for me...
40// #define _XOPEN_SOURCE_EXTENDED 1
41
42#include "depositoraction.h"
43#include "OIDtools.h"
44#include "fileutil.h"
45#include "cfgread.h"
46#include "gsdltools.h"
47#include "gsdltimes.h"
48#include "argdb.h"
49#include "cgiutils.h"
50
51#include <stdio.h>
52//#include <strings.h> // seems to be needed for gcc 2.95, but not particularly happy it
53#include <fcntl.h>
54#include <time.h>
55
56#if !defined (__WIN32__)
57#include <sys/utsname.h>
58#include <unistd.h>
59#endif
60
61#include <new>
62//#include <string>
63#include <vector>
64#include <stdexcept>
65#include <iostream>
66//#include <cstdlib>
67
68depositoraction::depositoraction ()
69 : wizardaction()
70{
71 macro_prefix = "di1"; //deposit item
72 lastpage = 0;
73
74 cgiarginfo arg_ainfo;
75 arg_ainfo.shortname = "a";
76 arg_ainfo.longname = "action";
77 arg_ainfo.multiplechar = true;
78 arg_ainfo.defaultstatus = cgiarginfo::weak;
79 arg_ainfo.argdefault = "depositor";
80 arg_ainfo.savedarginfo = cgiarginfo::must;
81 argsinfo.addarginfo (NULL, arg_ainfo);
82
83 arg_ainfo.shortname = "p";
84 arg_ainfo.longname = "page";
85 arg_ainfo.multiplechar = true;
86 arg_ainfo.defaultstatus = cgiarginfo::weak;
87 arg_ainfo.argdefault = "select";
88 arg_ainfo.savedarginfo = cgiarginfo::must;
89 argsinfo.addarginfo (NULL, arg_ainfo);
90
91 //furthest page that has been visited
92 arg_ainfo.shortname = "di1lastpage";
93 arg_ainfo.longname = "depositor specific";
94 arg_ainfo.multiplechar = true;
95 arg_ainfo.defaultstatus = cgiarginfo::weak;
96 arg_ainfo.argdefault = "0";
97 arg_ainfo.savedarginfo = cgiarginfo::must;
98 argsinfo.addarginfo (NULL, arg_ainfo);
99
100 // the fileupload info that cgiwrapper produces - we parse the fileupload_t
101 // and set di1userfile, di1userfilesize
102 arg_ainfo.shortname = "di1userfileinfo";
103 arg_ainfo.longname = "depositor specific";
104 arg_ainfo.fileupload = true;
105 arg_ainfo.savedarginfo = cgiarginfo::mustnot;
106 argsinfo.addarginfo (NULL, arg_ainfo);
107
108 // essential: reset fileupload to default as we are not setting
109 // it for each arg
110 arg_ainfo.fileupload = false;
111
112 //the name of the file to be added
113 arg_ainfo.shortname = "di1userfile";
114 arg_ainfo.longname = "depositor specific";
115 arg_ainfo.multiplechar = true;
116 arg_ainfo.defaultstatus = cgiarginfo::weak;
117 arg_ainfo.argdefault = g_EmptyText;
118 arg_ainfo.savedarginfo = cgiarginfo::must;
119 argsinfo.addarginfo (NULL, arg_ainfo);
120
121 //the file size
122 arg_ainfo.shortname = "di1userfilesize";
123 arg_ainfo.longname = "depositor specific";
124 arg_ainfo.multiplechar = true;
125 arg_ainfo.defaultstatus = cgiarginfo::weak;
126 arg_ainfo.argdefault = "0";
127 arg_ainfo.savedarginfo = cgiarginfo::must;
128 argsinfo.addarginfo (NULL, arg_ainfo);
129
130 //the file timestamp
131 arg_ainfo.shortname = "di1timestamp";
132 arg_ainfo.longname = "depositor specific";
133 arg_ainfo.multiplechar = true;
134 arg_ainfo.defaultstatus = cgiarginfo::weak;
135 arg_ainfo.argdefault = "0";
136 arg_ainfo.savedarginfo = cgiarginfo::must;
137 argsinfo.addarginfo (NULL, arg_ainfo);
138
139 // temporary directory name for this collector
140 // session
141 arg_ainfo.shortname = "di1tmp";
142 arg_ainfo.longname = "depositor specific";
143 arg_ainfo.multiplechar = true;
144 arg_ainfo.defaultstatus = cgiarginfo::weak;
145 arg_ainfo.argdefault = g_EmptyText;
146 arg_ainfo.savedarginfo = cgiarginfo::must;
147 argsinfo.addarginfo (NULL, arg_ainfo);
148
149// arg_ainfo.shortname = "di1fullname";
150// arg_ainfo.longname = "depositor specific";
151// arg_ainfo.multiplechar = true;
152// arg_ainfo.defaultstatus = cgiarginfo::weak;
153// arg_ainfo.argdefault = g_EmptyText;
154// arg_ainfo.savedarginfo = cgiarginfo::mustnot; // saved on disk
155// argsinfo.addarginfo (NULL, arg_ainfo);
156
157 arg_ainfo.shortname = "di1dirname";
158 arg_ainfo.longname = "depositor specific";
159 arg_ainfo.multiplechar = true;
160 arg_ainfo.defaultstatus = cgiarginfo::weak;
161 arg_ainfo.argdefault = g_EmptyText;
162 arg_ainfo.savedarginfo = cgiarginfo::must;
163 argsinfo.addarginfo (NULL, arg_ainfo);
164
165 arg_ainfo.shortname = "di1contactemail";
166 arg_ainfo.longname = "depositor specific";
167 arg_ainfo.multiplechar = true;
168 arg_ainfo.defaultstatus = cgiarginfo::weak;
169 arg_ainfo.argdefault = g_EmptyText;
170 arg_ainfo.savedarginfo = cgiarginfo::mustnot; // saved on disk
171 argsinfo.addarginfo (NULL, arg_ainfo);
172
173// arg_ainfo.shortname = "di1clone";
174// arg_ainfo.longname = "depositor specific";
175// arg_ainfo.multiplechar = false;
176// arg_ainfo.defaultstatus = cgiarginfo::weak;
177// arg_ainfo.argdefault = "0";
178// arg_ainfo.savedarginfo = cgiarginfo::mustnot; // saved on disk
179// argsinfo.addarginfo (NULL, arg_ainfo);
180
181// arg_ainfo.shortname = "di1clonecol";
182// arg_ainfo.longname = "depositor specific";
183// arg_ainfo.multiplechar = true;
184// arg_ainfo.defaultstatus = cgiarginfo::weak;
185// arg_ainfo.argdefault = g_EmptyText;
186// arg_ainfo.savedarginfo = cgiarginfo::mustnot; // saved on disk
187// argsinfo.addarginfo (NULL, arg_ainfo);
188
189// arg_ainfo.shortname = "cfgfile";
190// arg_ainfo.longname = "configuration file contents";
191// arg_ainfo.multiplechar = true;
192// arg_ainfo.defaultstatus = cgiarginfo::weak;
193// arg_ainfo.argdefault = g_EmptyText;
194// arg_ainfo.savedarginfo = cgiarginfo::mustnot;
195// argsinfo.addarginfo (NULL, arg_ainfo);
196
197// // will be set if we arrived at the "configure collection" page
198// // via the "changing an existing collection" page
199// arg_ainfo.shortname = "di1econf";
200// arg_ainfo.longname = "depositor specific";
201// arg_ainfo.multiplechar = false;
202// arg_ainfo.defaultstatus = cgiarginfo::weak;
203// arg_ainfo.argdefault = "0";
204// arg_ainfo.savedarginfo = cgiarginfo::must;
205// argsinfo.addarginfo (NULL, arg_ainfo);
206
207 // wizard uses this to see if we are working with an existing collection:
208 // set the default to 1
209 arg_ainfo.shortname = "di1esrce";
210 arg_ainfo.longname = "depositor specific";
211 arg_ainfo.multiplechar = false;
212 arg_ainfo.defaultstatus = cgiarginfo::weak;
213 arg_ainfo.argdefault = "1";
214 arg_ainfo.savedarginfo = cgiarginfo::must;
215 argsinfo.addarginfo (NULL, arg_ainfo);
216
217// arg_ainfo.shortname = "di1input";
218// arg_ainfo.longname = "depositor specific";
219// arg_ainfo.multiplechar = true;
220// arg_ainfo.multiplevalue = true;
221// arg_ainfo.defaultstatus = cgiarginfo::weak;
222// arg_ainfo.argdefault = g_EmptyText;
223// arg_ainfo.savedarginfo = cgiarginfo::mustnot; // saved on disk
224// argsinfo.addarginfo (NULL, arg_ainfo);
225
226// //we don't need this in the depositor
227// arg_ainfo.shortname = "di1inputtype";
228// arg_ainfo.longname = "depositor specific";
229// arg_ainfo.multiplechar = true;
230// arg_ainfo.multiplevalue = true;
231// arg_ainfo.defaultstatus = cgiarginfo::weak;
232// arg_ainfo.argdefault = g_EmptyText;
233// arg_ainfo.savedarginfo = cgiarginfo::mustnot; // saved on disk
234// argsinfo.addarginfo (NULL, arg_ainfo);
235
236}
237
238depositoraction::~depositoraction () {
239}
240
241
242bool depositoraction::check_cgiargs (cgiargsinfoclass &argsinfo, cgiargsclass &args, recptprotolistclass * protos, ostream &logout) {
243
244 wizardaction::check_cgiargs(argsinfo,args,protos,logout);
245
246 text_t &current_page = args["p"];
247
248 // note that the "bildstatus" and "bildframe1" pages don't actually do anything
249 // functional so we don't need to worry about authenticating them (it's the
250 // underlying "bild" page that does the building (and creates the frameset))
251 // This helps us overcome a bit of a problem we have with multiple pages trying
252 // to read from the key.gdb database at the same time.
253
254 //right now, anyone can do anything until it's time to build
255 if (current_page == "select") {
256 //authenticate the user if authentication is available
257 args["uan"] = 1;
258 args["ug"] = "all-collections-editor";
259 }
260
261
262 //Check to see if a file was specified
263 //if so, upload it and set the di1userfile, di1userfilesize, and
264 //di1timestamp args.
265 //every time a new file is specified, a new timestamped folder is
266 //created.
267
268 // This doesn't create a problem but is inefficient (wasted space)
269 // Consider recoding at some point in the future.
270
271 fileupload_t *fileupload = args.getargfile("di1userfileinfo");
272
273 if (fileupload != NULL) {
274
275 if (!(*fileupload).tmp_name.empty() && file_exists((*fileupload).tmp_name)) {
276 // create the timestamp
277 time_t timestamp = time(NULL);
278 text_t timestamp_str(timestamp);
279
280 args["di1timestamp"] = timestamp_str;
281
282 // set filename and size from the fileupload struct
283 args["di1userfilesize"] = (*fileupload).size;
284 args["di1userfile"] = (*fileupload).name;
285 // copy the file into its temporary location
286 text_t tmpdir = filename_cat(gsdlhome,"tmp",args["di1tmp"],timestamp_str);
287 bool tflag = mk_dir(tmpdir);
288 text_t tmpfile = filename_cat(tmpdir,(*fileupload).name);
289 if (!file_copy((*fileupload).tmp_name, tmpfile)) {
290 cerr << "Depositor error: cannot save uploaded fileto "<<tmpfile<<endl;
291 } else {
292 cerr << "Depositor: saved uploaded file to "<<tmpfile<<endl;
293 }
294 }
295 }
296
297 if (current_page == "select") {
298
299 //make sure the last page arg is re-set to zero
300 //since a new collection is being chosen
301 args["di1lastpage"] = "0";
302
303 // assign (and create) a temporary directory This will create a new
304 // tbuild directory every time the user goes back to select a new
305 // collection. It causes no problems except wasted space. Should be
306 // addressed in the future
307 if (assign_tmpname (args, logout)==false) {
308 // there was an error creating the tmp dir
309 message="tmpfail";
310 return true; // true because we could still parse the arguments
311 }
312
313 // clean up any old builds left laying about in the tmp directory
314 // (note that it's possible this could take some time if there's a huge
315 // partially built collecton laying about so we'll make it an asynchronous
316 // system call)
317 gsdl_system ("perl -S cleantmp.pl", false, logout);
318
319 }
320
321 if (current_page == "bildstatus" || current_page == "bildcancel") {
322 // if .final file exists then build has finished
323 text_t fbld = filename_cat (gsdlhome, "tmp", args[macro_prefix + "tmp"], args[macro_prefix + "dirname"] + ".bld.final");
324 if (file_exists (fbld)) {
325 char *fbldc = fbld.getcstr();
326 ifstream fbld_in (fbldc);
327 if (fbld_in) {
328 failcode = fbld_in.get();
329 fbld_in.close();
330 if (failcode == '0') {
331 // success - we need to create and configure a collection server for the
332 // newly built collection (for fastcgi and local library where
333 // initialization isn't going to be redone when the user clicks the
334 // "view your new collection" button
335 create_colserver (args[macro_prefix + "dirname"], logout);
336 current_page = "bilddone";
337 }
338 else current_page = "bildfail";
339 } else {
340 // assume build failed (we shouldn't get here though ... right?)
341 current_page = "bildfail";
342 }
343 delete []fbldc;
344 }
345 }
346
347 //is it a step page?
348 text_t::const_iterator here = current_page.begin();
349 text_t::const_iterator end = current_page.end();
350 text_t stepstring = substr(here,here+4);
351
352 //if so, increment the step count
353 //this should not be going up more than one at a time.
354 if(stepstring == "step") {
355 text_t currpage_textt = substr(here+4,here+5);
356 int currpage = currpage_textt.getint();
357
358 text_t lastpage_textt = args["di1lastpage"];
359 int lastpage = lastpage_textt.getint();
360 if (currpage > lastpage) {
361 lastpage++;
362 text_t lastpage_textt(lastpage);
363 args["di1lastpage"] = lastpage_textt;
364 }
365
366 // create cached metadata values
367 text_t cached_metadata_values = "";
368 cgiargsclass::const_iterator args_here = args.begin();
369 cgiargsclass::const_iterator args_end = args.end();
370 while (args_here != args_end) {
371 text_t args_name = (*args_here).first;
372 int prefix_len = macro_prefix.size();
373 text_t args_prefix = substr(args_name.begin(),args_name.begin()+prefix_len+3);
374
375 if (args_prefix == (macro_prefix+"md.")) {
376 text_t args_val = args[args_name];
377 text_t args_suffix = substr(args_name.begin()+prefix_len+3,args_name.end());
378
379 text_tarray mdvalues;
380 splitchar (args_val.begin(), args_val.end(), ',', mdvalues);
381 int numvalues = mdvalues.size();
382
383 for (int i = 0; i < numvalues; ++i) {
384 if (!mdvalues[i].empty()) {
385 decode_cgi_arg(mdvalues[i]);
386
387 if (cached_metadata_values == "") {
388 cached_metadata_values = "var CachedMDValues = new Array(\\{";
389 } else {
390 cached_metadata_values += ",";
391 }
392
393 cached_metadata_values += "\"" + args_name + "\":\"" + args_val + "\"";
394 }
395 }
396 }
397 ++args_here;
398 }
399
400 if (cached_metadata_values != "") {
401 cached_metadata_values += "\\});";
402 args["cachedMDValues"] = cached_metadata_values;
403 }
404 }
405 return true;
406}
407
408
409void depositoraction::define_internal_macros (displayclass &disp, cgiargsclass &args,
410 recptprotolistclass *protos, ostream &logout) {
411
412 // define_internal_macros sets some/all of the following macros (depending
413 // on cgiargs):
414 //
415 // _pagescriptextra_
416 // _header_
417 // _depositorbar_
418 // _textfailmsg_
419 // _di1userfile_
420 // _di1userfilesize_
421
422 text_t &depositor_page = args["p"];
423
424 // set _pagescriptextra_ macro to _cpagescriptextra_
425 disp.setmacro ("pagescriptextra", "depositor", "_" + depositor_page + "scriptextra_");
426
427 if (depositor_page == "bildstatus" || depositor_page == "bilddone" ||
428 depositor_page == "bildfail" || depositor_page == "bildframe1" ||
429 depositor_page == "select") {
430 disp.setmacro ("header", "depositor", "_" + depositor_page + "header_");
431 }
432
433 if (depositor_page == "bildstatus") {
434 set_statusline (disp, args, logout);
435 }
436
437 if (depositor_page == "select") {
438 set_fullnamemenu (disp, args, protos, logout);
439 }
440
441 //how many pages in collection?
442 //we need a _depositorbar_ for each
443
444 text_t numsteps_str;
445 disp.expandstring("depositor", "_numsteps_", numsteps_str);
446 int numsteps = numsteps_str.getint();
447
448 //is the page a step page?
449
450 text_t::const_iterator here = depositor_page.begin();
451 text_t::const_iterator end = depositor_page.end();
452 text_t stepstring = substr(here,here+4);
453
454 if((stepstring) == "step" || (depositor_page == "depositonly") ) {
455 disp.setmacro("di1userfile","depositor",args["di1userfile"]);
456 disp.setmacro("di1userfilesize","depositor",args["di1userfilesize"]);
457 }
458
459 //set up the depositor bar
460 text_t depositorbar = "<table class=wizardbar border=0 cellspacing=4 cellpadding=0><tr>\n";
461
462 if(stepstring == "step") {
463 // check configured metadata elements
464 if (args["metadataconf"] == "var DepositorMDFields = new Array();" || args["metadataconf"] == "") {
465 text_t cfgfile_name = filename_cat (collecthome, args[macro_prefix + "dirname"], "etc", "collect.cfg");
466 text_t cfgfile_content;
467 text_t metadata_str;
468
469 if (read_file (cfgfile_name, cfgfile_content)) {
470 text_t::const_iterator here = cfgfile_content.begin();
471 text_t::const_iterator end = cfgfile_content.end();
472 while (here != end) {
473 here = findchar (here, end, 'd');
474 if (here == end) break;
475 if ((here+17 < end) && (substr (here, here+17) == "depositormetadata")) {
476 here = findchar (here, end, '"');
477 if (here == end) break;
478 text_t enddelimit = "\"\n";
479 getdelimitstr (here+1, end, enddelimit, metadata_str);
480 args["metadataconf"] = "var DepositorMDFields = new Array("+metadata_str+");";
481 }
482 ++here;
483 }
484 }
485
486 if (metadata_str == "") {
487 args["metadataconf"] = "var DepositorMDFields = new Array({\"name\":\"dc.Title\",\"label\":\"Title\",\"tooltip\":\"dc.Title: A name given to the resource.\",\"type\":\"text\"}, {\"name\":\"dc.Creator\",\"label\":\"Creator\",\"tooltip\":\"dc.Creator: An entity primarily responsible for making the content of the resource.\",\"type\":\"text\"}, {\"name\":\"dc.Description\",\"label\":\"Description\",\"tooltip\":\"dc.Description: An account of the content of the resource.\",\"type\":\"textarea\"});";
488 }
489 }
490
491 here = depositor_page.begin();
492 text_t stepnums = substr(here+4,here+5);
493 int stepnum = stepnums.getint();
494
495 text_t lastpage_textt = args["di1lastpage"];
496 int lastpage = lastpage_textt.getint();
497
498 // again, begin with the select bar...
499 //the first button - selecting a collection
500 for(int i = 1; i <= numsteps; i++) {
501 text_t numstr(i);
502 if(i <= lastpage && i == stepnum) {
503 depositorbar += get_button (args,depositor_page, "yellow", "step"+numstr, false);
504 } else if (i <= lastpage) {
505 depositorbar += get_button (args,depositor_page, "yellow", "step"+numstr, true);
506 } else if((i == lastpage+1)){
507 depositorbar += get_button (args,depositor_page, "green", "step"+numstr, true);
508 } else {
509 depositorbar += get_button (args,depositor_page, "grey", "step"+numstr, false);
510 }
511 depositorbar += "<td>_icongreyarrow_</td>\n";
512
513 }
514
515 //the build and preview pages are always last
516 if(lastpage == numsteps) {
517 depositorbar += get_button (args,depositor_page, "green", "laststep", true);
518 } else {
519 depositorbar += get_button (args,depositor_page, "grey", "laststep", false);
520 }
521
522 text_t laststep_textt;
523 disp.expandstring("depositor", "_laststep_", laststep_textt);
524 if(laststep_textt == "bild") {
525 depositorbar += "<td>_icongreyarrow_</td>\n";
526 depositorbar += get_button (args,depositor_page, "grey", "view", false);
527 }
528 depositorbar += "</tr><tr>";
529
530 for(int j = 1; j <= 2*(stepnum-1); j++) {
531 depositorbar += "<td></td>";
532 }
533 depositorbar += "<td align=center>_icongreyuparrow_</td>\n";
534
535 depositorbar += "</tr></table>\n";
536
537 // set the javascript so that the metadata form can read the existing values
538 if (args["cachedMDValues"] != "") {
539 disp.setmacro("cachedmetadatavalues", "depositor", args["cachedMDValues"]);
540 }
541 if (args["metadataconf"] != "") {
542 disp.setmacro("metadataconf", "depositor", args["metadataconf"]);
543 }
544 }
545
546 if ((depositor_page == "bildcancel") || (depositor_page == "bildfail")) {
547
548 for(int i = 1; i <= numsteps; i++) {
549
550 text_t numstr(i);
551 depositorbar += get_button (args,depositor_page, "yellow", "step"+numstr, true);
552 depositorbar += "<td>_icongreyarrow_</td>\n";
553
554 }
555
556 //the build and preview pages are always last
557 depositorbar += get_button (args,depositor_page, "green", "laststep", true);
558 depositorbar += "<td>_icongreyarrow_</td>\n";
559 depositorbar += get_button (args,depositor_page, "grey", "view", false);
560
561 depositorbar += "</tr><tr>";
562
563 for(int j = 1; j <= 2*numsteps; j++) {
564 depositorbar += "<td></td>";
565 }
566 depositorbar += "<td align=center>_icongreyuparrow_</td>\n";
567
568 depositorbar += "</tr></table>\n";
569
570 }
571
572 if(depositor_page == "bilddone"){
573
574 for(int i = 1; i <= numsteps; i++) {
575
576 text_t numstr(i);
577 depositorbar += get_button (args, depositor_page, "grey", "step"+numstr, false);
578 depositorbar += "<td>_icongreyarrow_</td>\n";
579
580 }
581
582 //the build and preview pages are always last
583 depositorbar += get_button (args,depositor_page, "yellow", "laststep", false);
584 depositorbar += "<td>_icongreyarrow_</td>\n";
585 depositorbar += get_button (args,depositor_page, "green", "view", true);
586
587 depositorbar += "</tr><tr>";
588
589 for(int j = 1; j <= 2*numsteps; j++) {
590 depositorbar += "<td></td>";
591 }
592 depositorbar += "<td align=center>_icongreyuparrow_</td>\n";
593
594 depositorbar += "</tr></table>\n";
595 }
596 disp.setmacro ("depositorbar", "depositor", depositorbar);
597
598 if (depositor_page == "bildfail") {
599
600 text_t textfailmsg = "_textfailmsg";
601 textfailmsg.push_back(failcode);
602 textfailmsg.push_back('_');
603 disp.setmacro("textfailmsg", "depositor", textfailmsg);
604
605 text_t bldlog = filename_cat(gsdlhome, "tmp", args["di1tmp"], args["di1dirname"] + ".bld");
606 text_t rawlog = file_tail (bldlog, 6, 0);
607 // we'll shove in some <br> tags where \n's occur
608 text_t faillog;
609 text_t::const_iterator here = rawlog.begin();
610 text_t::const_iterator end = rawlog.end();
611 while (here != end) {
612 if (*here == '\n') faillog += "<br>";
613 faillog.push_back (*here);
614 ++here;
615 }
616 disp.setmacro ("faillog", "depositor", dm_safe(faillog));
617 }
618}
619
620
621//basic framework
622bool depositoraction::do_action (cgiargsclass &args, recptprotolistclass * /*protos*/,
623 browsermapclass * /*browsers*/, displayclass &disp,
624 outconvertclass &outconvert, ostream &textout,
625 ostream &logout) {
626 // make sure the depositor is enabled
627 if (disabled) {
628 textout << outconvert
629 << "<html>\n"
630 << "<head>\n"
631 << "<title>Depositor disabled</title>\n"
632 << "</head>\n"
633 << "<body bgcolor=\"#ffffff\" text=\"#000000\" link=\"#006666\" "
634 << "alink=\"#cc9900\" vlink=\"#666633\">\n"
635 << "<h2>Facility disabled</h2>\n"
636 << "Sorry, the Depositor end-user collecation update facility is currently disabled\n"
637 << "\n</body>\n"
638 << "</html>\n";
639 return true;
640 }
641
642 text_t &depositor_page = args["p"];
643 text_t &collection = args["di1dirname"];
644
645 // make sure we have perl (we won't bother with this check for the
646 // building status pages to avoid slowing things down unneccessarily)
647 if (depositor_page != "bildstatus" && depositor_page != "bildframe1" && !perl_ok(logout)) {
648 textout << outconvert
649 << "<html>\n"
650 << "<head>\n"
651 << "<title>Perl not found</title>\n"
652 << "</head>\n"
653 << "<body bgcolor=\"#ffffff\" text=\"#000000\" link=\"#006666\" "
654 << "alink=\"#cc9900\" vlink=\"#666633\">\n"
655 << "<h2>Perl not found</h2>\n"
656 << "Greenstone could not detect perl on this system. It is therefore not\n"
657 << "possible to build a Greenstone collection, either from the Collector or the \n"
658 << "command-line tools, or to use the Collector for any other task.\n"
659 << "<p>Please refer to the Greenstone Installer's Guide for details on\n"
660 << "installing perl on your system.\n"
661 << "\n</body>\n"
662 << "</html>\n";
663 return true;
664
665 }
666
667 text_t::const_iterator here = depositor_page.begin();
668 text_t::const_iterator end = depositor_page.end();
669 text_t stepstring = substr(here,here+4);
670
671 if ((depositor_page == "select") || (stepstring == "step")) {
672 textout << outconvert << disp << ("_depositor:header_\n")
673 << ("_depositor:" + depositor_page + "content_\n")
674 << ("_depositor:footer_\n");
675
676 }
677
678 if ((depositor_page == "bild") || (depositor_page == "depositonly")) {
679
680 text_t filename_textt = args["di1userfile"];
681 text_t timestamp_str = args["di1timestamp"];
682 //check to make sure a file was uploaded
683 //if more than one upload occurred, the last one is taken
684 if(filename_textt == "") {
685 textout << outconvert << disp << "<p> no file available</p>\n";
686 } else {
687
688 text_t col_dirname = filename_cat(collecthome,args[macro_prefix+"dirname"]);
689 text_t import_dirname = filename_cat(col_dirname,"import");
690 if(!directory_exists(import_dirname)) {
691 bool flag = mk_dir(import_dirname);
692 if(!flag) {
693 cerr << "Error: unable to make directory " << import_dirname << endl;
694 return true;
695 }
696 }
697
698 text_t dirname = filename_cat(import_dirname,timestamp_str);
699 bool flag = mk_dir(dirname);
700 if(!flag) {
701 cerr << "Error: unable to make timestamp directory " << dirname << endl;
702 return true;
703 }
704
705 text_t filename_textt = args["di1userfile"];
706 text_t tmpdir = filename_cat(gsdlhome,"tmp",args["di1tmp"],timestamp_str);
707 text_t tmpfile = filename_cat(tmpdir,filename_textt);
708 text_t filename = filename_cat(dirname, filename_textt);
709 if(!file_copy(tmpfile, filename)) {
710 cerr << "Unable to copy " << tmpfile << " to " << filename << endl;
711 }
712
713 //write the metadata file
714 write_metadata_file(args, filename_textt, timestamp_str);
715
716 if(depositor_page == "bild"){
717
718 //create a manifest file
719 write_manifest_file(args, filename_textt, timestamp_str);
720
721 // do the work (download, import, build)
722 gsdl_build (args, logout);
723
724 if (message.empty()) {
725 // bild page is a frameset so we don't want headers and stuff
726 textout << outconvert << disp << ("_depositor:bildcontent_\n");
727 }
728 }
729 }
730 }
731
732 if (message.empty()) {
733
734 if (depositor_page != "bild" && stepstring != "step" && depositor_page != "select") {
735 // output page ("bild" page was already output above)
736 textout << outconvert << disp << ("_depositor:header_\n")
737 << ("_depositor:" + depositor_page + "content_\n")
738 << ("_depositor:footer_\n");
739 }
740 } else {
741 // message was set somewhere (probably an error), output message page
742 textout << outconvert << disp << ("_depositor:header_\n")
743 << ("_depositor:" + message + "content_\n")
744 << ("_depositor:footer_\n");
745 message.clear();
746 }
747
748 return true;
749
750}
751
752
753//This function creates a metadata.xml file
754void depositoraction::write_metadata_file(cgiargsclass &args, text_t filename_str, text_t& timestamp_str)
755{
756
757 //build metadata file
758 //the front
759 text_t metadata_file = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
760 metadata_file += "<!DOCTYPE DirectoryMetadata SYSTEM \"http://greenstone.org/dtd/DirectoryMetadata/1.0/DirectoryMetadata.dtd\">\n";
761 metadata_file += "<DirectoryMetadata>\n";
762 metadata_file += " <FileSet>\n";
763 metadata_file += " <FileName>";
764
765 // Generate 'regular expression' save version of filename
766 // for now that means escaping any '.' characters
767 // consider generalising as function in gsdltools.h/cpp?
768
769 text_t filename_re;
770 text_t::const_iterator here = filename_str.begin();
771 text_t::const_iterator end = filename_str.end();
772 while (here != end) {
773 if (*here == '.') {
774 filename_re.push_back('\\');
775 }
776
777 filename_re.push_back(*here);
778 ++here;
779 }
780
781 metadata_file += filename_re;
782
783 metadata_file += "</FileName>\n";
784 metadata_file += " <Description>\n";
785
786
787 cgiargsclass::const_iterator args_here = args.begin();
788 cgiargsclass::const_iterator args_end = args.end();
789
790 while (args_here != args_end) {
791 text_t args_name = (*args_here).first;
792
793 int prefix_len = macro_prefix.size();
794
795 text_t args_prefix = substr(args_name.begin(),args_name.begin()+prefix_len+3);
796
797 if (args_prefix == (macro_prefix+"md.")) {
798 text_t args_val = args[args_name];
799
800 text_t args_suffix = substr(args_name.begin()+prefix_len+3,args_name.end());
801
802 text_tarray mdvalues;
803 splitchar (args_val.begin(), args_val.end(), ',', mdvalues);
804 int numvalues = mdvalues.size();
805
806 for (int i = 0; i < numvalues; ++i) {
807 if (!mdvalues[i].empty()) {
808
809 decode_cgi_arg(mdvalues[i]);
810
811 metadata_file += " <Metadata mode=\"accumulate\" name=\"";
812 metadata_file += args_suffix;
813 metadata_file += "\">";
814 metadata_file += mdvalues[i];
815 metadata_file += "</Metadata>\n";
816
817 }
818 }
819 }
820
821 ++args_here;
822 }
823
824 //the end of the file
825 metadata_file += " </Description>\n";
826 metadata_file += " </FileSet>\n";
827 metadata_file += "</DirectoryMetadata>\n";
828
829 //create metadata.xml file
830 text_t metadata_path = filename_cat(collecthome, args[macro_prefix+"dirname"], "import", timestamp_str, "metadata.xml");
831 text_t my_path = filename_cat(gsdlhome,"tmp", "metadata.xml");
832
833 ofstream metadata(metadata_path.getcstr());
834 ofstream my_tmp(my_path.getcstr());
835
836 if(!metadata.is_open()) {
837 cerr << "Cannot open metadata.xml!" << endl;
838 } else {
839
840 //write metadata.xml
841 metadata.write(metadata_file.getcstr(), metadata_file.size());
842 my_tmp.write(metadata_file.getcstr(), metadata_file.size());
843 }
844}
845
846//This function creates a manifest.xml file
847void depositoraction::write_manifest_file(cgiargsclass &args, text_t filename,
848 text_t& timestamp_str)
849{
850
851 //build manifest file
852
853 //the front
854 text_t manifest_file = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
855 manifest_file += "<Manifest>\n";
856 manifest_file += " <Import>\n";
857 manifest_file += " <Filename>\n";
858 manifest_file += " " + timestamp_str + "\n";
859 manifest_file += " </Filename>\n";
860
861 //the end of the file
862 manifest_file += " </Import>\n";
863 manifest_file += "</Manifest>\n";
864
865 //create manifest.xml file
866 text_t manifest_path = filename_cat(collecthome, args[macro_prefix+"dirname"], "manifest.xml");
867
868 char* manifest_cstr = manifest_path.getcstr();
869
870 ofstream manifest(manifest_cstr);
871
872 if(!manifest.is_open()) {
873 cerr << "Cannot open" << manifest_cstr << endl;
874 } else {
875
876 //write manifest.xml
877 char *manifest_file_cstr = manifest_file.getcstr();
878 manifest.write(manifest_file_cstr, manifest_file.size());
879 delete [] manifest_file_cstr;
880 }
881
882 delete [] manifest_cstr;
883}
884
885
886
887text_t depositoraction::get_button(cgiargsclass &args, const text_t &thispage,
888 const text_t &color,
889 const text_t &type, bool enabled)
890{
891
892 text_t::const_iterator here = type.begin();
893 text_t::const_iterator end = type.end();
894 text_t stepstring = substr(here,here+4);
895 if ((color != "green" && color != "grey" && color != "yellow") ||
896 (type != "select" && type != "laststep" && stepstring != "step" && type != "view"))
897 return g_EmptyText;
898
899 text_t href = "_http"+type+"_";
900 text_t target = "";
901
902 here = thispage.begin();
903 stepstring = substr(here,here+4);
904 if (thispage == "bildcancel" || thispage == "bildfail" || thispage == "select"|| stepstring == "step") {
905 // call the check submit macro instead of linking directly to the page
906
907 if(type == "laststep") {
908 href="\"javascript:check_submit('_"+type+"_');\"";
909 } else {
910 href="\"javascript:check_submit('"+type+"');\"";
911 }
912
913 } else if (type == "view") {
914 //target = " target=_top";
915 target = " target=_blank";
916 }
917
918 text_t tdclass = "wizardbar"+color;
919 if (enabled) {
920 // link to the appropriate page
921 return "<td class="+tdclass+"><a href="+href+target+">_text"+type+"_</a></td>";
922 }
923 else {
924 // just display the text
925 return "<td class="+tdclass+">_text"+type+"_</td>";
926 }
927}
928
929
930#endif //GSDL_USE_DEPOSITOR_ACTION
Note: See TracBrowser for help on using the repository browser.