manage.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaManageCmd UNA Manage command line script
  7. * @{
  8. *
  9. /**
  10. * UNA command line interface
  11. */
  12. class BxDolManageCmd
  13. {
  14. protected $_sCmd;
  15. protected $_sCmdOptions;
  16. protected $_sPathToUna;
  17. protected $_aSiteConfig;
  18. protected $_isQuiet = false;
  19. protected $_aReturnCodes = array(
  20. 'success' => array ('code' => 0, 'msg' => 'Success.'),
  21. 'una not found' => array ('code' => 1, 'msg' => 'UNA wasn\'t found in the specified path: '),
  22. 'unknown cmd' => array ('code' => 2, 'msg' => 'Unknown command.'),
  23. 'db connect failed' => array ('code' => 3, 'msg' => 'Database connection failed: '),
  24. 'error' => array ('code' => 9, 'msg' => 'Error occured.'),
  25. );
  26. public function __construct()
  27. {
  28. $this->_aSiteConfig = array (
  29. 'db_host' => '',
  30. 'db_port' => '',
  31. 'db_sock' => '',
  32. 'db_name' => '',
  33. 'db_user' => '',
  34. 'db_password' => '',
  35. );
  36. }
  37. public function main()
  38. {
  39. // set neccessary options
  40. $a = getopt('hqc:u:p:o:', $this->getOptions());
  41. if (isset($a['h']))
  42. $this->finish($this->_aReturnCodes['success']['code'], $this->getHelp());
  43. if (isset($a['q']))
  44. $this->_isQuiet = true;
  45. if (isset($a['u']))
  46. $this->_sPathToUna = $a['u'];
  47. if (isset($a['c']))
  48. $this->_sCmd = $a['c'];
  49. if (isset($a['o']))
  50. $this->_sCmdOptions = $a['o'];
  51. $this->_aSiteConfig = array_merge($this->_aSiteConfig, $a);
  52. // initialize environment
  53. $this->init();
  54. // run command
  55. $sMethod = $this->_sCmd ? 'cmd' . bx_gen_method_name($this->_sCmd) : 'cmdDefault';
  56. if (!method_exists($this, $sMethod))
  57. $this->finish($this->_aReturnCodes['unknown cmd']['code'], $this->_aReturnCodes['unknown cmd']['msg']);
  58. $this->$sMethod();
  59. $this->finish($this->_aReturnCodes['success']['code'], $this->_aReturnCodes['success']['msg']);
  60. }
  61. protected function getOptions()
  62. {
  63. $a = array ();
  64. foreach ($this->_aSiteConfig as $sKey => $sValue)
  65. if ('site_config' != $sKey)
  66. $a[] = "$sKey::";
  67. return $a;
  68. }
  69. protected function getHelp()
  70. {
  71. $s = "Usage: php manage.php [params]\n";
  72. $s .= str_pad("\t -h", 35) . "Print this help\n";
  73. $s .= str_pad("\t -q", 35) . "Quiet\n";
  74. $s .= str_pad("\t -u", 35) . "Path to UNA\n";
  75. $s .= str_pad("\t -c", 35) . "Command to run (update, update_modules, install_modules)\n";
  76. $s .= str_pad("\t -o", 35) . "Command options, comma separated list of modules names for 'update_modules' and 'install_module' commands\n";
  77. foreach ($this->_aSiteConfig as $sKey => $sVal)
  78. if ('site_config' != $sKey)
  79. $s .= str_pad("\t --{$sKey}=<value>", 35) . "Database connection params\n";
  80. $s .= "\nIf DB params aren't specified then UNA DB params from header.inc.php are used.\n\n";
  81. $s .= "Return codes:\n";
  82. foreach ($this->_aReturnCodes as $r)
  83. $s .= str_pad("\t {$r['code']}", 5) . "{$r['msg']}\n";
  84. return $s;
  85. }
  86. protected function finish($iCode, $sMsg)
  87. {
  88. if (!$this->_isQuiet)
  89. fwrite($iCode ? STDERR : STDOUT, $sMsg . "\n");
  90. exit($iCode);
  91. }
  92. protected function init()
  93. {
  94. $this->_sPathToUna = trim($this->_sPathToUna);
  95. if ('.' == $this->_sPathToUna)
  96. $this->_sPathToUna = '';
  97. if ($this->_sPathToUna && '/' !== $this->_sPathToUna[0]) {
  98. $aPathInfo = pathinfo(__FILE__);
  99. $this->_sPathToUna = $aPathInfo['dirname'] . '/' . $this->_sPathToUna;
  100. }
  101. $this->_sPathToUna = rtrim($this->_sPathToUna, '/');
  102. // include header.inc.php
  103. $sHeaderPath = $this->_sPathToUna . '/inc/header.inc.php';
  104. if (!file_exists($sHeaderPath))
  105. $this->finish($this->_aReturnCodes['una not found']['code'], $this->_aReturnCodes['una not found']['msg'] . $sHeaderPath);
  106. require_once($sHeaderPath);
  107. // db connection
  108. if (!empty($this->_aSiteConfig['db_name'])) {
  109. BxDolDb::getInstance()->cacheParamsClear();
  110. BxDolDb::getInstance()->disconnect();
  111. unset($GLOBALS['bxDolClasses']['BxDolDb']);
  112. $sErr = null;
  113. try {
  114. $oDb = BxDolDb::getInstanceWithConf(array(
  115. 'host' => $this->_aSiteConfig['db_host'],
  116. 'port' => $this->_aSiteConfig['db_port'],
  117. 'sock' => $this->_aSiteConfig['db_sock'],
  118. 'name' => $this->_aSiteConfig['db_name'],
  119. 'user' => $this->_aSiteConfig['db_user'],
  120. 'pwd' => $this->_aSiteConfig['db_password'],
  121. ), $sErr);
  122. } catch (Exception $e) {
  123. $this->finish($this->_aReturnCodes['db connect failed']['code'], $this->_aReturnCodes['db connect failed']['msg'] . $e->getMessage());
  124. }
  125. }
  126. }
  127. function cmdUpdate()
  128. {
  129. }
  130. function cmdUpdateModules()
  131. {
  132. $aModules = $this->_parseModules($this->_sCmdOptions);
  133. var_dump($aModules);
  134. }
  135. function cmdInstallModules()
  136. {
  137. $aModules = $this->_parseModules($this->_sCmdOptions);
  138. var_dump($aModules);
  139. }
  140. function _parseModules($s)
  141. {
  142. if (!$s)
  143. return [];
  144. $a = explode(',', trim($s));
  145. foreach ($a as $k => $v)
  146. $a[$k] = trim($v);
  147. return $a;
  148. }
  149. }
  150. $o = new BxDolManageCmd();
  151. $o->main();
  152. /** @} */