*/ class ResyncForumStatsCommand extends Command { /** * A quick description of this command. * @return string. */ public function brief(): string { return 'Resynchronises the user topics and posts counts.'; } /** * Does the repository installing. */ public function execute(): void { $this->getLogger()->writeln("This might take a while..."); $this->getLogger()->writeln(""); $this->forumStats(); $this->topicStats(); $this->userStats(); $this->getLogger()->writeln("Done!"); } private function forumStats(): void { $this->getLogger()->writeln("=> Forums"); $forums = DB::table('forums') ->where('forum_type', 0) ->get(['forum_id']); foreach ($forums as $forum) { $this->getLogger()->writeln("==> Forum {$forum->forum_id}"); $topic_count = DB::table('topics') ->where('forum_id', $forum->forum_id) ->count(); // if topic count is 0 then there's no point in continuing if (!$topic_count) { $this->getLogger()->writeln("No posts found, skipping..."); continue; } $post_count = DB::table('posts') ->where('forum_id', $forum->forum_id) ->count(); $last_post_id = DB::table('posts') ->where('forum_id', $forum->forum_id) ->orderBy('post_id', 'desc') ->take(1) ->value('post_id'); $last_post = new Post($last_post_id); DB::table('forums') ->where('forum_id', $forum->forum_id) ->update([ 'forum_count_topics' => $topic_count, 'forum_count_posts' => $post_count, 'last_post_id' => $last_post->id, 'last_post_title' => $last_post->subject, 'last_post_time' => $last_post->time, 'last_post_user_id' => $last_post->poster->id, 'last_post_username' => $last_post->poster->username, 'last_post_user_colour' => $last_post->poster->colour, ]); } $this->getLogger()->writeln(""); } private function topicStats(): void { $this->getLogger()->writeln("=> Topics"); $topics = DB::table('topics') ->get(['topic_id', 'post_id']); foreach ($topics as $topic) { $this->getLogger()->writeln("==> Topic {$topic->topic_id}"); $reply_count = DB::table('posts') ->where('topic_id', $topic->topic_id) ->count(); // delete the topic record if there's no posts presents if ($reply_count === 0) { $this->getLogger()->writeln("No posts found, deleting record..."); DB::table('topics') ->where('topic_id', $topic->topic_id) ->delete(); continue; } // take one off the reply count because the OP technically isn't a reply $reply_count--; $last_post_id = DB::table('posts') ->where('topic_id', $topic->topic_id) ->orderBy('post_id', 'desc') ->take(1) ->value('post_id'); $last_post = new Post($last_post_id); $update_data = [ 'topic_replies' => $reply_count, 'last_post_id' => $last_post->id, 'last_post_user_id' => $last_post->poster->id, 'last_post_username' => $last_post->poster->username, 'last_post_user_colour' => $last_post->poster->colour, ]; if (intval($topic->post_id) === 0) { $this->getLogger()->writeln("Missing first post record, resolving..."); $first_post_id = DB::table('posts') ->where('topic_id', $topic->topic_id) ->orderBy('post_id') ->take(1) ->value('post_id'); $first_post = new Post($first_post_id); $update_data = array_merge($update_data, [ 'post_id' => $first_post->id, 'user_id' => $first_post->poster->id, 'first_post_username' => $first_post->poster->username, 'first_post_user_colour' => $first_post->poster->colour, ]); $this->getLogger()->writeln("Linked post {$first_post->id} to topic {$topic->topic_id}."); } DB::table('topics') ->where('topic_id', $topic->topic_id) ->update($update_data); } $this->getLogger()->writeln(""); } private function userStats(): void { $this->getLogger()->writeln("=> Users"); $users = DB::table('users') ->orderBy('user_id') ->get(['user_id']); foreach ($users as $user) { $this->getLogger()->writeln("==> User {$user->user_id}"); $posts_count = DB::table('posts') ->where('poster_id', $user->user_id) ->count(); $topics_count = DB::table('topics') ->where('user_id', $user->user_id) ->count(); DB::table('users') ->where('user_id', $user->user_id) ->update([ 'user_count_topics' => $topics_count, 'user_count_posts' => $posts_count, ]); } $this->getLogger()->writeln(""); } }