[core] remove config->set_XXX("foo", null) -- use config->delete("foo") instead

This commit is contained in:
Shish 2024-06-21 19:00:43 +01:00 committed by Shish
parent f84bcaec01
commit 17b0b4e94f
5 changed files with 74 additions and 61 deletions

View file

@ -15,22 +15,22 @@ interface Config
/**
* Set a configuration option to a new value, regardless of what the value is at the moment.
*/
public function set_int(string $name, ?int $value): void;
public function set_int(string $name, int $value): void;
/**
* Set a configuration option to a new value, regardless of what the value is at the moment.
*/
public function set_float(string $name, ?float $value): void;
public function set_float(string $name, float $value): void;
/**
* Set a configuration option to a new value, regardless of what the value is at the moment.
*/
public function set_string(string $name, ?string $value): void;
public function set_string(string $name, string $value): void;
/**
* Set a configuration option to a new value, regardless of what the value is at the moment.
*/
public function set_bool(string $name, ?bool $value): void;
public function set_bool(string $name, bool $value): void;
/**
* Set a configuration option to a new value, regardless of what the value is at the moment.
@ -38,6 +38,11 @@ interface Config
* @param mixed[] $value
*/
public function set_array(string $name, array $value): void;
/**
* Delete a configuration option.
*/
public function delete(string $name): void;
//@} /*--------------------------------------------------------------------------------------------*/
//@{ /*-------------------------------- SET DEFAULT -----------------------------------------------*/
@ -134,7 +139,7 @@ interface Config
*/
abstract class BaseConfig implements Config
{
/** @var array<string, mixed> */
/** @var array<string, string> */
public array $values = [];
/**
@ -144,51 +149,53 @@ abstract class BaseConfig implements Config
*/
abstract protected function save(string $name): void;
public function set_int(string $name, ?int $value): void
public function set_int(string $name, int $value): void
{
$this->values[$name] = is_null($value) ? null : $value;
$this->values[$name] = (string)$value;
$this->save($name);
}
public function set_float(string $name, ?float $value): void
public function set_float(string $name, float $value): void
{
$this->values[$name] = (string)$value;
$this->save($name);
}
public function set_string(string $name, string $value): void
{
$this->values[$name] = $value;
$this->save($name);
}
public function set_string(string $name, ?string $value): void
{
$this->values[$name] = $value;
$this->save($name);
}
public function set_bool(string $name, ?bool $value): void
public function set_bool(string $name, bool $value): void
{
$this->values[$name] = $value ? 'Y' : 'N';
$this->save($name);
}
public function set_array(string $name, ?array $value): void
public function set_array(string $name, array $value): void
{
if ($value != null) {
$this->values[$name] = implode(",", $value);
} else {
$this->values[$name] = null;
}
$this->values[$name] = implode(",", $value);
$this->save($name);
}
public function delete(string $name): void
{
unset($this->values[$name]);
$this->save($name);
}
public function set_default_int(string $name, int $value): void
{
if (is_null($this->get($name))) {
$this->values[$name] = $value;
$this->values[$name] = (string)$value;
}
}
public function set_default_float(string $name, float $value): void
{
if (is_null($this->get($name))) {
$this->values[$name] = $value;
$this->values[$name] = (string)$value;
}
}
@ -240,11 +247,7 @@ abstract class BaseConfig implements Config
*/
public function get_string(string $name, ?string $default = null): ?string
{
$val = $this->get($name, $default);
if (!is_string($val) && !is_null($val)) {
throw new ServerError("$name is not a string: $val");
}
return $val;
return $this->get($name, $default);
}
/**
@ -335,7 +338,11 @@ class DatabaseConfig extends BaseConfig
}
foreach ($this->database->get_all($query, $args) as $row) {
$values[$row["name"]] = $row["value"];
// versions prior to 2.12 would store null
// instead of deleting the row
if(!is_null($row["value"])) {
$values[$row["name"]] = $row["value"];
}
}
return $values;
@ -358,11 +365,13 @@ class DatabaseConfig extends BaseConfig
$this->database->execute($query, $args);
$args["value"] = $this->values[$name];
$this->database->execute(
"INSERT INTO {$this->table_name} (".join(",", $cols).") VALUES (".join(",", $params).")",
$args
);
if(isset($this->values[$name])) {
$args["value"] = $this->values[$name];
$this->database->execute(
"INSERT INTO {$this->table_name} (".join(",", $cols).") VALUES (".join(",", $params).")",
$args
);
}
// rather than deleting and having some other request(s) do a thundering
// herd of race-conditioned updates, just save the updated version once here

View file

@ -72,7 +72,7 @@ class Featured extends Extension
{
global $config;
if ($event->image->id == $config->get_int("featured_id")) {
$config->set_int("featured_id", 0);
$config->delete("featured_id");
}
}

View file

@ -74,7 +74,7 @@ class ImageIO extends Extension
$config->set_string(ImageConfig::THUMB_MIME, MimeType::JPEG);
break;
}
$config->set_string("thumb_type", null);
$config->delete("thumb_type");
$this->set_version(ImageConfig::VERSION, 1);
}

View file

@ -28,8 +28,8 @@ class ResolutionLimitTest extends ShimmiePHPUnitTestCase
global $config;
$config->set_int("upload_min_height", 900);
$config->set_int("upload_min_width", 900);
$config->set_int("upload_max_height", -1);
$config->set_int("upload_max_width", -1);
$config->delete("upload_max_height");
$config->delete("upload_max_width");
$config->set_string("upload_ratios", "4:3 16:9");
$this->log_in_as_user();
@ -57,10 +57,10 @@ class ResolutionLimitTest extends ShimmiePHPUnitTestCase
public function testResLimitRatio(): void
{
global $config;
$config->set_int("upload_min_height", -1);
$config->set_int("upload_min_width", -1);
$config->set_int("upload_max_height", -1);
$config->set_int("upload_max_width", -1);
$config->delete("upload_min_height");
$config->delete("upload_min_width");
$config->delete("upload_max_height");
$config->delete("upload_max_width");
$config->set_string("upload_ratios", "16:9");
$e = $this->assertException(UploadException::class, function () {
@ -74,11 +74,11 @@ class ResolutionLimitTest extends ShimmiePHPUnitTestCase
public function tearDown(): void
{
global $config;
$config->set_int("upload_min_height", -1);
$config->set_int("upload_min_width", -1);
$config->set_int("upload_max_height", -1);
$config->set_int("upload_max_width", -1);
$config->set_string("upload_ratios", "");
$config->delete("upload_min_height");
$config->delete("upload_min_width");
$config->delete("upload_max_height");
$config->delete("upload_max_width");
$config->delete("upload_ratios");
parent::tearDown();
}

View file

@ -389,20 +389,24 @@ class Setup extends Extension
if (substr($_name, 0, 6) == "_type_") {
$name = substr($_name, 6);
$type = $event->values["_type_$name"];
$value = isset($event->values["_config_$name"]) ? $event->values["_config_$name"] : null;
switch ($type) {
case "string":
$config->set_string($name, $value);
break;
case "int":
$config->set_int($name, parse_shorthand_int((string)$value));
break;
case "bool":
$config->set_bool($name, bool_escape($value));
break;
case "array":
$config->set_array($name, $value);
break;
if(isset($event->values["_config_$name"])) {
$value = $event->values["_config_$name"];
switch ($type) {
case "string":
$config->set_string($name, $value);
break;
case "int":
$config->set_int($name, parse_shorthand_int((string)$value));
break;
case "bool":
$config->set_bool($name, bool_escape($value));
break;
case "array":
$config->set_array($name, $value);
break;
}
} else {
$config->delete($name);
}
}
}