baseline
[omp.pkp.sfu.ca.git] / lib / pkp / classes / cliTool / UpgradeTool.inc.php
blobbe634e4eb853f4c8026af8dff2333a4f143a6d05
1 <?php
3 /**
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.
9 * @class upgradeTool
10 * @ingroup tools
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) */
29 var $command;
30 /**
31 * Constructor.
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'))) {
38 $this->usage();
39 exit(1);
42 $this->command = $this->argv[0];
45 /**
46 * Print command usage information.
48 function usage() {
49 echo "Upgrade tool\n"
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";
59 /**
60 * Execute the specified command.
62 function execute() {
63 $command = $this->command;
64 $this->$command();
67 /**
68 * Perform version check against latest available version.
70 function check() {
71 $this->checkVersion(VersionCheck::getLatestVersion());
74 /**
75 * Print information about the latest available version.
77 function latest() {
78 $this->checkVersion(VersionCheck::getLatestVersion(), true);
81 /**
82 * Run upgrade script.
84 function upgrade() {
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);
98 if ($pretend) {
99 if (count($installer->getSQL()) > 0) {
100 printf("\nSQL\n");
101 printf("----------------------------------------\n");
102 foreach ($installer->getSQL() as $sql) {
103 printf("%s\n\n", $sql);
107 } else {
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() == '') {
112 $product = 'NULL';
114 $versionDao->disableVersion($product);
116 $newVersion =& $installer->getNewVersion();
117 printf("Successfully upgraded to version %s\n", $newVersion->getVersionString());
120 } else {
121 printf("ERROR: Upgrade failed: %s\n", $installer->getErrorString());
126 * Apply patch to update code to latest version.
128 function patch() {
129 $versionInfo = VersionCheck::getLatestVersion();
130 $check = $this->checkVersion($versionInfo);
132 if ($check < 0) {
133 $patch = VersionCheck::getPatch($versionInfo);
134 if (!isset($patch)) {
135 printf("No applicable patch available\n");
136 return;
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()) {
144 exit(0);
147 $out = fopen($outFile, 'wb');
148 if (!$out) {
149 printf("Failed to open %s for writing\n", $outFile);
150 exit(1);
153 $in = gzopen($patch, 'r');
154 if (!$in) {
155 printf("Failed to open %s for reading\n", $patch);
156 fclose($out);
157 exit(1);
160 printf('Downloading patch...');
162 while(($data = gzread($in, 4096)) !== '') {
163 printf('.');
164 fwrite($out, $data);
167 printf("done\n");
169 gzclose($in);
170 fclose($out);
172 $command = 'patch -p1 < ' . escapeshellarg($outFile);
173 printf("Apply patch: %s\n", $command);
175 if (!$this->promptContinue()) {
176 exit(0);
179 system($command, $ret);
180 if ($ret == 0) {
181 printf("Successfully applied patch for version %s\n", $versionInfo['release']);
182 } else {
183 printf("ERROR: Failed to apply patch\n");
189 * Download latest package/patch.
191 function download() {
192 $versionInfo = VersionCheck::getLatestVersion();
193 if (!$versionInfo) {
194 $application =& PKPApplication::getApplication();
195 printf("Failed to load version info from %s\n", $application->getVersionDescriptorUrl());
196 exit(1);
199 $type = isset($this->argv[1]) && $this->argv[1] == 'patch' ? 'patch' : 'package';
200 if ($type == 'package') {
201 $download = $versionInfo['package'];
202 } else {
203 $download = VersionCheck::getPatch($versionInfo);
205 if (!isset($download)) {
206 printf("No applicable download available\n");
207 return;
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()) {
215 exit(0);
218 $out = fopen($outFile, 'wb');
219 if (!$out) {
220 printf("Failed to open %s for writing\n", $outFile);
221 exit(1);
224 $in = fopen($download, 'rb');
225 if (!$in) {
226 printf("Failed to open %s for reading\n", $download);
227 fclose($out);
228 exit(1);
231 printf('Downloading file...');
233 while(($data = fread($in, 4096)) !== '') {
234 printf('.');
235 fwrite($out, $data);
238 printf("done\n");
240 fclose($in);
241 fclose($out);
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) {
250 if (!$versionInfo) {
251 $application =& PKPApplication::getApplication();
252 printf("Failed to load version info from %s\n", $application->getVersionDescriptorUrl());
253 exit(1);
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);
267 if (!$displayInfo) {
268 if ($compare2 < 0) {
269 printf("Database version is older than code version\n");
270 printf("Run \"{$this->scriptName} upgrade\" to update\n");
271 exit(0);
273 } else if($compare2 > 0) {
274 printf("Database version is newer than code version!\n");
275 exit(1);
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");
282 $displayInfo = true;
283 } else {
284 printf("Current version is newer than latest!\n");
285 exit(1);
289 if ($displayInfo) {
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');
298 return $compare1;
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);