• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

classes/class.ilDBUpdate.php

Go to the documentation of this file.
00001 <?php
00002 /*
00003         +-----------------------------------------------------------------------------+
00004         | ILIAS open source                                                           |
00005         +-----------------------------------------------------------------------------+
00006         | Copyright (c) 1998-2001 ILIAS open source, University of Cologne            |
00007         |                                                                             |
00008         | This program is free software; you can redistribute it and/or               |
00009         | modify it under the terms of the GNU General Public License                 |
00010         | as published by the Free Software Foundation; either version 2              |
00011         | of the License, or (at your option) any later version.                      |
00012         |                                                                             |
00013         | This program is distributed in the hope that it will be useful,             |
00014         | but WITHOUT ANY WARRANTY; without even the implied warranty of              |
00015         | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
00016         | GNU General Public License for more details.                                |
00017         |                                                                             |
00018         | You should have received a copy of the GNU General Public License           |
00019         | along with this program; if not, write to the Free Software                 |
00020         | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. |
00021         +-----------------------------------------------------------------------------+
00022 */
00023 
00024 
00025 // include pear
00026 //require_once("DB.php");
00027 
00035 class ilDBUpdate
00036 {
00040         var $DB_UPDATE_FILE;
00041 
00046         var $currentVersion;
00047 
00052         var $fileVersion;
00053 
00057         function ilDBUpdate($a_db_handler = 0,$tmp_flag = false)
00058         {
00059                 // workaround to allow setup migration
00060                 if ($a_db_handler)
00061                 {
00062                         $this->db =& $a_db_handler;
00063                         
00064                         if ($tmp_flag)
00065                         {
00066                                 $this->DB_UPDATE_FILE = "./sql/dbupdate.php";                   
00067                         }
00068                         else
00069                         {
00070                                 $this->DB_UPDATE_FILE = "../sql/dbupdate.php";
00071                         }
00072                 }
00073                 else
00074                 {
00075                         global $mySetup;
00076                         $this->db = $mySetup->db;       
00077                         $this->DB_UPDATE_FILE = "./sql/dbupdate.php";
00078                 }
00079    
00080                 $this->readDBUpdateFile();
00081                 $this->getFileVersion();
00082                 $this->getCurrentVersion();
00083         }
00084         
00090         function _DBUpdate()
00091         {
00092                 $this->db->disconnect();
00093         }
00094 
00095         function readDBUpdateFile()
00096         {
00097                 if (!file_exists($this->DB_UPDATE_FILE))
00098                 {
00099                         $this->error = "no_db_update_file";
00100                         $this->filecontent = array();
00101                         return false;
00102                 }
00103                 
00104                 $this->filecontent = @file($this->DB_UPDATE_FILE);
00105                 return true;
00106         }
00107 
00108         function getCurrentVersion()
00109         {
00110                 $q = "SELECT value FROM settings ".
00111                          "WHERE keyword = 'db_version'";
00112                 $r = $this->db->query($q);
00113                         
00114                 $row = $r->fetchRow(DB_FETCHMODE_OBJECT);
00115                         
00116                 $this->currentVersion = (integer) $row->value;
00117 
00118                 return $this->currentVersion;
00119         }
00120 
00121         function setCurrentVersion ($a_version)
00122         {
00123                 {
00124                         $q = "UPDATE settings SET ".
00125                                  "value = '".$a_version."' ".
00126                                  "WHERE keyword = 'db_version'";
00127                 }
00128 
00129                 $this->db->query($q);
00130                 $this->currentVersion = $a_version;
00131                 
00132                 return true;
00133         }
00134 
00135         function getFileVersion()
00136         {
00137                 //go through filecontent and search for last occurence of <#x>
00138                 reset($this->filecontent);
00139                 $regs = array();
00140                 foreach ($this->filecontent as $row)
00141                 {
00142                         if (ereg("^<#([0-9]+)>", $row, $regs))
00143                         {
00144                                 $version = $regs[1];
00145                         }
00146                 }
00147 
00148                 $this->fileVersion = (integer) $version;
00149                 return $this->fileVersion; 
00150         }
00151         
00158         function execQuery($db,$str)
00159         {
00160                 $sql = explode("\n",trim($str));
00161                 for ($i=0; $i<count($sql); $i++)
00162                 {
00163                         $sql[$i] = trim($sql[$i]);
00164                         if ($sql[$i] != "" && substr($sql[$i],0,1)!="#")
00165                         {
00166                                 //take line per line, until last char is ";"
00167                                 if (substr($sql[$i],-1)==";")
00168                                 {
00169                                         //query is complete
00170                                         $q .= " ".substr($sql[$i],0,-1);
00171                                         $r = $db->query($q);
00172                                         if (DB::isError($r))
00173                                         {
00174                                                 $this->error = $r->getMessage();
00175                                                 return false;
00176                                         }
00177                                         unset($q);
00178                                 } //if
00179                                 else
00180                                 {
00181                                         $q .= " ".$sql[$i];
00182                                 } //else
00183                         } //if
00184                 } //for
00185                 if ($q != "")
00186                         echo "incomplete_statement: ".$q."<br>";
00187                 return true;
00188         }
00189 
00190         function applyUpdate()
00191         {
00192                 $f = $this->fileVersion;
00193                 $c = $this->currentVersion;
00194 
00195                 if ($c < $f)
00196                 {
00197                         $msg = array();
00198                         for ($i=($c+1); $i<=$f; $i++)
00199                         {
00200                                 if ($this->applyUpdateNr($i) == false)
00201                                 {
00202                                         $msg[] = array(
00203                                                 "msg" => "update_error: ".$this->error,
00204                                                 "nr" => $i
00205                                         );
00206                                         $this->updateMsg = $msg;
00207                                         return false;
00208                                 }
00209                                 else
00210                                 {
00211                                         $msg[] = array(
00212                                                 "msg" => "update_applied",
00213                                                 "nr" => $i
00214                                         );
00215                                 }
00216                         }
00217 
00218                         $this->updateMsg = $msg;
00219                 }
00220                 else
00221                 {
00222                         $this->updateMsg = "no_changes";
00223                 }
00224                 return true;
00225         }
00226 
00233         function applyUpdateNr($nr)
00234         {
00235                 global $ilDB,$ilErr,$ilUser,$ilCtrlStructureReader;
00236 
00237                 //search for desired $nr
00238                 reset($this->filecontent);
00239 
00240                 //init
00241                 $i = 0;
00242 
00243             //go through filecontent
00244                 while (!ereg("^<#".$nr.">", $this->filecontent[$i]) && $i<count($this->filecontent))
00245                 {
00246                         $i++;
00247                 }
00248 
00249                 //update not found
00250                 if ($i == count($this->filecontent))
00251                 {
00252                         $this->error = "update_not_found";
00253                         return false;
00254                 }
00255 
00256                 $i++;
00257 
00258                 //update found, now extract this update to a new array
00259                 $update = array();
00260                 while ($i<count($this->filecontent) && !ereg("^<#".($nr+1).">", $this->filecontent[$i]))
00261                 {
00262                         $update[] = trim($this->filecontent[$i]);
00263                         $i++;
00264                 }
00265 
00266                 //now you have the update, now process it
00267                 $sql = array();
00268                 $php = array();
00269                 $mode = "sql";
00270 
00271                 foreach ($update as $row)
00272                 {
00273                         if (ereg("<\?php", $row))
00274                         {
00275                                 if (count($sql)>0)
00276                                 {
00277                                         if ($this->execQuery($this->db, implode("\n", $sql)) == false)
00278                                         {
00279                                                 $this->error = "dump_error: ".$this->error;
00280                                                 return false;
00281                                         }
00282                                         $sql = array();
00283                                 }
00284                                 $mode = "php";
00285                         }
00286                         elseif (ereg("\?>", $row))
00287                         {
00288                                 if (count($php)>0)
00289                                 {
00290                                         eval(implode("\n", $php));
00291                                         $php = array();
00292                                 }
00293                                 $mode = "sql";
00294 
00295                         }
00296                         else
00297                         {
00298                                 if ($mode == "sql")
00299                                 {
00300                                         $sql[] = $row;
00301                                 }
00302 
00303                                 if ($mode == "php")
00304                                 {
00305                                         $php[] = $row;
00306                                 }
00307                         } //else
00308                 } //foreach
00309 
00310                 if ($mode == "sql" && count($sql) > 0)
00311                 {
00312                         if ($this->execQuery($this->db, implode("\n", $sql)) == false)
00313                         {
00314                                 $this->error = "dump_error: ".$this->error;
00315                                 return false;
00316                         }
00317                 }
00318         
00319                 //increase db_Version number
00320                 $this->setCurrentVersion($nr);
00321                 //$this->currentVersion = $ilias->getSetting("db_version");
00322                 
00323                 return true;
00324                 
00325         }
00326         
00327         function getDBVersionStatus()
00328         {
00329                 if ($this->fileVersion > $this->currentVersion)
00330                         return false;
00331                 else
00332                         return true;
00333         }
00334         
00335         function getTables()
00336         {
00337                 $a = array();
00338         
00339                 $query = "SHOW TABLES"; 
00340                 $res = $this->db->query($query);
00341                 while ($row = $res->fetchRow())
00342                 {
00343                         $status = $this->getTableStatus($row[0]);
00344                         $a[] = array(
00345                                 "name" => $status["Table"],
00346                                 "table" => $row[0],
00347                                 "status" => $status["Msg_text"]
00348                         );
00349                 }
00350                 return $a;
00351         }
00352         
00353         function getTableStatus($table)
00354         {
00355                 $a = array();
00356         
00357                 $query = "ANALYZE TABLE ".$table;       
00358                 $res = $this->db->query($query);
00359                 $row = $res->fetchRow(DB_FETCHMODE_ASSOC);
00360                 return $row;
00361         }
00362         
00363         function optimizeTables($tables)
00364         {
00365                 $msg = array();
00366                 foreach ($_POST["tables"] as $key => $value)
00367                 {
00368                         $query = "OPTIMIZE TABLE ".$key;        
00369                         $res = $this->db->query($query);
00370                         $msg[] = "table $key: ok";
00371                 }       
00372                 return $msg;
00373         }
00374 
00375 } // END class.DBUdate
00376 ?>

Generated on Fri Dec 13 2013 09:06:33 for ILIAS Release_3_4_x_branch .rev 46804 by  doxygen 1.7.1