Changeset 8102


Ignore:
Timestamp:
2004-09-08T13:34:23+12:00 (20 years ago)
Author:
mdewsnip
Message:

Unfinished, but I'm committing it now so I don't lose it.

Location:
trunk/gsdl
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/gsdl/perllib/translator.pm

    r5276 r8102  
    5555sub read_languages_from_main_cfg_file
    5656{
    57     local %name2abbrhash = ();
     57    local %code2namehash = ();
    5858
    5959    # Read the main.cfg file
     
    6868    # Parse the lines containing a language definition
    6969    if ($line =~ m/^Language\s+/i) {
    70         local @lineparts = split(/\s+/, $line);
    71         local ($langabbr) = ($lineparts[1] =~ m/^shortname=(.*)$/);
    72         local ($langname) = ($lineparts[2] =~ m/^longname=(.*)$/);
    73 
    74         # Casefold language name and code
    75         $langabbr =~ tr/A-Z/a-z/;
    76         $langname =~ tr/A-Z/a-z/;
    77 
    78         $name2abbrhash{$langname} = $langabbr;
     70        local ($langcode) = ($line =~ /shortname=(\S+)/i);
     71        local ($langname) = ($line =~ /longname=(\"[^\"]+\"|\S+)/i);
     72
     73        # Casefold the language code, and remove any quotes around the language name
     74        $langcode =~ tr/A-Z/a-z/;
     75        $langname =~ s/^\"(.+)\"$/$1/;
     76
     77        $code2namehash{$langcode} = $langname;
    7978    }
    8079    }
    8180    close(MAIN_CFG_FILE);
    8281
    83     return %name2abbrhash;
     82    return %code2namehash;
    8483}
    8584
     
    10099    # Check if a file is being defined
    101100    if ($line =~ m/^File\s+/i) {
    102         local @lineparts = split(/\s+/, $line);
    103         local ($fileid) = ($lineparts[1] =~ m/^id=(.*)$/i);
    104         local ($filedesc) = ($lineparts[2] =~ m/^description=(.*)$/i);
     101        local ($fileid) = ($line =~ /id=(\S+)/i);
     102        local ($filedesc) = ($line =~ /description=(\S+)/i);
    105103        $name2deschash{$fileid} = $filedesc;
    106104    }
     
    114112sub read_file_data_from_config_file
    115113{
    116     my ($sourcelang, $sourceabbr, $targetlang, $targetabbr, $file) = @_;
     114    my ($sourceabbr, $targetabbr, $file) = @_;
    117115
    118116    # Read the translator config file
     
    137135        local ($filetype) = ($lineparts[5] =~ m/^type=(.*)$/);
    138136
    139         # Resolve instances of {sourcelang}, {sourceabbr}, {targetlang}, {targetabbr}
    140         $sourcefile =~ s/\{sourcelang\}/$sourcelang/g;
     137        # Determine sourcebase and targetbase, using the filetype
     138        local $sourcebase = &get_file_base($sourceabbr, $filetype);
     139        local $targetbase = &get_file_base($targetabbr, $filetype);
     140
     141        # Resolve instances of {sourceabbr}, {sourcebase}, {targetabbr}, {targetbase}
    141142        $sourcefile =~ s/\{sourceabbr\}/$sourceabbr/g;
    142         $targetfile =~ s/\{targetlang\}/$targetlang/g;
     143        $sourcefile =~ s/\{sourcebase\}/$sourcebase/g;
    143144        $targetfile =~ s/\{targetabbr\}/$targetabbr/g;
     145        $targetfile =~ s/\{targetbase\}/$targetbase/g;
    144146
    145147        close(CONFIG_FILE);
     
    164166
    165167
     168sub get_file_base
     169{
     170    my ($langabbr, $filetype) = @_;
     171
     172    if ($filetype =~ /^macrofile/) {
     173    return &get_macrofile_base($langabbr);
     174    }
     175    if ($filetype =~ /^resourcebundle/ || $filetype =~ /^myresourcebundle/) {
     176    return &get_resource_bundle_base($langabbr);
     177    }
     178
     179    print STDERR "Error: Unknown file type $filetype.\n";
     180    die "\n";
     181}
     182
     183
    166184sub process_file
    167185{
     
    185203    if ($filetype =~ /^resourcebundle/) {
    186204    return &build_key_to_line_mapping_for_resource_bundle($filepath);
     205    }
     206    if ($filetype =~ /^myresourcebundle/) {
     207    return &build_key_to_line_mapping_for_my_resource_bundle($filepath);
    187208    }
    188209
     
    232253    if ($filetype =~ /^resourcebundle/) {
    233254    return &import_chunk_from_resource_bundle($chunktext);
     255    }
     256    if ($filetype =~ /^myresourcebundle/) {
     257    return &import_chunk_from_my_resource_bundle($chunktext);
    234258    }
    235259
     
    323347# ==========================================================================================
    324348
     349sub get_macrofile_base
     350{
     351    local ($langcode) = @_;
     352
     353    # List special cases here (macrofile names differ from language longnames)
     354    local %specialcases = ('id', 'indo',
     355               'pt-br', 'port-br',
     356               'pt-pt', 'port-pt',
     357               'zh', 'chinese' );
     358
     359    # Special cases override main.cfg entries
     360    if ($specialcases{$langcode}) {
     361    return $specialcases{$langcode};
     362    }
     363
     364    # Use the language name defined in the main.cfg file as the macrofile name
     365    my %langhash = &read_languages_from_main_cfg_file();
     366    my $langname = $langhash{$langcode} || die "Error: Language to base mapping undefined!\n";
     367    $langname =~ tr/A-Z/a-z/;
     368    return $langname;
     369}
     370
     371
    325372sub build_key_to_line_mapping_for_macrofile
    326373{
     
    355402        # While there is still text of the macro to go...
    356403        local $startline = $i;
    357         while ($line !~ /.*\}/) {
     404        while ($line !~ /\}$/) {
    358405        $i++;
    359406        $line = $lines[$i];
     407        $line =~ s/(\s*)$//;  # Remove any nasty whitespace, carriage returns etc.
    360408        }
    361409
     
    373421        local $startline = $i;
    374422        unless ($line =~ m/^\#\# .*\#\#/) {
    375         $line = $lines[++$i];
     423        $i++;
     424        $line = $lines[$i];
    376425        $macroname .= $line;
    377426        }
     
    383432
    384433        # Read the rest of the text associated with the image macro
    385         while ($line !~ /^\s*$/) {
     434        while (defined($line) && $line !~ /^\s*$/) {
    386435        $i++;
    387436        $line = $lines[$i];
     
    404453
    405454    # Is this an icon macro??
    406     if ($chunktext =~ m/^\#\# (.*)/) {
     455    if ($chunktext =~ /^\#\# (.*)/) {
    407456    # Extract image macro text
    408     $chunktext =~ s/^\#\# ((.|\n)+) \#\# (.+) \#\# (.|\n)+/$1/;
     457    $chunktext =~ /^\#\#\s+([^\#]+)\s+\#\#/;
     458    $chunktext = $1;
    409459
    410460    # Remove enclosing quotes
    411     $chunktext =~ s/^\s*\"((.|\n)+)\"\s*/$1/;
     461    $chunktext =~ s/^\"//;
     462    $chunktext =~ s/\"$//;
    412463    }
    413464
     
    430481# ------------------------------------------------------------------------------------------
    431482
     483sub get_resource_bundle_base
     484{
     485    local ($langabbr) = @_;
     486
     487    return "";
     488}
     489
     490
    432491sub build_key_to_line_mapping_for_resource_bundle
    433492{
     
    451510
    452511    # Line contains a dictionary string
    453     if ($line =~ m/^((\w|\.)+):/) {
     512    if ($line =~ /^((\w|\.)+):(.*)$/) {
    454513        local $key = $1;
    455514
     
    474533
    475534
     535
     536# ------------------------------------------------------------------------------------------
     537
     538sub build_key_to_line_mapping_for_my_resource_bundle
     539{
     540    local ($filepath) = @_;
     541
     542    # Open file for reading
     543    if (!open(FILE_IN, "<$filepath")) {
     544    print STDERR "Error: Could not open resource bundle $filepath.\n";
     545    die "\n";
     546    }
     547    local @lines = <FILE_IN>;
     548    close FILE_IN;
     549
     550    # Initialise some local variables
     551    local %linehash = ();
     552
     553    # Process the contents of the file, line by line
     554    for ($i = 0; $i < scalar(@lines); $i++) {
     555    local $line = $lines[$i];
     556    $line =~ s/(\s*)$//;  # Remove any nasty whitespace, carriage returns etc.
     557
     558    # Line contains a dictionary string
     559    if ($line =~ /^((\w|\.|\-)+):\"(.*)$/) {
     560        local $key = $1;
     561        local $text = $3;
     562
     563        # While there is still text of the chunk to go...
     564        local $startline = $i;
     565        while ($text !~ /\"$/ || $text =~ /\\\"$/) {
     566        $i++;
     567        $text = $lines[$i];
     568        }
     569
     570        $linehash{$key} = $startline . "-" . $i;
     571    }
     572    }
     573
     574    return %linehash;
     575}
     576
     577
     578sub import_chunk_from_my_resource_bundle
     579{
     580    local ($chunktext) = @_;
     581
     582    # Remove chunk key and quotes
     583    $chunktext =~ s/^((\w|\.|\-)+)://;
     584    $chunktext =~ s/^\"//;
     585    $chunktext =~ s/\"$//;
     586
     587    return $chunktext;
     588}
     589
     590
    4765911;
  • trunk/gsdl/src/recpt/langaction.cpp

    r7433 r8102  
    4848
    4949
    50 /*========================*
    51  * LOCAL UTILITY ROUTINES *
    52  *========================*/
    53 
    54 bool text_t_substring (text_t &text, text_t &substr)
    55 {
    56   int SIZE = substr.size();
    57 
    58   if (text.size() >= SIZE) {
    59     if (substr <= text) {
    60       if (text[0] == substr[0]) {
    61     text_t temp = "";
    62     // copy first SIZE elements of 'text' into 'temp'
    63     for (int size = 0; size < SIZE; size++) {
    64       temp.push_back(text[size]);
    65     }
    66     if (temp == substr)
    67       return true;
    68       }
    69     }
    70   }
    71 
    72   return false;
    73 }
    74 
    75 
    7650/*====================*
    7751 * CLASS DEFINITIONS  *
    7852 *====================*/
    7953
    80 langaction::langaction () {
    81 
     54langaction::langaction()
     55{
    8256  package = "";
    8357
     
    9266  argsinfo.addarginfo (&cerr, arg_ainfo);
    9367
    94   arg_ainfo.shortname = "bl";
    95   arg_ainfo.longname = "base language";
     68  arg_ainfo.shortname = "slng";
     69  arg_ainfo.longname = "translation source language";
    9670  arg_ainfo.multiplechar = true;
    9771  arg_ainfo.defaultstatus = cgiarginfo::weak;
    98   arg_ainfo.argdefault = "english";
     72  arg_ainfo.argdefault = "en";
    9973  arg_ainfo.savedarginfo = cgiarginfo::must;
    10074  argsinfo.addarginfo (&cerr, arg_ainfo);
    10175
    10276  arg_ainfo.shortname = "tlng";
    103   arg_ainfo.longname = "translation language";
     77  arg_ainfo.longname = "translation target language";
    10478  arg_ainfo.multiplechar = true;
    10579  arg_ainfo.defaultstatus = cgiarginfo::weak;
    106   arg_ainfo.argdefault = "french";
     80  arg_ainfo.argdefault = "fr";
    10781  arg_ainfo.savedarginfo = cgiarginfo::must;
    10882  argsinfo.addarginfo (&cerr, arg_ainfo);
     
    11084
    11185
     86
    11287langaction::~langaction () {
    11388  // Nothing to do in destructor
    11489}
     90
    11591
    11692
     
    163139
    164140
     141
    165142bool langaction::check_cgiargs (cgiargsinfoclass &/*argsinfo*/, cgiargsclass &args,
    166143                recptprotolistclass* /*protos*/, ostream &logout)
    167144{
    168145  // Authenticate the user before allowing modifications
    169   args["uan"] = 1;
    170   args["ug"] = "langadmin";
     146  // args["uan"] = 1;
     147  // args["ug"] = "langadmin";
    171148  return true;
    172149}
     150
    173151
    174152
     
    182160
    183161
     162
    184163void langaction::define_internal_macros (displayclass &disp, cgiargsclass &args,
    185164                     recptprotolistclass *protos, ostream &logout)
    186165{
    187   text_t text = "";
    188   text_t cmd = "";
    189   text_t dir = filename_cat(gsdlhome, "tmp", "lang");
    190   text_t page = "";
    191   text_t sourcelang = "";
    192   text_t targetlang = "";
    193 
    194   // Get the source language from the CGI arguments
    195   if (args["bl"] != "") {
    196     sourcelang = args["bl"];
    197   }
    198 
    199   // Get the target language from the CGI arguments
    200   if (args["ownchoice"] != "") {
    201     targetlang = args["ownchoice"];
    202   }
    203   else if (args["language"] != "") {
    204     targetlang = args["language"];
    205   }
    206   else if (args["tlng"] != "") {
    207     targetlang = args["tlng"];
    208   }
    209 
    210   // Case fold both languages
    211   for (int i = 0; i < strlen(sourcelang.getcstr()); i++) {
    212     sourcelang[i] = tolower(sourcelang[i]);
    213   }
    214   for (int i = 0; i < strlen(targetlang.getcstr()); i++) {
    215     targetlang[i] = tolower(targetlang[i]);
    216   }
     166  text_t scriptdir = filename_cat(gsdlhome, "bin", "script", "translator");
     167  text_t translatordir = filename_cat(gsdlhome, "translator");
     168
     169  text_t page = args["p"];
     170  logout << endl << "Page arg: " << page << endl;
     171  args["p"] = "translang";
     172
     173  if (page == "translang") {
     174    // Run the inittranslator.pl script to generate the translation agency start page
     175    text_t cmd = "perl " + filename_cat(scriptdir, "inittranslator.pl");
     176    if (gsdl_system(cmd, true, logout) != 0) {
     177      logout << "Error: Command " << cmd << " did not execute.\n";
     178      return;
     179    }
     180
     181    define_webpage(disp, translatordir, "picklanguage", false, logout);
     182    return;
     183  }
     184
     185  // Get the source and target languages, and file to translate, from the CGI arguments
     186  text_t sourceabbr = args["slng"];
     187  text_t targetabbr = args["tlng"];
     188  text_t transfile = args["file"];
     189
     190  // Case fold both language codes, just in case
     191  for (int i = 0; i < strlen(sourceabbr.getcstr()); i++) {
     192    sourceabbr[i] = tolower(sourceabbr[i]);
     193  }
     194  for (int i = 0; i < strlen(targetabbr.getcstr()); i++) {
     195    targetabbr[i] = tolower(targetabbr[i]);
     196  }
     197
     198  logout << "Source abbr: " << sourceabbr << " Target abbr: " << targetabbr << endl;
     199  logout << "Trans file: " << transfile << endl;
    217200
    218201  // overwrite the global content (which should be overwritten by package)
     
    220203  disp.setmacro("content", displayclass::defaultpackage, "\\_translang:content\\_");
    221204
    222   // Make sure the source and target languages are different!
    223   if (sourcelang == targetlang) {
     205  // Make sure the source and target languages are different
     206  if (sourceabbr == targetabbr) {
    224207    logout << "Same language!" << endl;
    225     args["p"] = "translang";
    226     page = "picklanguage";
    227     define_webpage(disp, sourcelang, targetlang, dir, page, -1, true, logout);
     208    define_webpage(disp, translatordir, "picklanguage", true, logout);
    228209    return;
    229210  }
    230211
    231   // logout << "Source lang: " << sourcelang << " Target lang: " << targetlang << endl;
    232   text_t translation = sourcelang + "-" + targetlang;
    233   logout << "Translation: " << translation << endl;
    234   text_t trans_dir = filename_cat(dir, translation);
    235 
    236   // argument for page is of the form 'macrofile_pageno' so
    237   // we need to split the argument to get the proper page content
    238   text_t &arg_p = args["p"];
     212  text_t translation = sourceabbr + "-" + targetabbr;
     213  text_t translationdir = filename_cat(translatordir, translation, transfile);
     214  logout << "Translation dir: " << translationdir << endl;
     215
     216  if (page == "translang_picklanguage") {
     217    // Run the inittranslation.pl script to make sure everything is ready
     218    text_t cmd = "perl " + filename_cat(scriptdir, "inittranslation.pl") +
     219      " " + sourceabbr + " " + targetabbr + " " + transfile;
     220    if (gsdl_system(cmd, true, logout) != 0) {
     221      logout << "Error: Command " << cmd << " did not execute...\n";
     222      return;
     223    }
     224
     225    // Run the writetranslationmedium.pl script to generate the HTML pages
     226    cmd = "perl " + filename_cat(scriptdir, "writetranslationmedium.pl") +
     227      " " + sourceabbr + " " + targetabbr + " " + transfile + " " + "html";
     228    if (gsdl_system(cmd, true, logout) != 0) {
     229      logout << "Error: Command " << cmd << " did not execute...\n";
     230      return;
     231    }
     232
     233    // Send an email notification to the site maintainer
     234    text_t mailfilepath = filename_cat(translationdir, "email.txt");
     235    char *mailfilenamec = mailfilepath.getcstr();
     236    ofstream mailfile (mailfilenamec);
     237    if (!mailfile) {
     238      logout << "Error: Couldn't write file " << mailfilepath << "." << endl;
     239      return;
     240    }
     241
     242    mailfile << "[Translator Event]\n"
     243         << "Date: " << get_date (true) << "\n"
     244         << "Greenstone Username: " << args["un"] << "\n"
     245         << "The " << targetabbr << " language translation was entered.\n";
     246    mailfile.close();
     247
     248    const recptconf &rcinfo = recpt->get_configinfo();
     249
     250    // Use sendmail.pl perl script to send an email notification
     251    text_t sendmail_cmd = "perl -S sendmail.pl";
     252    sendmail_cmd += " -to \"" + rcinfo.maintainer + "\"";
     253    sendmail_cmd += " -from \"[email protected]\"";
     254    sendmail_cmd += " -smtp \"" + rcinfo.MailServer + "\"";
     255    sendmail_cmd += " -subject \"Greenstone Translator Event\"";
     256    sendmail_cmd += " -msgfile \"" + mailfilepath + "\"";
     257
     258    // if (gsdl_system(sendmail_cmd, true, logout) != 0) {
     259    // logout << "Error: Command " << sendmail_cmd << " did not execute...\n";
     260    // return;
     261    // }
     262
     263    // Finished with the temporary file
     264    unlink(mailfilenamec);
     265    delete mailfilenamec;
     266
     267    define_webpage(disp, translationdir, "1", false, logout);
     268    return;
     269  }
     270
     271  // One of the translation pages
    239272  text_tarray splitarray;
    240   splitchar(arg_p.begin(), arg_p.end(), '_', splitarray);
    241 
    242   arg_p = splitarray[0];
    243   if (splitarray.size() > 1)
    244     page = splitarray[splitarray.size()-1];
    245 
    246   //if are entering page from macro search
    247   if (args["macroname"] != "") {
    248     text_t macroname = args["macroname"];
    249 
    250     // !! TO DO !!
    251     cmd = "perl " + filename_cat(gsdlhome, "bin", "script", "fromsearch.pl") + " " + sourcelang + " " + targetlang + " " + macroname;
    252     logout << "COMMAND: " << cmd <<endl;
    253 
    254     if ((gsdl_system(cmd, true, logout)) != 0) {
    255       logout << "Process " << cmd << " did not execute.../;-D\n";
    256       return;
    257     }
    258 
    259     define_webpage(disp, sourcelang, targetlang, trans_dir, page, -1, false, logout);
     273  splitchar(page.begin(), page.end(), '_', splitarray);
     274  text_t subpage = splitarray[0];
     275  if (splitarray.size() > 1) {
     276    subpage = splitarray[splitarray.size() - 1];
     277  }
     278  logout << "Subpage: " << subpage << endl;
     279
     280  // Check if the "NEXT" button has been pressed
     281  if (args[subpage] == "NEXT") {    // !! This is BAD !!
     282    // Write the submitted translations to a temporary file
     283    text_t submissionfilepath = filename_cat(translationdir, "submission.tmp");
     284    ofstream submissionfile(submissionfilepath.getcstr(), ios::out);
     285    if (!submissionfile) {
     286      logout << "Error: File " << submissionfilepath << " could not be created." << endl;
     287      return;
     288    }
     289
     290    // Write out the cgiargsclass variable to the submission file
     291    submissionfile << args;
     292    submissionfile.close();
     293
     294    // Make the file world writable
     295    gsdl_system("chmod a+w " + submissionfilepath, false, logout);
     296
     297    // Run the submittranslation.pl script to record the translations
     298    text_t cmd = "perl " + filename_cat(scriptdir, "submittranslations.pl") +
     299      " " + sourceabbr + " " + targetabbr + " " + transfile + " " + "html";
     300    if (gsdl_system(cmd, true, logout) != 0) {
     301      logout << "Error: Command " << cmd << " did not execute...\n";
     302      return;
     303    }
     304
     305    // Move onto the next page
     306    subpage = atoi(subpage.getcstr()) + 1;
     307  }
     308
     309  define_webpage(disp, translationdir, subpage, false, logout);
     310}
     311
     312
     313
     314void langaction::define_webpage (displayclass &disp, text_t dir, text_t page, bool error,
     315                 ostream &logout)
     316{
     317  // Open the file to read the webpage from
     318  text_t webpagefilename = filename_cat(dir, page + ".trans");
     319  ifstream webpagefile(webpagefilename.getcstr(), ios::in);
     320  if (!webpagefile) {
     321    logout << "Error: File " << webpagefilename << " could not be found.\n";
    260322    return;
    261323  }
    262324
    263   if (page == "fromsearch")
    264     page = "thankyou";
    265 
    266   if (page == "picklanguage") {
    267     page = "1";
    268 
    269     //calls the translator file to create HTML files
    270     cmd = "perl " + filename_cat(gsdlhome, "bin", "script", "translator.pl") + " " + sourcelang + " " + targetlang;
    271     int ret=gsdl_system(cmd, true, logout);
    272     if (ret != 0) {
    273       ret = ret >> 8;
    274       text_t errmsg="<H1>Error</H1>\nrunning translator.pl: ";
    275       if (ret==1) errmsg += "GSDLHOME unset";
    276       else if (ret==2) errmsg += "missing arguments";
    277       else if (ret==3) errmsg += "translation not yet initialised";
    278       else if (ret==4) errmsg += "no source macro found";
    279       errmsg+= " (error " + text_t(ret) + ")\n";
    280       disp.setmacro("content", "translang", errmsg);
    281       logout << "Process " << cmd << " did not execute.../;-D\n";
    282       return;
    283     }
    284 
    285     const recptconf &rcinfo = recpt->get_configinfo();
    286 
    287     // Use sendmail.pl perl script to send email events
    288     text_t tmpmailfile = filename_cat (gsdlhome, "tmp", "lang", "email.txt");
    289     char *tmpmailfilec = tmpmailfile.getcstr();
    290     ofstream tmpfile (tmpmailfilec);
    291     if (tmpfile) {
    292       tmpfile << "[Translator Event]\n"
    293           << "Date: " << get_date (true) << "\n"
    294           << "Greenstone Username: " << args["un"] << "\n";
    295       tmpfile << "The " << targetlang << " language translation was entered.\n";
    296       tmpfile.close();
    297 
    298       text_t sendmail_cmd = "perl -S sendmail.pl";
    299       sendmail_cmd += " -to \"" + rcinfo.maintainer + "\"";
    300       sendmail_cmd += " -from \"[email protected]\"";
    301       sendmail_cmd += " -smtp \"" + rcinfo.MailServer + "\"";
    302       sendmail_cmd += " -subject \"Greenstone Translator Event\"";
    303       sendmail_cmd += " -msgfile \"" + tmpmailfile + "\"";
    304       // logout << "Sendmail command: " << sendmail_cmd << endl;
    305       gsdl_system (sendmail_cmd, true, logout);
    306       unlink(tmpmailfilec);
    307     }
    308     else {
    309       logout << "translatoraction:: ERROR: Couldn't open "
    310          << "temporary event log file " << tmpmailfile << " for email event.\n";
    311     }
    312     delete tmpmailfilec;
    313   }
    314 
    315   //generates the picklanguage page
    316   if (page.empty()) {
    317     page = "picklanguage";
    318     cmd = "perl " + filename_cat(gsdlhome, "bin", "script", "picklanguage.pl");
    319     logout << cmd << endl;
    320     int ret=gsdl_system(cmd,true,logout);
    321     if (ret != 0) { // determine error
    322       /*
    323     // ret=-1 => we couldn't run the script.
    324     // ret=1 => GSDLHOME not set.
    325     // >=2 implies some file IO error:
    326     // ret=2 => can't read etc/main.cfg
    327     // ret=3 => "can't create directory $GSDLHOME/tmp/lang/
    328     // ret=4 => can't write $dir/picklanguage.lang
    329       **/
    330       // maybe this should be a "real" macro for translation?!
    331       text_t errmsg="<H1>Error</H1>\nPermissions problem: picklanguage.pl failed ";
    332 
    333       ret = ret >> 8; // man perlfunc for the reason for this
    334       if (ret==255) errmsg+="running script";
    335       else if (ret==1) errmsg+="GSDLHOME not set";
    336       else if (ret==2) errmsg+="reading etc/main.cfg";
    337       else if (ret==3) errmsg+="creating tmp/lang/";
    338       else if (ret==4) errmsg+="writing picklanguage.lang";
    339       errmsg += " (error="; errmsg += text_t(ret); errmsg += ")";
    340       disp.setmacro("content", "translang", errmsg);
    341 
    342       return;
    343     }
    344     define_webpage(disp, sourcelang, targetlang, dir, page, -1, false, logout);
    345     return;
    346   }
    347 
    348   // Read the number of translation pages from numpages.log
    349   text_t numpagesfilepath = filename_cat(trans_dir, "numpages.log");
    350   ifstream numpagesfile(numpagesfilepath.getcstr(), ios::in);
    351 
    352   // Check file opened OK
    353   if (!numpagesfile) {
    354     text_t errmsg="Error: File " + numpagesfilepath + " could not be opened.";
    355     logout << errmsg << endl;
    356     disp.setmacro("content", "translang", errmsg);
    357     return;
    358   }
    359 
    360   // Read the number of pages from the file
    361   int numpages;
    362   char line[LINESIZE];
    363   if (numpagesfile.getline(line, LINESIZE-1)) {
    364     numpages = atoi(line);
    365   }
    366   numpagesfile.close();
    367 
    368   // if (pagenos.empty() && pageno == "")
    369   if (numpages == 0)
    370     page = "thankyou";
    371 
    372   text_t &submitargs = args[page];
    373   text_t submit = "SUBMIT TRANSLATION";
    374 
    375   //if the SUBMIT TRANSLATION >> button has been pushed
    376   if (text_t_substring(submitargs,submit)) {
    377     //creates output stream to argsfile
    378     text_t argsfile = filename_cat(trans_dir, "arguments.arg");
    379     logout << "Argsfile: " << argsfile << endl;
    380     write_args_file(args, argsfile, logout);
    381 
    382     //create the command line for submitting the translation
    383     cmd = "perl " + filename_cat(gsdlhome, "bin", "script", "submit_translation.pl") + " ";
    384     cmd += (sourcelang + " " + targetlang + " " + "arguments.arg");
    385 
    386     //give command to system to execute submission of translation
    387     if((gsdl_system(cmd, true, logout)) != 0)
    388       logout << "Process " << cmd << " did not execute\n";
    389 
    390     //increments which page is displayed when translation is submitted
    391     int pageno = atoi(page.getcstr());
    392     if (pageno >= numpages)
    393       page = "thankyou";
    394     else
    395       page = (pageno + 1);
    396   }
    397 
    398   define_webpage(disp, sourcelang, targetlang, trans_dir, page, numpages, false, logout);
    399 }
    400 
    401 
    402 void langaction::write_args_file (cgiargsclass &args, text_t argsfile, ostream &logout)
    403 {
    404   ofstream argsout(argsfile.getcstr(), ios::out);
    405 
    406   // Informs user and returns if output stream not opened
    407   if (!argsout) {
    408     logout << "Error: File " << argsfile << " could not be opened." << endl;
    409     return;
    410   }
    411 
    412   // Make the file world writable
    413   text_t changemode = "chmod a+w " + argsfile;
    414   gsdl_system(changemode, false, logout);
    415 
    416   // Write out the cgiargsclass variable to the argsout file
    417   argsout << args;
    418   argsout.close();
    419 }
    420 
    421 
    422 void langaction::define_webpage (displayclass &disp, text_t sourcelang, text_t targetlang,
    423                  text_t dir, text_t page, int numpages, bool error,
    424                  ostream &logout)
    425 {
    426   // produces HTML page with the current pages contents
    427   // formfile is the file that contains web-form for the
    428   // current package whose name is stored in 'package'
    429 
    430   text_t formfile = filename_cat(dir, page + ".lang");
    431   ifstream readfile(formfile.getcstr(), ios::in);
    432   if (!readfile) {
    433     logout << "File " << formfile << " could not be found.\n";
    434     return;
    435   }
    436 
    437 
    438   /* ============================ START WEB-FORM HTML CODE ============================ */
    439   text_t text = "";
    440   text += "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"></head><body>\n";
     325  // ============================ START HTML CODE ============================
     326  text_t text = "<html>\n";
     327  text += "<head>\n";
     328  text += "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\n";
     329  text += "</head>\n";
     330
     331  text += "<body>\n";
     332  text += "_texttranshead_\n";
    441333
    442334  text += "<input type=hidden name=\"a\" value=\"lang\">\n";
     
    444336  text += "<input type=hidden name=\"e\" value=\"_decodedcompressedoptions_\">\n";
    445337
    446   text += "_texttranshead_";
    447 
    448   // If not a special case page display "Page x of y" and Next button
    449   if ((page != "thankyou") && (page != "picklanguage") && (page != "fromsearch")) {
    450     text += "<center><strong>";
    451     text += " _textcurrpage_ " + page + " _textof_ " + numpages;
    452     text += "</strong>";
    453 
    454     int pageno = atoi(page.getcstr());
    455     text += " <a href=\"_gwcgi_?e=_compressedoptions_&a=lang&p=translang_";
    456     if (pageno >= numpages)
    457       text += "thankyou";
    458     else
    459       text += (pageno + 1);
    460     text += "&baselanguage=" + sourcelang;
    461     text += "&language=" + targetlang;
    462     text += "\">";
    463     text += "<strong>NEXT>></strong></a></center>\n";
    464   }
    465 
    466338  // Read in HTML from file
    467339  char line[LINESIZE];
    468   while (readfile.getline(line, LINESIZE-1)) {
     340  while (webpagefile.getline(line, LINESIZE-1)) {
    469341    text_t temp = line;
    470342    text += temp + "\n";
    471343  }
    472   readfile.close();
    473 
     344  webpagefile.close();
     345
     346  // Display an error if the source and target languages are the same
    474347  if (page == "picklanguage" && error) {
    475348    text += "<center><strong>_textchoosedifferent_</strong></center>";
    476349  }
    477350
    478   // If not a special case page display "Page x of y" and Next button
    479   if ((page != "thankyou") && (page != "picklanguage") && (page != "fromsearch")) {
    480     text += "<center><strong>";
    481     text += " _textcurrpage_ " + page + " _textof_ " + numpages;
    482     text += "</strong>";
    483 
    484     int pageno = atoi(page.getcstr());
    485     text += " <a href=\"_gwcgi_?e=_compressedoptions_&a=lang&p=translang_";
    486     if (pageno >= numpages)
    487       text += "thankyou";
    488     else
    489       text += (pageno + 1);
    490     text += "&baselanguage=" + sourcelang;
    491     text += "&language=" + targetlang;
    492     text += "\">";
    493     text += "<strong>NEXT>></strong></a></center>\n";
    494   }
    495 
    496   if (page == "fromsearch")
    497     text += "<center><strong> _textcurrpage_ 1 _textof_ 1</strong><br>\n";
    498 
    499   text += "</body></html>\n";
    500  
    501  
    502   /* ============================ END WEB-FORM HTML CODE ============================ */
    503  
    504  
    505   // THE FOLLOWING MACROS ARE DEFINED IN translang.dm MACRO FILE
    506   // set the formcontent  as the content of the form-page
    507 
     351  text += "</body>\n";
     352  text += "</html>\n";
     353
     354  // if (page == "fromsearch") {
     355  //   text += "<center><strong> _textcurrpage_ 1 _textof_ 1</strong><br>\n";
     356  // }
     357
     358  // ============================ END HTML CODE ============================
     359
     360  // The following macros are defined in translang.dm
    508361  disp.setmacro("formcontent", "translang", text);
    509  
    510   // set the action for the form
    511362  disp.setmacro("formaction", "translang", "_gwcgi_");
    512363
     364  // Change the navigation bar to include a search button
    513365  text_t navigationbar = "<!-- Navigation Bar -->\n";
    514366  navigationbar += "<table width=\"100%\" cellspacing=0 cellpadding=0>";
    515   navigationbar += "<tr valign=top><td rowspan=2 align=left>_imagecollection_</td>";
    516   navigationbar += "<td align=right>_javalinks_</td><td>";
     367  navigationbar += "<tr valign=top>\n";
     368  navigationbar += "<td rowspan=2 align=left>_imagecollection_</td>";
     369  navigationbar += "<td align=right>_javalinks_</td>";
    517370
    518371  // if (page != "picklanguage") {
    519     // navigationbar += "_imageserch_";
     372  // navigationbar += "<td>_imageserch_</td>";
    520373  // }
    521   navigationbar += "</td></tr></table>\n</nobr>\n";
     374
     375  navigationbar += "</tr></table>\n</nobr>\n";
    522376  navigationbar += "<!-- End of Navigation Bar -->\n";
    523377  disp.setmacro ("navigationbar", "translang", navigationbar);
    524378}
     379
    525380
    526381
     
    530385                ostream &logout) {
    531386  text_t &arg_p = args["p"];
    532   // logout << "Arg P: " << arg_p << endl;
    533387
    534388  outconvertclass text2utf8;
    535 
    536389  textout << text2utf8 << disp << ("_" + arg_p + ":header_\n")
    537390      << ("_" + arg_p + ":content_\n")
     
    540393  return true;
    541394}
     395
     396
     397
     398
     399  //if are entering page from macro search
     400  /* if (args["macroname"] != "") {
     401    text_t macroname = args["macroname"];
     402
     403    // !! TO DO !!
     404    cmd = "perl " + filename_cat(gsdlhome, "bin", "script", "fromsearch.pl") + " " + sourceabbr + " " + targetabbr + " " + macroname;
     405    logout << "COMMAND: " << cmd <<endl;
     406
     407    if ((gsdl_system(cmd, true, logout)) != 0) {
     408      logout << "Process " << cmd << " did not execute.../;-D\n";
     409      return;
     410    }
     411
     412    define_webpage(disp, sourcelang, targetlang, trans_dir, page, -1, false, logout);
     413    return;
     414  }
     415
     416  if (page == "fromsearch")
     417  page = "thankyou"; */
  • trunk/gsdl/src/recpt/langaction.h

    r4123 r8102  
    5858                   recptprotolistclass *protos, ostream &logout);
    5959
    60   void write_args_file (cgiargsclass &args, text_t argsfile, ostream &logout);
     60  void write_submission_file (cgiargsclass &args, text_t filename, ostream &logout);
    6161
    6262  void get_cgihead_info (cgiargsclass &args, recptprotolistclass *protos,
     
    7171  bool init (ostream & /*logout*/);
    7272
    73   void define_webpage (displayclass &disp, text_t sourcelang, text_t targetlang,
    74                text_t dir, text_t pageno, int numpages, bool error,
     73  void define_webpage (displayclass &disp, text_t dir, text_t page, bool error,
    7574               ostream &logout);
    7675};
Note: See TracChangeset for help on using the changeset viewer.