| 1 | <?php␊ |
| 2 | /* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */␊ |
| 3 | /*␊ |
| 4 | # ***** BEGIN LICENSE BLOCK *****␊ |
| 5 | # This file is part of Plume Framework, a simple PHP Application Framework.␊ |
| 6 | # Copyright (C) 2001-2007 Loic d'Anterroches and contributors.␊ |
| 7 | #␊ |
| 8 | # Plume Framework is free software; you can redistribute it and/or modify␊ |
| 9 | # it under the terms of the GNU Lesser General Public License as published by␊ |
| 10 | # the Free Software Foundation; either version 2.1 of the License, or␊ |
| 11 | # (at your option) any later version.␊ |
| 12 | #␊ |
| 13 | # Plume Framework is distributed in the hope that it will be useful,␊ |
| 14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of␊ |
| 15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the␊ |
| 16 | # GNU Lesser General Public License for more details.␊ |
| 17 | #␊ |
| 18 | # You should have received a copy of the GNU Lesser General Public License␊ |
| 19 | # along with this program; if not, write to the Free Software␊ |
| 20 | # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA␊ |
| 21 | #␊ |
| 22 | # ***** END LICENSE BLOCK ***** */␊ |
| 23 | ␊ |
| 24 | /**␊ |
| 25 | * Migration script.␊ |
| 26 | */␊ |
| 27 | if (version_compare(PHP_VERSION, '5.2.4', '<')) {␊ |
| 28 | echo 'Error: You need at least PHP 5.2.4'."\n";␊ |
| 29 | exit(1);␊ |
| 30 | }␊ |
| 31 | set_include_path(get_include_path().PATH_SEPARATOR.dirname(__FILE__));␊ |
| 32 | require 'Pluf.php';␊ |
| 33 | require 'Console/Getopt.php';␊ |
| 34 | ␊ |
| 35 | global $debug;␊ |
| 36 | $debug = false; // Yes a dirty global variable.␊ |
| 37 | $search_path = null;␊ |
| 38 | ␊ |
| 39 | $cg = new Console_Getopt();␊ |
| 40 | $shortoptions = 'aixubrc:v:d';␊ |
| 41 | $longoptions = array('app=', 'version=', 'conf=', 'search-path=', ␊ |
| 42 | 'include-path=');␊ |
| 43 | ␊ |
| 44 | $args = $cg->readPHPArgv(); ␊ |
| 45 | ␊ |
| 46 | function debug($what)␊ |
| 47 | {␊ |
| 48 | global $debug;␊ |
| 49 | if ($debug) {␊ |
| 50 | echo($what."\n");␊ |
| 51 | }␊ |
| 52 | }␊ |
| 53 | ␊ |
| 54 | function usage()␊ |
| 55 | {␊ |
| 56 | echo 'Usage examples:'."\n"␊ |
| 57 | .' Upgrade all: migrate.php --conf=path/to/config.php -a'."\n"␊ |
| 58 | .' Upgrade MyApp: migrate.php --conf=path/to/config.php --app=MyApp'."\n"␊ |
| 59 | .' Backup MyApp: migrate.php --conf=path/to/config.php --app=MyApp -b /path/to/backup/folder [backupname]'."\n"␊ |
| 60 | .' Restore MyApp: migrate.php --conf=path/to/config.php --app=MyApp -r /path/to/backup/folder backupname'."\n"␊ |
| 61 | .' MyApp to ver. 3: migrate.php --conf=path/to/config.php --app=MyApp -v3'."\n"␊ |
| 62 | .''."\n"␊ |
| 63 | .'Options:'."\n"␊ |
| 64 | .' c, --conf: Path to the configuration file.'."\n"␊ |
| 65 | .' a: Upgrade all the installed applications.'."\n"␊ |
| 66 | .' v, --version: Upgrade/Downgrade to the given version.'."\n"␊ |
| 67 | .' --app: Application to upgrade/downgrade.'."\n"␊ |
| 68 | .' u: Dry run, do nothing.'."\n"␊ |
| 69 | .' --search-path: Set the DB search path before the run.'."\n"␊ |
| 70 | .' --include-path: Paths to add to the PHP include path.'."\n" ␊ |
| 71 | .' d: Display debug information.'."\n"␊ |
| 72 | .' i: Install the application(s).'."\n"␊ |
| 73 | .' x: Uninstall the application(s).'."\n"␊ |
| 74 | .' b: Backup the application(s).'."\n"␊ |
| 75 | .' r: Restore the application(s).'."\n"␊ |
| 76 | .''."\n"␊ |
| 77 | .'Note: The command line parser of PEAR is not very robust'."\n"␊ |
| 78 | .' if you have an unexpected error about an offset not'."\n"␊ |
| 79 | .' set. Please report a bug. Thanks!'."\n";␊ |
| 80 | ␊ |
| 81 | }␊ |
| 82 | ␊ |
| 83 | try {␊ |
| 84 | $ret = $cg->getopt($args, $shortoptions, $longoptions);␊ |
| 85 | } catch (Exception $e) {␊ |
| 86 | echo('Error in getopt command line: '.$e->getMessage()."\n"); ␊ |
| 87 | usage();␊ |
| 88 | die();␊ |
| 89 | }␊ |
| 90 | ␊ |
| 91 | // Note that PEAR is not PHP 5 compatible, so the need to create $p.␊ |
| 92 | $p = new PEAR();␊ |
| 93 | if ($p->isError($ret)) {␊ |
| 94 | echo('Error in command line: '.$ret->getMessage()."\n"); ␊ |
| 95 | usage();␊ |
| 96 | die();␊ |
| 97 | }␊ |
| 98 | ␊ |
| 99 | // Parse the options.␊ |
| 100 | $what = array(␊ |
| 101 | 'all' => false,␊ |
| 102 | 'app' => '',␊ |
| 103 | 'conf' => '',␊ |
| 104 | 'version' => null,␊ |
| 105 | 'dry_run' => false,␊ |
| 106 | 'un-install' => false,␊ |
| 107 | 'install' => false,␊ |
| 108 | 'backup' => false,␊ |
| 109 | 'restore' => false,␊ |
| 110 | );␊ |
| 111 | ␊ |
| 112 | $opts = $ret[0];␊ |
| 113 | $args = $ret[1];␊ |
| 114 | if (sizeof($opts) > 0) {␊ |
| 115 | foreach ($opts as $o) {␊ |
| 116 | switch ($o[0]) {␊ |
| 117 | case 'a':␊ |
| 118 | $what['all'] = true;␊ |
| 119 | break;␊ |
| 120 | case 'b':␊ |
| 121 | $what['backup'] = true;␊ |
| 122 | break;␊ |
| 123 | case 'r':␊ |
| 124 | $what['restore'] = true;␊ |
| 125 | break;␊ |
| 126 | case 'v':␊ |
| 127 | case '--version':␊ |
| 128 | $what['version'] = $o[1];␊ |
| 129 | break;␊ |
| 130 | case 'c':␊ |
| 131 | case '--conf':␊ |
| 132 | $what['conf'] = $o[1];␊ |
| 133 | break;␊ |
| 134 | case '--app':␊ |
| 135 | $what['app'] = $o[1];␊ |
| 136 | break;␊ |
| 137 | case 'd':␊ |
| 138 | $debug = true;␊ |
| 139 | break;␊ |
| 140 | case '--search-path':␊ |
| 141 | $search_path = trim($o[1]);␊ |
| 142 | break;␊ |
| 143 | case '--include-path':␊ |
| 144 | set_include_path(get_include_path().PATH_SEPARATOR.trim($o[1]));␊ |
| 145 | break;␊ |
| 146 | case 'u':␊ |
| 147 | $what['dry_run'] = true;␊ |
| 148 | break;␊ |
| 149 | case 'x':␊ |
| 150 | $what['un-install'] = true;␊ |
| 151 | break;␊ |
| 152 | case 'i':␊ |
| 153 | $what['install'] = true;␊ |
| 154 | break;␊ |
| 155 | }␊ |
| 156 | }␊ |
| 157 | } else {␊ |
| 158 | echo 'Error: Missing what to do.'."\n";␊ |
| 159 | usage();␊ |
| 160 | die();␊ |
| 161 | }␊ |
| 162 | ␊ |
| 163 | // control the arguments.␊ |
| 164 | if (('' == $what['conf'] or !file_exists($what['conf']))␊ |
| 165 | or ($what['all'] == false and $what['app'] == '')) {␊ |
| 166 | echo 'Error: Missing what to do or config file.'."\n";␊ |
| 167 | usage();␊ |
| 168 | die();␊ |
| 169 | }␊ |
| 170 | if ($what['all'] and $what['version'] !== null) {␊ |
| 171 | echo 'Error: -a and -v --version cannot be used together.'."\n";␊ |
| 172 | echo ' Run the migration to a given version indenpendtly'."\n";␊ |
| 173 | echo ' for each application.'."\n";␊ |
| 174 | usage();␊ |
| 175 | die();␊ |
| 176 | }␊ |
| 177 | Pluf::start($what['conf']);␊ |
| 178 | if (PHP_SAPI != 'cli' and Pluf::f('migrate_allow_web', false)) {␊ |
| 179 | echo('Error: This script can only be run from the command line.'."\n");␊ |
| 180 | exit();␊ |
| 181 | }␊ |
| 182 | ␊ |
| 183 | debug('PHP include path: '.get_include_path());␊ |
| 184 | ␊ |
| 185 | if ($what['un-install']) {␊ |
| 186 | $apps = array();␊ |
| 187 | if ($what['all']) {␊ |
| 188 | $apps = Pluf::f('installed_apps');␊ |
| 189 | } else {␊ |
| 190 | $apps = array($what['app']);␊ |
| 191 | }␊ |
| 192 | echo 'Applications to uninstall: '.implode(', ', $apps)."\n";␊ |
| 193 | echo 'Please confirm that you want to uninstall by typing "yes": ';␊ |
| 194 | $line = trim(fgets(STDIN)); // reads one line from STDIN␊ |
| 195 | if ($line != 'yes') {␊ |
| 196 | echo 'Abort...'."\n";␊ |
| 197 | die();␊ |
| 198 | }␊ |
| 199 | }␊ |
| 200 | ␊ |
| 201 | if (!is_null($search_path)) {␊ |
| 202 | $db =& Pluf::db();␊ |
| 203 | $db->setSearchPath($search_path);␊ |
| 204 | debug('Set search path to: '.$search_path);␊ |
| 205 | }␊ |
| 206 | $app = null; // Migrate all the applications.␊ |
| 207 | $app_disp = 'all the apps';␊ |
| 208 | $v_disp = 'latest';␊ |
| 209 | if (!is_null($what['version'])) {␊ |
| 210 | $v_disp = $what['version'];␊ |
| 211 | }␊ |
| 212 | if ($what['app']) {␊ |
| 213 | $app = trim($what['app']);␊ |
| 214 | $app_disp = $app;␊ |
| 215 | }␊ |
| 216 | $m = new Pluf_Migration($app);␊ |
| 217 | if ($debug) {␊ |
| 218 | $m->display = true;␊ |
| 219 | }␊ |
| 220 | $m->dry_run = $what['dry_run'];␊ |
| 221 | ␊ |
| 222 | if ($what['install']) {␊ |
| 223 | debug('Install '.$app_disp);␊ |
| 224 | $m->install();␊ |
| 225 | } elseif ($what['un-install']) {␊ |
| 226 | debug('Uninstall '.$app_disp);␊ |
| 227 | $m->unInstall();␊ |
| 228 | } elseif ($what['backup']) {␊ |
| 229 | debug('Backup '.$app_disp);␊ |
| 230 | if (!isset($args[1])) $args[1] = null;␊ |
| 231 | $m->backup($args[0], $args[1]);␊ |
| 232 | } elseif ($what['restore']) {␊ |
| 233 | debug('Restore '.$app_disp);␊ |
| 234 | $m->restore($args[0], $args[1]);␊ |
| 235 | } else {␊ |
| 236 | debug('Migrate '.$app.' to version '.$v_disp);␊ |
| 237 | $m->migrate($what['version']);␊ |
| 238 | }␊ |
| 239 | ␊ |
| 240 | ␊ |
| 241 | ␊ |
| 242 | |