diff --git a/src/Commands/MybbToFlarumCommand.php b/src/Commands/MybbToFlarumCommand.php index 086c491..4c88dcc 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'], @@ -55,6 +56,7 @@ 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'); $doThreadsPosts = $this->input->getOption('threads-posts'); @@ -98,7 +100,7 @@ protected function fire() $migrator->migrateUserGroups(); if ($doUsers) - $migrator->migrateUsers($migrate_avatars, $doGroups); + $migrator->migrateUsers($migrate_avatars, $doGroups, $migrate_privatemessages); if ($doCategories) $migrator->migrateCategories(); diff --git a/src/Migrator.php b/src/Migrator.php index 7ca183b..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,9 +154,68 @@ public function migrateUsers(bool $migrateAvatars = false, bool $migrateWithUser } } + if($migratePrivateMessages) + $this->migratePrivateMessages(); + $this->enableForeignKeyChecks(); } + 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 + $user = User::find($row->uid); + $discussion = Discussion::start($row->subject, $user); + + $discussion->slug = $this->slugDiscussion($row->subject); + $discussion->is_approved = true; + $discussion->is_private = true; + $discussion->created_at = $row->dateline; + $discussion->save(); + + $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, $user->id, null); + $post->created_at = $row->dateline; + $post->is_approved = true; + $post->number = $number; + + $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()) + { + $user = User::find($rRow->uid); + + $post = CommentPost::reply($discussion->id, $rRow->message, $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(); + } + } + /** * Transform/migrate categories and forums into tags */