1 | /**********************************************************************
|
---|
2 | *
|
---|
3 | * buildaction.cpp -- building collections
|
---|
4 | * Copyright (C) 1999 DigiLib Systems Limited, New Zealand
|
---|
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 <unistd.h>
|
---|
27 |
|
---|
28 | #include "OIDtools.h"
|
---|
29 | #include "fileutil.h"
|
---|
30 | #include "buildaction.h"
|
---|
31 | #include "gsdlhome.h"
|
---|
32 |
|
---|
33 | ///////////////
|
---|
34 | // buildaction
|
---|
35 | ///////////////
|
---|
36 |
|
---|
37 | buildaction::buildaction () {
|
---|
38 | // this action uses cgi variable "a"
|
---|
39 | cgiarginfo arg_ainfo;
|
---|
40 | arg_ainfo.shortname = "a";
|
---|
41 | arg_ainfo.longname = "action";
|
---|
42 | arg_ainfo.multiplechar = true;
|
---|
43 | arg_ainfo.defaultstatus = cgiarginfo::weak;
|
---|
44 | arg_ainfo.argdefault = "bc"; // build collection
|
---|
45 | arg_ainfo.savedarginfo = cgiarginfo::must;
|
---|
46 | argsinfo.addarginfo (NULL, arg_ainfo);
|
---|
47 |
|
---|
48 | // "bca"
|
---|
49 | arg_ainfo.shortname = "bca";
|
---|
50 | arg_ainfo.longname = "build collection action";
|
---|
51 | arg_ainfo.multiplechar = true;
|
---|
52 | arg_ainfo.defaultstatus = cgiarginfo::weak;
|
---|
53 | arg_ainfo.argdefault = "editcol";
|
---|
54 | arg_ainfo.savedarginfo = cgiarginfo::mustnot;
|
---|
55 | argsinfo.addarginfo (NULL, arg_ainfo);
|
---|
56 |
|
---|
57 |
|
---|
58 | // "mess"
|
---|
59 | arg_ainfo.shortname = "mess";
|
---|
60 | arg_ainfo.longname = "macro name of text for the message page";
|
---|
61 | arg_ainfo.multiplechar = true;
|
---|
62 | arg_ainfo.defaultstatus = cgiarginfo::weak;
|
---|
63 | arg_ainfo.argdefault = "";
|
---|
64 | arg_ainfo.savedarginfo = cgiarginfo::mustnot;
|
---|
65 | argsinfo.addarginfo (NULL, arg_ainfo);
|
---|
66 |
|
---|
67 | // "head"
|
---|
68 | arg_ainfo.shortname = "head";
|
---|
69 | arg_ainfo.longname = "macro name of text to use as header for the message page";
|
---|
70 | arg_ainfo.multiplechar = true;
|
---|
71 | arg_ainfo.defaultstatus = cgiarginfo::weak;
|
---|
72 | arg_ainfo.argdefault = "";
|
---|
73 | arg_ainfo.savedarginfo = cgiarginfo::mustnot;
|
---|
74 | argsinfo.addarginfo (NULL, arg_ainfo);
|
---|
75 |
|
---|
76 | }
|
---|
77 |
|
---|
78 | bool buildaction::check_cgiargs (cgiargsinfoclass &/*argsinfo*/, cgiargsclass &args,
|
---|
79 | ostream &/*logout*/) {
|
---|
80 | if (args["bca"] != "buildstatus")
|
---|
81 | {
|
---|
82 | // authenticate the user if authentication is avaiable
|
---|
83 | args["uan"] = 1;
|
---|
84 | args["ug"] = "colbuilder";
|
---|
85 | }
|
---|
86 | return true;
|
---|
87 | }
|
---|
88 |
|
---|
89 | void buildaction::get_cgihead_info (cgiargsclass &/*args*/, recptprotolistclass * /*protos*/,
|
---|
90 | response_t &response,text_t &response_data,
|
---|
91 | ostream &/*logout*/) {
|
---|
92 |
|
---|
93 | response = content;
|
---|
94 | response_data = "text/html";
|
---|
95 | }
|
---|
96 |
|
---|
97 | void buildaction::define_internal_macros (displayclass &disp, cgiargsclass &args,
|
---|
98 | recptprotolistclass *protos, ostream &logout) {
|
---|
99 |
|
---|
100 | // build up javascript and HTML code useful in web forms
|
---|
101 | text_t first_dirname = "";
|
---|
102 | text_t fullnamelist = "var fullnamelist = new Array(";
|
---|
103 | text_t dirnamelist = "var dirnamelist = new Array(";
|
---|
104 | text_t fullnamemenu = (text_t)"<input type=hidden name=\"bc1fullnameindex\""
|
---|
105 | +" value=\"_bcargfullnameindex_\">\n"
|
---|
106 | +"<select name=\"bc1fullnamemenu\" onChange=fullnameindex_changed()>\n";
|
---|
107 | int rcount = 1;
|
---|
108 | int fcount = 1;
|
---|
109 | int starting_fullname_index = 0;
|
---|
110 | disp.setmacro("bcargfirsttime","build","false");
|
---|
111 |
|
---|
112 | recptprotolistclass::iterator rprotolist_here = protos->begin();
|
---|
113 | recptprotolistclass::iterator rprotolist_end = protos->end();
|
---|
114 | while (rprotolist_here != rprotolist_end) {
|
---|
115 | if ((*rprotolist_here).p != NULL) {
|
---|
116 | text_tarray collist;
|
---|
117 | comerror_t err;
|
---|
118 | (*rprotolist_here).p->get_collection_list (collist, err, logout);
|
---|
119 | if (err == noError) {
|
---|
120 | text_tarray::iterator collist_here = collist.begin();
|
---|
121 | text_tarray::iterator collist_end = collist.end();
|
---|
122 |
|
---|
123 | if (rcount>1) { fullnamelist += ","; }
|
---|
124 | fullnamelist += "new Array(";
|
---|
125 |
|
---|
126 | FilterResponse_t response;
|
---|
127 | text_tset metadata;
|
---|
128 | metadata.insert ("collectionname");
|
---|
129 |
|
---|
130 | int ccount = 1;
|
---|
131 |
|
---|
132 | while (collist_here != collist_end) {
|
---|
133 |
|
---|
134 | ColInfoResponse_t cinfo;
|
---|
135 | (*rprotolist_here).p->get_collectinfo (*collist_here, cinfo, err, logout);
|
---|
136 | if (err == noError) {
|
---|
137 | text_t collectionname = *collist_here;
|
---|
138 | if (!cinfo.collectionmeta["collectionname"].empty())
|
---|
139 | {
|
---|
140 | // get collection name from the collection cfg file
|
---|
141 | collectionname = cinfo.collectionmeta["collectionname"];
|
---|
142 | }
|
---|
143 | else
|
---|
144 | {
|
---|
145 | if (get_info ("collection", *collist_here, metadata, false, (*rprotolist_here).p,
|
---|
146 | response, logout))
|
---|
147 | {
|
---|
148 | // get collection name from gdbm file
|
---|
149 | collectionname = response.docInfo[0].metadata["collectionname"].values[0];
|
---|
150 | }
|
---|
151 | }
|
---|
152 |
|
---|
153 | if (ccount>1) { fullnamelist += ","; dirnamelist += ","; }
|
---|
154 | fullnamelist += (text_t)"\"" + collectionname + "\"";
|
---|
155 | dirnamelist += (text_t)"\"" + *collist_here + "\"";
|
---|
156 |
|
---|
157 | fullnamemenu += (text_t)"<option value=\"" + collectionname + "\"";
|
---|
158 | if (args["bc1dirname"] == "")
|
---|
159 | {
|
---|
160 | if ((rcount==1) && (ccount==1))
|
---|
161 | {
|
---|
162 | fullnamemenu += " selected";
|
---|
163 | first_dirname = *collist_here;
|
---|
164 | starting_fullname_index = 0;
|
---|
165 | }
|
---|
166 | }
|
---|
167 | else
|
---|
168 | {
|
---|
169 | if (args["bc1dirname"] == *collist_here)
|
---|
170 | {
|
---|
171 | fullnamemenu += " selected";
|
---|
172 | first_dirname = *collist_here;
|
---|
173 | starting_fullname_index = fcount-1;
|
---|
174 | }
|
---|
175 | }
|
---|
176 | fullnamemenu += (text_t)">" + collectionname;
|
---|
177 |
|
---|
178 | // check to see if the "collection" is writable
|
---|
179 | text_t cfg_fname
|
---|
180 | = filename_cat(GSDL_GSDLHOME,"collect",*collist_here,"etc","collect.cfg");
|
---|
181 | if (!file_writable(cfg_fname))
|
---|
182 | {
|
---|
183 | fullnamemenu += " <b>(write protected)</b>";
|
---|
184 | }
|
---|
185 |
|
---|
186 | fullnamemenu += "\n";
|
---|
187 |
|
---|
188 | if (args["bca"]=="buildcol")
|
---|
189 | {
|
---|
190 | if (((args["bc1dirname"]=="") && (rcount==1) && (ccount==1))
|
---|
191 | || (args["bc1dirname"]==*collist_here))
|
---|
192 | {
|
---|
193 | disp.setmacro("bcargingsdlarea","build",cinfo.building["ingsdlarea"]);
|
---|
194 | disp.setmacro("bcargcopydir","build",cinfo.building["copydir"]);
|
---|
195 |
|
---|
196 | if (cinfo.buildDate==0) // not built
|
---|
197 | {
|
---|
198 | // assume that's because this is the first build ever for the collection
|
---|
199 | args["bc1copydata"]="true";
|
---|
200 | disp.setmacro("bcargcopydata","build","true");
|
---|
201 | disp.setmacro("bcargfirsttime","build","true"); // override value
|
---|
202 | }
|
---|
203 | }
|
---|
204 | }
|
---|
205 |
|
---|
206 | ccount++;
|
---|
207 | fcount++;
|
---|
208 | }
|
---|
209 | collist_here ++;
|
---|
210 | }
|
---|
211 |
|
---|
212 | fullnamelist += ")";
|
---|
213 | }
|
---|
214 | rcount ++;
|
---|
215 | }
|
---|
216 | fullnamelist += ");\n";
|
---|
217 | dirnamelist += ");\n";
|
---|
218 |
|
---|
219 | fullnamemenu += "</select>\n";
|
---|
220 | rprotolist_here ++;
|
---|
221 | }
|
---|
222 |
|
---|
223 | if ((args["bca"]=="newcol") || (args["bca"]=="editcol"))
|
---|
224 | {
|
---|
225 | disp.setmacro ("fullnamelist", "build", fullnamelist);
|
---|
226 | }
|
---|
227 |
|
---|
228 | if ((args["bca"]=="delcol") || (args["bca"]=="editcol") || (args["bca"]=="buildcol"))
|
---|
229 | {
|
---|
230 | disp.setmacro ("dirnamelist", "build", dirnamelist);
|
---|
231 | disp.setmacro ("fullnamemenu", "build", fullnamemenu);
|
---|
232 | disp.setmacro ("bcargfullnameindex", "build", starting_fullname_index);
|
---|
233 | }
|
---|
234 |
|
---|
235 |
|
---|
236 | // define special "wizard" form value so webform building process can
|
---|
237 | // keep track of which page it is on
|
---|
238 | text_t wizard_value
|
---|
239 | = (args["wizard"] == "") ? (text_t)"textinfo" : args["wizard"];
|
---|
240 | text_t hiddenargs
|
---|
241 | = "<input type=\"hidden\" name=\"wizard\" value=\""+wizard_value+"\">\n";
|
---|
242 | disp.setmacro ("hiddenargs", "build", hiddenargs);
|
---|
243 | disp.setmacro ("wizard", "build", wizard_value);
|
---|
244 |
|
---|
245 | // cache any cgi arguments starting "newcol:" (from previous webform)
|
---|
246 | cgiargsclass::const_iterator args_here = args.begin();
|
---|
247 | cgiargsclass::const_iterator args_end = args.end();
|
---|
248 | while (args_here != args_end) {
|
---|
249 | // define macro if args came from a previous col building webpage
|
---|
250 | text_t args_entry = (*args_here).first;
|
---|
251 |
|
---|
252 | if (substr(args_entry.begin(),args_entry.begin()+3) == "bc1")
|
---|
253 | {
|
---|
254 | text_t bc1name = substr(args_entry.begin()+3,args_entry.end());
|
---|
255 | text_t cached_cgiarg = "bcarg" + bc1name;
|
---|
256 | disp.setmacro(cached_cgiarg,"build",args[args_entry]);
|
---|
257 | }
|
---|
258 | args_here++;
|
---|
259 | }
|
---|
260 |
|
---|
261 | // if edit collection, read in collect.cfg file
|
---|
262 | if (args["bca"]=="editcol")
|
---|
263 | {
|
---|
264 | text_t dirname
|
---|
265 | = (args["bc1dirname"] == "") ? first_dirname : args["bc1dirname"];
|
---|
266 |
|
---|
267 | // read in collect.cfg
|
---|
268 | text_t cfg_fname
|
---|
269 | = filename_cat(GSDL_GSDLHOME,"collect",dirname,"etc","collect.cfg");
|
---|
270 | ifstream cfg_ifs (cfg_fname.getcstr(), ios::in | ios::nocreate);
|
---|
271 | if (cfg_ifs)
|
---|
272 | {
|
---|
273 | text_t cfg_text = "";
|
---|
274 |
|
---|
275 | char c;
|
---|
276 | cfg_ifs.get(c);
|
---|
277 | while (!cfg_ifs.eof ()) {
|
---|
278 | cfg_text.push_back(c);
|
---|
279 | cfg_ifs.get(c);
|
---|
280 | }
|
---|
281 |
|
---|
282 | cfg_ifs.close();
|
---|
283 |
|
---|
284 | // define it as a macro
|
---|
285 | disp.setmacro("cfgfile","build",cfg_text);
|
---|
286 | }
|
---|
287 | else
|
---|
288 | {
|
---|
289 | text_t error_mess = "Unable to read configuration file: "+cfg_fname;
|
---|
290 | disp.setmacro("cfgfile","build",error_mess);
|
---|
291 | }
|
---|
292 | }
|
---|
293 | }
|
---|
294 |
|
---|
295 | bool buildaction::do_action (cgiargsclass &args, recptprotolistclass * /*protos*/,
|
---|
296 | browsermapclass * /*browsers*/, displayclass &disp,
|
---|
297 | outconvertclass &outconvert, ostream &textout,
|
---|
298 | ostream &logout) {
|
---|
299 |
|
---|
300 |
|
---|
301 | if (args["bca"] == "newcol") {
|
---|
302 | return do_newcol (args, disp, outconvert, textout, logout);
|
---|
303 | }
|
---|
304 | else if (args["bca"] == "editcol") {
|
---|
305 | return do_editcol (args, disp, outconvert, textout, logout);
|
---|
306 | }
|
---|
307 | else if (args["bca"] == "buildcol") {
|
---|
308 | return do_buildcol (args, disp, outconvert, textout, logout);
|
---|
309 | }
|
---|
310 | else if (args["bca"] == "buildstatus") {
|
---|
311 | return do_buildstatus (args, disp, outconvert, textout, logout);
|
---|
312 | }
|
---|
313 | else if (args["bca"] == "delcol") {
|
---|
314 | return do_delcol (args, disp, outconvert, textout, logout);
|
---|
315 | }
|
---|
316 | else if (args["bca"] == "mess") {
|
---|
317 | return do_mess (args, disp, outconvert, textout, logout);
|
---|
318 | }
|
---|
319 |
|
---|
320 | // default (bca == blankpage)
|
---|
321 | textout << outconvert << "<html><body bgcolor=#ffffff></body></html>\n";
|
---|
322 |
|
---|
323 | return true;
|
---|
324 | }
|
---|
325 |
|
---|
326 |
|
---|
327 |
|
---|
328 | bool buildaction::do_newcol (cgiargsclass &args, displayclass &disp,
|
---|
329 | outconvertclass &outconvert, ostream &textout,
|
---|
330 | ostream &/*logout*/) {
|
---|
331 |
|
---|
332 | text_tarray wizard_split;
|
---|
333 | const text_t& wizard = (args["wizard"] == "") ? (text_t)"textinfo" : args["wizard"];
|
---|
334 | splitchar(wizard.begin(),wizard.end(),':',wizard_split);
|
---|
335 | const text_t& wizard_last = wizard_split.back();
|
---|
336 |
|
---|
337 | text_t content = "_build:content" + wizard_last + "_\n";
|
---|
338 | text_t headmess = "_build:header_(_build:headmess" + wizard_last + "_)\n";
|
---|
339 |
|
---|
340 | textout << outconvert << disp << (headmess) << (content) << ("_build:wizardfooter_\n");
|
---|
341 |
|
---|
342 | return true;
|
---|
343 | }
|
---|
344 |
|
---|
345 |
|
---|
346 | bool buildaction::do_editcol (cgiargsclass &/*args*/, displayclass &disp,
|
---|
347 | outconvertclass &outconvert, ostream &textout,
|
---|
348 | ostream &/*logout*/) {
|
---|
349 |
|
---|
350 | textout << outconvert << disp << ("_build:header_(_build:headmesseditcol_)\n")
|
---|
351 | << ("_build:contenteditcol_\n")
|
---|
352 | << ("_build:wizardfooter_\n");
|
---|
353 |
|
---|
354 | return true;
|
---|
355 | }
|
---|
356 |
|
---|
357 | bool buildaction::do_buildcol (cgiargsclass &args, displayclass &disp,
|
---|
358 | outconvertclass &outconvert, ostream &textout,
|
---|
359 | ostream &logout) {
|
---|
360 |
|
---|
361 |
|
---|
362 | text_tarray wizard_split;
|
---|
363 | const text_t& wizard = (args["wizard"]=="") ? (text_t)"buildcol" : args["wizard"];
|
---|
364 | splitchar(wizard.begin(),wizard.end(),':',wizard_split);
|
---|
365 | const text_t& wizard_last = wizard_split.back();
|
---|
366 |
|
---|
367 | if (wizard_last != "buildframe")
|
---|
368 | {
|
---|
369 | text_t headmess = "_build:header_(_build:headmess" + wizard_last + "_)\n";
|
---|
370 | text_t content = "_build:content" + wizard_last + "_\n";
|
---|
371 |
|
---|
372 | textout << outconvert << disp << headmess << content << "_build:wizardfooter_\n";
|
---|
373 | }
|
---|
374 | else
|
---|
375 | {
|
---|
376 | text_t dirname = (args["bc1dirname"] == "") ? (text_t)"unknown" : args["bc1dirname"];
|
---|
377 | text_t tmpname = dirname+"_XXXXXX";
|
---|
378 | if (mktemp(tmpname.getcstr())==NULL)
|
---|
379 | {
|
---|
380 | logout << "Failed to create temporary filename" << endl;
|
---|
381 | }
|
---|
382 | disp.setmacro("bcargtmpname","build",tmpname);
|
---|
383 |
|
---|
384 |
|
---|
385 | text_t headmess = "_build:frameheader_(_build:headmess" + wizard_last + "_)\n";
|
---|
386 | text_t content = "_build:content" + wizard_last + "_\n";
|
---|
387 |
|
---|
388 | textout << outconvert << disp << headmess << content << "_build:framefooter_\n";
|
---|
389 |
|
---|
390 | text_t cmd = filename_cat(GSDL_GSDLHOME,"cgi-bin","webpage_buildcol.pl");
|
---|
391 | cmd += " gsdlhome=\""+args["gsdlhome"]+"\" httpbuild=\""+args["httpbuild"]+"\"";
|
---|
392 | cmd += " bc1copydata=\""+args["bc1copydata"]+"\"";
|
---|
393 | cmd += " bc1doimport=\""+args["bc1doimport"]+"\"";
|
---|
394 | cmd += " bc1dobuild=\""+args["bc1dobuild"]+"\"";
|
---|
395 | cmd += " bc1dirname=\""+args["bc1dirname"]+"\" bc1tmpname=\""+tmpname+"\" &";
|
---|
396 |
|
---|
397 | system(cmd.getcstr());
|
---|
398 | }
|
---|
399 |
|
---|
400 | return true;
|
---|
401 | }
|
---|
402 |
|
---|
403 | bool buildaction::do_buildstatus (cgiargsclass &args, displayclass &disp,
|
---|
404 | outconvertclass &outconvert, ostream &textout,
|
---|
405 | ostream &/*logout*/) {
|
---|
406 |
|
---|
407 | disp.setmacro("contentpara","buildstatus", args["mess"]);
|
---|
408 |
|
---|
409 | textout << outconvert << disp << ("_buildstatus:header_\n")
|
---|
410 | << ("_buildstatus:content_\n")
|
---|
411 | << ("_buildstatus:footer_\n");
|
---|
412 |
|
---|
413 | return true;
|
---|
414 | }
|
---|
415 |
|
---|
416 | bool buildaction::do_delcol (cgiargsclass &/*args*/, displayclass &disp,
|
---|
417 | outconvertclass &outconvert, ostream &textout,
|
---|
418 | ostream &/*logout*/) {
|
---|
419 |
|
---|
420 | textout << outconvert << disp << ("_build:header_(_build:headmessdelcol_)\n")
|
---|
421 | << ("_build:contentdelcol_\n")
|
---|
422 | << ("_build:footer_\n");
|
---|
423 |
|
---|
424 | return true;
|
---|
425 | }
|
---|
426 |
|
---|
427 |
|
---|
428 | bool buildaction::do_mess (cgiargsclass &args, displayclass &disp,
|
---|
429 | outconvertclass &outconvert, ostream &textout,
|
---|
430 | ostream &/*logout*/) {
|
---|
431 |
|
---|
432 | disp.setmacro("contentpara","buildmess", args["mess"]);
|
---|
433 | disp.setmacro("headmess","buildmess", args["head"]);
|
---|
434 | textout << outconvert << disp << ("_build:header_(_buildmess:headmess_)\n")
|
---|
435 | << ("_buildmess:content_\n")
|
---|
436 | << ("_build:footer_\n");
|
---|
437 |
|
---|
438 | return true;
|
---|
439 | }
|
---|