Improve performance of UserImporter
authorTim Düsterhus <duesterhus@woltlab.com>
Mon, 15 Mar 2021 14:31:32 +0000 (15:31 +0100)
committerTim Düsterhus <duesterhus@woltlab.com>
Mon, 15 Mar 2021 14:31:32 +0000 (15:31 +0100)
wcfsetup/install/files/lib/system/importer/UserImporter.class.php

index fe102a815bbcdc1fa22b2589733f54df5235365a..bc981942e2797a125e8958b4f651d5547a3bff5b 100644 (file)
@@ -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);