https://ant.apache.org/manual/Tasks/exec.html */ class Route_Ant extends RouteBase { public $pathTools; public $pathUrlActions; public $antBin; public $_antUrlProjects; public $_antUrlProjectTemplates; public function __construct() { $this->pathTools = APP_PATH_SCHEMA . "/ant-tool/"; $this->pathUrlActions = APP_PATH_SCHEMA . "/ant-url_action"; $this->antBin = APP_PATH_WWW . DS . 'stuff' . DS . 'dita-ot-2.3.3' . DS . 'bin' . DS . 'ant'; $this->_antUrlProjects = [];// path => label $this->_antUrlProjectTemplates = [];// path => [ template => label ] } public function defaultAction() { UI::gora(); UI::tag('h1', [], 'Ant'); try { $taskList = $this->getAntToolList(); DBG::nicePrint($taskList, 'ant-tool'); $featureID = V::get('featureID', '', $_GET); $namespace = V::get('namespace', '', $_GET); UI::startTag('ul'); foreach ($taskList as $path => $label) { $link = "index.php?_route=Ant&_task=ant&path={$path}"; echo UI::h('li', [], [ UI::h('a', ['href'=>$link], "Uruchom '{$label}'") ]); } UI::endTag('ul'); } catch (Exception $e) { UI::alert('danger', $e->getMessage()); DBG::log($e); } UI::dol(); } public function antAction() { UI::gora(); try { $path = V::get('path', '', $_GET); if (!$path) throw new Exception("Missing Ant path!"); $taskList = $this->getAntToolList(); if (!array_key_exists($path, $taskList)) throw new Exception("Ant path not exists! '{$path}'"); $cmd = "cd {$this->pathTools}{$path} && {$this->antBin} -Xms5g -Xmx11g 2>&1";// wymaga java jdk V::exec($cmd, $out, $ret); } catch (Exception $e) { UI::alert('danger', $e->getMessage()); DBG::log($e); } UI::dol(); } public function execAntTool($antTask) { $antList = $this->getAntToolList(); if (!array_key_exists($antTask, $antList)) throw new Exception("ant task not found", 404); throw new Exception("TODO: run ant task '{$antTask}'", 501); } //todo headery xml itp - aby szly z anta do tej funkcji i wysylane byly przez php // /SE/ant.php?antTask=$ jakas nazwa z ant_tool //ant ogolne public function getAntUrlActionList() { if (!empty($this->_antUrlProjects)) return $this->_antUrlProjects; DBG::log("DBG::getAntUrlActionList START"); V::exec("ls -1 {$this->pathUrlActions}/*/*/build.xml", $out, $ret); DBG::log($out, 'array', "DBG::getAntUrlActionList ls -1 */*/build.xml ret({$ret})"); if (1) { // tmp use glob to store filemtime($file) $antFileList = glob("{$this->pathUrlActions}/*/*/build.xml", GLOB_NOSORT); DBG::log($antFileList, 'array', "DBG::getAntUrlActionList glob */*/build.xml"); // tmp use glob to store filemtime($file) $antFileList = V::glob("{$this->pathUrlActions}/*/*/build.xml", GLOB_NOSORT); $pathUrlActions = $this->pathUrlActions; DBG::log($pathUrlActions, 'array', "DBG::getAntUrlActionList pathUrlActions"); $antMtimeFileList = array_map(function ($filePath) use ($pathUrlActions) { // $pathUrlActions = '/Users/plabudda/rsync.se.master/SE/schema/ant-url_action' // 0 => '/Users/plabudda/rsync.se.master/SE/schema/ant-url_action/default_db.in7_dziennik_koresp/etykieta/build.xml', // 0 => 'default_db.in7_dziennik_koresp/etykieta/build.xml', list($ns, $projectFolder, $buildXml) = explode('/', str_replace($pathUrlActions . '/', '', $filePath)); return [ 'ns' => $ns, 'project' => $projectFolder, 'mtime' => filemtime($filePath), ]; }, $antFileList); DBG::log($antMtimeFileList, 'array', "DBG::getAntUrlActionList glob + mtime"); $forceUpdate = ('1' === V::get('forceUpdate', '', $_POST)); $antInfoList = array_map(function ($fileInfo) use ($forceUpdate) { return $this->fetchProjectInfo($fileInfo, $forceUpdate); }, $antMtimeFileList); DBG::log($antInfoList, 'array', "DBG::getAntUrlActionList antInfoList"); $this->_antUrlProjects = array(); foreach ($antInfoList as $antInfo) { $path = "{$antInfo['ns']}/{$antInfo['project']}"; $this->_antUrlProjects[$path] = $antInfo['label']; foreach ($antInfo['main_targets'] as $targetInfo) { $this->_antUrlProjectTemplates[$path][$targetInfo['target']] = $targetInfo['label']; } } return $this->_antUrlProjects; } $this->_antUrlProjects = array(); foreach ($out as $line) { try { if ($this->pathUrlActions != substr($line, 0, strlen($this->pathUrlActions))) throw new Exception("Wrong line prefix '{$line}' - skip"); if ('/build.xml' != substr($line, -1 * strlen('/build.xml'))) throw new Exception("Wrong line suffix '{$line}' - expected '/build.xml' - skip"); } catch (Exception $e) { UI::alert('warning', $e->getMessage()); continue; } $line = substr($line, strlen($this->pathUrlActions) + 1); $path = substr($line, 0, -1 * strlen('/build.xml')); $this->_antUrlProjects[$path] = $path; try { V::exec("cd {$this->pathUrlActions}/{$path} && ant -p|grep -v '^Unable to locate'|grep -v '^Buildfile:'", $out, $ret); // ant -p 2>/dev/null |grep -v '^Unable to locate'|grep -v '^Buildfile:' | grep 'URL_TASK\|^Default target:' // Test WFS for actions given featureID // TODO: first line - project description // Main targets: // // DescribeFeatureType URL_TASK Target DescribeFeatureType // GetFeature URL_TASK Target GetFeature // Default target: http_first_input if (!empty($out)) { $projectDesc = ''; $templates = []; $state = 'projectDesc';// 'projectDesc', 'Main targets', 'Default target' foreach ($out as $line) { switch ($state) { case 'projectDesc': { if ('Main targets' == substr($line, 0, strlen('Main targets'))) { $state = 'Main targets'; } else { $line = trim($line); if (!$projectDesc && $line) $projectDesc = $line; } } break; case 'Main targets': { if (false !== ($pos = strpos($line, 'URL_TASK'))) { $templateName = trim(substr($line, 0, $pos)); $label = trim(substr($line, $pos + 9)); if (!empty($templateName)) $templates[$templateName] = (!empty($label)) ? $label : $templateName; } if ('Default target' == substr($line, 0, strlen('Default target'))) { $state = 'Default target'; } } break; } } if (!empty($projectDesc)) $this->_antUrlProjects[$path] = $projectDesc; if (!empty($templates)) foreach ($templates as $t => $l) $this->_antUrlProjectTemplates[$path][$t] = $l; } } catch (Exception $e) { DBG::log($e); } } DBG::log((array)$this, 'array', "DBG::getAntUrlActionList END Ant projects and main targets"); return $this->_antUrlProjects; } function prepareCache($fileInfo) { // 'ns' => $ns, // 'project' => $projectFolder, // 'mtime' => filemtime($filePath), $sql = " CREATE TABLE IF NOT EXISTS `ANT_TOOL_#CACHE_PROJECT` ( `ID` int(11) NOT NULL AUTO_INCREMENT, `file_ns` varchar(255) NOT NULL, `file_project` varchar(255) NOT NULL, `file_mtime` int(11) unsigned NOT NULL DEFAULT 0, `label` varchar(255) DEFAULT '', `error` varchar(255) DEFAULT '', `is_active` tinyint(1) NOT NULL DEFAULT 0, `A_RECORD_CREATE_DATE` datetime NOT NULL, `A_RECORD_UPDATE_DATE` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', PRIMARY KEY (`ID`), KEY (`is_active`), UNIQUE KEY `uniq` (`file_ns`,`file_project`) ) ENGINE=MyISAM DEFAULT CHARSET=latin2; "; DB::getPDO()->execSql($sql); $sql = " CREATE TABLE IF NOT EXISTS `ANT_TOOL_#CACHE_MAIN_TARGETS` ( `ID_ANT_PROJECT` int(11) NOT NULL, `NAME` varchar(255) DEFAULT '', `LABEL` varchar(255) DEFAULT '', `A_RECORD_CREATE_DATE` datetime NOT NULL, KEY `ID_ANT_PROJECT` (`ID_ANT_PROJECT`), UNIQUE KEY `uniq` (`ID_ANT_PROJECT`,`NAME`) ) ENGINE=MyISAM DEFAULT CHARSET=latin2; "; DB::getPDO()->execSql($sql); } function fetchProjectInfo($fileInfo, $forceUpdate = false) { $cacheInfo = DB::getPDO()->tryHandleException([ $this, 'prepareCache' ], 'fetchFirst', $args = [ " select c.ID , c.file_ns , c.file_project , c.file_mtime , c.label from `ANT_TOOL_#CACHE_PROJECT` c where c.file_ns = :ns and c.file_project = :project ", [ ':ns' => $fileInfo['ns'], ':project' => $fileInfo['project'], ] ]); $parsedAntInfo = null; DBG::log($cacheInfo, 'array', "DBG:cacheInfo"); if (!$cacheInfo) { $errorMsg = ""; try { $parsedAntInfo = $this->parseAntOutput("{$this->pathUrlActions}/{$fileInfo['ns']}/{$fileInfo['project']}"); DBG::log($parsedAntInfo, 'array', "DBG:parsedAntInfo when !\$cacheInfo"); } catch (Exception $e) { $errorMsg = $e->getMessage(); } $idCache = DB::getPDO()->insert('ANT_TOOL_#CACHE_PROJECT', [ 'file_ns' => $fileInfo['ns'], 'file_project' => $fileInfo['project'], 'file_mtime' => $fileInfo['mtime'], 'label' => ($parsedAntInfo) ? $parsedAntInfo['label'] : '', 'error' => ($parsedAntInfo) ? '' : ( ($errorMsg) ? $errorMsg : 'Error' ), 'is_active' => ($errorMsg || !$parsedAntInfo) ? 0 : 1, 'A_RECORD_CREATE_DATE' => "NOW()", ]); DB::getPDO()->execSql(" delete from `ANT_TOOL_#CACHE_MAIN_TARGETS` where ID_ANT_PROJECT = :id ", [ ':id' => $idCache ]); if ($parsedAntInfo) foreach ($parsedAntInfo['main_targets'] as $targetInfo) { DB::getPDO()->insert('ANT_TOOL_#CACHE_MAIN_TARGETS', [ 'ID_ANT_PROJECT' => $idCache, 'NAME' => $targetInfo['target'], 'LABEL' => $targetInfo['label'], 'A_RECORD_CREATE_DATE' => "NOW()", ]); } } else if ($cacheInfo['file_mtime'] < $fileInfo['mtime'] || $forceUpdate) { // select c.ID, // c.file_ns, // c.file_project, // c.file_mtime, $errorMsg = "Error"; try { $parsedAntInfo = $this->parseAntOutput("{$this->pathUrlActions}/{$fileInfo['ns']}/{$fileInfo['project']}"); } catch (Exception $e) { $errorMsg = $e->getMessage(); } DBG::log($parsedAntInfo, 'array', "DBG:parsedAntInfo when file_mtime > cache || forceUpdate({$forceUpdate})"); DB::getPDO()->update('ANT_TOOL_#CACHE_PROJECT', 'ID', $cacheInfo['ID'], [ 'label' => ($parsedAntInfo) ? $parsedAntInfo['label'] : '', 'error' => ($errorMsg || !$parsedAntInfo) ? $errorMsg : '', 'A_RECORD_CREATE_DATE' => "NOW()", ]); DB::getPDO()->execSql(" delete from `ANT_TOOL_#CACHE_MAIN_TARGETS` where ID_ANT_PROJECT = :id ", [ ':id' => $cacheInfo['ID'] ]); if ($parsedAntInfo) foreach ($parsedAntInfo['main_targets'] as $targetInfo) { DB::getPDO()->insert('ANT_TOOL_#CACHE_MAIN_TARGETS', [ 'ID_ANT_PROJECT' => $cacheInfo['ID'], 'NAME' => $targetInfo['target'], 'LABEL' => $targetInfo['label'], 'A_RECORD_CREATE_DATE' => "NOW()", ]); } } else { $parsedAntInfo = [ 'label' => $cacheInfo['label'], 'main_targets' => DB::getPDO()->fetchAll(" select t.NAME as target , t.LABEL as label from `ANT_TOOL_#CACHE_MAIN_TARGETS` t where t.ID_ANT_PROJECT = :id ", [ ':id' => $cacheInfo['ID'], ]), ]; } return array_merge($fileInfo, $parsedAntInfo); } function parseAntOutput($antFolderPath) { // return [ label, main_targets => [ target, label ] ], throws Exception try { V::exec("cd {$antFolderPath} && ant -p|grep -v '^Unable to locate'|grep -v '^Buildfile:'", $out, $ret); // ant -p 2>/dev/null |grep -v '^Unable to locate'|grep -v '^Buildfile:' | grep 'URL_TASK\|^Default target:' // Test WFS for actions given featureID // TODO: first line - project description // Main targets: // // DescribeFeatureType URL_TASK Target DescribeFeatureType // GetFeature URL_TASK Target GetFeature // Default target: http_first_input if (empty($out)) throw new Exception("Missing ant -p output"); $projectDesc = ''; $mainTargets = []; $state = 'projectDesc'; // 'projectDesc', 'Main targets', 'Default target' foreach ($out as $line) { switch ($state) { case 'projectDesc': { if ('Main targets' == substr($line, 0, strlen('Main targets'))) { $state = 'Main targets'; } else { $line = trim($line); if (!$projectDesc && $line) $projectDesc = $line; } } break; case 'Main targets': { if (false !== ($pos = strpos($line, 'URL_TASK'))) { $templateName = trim(substr($line, 0, $pos)); $label = trim(substr($line, $pos + 9)); if (!empty($templateName)) $mainTargets[] = [ 'target' => $templateName, 'label' => (!empty($label)) ? $label : $templateName, ]; } if ('Default target' == substr($line, 0, strlen('Default target'))) { $state = 'Default target'; } } break; } if ('Default target' === $state) break; } if (empty($projectDesc)) throw new Exception("Missing project label (first line in ant -p)"); if (empty($mainTargets)) throw new Exception("Missing main targets with type URL_TASK"); return [ 'label' => $projectDesc, 'main_targets' => $mainTargets, ]; } catch (Exception $e) { DBG::log($e); throw $e; } } public function getAntUrlActionTemplates($path) { if (!empty($this->_antUrlProjectTemplates[$path])) return $this->_antUrlProjectTemplates[$path]; $this->getAntUrlActionList(); if (!empty($this->_antUrlProjectTemplates[$path])) return $this->_antUrlProjectTemplates[$path]; } public function getAntToolList() { V::exec("ls -1 {$this->pathTools}*/build.xml", $out, $ret); $taskList = array(); foreach ($out as $line) { try { if ($this->pathTools != substr($line, 0, strlen($this->pathTools))) throw new Exception("Wrong line prefix '{$line}' - skip"); if ('/build.xml' != substr($line, -1 * strlen('/build.xml'))) throw new Exception("Wrong line suffix '{$line}' - expected '/build.xml' - skip"); } catch (Exception $e) { UI::alert('warning', $e->getMessage()); continue; } $line = substr($line, strlen($this->pathTools)); $name = substr($line, 0, -1 * strlen('/build.xml')); $taskList[$name] = $name;// TODO: $name => $description (read from build.xml file first xml tag ) } return $taskList; } public function generatePdfAction() {// TODO: OLD TEST $antPath = V::get('ditaPath', '', $_GET); $idKoresp = V::get('idKoresp', 0, $_POST, 'int'); $doExecute = V::get('doExecute', '', $_POST); $antList = $this->getAntToolList(); if (!array_key_exists($antPath, $antList)) throw new Exception("dita path not found", 404); UI::gora(); UI::startContainer(); try { UI::tag('h1', [], "Dita: '{$antList[$antPath]}'"); UI::startTag('form', ['action'=>"", 'method'=>"POST", 'class'=>'form-inline']); UI::tag('input', ['type'=>'number', 'value'=>$idKoresp, 'class'=>'form-control', 'style'=>'max-width:200px']); UI::tag('input', ['type'=>'hidden', 'name'=>'doExecute', 'value'=>'1']); UI::tag('input', ['type'=>"submit", 'value'=>"Wykonaj", 'class'=>'btn btn-primary']); UI::endTag('form'); if ($doExecute) { // if ($idKoresp <= 0) throw new Exception("Bad Request - missing id koresp"); if ($idKoresp <= 0) UI::alert('danger', "Missing id koresp - using default file"); if ($idKoresp > 0) UI::alert('warning', "TODO: generate xml file for id koresp = {$idKoresp}"); $execPath = APP_PATH_SCHEMA . DS . 'ant' . DS . $antPath; $ant = APP_PATH_WWW . DS . 'stuff' . DS . 'dita-ot-2.3.3' . DS . 'bin' . DS . 'ant'; $cmd = "cd {$execPath} && {$ant} 2>&1";// wymaga java jdk V::exec($cmd, $out, $ret); DBG::_(true, true, "cmd: {$cmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__); } throw new Exception("TODO: F." . __FUNCTION__ . " . L." . __LINE__); } catch (Exception $e) { UI::alert('danger', $e->getMessage()); } UI::endContainer(); UI::dol(); } public function reinstallAction() { UI::gora(); UI::tag('h1', [], 'Reinstall Dita Open Toolkit 2.3.3'); $stuffPath = APP_PATH_WWW . DS . 'stuff'; // $cmd[]['rsh']="cd /Library/Server/Web/Data/Sites/Default/SE/stuff && rm dita-ot-2.3.3.zip || echo PASSED"; // $cmd[]['rsh']="cd /Library/Server/Web/Data/Sites/Default/SE/stuff && wget https://github.com/dita-ot/dita-ot/releases/download/2.3.3/dita-ot-2.3.3.zip -O dita-ot-2.3.3.zip "; // $cmd[]['rsh']="cd /Library/Server/Web/Data/Sites/Default/SE/stuff && rm dita-ot-2.3.3 || echo PASSED"; // $cmd[]['rsh']="cd /Library/Server/Web/Data/Sites/Default/SE/stuff && unzip dita-ot-2.3.3.zip || echo PASSED"; // $cmd[]['rsh']="cd /Library/Server/Web/Data/Sites/Default/SE/stuff/dita-ot-2.3.3 && ./startcmd.sh || echo PASSED"; $wget = "/opt/local/bin/wget"; $unzip = "/usr/bin/unzip"; // $testCmd = "which unzip"; // V::exec($testCmd, $out, $ret); // DBG::_(true, true, "testCmd: {$testCmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__); // $testCmd = "which wget"; // V::exec($testCmd, $out, $ret); // DBG::_(true, true, "testCmd: {$testCmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__); $cmd = "cd {$stuffPath} ; [ -f dita-ot-2.3.3.zip ] && rm dita-ot-2.3.3.zip"; V::exec($cmd, $out, $ret); DBG::_(true, true, "cmd: {$cmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__); $cmd = "cd {$stuffPath} ; {$wget} https://github.com/dita-ot/dita-ot/releases/download/2.3.3/dita-ot-2.3.3.zip -O dita-ot-2.3.3.zip"; V::exec($cmd, $out, $ret); DBG::_(true, true, "cmd: {$cmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__); $cmd = "cd {$stuffPath} [ -f dita-ot-2.3.3.zip ] && rm dita-ot-2.3.3.zip {$wget} https://github.com/dita-ot/dita-ot/releases/download/2.3.3/dita-ot-2.3.3.zip -O dita-ot-2.3.3.zip [ -d dita-ot-2.3.3 ] && rm -rf dita-ot-2.3.3 {$unzip} dita-ot-2.3.3.zip "; V::exec($cmd, $out, $ret); DBG::_(true, true, "cmd: {$cmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__); } }