PageRenderTime 59ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/cliTool/UpgradeTool.inc.php

https://github.com/michaeljoyce/pkp-lib
PHP | 313 lines | 204 code | 55 blank | 54 comment | 46 complexity | 5d3902631ae64defd29a3a100862dae2 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause
  1. <?php
  2. /**
  3. * @file classes/cliTool/UpgradeTool.php
  4. *
  5. * Copyright (c) 2000-2010 John Willinsky
  6. * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
  7. *
  8. * @class upgradeTool
  9. * @ingroup tools
  10. *
  11. * @brief CLI tool for upgrading OJS.
  12. *
  13. * Note: Some functions require fopen wrappers to be enabled.
  14. */
  15. // $Id$
  16. define('RUNNING_UPGRADE', 1);
  17. import('install.Upgrade');
  18. import('site.Version');
  19. import('site.VersionCheck');
  20. class UpgradeTool extends CommandLineTool {
  21. /** @var string command to execute (check|upgrade|patch|download) */
  22. var $command;
  23. /**
  24. * Constructor.
  25. * @param $argv array command-line arguments
  26. */
  27. function upgradeTool($argv = array()) {
  28. parent::CommandLineTool($argv);
  29. if (!isset($this->argv[0]) || !in_array($this->argv[0], array('check', 'latest', 'upgrade', 'patch', 'download'))) {
  30. $this->usage();
  31. exit(1);
  32. }
  33. $this->command = $this->argv[0];
  34. }
  35. /**
  36. * Print command usage information.
  37. */
  38. function usage() {
  39. echo "Upgrade tool\n"
  40. . "Usage: {$this->scriptName} command\n"
  41. . "Supported commands:\n"
  42. . " check perform version check\n"
  43. . " latest display latest version info\n"
  44. //. " upgrade [pretend] execute upgrade script\n"
  45. . " patch download and apply patch for latest version\n"
  46. . " download [package|patch] download latest version (does not unpack/install)\n";
  47. }
  48. /**
  49. * Execute the specified command.
  50. */
  51. function execute() {
  52. $command = $this->command;
  53. $this->$command();
  54. }
  55. /**
  56. * Perform version check against latest available version.
  57. */
  58. function check() {
  59. $this->checkVersion(VersionCheck::getLatestVersion());
  60. }
  61. /**
  62. * Print information about the latest available version.
  63. */
  64. function latest() {
  65. $this->checkVersion(VersionCheck::getLatestVersion(), true);
  66. }
  67. /**
  68. * Run upgrade script.
  69. */
  70. function upgrade() {
  71. $pretend = false; // isset($this->argv[1]) && $this->argv[1] == 'pretend';
  72. $installer = new Upgrade(array('manualInstall' => $pretend));
  73. $installer->setLogger($this);
  74. if ($installer->execute()) {
  75. if (count($installer->getNotes()) > 0) {
  76. printf("\nRelease Notes\n");
  77. printf("----------------------------------------\n");
  78. foreach ($installer->getNotes() as $note) {
  79. printf("%s\n\n", $note);
  80. }
  81. }
  82. if ($pretend) {
  83. if (count($installer->getSQL()) > 0) {
  84. printf("\nSQL\n");
  85. printf("----------------------------------------\n");
  86. foreach ($installer->getSQL() as $sql) {
  87. printf("%s\n\n", $sql);
  88. }
  89. }
  90. } else {
  91. $newVersion =& $installer->getNewVersion();
  92. printf("Successfully upgraded to version %s\n", $newVersion->getVersionString());
  93. }
  94. } else {
  95. printf("ERROR: Upgrade failed: %s\n", $installer->getErrorString());
  96. }
  97. }
  98. /**
  99. * Apply patch to update code to latest version.
  100. */
  101. function patch() {
  102. $versionInfo = VersionCheck::getLatestVersion();
  103. $check = $this->checkVersion($versionInfo);
  104. if ($check < 0) {
  105. $patch = VersionCheck::getPatch($versionInfo);
  106. if (!isset($patch)) {
  107. printf("No applicable patch available\n");
  108. return;
  109. }
  110. $outFile = $versionInfo['application'] . '-' . $versionInfo['release'] . '.patch';
  111. printf("Download patch: %s\n", $patch);
  112. printf("Patch will be saved to: %s\n", $outFile);
  113. if (!$this->promptContinue()) {
  114. exit(0);
  115. }
  116. $out = fopen($outFile, 'wb');
  117. if (!$out) {
  118. printf("Failed to open %s for writing\n", $outFile);
  119. exit(1);
  120. }
  121. $in = gzopen($patch, 'r');
  122. if (!$in) {
  123. printf("Failed to open %s for reading\n", $patch);
  124. fclose($out);
  125. exit(1);
  126. }
  127. printf('Downloading patch...');
  128. while(($data = gzread($in, 4096)) !== '') {
  129. printf('.');
  130. fwrite($out, $data);
  131. }
  132. printf("done\n");
  133. gzclose($in);
  134. fclose($out);
  135. $command = 'patch -p1 < ' . escapeshellarg($outFile);
  136. printf("Apply patch: %s\n", $command);
  137. if (!$this->promptContinue()) {
  138. exit(0);
  139. }
  140. system($command, $ret);
  141. if ($ret == 0) {
  142. printf("Successfully applied patch for version %s\n", $versionInfo['release']);
  143. } else {
  144. printf("ERROR: Failed to apply patch\n");
  145. }
  146. }
  147. }
  148. /**
  149. * Download latest package/patch.
  150. */
  151. function download() {
  152. $versionInfo = VersionCheck::getLatestVersion();
  153. if (!$versionInfo) {
  154. $application =& PKPApplication::getApplication();
  155. printf("Failed to load version info from %s\n", $application->getVersionDescriptorUrl());
  156. exit(1);
  157. }
  158. $type = isset($this->argv[1]) && $this->argv[1] == 'patch' ? 'patch' : 'package';
  159. if ($type == 'package') {
  160. $download = $versionInfo['package'];
  161. } else {
  162. $download = VersionCheck::getPatch($versionInfo);
  163. }
  164. if (!isset($download)) {
  165. printf("No applicable download available\n");
  166. return;
  167. }
  168. $outFile = basename($download);
  169. printf("Download %s: %s\n", $type, $download);
  170. printf("File will be saved to: %s\n", $outFile);
  171. if (!$this->promptContinue()) {
  172. exit(0);
  173. }
  174. $out = fopen($outFile, 'wb');
  175. if (!$out) {
  176. printf("Failed to open %s for writing\n", $outFile);
  177. exit(1);
  178. }
  179. $in = fopen($download, 'rb');
  180. if (!$in) {
  181. printf("Failed to open %s for reading\n", $download);
  182. fclose($out);
  183. exit(1);
  184. }
  185. printf('Downloading file...');
  186. while(($data = fread($in, 4096)) !== '') {
  187. printf('.');
  188. fwrite($out, $data);
  189. }
  190. printf("done\n");
  191. fclose($in);
  192. fclose($out);
  193. }
  194. /**
  195. * Perform version check.
  196. * @param $versionInfo array latest version info
  197. * @param $displayInfo boolean just display info, don't perform check
  198. */
  199. function checkVersion($versionInfo, $displayInfo = false) {
  200. if (!$versionInfo) {
  201. $application =& PKPApplication::getApplication();
  202. printf("Failed to load version info from %s\n", $application->getVersionDescriptorUrl());
  203. exit(1);
  204. }
  205. $dbVersion = VersionCheck::getCurrentDBVersion();
  206. $codeVersion = VersionCheck::getCurrentCodeVersion();
  207. $latestVersion = $versionInfo['version'];
  208. printf("Code version: %s\n", $codeVersion->getVersionString());
  209. printf("Database version: %s\n", $dbVersion->getVersionString());
  210. printf("Latest version: %s\n", $latestVersion->getVersionString());
  211. $compare1 = $codeVersion->compare($latestVersion);
  212. $compare2 = $dbVersion->compare($codeVersion);
  213. if (!$displayInfo) {
  214. if ($compare2 < 0) {
  215. printf("Database version is older than code version\n");
  216. printf("Run \"{$this->scriptName} upgrade\" to update\n");
  217. exit(0);
  218. } else if($compare2 > 0) {
  219. printf("Database version is newer than code version!\n");
  220. exit(1);
  221. } else if ($compare1 == 0) {
  222. printf("Your system is up-to-date\n");
  223. } else if($compare1 < 0) {
  224. printf("A newer version is available:\n");
  225. $displayInfo = true;
  226. } else {
  227. printf("Current version is newer than latest!\n");
  228. exit(1);
  229. }
  230. }
  231. if ($displayInfo) {
  232. $patch = VersionCheck::getPatch($versionInfo, $codeVersion);
  233. printf(" tag: %s\n", $versionInfo['tag']);
  234. printf(" date: %s\n", $versionInfo['date']);
  235. printf(" info: %s\n", $versionInfo['info']);
  236. printf(" package: %s\n", $versionInfo['package']);
  237. printf(" patch: %s\n", isset($patch) ? $patch : 'N/A');
  238. }
  239. return $compare1;
  240. }
  241. /**
  242. * Prompt user for yes/no input (default no).
  243. * @param $prompt string
  244. */
  245. function promptContinue($prompt = "Continue?") {
  246. printf("%s [y/N] ", $prompt);
  247. $continue = fread(STDIN, 255);
  248. return (strtolower(substr(trim($continue), 0, 1)) == 'y');
  249. }
  250. /**
  251. * Log install message to stdout.
  252. * @param $message string
  253. */
  254. function log($message) {
  255. printf("[%s]\n", $message);
  256. }
  257. }
  258. ?>