From: Marcel Werk Date: Wed, 14 Aug 2013 19:03:12 +0000 (+0200) Subject: Improved user import performance X-Git-Tag: 2.0.0_Beta_7~24 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=a96244e1b013bc717924a2852c7cbd307a581dcb;p=GitHub%2FWoltLab%2FWCF.git Improved user import performance --- diff --git a/wcfsetup/install/files/lib/system/importer/UserImporter.class.php b/wcfsetup/install/files/lib/system/importer/UserImporter.class.php index e2cb7c4463..36c8ebf17a 100644 --- a/wcfsetup/install/files/lib/system/importer/UserImporter.class.php +++ b/wcfsetup/install/files/lib/system/importer/UserImporter.class.php @@ -1,7 +1,8 @@ + */ + protected $userIDs = array(); + + /** + * Creates a new UserImporter object. + */ + public function __construct() { + register_shutdown_function(array($this, 'finalizeImport')); + } + /** * @see wcf\system\importer\IImporter::import() */ @@ -80,18 +106,83 @@ class UserImporter extends AbstractImporter { } // create user - $action = new UserAction(array(), 'create', array( - 'data' => $data, - 'groups' => $groupIDs, - 'options' => $userOptions, - 'languages' => $languageIDs - )); - $returnValues = $action->executeAction(); + $user = UserEditor::create($data); + $userEditor = new UserEditor($user); + + // updates user options + $userEditor->updateUserOptions($userOptions); + + // store data + $this->userIDs[] = $user->userID; + $this->userToGroups[$user->userID] = $groupIDs; + $this->userToLanguages[$user->userID] = $languageIDs; + + // save mapping + ImportHandler::getInstance()->saveNewID('com.woltlab.wcf.user', $oldID, $user->userID); - $newUserID = $returnValues['returnValues']->userID; - ImportHandler::getInstance()->saveNewID('com.woltlab.wcf.user', $oldID, $newUserID); + return $user->userID; + } + + /** + * Finalizes the user import. + */ + public function finalizeImport() { + if (empty($this->userIDs)) return; + + // save groups + $sql = "INSERT IGNORE INTO wcf".WCF_N."_user_to_group + (userID, groupID) + VALUES (?, ?)"; + $statement = WCF::getDB()->prepareStatement($sql); + WCF::getDB()->beginTransaction(); + foreach ($this->userToGroups as $userID => $groupIDs) { + $groupIDs = array_merge($groupIDs, UserGroup::getGroupIDsByType(array(UserGroup::EVERYONE, UserGroup::USERS))); + + foreach ($groupIDs as $groupID) { + $statement->execute(array($userID, $groupID)); + } + } + WCF::getDB()->commitTransaction(); + + // save languages + $sql = "INSERT IGNORE INTO wcf".WCF_N."_user_to_language + (userID, languageID) + VALUES (?, ?)"; + $statement = WCF::getDB()->prepareStatement($sql); + WCF::getDB()->beginTransaction(); + foreach ($this->userToLanguages as $userID => $languageIDs) { + if (empty($languageIDs)) $languageIDs = array(LanguageFactory::getInstance()->getDefaultLanguageID()); + foreach ($languageIDs as $languageID) { + $statement->execute(array($userID, $languageID)); + } + } + WCF::getDB()->commitTransaction(); + + // get default notification events + $eventIDs = array(); + $sql = "SELECT eventID + FROM wcf".WCF_N."_user_notification_event + WHERE preset = ?"; + $statement = WCF::getDB()->prepareStatement($sql); + $statement->execute(array(1)); + while ($row = $statement->fetchArray()) { + $eventIDs[] = $row['eventID']; + } + + $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->userIDs as $userID) { + foreach ($eventIDs as $eventID) { + $statement->execute(array($userID, $eventID)); + } + } + WCF::getDB()->commitTransaction(); - return $newUserID; + // reset user ids + $this->userIDs = array(); } /**