diogenes.database-creator.inc.php

00001 <?php
00002 /*
00003  * Copyright (C) 2003-2004 Polytechnique.org
00004  * http://opensource.polytechnique.org/
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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     // debugging info
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     // set default options
00156     $this->dbdb = $dbdb;
00157     $this->dbhost = $dbhost;
00158     $this->dbuser = $dbuser;
00159     $this->dbpass = $dbpass;
00160     
00161     // parse options
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     // check we know the current database version
00236     if (!in_array($dbversion, $versions)) {
00237       $this->error("Unknown database format version '$dbversion'");
00238       exit(1);
00239     }
00240 
00241     // runs the successive updates
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 ?>

Generated on Fri Jan 11 01:20:08 2008 for Diogenes by  doxygen 1.5.1