00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 require_once dirname(__FILE__).'/diogenes.database.inc.php';
00023
00024
00027 class DiogenesDatabaseCreator {
00029 var $opt_info = true;
00030
00032 var $opt_debug = false;
00033
00035 var $opt_table;
00036
00038 var $versions = array();
00039
00045 function DiogenesDatabaseCreator($opt_table)
00046 {
00047 $this->opt_table = $opt_table;
00048 }
00049
00050
00054 function connect()
00055 {
00056
00057 $this->debug("host : ".$this->dbhost);
00058 $this->debug("user : ".$this->dbuser);
00059 $this->debug("pass : ".(($this->dbpass != "") ? "true" : "false"));
00060 $this->debug("database : ".$this->dbdb);
00061
00062 $this->dbh = new DiogenesDatabase($this->dbdb, $this->dbhost, $this->dbuser, $this->dbpass);
00063
00064 if (!$this->dbh->connect_id) {
00065 $this->error("Unable to connect to the database!");
00066 }
00067
00068 return $this->dbh->connect_id;
00069 }
00070
00071
00077 function debug($msg)
00078 {
00079 if ($this->opt_debug)
00080 echo "D: $msg\n";
00081 }
00082
00083
00089 function info($msg)
00090 {
00091 if ($this->opt_info)
00092 echo "I: $msg\n";
00093 }
00094
00095
00101 function error($msg)
00102 {
00103 echo "E: $msg\n";
00104 }
00105
00106
00112 function upgradeDb($newversion)
00113 {
00114 $this->info("updrade to $newversion");
00115 }
00116
00117
00121 function getVersion()
00122 {
00123 $res = $this->dbh->query("SELECT value FROM {$this->opt_table} WHERE name='dbversion'");
00124 if (list($dbversion) = mysql_fetch_row($res)) {
00125 mysql_free_result($res);
00126 } else {
00127 $dbversion = $this->versions[0];
00128 }
00129 return $dbversion;
00130 }
00131
00132
00138 function setVersion($newversion)
00139 {
00140 $this->dbh->query("REPLACE INTO {$this->opt_table} SET name='dbversion',value='$newversion'");
00141 }
00142
00143
00153 function parseOptions($argv, $dbdb, $dbhost, $dbuser, $dbpass)
00154 {
00155
00156 $this->dbdb = $dbdb;
00157 $this->dbhost = $dbhost;
00158 $this->dbuser = $dbuser;
00159 $this->dbpass = $dbpass;
00160
00161
00162 $script = basename($argv[0]);
00163 $opts = Console_GetOpt::getopt($argv, "d:hp:qs:u:v");
00164
00165 if ( PEAR::isError($opts) ) {
00166 echo $opts->getMessage();
00167 $this->syntax($script);
00168 exit(1);
00169 } else {
00170 $opts = $opts[0];
00171 foreach ( $opts as $opt) {
00172 switch ($opt[0]) {
00173 case "h":
00174 $this->syntax($script);
00175 exit(0);
00176 case "q":
00177 $this->opt_info = false;
00178 $this->opt_debug = false;
00179 break;
00180 case "d":
00181 $this->dbdb = $opt[1];
00182 break;
00183 case "u":
00184 $this->dbuser = $opt[1];
00185 break;
00186 case "v":
00187 $this->opt_info = true;
00188 $this->opt_debug = true;
00189 break;
00190 case "s":
00191 $this->dbhost = $opt[1];
00192 break;
00193 case "p":
00194 $this->dbpass = $opt[1];
00195 break;
00196 }
00197 }
00198 }
00199
00200 }
00201
00202
00206 function syntax($script)
00207 {
00208 echo
00209 "\nSyntax\n".
00210 " $script [options]\n\n".
00211 "Options\n".
00212 " -h display this help message\n".
00213 " -q quiet mode\n".
00214 " -v verbose mode\n\n".
00215 " -d database\n".
00216 " -s host\n".
00217 " -u user\n".
00218 " -p password\n\n";
00219 }
00220
00221
00225 function run()
00226 {
00227 if (!$this->connect()) {
00228 exit(1);
00229 }
00230
00231 $versions = $this->versions;
00232 $dbversion = $this->getVersion();
00233 $this->info("Current database version is $dbversion");
00234
00235
00236 if (!in_array($dbversion, $versions)) {
00237 $this->error("Unknown database format version '$dbversion'");
00238 exit(1);
00239 }
00240
00241
00242 $from = array_search($dbversion, $versions);
00243 $to = sizeof($versions)-1;
00244
00245 for($pos = $from; $pos < $to; $pos++) {
00246 $oldversion = $versions[$pos];
00247 $newversion = $versions[$pos+1];
00248 $this->info("Upgrading from DB format '$oldversion' to '$newversion'");
00249
00250 $this->upgradeDb($newversion);
00251 $this->setVersion($newversion);
00252 }
00253
00254 }
00255
00256 }
00257
00258 ?>