install_cli.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. #!/usr/bin/env php
  2. <?php
  3. /**
  4. * StatusNet - the distributed open-source microblogging tool
  5. * Copyright (C) 2010, StatusNet, Inc.
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. * @category Installation
  21. * @package Installation
  22. *
  23. * @author Brion Vibber <brion@status.net>
  24. * @author Mikael Nordfeldth <mmn@hethane.se>
  25. * @license GNU Affero General Public License http://www.gnu.org/licenses/
  26. * @version 1.1.x
  27. * @link http://status.net
  28. */
  29. if (php_sapi_name() !== 'cli') {
  30. exit(1);
  31. }
  32. define('INSTALLDIR', dirname(dirname(__FILE__)));
  33. set_include_path(get_include_path() . PATH_SEPARATOR . INSTALLDIR . '/extlib');
  34. require_once INSTALLDIR . '/lib/installer.php';
  35. require_once 'Console/Getopt.php';
  36. class CliInstaller extends Installer
  37. {
  38. public $verbose = true;
  39. /**
  40. * Go for it!
  41. * @return boolean success
  42. */
  43. function main()
  44. {
  45. if ($this->prepare()) {
  46. if (!$this->checkPrereqs()) {
  47. return false;
  48. }
  49. return $this->handle();
  50. } else {
  51. $this->showHelp();
  52. return false;
  53. }
  54. }
  55. /**
  56. * Get our input parameters...
  57. * @return boolean success
  58. */
  59. function prepare()
  60. {
  61. $shortoptions = 'qvh';
  62. $longoptions = array('quiet', 'verbose', 'help', 'skip-config');
  63. $map = array(
  64. '-s' => 'server',
  65. '--server' => 'server',
  66. '-p' => 'path',
  67. '--path' => 'path',
  68. '--sitename' => 'sitename',
  69. '--fancy' => 'fancy',
  70. '--ssl' => 'ssl',
  71. '--dbtype' => 'dbtype',
  72. '--host' => 'host',
  73. '--database' => 'database',
  74. '--username' => 'username',
  75. '--password' => 'password',
  76. '--admin-nick' => 'adminNick',
  77. '--admin-pass' => 'adminPass',
  78. '--admin-email' => 'adminEmail',
  79. '--site-profile' => 'siteProfile'
  80. );
  81. foreach ($map as $arg => $target) {
  82. if (substr($arg, 0, 2) == '--') {
  83. $longoptions[] = substr($arg, 2) . '=';
  84. } else {
  85. $shortoptions .= substr($arg, 1) . ':';
  86. }
  87. }
  88. $parser = new Console_Getopt();
  89. $result = $parser->getopt($_SERVER['argv'], $shortoptions, $longoptions);
  90. if (PEAR::isError($result)) {
  91. $this->warning($result->getMessage());
  92. return false;
  93. }
  94. list($options, $args) = $result;
  95. // defaults
  96. $this->dbtype = 'mysql';
  97. $this->verbose = true;
  98. // ssl is defaulted in lib/installer.php
  99. foreach ($options as $option) {
  100. $arg = $option[0];
  101. if (isset($map[$arg])) {
  102. $var = $map[$arg];
  103. $this->$var = $option[1];
  104. if ($arg == '--fancy') {
  105. $this->$var = ($option[1] != 'false') && ($option[1] != 'no');
  106. }
  107. } else if ($arg == '--skip-config') {
  108. $this->skipConfig = true;
  109. } else if ($arg == 'q' || $arg == '--quiet') {
  110. $this->verbose = false;
  111. } else if ($arg == 'v' || $arg == '--verbose') {
  112. $this->verbose = true;
  113. } else if ($arg == 'h' || $arg == '--help') {
  114. // will go back to show help
  115. return false;
  116. }
  117. }
  118. $fail = false;
  119. if (empty($this->server)) {
  120. $this->updateStatus("You must specify a web server for the site.", true);
  121. // path is optional though
  122. $fail = true;
  123. }
  124. if (!$this->validateDb()) {
  125. $fail = true;
  126. }
  127. if (!$this->validateAdmin()) {
  128. $fail = true;
  129. }
  130. return !$fail;
  131. }
  132. function handle()
  133. {
  134. return $this->doInstall();
  135. }
  136. function showHelp()
  137. {
  138. echo <<<END_HELP
  139. install_cli.php - StatusNet command-line installer
  140. -s --server=<name> Use <name> as server name (required)
  141. -p --path=<path> Use <path> as path name
  142. --sitename User-friendly site name (required)
  143. --fancy Whether to use fancy URLs (default no)
  144. --ssl Server SSL enabled (default never),
  145. [never | always]
  146. --dbtype 'mysql' (default) or 'pgsql'
  147. --host Database hostname (required)
  148. --database Database/schema name (required)
  149. --username Database username (required)
  150. --password Database password (required)
  151. --admin-nick Administrator nickname (required)
  152. --admin-pass Initial password for admin user (required)
  153. --admin-email Initial email address for admin user
  154. --admin-updates 'yes' (default) or 'no', whether to subscribe
  155. admin to update@status.net (default yes)
  156. --site-profile site profile ['public', 'private' (default), 'community', 'singleuser']
  157. --skip-config Don't write a config.php -- use with caution,
  158. requires a global configuration file.
  159. General options:
  160. -q --quiet Quiet (little output)
  161. -v --verbose Verbose (lots of output)
  162. -h --help Show this message and quit.
  163. END_HELP;
  164. }
  165. function warning($message, $submessage='')
  166. {
  167. print $this->html2text($message) . "\n";
  168. if ($submessage != '') {
  169. print " " . $this->html2text($submessage) . "\n";
  170. }
  171. print "\n";
  172. }
  173. function updateStatus($status, $error=false)
  174. {
  175. if ($this->verbose || $error) {
  176. if ($error) {
  177. print "ERROR: ";
  178. }
  179. print $this->html2text($status);
  180. print "\n";
  181. }
  182. }
  183. private function html2text($html)
  184. {
  185. // break out any links for text legibility
  186. $breakout = preg_replace('/<a[^>+]\bhref="(.*)"[^>]*>(.*)<\/a>/',
  187. '\2 &lt;\1&gt;',
  188. $html);
  189. return html_entity_decode(strip_tags($breakout), ENT_QUOTES, 'UTF-8');
  190. }
  191. }
  192. $installer = new CliInstaller();
  193. $ok = $installer->main();
  194. exit($ok ? 0 : 1);