4 * @file classes/cliTool/UpgradeTool.php
6 * Copyright (c) 2000-2009 John Willinsky
7 * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
12 * @brief CLI tool for upgrading OJS.
14 * Note: Some functions require fopen wrappers to be enabled.
17 // $Id: UpgradeTool.inc.php,v 1.5 2009/05/13 00:13:20 asmecher Exp $
20 define('RUNNING_UPGRADE', 1);
22 import('install.Upgrade');
23 import('site.Version');
24 import('site.VersionCheck');
26 class UpgradeTool
extends CommandLineTool
{
28 /** @var string command to execute (check|upgrade|patch|download) */
32 * @param $argv array command-line arguments
34 function upgradeTool($argv = array()) {
35 parent
::CommandLineTool($argv);
37 if (!isset($this->argv
[0]) ||
!in_array($this->argv
[0], array('check', 'latest', 'upgrade', 'patch', 'download'))) {
42 $this->command
= $this->argv
[0];
46 * Print command usage information.
50 . "Usage: {$this->scriptName} command\n"
51 . "Supported commands:\n"
52 . " check perform version check\n"
53 . " latest display latest version info\n"
54 //. " upgrade [pretend] execute upgrade script\n"
55 . " patch download and apply patch for latest version\n"
56 . " download [package|patch] download latest version (does not unpack/install)\n";
60 * Execute the specified command.
63 $command = $this->command
;
68 * Perform version check against latest available version.
71 $this->checkVersion(VersionCheck
::getLatestVersion());
75 * Print information about the latest available version.
78 $this->checkVersion(VersionCheck
::getLatestVersion(), true);
85 $pretend = false; // isset($this->argv[1]) && $this->argv[1] == 'pretend';
86 $installer = new Upgrade(array('manualInstall' => $pretend));
87 $installer->setLogger($this);
89 if ($installer->execute()) {
90 if (count($installer->getNotes()) > 0) {
91 printf("\nRelease Notes\n");
92 printf("----------------------------------------\n");
93 foreach ($installer->getNotes() as $note) {
94 printf("%s\n\n", $note);
99 if (count($installer->getSQL()) > 0) {
101 printf("----------------------------------------\n");
102 foreach ($installer->getSQL() as $sql) {
103 printf("%s\n\n", $sql);
108 //get current version's name, check if its null, otherwise disable that version
109 $versionDao =& DAORegistry
::getDAO('VersionDAO');
110 $currentVersion =& $installer->getCurrentVersion();
111 if ($product = $currentVersion->getProduct() == '') {
114 $versionDao->disableVersion($product);
116 $newVersion =& $installer->getNewVersion();
117 printf("Successfully upgraded to version %s\n", $newVersion->getVersionString());
121 printf("ERROR: Upgrade failed: %s\n", $installer->getErrorString());
126 * Apply patch to update code to latest version.
129 $versionInfo = VersionCheck
::getLatestVersion();
130 $check = $this->checkVersion($versionInfo);
133 $patch = VersionCheck
::getPatch($versionInfo);
134 if (!isset($patch)) {
135 printf("No applicable patch available\n");
139 $outFile = $versionInfo['application'] . '-' . $versionInfo['release'] . '.patch';
140 printf("Download patch: %s\n", $patch);
141 printf("Patch will be saved to: %s\n", $outFile);
143 if (!$this->promptContinue()) {
147 $out = fopen($outFile, 'wb');
149 printf("Failed to open %s for writing\n", $outFile);
153 $in = gzopen($patch, 'r');
155 printf("Failed to open %s for reading\n", $patch);
160 printf('Downloading patch...');
162 while(($data = gzread($in, 4096)) !== '') {
172 $command = 'patch -p1 < ' . escapeshellarg($outFile);
173 printf("Apply patch: %s\n", $command);
175 if (!$this->promptContinue()) {
179 system($command, $ret);
181 printf("Successfully applied patch for version %s\n", $versionInfo['release']);
183 printf("ERROR: Failed to apply patch\n");
189 * Download latest package/patch.
191 function download() {
192 $versionInfo = VersionCheck
::getLatestVersion();
194 $application =& PKPApplication
::getApplication();
195 printf("Failed to load version info from %s\n", $application->getVersionDescriptorUrl());
199 $type = isset($this->argv
[1]) && $this->argv
[1] == 'patch' ?
'patch' : 'package';
200 if ($type == 'package') {
201 $download = $versionInfo['package'];
203 $download = VersionCheck
::getPatch($versionInfo);
205 if (!isset($download)) {
206 printf("No applicable download available\n");
209 $outFile = basename($download);
211 printf("Download %s: %s\n", $type, $download);
212 printf("File will be saved to: %s\n", $outFile);
214 if (!$this->promptContinue()) {
218 $out = fopen($outFile, 'wb');
220 printf("Failed to open %s for writing\n", $outFile);
224 $in = fopen($download, 'rb');
226 printf("Failed to open %s for reading\n", $download);
231 printf('Downloading file...');
233 while(($data = fread($in, 4096)) !== '') {
245 * Perform version check.
246 * @param $versionInfo array latest version info
247 * @param $displayInfo boolean just display info, don't perform check
249 function checkVersion($versionInfo, $displayInfo = false) {
251 $application =& PKPApplication
::getApplication();
252 printf("Failed to load version info from %s\n", $application->getVersionDescriptorUrl());
256 $dbVersion = VersionCheck
::getCurrentDBVersion();
257 $codeVersion = VersionCheck
::getCurrentCodeVersion();
258 $latestVersion = $versionInfo['version'];
260 printf("Code version: %s\n", $codeVersion->getVersionString());
261 printf("Database version: %s\n", $dbVersion->getVersionString());
262 printf("Latest version: %s\n", $latestVersion->getVersionString());
264 $compare1 = $codeVersion->compare($latestVersion);
265 $compare2 = $dbVersion->compare($codeVersion);
269 printf("Database version is older than code version\n");
270 printf("Run \"{$this->scriptName} upgrade\" to update\n");
273 } else if($compare2 > 0) {
274 printf("Database version is newer than code version!\n");
277 } else if ($compare1 == 0) {
278 printf("Your system is up-to-date\n");
280 } else if($compare1 < 0) {
281 printf("A newer version is available:\n");
284 printf("Current version is newer than latest!\n");
290 $patch = VersionCheck
::getPatch($versionInfo, $codeVersion);
291 printf(" tag: %s\n", $versionInfo['tag']);
292 printf(" date: %s\n", $versionInfo['date']);
293 printf(" info: %s\n", $versionInfo['info']);
294 printf(" package: %s\n", $versionInfo['package']);
295 printf(" patch: %s\n", isset($patch) ?
$patch : 'N/A');
302 * Prompt user for yes/no input (default no).
303 * @param $prompt string
305 function promptContinue($prompt = "Continue?") {
306 printf("%s [y/N] ", $prompt);
307 $continue = fread(STDIN
, 255);
308 return (strtolower(substr(trim($continue), 0, 1)) == 'y');
312 * Log install message to stdout.
313 * @param $message string
315 function log($message) {
316 printf("[%s]\n", $message);