2013-11-09 17:24:03 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace ManiaControl;
|
|
|
|
|
|
|
|
/**
|
2013-11-10 17:38:54 +01:00
|
|
|
* Database connection class
|
2013-11-09 17:24:03 +01:00
|
|
|
*
|
2013-11-10 17:38:54 +01:00
|
|
|
* @author steeffeen & kremsy
|
2013-11-09 17:24:03 +01:00
|
|
|
*/
|
|
|
|
class Database {
|
|
|
|
/**
|
|
|
|
* Public properties
|
|
|
|
*/
|
|
|
|
public $mysqli = null;
|
2013-11-10 02:55:08 +01:00
|
|
|
|
2013-11-09 17:24:03 +01:00
|
|
|
/**
|
|
|
|
* Private properties
|
|
|
|
*/
|
2013-11-10 02:55:08 +01:00
|
|
|
private $maniaControl = null;
|
2013-11-09 17:24:03 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct database connection
|
|
|
|
*/
|
2013-11-10 02:55:08 +01:00
|
|
|
public function __construct(ManiaControl $maniaControl) {
|
|
|
|
$this->maniaControl = $maniaControl;
|
2013-11-09 17:24:03 +01:00
|
|
|
|
|
|
|
// Get mysql server information
|
2014-01-06 14:22:48 +01:00
|
|
|
$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');
|
2013-11-10 17:38:54 +01:00
|
|
|
|
2014-01-06 14:22:48 +01:00
|
|
|
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);
|
2013-11-10 17:38:54 +01:00
|
|
|
|
|
|
|
$host = (string) $host[0];
|
|
|
|
$port = (int) $port[0];
|
|
|
|
$user = (string) $user[0];
|
2013-11-09 17:24:03 +01:00
|
|
|
$pass = (string) $pass[0];
|
|
|
|
|
|
|
|
// Open database connection
|
|
|
|
$this->mysqli = new \mysqli($host, $user, $pass, null, $port);
|
|
|
|
if ($this->mysqli->connect_error) {
|
2013-11-10 02:55:08 +01:00
|
|
|
trigger_error($this->mysqli->connect_error, E_USER_ERROR);
|
2013-11-09 17:24:03 +01:00
|
|
|
}
|
|
|
|
$this->mysqli->set_charset("utf8");
|
|
|
|
|
|
|
|
$this->initDatabase();
|
2013-11-10 02:55:08 +01:00
|
|
|
$this->optimizeTables();
|
2013-11-09 17:24:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destruct database connection
|
|
|
|
*/
|
|
|
|
public function __destruct() {
|
|
|
|
$this->mysqli->close();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Connect to the defined database (create it if needed)
|
2013-11-10 02:55:08 +01:00
|
|
|
*
|
|
|
|
* @return bool
|
2013-11-09 17:24:03 +01:00
|
|
|
*/
|
|
|
|
private function initDatabase() {
|
2014-01-06 14:22:48 +01:00
|
|
|
$dbName = $this->maniaControl->config->database->xpath('db_name');
|
2013-12-09 13:45:58 +01:00
|
|
|
if (!$dbName) {
|
2013-11-10 17:38:54 +01:00
|
|
|
trigger_error("Invalid database configuration (database).", E_USER_ERROR);
|
|
|
|
return false;
|
|
|
|
}
|
2014-01-06 14:22:48 +01:00
|
|
|
$dbName = (string) $dbName[0];
|
2013-11-09 17:24:03 +01:00
|
|
|
|
|
|
|
// Try to connect
|
2013-12-09 13:45:58 +01:00
|
|
|
$result = $this->mysqli->select_db($dbName);
|
2014-01-06 14:22:48 +01:00
|
|
|
if ($result) return true;
|
2013-11-09 17:24:03 +01:00
|
|
|
|
2013-11-10 02:55:08 +01:00
|
|
|
// Create database
|
|
|
|
$databaseQuery = "CREATE DATABASE ?;";
|
|
|
|
$databaseStatement = $this->mysqli->prepare($databaseQuery);
|
|
|
|
if ($this->mysqli->error) {
|
|
|
|
trigger_error($this->mysqli->error, E_USER_ERROR);
|
|
|
|
return false;
|
2013-11-09 17:24:03 +01:00
|
|
|
}
|
2013-12-09 13:45:58 +01:00
|
|
|
$databaseStatement->bind_param('s', $dbName);
|
2013-11-10 02:55:08 +01:00
|
|
|
$databaseStatement->execute();
|
|
|
|
if ($databaseStatement->error) {
|
|
|
|
trigger_error($databaseStatement->error, E_USER_ERROR);
|
|
|
|
return false;
|
2013-11-09 17:24:03 +01:00
|
|
|
}
|
2013-11-10 02:55:08 +01:00
|
|
|
$databaseStatement->close();
|
|
|
|
|
|
|
|
// Connect to new database
|
2013-12-09 13:45:58 +01:00
|
|
|
$this->mysqli->select_db($dbName);
|
2013-11-10 02:55:08 +01:00
|
|
|
if ($this->mysqli->error) {
|
2013-12-09 13:45:58 +01:00
|
|
|
trigger_error("Couldn't select database '{$dbName}'. " . $this->mysqli->error, E_USER_ERROR);
|
2013-11-10 02:55:08 +01:00
|
|
|
return false;
|
2013-11-09 17:24:03 +01:00
|
|
|
}
|
2013-11-10 02:55:08 +01:00
|
|
|
return true;
|
2013-11-09 17:24:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-11-10 02:55:08 +01:00
|
|
|
* Optimize all existing tables
|
2013-11-09 17:24:03 +01:00
|
|
|
*
|
2013-11-10 02:55:08 +01:00
|
|
|
* @return bool
|
2013-11-09 17:24:03 +01:00
|
|
|
*/
|
2013-11-10 02:55:08 +01:00
|
|
|
private function optimizeTables() {
|
|
|
|
$showQuery = "SHOW TABLES;";
|
|
|
|
$result = $this->mysqli->query($showQuery);
|
2013-11-09 17:24:03 +01:00
|
|
|
if ($this->mysqli->error) {
|
2013-11-10 02:55:08 +01:00
|
|
|
trigger_error($this->mysqli->error);
|
|
|
|
return false;
|
2013-11-09 17:24:03 +01:00
|
|
|
}
|
2013-11-10 02:55:08 +01:00
|
|
|
$count = $result->num_rows;
|
|
|
|
if ($count <= 0) {
|
|
|
|
return true;
|
2013-11-09 17:24:03 +01:00
|
|
|
}
|
2013-11-10 02:55:08 +01:00
|
|
|
$optimizeQuery = "OPTIMIZE TABLE ";
|
|
|
|
$index = 0;
|
|
|
|
while ($row = $result->fetch_row()) {
|
|
|
|
$tableName = $row[0];
|
|
|
|
$optimizeQuery .= "`{$tableName}`";
|
|
|
|
if ($index < $count - 1) {
|
|
|
|
$optimizeQuery .= ", ";
|
2013-11-09 17:24:03 +01:00
|
|
|
}
|
2013-11-10 02:55:08 +01:00
|
|
|
$index++;
|
2013-11-09 17:24:03 +01:00
|
|
|
}
|
2013-11-10 02:55:08 +01:00
|
|
|
$optimizeQuery .= ";";
|
|
|
|
$this->mysqli->query($optimizeQuery);
|
2013-11-09 17:24:03 +01:00
|
|
|
if ($this->mysqli->error) {
|
2013-11-10 02:55:08 +01:00
|
|
|
trigger_error($this->mysqli->error);
|
|
|
|
return false;
|
2013-11-09 17:24:03 +01:00
|
|
|
}
|
2013-11-10 02:55:08 +01:00
|
|
|
return true;
|
2013-11-09 17:24:03 +01:00
|
|
|
}
|
|
|
|
}
|