From 86f97c310373ef48d8961630aae125bb6bdc6fb6 Mon Sep 17 00:00:00 2001 From: Benjamin Date: Thu, 2 Jul 2026 18:06:32 +0200 Subject: [PATCH] Add Rebuild Entries job --- Job/EntriesCount.php | 56 ++++++++++++ Service/Discord/BotService.php | 10 ++- _data/help_pages.xml | 8 +- _data/navigation.xml | 9 +- _data/phrases.xml | 14 ++- _data/templates.xml | 86 +++++++++++++++++++ _output/phrases/_metadata.json | 6 ++ _output/phrases/rebuld_entries_count.txt | 1 + _output/template_modifications/_metadata.json | 3 + .../rhpz_tools_rebuild_rhpz_entry_count.json | 9 ++ addon.json | 4 +- build.json | 6 ++ 12 files changed, 200 insertions(+), 12 deletions(-) create mode 100644 Job/EntriesCount.php create mode 100644 _output/phrases/rebuld_entries_count.txt create mode 100644 _output/template_modifications/admin/rhpz_tools_rebuild_rhpz_entry_count.json create mode 100644 build.json diff --git a/Job/EntriesCount.php b/Job/EntriesCount.php new file mode 100644 index 0000000..d2653c1 --- /dev/null +++ b/Job/EntriesCount.php @@ -0,0 +1,56 @@ +app->db(); + + return $db->fetchAllColumn( + $db->limit( + 'SELECT user_id + FROM xf_user + WHERE user_id > ? + ORDER BY user_id', + $batch + ), + $start + ); + } + + private function countPublishedEntriesByUserId($userId): int + { + $db = Laravel::db(); + + if( $userId instanceof User ) + $userId = $userId->user_id; + + return $db->fetchOne( + "SELECT COUNT(*) FROM entries WHERE state = 'published' AND user_id = ?", $userId + ); + } + + protected function rebuildById($id): void + { + $count = $this->countPublishedEntriesByUserId($id); + + $this->app->db()->update( + 'xf_user', + ['rhpz_entry_count' => $count], + 'user_id = ?', + $id + ); + } + + protected function getStatusType(): Phrase + { + return \XF::phrase('entries'); + } +} \ No newline at end of file diff --git a/Service/Discord/BotService.php b/Service/Discord/BotService.php index b0f6a9e..d50e241 100644 --- a/Service/Discord/BotService.php +++ b/Service/Discord/BotService.php @@ -6,17 +6,17 @@ use XF\Service\AbstractService; class BotService extends AbstractService { - protected string $dbPath; + protected ?string $dbPath; protected ?\PDO $pdo = null; protected function setup() { - $this->dbPath = \XF::config('discord_db_path'); + $this->dbPath = \XF::config('discord_db_path') ?? null; } - protected function getDb(): \PDO + protected function getDb(): ?\PDO { - if (!$this->pdo) { + if (!$this->pdo && $this->dbPath) { $this->pdo = new \PDO("sqlite:" . $this->dbPath); $this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); $this->pdo->exec('PRAGMA journal_mode=WAL;'); @@ -29,6 +29,8 @@ class BotService extends AbstractService { try { $db = $this->getDb(); + if( !$db ) + return false; $stmt = $db->prepare("INSERT INTO actions (action, data) VALUES (:action, :data)"); return $stmt->execute([ diff --git a/_data/help_pages.xml b/_data/help_pages.xml index 31828b1..bdd1e2c 100644 --- a/_data/help_pages.xml +++ b/_data/help_pages.xml @@ -1,2 +1,8 @@ - + + + + + + + diff --git a/_data/navigation.xml b/_data/navigation.xml index 7d2bc0f..ddcd6e8 100644 --- a/_data/navigation.xml +++ b/_data/navigation.xml @@ -1,22 +1,23 @@ - + - + - - + + + diff --git a/_data/phrases.xml b/_data/phrases.xml index 9febf13..9d3c5f9 100644 --- a/_data/phrases.xml +++ b/_data/phrases.xml @@ -7,6 +7,17 @@ + + + + + + + + + + + @@ -17,13 +28,14 @@ - + + diff --git a/_data/templates.xml b/_data/templates.xml index 607890b..bfd1451 100644 --- a/_data/templates.xml +++ b/_data/templates.xml @@ -536,6 +536,92 @@ ]]> + + + + +