2021-12-14 18:32:47 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2014-04-29 00:12:31 +00:00
|
|
|
|
2023-01-10 22:44:09 +00:00
|
|
|
namespace Shimmie2;
|
|
|
|
|
2009-07-19 07:38:13 +00:00
|
|
|
/**
|
2014-04-29 00:12:31 +00:00
|
|
|
* Interface Config
|
|
|
|
*
|
|
|
|
* An abstract interface for altering a name:value pair list.
|
2009-01-04 14:38:48 +00:00
|
|
|
*/
|
2019-05-28 16:59:38 +00:00
|
|
|
interface Config
|
|
|
|
{
|
|
|
|
//@{ /*--------------------------------- SET ------------------------------------------------------*/
|
|
|
|
/**
|
|
|
|
* Set a configuration option to a new value, regardless of what the value is at the moment.
|
|
|
|
*/
|
2024-06-21 18:00:43 +00:00
|
|
|
public function set_int(string $name, int $value): void;
|
2019-05-28 16:59:38 +00:00
|
|
|
|
2019-06-27 13:12:15 +00:00
|
|
|
/**
|
|
|
|
* Set a configuration option to a new value, regardless of what the value is at the moment.
|
|
|
|
*/
|
2024-06-21 18:00:43 +00:00
|
|
|
public function set_float(string $name, float $value): void;
|
2019-06-27 13:12:15 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
/**
|
|
|
|
* Set a configuration option to a new value, regardless of what the value is at the moment.
|
|
|
|
*/
|
2024-06-21 18:00:43 +00:00
|
|
|
public function set_string(string $name, string $value): void;
|
2019-05-28 16:59:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a configuration option to a new value, regardless of what the value is at the moment.
|
|
|
|
*/
|
2024-06-21 18:00:43 +00:00
|
|
|
public function set_bool(string $name, bool $value): void;
|
2019-05-28 16:59:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a configuration option to a new value, regardless of what the value is at the moment.
|
2024-01-20 14:10:59 +00:00
|
|
|
*
|
|
|
|
* @param mixed[] $value
|
2019-05-28 16:59:38 +00:00
|
|
|
*/
|
|
|
|
public function set_array(string $name, array $value): void;
|
2024-06-21 18:00:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a configuration option.
|
|
|
|
*/
|
|
|
|
public function delete(string $name): void;
|
2019-05-28 16:59:38 +00:00
|
|
|
//@} /*--------------------------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
//@{ /*-------------------------------- SET DEFAULT -----------------------------------------------*/
|
|
|
|
/**
|
|
|
|
* Set a configuration option to a new value, if there is no value currently.
|
|
|
|
*
|
|
|
|
* Extensions should generally call these from their InitExtEvent handlers.
|
|
|
|
* This has the advantage that the values will show up in the "advanced" setup
|
|
|
|
* page where they can be modified, while calling get_* with a "default"
|
|
|
|
* parameter won't show up.
|
|
|
|
*/
|
|
|
|
public function set_default_int(string $name, int $value): void;
|
|
|
|
|
2019-06-27 13:12:15 +00:00
|
|
|
/**
|
|
|
|
* Set a configuration option to a new value, if there is no value currently.
|
|
|
|
*
|
|
|
|
* Extensions should generally call these from their InitExtEvent handlers.
|
|
|
|
* This has the advantage that the values will show up in the "advanced" setup
|
|
|
|
* page where they can be modified, while calling get_* with a "default"
|
|
|
|
* parameter won't show up.
|
|
|
|
*/
|
|
|
|
public function set_default_float(string $name, float $value): void;
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
/**
|
|
|
|
* Set a configuration option to a new value, if there is no value currently.
|
|
|
|
*
|
|
|
|
* Extensions should generally call these from their InitExtEvent handlers.
|
|
|
|
* This has the advantage that the values will show up in the "advanced" setup
|
|
|
|
* page where they can be modified, while calling get_* with a "default"
|
|
|
|
* parameter won't show up.
|
|
|
|
*/
|
|
|
|
public function set_default_string(string $name, string $value): void;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a configuration option to a new value, if there is no value currently.
|
|
|
|
*
|
|
|
|
* Extensions should generally call these from their InitExtEvent handlers.
|
|
|
|
* This has the advantage that the values will show up in the "advanced" setup
|
|
|
|
* page where they can be modified, while calling get_* with a "default"
|
|
|
|
* parameter won't show up.
|
|
|
|
*/
|
|
|
|
public function set_default_bool(string $name, bool $value): void;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a configuration option to a new value, if there is no value currently.
|
|
|
|
*
|
|
|
|
* Extensions should generally call these from their InitExtEvent handlers.
|
|
|
|
* This has the advantage that the values will show up in the "advanced" setup
|
|
|
|
* page where they can be modified, while calling get_* with a "default"
|
|
|
|
* parameter won't show up.
|
2024-01-20 14:10:59 +00:00
|
|
|
*
|
|
|
|
* @param mixed[] $value
|
2019-05-28 16:59:38 +00:00
|
|
|
*/
|
|
|
|
public function set_default_array(string $name, array $value): void;
|
|
|
|
//@} /*--------------------------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
//@{ /*--------------------------------- GET ------------------------------------------------------*/
|
|
|
|
/**
|
|
|
|
* Pick a value out of the table by name, cast to the appropriate data type.
|
|
|
|
*/
|
2023-11-11 21:49:12 +00:00
|
|
|
public function get_int(string $name, ?int $default = null): ?int;
|
2019-05-28 16:59:38 +00:00
|
|
|
|
2019-06-27 13:12:15 +00:00
|
|
|
/**
|
|
|
|
* Pick a value out of the table by name, cast to the appropriate data type.
|
|
|
|
*/
|
2023-11-11 21:49:12 +00:00
|
|
|
public function get_float(string $name, ?float $default = null): ?float;
|
2019-06-27 13:12:15 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
/**
|
|
|
|
* Pick a value out of the table by name, cast to the appropriate data type.
|
2024-08-31 18:10:03 +00:00
|
|
|
*
|
|
|
|
* @template T of string|null
|
|
|
|
* @param T $default
|
|
|
|
* @return T|string
|
2019-05-28 16:59:38 +00:00
|
|
|
*/
|
2023-11-11 21:49:12 +00:00
|
|
|
public function get_string(string $name, ?string $default = null): ?string;
|
2019-05-28 16:59:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Pick a value out of the table by name, cast to the appropriate data type.
|
|
|
|
*/
|
2023-11-11 21:49:12 +00:00
|
|
|
public function get_bool(string $name, ?bool $default = null): ?bool;
|
2019-05-28 16:59:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Pick a value out of the table by name, cast to the appropriate data type.
|
2024-01-20 14:10:59 +00:00
|
|
|
*
|
|
|
|
* @param mixed[] $default
|
|
|
|
* @return mixed[]
|
2019-05-28 16:59:38 +00:00
|
|
|
*/
|
2023-11-11 21:49:12 +00:00
|
|
|
public function get_array(string $name, ?array $default = []): ?array;
|
2019-05-28 16:59:38 +00:00
|
|
|
//@} /*--------------------------------------------------------------------------------------------*/
|
2009-01-04 14:38:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-19 07:38:13 +00:00
|
|
|
/**
|
2014-04-29 00:12:31 +00:00
|
|
|
* Class BaseConfig
|
|
|
|
*
|
2009-01-04 14:38:48 +00:00
|
|
|
* Common methods for manipulating the list, loading and saving is
|
|
|
|
* left to the concrete implementation
|
|
|
|
*/
|
2019-05-28 16:59:38 +00:00
|
|
|
abstract class BaseConfig implements Config
|
|
|
|
{
|
2024-06-21 18:00:43 +00:00
|
|
|
/** @var array<string, string> */
|
2021-03-14 23:43:50 +00:00
|
|
|
public array $values = [];
|
2019-05-28 16:59:38 +00:00
|
|
|
|
2024-06-19 13:34:22 +00:00
|
|
|
/**
|
|
|
|
* Save the list of name:value pairs to wherever they came from,
|
|
|
|
* so that the next time a page is loaded it will use the new
|
|
|
|
* configuration.
|
|
|
|
*/
|
|
|
|
abstract protected function save(string $name): void;
|
|
|
|
|
2024-06-21 18:00:43 +00:00
|
|
|
public function set_int(string $name, int $value): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2024-06-21 18:00:43 +00:00
|
|
|
$this->values[$name] = (string)$value;
|
2019-05-28 16:59:38 +00:00
|
|
|
$this->save($name);
|
|
|
|
}
|
|
|
|
|
2024-06-21 18:00:43 +00:00
|
|
|
public function set_float(string $name, float $value): void
|
2019-06-27 13:12:15 +00:00
|
|
|
{
|
2024-06-21 18:00:43 +00:00
|
|
|
$this->values[$name] = (string)$value;
|
2019-06-27 13:12:15 +00:00
|
|
|
$this->save($name);
|
|
|
|
}
|
|
|
|
|
2024-06-21 18:00:43 +00:00
|
|
|
public function set_string(string $name, string $value): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
$this->values[$name] = $value;
|
|
|
|
$this->save($name);
|
|
|
|
}
|
|
|
|
|
2024-06-21 18:00:43 +00:00
|
|
|
public function set_bool(string $name, bool $value): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2020-01-26 13:19:35 +00:00
|
|
|
$this->values[$name] = $value ? 'Y' : 'N';
|
2019-05-28 16:59:38 +00:00
|
|
|
$this->save($name);
|
|
|
|
}
|
|
|
|
|
2024-06-21 18:00:43 +00:00
|
|
|
public function set_array(string $name, array $value): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2024-06-21 18:00:43 +00:00
|
|
|
$this->values[$name] = implode(",", $value);
|
|
|
|
$this->save($name);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function delete(string $name): void
|
|
|
|
{
|
|
|
|
unset($this->values[$name]);
|
2019-05-28 16:59:38 +00:00
|
|
|
$this->save($name);
|
|
|
|
}
|
|
|
|
|
2019-05-28 18:00:23 +00:00
|
|
|
public function set_default_int(string $name, int $value): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
if (is_null($this->get($name))) {
|
2024-06-21 18:00:43 +00:00
|
|
|
$this->values[$name] = (string)$value;
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-15 16:03:09 +00:00
|
|
|
public function set_default_float(string $name, float $value): void
|
|
|
|
{
|
|
|
|
if (is_null($this->get($name))) {
|
2024-06-21 18:00:43 +00:00
|
|
|
$this->values[$name] = (string)$value;
|
2019-06-15 16:03:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-28 18:00:23 +00:00
|
|
|
public function set_default_string(string $name, string $value): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
if (is_null($this->get($name))) {
|
|
|
|
$this->values[$name] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-28 18:00:23 +00:00
|
|
|
public function set_default_bool(string $name, bool $value): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
if (is_null($this->get($name))) {
|
|
|
|
$this->values[$name] = $value ? 'Y' : 'N';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-28 18:00:23 +00:00
|
|
|
public function set_default_array(string $name, array $value): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
if (is_null($this->get($name))) {
|
|
|
|
$this->values[$name] = implode(",", $value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-20 14:10:59 +00:00
|
|
|
/**
|
|
|
|
* @template T of int|null
|
|
|
|
* @param T $default
|
|
|
|
* @return T|int
|
|
|
|
*/
|
2023-11-11 21:49:12 +00:00
|
|
|
public function get_int(string $name, ?int $default = null): ?int
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
return (int)($this->get($name, $default));
|
|
|
|
}
|
|
|
|
|
2024-01-20 14:10:59 +00:00
|
|
|
/**
|
|
|
|
* @template T of float|null
|
|
|
|
* @param T $default
|
|
|
|
* @return T|float
|
|
|
|
*/
|
2023-11-11 21:49:12 +00:00
|
|
|
public function get_float(string $name, ?float $default = null): ?float
|
2019-06-15 16:03:09 +00:00
|
|
|
{
|
|
|
|
return (float)($this->get($name, $default));
|
|
|
|
}
|
|
|
|
|
2024-01-20 14:10:59 +00:00
|
|
|
/**
|
|
|
|
* @template T of string|null
|
|
|
|
* @param T $default
|
|
|
|
* @return T|string
|
|
|
|
*/
|
2023-11-11 21:49:12 +00:00
|
|
|
public function get_string(string $name, ?string $default = null): ?string
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2024-06-21 18:00:43 +00:00
|
|
|
return $this->get($name, $default);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2024-01-20 14:10:59 +00:00
|
|
|
/**
|
|
|
|
* @template T of bool|null
|
|
|
|
* @param T $default
|
|
|
|
* @return T|bool
|
|
|
|
*/
|
2023-11-11 21:49:12 +00:00
|
|
|
public function get_bool(string $name, ?bool $default = null): ?bool
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
return bool_escape($this->get($name, $default));
|
|
|
|
}
|
|
|
|
|
2024-01-20 14:10:59 +00:00
|
|
|
/**
|
|
|
|
* @template T of array<string>|null
|
|
|
|
* @param T $default
|
|
|
|
* @return T|array<string>
|
|
|
|
*/
|
|
|
|
public function get_array(string $name, ?array $default = null): ?array
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2024-01-20 14:10:59 +00:00
|
|
|
$val = $this->get($name);
|
2024-08-31 16:05:18 +00:00
|
|
|
if (is_null($val)) {
|
2024-01-20 14:10:59 +00:00
|
|
|
return $default;
|
|
|
|
}
|
2024-08-31 16:05:18 +00:00
|
|
|
if (empty($val)) {
|
2024-01-20 14:10:59 +00:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
return explode(",", $val);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2024-01-20 14:10:59 +00:00
|
|
|
private function get(string $name, mixed $default = null): mixed
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
if (isset($this->values[$name])) {
|
|
|
|
return $this->values[$name];
|
|
|
|
} else {
|
|
|
|
return $default;
|
|
|
|
}
|
|
|
|
}
|
2009-01-04 14:38:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-19 07:38:13 +00:00
|
|
|
/**
|
2014-04-29 00:12:31 +00:00
|
|
|
* Class DatabaseConfig
|
|
|
|
*
|
2009-01-04 15:57:54 +00:00
|
|
|
* Loads the config list from a table in a given database, the table should
|
|
|
|
* be called config and have the schema:
|
|
|
|
*
|
2009-07-21 06:36:12 +00:00
|
|
|
* \code
|
2009-01-04 15:57:54 +00:00
|
|
|
* CREATE TABLE config(
|
|
|
|
* name VARCHAR(255) NOT NULL,
|
|
|
|
* value TEXT
|
|
|
|
* );
|
2009-07-21 06:36:12 +00:00
|
|
|
* \endcode
|
2007-12-06 11:01:18 +00:00
|
|
|
*/
|
2019-05-28 16:59:38 +00:00
|
|
|
class DatabaseConfig extends BaseConfig
|
|
|
|
{
|
2021-03-14 23:43:50 +00:00
|
|
|
private Database $database;
|
|
|
|
private string $table_name;
|
|
|
|
private ?string $sub_column;
|
|
|
|
private ?string $sub_value;
|
2023-02-03 16:44:16 +00:00
|
|
|
private string $cache_name;
|
2019-06-27 13:12:15 +00:00
|
|
|
|
2019-09-29 13:30:55 +00:00
|
|
|
public function __construct(
|
|
|
|
Database $database,
|
|
|
|
string $table_name = "config",
|
|
|
|
string $sub_column = null,
|
|
|
|
string $sub_value = null
|
|
|
|
) {
|
2019-10-02 09:49:32 +00:00
|
|
|
global $cache;
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
$this->database = $database;
|
2019-06-27 13:12:15 +00:00
|
|
|
$this->table_name = $table_name;
|
|
|
|
$this->sub_value = $sub_value;
|
|
|
|
$this->sub_column = $sub_column;
|
2023-12-14 17:21:41 +00:00
|
|
|
$this->cache_name = empty($sub_value) ? "config" : "config_{$sub_column}_{$sub_value}";
|
2023-12-14 17:06:54 +00:00
|
|
|
$this->values = cache_get_or_set($this->cache_name, fn () => $this->get_values());
|
|
|
|
}
|
2019-05-28 16:59:38 +00:00
|
|
|
|
2023-12-14 17:06:54 +00:00
|
|
|
private function get_values(): mixed
|
|
|
|
{
|
|
|
|
$values = [];
|
2019-06-27 13:12:15 +00:00
|
|
|
|
2023-12-14 17:06:54 +00:00
|
|
|
$query = "SELECT name, value FROM {$this->table_name}";
|
|
|
|
$args = [];
|
2019-06-27 13:12:15 +00:00
|
|
|
|
2023-12-14 17:06:54 +00:00
|
|
|
if (!empty($this->sub_column) && !empty($this->sub_value)) {
|
|
|
|
$query .= " WHERE {$this->sub_column} = :sub_value";
|
|
|
|
$args["sub_value"] = $this->sub_value;
|
|
|
|
}
|
2019-06-27 13:12:15 +00:00
|
|
|
|
2023-12-14 17:06:54 +00:00
|
|
|
foreach ($this->database->get_all($query, $args) as $row) {
|
2024-06-21 18:00:43 +00:00
|
|
|
// versions prior to 2.12 would store null
|
|
|
|
// instead of deleting the row
|
2024-08-31 16:05:18 +00:00
|
|
|
if (!is_null($row["value"])) {
|
2024-06-21 18:00:43 +00:00
|
|
|
$values[$row["name"]] = $row["value"];
|
|
|
|
}
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2023-12-14 17:06:54 +00:00
|
|
|
|
|
|
|
return $values;
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2024-06-19 13:34:22 +00:00
|
|
|
protected function save(string $name): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2019-10-02 09:49:32 +00:00
|
|
|
global $cache;
|
|
|
|
|
2024-06-19 13:34:22 +00:00
|
|
|
$query = "DELETE FROM {$this->table_name} WHERE name = :name";
|
|
|
|
$args = ["name" => $name];
|
|
|
|
$cols = ["name","value"];
|
|
|
|
$params = [":name",":value"];
|
|
|
|
if (!empty($this->sub_column) && !empty($this->sub_value)) {
|
|
|
|
$query .= " AND $this->sub_column = :sub_value";
|
|
|
|
$args["sub_value"] = $this->sub_value;
|
|
|
|
$cols[] = $this->sub_column;
|
|
|
|
$params[] = ":sub_value";
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2024-06-19 13:34:22 +00:00
|
|
|
|
|
|
|
$this->database->execute($query, $args);
|
|
|
|
|
2024-08-31 16:05:18 +00:00
|
|
|
if (isset($this->values[$name])) {
|
2024-06-21 18:00:43 +00:00
|
|
|
$args["value"] = $this->values[$name];
|
|
|
|
$this->database->execute(
|
|
|
|
"INSERT INTO {$this->table_name} (".join(",", $cols).") VALUES (".join(",", $params).")",
|
|
|
|
$args
|
|
|
|
);
|
|
|
|
}
|
2024-06-19 13:34:22 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
// rather than deleting and having some other request(s) do a thundering
|
|
|
|
// herd of race-conditioned updates, just save the updated version once here
|
2023-02-02 16:39:36 +00:00
|
|
|
$cache->set($this->cache_name, $this->values);
|
|
|
|
$this->database->notify($this->cache_name);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|