PageRenderTime 27ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/cliTool/InstallTool.inc.php

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