.
*/
include("../include/include-sub.php");
//require_once("../../oms/include/classes/DefaultLayout.php");
require_once(FILESYSTEMROOTPATH."jc/include/classes/DefaultLayout.php");
//$layout = new ImportLayout(Access::ANONYMOUS, "./");
$layout = new DefaultLayout(Access::ANONYMOUS);
$layout->init();
class DbUpdate {
private $layout;
private $mysql;
public function __construct(Layout $layout, MySQL $mysql) {
$this->layout = $layout;
$this->mysql = $mysql;
$this->init();
}
private function init() {
if( !$this->mysql->connect() ) {
$this->layout->error("MySQL error: " . $this->mysql->getErrorMsg());
return;
}
$currentDbVersion = $this->getCurrentDbVersion();
$updateFiles = glob("../DB-files/MySQL/db-update-*.sql");
$handled = 0;
foreach( $updateFiles as $file ) {
if( preg_match("/db-update-([0-9]+).sql$/", $file, $matches) ) {
if( $currentDbVersion<$matches[1] ) {
// Process file
$this->handleFile($file, $matches[1]);
$handled++;
}
}
}
$this->layout->echoln("
Handled " . $handled . " update files. Script finished. ");
$this->layout->echoln("
MySQL database is now at version ".$this->getCurrentDbVersion().".");
}
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 `config` SET `value`='".$this->mysql->escape($version)."' WHERE `name`='db-version'");
}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 `config` WHERE `name`='db-version'");
$row = $this->mysql->fetchArray();
return $row['value'];
}
}else {
return 99999;
}
}
}
$layout->echoln("JC DB updates
");
$dbUpdate = new DbUpdate($layout, $sql);
$layout->page_footer();
?>