TrackManiaControl/core/Settings/SettingManager.php

719 lines
20 KiB
PHP
Raw Permalink Normal View History

<?php
2013-12-09 11:25:30 +01:00
2014-01-27 09:07:43 +01:00
namespace ManiaControl\Settings;
use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\Callbacks;
use ManiaControl\General\UsageInformationAble;
use ManiaControl\General\UsageInformationTrait;
2014-02-12 21:12:51 +01:00
use ManiaControl\ManiaControl;
2014-04-30 03:45:54 +02:00
use ManiaControl\Plugins\PluginManager;
use ManiaControl\Utils\ClassUtil;
/**
* Class managing ManiaControl Settings and Configurations
2014-05-02 17:50:30 +02:00
*
* @author ManiaControl Team <mail@maniacontrol.com>
2020-01-22 10:39:35 +01:00
* @copyright 2014-2020 ManiaControl Team
2014-05-02 17:50:30 +02:00
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/
class SettingManager implements CallbackListener, UsageInformationAble {
use UsageInformationTrait;
/*
* Constants
*/
const TABLE_SETTINGS = 'mc_settings';
const CB_SETTING_CHANGED = 'SettingManager.SettingChanged';
const SETTING_ALLOW_UNLINK_SERVER = 'Allow to unlink settings with multiple servers';
const SETTING_DELETE_UNUSED_SETTING_AT_START = 'Delete unused settings at ManiaControl start';
2023-09-07 18:16:25 +02:00
const SETTING_DISABLE_SETTING_CACHE = 'Disable settings cache';
2014-05-02 17:50:30 +02:00
/*
* Private properties
*/
2014-07-25 16:39:07 +02:00
/** @var ManiaControl $maniaControl */
private $maniaControl = null;
2014-07-25 16:39:07 +02:00
/** @var Setting[] $storedSettings */
2014-04-20 19:25:06 +02:00
private $storedSettings = array();
2023-09-07 18:16:25 +02:00
/** @var bool $disableCache */
private $disableCache = false;
/**
* Construct a new setting manager instance
2014-05-02 17:50:30 +02:00
*
2014-01-05 22:51:21 +01:00
* @param ManiaControl $maniaControl
*/
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
$this->initTables();
2014-02-12 21:12:51 +01:00
// Callbacks
2014-08-13 11:05:52 +02:00
$this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::AFTERINIT, $this, 'handleAfterInit');
$this->initSetting($this, self::SETTING_ALLOW_UNLINK_SERVER, false);
$this->initSetting($this, self::SETTING_DELETE_UNUSED_SETTING_AT_START, true);
2023-09-07 18:16:25 +02:00
$this->initSetting($this, self::SETTING_DISABLE_SETTING_CACHE, false, "only for not linked settings");
}
/**
* Initialize the necessary database tables
2014-05-02 17:50:30 +02:00
*
* @return bool
*/
private function initTables() {
2014-08-13 11:05:52 +02:00
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
2014-04-20 19:25:06 +02:00
$settingTableQuery = "CREATE TABLE IF NOT EXISTS `" . self::TABLE_SETTINGS . "` (
`index` INT(11) NOT NULL AUTO_INCREMENT,
`class` VARCHAR(100) NOT NULL,
`setting` VARCHAR(150) NOT NULL,
`type` VARCHAR(50) NOT NULL,
2017-02-04 11:41:31 +01:00
`value` VARCHAR(150) NOT NULL,
`default` VARCHAR(100) NOT NULL,
`set` VARCHAR(100) NOT NULL,
`changed` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`index`),
UNIQUE KEY `settingId` (`class`,`setting`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Settings and Configurations' AUTO_INCREMENT=1;";
2014-06-22 18:26:01 +02:00
$result = $mysqli->query($settingTableQuery);
2014-01-27 09:07:43 +01:00
if ($mysqli->error) {
trigger_error($mysqli->error, E_USER_ERROR);
}
2020-05-19 22:07:18 +02:00
$mysqli->query("ALTER TABLE `" . self::TABLE_SETTINGS . "` ADD `description` VARCHAR(500) DEFAULT NULL;");
if ($mysqli->error) {
// If not Duplicate
if ($mysqli->errno !== 1060) {
trigger_error($mysqli->error, E_USER_ERROR);
}
}
2020-08-02 10:32:27 +02:00
// Grow the size limit for plugins settings
2021-06-30 16:19:31 +02:00
$mysqli->query("ALTER TABLE `" . self::TABLE_SETTINGS . "` MODIFY `value` VARCHAR(1000);");
2020-08-02 10:32:27 +02:00
if ($mysqli->error) {
// If not Duplicate
if ($mysqli->errno !== 1060) {
trigger_error($mysqli->error, E_USER_ERROR);
}
}
2022-03-17 21:19:31 +01:00
// Add priority value
$mysqli->query("ALTER TABLE `" . self::TABLE_SETTINGS . "` ADD `priority` INT(5) DEFAULT 100;");
if ($mysqli->error) {
// If not Duplicate
if ($mysqli->errno !== 1060) {
trigger_error($mysqli->error, E_USER_ERROR);
}
}
// Add link status
$mysqli->query("ALTER TABLE `" . self::TABLE_SETTINGS . "` ADD COLUMN `linked` TINYINT(1) DEFAULT 1 AFTER `type`,
ADD COLUMN `serverIndex` INT(11) DEFAULT 0 AFTER `linked`,
DROP INDEX `settingId`,
ADD UNIQUE KEY `settingId` (`class`,`setting`,`serverIndex`);");
if ($mysqli->error) {
// If not Duplicate
if ($mysqli->errno !== 1060) {
trigger_error($mysqli->error, E_USER_ERROR);
}
}
2014-06-22 18:26:01 +02:00
return $result;
}
2014-05-02 17:50:30 +02:00
/**
* Handle After Init Callback
*/
public function handleAfterInit() {
2023-09-08 21:23:47 +02:00
$this->disableCache = boolval($this->getSettingValue($this, self::SETTING_DISABLE_SETTING_CACHE));
if ($this->disableCache) $this->clearStorage();
2014-05-02 17:50:30 +02:00
$this->deleteUnusedSettings();
}
/**
* Delete all unused Settings that haven't been initialized during the current Startup
*
* @return bool
*/
private function deleteUnusedSettings() {
2022-04-27 09:52:53 +02:00
if (!$this->getSettingValue($this, self::SETTING_DELETE_UNUSED_SETTING_AT_START)) {
return;
}
2014-08-13 11:05:52 +02:00
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
$settingStatement = $mysqli->prepare("DELETE FROM `" . self::TABLE_SETTINGS . "`
2022-04-27 09:26:43 +02:00
WHERE ((`linked` = 0 AND `serverIndex` = ?) OR `linked` = 1) AND `changed` < NOW() - INTERVAL 1 HOUR;");
2014-05-02 17:50:30 +02:00
if ($mysqli->error) {
trigger_error($mysqli->error);
return null;
2014-05-02 17:50:30 +02:00
}
$serverInfo = $this->maniaControl->getServer();
if ($serverInfo === null) {
return;
} else {
$serverIndex = $serverInfo->index;
}
$settingStatement->bind_param('i', $serverIndex);
if (!$settingStatement->execute()) {
trigger_error('Error executing MySQL query: ' . $settingStatement->error);
}
$result = $settingStatement->get_result();
2014-05-13 14:15:00 +02:00
if ($result) {
$this->clearStorage();
2014-05-13 14:15:00 +02:00
return true;
2014-05-02 17:50:30 +02:00
}
2014-05-13 14:15:00 +02:00
return false;
2014-05-02 17:50:30 +02:00
}
/**
* Clear the Settings Storage
*/
public function clearStorage() {
$this->storedSettings = array();
}
/**
* @deprecated
* @see SettingManager::getSettingValueByIndex()
*/
public function getSettingByIndex($settingIndex, $defaultValue = null) {
return $this->getSettingValueByIndex($settingIndex, $defaultValue);
}
/**
* Get a Setting Value by its Index
2014-05-02 17:50:30 +02:00
*
2014-05-13 14:15:00 +02:00
* @param int $settingIndex
* @param mixed $defaultValue
* @return mixed
*/
public function getSettingValueByIndex($settingIndex, $defaultValue = null) {
$setting = $this->getSettingObjectByIndex($settingIndex);
if (!$setting) {
return $defaultValue;
}
return $setting->value;
}
/**
* Get a Setting Object by its Index
*
* @param int $settingIndex
2014-05-13 14:15:00 +02:00
* @return Setting
2014-05-02 17:50:30 +02:00
*/
public function getSettingObjectByIndex($settingIndex) {
2014-08-13 11:05:52 +02:00
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
2014-05-13 14:15:00 +02:00
$settingQuery = "SELECT * FROM `" . self::TABLE_SETTINGS . "`
WHERE `index` = {$settingIndex};";
$result = $mysqli->query($settingQuery);
2014-05-02 17:50:30 +02:00
if ($mysqli->error) {
trigger_error($mysqli->error);
2014-05-13 14:15:00 +02:00
return null;
2014-05-02 17:50:30 +02:00
}
2014-05-13 14:15:00 +02:00
if ($result->num_rows <= 0) {
$result->free();
return null;
2014-05-02 17:50:30 +02:00
}
2014-05-13 14:15:00 +02:00
/** @var Setting $setting */
$setting = $result->fetch_object(Setting::CLASS_NAME, array(false, null, null));
$result->free();
$this->storeSetting($setting);
2013-11-10 15:11:50 +01:00
2014-05-13 14:15:00 +02:00
return $setting;
}
/**
* Store the given Setting
*
* @param Setting $setting
*/
private function storeSetting(Setting $setting) {
2023-09-07 18:16:25 +02:00
if ($this->disableCache && $setting->linked) return;
$this->storedSettings[$setting->class . $setting->setting] = $setting;
}
2013-11-10 13:23:35 +01:00
/**
2014-05-13 14:15:00 +02:00
* Set a Setting for the given Object
2014-05-02 17:50:30 +02:00
*
2020-05-19 22:07:18 +02:00
* @param mixed $object
* @param string $settingName
* @param mixed $value
* @param string|null $description
* @return bool
2013-11-10 13:23:35 +01:00
*/
2020-05-19 22:07:18 +02:00
public function setSetting($object, $settingName, $value, $description = null) {
//TODO nowhere used, everywhere saveSettings used, is it depreciated?
$setting = $this->getSettingObject($object, $settingName);
2014-05-13 14:15:00 +02:00
if ($setting) {
$setting->value = $value;
$saved = $this->saveSetting($setting);
if (!$saved) {
return false;
2014-05-13 14:15:00 +02:00
}
} else {
2020-05-19 22:07:18 +02:00
$saved = $this->initSetting($object, $settingName, $value, $description);
if (!$saved) {
return false;
}
$setting = $this->getSettingObject($object, $settingName, $value);
2014-01-05 22:51:21 +01:00
}
2014-05-13 14:15:00 +02:00
$this->storeSetting($setting);
2014-05-13 14:15:00 +02:00
return true;
2014-01-05 22:51:21 +01:00
}
/**
* Get Setting by Name for the given Object
2014-05-02 17:50:30 +02:00
*
* @param mixed $object
* @param string $settingName
2014-05-02 17:50:30 +02:00
* @param mixed $default
2014-05-13 14:15:00 +02:00
* @return Setting
2014-01-05 22:51:21 +01:00
*/
public function getSettingObject($object, $settingName, $default = null) {
2014-05-13 14:15:00 +02:00
$settingClass = ClassUtil::getClass($object);
2014-05-02 17:50:30 +02:00
2014-05-13 14:15:00 +02:00
// Retrieve from Storage if possible
$storedSetting = $this->getStoredSetting($object, $settingName);
if ($storedSetting) {
return $storedSetting;
2014-04-20 19:25:06 +02:00
}
2014-05-02 17:50:30 +02:00
2014-05-13 14:15:00 +02:00
// Fetch setting
2014-08-13 11:05:52 +02:00
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
$settingStatement = $mysqli->prepare("SELECT * FROM `" . self::TABLE_SETTINGS . "`
WHERE `class` = ?
AND `setting` = ?
AND (`serverIndex` = ? OR `serverIndex` = 0) ORDER BY `serverIndex` DESC ;");
2014-01-27 09:07:43 +01:00
if ($mysqli->error) {
trigger_error($mysqli->error);
return null;
}
$serverInfo = $this->maniaControl->getServer();
if ($serverInfo == null) {
$serverIndex = "";
} else {
$serverIndex = $serverInfo->index;
}
$settingStatement->bind_param('ssi', $settingClass, $settingName, $serverIndex);
if (!$settingStatement->execute()) {
trigger_error('Error executing MySQL query: ' . $settingStatement->error);
}
$result = $settingStatement->get_result();
2014-05-13 14:15:00 +02:00
if ($result->num_rows <= 0) {
$result->free();
2014-05-13 14:15:00 +02:00
if ($default === null) {
return null;
}
$saved = $this->initSetting($object, $settingName, $default);
if ($saved) {
return $this->getSettingObject($object, $settingName, $default);
} else {
return null;
}
}
2014-05-02 17:50:30 +02:00
2014-05-13 14:15:00 +02:00
/** @var Setting $setting */
$setting = $result->fetch_object(Setting::CLASS_NAME, array(false, null, null));
2014-05-13 14:15:00 +02:00
$result->free();
$this->storeSetting($setting);
2014-05-13 14:15:00 +02:00
return $setting;
}
2013-11-10 13:23:35 +01:00
/**
* Retrieve a stored Setting
*
* @param mixed $settingClass
* @param string $settingName
* @return Setting
*/
private function getStoredSetting($settingClass, $settingName) {
$settingClass = ClassUtil::getClass($settingClass);
if (isset($this->storedSettings[$settingClass . $settingName])) {
return $this->storedSettings[$settingClass . $settingName];
}
return null;
}
2014-01-05 22:51:21 +01:00
/**
2014-05-13 14:15:00 +02:00
* Initialize a Setting for the given Object
2014-05-02 17:50:30 +02:00
*
2020-05-19 22:07:18 +02:00
* @param mixed $object
* @param string $settingName
* @param mixed $defaultValue
* @param string|null $description
* @return bool
2013-11-10 13:23:35 +01:00
*/
2022-03-17 21:19:31 +01:00
public function initSetting($object, $settingName, $defaultValue, $description = null, $priority = 100) {
$setting = new Setting($object, $settingName, $defaultValue, $description, $priority);
return $this->saveSetting($setting, true);
2014-05-13 14:15:00 +02:00
}
2014-05-02 17:50:30 +02:00
2014-05-13 14:15:00 +02:00
/**
* Save the given Setting in the Database
*
* @param Setting $setting
* @param bool $init
2014-05-13 14:15:00 +02:00
* @return bool
*/
public function saveSetting(Setting $setting, $init = false) {
2014-08-13 11:05:52 +02:00
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
if ($init) {
$existingsetting = $this->getSettingObject($setting->class, $setting->setting);
if ($existingsetting !== null && !$existingsetting->linked){
$setting->linked = false;
}
// Init - Keep old value if the default didn't change
$valueUpdateString = '`value` = IF(`default` = VALUES(`default`), `value`, VALUES(`default`)),
`serverIndex` = IF(`serverIndex` IS NULL, 0, `serverIndex`)';
} else {
// Set - Update value in any case
$valueUpdateString = '`value` = VALUES(`value`), `serverIndex` = VALUES(`serverIndex`)';
}
2014-05-13 14:15:00 +02:00
$settingQuery = "INSERT INTO `" . self::TABLE_SETTINGS . "` (
`class`,
`setting`,
`type`,
2020-05-19 22:07:18 +02:00
`description`,
2014-05-13 14:15:00 +02:00
`value`,
`serverIndex`,
`linked`,
2014-05-18 21:46:00 +02:00
`default`,
2022-03-17 21:19:31 +01:00
`set`,
`priority`
2014-05-13 14:15:00 +02:00
) VALUES (
?, ?, ?, ?, ?, ?, ?, ?, ?, ?
2014-05-13 14:15:00 +02:00
) ON DUPLICATE KEY UPDATE
`index` = LAST_INSERT_ID(`index`),
`type` = VALUES(`type`),
{$valueUpdateString},
`linked` = VALUES(`linked`),
2014-05-13 14:15:00 +02:00
`default` = VALUES(`default`),
2014-05-18 21:46:00 +02:00
`set` = VALUES(`set`),
2020-05-19 22:07:18 +02:00
`description` = VALUES(`description`),
2022-03-17 21:19:31 +01:00
`priority` = VALUES(`priority`),
2014-05-13 14:15:00 +02:00
`changed` = NOW();";
2013-11-10 13:23:35 +01:00
$settingStatement = $mysqli->prepare($settingQuery);
2014-01-27 09:07:43 +01:00
if ($mysqli->error) {
2013-11-10 13:23:35 +01:00
trigger_error($mysqli->error);
return false;
}
2014-05-18 21:46:00 +02:00
$formattedValue = $setting->getFormattedValue();
$formattedDefault = $setting->getFormattedDefault();
$formattedSet = $setting->getFormattedSet();
$serverInfo = $this->maniaControl->getServer();
if ($setting->linked || $serverInfo == null || $serverInfo->index == null) {
$serverIndex = 0;
} else {
$serverIndex = $serverInfo->index;
}
2020-05-19 22:07:18 +02:00
$settingStatement->bind_param(
'sssssiissi',
2020-05-19 22:07:18 +02:00
$setting->class,
$setting->setting,
$setting->type,
$setting->description,
$formattedValue,
$serverIndex,
$setting->linked,
2020-05-19 22:07:18 +02:00
$formattedDefault,
2022-03-17 21:19:31 +01:00
$formattedSet,
$setting->priority
2020-05-19 22:07:18 +02:00
);
2014-05-13 14:15:00 +02:00
$settingStatement->execute();
2014-01-27 09:07:43 +01:00
if ($settingStatement->error) {
2013-11-10 13:23:35 +01:00
trigger_error($settingStatement->error);
$settingStatement->close();
2013-11-10 13:23:35 +01:00
return false;
}
$settingStatement->close();
// Trigger Settings Changed Callback
if (!$init) {
2014-08-13 11:05:52 +02:00
$this->maniaControl->getCallbackManager()->triggerCallback(self::CB_SETTING_CHANGED, $setting);
2023-09-07 18:16:25 +02:00
2023-09-08 21:23:47 +02:00
// during the init, value = default
if ($setting->setting === self::SETTING_DISABLE_SETTING_CACHE) {
$this->disableCache = boolval($setting->value);
if ($this->disableCache) {
$this->clearStorage();
}
2023-09-07 18:16:25 +02:00
}
}
2014-05-13 14:15:00 +02:00
return true;
2013-11-10 13:23:35 +01:00
}
2013-11-10 15:11:50 +01:00
/**
* @deprecated
* @see SettingManager::getSettingValue()
*/
public function getSetting($object, $settingName, $default = null) {
return $this->getSettingValue($object, $settingName, $default);
}
2014-05-13 16:03:26 +02:00
/**
* Get the Setting Value directly
*
* @param mixed $object
* @param string $settingName
* @param mixed $default
* @return mixed
*/
public function getSettingValue($object, $settingName, $default = null) {
$setting = $this->getSettingObject($object, $settingName, $default);
2014-05-13 16:03:26 +02:00
if ($setting) {
return $setting->value;
}
return null;
}
/**
* setSettingUnlinked
*
* @param mixed $object
* @param string $settingName
* @return void
*/
public function setSettingUnlinked($object, $settingName = "") {
if ($object instanceof Setting) {
$settingClass = $object->class;
$settingName = $object->setting;
} else {
$settingClass = ClassUtil::getClass($object);
}
// Fetch setting
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
$settingStatement = $mysqli->prepare("UPDATE `" . self::TABLE_SETTINGS . "`
SET `linked` = 0, `changed` = NOW()
WHERE `class` = ?
AND `setting` = ?");
if ($mysqli->error) {
trigger_error($mysqli->error);
return null;
}
$settingStatement->bind_param('ss', $settingClass, $settingName);
if (!$settingStatement->execute()) {
trigger_error('Error executing MySQL query: ' . $settingStatement->error);
}
$result = $settingStatement->get_result();
if (isset($this->storedSettings[$settingClass . $settingName])) {
unset($this->storedSettings[$settingClass . $settingName]);
}
return $result;
}
2014-05-13 16:03:26 +02:00
2014-05-02 17:50:30 +02:00
/**
2014-05-13 14:15:00 +02:00
* Reset a Setting to its Default Value
2014-05-02 17:50:30 +02:00
*
* @param mixed $object
* @param string $settingName
2013-11-10 15:11:50 +01:00
* @return bool
*/
2014-05-13 14:15:00 +02:00
public function resetSetting($object, $settingName = null) {
if ($object instanceof Setting) {
$settingClass = $object->class;
2014-05-13 14:15:00 +02:00
$settingName = $object->setting;
} else {
$settingClass = ClassUtil::getClass($object);
2014-05-13 14:15:00 +02:00
}
2014-08-13 11:05:52 +02:00
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
$settingStatement = $mysqli->prepare("UPDATE `" . self::TABLE_SETTINGS . "`
2013-11-10 15:11:50 +01:00
SET `value` = `default`
WHERE `class` = ?
AND `setting` = ?
AND (`serverIndex` = ? OR `serverIndex` = 0) ORDER BY `serverIndex` DESC LIMIT 1;"); // TODO : by server if linked or not
2014-01-27 09:07:43 +01:00
if ($mysqli->error) {
2013-11-10 15:11:50 +01:00
trigger_error($mysqli->error);
return null;
2013-11-10 15:11:50 +01:00
}
$serverInfo = $this->maniaControl->getServer();
if ($serverInfo == null) {
$serverIndex = 0;
} else {
$serverIndex = $serverInfo->index;
}
$settingStatement->bind_param('ssi', $settingClass, $settingName, $serverIndex);
if (!$settingStatement->execute()) {
trigger_error('Error executing MySQL query: ' . $settingStatement->error);
}
$result = $settingStatement->get_result();
if (isset($this->storedSettings[$settingClass . $settingName])) {
unset($this->storedSettings[$settingClass . $settingName]);
}
2014-05-13 14:15:00 +02:00
return $result;
2013-12-09 10:19:29 +01:00
}
/**
* Delete a Setting
2014-05-02 17:50:30 +02:00
*
* @param mixed $object
* @param string $settingName
2013-12-09 10:19:29 +01:00
* @return bool
*/
2014-05-13 14:15:00 +02:00
public function deleteSetting($object, $settingName = null) {
if ($object instanceof Setting) {
$className = $object->class;
$settingName = $object->setting;
} else {
$className = ClassUtil::getClass($object);
}
2014-08-13 11:05:52 +02:00
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
2014-05-13 14:15:00 +02:00
$settingQuery = "DELETE FROM `" . self::TABLE_SETTINGS . "`
WHERE `class` = '" . $mysqli->escape_string($className) . "'
AND `setting` = '" . $mysqli->escape_string($settingName) . "';";
$result = $mysqli->query($settingQuery);
2014-01-27 09:07:43 +01:00
if ($mysqli->error) {
2013-12-09 10:19:29 +01:00
trigger_error($mysqli->error);
return false;
}
2014-05-13 14:15:00 +02:00
if (isset($this->storedSettings[$className . $settingName])) {
unset($this->storedSettings[$className . $settingName]);
}
2014-05-13 14:15:00 +02:00
return $result;
2013-11-10 15:11:50 +01:00
}
/**
* Delete unlinked settings
*
* @param mixed $object
* @param string $settingName
* @return bool
*/
public function deleteSettingUnlinked($object, $settingName = null) {
if ($object instanceof Setting) {
$settingClass = $object->class;
$settingName = $object->setting;
} else {
$settingClass = ClassUtil::getClass($object);
}
// Fetch setting
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
$settingStatement = $mysqli->prepare("DELETE FROM `" . self::TABLE_SETTINGS . "`
WHERE `class` = ?
AND `setting` = ?
AND `serverIndex` != 0");
if ($mysqli->error) {
trigger_error($mysqli->error);
return null;
}
$settingStatement->bind_param('ss', $settingClass, $settingName);
if (!$settingStatement->execute()) {
trigger_error('Error executing MySQL query: ' . $settingStatement->error);
}
$result = $settingStatement->get_result();
if (isset($this->storedSettings[$settingClass . $settingName])) {
unset($this->storedSettings[$settingClass . $settingName]);
}
return $result;
}
2014-01-18 20:05:50 +01:00
/**
* Get all Settings for the given Class
2014-05-02 17:50:30 +02:00
*
* @param mixed $object
2014-05-27 23:00:39 +02:00
* @return Setting[]
2014-01-18 20:05:50 +01:00
*/
public function getSettingsByClass($object) {
2014-05-13 14:15:00 +02:00
$className = ClassUtil::getClass($object);
2014-08-13 11:05:52 +02:00
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
// LIMIT is required to keep unlinked setting
$settingStatement = $mysqli->prepare("SELECT * FROM (SELECT * FROM `" . self::TABLE_SETTINGS . "`
WHERE class = ? AND (`serverIndex` = ? OR `serverIndex` = 0)
2022-04-13 10:46:42 +02:00
ORDER BY `serverIndex` DESC
LIMIT 9999999)
2022-04-13 10:46:42 +02:00
as t GROUP BY `setting` ORDER BY `priority` ASC, `setting`;");
2014-01-27 09:07:43 +01:00
if ($mysqli->error) {
2014-01-18 20:05:50 +01:00
trigger_error($mysqli->error);
return null;
}
$serverInfo = $this->maniaControl->getServer();
if ($serverInfo == null) {
$serverIndex = 0;
} else {
$serverIndex = $serverInfo->index;
}
$settingStatement->bind_param('si', $className, $serverIndex);
if (!$settingStatement->execute()) {
trigger_error('Error executing MySQL query: ' . $settingStatement->error);
}
$result = $settingStatement->get_result();
2014-01-18 20:05:50 +01:00
$settings = array();
while ($setting = $result->fetch_object(Setting::CLASS_NAME, array(false, null, null))) {
2014-01-18 20:05:50 +01:00
$settings[$setting->index] = $setting;
}
$result->free();
return $settings;
}
/**
2014-05-13 14:15:00 +02:00
* Get all Settings
* CAREFUL: could have multiple time the same setting if in unlinked mode
2014-05-02 17:50:30 +02:00
*
2014-05-27 23:00:39 +02:00
* @return Setting[]
*/
public function getSettings() {
2014-08-13 11:05:52 +02:00
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
2014-05-02 17:50:30 +02:00
$query = "SELECT * FROM `" . self::TABLE_SETTINGS . "`
2022-03-17 21:19:31 +01:00
ORDER BY `class` ASC, `priority` ASC, `setting` ASC;";
$result = $mysqli->query($query);
2014-01-27 09:07:43 +01:00
if ($mysqli->error) {
trigger_error($mysqli->error);
return null;
}
$settings = array();
while ($setting = $result->fetch_object(Setting::CLASS_NAME, array(false, null, null))) {
$settings[$setting->index] = $setting;
}
$result->free();
return $settings;
}
2014-02-12 21:12:51 +01:00
2014-04-30 03:45:54 +02:00
/**
* Get all Setting Classes
*
* @param bool $hidePluginClasses
2014-05-27 23:00:39 +02:00
* @return string[]
2014-04-30 03:45:54 +02:00
*/
public function getSettingClasses($hidePluginClasses = false) {
2014-08-13 11:05:52 +02:00
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
2014-05-02 17:50:30 +02:00
$query = "SELECT DISTINCT `class` FROM `" . self::TABLE_SETTINGS . "`
2014-04-30 03:45:54 +02:00
ORDER BY `class` ASC;";
$result = $mysqli->query($query);
if ($mysqli->error) {
trigger_error($mysqli->error);
return null;
}
$settingClasses = array();
2014-05-13 14:15:00 +02:00
while ($row = $result->fetch_object()) {
if (!$hidePluginClasses || !PluginManager::isPluginClass($row->class)) {
array_push($settingClasses, $row->class);
2014-04-30 03:45:54 +02:00
}
}
$result->free();
return $settingClasses;
}
}