Ant.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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} -Xms5g -Xmx11g 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. public function getAntUrlActionList() {
  79. if (!empty($this->_antUrlProjects)) return $this->_antUrlProjects;
  80. DBG::log("DBG::getAntUrlActionList START");
  81. V::exec("ls -1 {$this->pathUrlActions}/*/*/build.xml", $out, $ret);
  82. DBG::log($out, 'array', "DBG::getAntUrlActionList ls -1 */*/build.xml ret({$ret})");
  83. if (0) { // tmp use glob to store filemtime($file)
  84. $antFileList = glob("{$this->pathUrlActions}/*/*/build.xml", GLOB_NOSORT);
  85. DBG::log($antFileList, 'array', "DBG::getAntUrlActionList glob */*/build.xml");
  86. // tmp use glob to store filemtime($file)
  87. $antFileList = V::glob("{$this->pathUrlActions}/*/*/build.xml", GLOB_NOSORT);
  88. $pathUrlActions = $this->pathUrlActions;
  89. DBG::log($pathUrlActions, 'array', "DBG::getAntUrlActionList pathUrlActions");
  90. $antMtimeFileList = array_map(function ($filePath) use ($pathUrlActions) {
  91. // $pathUrlActions = '/Users/plabudda/rsync.se.master/SE/schema/ant-url_action'
  92. // 0 => '/Users/plabudda/rsync.se.master/SE/schema/ant-url_action/default_db.in7_dziennik_koresp/etykieta/build.xml',
  93. // 0 => 'default_db.in7_dziennik_koresp/etykieta/build.xml',
  94. list($ns, $folderName, $buildXml) = explode('/', str_replace($pathUrlActions . '/', '', $filePath));
  95. return [
  96. 'ns' => $ns,
  97. 'folderName' => $folderName,
  98. 'mtime' => filemtime($filePath),
  99. ];
  100. }, $antFileList);
  101. DBG::log($antMtimeFileList, 'array', "DBG::getAntUrlActionList glob + mtime");
  102. throw new Exception("TODO: DBG: END.");
  103. }
  104. $this->_antUrlProjects = array();
  105. foreach ($out as $line) {
  106. try {
  107. if ($this->pathUrlActions != substr($line, 0, strlen($this->pathUrlActions))) throw new Exception("Wrong line prefix '{$line}' - skip");
  108. if ('/build.xml' != substr($line, -1 * strlen('/build.xml'))) throw new Exception("Wrong line suffix '{$line}' - expected '/build.xml' - skip");
  109. } catch (Exception $e) {
  110. UI::alert('warning', $e->getMessage());
  111. continue;
  112. }
  113. $line = substr($line, strlen($this->pathUrlActions) + 1);
  114. $path = substr($line, 0, -1 * strlen('/build.xml'));
  115. $this->_antUrlProjects[$path] = $path;
  116. try {
  117. V::exec("cd {$this->pathUrlActions}/{$path} && ant -p|grep -v '^Unable to locate'|grep -v '^Buildfile:'", $out, $ret);
  118. // Test WFS for actions given featureID // TODO: first line - project description
  119. // Main targets:
  120. //
  121. // DescribeFeatureType URL_TASK Target DescribeFeatureType
  122. // GetFeature URL_TASK Target GetFeature
  123. // Default target: http_first_input
  124. if (!empty($out)) {
  125. $projectDesc = '';
  126. $templates = [];
  127. $state = 'projectDesc';// 'projectDesc', 'Main targets', 'Default target'
  128. foreach ($out as $line) {
  129. switch ($state) {
  130. case 'projectDesc': {
  131. if ('Main targets' == substr($line, 0, strlen('Main targets'))) {
  132. $state = 'Main targets';
  133. } else {
  134. $line = trim($line);
  135. if (!$projectDesc && $line) $projectDesc = $line;
  136. }
  137. } break;
  138. case 'Main targets': {
  139. if (false !== ($pos = strpos($line, 'URL_TASK'))) {
  140. $templateName = trim(substr($line, 0, $pos));
  141. $label = trim(substr($line, $pos + 9));
  142. if (!empty($templateName)) $templates[$templateName] = (!empty($label)) ? $label : $templateName;
  143. }
  144. if ('Default target' == substr($line, 0, strlen('Default target'))) {
  145. $state = 'Default target';
  146. }
  147. } break;
  148. }
  149. }
  150. if (!empty($projectDesc)) $this->_antUrlProjects[$path] = $projectDesc;
  151. if (!empty($templates)) foreach ($templates as $t => $l) $this->_antUrlProjectTemplates[$path][$t] = $l;
  152. }
  153. } catch (Exception $e) {
  154. DBG::log($e);
  155. }
  156. }
  157. DBG::log((array)$this, 'array', "DBG::getAntUrlActionList END Ant projects and main targets");
  158. return $this->_antUrlProjects;
  159. }
  160. public function getAntUrlActionTemplates($path) {
  161. if (!empty($this->_antUrlProjectTemplates[$path])) return $this->_antUrlProjectTemplates[$path];
  162. $this->getAntUrlActionList();
  163. if (!empty($this->_antUrlProjectTemplates[$path])) return $this->_antUrlProjectTemplates[$path];
  164. }
  165. public function getAntToolList() {
  166. V::exec("ls -1 {$this->pathTools}*/build.xml", $out, $ret);
  167. $taskList = array();
  168. foreach ($out as $line) {
  169. try {
  170. if ($this->pathTools != substr($line, 0, strlen($this->pathTools))) throw new Exception("Wrong line prefix '{$line}' - skip");
  171. if ('/build.xml' != substr($line, -1 * strlen('/build.xml'))) throw new Exception("Wrong line suffix '{$line}' - expected '/build.xml' - skip");
  172. } catch (Exception $e) {
  173. UI::alert('warning', $e->getMessage());
  174. continue;
  175. }
  176. $line = substr($line, strlen($this->pathTools));
  177. $name = substr($line, 0, -1 * strlen('/build.xml'));
  178. $taskList[$name] = $name;// TODO: $name => $description (read from build.xml file first xml tag <project...>)
  179. }
  180. return $taskList;
  181. }
  182. public function generatePdfAction() {// TODO: OLD TEST
  183. $antPath = V::get('ditaPath', '', $_GET);
  184. $idKoresp = V::get('idKoresp', 0, $_POST, 'int');
  185. $doExecute = V::get('doExecute', '', $_POST);
  186. $antList = $this->getAntToolList();
  187. if (!array_key_exists($antPath, $antList)) throw new Exception("dita path not found", 404);
  188. UI::gora();
  189. UI::startContainer();
  190. try {
  191. UI::tag('h1', [], "Dita: '{$antList[$antPath]}'");
  192. UI::startTag('form', ['action'=>"", 'method'=>"POST", 'class'=>'form-inline']);
  193. UI::tag('input', ['type'=>'number', 'value'=>$idKoresp, 'class'=>'form-control', 'style'=>'max-width:200px']);
  194. UI::tag('input', ['type'=>'hidden', 'name'=>'doExecute', 'value'=>'1']);
  195. UI::tag('input', ['type'=>"submit", 'value'=>"Wykonaj", 'class'=>'btn btn-primary']);
  196. UI::endTag('form');
  197. if ($doExecute) {
  198. // if ($idKoresp <= 0) throw new Exception("Bad Request - missing id koresp");
  199. if ($idKoresp <= 0) UI::alert('danger', "Missing id koresp - using default file");
  200. if ($idKoresp > 0) UI::alert('warning', "TODO: generate xml file for id koresp = {$idKoresp}");
  201. $execPath = APP_PATH_SCHEMA . DS . 'ant' . DS . $antPath;
  202. $ant = APP_PATH_WWW . DS . 'stuff' . DS . 'dita-ot-2.3.3' . DS . 'bin' . DS . 'ant';
  203. $cmd = "cd {$execPath} && {$ant} 2>&1";// wymaga java jdk
  204. V::exec($cmd, $out, $ret);
  205. DBG::_(true, true, "cmd: {$cmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__);
  206. }
  207. throw new Exception("TODO: F." . __FUNCTION__ . " . L." . __LINE__);
  208. } catch (Exception $e) {
  209. UI::alert('danger', $e->getMessage());
  210. }
  211. UI::endContainer();
  212. UI::dol();
  213. }
  214. public function reinstallAction() {
  215. UI::gora();
  216. UI::tag('h1', [], 'Reinstall Dita Open Toolkit 2.3.3');
  217. $stuffPath = APP_PATH_WWW . DS . 'stuff';
  218. // $cmd[]['rsh']="cd /Library/Server/Web/Data/Sites/Default/SE/stuff && rm dita-ot-2.3.3.zip || echo PASSED";
  219. // $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 ";
  220. // $cmd[]['rsh']="cd /Library/Server/Web/Data/Sites/Default/SE/stuff && rm dita-ot-2.3.3 || echo PASSED";
  221. // $cmd[]['rsh']="cd /Library/Server/Web/Data/Sites/Default/SE/stuff && unzip dita-ot-2.3.3.zip || echo PASSED";
  222. // $cmd[]['rsh']="cd /Library/Server/Web/Data/Sites/Default/SE/stuff/dita-ot-2.3.3 && ./startcmd.sh || echo PASSED";
  223. $wget = "/opt/local/bin/wget";
  224. $unzip = "/usr/bin/unzip";
  225. // $testCmd = "which unzip";
  226. // V::exec($testCmd, $out, $ret);
  227. // DBG::_(true, true, "testCmd: {$testCmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__);
  228. // $testCmd = "which wget";
  229. // V::exec($testCmd, $out, $ret);
  230. // DBG::_(true, true, "testCmd: {$testCmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__);
  231. $cmd = "cd {$stuffPath} ; [ -f dita-ot-2.3.3.zip ] && rm dita-ot-2.3.3.zip";
  232. V::exec($cmd, $out, $ret);
  233. DBG::_(true, true, "cmd: {$cmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__);
  234. $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";
  235. V::exec($cmd, $out, $ret);
  236. DBG::_(true, true, "cmd: {$cmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__);
  237. $cmd = "cd {$stuffPath}
  238. [ -f dita-ot-2.3.3.zip ] && rm dita-ot-2.3.3.zip
  239. {$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
  240. [ -d dita-ot-2.3.3 ] && rm -rf dita-ot-2.3.3
  241. {$unzip} dita-ot-2.3.3.zip
  242. ";
  243. V::exec($cmd, $out, $ret);
  244. DBG::_(true, true, "cmd: {$cmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__);
  245. }
  246. }