PageRenderTime 38ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/lib-uoguelph-ca/ocs
PHP | 301 lines | 193 code | 54 blank | 54 comment | 43 complexity | 314b5a6186831e5513b6c25a7a8dc93e MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * @file classes/cliTool/UpgradeTool.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 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. $installer = new Upgrade(array());
  72. $installer->setLogger($this);
  73. if ($installer->execute()) {
  74. if (count($installer->getNotes()) > 0) {
  75. printf("\nRelease Notes\n");
  76. printf("----------------------------------------\n");
  77. foreach ($installer->getNotes() as $note) {
  78. printf("%s\n\n", $note);
  79. }
  80. }
  81. $newVersion =& $installer->getNewVersion();
  82. printf("Successfully upgraded to version %s\n", $newVersion->getVersionString());
  83. } else {
  84. printf("ERROR: Upgrade failed: %s\n", $installer->getErrorString());
  85. }
  86. }
  87. /**
  88. * Apply patch to update code to latest version.
  89. */
  90. function patch() {
  91. $versionInfo = VersionCheck::getLatestVersion();
  92. $check = $this->checkVersion($versionInfo);
  93. if ($check < 0) {
  94. $patch = VersionCheck::getPatch($versionInfo);
  95. if (!isset($patch)) {
  96. printf("No applicable patch available\n");
  97. return;
  98. }
  99. $outFile = $versionInfo['application'] . '-' . $versionInfo['release'] . '.patch';
  100. printf("Download patch: %s\n", $patch);
  101. printf("Patch will be saved to: %s\n", $outFile);
  102. if (!$this->promptContinue()) {
  103. exit(0);
  104. }
  105. $out = fopen($outFile, 'wb');
  106. if (!$out) {
  107. printf("Failed to open %s for writing\n", $outFile);
  108. exit(1);
  109. }
  110. $in = gzopen($patch, 'r');
  111. if (!$in) {
  112. printf("Failed to open %s for reading\n", $patch);
  113. fclose($out);
  114. exit(1);
  115. }
  116. printf('Downloading patch...');
  117. while(($data = gzread($in, 4096)) !== '') {
  118. printf('.');
  119. fwrite($out, $data);
  120. }
  121. printf("done\n");
  122. gzclose($in);
  123. fclose($out);
  124. $command = 'patch -p1 < ' . escapeshellarg($outFile);
  125. printf("Apply patch: %s\n", $command);
  126. if (!$this->promptContinue()) {
  127. exit(0);
  128. }
  129. system($command, $ret);
  130. if ($ret == 0) {
  131. printf("Successfully applied patch for version %s\n", $versionInfo['release']);
  132. } else {
  133. printf("ERROR: Failed to apply patch\n");
  134. }
  135. }
  136. }
  137. /**
  138. * Download latest package/patch.
  139. */
  140. function download() {
  141. $versionInfo = VersionCheck::getLatestVersion();
  142. if (!$versionInfo) {
  143. $application =& PKPApplication::getApplication();
  144. printf("Failed to load version info from %s\n", $application->getVersionDescriptorUrl());
  145. exit(1);
  146. }
  147. $type = isset($this->argv[1]) && $this->argv[1] == 'patch' ? 'patch' : 'package';
  148. if ($type == 'package') {
  149. $download = $versionInfo['package'];
  150. } else {
  151. $download = VersionCheck::getPatch($versionInfo);
  152. }
  153. if (!isset($download)) {
  154. printf("No applicable download available\n");
  155. return;
  156. }
  157. $outFile = basename($download);
  158. printf("Download %s: %s\n", $type, $download);
  159. printf("File will be saved to: %s\n", $outFile);
  160. if (!$this->promptContinue()) {
  161. exit(0);
  162. }
  163. $out = fopen($outFile, 'wb');
  164. if (!$out) {
  165. printf("Failed to open %s for writing\n", $outFile);
  166. exit(1);
  167. }
  168. $in = fopen($download, 'rb');
  169. if (!$in) {
  170. printf("Failed to open %s for reading\n", $download);
  171. fclose($out);
  172. exit(1);
  173. }
  174. printf('Downloading file...');
  175. while(($data = fread($in, 4096)) !== '') {
  176. printf('.');
  177. fwrite($out, $data);
  178. }
  179. printf("done\n");
  180. fclose($in);
  181. fclose($out);
  182. }
  183. /**
  184. * Perform version check.
  185. * @param $versionInfo array latest version info
  186. * @param $displayInfo boolean just display info, don't perform check
  187. */
  188. function checkVersion($versionInfo, $displayInfo = false) {
  189. if (!$versionInfo) {
  190. $application =& PKPApplication::getApplication();
  191. printf("Failed to load version info from %s\n", $application->getVersionDescriptorUrl());
  192. exit(1);
  193. }
  194. $dbVersion = VersionCheck::getCurrentDBVersion();
  195. $codeVersion = VersionCheck::getCurrentCodeVersion();
  196. $latestVersion = $versionInfo['version'];
  197. printf("Code version: %s\n", $codeVersion->getVersionString());
  198. printf("Database version: %s\n", $dbVersion->getVersionString());
  199. printf("Latest version: %s\n", $latestVersion->getVersionString());
  200. $compare1 = $codeVersion->compare($latestVersion);
  201. $compare2 = $dbVersion->compare($codeVersion);
  202. if (!$displayInfo) {
  203. if ($compare2 < 0) {
  204. printf("Database version is older than code version\n");
  205. printf("Run \"{$this->scriptName} upgrade\" to update\n");
  206. exit(0);
  207. } else if($compare2 > 0) {
  208. printf("Database version is newer than code version!\n");
  209. exit(1);
  210. } else if ($compare1 == 0) {
  211. printf("Your system is up-to-date\n");
  212. } else if($compare1 < 0) {
  213. printf("A newer version is available:\n");
  214. $displayInfo = true;
  215. } else {
  216. printf("Current version is newer than latest!\n");
  217. exit(1);
  218. }
  219. }
  220. if ($displayInfo) {
  221. $patch = VersionCheck::getPatch($versionInfo, $codeVersion);
  222. printf(" tag: %s\n", $versionInfo['tag']);
  223. printf(" date: %s\n", $versionInfo['date']);
  224. printf(" info: %s\n", $versionInfo['info']);
  225. printf(" package: %s\n", $versionInfo['package']);
  226. printf(" patch: %s\n", isset($patch) ? $patch : 'N/A');
  227. }
  228. return $compare1;
  229. }
  230. /**
  231. * Prompt user for yes/no input (default no).
  232. * @param $prompt string
  233. */
  234. function promptContinue($prompt = "Continue?") {
  235. printf("%s [y/N] ", $prompt);
  236. $continue = fread(STDIN, 255);
  237. return (strtolower(substr(trim($continue), 0, 1)) == 'y');
  238. }
  239. /**
  240. * Log install message to stdout.
  241. * @param $message string
  242. */
  243. function log($message) {
  244. printf("[%s]\n", $message);
  245. }
  246. }
  247. ?>