PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/scripts/install_cli.php

https://github.com/zh/statusnet
PHP | 217 lines | 159 code | 21 blank | 37 comment | 40 complexity | c863883f2689c3de0f50a86c25138468 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-3.0, GPL-2.0
  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. * @license GNU Affero General Public License http://www.gnu.org/licenses/
  25. * @version 0.9.x
  26. * @link http://status.net
  27. */
  28. if (php_sapi_name() !== 'cli') {
  29. exit(1);
  30. }
  31. define('INSTALLDIR', dirname(dirname(__FILE__)));
  32. set_include_path(get_include_path() . PATH_SEPARATOR . INSTALLDIR . '/extlib');
  33. require_once INSTALLDIR . '/lib/installer.php';
  34. require_once 'Console/Getopt.php';
  35. class CliInstaller extends Installer
  36. {
  37. public $verbose = true;
  38. /**
  39. * Go for it!
  40. * @return boolean success
  41. */
  42. function main()
  43. {
  44. if (!$this->checkPrereqs()) {
  45. return false;
  46. }
  47. if ($this->prepare()) {
  48. return $this->handle();
  49. } else {
  50. $this->showHelp();
  51. return false;
  52. }
  53. }
  54. /**
  55. * Get our input parameters...
  56. * @return boolean success
  57. */
  58. function prepare()
  59. {
  60. $shortoptions = 'qvh';
  61. $longoptions = array('quiet', 'verbose', 'help', 'skip-config');
  62. $map = array(
  63. '-s' => 'server',
  64. '--server' => 'server',
  65. '-p' => 'path',
  66. '--path' => 'path',
  67. '--sitename' => 'sitename',
  68. '--fancy' => 'fancy',
  69. '--dbtype' => 'dbtype',
  70. '--host' => 'host',
  71. '--database' => 'database',
  72. '--username' => 'username',
  73. '--password' => 'password',
  74. '--admin-nick' => 'adminNick',
  75. '--admin-pass' => 'adminPass',
  76. '--admin-email' => 'adminEmail',
  77. '--admin-updates' => 'adminUpdates'
  78. );
  79. foreach ($map as $arg => $target) {
  80. if (substr($arg, 0, 2) == '--') {
  81. $longoptions[] = substr($arg, 2) . '=';
  82. } else {
  83. $shortoptions .= substr($arg, 1) . ':';
  84. }
  85. }
  86. $parser = new Console_Getopt();
  87. $result = $parser->getopt($_SERVER['argv'], $shortoptions, $longoptions);
  88. if (PEAR::isError($result)) {
  89. $this->warning($result->getMessage());
  90. return false;
  91. }
  92. list($options, $args) = $result;
  93. // defaults
  94. $this->dbtype = 'mysql';
  95. $this->adminUpdates = true;
  96. $this->verbose = true;
  97. foreach ($options as $option) {
  98. $arg = $option[0];
  99. if (isset($map[$arg])) {
  100. $var = $map[$arg];
  101. $this->$var = $option[1];
  102. if ($var == 'adminUpdates' || $arg == '--fancy') {
  103. $this->$var = ($option[1] != 'false') && ($option[1] != 'no');
  104. }
  105. } else if ($arg == '--skip-config') {
  106. $this->skipConfig = true;
  107. } else if ($arg == 'q' || $arg == '--quiet') {
  108. $this->verbose = false;
  109. } else if ($arg == 'v' || $arg == '--verbose') {
  110. $this->verbose = true;
  111. } else if ($arg == 'h' || $arg == '--help') {
  112. // will go back to show help
  113. return false;
  114. }
  115. }
  116. $fail = false;
  117. if (empty($this->server)) {
  118. $this->updateStatus("You must specify a web server for the site.", true);
  119. // path is optional though
  120. $fail = true;
  121. }
  122. if (!$this->validateDb()) {
  123. $fail = true;
  124. }
  125. if (!$this->validateAdmin()) {
  126. $fail = true;
  127. }
  128. return !$fail;
  129. }
  130. function handle()
  131. {
  132. return $this->doInstall();
  133. }
  134. function showHelp()
  135. {
  136. echo <<<END_HELP
  137. install_cli.php - StatusNet command-line installer
  138. -s --server=<name> Use <name> as server name (required)
  139. -p --path=<path> Use <path> as path name
  140. --sitename User-friendly site name (required)
  141. --fancy Whether to use fancy URLs (default no)
  142. --dbtype 'mysql' (default) or 'pgsql'
  143. --host Database hostname (required)
  144. --database Database/schema name (required)
  145. --username Database username (required)
  146. --password Database password (required)
  147. --admin-nick Administrator nickname (required)
  148. --admin-pass Initial password for admin user (required)
  149. --admin-email Initial email address for admin user
  150. --admin-updates 'yes' (default) or 'no', whether to subscribe
  151. admin to update@status.net (default yes)
  152. --skip-config Don't write a config.php -- use with caution,
  153. requires a global configuration file.
  154. General options:
  155. -q --quiet Quiet (little output)
  156. -v --verbose Verbose (lots of output)
  157. -h --help Show this message and quit.
  158. END_HELP;
  159. }
  160. function warning($message, $submessage='')
  161. {
  162. print $this->html2text($message) . "\n";
  163. if ($submessage != '') {
  164. print " " . $this->html2text($submessage) . "\n";
  165. }
  166. print "\n";
  167. }
  168. function updateStatus($status, $error=false)
  169. {
  170. if ($this->verbose || $error) {
  171. if ($error) {
  172. print "ERROR: ";
  173. }
  174. print $this->html2text($status);
  175. print "\n";
  176. }
  177. }
  178. private function html2text($html)
  179. {
  180. // break out any links for text legibility
  181. $breakout = preg_replace('/<a[^>+]\bhref="(.*)"[^>]*>(.*)<\/a>/',
  182. '\2 &lt;\1&gt;',
  183. $html);
  184. return html_entity_decode(strip_tags($breakout), ENT_QUOTES, 'UTF-8');
  185. }
  186. }
  187. $installer = new CliInstaller();
  188. $ok = $installer->main();
  189. exit($ok ? 0 : 1);