/********************************************************************** * * langaction.cpp -- * Copyright (C) 1999 The New Zealand Digital Library Project * * A component of the Greenstone digital library software * from the New Zealand Digital Library Project at the * University of Waikato, New Zealand. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * *********************************************************************/ #include #include #include "langaction.h" #include "fileutil.h" #include "gsdltools.h" #include "gsdlunicode.h" #include "langdb.h" #include "errno.h" #include /*====================* * GLOBAL DEFINITIONS * *====================*/ // text file reading buffer const int BUFFSIZE = 1024; char temp[BUFFSIZE]; /*========================* * LOCAL UTILITY ROUTINES * *========================*/ // gets the list of packages/files that contain the content of the web-forms text_tarray get_pagenos(text_t dir, ostream &logout) { text_tarray pagenos; // file 'logfile' contains all the page numbers, one number per line text_t logfile = filename_cat(dir, "package_forms", "pageno.log"); //open the file as a read only file ifstream readfile(logfile.getcstr(), ios::in); // if file isn't opened, print error and return if (!readfile) { logout << "File " << logfile.getcstr() << " could not be found.\n"; return pagenos; } // go through file, pushing each page number on to the end of a text_tarray while (readfile.getline(temp, BUFFSIZE-1)) { text_t t_temp = temp; pagenos.push_back(temp); } readfile.close(); //returns the array of page numbers return pagenos; } bool text_t_substring (text_t &text, text_t &substr) { int SIZE = substr.size(); if (text.size() >= SIZE) { if (substr <= text) { if (text[0] == substr[0]) { text_t temp = ""; // copy first SIZE elements of 'text' into 'temp' for (int size = 0; size < SIZE; size++) { temp.push_back(text[size]); } if (temp == substr) return true; } } } return false; } bool langaction::init (ostream & /*logout*/) { // set up GSDLOS, GSDLHOME and PATH environment variables text_t gsdlos, path; unsigned int path_separator = ':'; #if defined (__WIN32__) gsdlos = "windows"; path_separator = ';'; path = filename_cat (gsdlhome, "bin", "windows", "perl", "bin;"); #else struct utsname *buf = new struct utsname(); int i = uname (buf); if (i == -1) gsdlos = "linux"; // uname failed else gsdlos.setcstr (buf->sysname); delete buf; lc (gsdlos); #endif pathc = getenv ("PATH"); path += filename_cat (gsdlhome, "bin", gsdlos); path.push_back (path_separator); path += filename_cat (gsdlhome, "bin", "script"); if (pathc != NULL) { path.push_back (path_separator); path += pathc; } path = "PATH=" + path; gsdlos = "GSDLOS=" + gsdlos; text_t setgsdlhome = "GSDLHOME=" + gsdlhome; // these will be cleaned up in the destructor gsdlosc = gsdlos.getcstr(); gsdlhomec = setgsdlhome.getcstr(); pathc = path.getcstr(); putenv (gsdlosc); putenv (gsdlhomec); putenv (pathc); return true; } /*====================* * CLASS DEFINITIONS * *====================*/ langaction::langaction () { package = ""; // this action uses cgi variables "a", and "p" cgiarginfo arg_ainfo; arg_ainfo.shortname = "l"; arg_ainfo.longname = "language"; arg_ainfo.multiplechar = true; arg_ainfo.defaultstatus = cgiarginfo::weak; arg_ainfo.argdefault = "en"; arg_ainfo.savedarginfo = cgiarginfo::must; argsinfo.addarginfo (NULL, arg_ainfo); } langaction::~langaction () { } bool langaction::check_cgiargs (cgiargsinfoclass &/*argsinfo*/, cgiargsclass &args, ostream &logout) { // authenticate the user before allowing modifications // args["uan"] = 1; // args["ug"] = "langadmin"; return true; } void langaction::get_cgihead_info (cgiargsclass &/*args*/, recptprotolistclass * /*protos*/, response_t &response,text_t &response_data, ostream &/*logout*/) { response = content; response_data = "text/html"; } void langaction::define_internal_macros (displayclass &disp, cgiargsclass &args, recptprotolistclass *protos, ostream &logout) { text_t text = ""; text_t cmd = ""; text_t &arg_p = args["p"]; text_tarray pagenos; text_t dir = filename_cat(gsdlhome, "tmp", "lang"); text_t pageno =""; text_t lang = ""; // argument for page is of the form 'macrofile_pageno' so // we need to split the argument to get the proper page content text_tarray splitarray; splitchar(arg_p.begin(), arg_p.end(), '_', splitarray); arg_p = splitarray[0]; if (splitarray.size() > 1) pageno = splitarray[splitarray.size()-1]; if (pageno == "picklanguage") pageno = "1"; if (pageno.empty()) pageno = "picklanguage"; //produce the picklanguage webpage if (pageno == "picklanguage") { cmd = "perl " + filename_cat(gsdlhome, "bin", "script", "picklanguage.pl"); if(gsdl_system(cmd,true,logout) != 0) return; define_webpage(disp, protos, logout, pageno, lang, dir, pagenos, text); return; } //creates output stream to argsfile (dir is gsdlhome/tmp/lang) text_t argsfile = filename_cat(dir, "arguments.arg"); ofstream argsout(argsfile.getcstr(), ios::out); //informs user and returns if output stream not opened if(!argsout) { logout << "File " << argsfile.getcstr() << " could not be opened\n"; return; } //ensures access permissions make the file world writable text_t changemode = "chmod a+w "; changemode += argsfile; gsdl_system(changemode, false, logout); //write out the cgiargsclass passes as a parameter to the subroutine to the argsout file argsout << args; //close the argsout output stream argsout.close(); //sets the current foreign language if (args["ownchoice"] != "") { lang = args["ownchoice"]; } else if (args["hiddenlanguage"] != "") { lang = args["hiddenlanguage"]; } else { lang = args["language"]; } text_t baselanguage = ""; //sets current base language if (args["baselanguage"] != "") { baselanguage = args["baselanguage"]; } //calls the translator file to create HTML files cmd = "perl " + filename_cat(gsdlhome, "bin", "script", "translator.pl") + " " + lang + " " + baselanguage; if ((gsdl_system(cmd, true, logout)) != 0) { logout << "Process " << cmd.getcstr() << " did not execute.../;-D\n"; return; } //if no more pages will set pageno to thankyou script pagenos = get_pagenos(dir,logout); if (pagenos.empty()) pageno = "thankyou"; text_t &submitargs = args[pageno]; text_t submit = "SUBMIT TRANSLATION >>"; //if the SUBMIT TRANSLATION >> button has been pushed if (text_t_substring(submitargs,submit)) { //create the command line for submitting the translation cmd = "perl " + filename_cat(gsdlhome, "bin", "script", "submit_translation.pl") + " "; cmd += (argsfile + " " + lang + " " + baselanguage); //give command to system to execute submission of translation if((gsdl_system(cmd, true, logout)) != 0) logout << "Process " << cmd.getcstr() << " did not execute\n"; //increments which page is displayed when translation is submitted for (text_tarray::iterator page_itr = pagenos.begin(); page_itr != pagenos.end(); page_itr++) { if (*page_itr == pageno) { page_itr++; if (page_itr != pagenos.end()) pageno = *page_itr; else pageno = "thankyou"; break; } } } //want it to automatically load the next page here instead of reloading page //that has just been translated with everything empty again. define_webpage(disp, protos, logout, pageno, lang, dir, pagenos, text); } void langaction::define_webpage (displayclass &disp, recptprotolistclass *protos, ostream &logout, text_t &pageno, text_t &lang, text_t &dir, text_tarray &pagenos, text_t &text) { // produces HTML page with the current pages contents // formfile is the file that contains web-form for the // current package whose name is stored in 'package' text_t formfile = filename_cat(dir, "package_forms"); formfile += ("/" + pageno + ".lang"); ifstream readfile(formfile.getcstr(), ios::in); if (!readfile) { logout << "File " << formfile.getcstr() << " could not be found.\n"; return; } /* ============================ START WEB-FORM HTML CODE ============================ */ text += "\n"; text += "\n\n"; text += "
\n"; text += "

"; text += "

"; text += "Greenstone Language Translation Facility

\n"; //if not a special case page display translation info if ((pageno != "thankyou") && (pageno !="picklanguage")) { text += "

You are currently translating page "; text += pageno; text += " of "; text += pagenos.size(); text += " in to "; text += lang; text += "

\n"; } //read in HTML from file while(readfile.getline(temp, BUFFSIZE-1)) { text_t t_temp = temp; text += t_temp; text += "\n"; } readfile.close(); //if not a special case page display translation info if ((pageno != "thankyou") && (pageno != "picklanguage")) { text += "

You are currently translating page "; text += pageno; text += " of "; text += pagenos.size(); text += " in to "; text += lang; text += "

\n"; } /* ============================ END WEB-FORM HTML CODE ============================ */ // THE FOLLOWING MACROS ARE DEFINED IN translang.dm MACRO FILE // set the formcontent as the content of the form-page disp.setmacro("formcontent", "translang", text); // set the action for the form // disp.setmacro("formaction", "translang", "/cgi-bin/dg5/library"); disp.setmacro("formaction", "translang", "_gwcgi_"); } bool langaction::do_action (cgiargsclass &args, recptprotolistclass * /*protos*/, browsermapclass * /*browsers*/, displayclass &disp, outconvertclass &outconvert, ostream &textout, ostream &logout) { text_t &arg_p = args["p"]; textout << outconvert << disp << ("_" + arg_p + ":header_\n") << ("_" + arg_p + ":content_\n") << ("_" + arg_p + ":footer_\n"); return true; }