source: main/trunk/greenstone2/runtime-src/src/recpt/depositoraction.cpp@ 21877

Last change on this file since 21877 was 21877, checked in by kjdon, 14 years ago

added ifndefs around the define _XOPEN_SOURCE. Adding in gsdlconf to gsdl_modules_cfg.h meant that XOPEN_SOURCE was already defined on linux and giving a compile warning.

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