From 728200370e5cd9158b46ea40f000eac38901e66b Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tim=20D=C3=BCsterhus?= Date: Mon, 15 Mar 2021 15:31:32 +0100 Subject: [PATCH] Improve performance of UserImporter --- .../system/importer/UserImporter.class.php | 73 +++++++++++-------- 1 file changed, 43 insertions(+), 30 deletions(-) diff --git a/wcfsetup/install/files/lib/system/importer/UserImporter.class.php b/wcfsetup/install/files/lib/system/importer/UserImporter.class.php index fe102a815b..bc981942e2 100644 --- a/wcfsetup/install/files/lib/system/importer/UserImporter.class.php +++ b/wcfsetup/install/files/lib/system/importer/UserImporter.class.php @@ -31,6 +31,15 @@ class UserImporter extends AbstractImporter */ protected $eventIDs = []; + /** + * @since 5.4 + * @var array + */ + protected $defaultGroups = [ + 'notActivated' => [], + 'activated' => [], + ]; + /** * list of user options * @var UserOption[] @@ -57,6 +66,11 @@ class UserImporter extends AbstractImporter $userOptionList = new UserOptionList(); $userOptionList->readObjects(); $this->userOptions = $userOptionList->getObjects(); + + $this->defaultGroups = [ + 'notActivated' => UserGroup::getGroupIDsByType([UserGroup::EVERYONE, UserGroup::GUESTS]), + 'activated' => UserGroup::getGroupIDsByType([UserGroup::EVERYONE, UserGroup::USERS]), + ]; } /** @@ -206,25 +220,23 @@ class UserImporter extends AbstractImporter } } - if (!$user->pendingActivation()) { - $defaultGroupIDs = UserGroup::getGroupIDsByType([UserGroup::EVERYONE, UserGroup::USERS]); - } else { - $defaultGroupIDs = UserGroup::getGroupIDsByType([UserGroup::EVERYONE, UserGroup::GUESTS]); - } - - $groupIDs = \array_merge($groupIDs, $defaultGroupIDs); - $sql = "INSERT IGNORE INTO wcf" . WCF_N . "_user_to_group - (userID, groupID) - VALUES (?, ?)"; - $statement = WCF::getDB()->prepareStatement($sql); - WCF::getDB()->beginTransaction(); - foreach ($groupIDs as $groupID) { - $statement->execute([ - $user->userID, - $groupID, - ]); + $groupIDs = \array_merge( + $groupIDs, + $this->defaultGroups[$user->pendingActivation() ? 'notActivated' : 'activated'] + ); + if (!empty($groupIDs)) { + $placeholders = '(?,?)' . \str_repeat(',(?,?)', \count($groupIDs) - 1); + $sql = "INSERT IGNORE INTO wcf" . WCF_N . "_user_to_group + (userID, groupID) + VALUES {$placeholders}"; + $statement = WCF::getDB()->prepareStatement($sql); + $parameters = []; + foreach ($groupIDs as $groupID) { + $parameters[] = $user->userID; + $parameters[] = $groupID; + } + $statement->execute($parameters); } - WCF::getDB()->commitTransaction(); // save languages $sql = "INSERT IGNORE INTO wcf" . WCF_N . "_user_to_language @@ -238,19 +250,20 @@ class UserImporter extends AbstractImporter ]); } - // save default user events - $sql = "INSERT IGNORE INTO wcf" . WCF_N . "_user_notification_event_to_user - (userID, eventID) - VALUES (?, ?)"; - $statement = WCF::getDB()->prepareStatement($sql); - WCF::getDB()->beginTransaction(); - foreach ($this->eventIDs as $eventID) { - $statement->execute([ - $user->userID, - $eventID, - ]); + if (!empty($this->eventIDs)) { + // save default user events + $placeholders = '(?,?)' . \str_repeat(',(?,?)', \count($this->eventIDs) - 1); + $sql = "INSERT IGNORE INTO wcf" . WCF_N . "_user_notification_event_to_user + (userID, eventID) + VALUES {$placeholders}"; + $statement = WCF::getDB()->prepareStatement($sql); + $parameters = []; + foreach ($this->eventIDs as $eventID) { + $parameters[] = $user->userID; + $parameters[] = $eventID; + } + $statement->execute($parameters); } - WCF::getDB()->commitTransaction(); // save mapping ImportHandler::getInstance()->saveNewID('com.woltlab.wcf.user', $oldID, $user->userID); -- 2.20.1