Ant.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. Lib::loadClass('RouteBase');
  3. Lib::loadClass('UI');
  4. Lib::loadClass('DBG');
  5. /**
  6. * Executes ant build for dita file - creates pdf file
  7. * - ant build in SE/schema/ant-url_action/{object_namespace_to_file_path}/{function_name}/build.xml
  8. *
  9. * APP_PATH_SCHEMA . '/ant-url_action/' . {$namespace_sep_by_dot} . '/' . {$function_name} . '/build.xml'
  10. * APP_PATH_SCHEMA . '/ant-tool/' . {$ant_tool_name} . '/build.xml'
  11. *
  12. * usage:
  13. * index.php?_route=Ant
  14. * index.php?_route=Ant&_task=ant&path={$ant_tool_name}
  15. * index.php?_route=UrlAction_Ant
  16. * index.php?_route=UrlAction_Ant&_task=ant&path={$namespace_sep_by_dot}{$function_name}&featureID={featureID}&namespace={$namespace}
  17. *
  18. * @doc ant <exec> https://ant.apache.org/manual/Tasks/exec.html
  19. */
  20. class Route_Ant extends RouteBase {
  21. public $pathTools;
  22. public $pathUrlActions;
  23. public $antBin;
  24. public $_antUrlProjects;
  25. public $_antUrlProjectTemplates;
  26. public function __construct() {
  27. $this->pathTools = APP_PATH_SCHEMA . "/ant-tool/";
  28. $this->pathUrlActions = APP_PATH_SCHEMA . "/ant-url_action/";
  29. $this->antBin = APP_PATH_WWW . DS . 'stuff' . DS . 'dita-ot-2.3.3' . DS . 'bin' . DS . 'ant';
  30. $this->_antUrlProjects = [];// path => label
  31. $this->_antUrlProjectTemplates = [];// path => [ template => label ]
  32. }
  33. public function defaultAction() {
  34. UI::gora();
  35. UI::tag('h1', [], 'Ant');
  36. try {
  37. $taskList = $this->getAntToolList();
  38. DBG::nicePrint($taskList, 'ant-tool');
  39. $featureID = V::get('featureID', '', $_GET);
  40. $namespace = V::get('namespace', '', $_GET);
  41. UI::startTag('ul');
  42. foreach ($taskList as $path => $label) {
  43. $link = "index.php?_route=Ant&_task=ant&path={$path}";
  44. echo UI::h('li', [], [
  45. UI::h('a', ['href'=>$link], "Uruchom '{$label}'")
  46. ]);
  47. }
  48. UI::endTag('ul');
  49. } catch (Exception $e) {
  50. UI::alert('danger', $e->getMessage());
  51. DBG::log($e);
  52. }
  53. UI::dol();
  54. }
  55. public function antAction() {
  56. UI::gora();
  57. try {
  58. $path = V::get('path', '', $_GET);
  59. if (!$path) throw new Exception("Missing Ant path!");
  60. $taskList = $this->getAntToolList();
  61. if (!array_key_exists($path, $taskList)) throw new Exception("Ant path not exists! '{$path}'");
  62. $cmd = "cd {$this->pathTools}{$path} && {$this->antBin} 2>&1";// wymaga java jdk
  63. V::exec($cmd, $out, $ret);
  64. } catch (Exception $e) {
  65. UI::alert('danger', $e->getMessage());
  66. DBG::log($e);
  67. }
  68. UI::dol();
  69. }
  70. public function execAntTool($antTask) {
  71. $antList = $this->getAntToolList();
  72. if (!array_key_exists($antTask, $antList)) throw new Exception("ant task not found", 404);
  73. throw new Exception("TODO: run ant task '{$antTask}'", 501);
  74. }
  75. //todo headery xml itp - aby szly z anta do tej funkcji i wysylane byly przez php
  76. // /SE/ant.php?antTask=$ jakas nazwa z ant_tool
  77. //ant ogolne
  78. //
  79. public function getAntUrlActionList() {
  80. if (!empty($this->_antUrlProjects)) return $this->_antUrlProjects;
  81. V::exec("ls -1 {$this->pathUrlActions}*/*/build.xml", $out, $ret);
  82. $this->_antUrlProjects = array();
  83. foreach ($out as $line) {
  84. try {
  85. if ($this->pathUrlActions != substr($line, 0, strlen($this->pathUrlActions))) throw new Exception("Wrong line prefix '{$line}' - skip");
  86. if ('/build.xml' != substr($line, -1 * strlen('/build.xml'))) throw new Exception("Wrong line suffix '{$line}' - expected '/build.xml' - skip");
  87. } catch (Exception $e) {
  88. UI::alert('warning', $e->getMessage());
  89. continue;
  90. }
  91. $line = substr($line, strlen($this->pathUrlActions));
  92. $path = substr($line, 0, -1 * strlen('/build.xml'));
  93. $this->_antUrlProjects[$path] = $path;
  94. try {
  95. V::exec("cd {$this->pathUrlActions}/{$path} && ant -p|grep -v '^Unable to locate'|grep -v '^Buildfile:'", $out, $ret);
  96. // Test WFS for actions given featureID // TODO: first line - project description
  97. // Main targets:
  98. //
  99. // DescribeFeatureType URL_TASK Target DescribeFeatureType
  100. // GetFeature URL_TASK Target GetFeature
  101. // Default target: http_first_input
  102. if (!empty($out)) {
  103. $projectDesc = '';
  104. $templates = [];
  105. $state = 'projectDesc';// 'projectDesc', 'Main targets', 'Default target'
  106. foreach ($out as $line) {
  107. switch ($state) {
  108. case 'projectDesc': {
  109. if ('Main targets' == substr($line, 0, strlen('Main targets'))) {
  110. $state = 'Main targets';
  111. } else {
  112. $line = trim($line);
  113. if (!$projectDesc && $line) $projectDesc = $line;
  114. }
  115. } break;
  116. case 'Main targets': {
  117. if (false !== ($pos = strpos($line, 'URL_TASK'))) {
  118. $templateName = trim(substr($line, 0, $pos));
  119. $label = trim(substr($line, $pos + 9));
  120. if (!empty($templateName)) $templates[$templateName] = (!empty($label)) ? $label : $templateName;
  121. }
  122. if ('Default target' == substr($line, 0, strlen('Default target'))) {
  123. $state = 'Default target';
  124. }
  125. } break;
  126. }
  127. }
  128. if (!empty($projectDesc)) $this->_antUrlProjects[$path] = $projectDesc;
  129. if (!empty($templates)) foreach ($templates as $t => $l) $this->_antUrlProjectTemplates[$path][$t] = $l;
  130. }
  131. } catch (Exception $e) {
  132. DBG::log($e);
  133. }
  134. }
  135. DBG::log((array)$this, 'array', "Ant projects and main targets");
  136. return $this->_antUrlProjects;
  137. }
  138. public function getAntUrlActionTemplates($path) {
  139. if (!empty($this->_antUrlProjectTemplates[$path])) return $this->_antUrlProjectTemplates[$path];
  140. $this->getAntUrlActionList();
  141. if (!empty($this->_antUrlProjectTemplates[$path])) return $this->_antUrlProjectTemplates[$path];
  142. }
  143. public function getAntToolList() {
  144. V::exec("ls -1 {$this->pathTools}*/build.xml", $out, $ret);
  145. $taskList = array();
  146. foreach ($out as $line) {
  147. try {
  148. if ($this->pathTools != substr($line, 0, strlen($this->pathTools))) throw new Exception("Wrong line prefix '{$line}' - skip");
  149. if ('/build.xml' != substr($line, -1 * strlen('/build.xml'))) throw new Exception("Wrong line suffix '{$line}' - expected '/build.xml' - skip");
  150. } catch (Exception $e) {
  151. UI::alert('warning', $e->getMessage());
  152. continue;
  153. }
  154. $line = substr($line, strlen($this->pathTools));
  155. $name = substr($line, 0, -1 * strlen('/build.xml'));
  156. $taskList[$name] = $name;// TODO: $name => $description (read from build.xml file first xml tag <project...>)
  157. }
  158. return $taskList;
  159. }
  160. public function generatePdfAction() {// TODO: OLD TEST
  161. $antPath = V::get('ditaPath', '', $_GET);
  162. $idKoresp = V::get('idKoresp', 0, $_POST, 'int');
  163. $doExecute = V::get('doExecute', '', $_POST);
  164. $antList = $this->getAntToolList();
  165. if (!array_key_exists($antPath, $antList)) throw new Exception("dita path not found", 404);
  166. UI::gora();
  167. UI::startContainer();
  168. try {
  169. UI::tag('h1', [], "Dita: '{$antList[$antPath]}'");
  170. UI::startTag('form', ['action'=>"", 'method'=>"POST", 'class'=>'form-inline']);
  171. UI::tag('input', ['type'=>'number', 'value'=>$idKoresp, 'class'=>'form-control', 'style'=>'max-width:200px']);
  172. UI::tag('input', ['type'=>'hidden', 'name'=>'doExecute', 'value'=>'1']);
  173. UI::tag('input', ['type'=>"submit", 'value'=>"Wykonaj", 'class'=>'btn btn-primary']);
  174. UI::endTag('form');
  175. if ($doExecute) {
  176. // if ($idKoresp <= 0) throw new Exception("Bad Request - missing id koresp");
  177. if ($idKoresp <= 0) UI::alert('danger', "Missing id koresp - using default file");
  178. if ($idKoresp > 0) UI::alert('warning', "TODO: generate xml file for id koresp = {$idKoresp}");
  179. $execPath = APP_PATH_SCHEMA . DS . 'ant' . DS . $antPath;
  180. $ant = APP_PATH_WWW . DS . 'stuff' . DS . 'dita-ot-2.3.3' . DS . 'bin' . DS . 'ant';
  181. $cmd = "cd {$execPath} && {$ant} 2>&1";// wymaga java jdk
  182. V::exec($cmd, $out, $ret);
  183. DBG::_(true, true, "cmd: {$cmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__);
  184. }
  185. throw new Exception("TODO: F." . __FUNCTION__ . " . L." . __LINE__);
  186. } catch (Exception $e) {
  187. UI::alert('danger', $e->getMessage());
  188. }
  189. UI::endContainer();
  190. UI::dol();
  191. }
  192. public function reinstallAction() {
  193. UI::gora();
  194. UI::tag('h1', [], 'Reinstall Dita Open Toolkit 2.3.3');
  195. $stuffPath = APP_PATH_WWW . DS . 'stuff';
  196. // $cmd[]['rsh']="cd /Library/Server/Web/Data/Sites/Default/SE/stuff && rm dita-ot-2.3.3.zip || echo PASSED";
  197. // $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 ";
  198. // $cmd[]['rsh']="cd /Library/Server/Web/Data/Sites/Default/SE/stuff && rm dita-ot-2.3.3 || echo PASSED";
  199. // $cmd[]['rsh']="cd /Library/Server/Web/Data/Sites/Default/SE/stuff && unzip dita-ot-2.3.3.zip || echo PASSED";
  200. // $cmd[]['rsh']="cd /Library/Server/Web/Data/Sites/Default/SE/stuff/dita-ot-2.3.3 && ./startcmd.sh || echo PASSED";
  201. $wget = "/opt/local/bin/wget";
  202. $unzip = "/usr/bin/unzip";
  203. // $testCmd = "which unzip";
  204. // V::exec($testCmd, $out, $ret);
  205. // DBG::_(true, true, "testCmd: {$testCmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__);
  206. // $testCmd = "which wget";
  207. // V::exec($testCmd, $out, $ret);
  208. // DBG::_(true, true, "testCmd: {$testCmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__);
  209. $cmd = "cd {$stuffPath} ; [ -f dita-ot-2.3.3.zip ] && rm dita-ot-2.3.3.zip";
  210. V::exec($cmd, $out, $ret);
  211. DBG::_(true, true, "cmd: {$cmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__);
  212. $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";
  213. V::exec($cmd, $out, $ret);
  214. DBG::_(true, true, "cmd: {$cmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__);
  215. $cmd = "cd {$stuffPath}
  216. [ -f dita-ot-2.3.3.zip ] && rm dita-ot-2.3.3.zip
  217. {$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
  218. [ -d dita-ot-2.3.3 ] && rm -rf dita-ot-2.3.3
  219. {$unzip} dita-ot-2.3.3.zip
  220. ";
  221. V::exec($cmd, $out, $ret);
  222. DBG::_(true, true, "cmd: {$cmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__);
  223. }
  224. }