From 63c6f9d2ac52816215daca4496fdc26460ed9e90 Mon Sep 17 00:00:00 2001 From: Shish Date: Wed, 19 Jun 2024 14:34:22 +0100 Subject: [PATCH] [core] config->save() is implicit in set_XXX(), no need to call it from outside of there --- core/config.php | 58 +++++++++++++++++++------------------------ ext/featured/main.php | 1 - ext/setup/main.php | 1 - 3 files changed, 26 insertions(+), 34 deletions(-) diff --git a/core/config.php b/core/config.php index d53eb997..473afd10 100644 --- a/core/config.php +++ b/core/config.php @@ -11,13 +11,6 @@ namespace Shimmie2; */ interface Config { - /** - * 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. - */ - public function save(string $name = null): void; - //@{ /*--------------------------------- SET ------------------------------------------------------*/ /** * Set a configuration option to a new value, regardless of what the value is at the moment. @@ -144,6 +137,13 @@ abstract class BaseConfig implements Config /** @var array */ public array $values = []; + /** + * 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; + public function set_int(string $name, ?int $value): void { $this->values[$name] = is_null($value) ? null : $value; @@ -341,35 +341,29 @@ class DatabaseConfig extends BaseConfig return $values; } - public function save(string $name = null): void + protected function save(string $name): void { global $cache; - if (is_null($name)) { - reset($this->values); // rewind the array to the first element - foreach ($this->values as $name => $value) { - $this->save($name); - } - } else { - $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"; - } - - $this->database->execute($query, $args); - - $args["value"] = $this->values[$name]; - $this->database->execute( - "INSERT INTO {$this->table_name} (".join(",", $cols).") VALUES (".join(",", $params).")", - $args - ); + $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"; } + + $this->database->execute($query, $args); + + $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 $cache->set($this->cache_name, $this->values); diff --git a/ext/featured/main.php b/ext/featured/main.php index a4afa117..c8503b6d 100644 --- a/ext/featured/main.php +++ b/ext/featured/main.php @@ -73,7 +73,6 @@ class Featured extends Extension global $config; if ($event->image->id == $config->get_int("featured_id")) { $config->set_int("featured_id", 0); - $config->save(); } } diff --git a/ext/setup/main.php b/ext/setup/main.php index 2af0b490..dd6404f8 100644 --- a/ext/setup/main.php +++ b/ext/setup/main.php @@ -406,7 +406,6 @@ class Setup extends Extension } } } - $config->save(); log_warning("setup", "Configuration updated"); }