From 50dd1241a2b48535ce15a3f5cc76d54abce8ca8b Mon Sep 17 00:00:00 2001 From: Michael V Date: Sun, 22 May 2022 15:09:10 +0200 Subject: [PATCH 1/3] Start implementation of migrating private messages --- src/Commands/MybbToFlarumCommand.php | 5 +++ src/Migrator.php | 50 ++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/src/Commands/MybbToFlarumCommand.php b/src/Commands/MybbToFlarumCommand.php index 086c491..91825b5 100644 --- a/src/Commands/MybbToFlarumCommand.php +++ b/src/Commands/MybbToFlarumCommand.php @@ -23,6 +23,7 @@ class MybbToFlarumCommand extends AbstractCommand 'threads-posts' => ['threads-posts', null, InputOption::VALUE_NONE, 'Import posts (excluding soft deleted posts/threads)'], 'groups' => ['groups', null, InputOption::VALUE_NONE, 'Import groups'], 'categories' => ['categories', null, InputOption::VALUE_NONE, 'Import categories'], + 'privatemessages' => ['privatemessages', null, InputOption::VALUE_NONE, 'Import private messages'], //sub options for avatars 'avatars' => ['avatars', null, InputOption::VALUE_NONE, 'Import avatars'], @@ -57,6 +58,7 @@ protected function fire() $migrate_attachments = $this->input->getOption('attachments'); $doUsers = $this->input->getOption('users'); + $doPrivateMessages = $this->input->getOption('privatemessages'); $doThreadsPosts = $this->input->getOption('threads-posts'); $doGroups = $this->input->getOption('groups'); $doCategories = $this->input->getOption('categories'); @@ -100,6 +102,9 @@ protected function fire() if ($doUsers) $migrator->migrateUsers($migrate_avatars, $doGroups); + if ($doPrivateMessages) + $migrator->migratePrivateMessages($doUsers); + if ($doCategories) $migrator->migrateCategories(); diff --git a/src/Migrator.php b/src/Migrator.php index 7ca183b..16311f2 100644 --- a/src/Migrator.php +++ b/src/Migrator.php @@ -157,6 +157,56 @@ public function migrateUsers(bool $migrateAvatars = false, bool $migrateWithUser $this->enableForeignKeyChecks(); } + public function migratePrivateMessages(bool $withUsers) + { + $messages = $this->getMybbConnection()->query("SELECT * FROM {$this->getPrefix()}privatemessages WHERE subject NOT LIKE 'Re: %' AND subject NOT LIKE '%buddy request%' AND folder = 2 ORDER BY dateline"); + + while($row = $messages->fetch_object()) + { + // initial thread + if($withUsers) + { + $user = User::find($row->uid); + $discussion = Discussion::start($row->subject, $user); + } + else + { + $user = null; + $discussion = new Discussion(); + $discussion->title = $row->subject; + } + + $discussion->slug = $this->slugDiscussion($row->subject); + $discussion->is_approved = true; + $discussion->is_private = true; + $discussion->created_at = $row->dateline; + $discussion->save(); + + if($withUsers) + { + $toUsers = unserialize($row->recipients)['to']; + $toUsers[] = $row->uid; + + $discussion->recipientUsers()->saveMany(array_map( + fn ($userId) => User::find($userId) + , $toUsers)); + } + + //pm replies + $number = 1; + + $post = CommentPost::reply($discussion->id, $row->message, optional($user)->id, null); + $post->created_at = $row->dateline; + $post->is_approved = true; + $post->number = $number; + + $post->save(); + $discussion->setFirstPost($post); + } + + // $this->enableForeignKeyChecks(); + } + /** * Transform/migrate categories and forums into tags */ From 9411fda09f4334736b852c39b95f1c84f6d031d0 Mon Sep 17 00:00:00 2001 From: Michael V Date: Sun, 22 May 2022 15:09:37 +0200 Subject: [PATCH 2/3] Add replies --- src/Migrator.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/Migrator.php b/src/Migrator.php index 16311f2..df776ac 100644 --- a/src/Migrator.php +++ b/src/Migrator.php @@ -202,6 +202,28 @@ public function migratePrivateMessages(bool $withUsers) $post->save(); $discussion->setFirstPost($post); + + $replies = $this->getMybbConnection()->query("SELECT uid, toid, subject, message, dateline FROM {$this->getPrefix()}privatemessages WHERE folder = 2 AND subject = 'Re: {$row->subject}' ORDER BY dateline ASC"); + + while($rRow = $replies->fetch_object()) + { + if($withUsers) + $user = User::find($rRow->uid); + else + $user = null; + + $post = CommentPost::reply($discussion->id, $rRow->message, optional($user)->id, null); + $post->created_at = $rRow->dateline; + $post->is_approved = true; + $post->number = ++$number; + + $post->save(); + } + + $discussion->refreshCommentCount(); + $discussion->refreshLastPost(); + $discussion->refreshParticipantCount(); + $discussion->save(); } // $this->enableForeignKeyChecks(); From 2b6524689b3e3e9eb750fb856f2914fb34206dea Mon Sep 17 00:00:00 2001 From: Michael V Date: Sun, 22 May 2022 15:14:59 +0200 Subject: [PATCH 3/3] Just require users with private messages --- src/Commands/MybbToFlarumCommand.php | 7 ++--- src/Migrator.php | 45 ++++++++++------------------ 2 files changed, 18 insertions(+), 34 deletions(-) diff --git a/src/Commands/MybbToFlarumCommand.php b/src/Commands/MybbToFlarumCommand.php index 91825b5..4c88dcc 100644 --- a/src/Commands/MybbToFlarumCommand.php +++ b/src/Commands/MybbToFlarumCommand.php @@ -56,9 +56,9 @@ protected function fire() $migrate_softposts = $this->input->getOption('soft-posts'); $migrate_softthreads = $this->input->getOption('soft-threads'); $migrate_attachments = $this->input->getOption('attachments'); + $migrate_privatemessages = $this->input->getOption('privatemessages'); $doUsers = $this->input->getOption('users'); - $doPrivateMessages = $this->input->getOption('privatemessages'); $doThreadsPosts = $this->input->getOption('threads-posts'); $doGroups = $this->input->getOption('groups'); $doCategories = $this->input->getOption('categories'); @@ -100,10 +100,7 @@ protected function fire() $migrator->migrateUserGroups(); if ($doUsers) - $migrator->migrateUsers($migrate_avatars, $doGroups); - - if ($doPrivateMessages) - $migrator->migratePrivateMessages($doUsers); + $migrator->migrateUsers($migrate_avatars, $doGroups, $migrate_privatemessages); if ($doCategories) $migrator->migrateCategories(); diff --git a/src/Migrator.php b/src/Migrator.php index df776ac..2cb34f9 100644 --- a/src/Migrator.php +++ b/src/Migrator.php @@ -90,7 +90,7 @@ public function migrateUserGroups() * @param bool $migrateAvatars * @param bool $migrateWithUserGroups */ - public function migrateUsers(bool $migrateAvatars = false, bool $migrateWithUserGroups = false) + public function migrateUsers(bool $migrateAvatars = false, bool $migrateWithUserGroups = false, bool $migratePrivateMessages = false) { $this->disableForeignKeyChecks(); @@ -154,27 +154,21 @@ public function migrateUsers(bool $migrateAvatars = false, bool $migrateWithUser } } + if($migratePrivateMessages) + $this->migratePrivateMessages(); + $this->enableForeignKeyChecks(); } - public function migratePrivateMessages(bool $withUsers) + private function migratePrivateMessages() { $messages = $this->getMybbConnection()->query("SELECT * FROM {$this->getPrefix()}privatemessages WHERE subject NOT LIKE 'Re: %' AND subject NOT LIKE '%buddy request%' AND folder = 2 ORDER BY dateline"); while($row = $messages->fetch_object()) { // initial thread - if($withUsers) - { - $user = User::find($row->uid); - $discussion = Discussion::start($row->subject, $user); - } - else - { - $user = null; - $discussion = new Discussion(); - $discussion->title = $row->subject; - } + $user = User::find($row->uid); + $discussion = Discussion::start($row->subject, $user); $discussion->slug = $this->slugDiscussion($row->subject); $discussion->is_approved = true; @@ -182,20 +176,18 @@ public function migratePrivateMessages(bool $withUsers) $discussion->created_at = $row->dateline; $discussion->save(); - if($withUsers) - { - $toUsers = unserialize($row->recipients)['to']; - $toUsers[] = $row->uid; + $toUsers = unserialize($row->recipients)['to']; + $toUsers[] = $row->uid; - $discussion->recipientUsers()->saveMany(array_map( - fn ($userId) => User::find($userId) - , $toUsers)); - } + $discussion->recipientUsers()->saveMany(array_map( + fn ($userId) => User::find($userId) + , $toUsers)); + //pm replies $number = 1; - $post = CommentPost::reply($discussion->id, $row->message, optional($user)->id, null); + $post = CommentPost::reply($discussion->id, $row->message, $user->id, null); $post->created_at = $row->dateline; $post->is_approved = true; $post->number = $number; @@ -207,12 +199,9 @@ public function migratePrivateMessages(bool $withUsers) while($rRow = $replies->fetch_object()) { - if($withUsers) - $user = User::find($rRow->uid); - else - $user = null; + $user = User::find($rRow->uid); - $post = CommentPost::reply($discussion->id, $rRow->message, optional($user)->id, null); + $post = CommentPost::reply($discussion->id, $rRow->message, $user->id, null); $post->created_at = $rRow->dateline; $post->is_approved = true; $post->number = ++$number; @@ -225,8 +214,6 @@ public function migratePrivateMessages(bool $withUsers) $discussion->refreshParticipantCount(); $discussion->save(); } - - // $this->enableForeignKeyChecks(); } /**