From 73b784266e8aece5eb11fc03243915e94d8cd800 Mon Sep 17 00:00:00 2001 From: Matthew Barbour Date: Thu, 27 Jun 2019 09:47:22 -0500 Subject: [PATCH] Moved user config stuff into an extension --- core/_bootstrap.php | 5 --- ext/upgrade/main.php | 19 ----------- ext/user_config/main.php | 69 ++++++++++++++++++++++++++++++++++++++++ index.php | 3 ++ 4 files changed, 72 insertions(+), 24 deletions(-) create mode 100644 ext/user_config/main.php diff --git a/core/_bootstrap.php b/core/_bootstrap.php index b48efbce..5db01d08 100644 --- a/core/_bootstrap.php +++ b/core/_bootstrap.php @@ -68,11 +68,6 @@ unset($themelet); $page = class_exists("CustomPage") ? new CustomPage() : new Page(); $_tracer->end(); -$_tracer->log_start("Loading user information"); -$user = _get_user(); -$user_config = new DatabaseConfig($database, "user_config","user_id", $user->id); -$_tracer->log_endok(); - // hook up event handlers $_tracer->begin("Loading event listeners"); _load_event_listeners(); diff --git a/ext/upgrade/main.php b/ext/upgrade/main.php index 63be82ad..103066a1 100644 --- a/ext/upgrade/main.php +++ b/ext/upgrade/main.php @@ -214,25 +214,6 @@ class Upgrade extends Extension $config->set_bool("in_upgrade", false); } - if ($config->get_int("db_version") < 18) { - $config->set_bool("in_upgrade", true); - $config->set_int("db_version", 18); - - log_info("upgrade", "Adding user config table"); - - $database->create_table("user_config", " - user_id INTEGER NOT NULL, - name VARCHAR(128) NOT NULL, - value TEXT, - PRIMARY KEY (user_id, name), - FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE - "); - $database->execute("CREATE INDEX user_config_user_id_idx ON user_config(user_id)"); - - log_info("upgrade", "Database at version 18"); - $config->set_bool("in_upgrade", false); - } - } public function get_priority(): int diff --git a/ext/user_config/main.php b/ext/user_config/main.php new file mode 100644 index 00000000..ed728101 --- /dev/null +++ b/ext/user_config/main.php @@ -0,0 +1,69 @@ + + * Description: Provides system-wide support for user-specific settings + * Visibility: admin + */ + +// The user object doesn't exist until after database setup operations and the first wave of InitExtEvents, +// so we can't reliably access this data until then. This event is triggered by the system after all of that is done. +class InitUserConfigEvent extends Event +{ + public $user; + public $user_config; + + public function __construct(User $user) + { + $this->user = $user; + } +} + +class UserConfig extends Extension +{ + private const VERSION = "ext_user_config_version"; + + public function onInitExt(InitExtEvent $event) + { + global $config; + + if ($config->get_int(self::VERSION,0)<1) { + $this->install(); + } + } + + public function onInitUserConfig(InitUserConfigEvent $event) { + global $database, $user_config; + + $user_config = new DatabaseConfig($database, "user_config", "user_id", $event->user->id); + $event->user_config = $user_config; + } + + private function install(): void + { + global $config, $database; + + if ($config->get_int(self::VERSION,0) < 1) { + + log_info("upgrade", "Adding user config table"); + + $database->create_table("user_config", " + user_id INTEGER NOT NULL, + name VARCHAR(128) NOT NULL, + value TEXT, + PRIMARY KEY (user_id, name), + FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE + "); + $database->execute("CREATE INDEX user_config_user_id_idx ON user_config(user_id)"); + + $config->set_int(self::VERSION, 1); + } + } + + + // This needs to happen before any other events, but after db upgrade + public function get_priority(): int + { + return 6; + } +} diff --git a/index.php b/index.php index 95300516..67855129 100644 --- a/index.php +++ b/index.php @@ -89,7 +89,10 @@ $_tracer->begin($_SERVER["REQUEST_URI"] ?? "No Request"); try { + // start the page generation waterfall + $user = _get_user(); + send_event(new InitUserConfigEvent($user)); if (PHP_SAPI === 'cli' || PHP_SAPI == 'phpdbg') { send_event(new CommandEvent($argv)); } else {