TrackManiaControl/application/core/Database.php

151 lines
3.4 KiB
PHP
Raw Normal View History

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-09 17:24:03 +01:00
/**
* Private properties
*/
private $maniaControl = null;
2013-11-09 17:24:03 +01:00
private $config = null;
/**
* Construct database connection
*/
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
2013-11-09 17:24:03 +01:00
// Load config
$this->config = FileUtil::loadConfig('database.xml');
2013-11-09 17:24:03 +01:00
// Get mysql server information
$host = $this->config->xpath('host');
$port = $this->config->xpath('port');
$user = $this->config->xpath('user');
$pass = $this->config->xpath('pass');
2013-11-10 17:38:54 +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);
}
$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) {
trigger_error($this->mysqli->connect_error, E_USER_ERROR);
2013-11-09 17:24:03 +01:00
}
$this->mysqli->set_charset("utf8");
$this->initDatabase();
$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)
*
* @return bool
2013-11-09 17:24:03 +01:00
*/
private function initDatabase() {
$dbName = $this->config->xpath('database');
if (!$dbName) {
2013-11-10 17:38:54 +01:00
trigger_error("Invalid database configuration (database).", E_USER_ERROR);
return false;
}
$dbName = (string) $dbName[0];
2013-11-09 17:24:03 +01:00
// Try to connect
$result = $this->mysqli->select_db($dbName);
if ($result) {
return true;
2013-11-09 17:24:03 +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
}
$databaseStatement->bind_param('s', $dbName);
$databaseStatement->execute();
if ($databaseStatement->error) {
trigger_error($databaseStatement->error, E_USER_ERROR);
return false;
2013-11-09 17:24:03 +01:00
}
$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;
2013-11-09 17:24:03 +01:00
}
return true;
2013-11-09 17:24:03 +01:00
}
/**
* Optimize all existing tables
2013-11-09 17:24:03 +01:00
*
* @return bool
2013-11-09 17:24:03 +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) {
trigger_error($this->mysqli->error);
return false;
2013-11-09 17:24:03 +01:00
}
$count = $result->num_rows;
if ($count <= 0) {
return true;
2013-11-09 17:24:03 +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
}
$index++;
2013-11-09 17:24:03 +01:00
}
$optimizeQuery .= ";";
$this->mysqli->query($optimizeQuery);
2013-11-09 17:24:03 +01:00
if ($this->mysqli->error) {
trigger_error($this->mysqli->error);
return false;
2013-11-09 17:24:03 +01:00
}
return true;
2013-11-09 17:24:03 +01:00
}
}