From 17b0b4e94f0dbeb0262cde8a91876f82727ec170 Mon Sep 17 00:00:00 2001 From: Shish Date: Fri, 21 Jun 2024 19:00:43 +0100 Subject: [PATCH] [core] remove config->set_XXX("foo", null) -- use config->delete("foo") instead --- core/config.php | 77 +++++++++++++++++++++++------------------- ext/featured/main.php | 2 +- ext/image/main.php | 2 +- ext/res_limit/test.php | 22 ++++++------ ext/setup/main.php | 32 ++++++++++-------- 5 files changed, 74 insertions(+), 61 deletions(-) diff --git a/core/config.php b/core/config.php index 473afd10..74b92dfa 100644 --- a/core/config.php +++ b/core/config.php @@ -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 */ + /** @var array */ 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 diff --git a/ext/featured/main.php b/ext/featured/main.php index c8503b6d..ba9addcc 100644 --- a/ext/featured/main.php +++ b/ext/featured/main.php @@ -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"); } } diff --git a/ext/image/main.php b/ext/image/main.php index d60724ec..a129fee8 100644 --- a/ext/image/main.php +++ b/ext/image/main.php @@ -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); } diff --git a/ext/res_limit/test.php b/ext/res_limit/test.php index 618a15ff..71e87b50 100644 --- a/ext/res_limit/test.php +++ b/ext/res_limit/test.php @@ -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(); } diff --git a/ext/setup/main.php b/ext/setup/main.php index dd6404f8..3dbcec3b 100644 --- a/ext/setup/main.php +++ b/ext/setup/main.php @@ -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); } } }