• 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 
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                 global $ilCtrlStructureReader;
00193                 
00194                 $f = $this->fileVersion;
00195                 $c = $this->currentVersion;
00196 
00197                 if ($c < $f)
00198                 {
00199                         $msg = array();
00200                         for ($i=($c+1); $i<=$f; $i++)
00201                         {
00202                                 if ($this->applyUpdateNr($i) == false)
00203                                 {
00204                                         $msg[] = array(
00205                                                 "msg" => "update_error: ".$this->error,
00206                                                 "nr" => $i
00207                                         );
00208                                         $this->updateMsg = $msg;
00209                                         return false;
00210                                 }
00211                                 else
00212                                 {
00213                                         $msg[] = array(
00214                                                 "msg" => "update_applied",
00215                                                 "nr" => $i
00216                                         );
00217                                 }
00218                         }
00219 
00220                         $this->updateMsg = $msg;
00221                 }
00222                 else
00223                 {
00224                         $this->updateMsg = "no_changes";
00225                 }
00226                 
00227                 // read module and service information into db
00228                 require_once "./classes/class.ilModuleReader.php";
00229                 require_once "./classes/class.ilServiceReader.php";
00230                 require_once "./classes/class.ilCtrlStructureReader.php";
00231                 $ilModuleReader = new ilModuleReader();
00232                 $ilModuleReader->getModules();
00233                 $ilServiceReader = new ilServiceReader();
00234                 $ilServiceReader->getServices();
00235                 $ilCtrlStructureReader->readStructure();
00236                                 
00237                 return true;
00238         }
00239 
00246         function applyUpdateNr($nr)
00247         {
00248                 global $ilDB,$ilErr,$ilUser,$ilCtrlStructureReader,$ilModuleReader;
00249 
00250                 //search for desired $nr
00251                 reset($this->filecontent);
00252 
00253                 //init
00254                 $i = 0;
00255 
00256             //go through filecontent
00257                 while (!ereg("^<#".$nr.">", $this->filecontent[$i]) && $i<count($this->filecontent))
00258                 {
00259                         $i++;
00260                 }
00261 
00262                 //update not found
00263                 if ($i == count($this->filecontent))
00264                 {
00265                         $this->error = "update_not_found";
00266                         return false;
00267                 }
00268 
00269                 $i++;
00270 
00271                 //update found, now extract this update to a new array
00272                 $update = array();
00273                 while ($i<count($this->filecontent) && !ereg("^<#".($nr+1).">", $this->filecontent[$i]))
00274                 {
00275                         $update[] = trim($this->filecontent[$i]);
00276                         $i++;
00277                 }
00278 
00279                 //now you have the update, now process it
00280                 $sql = array();
00281                 $php = array();
00282                 $mode = "sql";
00283 
00284                 foreach ($update as $row)
00285                 {
00286                         if (ereg("<\?php", $row))
00287                         {
00288                                 if (count($sql)>0)
00289                                 {
00290                                         if ($this->execQuery($this->db, implode("\n", $sql)) == false)
00291                                         {
00292                                                 $this->error = "dump_error: ".$this->error;
00293                                                 return false;
00294                                         }
00295                                         $sql = array();
00296                                 }
00297                                 $mode = "php";
00298                         }
00299                         elseif (ereg("\?>", $row))
00300                         {
00301                                 if (count($php)>0)
00302                                 {
00303                                         eval(implode("\n", $php));
00304                                         $php = array();
00305                                 }
00306                                 $mode = "sql";
00307 
00308                         }
00309                         else
00310                         {
00311                                 if ($mode == "sql")
00312                                 {
00313                                         $sql[] = $row;
00314                                 }
00315 
00316                                 if ($mode == "php")
00317                                 {
00318                                         $php[] = $row;
00319                                 }
00320                         } //else
00321                 } //foreach
00322 
00323                 if ($mode == "sql" && count($sql) > 0)
00324                 {
00325                         if ($this->execQuery($this->db, implode("\n", $sql)) == false)
00326                         {
00327                                 $this->error = "dump_error: ".$this->error;
00328                                 return false;
00329                         }
00330                 }
00331         
00332                 //increase db_Version number
00333                 $this->setCurrentVersion($nr);
00334                 //$this->currentVersion = $ilias->getSetting("db_version");
00335                 
00336                 return true;
00337                 
00338         }
00339         
00340         function getDBVersionStatus()
00341         {
00342                 if ($this->fileVersion > $this->currentVersion)
00343                         return false;
00344                 else
00345                         return true;
00346         }
00347         
00348         function getTables()
00349         {
00350                 $a = array();
00351         
00352                 $query = "SHOW TABLES"; 
00353                 $res = $this->db->query($query);
00354                 while ($row = $res->fetchRow())
00355                 {
00356                         $status = $this->getTableStatus($row[0]);
00357                         $a[] = array(
00358                                 "name" => $status["Table"],
00359                                 "table" => $row[0],
00360                                 "status" => $status["Msg_text"]
00361                         );
00362                 }
00363                 return $a;
00364         }
00365         
00366         function getTableStatus($table)
00367         {
00368                 $a = array();
00369         
00370                 $query = "ANALYZE TABLE ".$table;       
00371                 $res = $this->db->query($query);
00372                 $row = $res->fetchRow(DB_FETCHMODE_ASSOC);
00373                 return $row;
00374         }
00375         
00376         function optimizeTables($tables)
00377         {
00378                 $msg = array();
00379                 foreach ($_POST["tables"] as $key => $value)
00380                 {
00381                         $query = "OPTIMIZE TABLE ".$key;        
00382                         $res = $this->db->query($query);
00383                         $msg[] = "table $key: ok";
00384                 }       
00385                 return $msg;
00386         }
00387 
00388 } // END class.DBUdate
00389 ?>

Generated on Fri Dec 13 2013 13:52:06 for ILIAS Release_3_7_x_branch .rev 46817 by  doxygen 1.7.1