PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/pkp/classes/cliTool/InstallTool.inc.php

https://github.com/lib-uoguelph-ca/ocs
PHP | 265 lines | 161 code | 36 blank | 68 comment | 35 complexity | d522d7c8f2e94449b9d40cab8a1fa4aa MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * @file classes/cliTool/InstallTool.php
  4. *
  5. * Copyright (c) 2000-2012 John Willinsky
  6. * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
  7. *
  8. * @class installTool
  9. * @ingroup tools
  10. *
  11. * @brief CLI tool for installing a PKP app.
  12. */
  13. // $Id$
  14. import('install.Install');
  15. import('install.form.InstallForm');
  16. import('site.Version');
  17. import('site.VersionCheck');
  18. class InstallTool extends CommandLineTool {
  19. /** @var $params array installation parameters */
  20. var $params;
  21. /**
  22. * Constructor.
  23. * @param $argv array command-line arguments
  24. */
  25. function InstallTool($argv = array()) {
  26. parent::CommandLineTool($argv);
  27. }
  28. /**
  29. * Print command usage information.
  30. */
  31. function usage() {
  32. echo "Install tool\n"
  33. . "Usage: {$this->scriptName}\n";
  34. }
  35. /**
  36. * Execute the script.
  37. */
  38. function execute() {
  39. if ($this->readParams()) {
  40. $this->install();
  41. }
  42. }
  43. /**
  44. * Perform installation.
  45. */
  46. function install() {
  47. $installer = new Install($this->params);
  48. $installer->setLogger($this);
  49. if ($installer->execute()) {
  50. if (count($installer->getNotes()) > 0) {
  51. printf("\nRelease Notes\n");
  52. printf("----------------------------------------\n");
  53. foreach ($installer->getNotes() as $note) {
  54. printf("%s\n\n", $note);
  55. }
  56. }
  57. if (!$installer->wroteConfig()) {
  58. printf("\nNew config.inc.php:\n");
  59. printf("----------------------------------------\n");
  60. echo $installer->getConfigContents();
  61. printf("----------------------------------------\n");
  62. }
  63. $newVersion =& $installer->getNewVersion();
  64. printf("Successfully installed version %s\n", $newVersion->getVersionString());
  65. } else {
  66. printf("ERROR: Installation failed: %s\n", $installer->getErrorString());
  67. }
  68. }
  69. /**
  70. * Read installation parameters from stdin.
  71. * FIXME: May want to implement an abstract "CLIForm" class handling input/validation.
  72. * FIXME: Use readline if available?
  73. */
  74. function readParams() {
  75. if (checkPhpVersion('5.0.0')) { // WARNING: This form needs $this in constructor
  76. $installForm = new InstallForm();
  77. } else {
  78. $installForm =& new InstallForm();
  79. }
  80. // Locale Settings
  81. $this->printTitle('installer.localeSettings');
  82. $this->readParamOptions('locale', 'locale.primary', $installForm->supportedLocales, 'en_US');
  83. $this->readParamOptions('additionalLocales', 'installer.additionalLocales', $installForm->supportedLocales, '', true);
  84. $this->readParamOptions('clientCharset', 'installer.clientCharset', $installForm->supportedClientCharsets, 'utf-8');
  85. $this->readParamOptions('connectionCharset', 'installer.connectionCharset', $installForm->supportedConnectionCharsets, '');
  86. $this->readParamOptions('databaseCharset', 'installer.databaseCharset', $installForm->supportedDatabaseCharsets, '');
  87. // File Settings
  88. $this->printTitle('installer.fileSettings');
  89. $this->readParam('filesDir', 'installer.filesDir');
  90. $this->readParamBoolean('skipFilesDir', 'installer.skipFilesDir');
  91. // Security Settings
  92. $this->printTitle('installer.securitySettings');
  93. $this->readParamOptions('encryption', 'installer.encryption', $installForm->supportedEncryptionAlgorithms, 'md5');
  94. // Administrator Account
  95. $this->printTitle('installer.administratorAccount');
  96. $this->readParam('adminUsername', 'user.username');
  97. @`/bin/stty -echo`;
  98. $this->readParam('adminPassword', 'user.password');
  99. printf("\n");
  100. do {
  101. $this->readParam('adminPassword2', 'user.account.repeatPassword');
  102. printf("\n");
  103. } while ($this->params['adminPassword'] != $this->params['adminPassword2']);
  104. @`/bin/stty echo`;
  105. $this->readParam('adminEmail', 'user.email');
  106. // Database Settings
  107. $this->printTitle('installer.databaseSettings');
  108. $this->readParamOptions('databaseDriver', 'installer.databaseDriver', $installForm->checkDBDrivers());
  109. $this->readParam('databaseHost', 'installer.databaseHost', '');
  110. $this->readParam('databaseUsername', 'installer.databaseUsername', '');
  111. $this->readParam('databasePassword', 'installer.databasePassword', '');
  112. $this->readParam('databaseName', 'installer.databaseName');
  113. $this->readParamBoolean('createDatabase', 'installer.createDatabase', 'Y');
  114. // Miscellaneous Settings
  115. $this->printTitle('installer.miscSettings');
  116. $this->readParam('oaiRepositoryId', 'installer.oaiRepositoryId');
  117. printf("\n*** ");
  118. }
  119. /**
  120. * Print input section title.
  121. * @param $title string
  122. */
  123. function printTitle($title) {
  124. printf("\n%s\n%s\n%s\n", str_repeat('-', 80), __($title), str_repeat('-', 80));
  125. }
  126. /**
  127. * Read a line of user input.
  128. * @return string
  129. */
  130. function readInput() {
  131. $value = trim(fgets(STDIN));
  132. if ($value === false || feof(STDIN)) {
  133. printf("\n");
  134. exit(0);
  135. }
  136. return $value;
  137. }
  138. /**
  139. * Read a string parameter.
  140. * @param $name string
  141. * @param $prompt string
  142. * @param $defaultValue string
  143. */
  144. function readParam($name, $prompt, $defaultValue = null) {
  145. do {
  146. if (isset($defaultValue)) {
  147. printf("%s (%s): ", __($prompt), $defaultValue !== '' ? $defaultValue : __('common.none'));
  148. } else {
  149. printf("%s: ", __($prompt));
  150. }
  151. $value = $this->readInput();
  152. if ($value === '' && isset($defaultValue)) {
  153. $value = $defaultValue;
  154. }
  155. } while ($value === '' && $defaultValue !== '');
  156. $this->params[$name] = $value;
  157. }
  158. /**
  159. * Prompt user for yes/no input.
  160. * @param $name string
  161. * @param $prompt string
  162. * @param $default string default value, 'Y' or 'N'
  163. */
  164. function readParamBoolean($name, $prompt, $default = 'N') {
  165. if ($default == 'N') {
  166. printf("%s [y/N] ", __($prompt));
  167. $value = $this->readInput();
  168. $this->params[$name] = (int)(strtolower(substr(trim($value), 0, 1)) == 'y');
  169. } else {
  170. printf("%s [Y/n] ", __($prompt));
  171. $value = $this->readInput();
  172. $this->params[$name] = (int)(strtolower(substr(trim($value), 0, 1)) != 'n');
  173. }
  174. }
  175. /**
  176. * Read a parameter from a set of options.
  177. * @param $name string
  178. * @param $prompt string
  179. * @param $options array
  180. * @param $defaultOption string
  181. */
  182. function readParamOptions($name, $prompt, $options, $defaultValue = null, $allowMultiple = false) {
  183. do {
  184. printf("%s\n", __($prompt));
  185. foreach ($options as $k => $v) {
  186. printf(" %-10s %s\n", '[' . $k . ']', $v);
  187. }
  188. if ($allowMultiple) {
  189. printf(" (%s)\n", __('installer.form.separateMultiple'));
  190. }
  191. if (isset($defaultValue)) {
  192. printf("%s (%s): ", __('common.select'), $defaultValue !== '' ? $defaultValue : __('common.none'));
  193. } else {
  194. printf("%s: ", __('common.select'));
  195. }
  196. $value = $this->readInput();
  197. if ($value === '' && isset($defaultValue)) {
  198. $value = $defaultValue;
  199. }
  200. $values = array();
  201. if ($value !== '') {
  202. if ($allowMultiple) {
  203. $values = ($value === '' ? array() : preg_split('/\s*,\s*/', $value));
  204. } else {
  205. $values = array($value);
  206. }
  207. foreach ($values as $k) {
  208. if (!isset($options[$k])) {
  209. $value = '';
  210. break;
  211. }
  212. }
  213. }
  214. } while ($value === '' && $defaultValue !== '');
  215. if ($allowMultiple) {
  216. $this->params[$name] = $values;
  217. } else {
  218. $this->params[$name] = $value;
  219. }
  220. }
  221. /**
  222. * Log install message to stdout.
  223. * @param $message string
  224. */
  225. function log($message) {
  226. printf("[%s]\n", $message);
  227. }
  228. }
  229. ?>