1 | /**********************************************************************
|
---|
2 | *
|
---|
3 | * gtiaction.cpp --
|
---|
4 | * Copyright (C) 2005 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 |
|
---|
27 | #include "gsdl_modules_cfg.h"
|
---|
28 | #ifdef GSDL_USE_GTI_ACTION
|
---|
29 |
|
---|
30 |
|
---|
31 | #include <expat.h>
|
---|
32 | #include <sys/utsname.h>
|
---|
33 | #include "gtiaction.h"
|
---|
34 | #include "fileutil.h"
|
---|
35 | #include "gsdlunicode.h"
|
---|
36 |
|
---|
37 |
|
---|
38 |
|
---|
39 | gtiaction::gtiaction()
|
---|
40 | {
|
---|
41 | cgiarginfo arg_ainfo;
|
---|
42 |
|
---|
43 | arg_ainfo.shortname = "tlc";
|
---|
44 | arg_ainfo.longname = "translation target language code";
|
---|
45 | arg_ainfo.multiplechar = true;
|
---|
46 | arg_ainfo.defaultstatus = cgiarginfo::weak;
|
---|
47 | arg_ainfo.savedarginfo = cgiarginfo::must;
|
---|
48 | argsinfo.addarginfo (&cerr, arg_ainfo);
|
---|
49 |
|
---|
50 | arg_ainfo.shortname = "tfk";
|
---|
51 | arg_ainfo.longname = "translation file key";
|
---|
52 | arg_ainfo.multiplechar = true;
|
---|
53 | arg_ainfo.defaultstatus = cgiarginfo::weak;
|
---|
54 | arg_ainfo.savedarginfo = cgiarginfo::must;
|
---|
55 | argsinfo.addarginfo (&cerr, arg_ainfo);
|
---|
56 |
|
---|
57 | arg_ainfo.shortname = "ncpp";
|
---|
58 | arg_ainfo.longname = "number of chunks per page";
|
---|
59 | arg_ainfo.multiplechar = true;
|
---|
60 | arg_ainfo.defaultstatus = cgiarginfo::weak;
|
---|
61 | arg_ainfo.argdefault = "1";
|
---|
62 | arg_ainfo.savedarginfo = cgiarginfo::must;
|
---|
63 | argsinfo.addarginfo (&cerr, arg_ainfo);
|
---|
64 | }
|
---|
65 |
|
---|
66 |
|
---|
67 |
|
---|
68 | gtiaction::~gtiaction()
|
---|
69 | {
|
---|
70 | delete[] set_gsdlhome_cstr;
|
---|
71 | delete[] set_gsdlos_cstr;
|
---|
72 | }
|
---|
73 |
|
---|
74 |
|
---|
75 |
|
---|
76 | bool gtiaction::init (ostream& /*logout*/)
|
---|
77 | {
|
---|
78 | // Set GSDLHOME and GSDLOS environment variables
|
---|
79 | text_t set_gsdlhome = "GSDLHOME=" + gsdlhome;
|
---|
80 | text_t set_gsdlos = "GSDLOS=";
|
---|
81 |
|
---|
82 | #if defined (__WIN32__)
|
---|
83 | set_gsdlos += "windows";
|
---|
84 | #else
|
---|
85 | struct utsname *buf = new struct utsname();
|
---|
86 | if (uname(buf) == -1) {
|
---|
87 | // uname failed, so this must be linux
|
---|
88 | set_gsdlos += "linux";
|
---|
89 | }
|
---|
90 | else {
|
---|
91 | text_t gsdlos = buf->sysname;
|
---|
92 | lc(gsdlos);
|
---|
93 | set_gsdlos += gsdlos;
|
---|
94 | }
|
---|
95 | delete buf;
|
---|
96 | #endif
|
---|
97 |
|
---|
98 | // These will be cleaned up in the destructor
|
---|
99 | set_gsdlhome_cstr = set_gsdlhome.getcstr();
|
---|
100 | set_gsdlos_cstr = set_gsdlos.getcstr();
|
---|
101 | putenv(set_gsdlhome_cstr);
|
---|
102 | putenv(set_gsdlos_cstr);
|
---|
103 |
|
---|
104 | return true;
|
---|
105 | }
|
---|
106 |
|
---|
107 |
|
---|
108 |
|
---|
109 | bool gtiaction::check_cgiargs(cgiargsinfoclass& /*argsinfo*/, cgiargsclass& args,
|
---|
110 | recptprotolistclass* /*protos*/, ostream& logout)
|
---|
111 | {
|
---|
112 | // Authenticate the user, except for the "home" and "lang" pages
|
---|
113 | if (args["p"] != "home" && args["p"] != "lang") {
|
---|
114 | args["uan"] = 1;
|
---|
115 | args["ug"] = "langadmin_" + args["tlc"];
|
---|
116 | }
|
---|
117 |
|
---|
118 | return true;
|
---|
119 | }
|
---|
120 |
|
---|
121 |
|
---|
122 |
|
---|
123 | bool gtiaction::do_action(cgiargsclass& args, recptprotolistclass* /*protos*/,
|
---|
124 | browsermapclass* /*browsers*/, displayclass& disp,
|
---|
125 | outconvertclass& outconvert, ostream& textout,
|
---|
126 | ostream& logout)
|
---|
127 | {
|
---|
128 | textout << outconvert << disp << ("_header_\n") << ("_content_\n") << ("_footer_\n");
|
---|
129 | return true;
|
---|
130 | }
|
---|
131 |
|
---|
132 |
|
---|
133 |
|
---|
134 | void gtiaction::get_cgihead_info(cgiargsclass& /*args*/, recptprotolistclass* /*protos*/,
|
---|
135 | response_t& response, text_t& response_data,
|
---|
136 | ostream& logout)
|
---|
137 | {
|
---|
138 | response = content;
|
---|
139 | response_data = "text/html";
|
---|
140 | }
|
---|
141 |
|
---|
142 |
|
---|
143 |
|
---|
144 | void gtiaction::define_internal_macros(displayclass& disp, cgiargsclass& args,
|
---|
145 | recptprotolistclass* protos, ostream& logout)
|
---|
146 | {
|
---|
147 | // logout << endl << "Arguments: " << args << endl;
|
---|
148 | logout << endl << "CGI arg p: " << args["p"] << endl;
|
---|
149 |
|
---|
150 | // Set the page content to be the content macro for the gti package
|
---|
151 | disp.setmacro("content", displayclass::defaultpackage, "_gti:content_");
|
---|
152 | disp.setmacro("pagetitle", displayclass::defaultpackage, "_gti:textgti_");
|
---|
153 |
|
---|
154 | // Define the page content for the GTI home page
|
---|
155 | if (args["p"] == "home") {
|
---|
156 | define_gti_home_page(disp, args, logout);
|
---|
157 | return;
|
---|
158 | }
|
---|
159 |
|
---|
160 | // Define the page content for the GTI language page
|
---|
161 | if (args["p"] == "lang") {
|
---|
162 | define_gti_lang_page(disp, args, logout);
|
---|
163 | return;
|
---|
164 | }
|
---|
165 |
|
---|
166 | // Define the page content for the GTI search page
|
---|
167 | if (args["p"] == "find") {
|
---|
168 | define_gti_find_page(disp, args, logout);
|
---|
169 | return;
|
---|
170 | }
|
---|
171 |
|
---|
172 | // Process user translations
|
---|
173 | if (args["p"] == "submit") {
|
---|
174 | process_gti_submissions(disp, args, logout, true);
|
---|
175 | }
|
---|
176 |
|
---|
177 | // Define the page content for the GTI core pages (containing the translation input forms)
|
---|
178 | define_gti_core_page(disp, args, logout);
|
---|
179 | }
|
---|
180 |
|
---|
181 |
|
---|
182 |
|
---|
183 | void gtiaction::define_gti_home_page(displayclass& disp, cgiargsclass& args, ostream& logout)
|
---|
184 | {
|
---|
185 | disp.setmacro("gtiformcontent", "gti", "_gti:gtihome_");
|
---|
186 |
|
---|
187 | languageinfo_tmap loaded_languages = recpt->get_configinfo().languages;
|
---|
188 |
|
---|
189 | // Get the languages specified in the main.cfg file, and put them into a map to sort by name
|
---|
190 | text_tmap gti_languages_name_code_mapping;
|
---|
191 | languageinfo_tmap::const_iterator loaded_language = loaded_languages.begin();
|
---|
192 | while (loaded_language != loaded_languages.end()) {
|
---|
193 | // English is not a valid GTI target language, since it is the source language
|
---|
194 | if (loaded_language->first != "en") {
|
---|
195 | gti_languages_name_code_mapping[loaded_language->second.longname] = loaded_language->first;
|
---|
196 | }
|
---|
197 | ++loaded_language;
|
---|
198 | }
|
---|
199 |
|
---|
200 | // Set the gtitlcselection macro
|
---|
201 | text_t gti_tlc_selection = "<select name=\"tlc\">\n";
|
---|
202 | text_tmap::iterator gti_language = gti_languages_name_code_mapping.begin();
|
---|
203 | while (gti_language != gti_languages_name_code_mapping.end()) {
|
---|
204 | gti_tlc_selection += "<option value=\"" + gti_language->second + "\">" + gti_language->first + "</option>\n";
|
---|
205 | ++gti_language;
|
---|
206 | }
|
---|
207 | gti_tlc_selection += "</select>";
|
---|
208 | disp.setmacro("gtitlcselection", "gti", gti_tlc_selection);
|
---|
209 | }
|
---|
210 |
|
---|
211 |
|
---|
212 |
|
---|
213 | void gtiaction::define_gti_lang_page(displayclass& disp, cgiargsclass& args, ostream& logout)
|
---|
214 | {
|
---|
215 | // Get the target language code from the CGI arguments
|
---|
216 | text_t target_language_code = args["tlc"];
|
---|
217 |
|
---|
218 | disp.setmacro("gtiformcontent", "gti", "_gti:gtilang_");
|
---|
219 |
|
---|
220 | // Send a request to gti.pl to get the valid translation files
|
---|
221 | text_t gti_arguments = "get-language-status " + target_language_code;
|
---|
222 | GTI_Response gti_response = do_gti_request(gti_arguments, logout);
|
---|
223 | if (gti_response.error_message != "") {
|
---|
224 | // An error has occurred
|
---|
225 | disp.setmacro("gtiformcontent", "gti", "_gti:gtierror_");
|
---|
226 | disp.setmacro("gtierrormessage", "gti", gti_response.error_message);
|
---|
227 | return;
|
---|
228 | }
|
---|
229 |
|
---|
230 | languageinfo_tmap loaded_languages = recpt->get_configinfo().languages;
|
---|
231 | disp.setmacro("gtitargetlanguagename", "gti", loaded_languages[target_language_code].longname);
|
---|
232 |
|
---|
233 | // Set the gtitfkselection macro
|
---|
234 | text_t gti_tfk_selection = "<table>";
|
---|
235 | text_tmap::iterator translation_file = gti_response.translation_files_key_to_null_mapping.begin();
|
---|
236 | while (translation_file != gti_response.translation_files_key_to_null_mapping.end()) {
|
---|
237 | text_t translation_file_key = translation_file->first;
|
---|
238 |
|
---|
239 | gti_tfk_selection += "<tr><td>";
|
---|
240 | gti_tfk_selection += "<input type=\"radio\" name=\"tfk\" value=\"" + translation_file_key + "\"></td>\n";
|
---|
241 | gti_tfk_selection += "<td>_textgti" + translation_file_key + "_</td></tr>\n";
|
---|
242 |
|
---|
243 | text_t num_chunks_translated = gti_response.translation_files_key_to_num_chunks_translated_mapping[translation_file_key];
|
---|
244 | text_t num_chunks_requiring_translation = gti_response.translation_files_key_to_num_chunks_requiring_translation_mapping[translation_file_key];
|
---|
245 | text_t num_chunks_requiring_updating = gti_response.translation_files_key_to_num_chunks_requiring_updating_mapping[translation_file_key];
|
---|
246 | gti_tfk_selection += "<tr><td></td><td>_gtitranslationfilestatus_(" + num_chunks_translated + "," + num_chunks_requiring_translation + "," + num_chunks_requiring_updating + ")</td></tr>\n";
|
---|
247 | ++translation_file;
|
---|
248 | }
|
---|
249 | gti_tfk_selection += "</table>";
|
---|
250 | disp.setmacro("gtitfkselection", "gti", gti_tfk_selection);
|
---|
251 | }
|
---|
252 |
|
---|
253 |
|
---|
254 |
|
---|
255 | void gtiaction::define_gti_find_page(displayclass& disp, cgiargsclass& args, ostream& logout)
|
---|
256 | {
|
---|
257 | // Get the target language code and file to translate from the CGI arguments
|
---|
258 | text_t target_language_code = args["tlc"];
|
---|
259 | text_t translation_file_key = args["tfk"];
|
---|
260 | text_t query_string = to_utf8(args["q"]);
|
---|
261 |
|
---|
262 | // Process user corrections
|
---|
263 | if (args["sp"] != "") {
|
---|
264 | process_gti_submissions(disp, args, logout, false);
|
---|
265 | }
|
---|
266 |
|
---|
267 | disp.setmacro("gtiformcontent", "gti", "_gti:gtifind_");
|
---|
268 |
|
---|
269 | languageinfo_tmap loaded_languages = recpt->get_configinfo().languages;
|
---|
270 | disp.setmacro("gtitargetlanguagename", "gti", loaded_languages[target_language_code].longname);
|
---|
271 | disp.setmacro("gtitranslationfiledesc", "gti", "_gti:textgti" + translation_file_key + "_");
|
---|
272 |
|
---|
273 | if (query_string == "") {
|
---|
274 | // No query, so no search results
|
---|
275 | disp.setmacro("gtifindformcontent", "gti", "");
|
---|
276 | return;
|
---|
277 | }
|
---|
278 |
|
---|
279 | // Send a request to gti.pl to get the valid translation files
|
---|
280 | logout << "Query argument: " << query_string << endl;
|
---|
281 | text_t gti_arguments = "search-chunks " + target_language_code + " " + translation_file_key + " " + query_string;
|
---|
282 | GTI_Response gti_response = do_gti_request(gti_arguments, logout);
|
---|
283 | if (gti_response.error_message != "") {
|
---|
284 | // An error has occurred
|
---|
285 | disp.setmacro("gtiformcontent", "gti", "_gti:gtierror_");
|
---|
286 | disp.setmacro("gtierrormessage", "gti", gti_response.error_message);
|
---|
287 | return;
|
---|
288 | }
|
---|
289 |
|
---|
290 | disp.setmacro("gtinumchunksmatchingquery", "gti", gti_response.num_chunks_matching_query);
|
---|
291 |
|
---|
292 | // Loop through the chunks returned, displaying them on the page
|
---|
293 | text_t gti_find_form_content = "_gtifindformheader_\n";
|
---|
294 | text_tmap::iterator chunk_key_iterator = gti_response.target_file_chunks_key_to_text_mapping.begin();
|
---|
295 | while (chunk_key_iterator != gti_response.target_file_chunks_key_to_text_mapping.end()) {
|
---|
296 | text_t chunk_key = chunk_key_iterator->first;
|
---|
297 |
|
---|
298 | // Need to escape any underscores in the chunk key to show it correctly on the page
|
---|
299 | text_t chunk_key_escaped = escape_all(chunk_key, '_');
|
---|
300 |
|
---|
301 | // Need to escape any underscores, commas, parentheses and single quotes in the chunk text
|
---|
302 | text_t target_file_chunk_text = gti_response.target_file_chunks_key_to_text_mapping[chunk_key];
|
---|
303 | text_t target_file_chunk_text_escaped = escape_all(target_file_chunk_text, '_');
|
---|
304 | target_file_chunk_text_escaped = escape_all(target_file_chunk_text_escaped, ',');
|
---|
305 | target_file_chunk_text_escaped = escape_all(target_file_chunk_text_escaped, '(');
|
---|
306 | target_file_chunk_text_escaped = escape_all(target_file_chunk_text_escaped, ')');
|
---|
307 | target_file_chunk_text_escaped = escape_all(target_file_chunk_text_escaped, '\'');
|
---|
308 |
|
---|
309 | // This chunk matches the query
|
---|
310 | gti_find_form_content += "_gtichunkmatchingquery_(" + chunk_key_escaped + "," + target_file_chunk_text_escaped + ")\n";
|
---|
311 |
|
---|
312 | chunk_key_iterator++;
|
---|
313 | }
|
---|
314 | gti_find_form_content += "_gtifindformfooter_\n";
|
---|
315 |
|
---|
316 | disp.setmacro("gtifindformcontent", "gti", gti_find_form_content);
|
---|
317 | }
|
---|
318 |
|
---|
319 |
|
---|
320 |
|
---|
321 | void gtiaction::define_gti_core_page(displayclass& disp, cgiargsclass& args, ostream& logout)
|
---|
322 | {
|
---|
323 | // Get the target language code and file to translate from the CGI arguments
|
---|
324 | text_t target_language_code = args["tlc"];
|
---|
325 | text_t translation_file_key = args["tfk"];
|
---|
326 | text_t num_chunks_per_page = args["ncpp"];
|
---|
327 | logout << "Num chunks per page: " << num_chunks_per_page << endl;
|
---|
328 |
|
---|
329 | disp.setmacro("gtiformcontent", "gti", "_gti:gticore_");
|
---|
330 |
|
---|
331 | // Send a request to gti.pl to get the first string to translate
|
---|
332 | text_t gti_arguments = "get-first-n-chunks-requiring-work " + target_language_code + " " + translation_file_key + " " + num_chunks_per_page;
|
---|
333 | GTI_Response gti_response = do_gti_request(gti_arguments, logout);
|
---|
334 | if (gti_response.error_message != "") {
|
---|
335 | // An error has occurred
|
---|
336 | disp.setmacro("gtiformcontent", "gti", "_gti:gtierror_");
|
---|
337 | disp.setmacro("gtierrormessage", "gti", gti_response.error_message);
|
---|
338 | return;
|
---|
339 | }
|
---|
340 |
|
---|
341 | languageinfo_tmap loaded_languages = recpt->get_configinfo().languages;
|
---|
342 | disp.setmacro("gtitargetlanguagename", "gti", loaded_languages[target_language_code].longname);
|
---|
343 | disp.setmacro("gtitargetfilepath", "gti", gti_response.translation_files_key_to_target_file_path_mapping[translation_file_key]);
|
---|
344 | disp.setmacro("gtitranslationfiledesc", "gti", "_gti:textgti" + translation_file_key + "_");
|
---|
345 |
|
---|
346 | disp.setmacro("gtinumchunkstranslated", "gti", gti_response.translation_files_key_to_num_chunks_translated_mapping[translation_file_key]);
|
---|
347 | disp.setmacro("gtinumchunksrequiringtranslation", "gti", gti_response.translation_files_key_to_num_chunks_requiring_translation_mapping[translation_file_key]);
|
---|
348 | disp.setmacro("gtinumchunksrequiringupdating", "gti", gti_response.translation_files_key_to_num_chunks_requiring_updating_mapping[translation_file_key]);
|
---|
349 |
|
---|
350 | // Check if the translation file is finished
|
---|
351 | if (gti_response.translation_files_key_to_num_chunks_requiring_translation_mapping[translation_file_key] == "0" && gti_response.translation_files_key_to_num_chunks_requiring_updating_mapping[translation_file_key] == "0") {
|
---|
352 | disp.setmacro("gtiformcontent", "gti", "_gti:gtidone_");
|
---|
353 | return;
|
---|
354 | }
|
---|
355 |
|
---|
356 | // Loop through the chunks returned, displaying them on the page
|
---|
357 | text_t gti_core_form_content = "";
|
---|
358 | text_tmap::iterator chunk_key_iterator = gti_response.source_file_chunks_key_to_text_mapping.begin();
|
---|
359 | while (chunk_key_iterator != gti_response.source_file_chunks_key_to_text_mapping.end()) {
|
---|
360 | text_t chunk_key = chunk_key_iterator->first;
|
---|
361 |
|
---|
362 | // Need to escape any underscores in the chunk key to show it correctly on the page
|
---|
363 | text_t chunk_key_escaped = escape_all(chunk_key, '_');
|
---|
364 |
|
---|
365 | // Need to escape any underscores, commas, parentheses and single quotes in the chunk text
|
---|
366 | text_t source_file_chunk_text = gti_response.source_file_chunks_key_to_text_mapping[chunk_key];
|
---|
367 | text_t source_file_chunk_text_escaped = escape_all(source_file_chunk_text, '_');
|
---|
368 | source_file_chunk_text_escaped = escape_all(source_file_chunk_text_escaped, ',');
|
---|
369 | source_file_chunk_text_escaped = escape_all(source_file_chunk_text_escaped, '(');
|
---|
370 | source_file_chunk_text_escaped = escape_all(source_file_chunk_text_escaped, ')');
|
---|
371 | source_file_chunk_text_escaped = escape_all(source_file_chunk_text_escaped, '\'');
|
---|
372 | text_t target_file_chunk_text = gti_response.target_file_chunks_key_to_text_mapping[chunk_key];
|
---|
373 | text_t target_file_chunk_text_escaped = escape_all(target_file_chunk_text, '_');
|
---|
374 | target_file_chunk_text_escaped = escape_all(target_file_chunk_text_escaped, ',');
|
---|
375 | target_file_chunk_text_escaped = escape_all(target_file_chunk_text_escaped, '(');
|
---|
376 | target_file_chunk_text_escaped = escape_all(target_file_chunk_text_escaped, ')');
|
---|
377 | target_file_chunk_text_escaped = escape_all(target_file_chunk_text_escaped, '\'');
|
---|
378 |
|
---|
379 | text_t source_file_chunk_date = gti_response.source_file_chunks_key_to_date_mapping[chunk_key];
|
---|
380 | text_t target_file_chunk_date = gti_response.target_file_chunks_key_to_date_mapping[chunk_key];
|
---|
381 |
|
---|
382 | // This chunk requires translation
|
---|
383 | if (target_file_chunk_text == "") {
|
---|
384 | gti_core_form_content += "_gtichunkrequiringtranslation_(" + chunk_key_escaped + "," + source_file_chunk_text_escaped + "," + source_file_chunk_date + ")\n";
|
---|
385 | }
|
---|
386 | // This chunk requires updating
|
---|
387 | else {
|
---|
388 | gti_core_form_content += "_gtichunkrequiringupdating_(" + chunk_key_escaped + "," + source_file_chunk_text_escaped + "," + source_file_chunk_date + "," + target_file_chunk_text_escaped + "," + target_file_chunk_date + ")\n";
|
---|
389 | }
|
---|
390 |
|
---|
391 | chunk_key_iterator++;
|
---|
392 | }
|
---|
393 |
|
---|
394 | disp.setmacro("gticoreformcontent", "gti", gti_core_form_content);
|
---|
395 | }
|
---|
396 |
|
---|
397 |
|
---|
398 |
|
---|
399 | void gtiaction::process_gti_submissions(displayclass& disp, cgiargsclass& args, ostream& logout, bool force_submission)
|
---|
400 | {
|
---|
401 | // Get the target language code and file to translate from the CGI arguments
|
---|
402 | text_t target_language_code = args["tlc"];
|
---|
403 | text_t translation_file_key = args["tfk"];
|
---|
404 |
|
---|
405 | // Submitted chunk arguments contain the language code followed by "::"
|
---|
406 | char* source_chunk_key_start_cstr = ((text_t) "en" + "%3A%3A").getcstr();
|
---|
407 | char* target_chunk_key_start_cstr = (target_language_code + "%3A%3A").getcstr();
|
---|
408 |
|
---|
409 | // Find the cgi arguments with submitted chunk information
|
---|
410 | text_t submission_text;
|
---|
411 | cgiargsclass::const_iterator cgi_argument = args.begin();
|
---|
412 | while (cgi_argument != args.end()) {
|
---|
413 | char* cgi_argument_name_cstr = cgi_argument->first.getcstr();
|
---|
414 |
|
---|
415 | // Source file text
|
---|
416 | if (strncmp(cgi_argument_name_cstr, source_chunk_key_start_cstr, strlen(source_chunk_key_start_cstr)) == 0) {
|
---|
417 | submission_text += "<SourceFileText key=\"";
|
---|
418 | submission_text += &cgi_argument_name_cstr[strlen(source_chunk_key_start_cstr)];
|
---|
419 | submission_text += "\">\n";
|
---|
420 | submission_text += to_utf8(args[cgi_argument->first]) + "\n";
|
---|
421 | submission_text += "</SourceFileText>\n";
|
---|
422 | }
|
---|
423 | // Target file text
|
---|
424 | if (strncmp(cgi_argument_name_cstr, target_chunk_key_start_cstr, strlen(target_chunk_key_start_cstr)) == 0) {
|
---|
425 | submission_text += "<TargetFileText key=\"";
|
---|
426 | submission_text += &cgi_argument_name_cstr[strlen(target_chunk_key_start_cstr)];
|
---|
427 | submission_text += "\">\n";
|
---|
428 | submission_text += to_utf8(args[cgi_argument->first]) + "\n";
|
---|
429 | submission_text += "</TargetFileText>\n";
|
---|
430 | }
|
---|
431 |
|
---|
432 | ++cgi_argument;
|
---|
433 | }
|
---|
434 |
|
---|
435 | logout << "Submission text: " << submission_text << endl;
|
---|
436 |
|
---|
437 | // Send the submission to gti.pl
|
---|
438 | text_t gti_arguments = "submit-translation " + target_language_code + " " + translation_file_key;
|
---|
439 | if (force_submission) {
|
---|
440 | gti_arguments += " -force_submission";
|
---|
441 | }
|
---|
442 | do_gti_submission(gti_arguments, submission_text, logout);
|
---|
443 | logout << "Done." << endl;
|
---|
444 | }
|
---|
445 |
|
---|
446 |
|
---|
447 |
|
---|
448 | text_t gtiaction::escape_all(text_t text_string, char character_to_escape)
|
---|
449 | {
|
---|
450 | text_t text_string_escaped = "";
|
---|
451 |
|
---|
452 | text_t::iterator text_string_character = text_string.begin();
|
---|
453 | while (text_string_character != text_string.end()) {
|
---|
454 | if (*text_string_character == character_to_escape) {
|
---|
455 | text_string_escaped += "\\";
|
---|
456 | }
|
---|
457 | text_string_escaped.push_back(*text_string_character);
|
---|
458 | text_string_character++;
|
---|
459 | }
|
---|
460 |
|
---|
461 | return text_string_escaped;
|
---|
462 | }
|
---|
463 |
|
---|
464 |
|
---|
465 |
|
---|
466 | char* xml_get_attribute(const char** attributes, char* attribute_name)
|
---|
467 | {
|
---|
468 | for (int i = 0; (attributes[i] != NULL); i += 2) {
|
---|
469 | if (strcmp(attribute_name, attributes[i]) == 0) {
|
---|
470 | return strdup(attributes[i+1]);
|
---|
471 | }
|
---|
472 | }
|
---|
473 |
|
---|
474 | return NULL;
|
---|
475 | }
|
---|
476 |
|
---|
477 |
|
---|
478 | static void XMLCALL gti_response_xml_start_element(void* user_data, const char* element_name_cstr, const char** attributes)
|
---|
479 | {
|
---|
480 | GTI_Response* gti_response = (GTI_Response*) user_data;
|
---|
481 | text_t element_name = element_name_cstr;
|
---|
482 | cerr << "In startElement() for " << element_name << endl;
|
---|
483 |
|
---|
484 | if (element_name == "GTIError") {
|
---|
485 | gti_response->recorded_text = "";
|
---|
486 | gti_response->is_recording_text = true;
|
---|
487 | }
|
---|
488 |
|
---|
489 | if (element_name == "TranslationFile") {
|
---|
490 | gti_response->translation_file_key = xml_get_attribute(attributes, "key");
|
---|
491 | gti_response->translation_files_key_to_null_mapping[gti_response->translation_file_key] = "";
|
---|
492 | gti_response->translation_files_key_to_target_file_path_mapping[gti_response->translation_file_key] = xml_get_attribute(attributes, "target_file_path");
|
---|
493 | gti_response->translation_files_key_to_num_chunks_translated_mapping[gti_response->translation_file_key] = xml_get_attribute(attributes, "num_chunks_translated");
|
---|
494 | gti_response->translation_files_key_to_num_chunks_requiring_translation_mapping[gti_response->translation_file_key] = xml_get_attribute(attributes, "num_chunks_requiring_translation");
|
---|
495 | gti_response->translation_files_key_to_num_chunks_requiring_updating_mapping[gti_response->translation_file_key] = xml_get_attribute(attributes, "num_chunks_requiring_updating");
|
---|
496 | }
|
---|
497 |
|
---|
498 | if (element_name == "ChunksMatchingQuery") {
|
---|
499 | gti_response->num_chunks_matching_query = xml_get_attribute(attributes, "size");
|
---|
500 | }
|
---|
501 |
|
---|
502 | if (element_name == "Chunk") {
|
---|
503 | gti_response->chunk_key = xml_get_attribute(attributes, "key");
|
---|
504 | }
|
---|
505 |
|
---|
506 | if (element_name == "SourceFileText") {
|
---|
507 | gti_response->source_file_chunks_key_to_date_mapping[gti_response->chunk_key] = xml_get_attribute(attributes, "date");
|
---|
508 | gti_response->recorded_text = "";
|
---|
509 | gti_response->is_recording_text = true;
|
---|
510 | }
|
---|
511 |
|
---|
512 | if (element_name == "TargetFileText") {
|
---|
513 | if (xml_get_attribute(attributes, "date") != NULL) {
|
---|
514 | gti_response->target_file_chunks_key_to_date_mapping[gti_response->chunk_key] = xml_get_attribute(attributes, "date");
|
---|
515 | }
|
---|
516 | gti_response->recorded_text = "";
|
---|
517 | gti_response->is_recording_text = true;
|
---|
518 | }
|
---|
519 | }
|
---|
520 |
|
---|
521 |
|
---|
522 | static void XMLCALL gti_response_xml_end_element(void* user_data, const char* element_name_cstr)
|
---|
523 | {
|
---|
524 | GTI_Response* gti_response = (GTI_Response*) user_data;
|
---|
525 | text_t element_name = element_name_cstr;
|
---|
526 |
|
---|
527 | if (element_name == "GTIError") {
|
---|
528 | gti_response->error_message = to_uni(gti_response->recorded_text);
|
---|
529 | gti_response->is_recording_text = false;
|
---|
530 | }
|
---|
531 |
|
---|
532 | if (element_name == "SourceFileText") {
|
---|
533 | gti_response->source_file_chunks_key_to_text_mapping[gti_response->chunk_key] = to_uni(gti_response->recorded_text);
|
---|
534 | gti_response->is_recording_text = false;
|
---|
535 | }
|
---|
536 |
|
---|
537 | if (element_name == "TargetFileText") {
|
---|
538 | gti_response->target_file_chunks_key_to_text_mapping[gti_response->chunk_key] = to_uni(gti_response->recorded_text);
|
---|
539 | gti_response->is_recording_text = false;
|
---|
540 | }
|
---|
541 | }
|
---|
542 |
|
---|
543 |
|
---|
544 | static void XMLCALL gti_response_xml_character_data(void *user_data, const char* text_cstr, int text_length)
|
---|
545 | {
|
---|
546 | GTI_Response* gti_response = (GTI_Response*) user_data;
|
---|
547 | if (gti_response->is_recording_text) {
|
---|
548 | gti_response->recorded_text.appendcarr(text_cstr, text_length);
|
---|
549 | }
|
---|
550 | }
|
---|
551 |
|
---|
552 |
|
---|
553 |
|
---|
554 | GTI_Response gtiaction::do_gti_request(text_t gti_arguments, ostream& logout)
|
---|
555 | {
|
---|
556 | GTI_Response gti_response;
|
---|
557 | gti_response.is_recording_text = false;
|
---|
558 |
|
---|
559 | // Send the request to gti.pl and read the XML output
|
---|
560 | text_t gti_command = "perl -S " + filename_cat(gsdlhome, "bin", "script", "gti.pl") + " " + gti_arguments;
|
---|
561 | FILE *gti_pipe = popen(gti_command.getcstr(), "r");
|
---|
562 | if (gti_pipe == NULL) {
|
---|
563 | logout << "Error: Could not open pipe for GTI command " << gti_command << endl;
|
---|
564 | return gti_response;
|
---|
565 | }
|
---|
566 |
|
---|
567 | // Read the gti.pl response
|
---|
568 | text_t gti_response_xml_text;
|
---|
569 | while (!feof(gti_pipe)) {
|
---|
570 | char buffer[1024];
|
---|
571 | gti_response_xml_text.appendcarr(buffer, fread(buffer, 1, 1024, gti_pipe));
|
---|
572 | }
|
---|
573 | pclose(gti_pipe);
|
---|
574 |
|
---|
575 | // Parse the gti.pl response (XML)
|
---|
576 | logout << "Parsing GTI command response: " << gti_response_xml_text << endl;
|
---|
577 | XML_Parser xml_parser = XML_ParserCreate(NULL);
|
---|
578 | XML_SetUserData(xml_parser, >i_response);
|
---|
579 | XML_SetElementHandler(xml_parser, gti_response_xml_start_element, gti_response_xml_end_element);
|
---|
580 | XML_SetCharacterDataHandler(xml_parser, gti_response_xml_character_data);
|
---|
581 |
|
---|
582 | char* gti_response_xml_text_cstr = gti_response_xml_text.getcstr();
|
---|
583 | int parse_status = XML_Parse(xml_parser, gti_response_xml_text_cstr, strlen(gti_response_xml_text_cstr), XML_TRUE);
|
---|
584 | if (parse_status == XML_STATUS_ERROR) {
|
---|
585 | logout << "Parse error " << XML_ErrorString(XML_GetErrorCode(xml_parser)) << " at line " << XML_GetCurrentLineNumber(xml_parser) << endl;
|
---|
586 | return gti_response;
|
---|
587 | }
|
---|
588 |
|
---|
589 | free(gti_response_xml_text_cstr);
|
---|
590 | XML_ParserFree(xml_parser);
|
---|
591 |
|
---|
592 | logout << "Finished parse." << endl;
|
---|
593 | return gti_response;
|
---|
594 | }
|
---|
595 |
|
---|
596 |
|
---|
597 |
|
---|
598 | void gtiaction::do_gti_submission(text_t gti_arguments, text_t gti_submission, ostream& logout)
|
---|
599 | {
|
---|
600 | // Send the submission to gti.pl
|
---|
601 | text_t gti_command = "perl -S " + filename_cat(gsdlhome, "bin", "script", "gti.pl") + " " + gti_arguments;
|
---|
602 | FILE *gti_pipe = popen(gti_command.getcstr(), "w");
|
---|
603 | if (gti_pipe == NULL) {
|
---|
604 | logout << "Error: Could not open pipe for GTI command " << gti_command << endl;
|
---|
605 | return;
|
---|
606 | }
|
---|
607 |
|
---|
608 | // Write the gti.pl submission
|
---|
609 | char* gti_submission_cstr = gti_submission.getcstr();
|
---|
610 | fwrite(gti_submission_cstr, 1, strlen(gti_submission_cstr), gti_pipe);
|
---|
611 | free(gti_submission_cstr);
|
---|
612 |
|
---|
613 | pclose(gti_pipe);
|
---|
614 | }
|
---|
615 |
|
---|
616 |
|
---|
617 |
|
---|
618 | #endif // GSDL_USE_GTI_ACTION
|
---|