source: trunk/gsdl/src/recpt/gtiaction.cpp@ 10035

Last change on this file since 10035 was 10035, checked in by mdewsnip, 19 years ago

Removed the unnecessary PATH setting code and tidied up the rest of the init() function.

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