Ant.php 10 KB

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