source: trunk/gsdl/src/recpt/langaction.cpp@ 3632

Last change on this file since 3632 was 3632, checked in by sjboddie, 21 years ago

Added langaction

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 10.8 KB
Line 
1 /**********************************************************************
2 *
3 * langaction.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 <stdio.h>
27#include <fstream.h>
28#include "langaction.h"
29#include "fileutil.h"
30#include "gsdltools.h"
31#include "gsdlunicode.h"
32#include "langdb.h"
33#include "errno.h"
34#include <sys/utsname.h>
35
36
37/*====================*
38 * GLOBAL DEFINITIONS *
39 *====================*/
40
41// text file reading buffer
42const int BUFFSIZE = 1024;
43char temp[BUFFSIZE];
44
45
46
47/*========================*
48 * LOCAL UTILITY ROUTINES *
49 *========================*/
50
51// gets the list of packages/files that contain the content of the web-forms
52text_tarray get_pagenos(text_t dir, ostream &logout)
53{
54 text_tarray pagenos;
55
56 // file 'logfile' contains all the page numbers, one number per line
57 text_t logfile = filename_cat(dir, "package_forms", "pageno.log");
58
59 //open the file as a read only file
60 ifstream readfile(logfile.getcstr(), ios::in);
61
62 // if file isn't opened, print error and return
63 if (!readfile) {
64 logout << "File " << logfile.getcstr() << " could not be found.\n";
65 return pagenos;
66 }
67
68 // go through file, pushing each page number on to the end of a text_tarray
69 while (readfile.getline(temp, BUFFSIZE-1)) {
70 text_t t_temp = temp;
71 pagenos.push_back(temp);
72 }
73 readfile.close();
74
75 //returns the array of page numbers
76 return pagenos;
77}
78
79bool text_t_substring (text_t &text, text_t &substr)
80{
81 int SIZE = substr.size();
82
83 if (text.size() >= SIZE) {
84 if (substr <= text) {
85 if (text[0] == substr[0]) {
86 text_t temp = "";
87 // copy first SIZE elements of 'text' into 'temp'
88 for (int size = 0; size < SIZE; size++) {
89 temp.push_back(text[size]);
90 }
91 if (temp == substr)
92 return true;
93 }
94 }
95 }
96
97 return false;
98}
99
100bool langaction::init (ostream & /*logout*/) {
101
102 // set up GSDLOS, GSDLHOME and PATH environment variables
103 text_t gsdlos, path;
104 unsigned int path_separator = ':';
105#if defined (__WIN32__)
106 gsdlos = "windows";
107 path_separator = ';';
108
109 path = filename_cat (gsdlhome, "bin", "windows", "perl", "bin;");
110
111#else
112 struct utsname *buf = new struct utsname();
113 int i = uname (buf);
114 if (i == -1) gsdlos = "linux"; // uname failed
115 else gsdlos.setcstr (buf->sysname);
116 delete buf;
117 lc (gsdlos);
118#endif
119
120 pathc = getenv ("PATH");
121 path += filename_cat (gsdlhome, "bin", gsdlos);
122 path.push_back (path_separator);
123 path += filename_cat (gsdlhome, "bin", "script");
124 if (pathc != NULL) {
125 path.push_back (path_separator);
126 path += pathc;
127 }
128 path = "PATH=" + path;
129
130 gsdlos = "GSDLOS=" + gsdlos;
131 text_t setgsdlhome = "GSDLHOME=" + gsdlhome;
132
133 // these will be cleaned up in the destructor
134 gsdlosc = gsdlos.getcstr();
135 gsdlhomec = setgsdlhome.getcstr();
136 pathc = path.getcstr();
137
138 putenv (gsdlosc);
139 putenv (gsdlhomec);
140 putenv (pathc);
141
142 return true;
143}
144/*====================*
145 * CLASS DEFINITIONS *
146 *====================*/
147
148langaction::langaction () {
149
150 package = "";
151
152 // this action uses cgi variables "a", and "p"
153 cgiarginfo arg_ainfo;
154
155 arg_ainfo.shortname = "l";
156 arg_ainfo.longname = "language";
157 arg_ainfo.multiplechar = true;
158 arg_ainfo.defaultstatus = cgiarginfo::weak;
159 arg_ainfo.argdefault = "en";
160 arg_ainfo.savedarginfo = cgiarginfo::must;
161 argsinfo.addarginfo (NULL, arg_ainfo);
162}
163
164langaction::~langaction () {
165}
166
167bool langaction::check_cgiargs (cgiargsinfoclass &/*argsinfo*/, cgiargsclass &args,
168 ostream &logout) {
169 // authenticate the user before allowing modifications
170 // args["uan"] = 1;
171 // args["ug"] = "langadmin";
172
173 return true;
174}
175
176void langaction::get_cgihead_info (cgiargsclass &/*args*/, recptprotolistclass * /*protos*/,
177 response_t &response,text_t &response_data,
178 ostream &/*logout*/) {
179 response = content;
180 response_data = "text/html";
181}
182
183void langaction::define_internal_macros (displayclass &disp, cgiargsclass &args,
184 recptprotolistclass *protos, ostream &logout) {
185 text_t text = "";
186 text_t cmd = "";
187 text_t &arg_p = args["p"];
188 text_tarray pagenos;
189 text_t dir = filename_cat(gsdlhome, "tmp", "lang");
190 text_t pageno ="";
191 text_t lang = "";
192
193 // argument for page is of the form 'macrofile_pageno' so
194 // we need to split the argument to get the proper page content
195 text_tarray splitarray;
196 splitchar(arg_p.begin(), arg_p.end(), '_', splitarray);
197
198 arg_p = splitarray[0];
199 if (splitarray.size() > 1)
200 pageno = splitarray[splitarray.size()-1];
201
202 if (pageno == "picklanguage")
203 pageno = "1";
204
205 if (pageno.empty())
206 pageno = "picklanguage";
207
208 //produce the picklanguage webpage
209 if (pageno == "picklanguage") {
210 cmd = "perl " + filename_cat(gsdlhome, "bin", "script", "picklanguage.pl");
211 if(gsdl_system(cmd,true,logout) != 0)
212 return;
213 define_webpage(disp, protos, logout, pageno, lang, dir, pagenos, text);
214 return;
215 }
216
217 //creates output stream to argsfile (dir is gsdlhome/tmp/lang)
218 text_t argsfile = filename_cat(dir, "arguments.arg");
219 ofstream argsout(argsfile.getcstr(), ios::out);
220
221 //informs user and returns if output stream not opened
222 if(!argsout) {
223 logout << "File " << argsfile.getcstr() << " could not be opened\n";
224 return;
225 }
226
227 //ensures access permissions make the file world writable
228 text_t changemode = "chmod a+w ";
229 changemode += argsfile;
230 gsdl_system(changemode, false, logout);
231
232 //write out the cgiargsclass passes as a parameter to the subroutine to the argsout file
233 argsout << args;
234
235 //close the argsout output stream
236 argsout.close();
237
238 //sets the current foreign language
239 if (args["ownchoice"] != "") {
240 lang = args["ownchoice"];
241 }
242 else if (args["hiddenlanguage"] != "") {
243 lang = args["hiddenlanguage"];
244 }
245 else {
246 lang = args["language"];
247 }
248 text_t baselanguage = "";
249
250 //sets current base language
251 if (args["baselanguage"] != "") {
252 baselanguage = args["baselanguage"];
253 }
254
255
256 //calls the translator file to create HTML files
257 cmd = "perl " + filename_cat(gsdlhome, "bin", "script", "translator.pl") + " " + lang + " " + baselanguage;
258
259 if ((gsdl_system(cmd, true, logout)) != 0) {
260 logout << "Process " << cmd.getcstr() << " did not execute.../;-D\n";
261 return;
262 }
263
264 //if no more pages will set pageno to thankyou script
265 pagenos = get_pagenos(dir,logout);
266 if (pagenos.empty())
267 pageno = "thankyou";
268
269
270 text_t &submitargs = args[pageno];
271 text_t submit = "SUBMIT TRANSLATION >>";
272
273 //if the SUBMIT TRANSLATION >> button has been pushed
274 if (text_t_substring(submitargs,submit)) {
275
276 //create the command line for submitting the translation
277 cmd = "perl " + filename_cat(gsdlhome, "bin", "script", "submit_translation.pl") + " ";
278 cmd += (argsfile + " " + lang + " " + baselanguage);
279
280 //give command to system to execute submission of translation
281 if((gsdl_system(cmd, true, logout)) != 0)
282 logout << "Process " << cmd.getcstr() << " did not execute\n";
283
284 //increments which page is displayed when translation is submitted
285 for (text_tarray::iterator page_itr = pagenos.begin();
286 page_itr != pagenos.end();
287 page_itr++) {
288
289 if (*page_itr == pageno) {
290 page_itr++;
291 if (page_itr != pagenos.end())
292 pageno = *page_itr;
293 else
294 pageno = "thankyou";
295
296 break;
297 }
298 }
299 }
300 //want it to automatically load the next page here instead of reloading page
301 //that has just been translated with everything empty again.
302
303 define_webpage(disp, protos, logout, pageno, lang, dir, pagenos, text);
304}
305
306void langaction::define_webpage (displayclass &disp,
307 recptprotolistclass *protos, ostream &logout,
308 text_t &pageno, text_t &lang, text_t &dir,
309 text_tarray &pagenos, text_t &text) {
310
311 // produces HTML page with the current pages contents
312 // formfile is the file that contains web-form for the
313 // current package whose name is stored in 'package'
314
315 text_t formfile = filename_cat(dir, "package_forms");
316 formfile += ("/" + pageno + ".lang");
317
318 ifstream readfile(formfile.getcstr(), ios::in);
319 if (!readfile) {
320 logout << "File " << formfile.getcstr() << " could not be found.\n";
321 return;
322 }
323
324
325 /* ============================ START WEB-FORM HTML CODE ============================ */
326 text += "\n<input type=hidden name=\"a\" value=\"lang\">";
327 text += "\n<input type=hidden name=\"p\" value=\"translang_";
328 text += pageno;
329 text += "\">\n";
330
331 //if not a special case page display translation info
332 if ((pageno != "thankyou") && (pageno !="picklanguage")) {
333 text += "<center><strong>You are currently translating page ";
334 text += pageno;
335 text += " of ";
336 text += pagenos.size();
337 text += " in to ";
338 text += lang;
339 text += "</strong></center><p><p>\n";
340 }
341
342 //read in HTML from file
343 while(readfile.getline(temp, BUFFSIZE-1)) {
344 text_t t_temp = temp;
345 text += t_temp;
346 text += "\n";
347 }
348 readfile.close();
349
350 //if not a special case page display translation info
351 if ((pageno != "thankyou") && (pageno != "picklanguage")) {
352 text += "<center><strong>You are currently translating page ";
353 text += pageno;
354 text += " of ";
355 text += pagenos.size();
356 text += " in to ";
357 text += lang;
358 text += "</strong></center><br>\n";
359 }
360
361
362 /* ============================ END WEB-FORM HTML CODE ============================ */
363
364 // THE FOLLOWING MACROS ARE DEFINED IN translang.dm MACRO FILE
365 // set the formcontent as the content of the form-page
366 disp.setmacro("formcontent", "translang", text);
367
368 // set the action for the form
369 // disp.setmacro("formaction", "translang", "/cgi-bin/dg5/library");
370 disp.setmacro("formaction", "translang", "_gwcgi_");
371
372}
373
374bool langaction::do_action (cgiargsclass &args, recptprotolistclass * /*protos*/,
375 browsermapclass * /*browsers*/, displayclass &disp,
376 outconvertclass &outconvert, ostream &textout,
377 ostream &logout) {
378 text_t &arg_p = args["p"];
379
380 textout << outconvert << disp << ("_" + arg_p + ":header_\n")
381 << ("_" + arg_p + ":content_\n")
382 << ("_" + arg_p + ":footer_\n");
383
384 return true;
385}
386
387
388
389
Note: See TracBrowser for help on using the repository browser.