• Main Page
  • Related Pages
  • Modules
  • 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 
00034 class ilDBUpdate
00035 {
00039         var $DB_UPDATE_FILE;
00040 
00045         var $currentVersion;
00046 
00051         var $fileVersion;
00052 
00056         function ilDBUpdate($a_db_handler = 0,$tmp_flag = false)
00057         {               
00058                 // workaround to allow setup migration
00059                 if ($a_db_handler)
00060                 {
00061                         $this->db =& $a_db_handler;
00062                         
00063                         if ($tmp_flag)
00064                         {
00065                                 $this->PATH = "./";
00066                         }
00067                         else
00068                         {
00069                                 $this->PATH = "../";
00070                         }
00071                 }
00072                 else
00073                 {
00074                         global $mySetup;
00075                         $this->db = $mySetup->db;
00076                         $this->PATH = "./";
00077                 }
00078                 
00079                 $this->getCurrentVersion();
00080                 
00081                 // get update file for current version
00082                 $updatefile = $this->getFileForStep($this->currentVersion + 1);
00083 
00084                 $this->current_file = $updatefile;
00085                 $this->DB_UPDATE_FILE = $this->PATH."setup/sql/".$updatefile;
00086 
00087                 //
00088                 // NOTE: IF YOU SET THIS TO THE NEWEST FILE, CHANGE ALSO getFileForStep()
00089                 //
00090                 $this->LAST_UPDATE_FILE = $this->PATH."setup/sql/dbupdate_02.php";
00091    
00092                 $this->readDBUpdateFile();
00093                 $this->readLastUpdateFile();
00094                 $this->getFileVersion();
00095         }
00096         
00100         function getFileForStep($a_version)
00101         {
00102                 //
00103                 // NOTE: IF YOU ADD A NEW FILE HERE, CHANGE ALSO THE CONSTRUCTOR
00104                 //
00105                 if ((int)$a_version > 864)              // last number in previous file
00106                 {
00107                         return "dbupdate_02.php";
00108                 }
00109                 else
00110                 {
00111                         return "dbupdate.php";
00112                 }
00113         }
00114         
00120         function _DBUpdate()
00121         {
00122                 $this->db->disconnect();
00123         }
00124 
00125         function readDBUpdateFile()
00126         {
00127                 if (!file_exists($this->DB_UPDATE_FILE))
00128                 {
00129                         $this->error = "no_db_update_file";
00130                         $this->filecontent = array();
00131                         return false;
00132                 }
00133                 
00134                 $this->filecontent = @file($this->DB_UPDATE_FILE);
00135                 return true;
00136         }
00137 
00138         function readLastUpdateFile()
00139         {
00140                 if (!file_exists($this->LAST_UPDATE_FILE))
00141                 {
00142                         $this->error = "no_last_update_file";
00143                         $this->filecontent = array();
00144                         return false;
00145                 }
00146                 
00147                 $this->lastfilecontent = @file($this->LAST_UPDATE_FILE);
00148                 return true;
00149         }
00150 
00151         function getCurrentVersion()
00152         {
00153                 $q = "SELECT value FROM settings ".
00154                          "WHERE keyword = 'db_version'";
00155                 $r = $this->db->query($q);
00156                         
00157                 $row = $r->fetchRow(DB_FETCHMODE_OBJECT);
00158                         
00159                 $this->currentVersion = (integer) $row->value;
00160 
00161                 return $this->currentVersion;
00162         }
00163 
00164         function setCurrentVersion ($a_version)
00165         {
00166                 {
00167                         $q = "UPDATE settings SET ".
00168                                  "value = ".$this->db->quote($a_version)." ".
00169                                  "WHERE keyword = 'db_version'";
00170                 }
00171 
00172                 $this->db->query($q);
00173                 $this->currentVersion = $a_version;
00174                 
00175                 return true;
00176         }
00177 
00178         function getFileVersion()
00179         {
00180                 //go through filecontent and search for last occurence of <#x>
00181                 reset($this->lastfilecontent);
00182                 $regs = array();
00183                 foreach ($this->lastfilecontent as $row)
00184                 {
00185                         if (ereg("^<#([0-9]+)>", $row, $regs))
00186                         {
00187                                 $version = $regs[1];
00188                         }
00189                 }
00190 
00191                 $this->fileVersion = (integer) $version;
00192                 return $this->fileVersion; 
00193         }
00194         
00201         function execQuery($db,$str)
00202         {
00203                 $sql = explode("\n",trim($str));
00204                 for ($i=0; $i<count($sql); $i++)
00205                 {
00206                         $sql[$i] = trim($sql[$i]);
00207                         if ($sql[$i] != "" && substr($sql[$i],0,1)!="#")
00208                         {
00209                                 //take line per line, until last char is ";"
00210                                 if (substr($sql[$i],-1)==";")
00211                                 {
00212                                         //query is complete
00213                                         $q .= " ".substr($sql[$i],0,-1);
00214                                         $r = $db->query($q);
00215                                         if (DB::isError($r))
00216                                         {
00217                                                 $this->error = $r->getMessage();
00218                                                 return false;
00219                                         }
00220                                         unset($q);
00221                                 } //if
00222                                 else
00223                                 {
00224                                         $q .= " ".$sql[$i];
00225                                 } //else
00226                         } //if
00227                 } //for
00228                 if ($q != "")
00229                         echo "incomplete_statement: ".$q."<br>";
00230                 return true;
00231         }
00232 
00233         function applyUpdate()
00234         {
00235                 global $ilCtrlStructureReader;
00236                 
00237                 $f = $this->fileVersion;
00238                 $c = $this->currentVersion;
00239 
00240                 if ($c < $f)
00241                 {
00242                         $msg = array();
00243                         for ($i=($c+1); $i<=$f; $i++)
00244                         {
00245                                 // check wether next update file must be loaded
00246                                 if ($this->current_file != $this->getFileForStep($i))
00247                                 {
00248                                         $this->DB_UPDATE_FILE = $this->PATH."setup/sql/".$this->getFileForStep($i);
00249                                         $this->readDBUpdateFile();
00250                                 }
00251                                 
00252                                 if ($this->applyUpdateNr($i) == false)
00253                                 {
00254                                         $msg[] = array(
00255                                                 "msg" => "update_error: ".$this->error,
00256                                                 "nr" => $i
00257                                         );
00258                                         $this->updateMsg = $msg;
00259                                         return false;
00260                                 }
00261                                 else
00262                                 {
00263                                         $msg[] = array(
00264                                                 "msg" => "update_applied",
00265                                                 "nr" => $i
00266                                         );
00267                                 }
00268                         }
00269 
00270                         $this->updateMsg = $msg;
00271                 }
00272                 else
00273                 {
00274                         $this->updateMsg = "no_changes";
00275                 }
00276                 
00277                 // read module and service information into db
00278                 require_once "./classes/class.ilModuleReader.php";
00279                 require_once "./classes/class.ilServiceReader.php";
00280                 require_once "./classes/class.ilCtrlStructureReader.php";
00281                 $ilModuleReader = new ilModuleReader();
00282                 $ilModuleReader->getModules();
00283                 $ilServiceReader = new ilServiceReader();
00284                 $ilServiceReader->getServices();
00285                 $ilCtrlStructureReader->readStructure();
00286                                 
00287                 return true;
00288         }
00289 
00296         function applyUpdateNr($nr)
00297         {
00298                 global $ilDB,$ilErr,$ilUser,$ilCtrlStructureReader,$ilModuleReader;
00299 
00300                 //search for desired $nr
00301                 reset($this->filecontent);
00302 
00303                 //init
00304                 $i = 0;
00305 
00306             //go through filecontent
00307                 while (!ereg("^<#".$nr.">", $this->filecontent[$i]) && $i<count($this->filecontent))
00308                 {
00309                         $i++;
00310                 }
00311 
00312                 //update not found
00313                 if ($i == count($this->filecontent))
00314                 {
00315                         $this->error = "update_not_found";
00316                         return false;
00317                 }
00318 
00319                 $i++;
00320 
00321                 //update found, now extract this update to a new array
00322                 $update = array();
00323                 while ($i<count($this->filecontent) && !ereg("^<#".($nr+1).">", $this->filecontent[$i]))
00324                 {
00325                         $update[] = trim($this->filecontent[$i]);
00326                         $i++;
00327                 }
00328 
00329                 //now you have the update, now process it
00330                 $sql = array();
00331                 $php = array();
00332                 $mode = "sql";
00333 
00334                 foreach ($update as $row)
00335                 {
00336                         if (ereg("<\?php", $row))
00337                         {
00338                                 if (count($sql)>0)
00339                                 {
00340                                         if ($this->execQuery($this->db, implode("\n", $sql)) == false)
00341                                         {
00342                                                 $this->error = "dump_error: ".$this->error;
00343                                                 return false;
00344                                         }
00345                                         $sql = array();
00346                                 }
00347                                 $mode = "php";
00348                         }
00349                         elseif (ereg("\?>", $row))
00350                         {
00351                                 if (count($php)>0)
00352                                 {
00353                                         eval(implode("\n", $php));
00354                                         $php = array();
00355                                 }
00356                                 $mode = "sql";
00357 
00358                         }
00359                         else
00360                         {
00361                                 if ($mode == "sql")
00362                                 {
00363                                         $sql[] = $row;
00364                                 }
00365 
00366                                 if ($mode == "php")
00367                                 {
00368                                         $php[] = $row;
00369                                 }
00370                         } //else
00371                 } //foreach
00372 
00373                 if ($mode == "sql" && count($sql) > 0)
00374                 {
00375                         if ($this->execQuery($this->db, implode("\n", $sql)) == false)
00376                         {
00377                                 $this->error = "dump_error: ".$this->error;
00378                                 return false;
00379                         }
00380                 }
00381         
00382                 //increase db_Version number
00383                 $this->setCurrentVersion($nr);
00384                 //$this->currentVersion = $ilias->getSetting("db_version");
00385                 
00386                 return true;
00387                 
00388         }
00389         
00390         function getDBVersionStatus()
00391         {
00392                 if ($this->fileVersion > $this->currentVersion)
00393                         return false;
00394                 else
00395                         return true;
00396         }
00397         
00398         function getTables()
00399         {
00400                 $a = array();
00401         
00402                 $query = "SHOW TABLES"; 
00403                 $res = $this->db->query($query);
00404                 while ($row = $res->fetchRow())
00405                 {
00406                         $status = $this->getTableStatus($row[0]);
00407                         $a[] = array(
00408                                 "name" => $status["Table"],
00409                                 "table" => $row[0],
00410                                 "status" => $status["Msg_text"]
00411                         );
00412                 }
00413                 return $a;
00414         }
00415         
00416         function getTableStatus($table)
00417         {
00418                 $a = array();
00419         
00420                 $query = "ANALYZE TABLE ".$table;       
00421                 $res = $this->db->query($query);
00422                 $row = $res->fetchRow(DB_FETCHMODE_ASSOC);
00423                 return $row;
00424         }
00425         
00426         function optimizeTables($tables)
00427         {
00428                 $msg = array();
00429                 foreach ($_POST["tables"] as $key => $value)
00430                 {
00431                         $query = "OPTIMIZE TABLE ".$key;        
00432                         $res = $this->db->query($query);
00433                         $msg[] = "table $key: ok";
00434                 }       
00435                 return $msg;
00436         }
00437 
00438 } // END class.DBUdate
00439 ?>

Generated on Fri Dec 13 2013 17:56:47 for ILIAS Release_3_9_x_branch .rev 46835 by  doxygen 1.7.1