setting manager deletes old unused settings

This commit is contained in:
Steffen Schröder 2014-02-12 21:07:54 +01:00
parent 804eaf1609
commit 5619596659
3 changed files with 99 additions and 79 deletions

View File

@ -15,6 +15,7 @@ class CallbackManager {
*/ */
// ManiaControl callbacks // ManiaControl callbacks
const CB_MC_ONINIT = 'ManiaControl.OnInit'; const CB_MC_ONINIT = 'ManiaControl.OnInit';
const CB_MC_AFTERINIT = 'ManiaControl.AfterInit';
const CB_MC_ONSHUTDOWN = 'ManiaControl.OnShutdown'; const CB_MC_ONSHUTDOWN = 'ManiaControl.OnShutdown';
const CB_MC_CLIENTUPDATED = 'ManiaControl.ClientUpdated'; const CB_MC_CLIENTUPDATED = 'ManiaControl.ClientUpdated';
const CB_MC_BEGINMAP = 'ManiaControl.BeginMap'; const CB_MC_BEGINMAP = 'ManiaControl.BeginMap';

View File

@ -276,6 +276,9 @@ class ManiaControl implements CommandListener {
// Load plugins // Load plugins
$this->pluginManager->loadPlugins(); $this->pluginManager->loadPlugins();
// AfterInit callback
$this->callbackManager->triggerCallback(CallbackManager::CB_MC_AFTERINIT, array(CallbackManager::CB_MC_AFTERINIT));
// Announce ManiaControl // Announce ManiaControl
$this->chat->sendInformation('ManiaControl v' . self::VERSION . ' successfully started!'); $this->chat->sendInformation('ManiaControl v' . self::VERSION . ' successfully started!');

View File

@ -3,13 +3,15 @@
namespace ManiaControl\Settings; namespace ManiaControl\Settings;
use ManiaControl\ManiaControl; use ManiaControl\ManiaControl;
use ManiaControl\Callbacks\CallbackManager;
use ManiaControl\Callbacks\CallbackListener;
/** /**
* Class managing settings and configurations * Class managing Settings and Configurations
* *
* @author steeffeen & kremsy * @author steeffeen & kremsy
*/ */
class SettingManager { class SettingManager implements CallbackListener {
/** /**
* Constants * Constants
*/ */
@ -28,28 +30,39 @@ class SettingManager {
private $arrayDelimiter = ';;'; private $arrayDelimiter = ';;';
/** /**
* Construct setting manager * Construct a new Setting Manager
* *
* @param ManiaControl $maniaControl * @param ManiaControl $maniaControl
*/ */
public function __construct(ManiaControl $maniaControl) { public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl; $this->maniaControl = $maniaControl;
$this->initTables(); $this->initTables();
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MC_AFTERINIT, $this, 'handleAfterInit');
} }
/** /**
* Initialize necessary database tables * Handle After Init Callback
*
* @param array $callback
*/
public function handleAfterInit(array $callback) {
$this->deleteUnusedSettings();
}
/**
* Initialize necessary Database Tables
* *
* @return bool * @return bool
*/ */
private function initTables() { private function initTables() {
$mysqli = $this->maniaControl->database->mysqli; $mysqli = $this->maniaControl->database->mysqli;
$defaultType = "'" . self::TYPE_STRING . "'"; $defaultType = "'" . self::TYPE_STRING . "'";
$typeSet = $defaultType; $typeSet = $defaultType . ",'"
$typeSet .= ",'" . self::TYPE_INT . "'"; . self::TYPE_INT . "'" . ",'"
$typeSet .= ",'" . self::TYPE_REAL . "'"; . self::TYPE_REAL . "'" . ",'"
$typeSet .= ",'" . self::TYPE_BOOL . "'"; . self::TYPE_BOOL . "'" . ",'"
$typeSet .= ",'" . self::TYPE_ARRAY . "'"; . self::TYPE_ARRAY . "'";
$settingTableQuery = "CREATE TABLE IF NOT EXISTS `" . self::TABLE_SETTINGS . "` ( $settingTableQuery = "CREATE TABLE IF NOT EXISTS `" . self::TABLE_SETTINGS . "` (
`index` int(11) NOT NULL AUTO_INCREMENT, `index` int(11) NOT NULL AUTO_INCREMENT,
`class` varchar(50) COLLATE utf8_unicode_ci NOT NULL, `class` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
@ -66,34 +79,34 @@ class SettingManager {
trigger_error($mysqli->error, E_USER_ERROR); trigger_error($mysqli->error, E_USER_ERROR);
return false; return false;
} }
$settingTableStatement->execute(); $success = $settingTableStatement->execute();
if ($settingTableStatement->error) { if ($settingTableStatement->error) {
trigger_error($settingTableStatement->error, E_USER_ERROR); trigger_error($settingTableStatement->error, E_USER_ERROR);
return false; return false;
} }
$settingTableStatement->close(); $settingTableStatement->close();
return true; return $success;
} }
/** /**
* Get class name from param * Get Class Name of a Parameter
* *
* @param mixed $object * @param mixed $param
* @return string * @return string
*/ */
private function getClassName($object) { private function getClassName($param) {
if (is_object($object)) { if (is_object($param)) {
return get_class($object); return get_class($param);
} }
if (is_string($object)) { if (is_string($param)) {
return $object; return $param;
} }
trigger_error('Invalid class param. ' . $object); trigger_error('Invalid class param. ' . $param);
return (string)$object; return (string) $param;
} }
/** /**
* Get type for given parameter * Get Type of a Parameter
* *
* @param mixed $param * @param mixed $param
* @return string * @return string
@ -119,7 +132,7 @@ class SettingManager {
} }
/** /**
* Cast a setting to the given type * Cast a Setting to the given Type
* *
* @param string $type * @param string $type
* @param mixed $value * @param mixed $value
@ -146,7 +159,7 @@ class SettingManager {
} }
/** /**
* Format a setting for saving it to the database * Format a Setting for saving it to the Database
* *
* @param mixed $value * @param mixed $value
* @param string $type * @param string $type
@ -166,9 +179,9 @@ class SettingManager {
} }
/** /**
* Initialize a setting for given object * Initialize a Setting for the given Object
* *
* @param object $object * @param mixed $object
* @param string $settingName * @param string $settingName
* @param mixed $default * @param mixed $default
* @return bool * @return bool
@ -194,7 +207,8 @@ class SettingManager {
) ON DUPLICATE KEY UPDATE ) ON DUPLICATE KEY UPDATE
`type` = VALUES(`type`), `type` = VALUES(`type`),
`value` = IF(`default` = VALUES(`default`), `value`, VALUES(`default`)), `value` = IF(`default` = VALUES(`default`), `value`, VALUES(`default`)),
`default` = VALUES(`default`);"; `default` = VALUES(`default`),
`changed` = NOW();";
$settingStatement = $mysqli->prepare($settingQuery); $settingStatement = $mysqli->prepare($settingQuery);
if ($mysqli->error) { if ($mysqli->error) {
trigger_error($mysqli->error); trigger_error($mysqli->error);
@ -212,14 +226,11 @@ class SettingManager {
} }
/** /**
* Get a Setting by its index * Get a Setting by its Index
* *
* @param $settingIndex * @param int $settingIndex
* @param bool $default * @param mixed $default
* @internal param null $default * @return mixed
* @internal param $className
* @internal param \ManiaControl\gIndex $settin
* @return mixed null
*/ */
public function getSettingByIndex($settingIndex, $default = false) { public function getSettingByIndex($settingIndex, $default = false) {
$mysqli = $this->maniaControl->database->mysqli; $mysqli = $this->maniaControl->database->mysqli;
@ -228,22 +239,27 @@ class SettingManager {
$result = $mysqli->query($settingQuery); $result = $mysqli->query($settingQuery);
if (!$result) { if (!$result) {
trigger_error($mysqli->error); trigger_error($mysqli->error);
return false; return null;
}
if ($result->num_rows <= 0) {
$result->close();
return $default;
} }
$row = $result->fetch_object(); $row = $result->fetch_object();
$result->close(); $result->close();
return $row; return $row;
} }
/** /**
* Gets a Setting Via it's class name * Get Setting by Name for the given Object
* *
* @param $className * @param mixed $object
* @param $settingName * @param string $settingName
* @param $value * @param mixed $default
* @return mixed
*/ */
public function getSettingByClassName($className, $settingName, $default = null) { public function getSetting($object, $settingName, $default = null) {
$className = $this->getClassName($object);
$mysqli = $this->maniaControl->database->mysqli; $mysqli = $this->maniaControl->database->mysqli;
$settingQuery = "SELECT `type`, `value` FROM `" . self::TABLE_SETTINGS . "` $settingQuery = "SELECT `type`, `value` FROM `" . self::TABLE_SETTINGS . "`
WHERE `class` = ? WHERE `class` = ?
@ -273,27 +289,15 @@ class SettingManager {
} }
/** /**
* Get setting by name for given object * Set a Setting for the given Object
* *
* @param object $object * @param mixed $object
* @param string $settingName * @param string $settingName
* @param mixed $default * @param mixed $value
* @return mixed
*/
public function getSetting($object, $settingName, $default = null) {
$className = $this->getClassName($object);
return $this->getSettingByClassName($className, $settingName, $default);
}
/**
* Updates a Setting
*
* @param $className
* @param $settingName
* @param $value
* @return bool * @return bool
*/ */
public function updateSetting($className, $settingName, $value) { public function setSetting($object, $settingName, $value) {
$className = $this->getClassName($object);
$mysqli = $this->maniaControl->database->mysqli; $mysqli = $this->maniaControl->database->mysqli;
$settingQuery = "UPDATE `" . self::TABLE_SETTINGS . "` $settingQuery = "UPDATE `" . self::TABLE_SETTINGS . "`
SET `value` = ? SET `value` = ?
@ -320,22 +324,9 @@ class SettingManager {
} }
/** /**
* Set a setting for the given object * Reset a Setting to its default Value
* *
* @param object $object * @param mixed $object
* @param string $settingName
* @param mixed $value
* @return bool
*/
public function setSetting($object, $settingName, $value) {
$className = $this->getClassName($object);
$this->updateSetting($className, $settingName, $value);
}
/**
* Reset a setting to its default value
*
* @param object $object
* @param string $settingName * @param string $settingName
* @return bool * @return bool
*/ */
@ -363,9 +354,9 @@ class SettingManager {
} }
/** /**
* Delete a setting from the database * Delete a Setting
* *
* @param object $object * @param mixed $object
* @param string $settingName * @param string $settingName
* @return bool * @return bool
*/ */
@ -392,12 +383,13 @@ class SettingManager {
} }
/** /**
* Get all settings from a Defined class * Get all Settings for the given Class
* *
* @param $className * @param mixed $object
* @return array * @return array
*/ */
public function getSettingsByClass($className) { public function getSettingsByClass($object) {
$className = $this->getClassName($object);
$mysqli = $this->maniaControl->database->mysqli; $mysqli = $this->maniaControl->database->mysqli;
$query = "SELECT * FROM `" . self::TABLE_SETTINGS . "` WHERE `class`= '" . $mysqli->escape_string($className) . "' $query = "SELECT * FROM `" . self::TABLE_SETTINGS . "` WHERE `class`= '" . $mysqli->escape_string($className) . "'
ORDER BY `setting` ASC;"; ORDER BY `setting` ASC;";
@ -407,7 +399,7 @@ class SettingManager {
return null; return null;
} }
$settings = array(); $settings = array();
while($setting = $result->fetch_object()) { while ($setting = $result->fetch_object()) {
$settings[$setting->index] = $setting; $settings[$setting->index] = $setting;
} }
$result->free(); $result->free();
@ -429,10 +421,34 @@ class SettingManager {
return null; return null;
} }
$settings = array(); $settings = array();
while($setting = $result->fetch_object()) { while ($setting = $result->fetch_object()) {
$settings[$setting->index] = $setting; $settings[$setting->index] = $setting;
} }
$result->free(); $result->free();
return $settings; return $settings;
} }
/**
* Delete all unused Settings that haven't been initialized during the current Startup
*
* @return bool
*/
private function deleteUnusedSettings() {
$mysqli = $this->maniaControl->database->mysqli;
$settingQuery = "DELETE FROM `" . self::TABLE_SETTINGS . "`
WHERE `changed` < NOW() - INTERVAL 1 HOUR;";
$settingStatement = $mysqli->prepare($settingQuery);
if ($mysqli->error) {
trigger_error($mysqli->error);
return false;
}
$success = $settingStatement->execute();
if ($settingStatement->error) {
trigger_error($settingStatement->error);
$settingStatement->close();
return false;
}
$settingStatement->close();
return $success;
}
} }