Go to the documentation of this file.00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
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
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
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
00167 if (substr($sql[$i],-1)==";")
00168 {
00169
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 }
00179 else
00180 {
00181 $q .= " ".$sql[$i];
00182 }
00183 }
00184 }
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
00225 return true;
00226 }
00227
00234 function applyUpdateNr($nr)
00235 {
00236 global $ilDB,$ilErr,$ilUser,$ilCtrlStructureReader,$ilModuleReader;
00237
00238
00239 reset($this->filecontent);
00240
00241
00242 $i = 0;
00243
00244
00245 while (!ereg("^<#".$nr.">", $this->filecontent[$i]) && $i<count($this->filecontent))
00246 {
00247 $i++;
00248 }
00249
00250
00251 if ($i == count($this->filecontent))
00252 {
00253 $this->error = "update_not_found";
00254 return false;
00255 }
00256
00257 $i++;
00258
00259
00260 $update = array();
00261 while ($i<count($this->filecontent) && !ereg("^<#".($nr+1).">", $this->filecontent[$i]))
00262 {
00263 $update[] = trim($this->filecontent[$i]);
00264 $i++;
00265 }
00266
00267
00268 $sql = array();
00269 $php = array();
00270 $mode = "sql";
00271
00272 foreach ($update as $row)
00273 {
00274 if (ereg("<\?php", $row))
00275 {
00276 if (count($sql)>0)
00277 {
00278 if ($this->execQuery($this->db, implode("\n", $sql)) == false)
00279 {
00280 $this->error = "dump_error: ".$this->error;
00281 return false;
00282 }
00283 $sql = array();
00284 }
00285 $mode = "php";
00286 }
00287 elseif (ereg("\?>", $row))
00288 {
00289 if (count($php)>0)
00290 {
00291 eval(implode("\n", $php));
00292 $php = array();
00293 }
00294 $mode = "sql";
00295
00296 }
00297 else
00298 {
00299 if ($mode == "sql")
00300 {
00301 $sql[] = $row;
00302 }
00303
00304 if ($mode == "php")
00305 {
00306 $php[] = $row;
00307 }
00308 }
00309 }
00310
00311 if ($mode == "sql" && count($sql) > 0)
00312 {
00313 if ($this->execQuery($this->db, implode("\n", $sql)) == false)
00314 {
00315 $this->error = "dump_error: ".$this->error;
00316 return false;
00317 }
00318 }
00319
00320
00321 $this->setCurrentVersion($nr);
00322
00323
00324 return true;
00325
00326 }
00327
00328 function getDBVersionStatus()
00329 {
00330 if ($this->fileVersion > $this->currentVersion)
00331 return false;
00332 else
00333 return true;
00334 }
00335
00336 function getTables()
00337 {
00338 $a = array();
00339
00340 $query = "SHOW TABLES";
00341 $res = $this->db->query($query);
00342 while ($row = $res->fetchRow())
00343 {
00344 $status = $this->getTableStatus($row[0]);
00345 $a[] = array(
00346 "name" => $status["Table"],
00347 "table" => $row[0],
00348 "status" => $status["Msg_text"]
00349 );
00350 }
00351 return $a;
00352 }
00353
00354 function getTableStatus($table)
00355 {
00356 $a = array();
00357
00358 $query = "ANALYZE TABLE ".$table;
00359 $res = $this->db->query($query);
00360 $row = $res->fetchRow(DB_FETCHMODE_ASSOC);
00361 return $row;
00362 }
00363
00364 function optimizeTables($tables)
00365 {
00366 $msg = array();
00367 foreach ($_POST["tables"] as $key => $value)
00368 {
00369 $query = "OPTIMIZE TABLE ".$key;
00370 $res = $this->db->query($query);
00371 $msg[] = "table $key: ok";
00372 }
00373 return $msg;
00374 }
00375
00376 }
00377 ?>