| 1 | <?php |
| 2 | /* |
| 3 | * Copyright (C) 2003-2004 Polytechnique.org |
| 4 | * http://opensource.polytechnique.org/ |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | */ |
| 20 | |
| 21 | |
| 22 | require_once dirname(__FILE__).'/diogenes.database.inc.php'; |
| 23 | |
| 24 | |
| 25 | /** This class is used to create, update and remove databases |
| 26 | */ |
| 27 | class DiogenesDatabaseCreator { |
| 28 | /** Do we show information messages? */ |
| 29 | var $opt_info = true; |
| 30 | |
| 31 | /** Do we show debugging info? */ |
| 32 | var $opt_debug = false; |
| 33 | |
| 34 | /** table containing options */ |
| 35 | var $opt_table; |
| 36 | |
| 37 | /** database versions history */ |
| 38 | var $versions = array(); |
| 39 | |
| 40 | /** |
| 41 | * Initialisation |
| 42 | * |
| 43 | * @param $opt_table |
| 44 | */ |
| 45 | function DiogenesDatabaseCreator($opt_table) |
| 46 | { |
| 47 | $this->opt_table = $opt_table; |
| 48 | } |
| 49 | |
| 50 | |
| 51 | /** |
| 52 | * Connect to the database |
| 53 | */ |
| 54 | function connect() |
| 55 | { |
| 56 | // debugging info |
| 57 | $this->debug("host : ".$this->dbhost); |
| 58 | $this->debug("user : ".$this->dbuser); |
| 59 | $this->debug("pass : ".(($this->dbpass != "") ? "true" : "false")); |
| 60 | $this->debug("database : ".$this->dbdb); |
| 61 | |
| 62 | $this->dbh = new DiogenesDatabase($this->dbdb, $this->dbhost, $this->dbuser, $this->dbpass); |
| 63 | |
| 64 | if (!$this->dbh->connect_id) { |
| 65 | $this->error("Unable to connect to the database!"); |
| 66 | } |
| 67 | |
| 68 | return $this->dbh->connect_id; |
| 69 | } |
| 70 | |
| 71 | |
| 72 | /** |
| 73 | * Displays a debugging message. |
| 74 | * |
| 75 | * @param $msg |
| 76 | */ |
| 77 | function debug($msg) |
| 78 | { |
| 79 | if ($this->opt_debug) |
| 80 | echo "D: $msg\n"; |
| 81 | } |
| 82 | |
| 83 | |
| 84 | /** |
| 85 | * Displays an info message. |
| 86 | * |
| 87 | * @param $msg |
| 88 | */ |
| 89 | function info($msg) |
| 90 | { |
| 91 | if ($this->opt_info) |
| 92 | echo "I: $msg\n"; |
| 93 | } |
| 94 | |
| 95 | |
| 96 | /** |
| 97 | * Displays an error message. |
| 98 | * |
| 99 | * @param $msg |
| 100 | */ |
| 101 | function error($msg) |
| 102 | { |
| 103 | echo "E: $msg\n"; |
| 104 | } |
| 105 | |
| 106 | |
| 107 | /** |
| 108 | * Upgrade the database from one version to the next |
| 109 | * |
| 110 | * @param $newversion |
| 111 | */ |
| 112 | function upgradeDb($newversion) |
| 113 | { |
| 114 | $this->info("updrade to $newversion"); |
| 115 | } |
| 116 | |
| 117 | |
| 118 | /** |
| 119 | * Retrieve the current database version |
| 120 | */ |
| 121 | function getVersion() |
| 122 | { |
| 123 | $res = $this->dbh->query("SELECT value FROM {$this->opt_table} WHERE name='dbversion'"); |
| 124 | if (list($dbversion) = mysql_fetch_row($res)) { |
| 125 | mysql_free_result($res); |
| 126 | } else { |
| 127 | $dbversion = $this->versions[0]; |
| 128 | } |
| 129 | return $dbversion; |
| 130 | } |
| 131 | |
| 132 | |
| 133 | /** |
| 134 | * Set the current database version |
| 135 | * |
| 136 | * @param $newversion |
| 137 | */ |
| 138 | function setVersion($newversion) |
| 139 | { |
| 140 | $this->dbh->query("REPLACE INTO {$this->opt_table} SET name='dbversion',value='$newversion'"); |
| 141 | } |
| 142 | |
| 143 | |
| 144 | /** |
| 145 | * Parse command line options |
| 146 | * |
| 147 | * @param $argv |
| 148 | * @param $dbdb |
| 149 | * @param $dbhost |
| 150 | * @param $dbuser |
| 151 | * @param $dbpass |
| 152 | */ |
| 153 | function parseOptions($argv, $dbdb, $dbhost, $dbuser, $dbpass) |
| 154 | { |
| 155 | // set default options |
| 156 | $this->dbdb = $dbdb; |
| 157 | $this->dbhost = $dbhost; |
| 158 | $this->dbuser = $dbuser; |
| 159 | $this->dbpass = $dbpass; |
| 160 | |
| 161 | // parse options |
| 162 | $script = basename($argv[0]); |
| 163 | $opts = Console_GetOpt::getopt($argv, "d:hp:qs:u:v"); |
| 164 | |
| 165 | if ( PEAR::isError($opts) ) { |
| 166 | echo $opts->getMessage(); |
| 167 | $this->syntax($script); |
| 168 | exit(1); |
| 169 | } else { |
| 170 | $opts = $opts[0]; |
| 171 | foreach ( $opts as $opt) { |
| 172 | switch ($opt[0]) { |
| 173 | case "h": |
| 174 | $this->syntax($script); |
| 175 | exit(0); |
| 176 | case "q": |
| 177 | $this->opt_info = false; |
| 178 | $this->opt_debug = false; |
| 179 | break; |
| 180 | case "d": |
| 181 | $this->dbdb = $opt[1]; |
| 182 | break; |
| 183 | case "u": |
| 184 | $this->dbuser = $opt[1]; |
| 185 | break; |
| 186 | case "v": |
| 187 | $this->opt_info = true; |
| 188 | $this->opt_debug = true; |
| 189 | break; |
| 190 | case "s": |
| 191 | $this->dbhost = $opt[1]; |
| 192 | break; |
| 193 | case "p": |
| 194 | $this->dbpass = $opt[1]; |
| 195 | break; |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | } |
| 201 | |
| 202 | |
| 203 | /** |
| 204 | * Displays program usage. |
| 205 | */ |
| 206 | function syntax($script) |
| 207 | { |
| 208 | echo |
| 209 | "\nSyntax\n". |
| 210 | " $script [options]\n\n". |
| 211 | "Options\n". |
| 212 | " -h display this help message\n". |
| 213 | " -q quiet mode\n". |
| 214 | " -v verbose mode\n\n". |
| 215 | " -d database\n". |
| 216 | " -s host\n". |
| 217 | " -u user\n". |
| 218 | " -p password\n\n"; |
| 219 | } |
| 220 | |
| 221 | |
| 222 | /** |
| 223 | * Main routine |
| 224 | */ |
| 225 | function run() |
| 226 | { |
| 227 | if (!$this->connect()) { |
| 228 | exit(1); |
| 229 | } |
| 230 | |
| 231 | $versions = $this->versions; |
| 232 | $dbversion = $this->getVersion(); |
| 233 | $this->info("Current database version is $dbversion"); |
| 234 | |
| 235 | // check we know the current database version |
| 236 | if (!in_array($dbversion, $versions)) { |
| 237 | $this->error("Unknown database format version '$dbversion'"); |
| 238 | exit(1); |
| 239 | } |
| 240 | |
| 241 | // runs the successive updates |
| 242 | $from = array_search($dbversion, $versions); |
| 243 | $to = sizeof($versions)-1; |
| 244 | |
| 245 | for($pos = $from; $pos < $to; $pos++) { |
| 246 | $oldversion = $versions[$pos]; |
| 247 | $newversion = $versions[$pos+1]; |
| 248 | $this->info("Upgrading from DB format '$oldversion' to '$newversion'"); |
| 249 | |
| 250 | $this->upgradeDb($newversion); |
| 251 | $this->setVersion($newversion); |
| 252 | } |
| 253 | |
| 254 | } |
| 255 | |
| 256 | } |
| 257 | |
| 258 | ?> |