karma plugin settings migration

This commit is contained in:
Steffen Schröder
2014-04-27 21:31:55 +02:00
parent e07ac29d13
commit 8bf32d7157
4 changed files with 317 additions and 203 deletions

View File

@ -0,0 +1,166 @@
<?php
namespace ManiaControl\Database;
use ManiaControl\Callbacks\TimerListener;
use ManiaControl\ManiaControl;
/**
* Database Connection Class
*
* @author steeffeen & kremsy
* @copyright ManiaControl Copyright © 2014 ManiaControl Team
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/
class Database implements TimerListener {
/*
* Public Properties
*/
public $mysqli = null;
public $migrationHelper = null;
/*
* Private Properties
*/
private $maniaControl = null;
/**
* Construct database connection
*/
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
// Get mysql server information
$host = $this->maniaControl->config->database->xpath('host');
$port = $this->maniaControl->config->database->xpath('port');
$user = $this->maniaControl->config->database->xpath('user');
$pass = $this->maniaControl->config->database->xpath('pass');
if (!$host) trigger_error("Invalid database configuration (host).", E_USER_ERROR);
if (!$port) trigger_error("Invalid database configuration (port).", E_USER_ERROR);
if (!$user) trigger_error("Invalid database configuration (user).", E_USER_ERROR);
if (!$pass) trigger_error("Invalid database configuration (pass).", E_USER_ERROR);
$host = (string) $host[0];
$port = (int) $port[0];
$user = (string) $user[0];
$pass = (string) $pass[0];
// Enable mysqli Reconnect
ini_set('mysqli.reconnect', 'on');
// Open database connection
$this->mysqli = @new \mysqli($host, $user, $pass, null, $port);
if ($this->mysqli->connect_error) {
trigger_error($this->mysqli->connect_error, E_USER_ERROR);
}
$this->mysqli->set_charset("utf8");
$this->initDatabase();
$this->optimizeTables();
// Register Method which checks the Database Connection every 5 seconds
$this->maniaControl->timerManager->registerTimerListening($this, 'checkConnection', 5000);
// Create migration helper
$this->migrationHelper = new MigrationHelper($maniaControl);
}
/**
* Check if Connection still exists every 5 seconds
*
* @param $time
*/
public function checkConnection($time) {
if (!$this->mysqli->ping()) {
$this->maniaControl->quit("The MySQL server has gone away");
}
}
/**
* Destruct database connection
*/
public function __destruct() {
if ($this->mysqli) {
$this->mysqli->close();
}
}
/**
* Connect to the defined database (create it if needed)
*
* @return bool
*/
private function initDatabase() {
$dbName = $this->maniaControl->config->database->xpath('db_name');
if (!$dbName) {
trigger_error("Invalid database configuration (database).", E_USER_ERROR);
return false;
}
$dbName = (string) $dbName[0];
// Try to connect
$result = $this->mysqli->select_db($dbName);
if ($result) return true;
// Create database
$databaseQuery = "CREATE DATABASE ?;";
$databaseStatement = $this->mysqli->prepare($databaseQuery);
if ($this->mysqli->error) {
trigger_error($this->mysqli->error, E_USER_ERROR);
return false;
}
$databaseStatement->bind_param('s', $dbName);
$databaseStatement->execute();
if ($databaseStatement->error) {
trigger_error($databaseStatement->error, E_USER_ERROR);
return false;
}
$databaseStatement->close();
// Connect to new database
$this->mysqli->select_db($dbName);
if ($this->mysqli->error) {
trigger_error("Couldn't select database '{$dbName}'. " . $this->mysqli->error, E_USER_ERROR);
return false;
}
return true;
}
/**
* Optimize all existing tables
*
* @return bool
*/
private function optimizeTables() {
$showQuery = "SHOW TABLES;";
$result = $this->mysqli->query($showQuery);
if ($this->mysqli->error) {
trigger_error($this->mysqli->error);
return false;
}
$count = $result->num_rows;
if ($count <= 0) {
$result->close();
return true;
}
$optimizeQuery = "OPTIMIZE TABLE ";
$index = 0;
while ($row = $result->fetch_row()) {
$tableName = $row[0];
$optimizeQuery .= "`{$tableName}`";
if ($index < $count - 1) {
$optimizeQuery .= ", ";
}
$index++;
}
$result->close();
$optimizeQuery .= ";";
$this->mysqli->query($optimizeQuery);
if ($this->mysqli->error) {
trigger_error($this->mysqli->error);
return false;
}
return true;
}
}

View File

@ -0,0 +1,75 @@
<?php
namespace ManiaControl\Database;
use ManiaControl\Settings\SettingManager;
use ManiaControl\ManiaControl;
/**
* Database Migration Assistant
*
* @author steeffeen
* @copyright ManiaControl Copyright © 2014 ManiaControl Team
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/
class MigrationHelper {
/*
* Private Properties
*/
private $maniaControl = null;
/**
* Construct Migration Helper
*
* @param ManiaControl $maniaControl
*/
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
}
/**
* Transfer the Settings of the given Class to a new One
*
* @param mixed $sourceClass
* @param mixed $targetClass
* @return bool
*/
public function transferSettings($sourceClass, $targetClass) {
$sourceClass = $this->getClass($sourceClass);
$targetClass = $this->getClass($targetClass);
var_dump($sourceClass, $targetClass);
$mysqli = $this->maniaControl->database->mysqli;
$query = "INSERT INTO `" . SettingManager::TABLE_SETTINGS . "` (`class`, `setting`, `type`, `value`, `default`)
SELECT ?, `setting`, `type`, `value`, `default` FROM `" . SettingManager::TABLE_SETTINGS . "` WHERE `class` = ?;";
$statement = $mysqli->prepare($query);
if ($mysqli->error) {
trigger_error($mysqli->error);
return false;
}
$statement->bind_param('ss', $targetClass, $sourceClass);
if ($statement->error) {
trigger_error($statement->error);
$statement->close();
return false;
}
$success = $statement->execute();
$statement->close();
return $success;
}
/**
* Get the Class of the given Object
*
* @param mixed $class
* @return string
*/
private function getClass($class) {
if (is_object($class)) {
return get_class($class);
}
return (string) $class;
}
}