.
*/
define("ROOTPATH", "../");
require_once(ROOTPATH . "include/include.php");
$layout = new Layout();
$layout->init(false);
$layout->pageHeader();
$layout->echoln("
DB updates
");
$dbUpdate = new DbUpdate($layout, $GLOBALS['sql']);
$layout->pageFooter();
class DbUpdate {
private $layout;
private $mysql;
public function __construct(Layout $layout, MySQL $mysql) {
$this->layout = $layout;
$this->mysql = $mysql;
$this->init();
}
private function init() {
$currentDbVersion = $this->getCurrentDbVersion();
$updateFiles = glob("../sql/db-update-*.sql");
$handled = 0;
foreach( $updateFiles as $file ) {
if( preg_match("/db-update-([0-9]{4}).sql$/", $file, $matches) ) {
if( $currentDbVersion<$matches[1] ) {
// Process file
$this->handleFile($file, $matches[1]);
$handled++;
}else {
$this->verifyChecksum($file);
}
}
}
$this->layout->echoln("Handled " . $handled . " update files. Script finished. ");
$this->layout->echoln("
MySQL database is now at version ".$this->getCurrentDbVersion().".");
}
private function verifyChecksum($file) {
$query = "SELECT `value` FROM `dbconfig` WHERE `name`='".$this->mysql->escape(basename($file).".md5")."'";
if( $this->mysql->query($query) && $this->mysql->getNumRows()==1 ) {
$row = $this->mysql->fetchAssoc();
if( $this->getMd5($file)!=$row['value'] ) {
$this->layout->echoln("
MD5 mismatch for already imported file '" . basename($file) . "'. The file has been modified after it was imported!
");
}
}
}
private function getMd5($file) {
return md5_file($file);
}
private function handleFile($file, $version) {
$this->layout->echoln("Processing update file '" . basename($file) . "'
");
if( $fp = fopen($file, "r") ) {
$line = "";
$success = 0;
$failed = 0;
while( ($buffer = fgets($fp, 4096)) !== false ) {
if( substr(trim($buffer), 0, 2)!="--" ) {
$line .= $buffer;
if( substr(trim($line), -1)==";" ) {
if( $this->processQuery($line) ) {
$success++;
}else {
$failed++;
}
$line = "";
}
}
}
if( substr(trim($line), -1)==";" ) {
if( $this->processQuery($line) ) {
$success++;
}else {
$failed++;
}
}
fclose($fp);
$this->layout->echoln("Finished processing update file '" . basename($file) . "'; success: ".$success." queries; failed: ".$failed." queries
");
$this->mysql->query("UPDATE `dbconfig` SET `value`='".$this->mysql->escape($version)."' WHERE `name`='db-version'");
$this->mysql->query("INSERT INTO `dbconfig` SET `name`='".$this->mysql->escape(basename($file).".md5")."', `value`='".$this->mysql->escape($this->getMd5($file))."'");
}else {
$this->layout->echoln("Failed opening file.
");
}
}
private function processQuery($query) {
if( $this->mysql->query($query) ) {
$this->layout->echoln("Query successful: " . $query . "
");
return true;
}else {
$this->layout->echoln("Query failed: " . $query . "
".$this->mysql->getErrorMsg()."
");
return false;
}
}
private function getCurrentDbVersion() {
if( $this->mysql->query("SHOW TABLES") ) {
if( $this->mysql->getNumRows()==0 ) {
return 0;
}else {
$this->mysql->query("SELECT `value` FROM `dbconfig` WHERE `name`='db-version'");
$row = $this->mysql->fetchArray();
return $row['value'];
}
}else {
return 99999;
}
}
}
?>