From 96f9b453a27790d794801a957914ef8cc69c3a3e Mon Sep 17 00:00:00 2001 From: Conrad Vermeulen Date: Fri, 1 Feb 2008 09:18:10 +0000 Subject: [PATCH] KTS-2903 "Improved capture of output from exec on text extractors" Fixed --- search2/indexing/extractorCore.inc.php | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 57 insertions(+), 5 deletions(-) diff --git a/search2/indexing/extractorCore.inc.php b/search2/indexing/extractorCore.inc.php index 9110fcc..0a19fb2 100755 --- a/search2/indexing/extractorCore.inc.php +++ b/search2/indexing/extractorCore.inc.php @@ -101,6 +101,13 @@ abstract class DocumentExtractor */ protected $indexStatus; + /** + * If an error occurred, this is the output that was captured + * + * @var string + */ + public $output; + public function __construct() { @@ -349,21 +356,66 @@ abstract class ExternalDocumentExtractor extends DocumentExtractor */ protected function exec($cmd) { + $config = KTConfig::getSingleton(); + $temp_dir = $config->get('urls/tmpDirectory'); + $res = 0; + + $script_prefix = $temp_dir . '/' . time(); + $script_out = $script_prefix . '.out'; + + // define the scripts that we want + if (OS_WINDOWS) { + $script_name = $script_prefix . '.bat'; + $script = "rem This is an auto generated file. \n"; + $script .= $cmd . ' 2> ' . $script_out . "\r\n"; + } + else + { + $script_name = $script_prefix . '.sh'; - $WshShell = new COM("WScript.Shell"); - $res = $WshShell->Run($cmd, 0, true); + $script = "#!/bin/sh\n"; + $script .= "# This is an auto generated file. \n"; + $script .= $cmd . ' 2> ' . $script_out . "\n"; + $script .= "exit $?\n"; + } + // write the script file + if (file_put_contents($script_name, $script) === false) + { + $this->output = _kt('Could not create exec script: ') . $script_name; + return false; + } - return $res == 0; + // execute the script file + if (OS_WINDOWS) + { + $WshShell = new COM("WScript.Shell"); + $res = $WshShell->Run($script_name, 0, true); } else { - $aRet = KTUtil::pexec($cmd); - return $aRet['ret'] == 0; + if (chmod($script_name, 0755) === false) + { + $this->output = _kt('Could change permission on exec script: ') . $script_name; + return false; + } + $aRet = KTUtil::pexec($script_name); + $res = $aRet['ret']; + } + + // remote the script file and get the output if available + @unlink($script_name); + + if (file_exists($script_out)) + { + $this->output = file_get_contents($script_out); + @unlink($script_out); } + + return $res == 0; } /** -- libgit2 0.21.4