00001 <?php
00002
00017
00018
00019
00020 define('RUNNING_UPGRADE', 1);
00021
00022 require(dirname(__FILE__) . '/includes/cliTool.inc.php');
00023
00024 import('install.Upgrade');
00025 import('site.Version');
00026 import('site.VersionCheck');
00027
00028 class upgradeTool extends CommandLineTool {
00029
00031 var $command;
00032
00037 function upgradeTool($argv = array()) {
00038 parent::CommandLineTool($argv);
00039
00040 if (!isset($this->argv[0]) || !in_array($this->argv[0], array('check', 'latest', 'upgrade', 'patch', 'download'))) {
00041 $this->usage();
00042 exit(1);
00043 }
00044
00045 $this->command = $this->argv[0];
00046 }
00047
00051 function usage() {
00052 echo "Upgrade tool\n"
00053 . "Usage: {$this->scriptName} command\n"
00054 . "Supported commands:\n"
00055 . " check perform version check\n"
00056 . " latest display latest version info\n"
00057 . " upgrade [pretend] execute upgrade script\n"
00058 . " patch download and apply patch for latest version\n"
00059 . " download [package|patch] download latest version (does not unpack/install)\n";
00060 }
00061
00065 function execute() {
00066 $command = $this->command;
00067 $this->$command();
00068 }
00069
00073 function check() {
00074 $this->checkVersion(VersionCheck::getLatestVersion());
00075 }
00076
00080 function latest() {
00081 $this->checkVersion(VersionCheck::getLatestVersion(), true);
00082 }
00083
00087 function upgrade() {
00088 $pretend = isset($this->argv[1]) && $this->argv[1] == 'pretend';
00089 $installer = &new Upgrade(array('manualInstall' => $pretend));
00090 $installer->setLogger($this);
00091
00092 if ($installer->execute()) {
00093 if (count($installer->getNotes()) > 0) {
00094 printf("\nRelease Notes\n");
00095 printf("----------------------------------------\n");
00096 foreach ($installer->getNotes() as $note) {
00097 printf("%s\n\n", $note);
00098 }
00099 }
00100
00101 if ($pretend) {
00102 if (count($installer->getSQL()) > 0) {
00103 printf("\nSQL\n");
00104 printf("----------------------------------------\n");
00105 foreach ($installer->getSQL() as $sql) {
00106 printf("%s\n\n", $sql);
00107 }
00108 }
00109
00110 } else {
00111 $newVersion = &$installer->getNewVersion();
00112 printf("Successfully upgraded to version %s\n", $newVersion->getVersionString());
00113 }
00114
00115 } else {
00116 printf("ERROR: Upgrade failed: %s\n", $installer->getErrorString());
00117 }
00118 }
00119
00123 function patch() {
00124 $versionInfo = VersionCheck::getLatestVersion();
00125 $check = $this->checkVersion($versionInfo);
00126
00127 if ($check < 0) {
00128 $patch = VersionCheck::getPatch($versionInfo);
00129 if (!isset($patch)) {
00130 printf("No applicable patch available\n");
00131 return;
00132 }
00133
00134 $outFile = $versionInfo['application'] . '-' . $versionInfo['release'] . '.patch';
00135 printf("Download patch: %s\n", $patch);
00136 printf("Patch will be saved to: %s\n", $outFile);
00137
00138 if (!$this->promptContinue()) {
00139 exit(0);
00140 }
00141
00142 $out = fopen($outFile, 'wb');
00143 if (!$out) {
00144 printf("Failed to open %s for writing\n", $outFile);
00145 exit(1);
00146 }
00147
00148 $in = gzopen($patch, 'r');
00149 if (!$in) {
00150 printf("Failed to open %s for reading\n", $patch);
00151 fclose($out);
00152 exit(1);
00153 }
00154
00155 printf('Downloading patch...');
00156
00157 while(($data = gzread($in, 4096)) !== '') {
00158 printf('.');
00159 fwrite($out, $data);
00160 }
00161
00162 printf("done\n");
00163
00164 gzclose($in);
00165 fclose($out);
00166
00167 $command = 'patch -p1 < ' . escapeshellarg($outFile);
00168 printf("Apply patch: %s\n", $command);
00169
00170 if (!$this->promptContinue()) {
00171 exit(0);
00172 }
00173
00174 system($command, $ret);
00175 if ($ret == 0) {
00176 printf("Successfully applied patch for version %s\n", $versionInfo['release']);
00177 } else {
00178 printf("ERROR: Failed to apply patch\n");
00179 }
00180 }
00181 }
00182
00186 function download() {
00187 $versionInfo = VersionCheck::getLatestVersion();
00188 if (!$versionInfo) {
00189 printf("Failed to load version info from %s\n", VersionCheck::getVersionCheckUrl());
00190 exit(1);
00191 }
00192
00193 $type = isset($this->argv[1]) && $this->argv[1] == 'patch' ? 'patch' : 'package';
00194 if ($type == 'package') {
00195 $download = $versionInfo['package'];
00196 } else {
00197 $download = VersionCheck::getPatch($versionInfo);
00198 }
00199 if (!isset($download)) {
00200 printf("No applicable download available\n");
00201 return;
00202 }
00203 $outFile = basename($download);
00204
00205 printf("Download %s: %s\n", $type, $download);
00206 printf("File will be saved to: %s\n", $outFile);
00207
00208 if (!$this->promptContinue()) {
00209 exit(0);
00210 }
00211
00212 $out = fopen($outFile, 'wb');
00213 if (!$out) {
00214 printf("Failed to open %s for writing\n", $outFile);
00215 exit(1);
00216 }
00217
00218 $in = fopen($download, 'rb');
00219 if (!$in) {
00220 printf("Failed to open %s for reading\n", $download);
00221 fclose($out);
00222 exit(1);
00223 }
00224
00225 printf('Downloading file...');
00226
00227 while(($data = fread($in, 4096)) !== '') {
00228 printf('.');
00229 fwrite($out, $data);
00230 }
00231
00232 printf("done\n");
00233
00234 fclose($in);
00235 fclose($out);
00236 }
00237
00243 function checkVersion($versionInfo, $displayInfo = false) {
00244 if (!$versionInfo) {
00245 printf("Failed to load version info from %s\n", VersionCheck::getVersionCheckUrl());
00246 exit(1);
00247 }
00248
00249 $dbVersion = VersionCheck::getCurrentDBVersion();
00250 $codeVersion = VersionCheck::getCurrentCodeVersion();
00251 $latestVersion = $versionInfo['version'];
00252
00253 printf("Code version: %s\n", $codeVersion->getVersionString());
00254 printf("Database version: %s\n", $dbVersion->getVersionString());
00255 printf("Latest version: %s\n", $latestVersion->getVersionString());
00256
00257 $compare1 = $codeVersion->compare($latestVersion);
00258 $compare2 = $dbVersion->compare($codeVersion);
00259
00260 if (!$displayInfo) {
00261 if ($compare2 < 0) {
00262 printf("Database version is older than code version\n");
00263 printf("Run \"{$this->scriptName} upgrade\" to update\n");
00264 exit(0);
00265
00266 } else if($compare2 > 0) {
00267 printf("Database version is newer than code version!\n");
00268 exit(1);
00269
00270 } else if ($compare1 == 0) {
00271 printf("Your system is up-to-date\n");
00272
00273 } else if($compare1 < 0) {
00274 printf("A newer version is available:\n");
00275 $displayInfo = true;
00276 } else {
00277 printf("Current version is newer than latest!\n");
00278 exit(1);
00279 }
00280 }
00281
00282 if ($displayInfo) {
00283 $patch = VersionCheck::getPatch($versionInfo, $codeVersion);
00284 printf(" tag: %s\n", $versionInfo['tag']);
00285 printf(" date: %s\n", $versionInfo['date']);
00286 printf(" info: %s\n", $versionInfo['info']);
00287 printf(" package: %s\n", $versionInfo['package']);
00288 printf(" patch: %s\n", isset($patch) ? $patch : 'N/A');
00289 }
00290
00291 return $compare1;
00292 }
00293
00298 function promptContinue($prompt = "Continue?") {
00299 printf("%s [y/N] ", $prompt);
00300 $continue = fread(STDIN, 255);
00301 return (strtolower(substr(trim($continue), 0, 1)) == 'y');
00302 }
00303
00308 function log($message) {
00309 printf("[%s]\n", $message);
00310 }
00311
00312 }
00313
00314 $tool = &new upgradeTool(isset($argv) ? $argv : array());
00315 $tool->execute();
00316 ?>