From 187c9504c6184511c9b44744e3a26232c94c1d77 Mon Sep 17 00:00:00 2001 From: Marcel Werk Date: Wed, 10 Jul 2013 00:28:19 +0200 Subject: [PATCH] Added user merge --- .../files/acp/templates/dataImport.tpl | 21 +++++++++- .../lib/acp/form/DataImportForm.class.php | 16 ++++++- .../system/importer/ImportHandler.class.php | 24 +++++++++++ .../system/importer/UserImporter.class.php | 42 +++++++++++++++++++ .../lib/system/worker/ImportWorker.class.php | 4 ++ wcfsetup/install/lang/de.xml | 4 ++ wcfsetup/install/lang/en.xml | 4 ++ 7 files changed, 112 insertions(+), 3 deletions(-) diff --git a/wcfsetup/install/files/acp/templates/dataImport.tpl b/wcfsetup/install/files/acp/templates/dataImport.tpl index a581c817fd..034b2b9898 100644 --- a/wcfsetup/install/files/acp/templates/dataImport.tpl +++ b/wcfsetup/install/files/acp/templates/dataImport.tpl @@ -123,13 +123,24 @@ {/foreach} + + {event name='dataFields'} - {*
+
{lang}wcf.acp.dataImport.configure.settings{/lang} +
+
+
+ + + +
+
-
*} + {event name='settingFields'} +
{lang}wcf.acp.dataImport.configure.database{/lang} @@ -171,6 +182,8 @@ {/if} + + {event name='databaseFields'}
@@ -186,7 +199,11 @@ {lang}wcf.acp.dataImport.configure.fileSystem.path.description{/lang} + + {event name='fileSystemFields'}
+ + {event name='fieldsets'}
diff --git a/wcfsetup/install/files/lib/acp/form/DataImportForm.class.php b/wcfsetup/install/files/lib/acp/form/DataImportForm.class.php index c65438580f..081fdc14a8 100644 --- a/wcfsetup/install/files/lib/acp/form/DataImportForm.class.php +++ b/wcfsetup/install/files/lib/acp/form/DataImportForm.class.php @@ -101,6 +101,12 @@ class DataImportForm extends AbstractForm { */ public $fileSystemPath = ''; + /** + * user merge mode + * @var integer + */ + public $userMergeMode = 2; + /** * @see wcf\page\IPage::readParameters() */ @@ -155,6 +161,7 @@ class DataImportForm extends AbstractForm { if (isset($_POST['dbName'])) $this->dbName = StringUtil::trim($_POST['dbName']); if (isset($_POST['dbPrefix'])) $this->dbPrefix = StringUtil::trim($_POST['dbPrefix']); if (isset($_POST['fileSystemPath'])) $this->fileSystemPath = StringUtil::trim($_POST['fileSystemPath']); + if (isset($_POST['userMergeMode'])) $this->userMergeMode = intval($_POST['userMergeMode']); } /** @@ -183,6 +190,11 @@ class DataImportForm extends AbstractForm { if (!$this->exporter->validateFileAccess()) { throw new UserInputException('fileSystemPath'); } + + // validate user merge mode + if ($this->userMergeMode < 1 || $this->userMergeMode > 3) { + $this->userMergeMode = 2; + } } /** @@ -203,6 +215,7 @@ class DataImportForm extends AbstractForm { 'dbName' => $this->dbName, 'dbPrefix' => $this->dbPrefix, 'fileSystemPath' => $this->fileSystemPath, + 'userMergeMode' => $this->userMergeMode )); WCF::getTPL()->assign('queue', $queue); @@ -226,7 +239,8 @@ class DataImportForm extends AbstractForm { 'dbPassword' => $this->dbPassword, 'dbName' => $this->dbName, 'dbPrefix' => $this->dbPrefix, - 'fileSystemPath' => $this->fileSystemPath + 'fileSystemPath' => $this->fileSystemPath, + 'userMergeMode' => $this->userMergeMode )); } } diff --git a/wcfsetup/install/files/lib/system/importer/ImportHandler.class.php b/wcfsetup/install/files/lib/system/importer/ImportHandler.class.php index 8cd75a4059..eded45a950 100644 --- a/wcfsetup/install/files/lib/system/importer/ImportHandler.class.php +++ b/wcfsetup/install/files/lib/system/importer/ImportHandler.class.php @@ -34,6 +34,12 @@ class ImportHandler extends SingletonFactory { */ protected $importers = array(); + /** + * user merge mode + * @var integer + */ + protected $userMergeMode = 2; + /** * @see wcf\system\SingletonFactory::init() */ @@ -103,4 +109,22 @@ class ImportHandler extends SingletonFactory { unset($this->idMappingCache[$objectTypeID][$oldID]); } + + /** + * Sets the user merge mode. + * + * @param integer $mode + */ + public function setUserMergeMode($mode) { + $this->userMergeMode = $mode; + } + + /** + * Gets the user merge mode. + * + * @return integer + */ + public function getUserMergeMode() { + return $this->userMergeMode; + } } diff --git a/wcfsetup/install/files/lib/system/importer/UserImporter.class.php b/wcfsetup/install/files/lib/system/importer/UserImporter.class.php index 5bc39b6eb3..cf81659237 100644 --- a/wcfsetup/install/files/lib/system/importer/UserImporter.class.php +++ b/wcfsetup/install/files/lib/system/importer/UserImporter.class.php @@ -4,6 +4,7 @@ use wcf\data\user\User; use wcf\data\user\UserAction; use wcf\system\database\DatabaseException; use wcf\system\WCF; +use wcf\util\StringUtil; /** * Imports users. @@ -20,6 +21,21 @@ class UserImporter implements IImporter { * @see wcf\system\importer\IImporter::import() */ public function import($oldID, array $data, array $additionalData = array()) { + // resolve duplicates + $existingUser = User::getUserByUsername($data['username']); + if ($existingUser->userID) { + if ($this->userMergeMode == 1 || ($this->userMergeMode == 3 && StringUtil::toLowerCase($existingUser->email) != StringUtil::toLowerCase($data['email']))) { + // rename user + $data['username'] = self::resolveDuplicate($data['username']); + } + else { + // merge user + ImportHandler::getInstance()->saveNewID('com.woltlab.wcf.user', $oldID, $existingUser->userID); + + return $existingUser->userID; + } + } + // check existing user id $sql = "SELECT COUNT(*) AS count FROM wcf".WCF_N."_user @@ -64,4 +80,30 @@ class UserImporter implements IImporter { return $newUserID; } + + /** + * Revolves duplicate user names. + * + * @param string $username + * @return string new username + */ + private static function resolveDuplicate($username) { + $i = 0; + $newUsername = ''; + do { + $i++; + $newUsername = 'Duplicate'.($i > 1 ? $i : '').' '.$username; + // try username + $sql = "SELECT userID + FROM wcf".WCF_N."_user + WHERE username = ?"; + $statement = WCF::getDB()->prepareStatement($sql); + $statement->execute(array($newUsername)); + $row = $statement->fetchArray(); + if (empty($row['userID'])) break; + } + while (true); + + return $newUsername; + } } diff --git a/wcfsetup/install/files/lib/system/worker/ImportWorker.class.php b/wcfsetup/install/files/lib/system/worker/ImportWorker.class.php index 6176b07094..aa48b34c77 100644 --- a/wcfsetup/install/files/lib/system/worker/ImportWorker.class.php +++ b/wcfsetup/install/files/lib/system/worker/ImportWorker.class.php @@ -2,6 +2,7 @@ namespace wcf\system\worker; use wcf\data\object\type\ObjectTypeCache; use wcf\system\exception\SystemException; +use wcf\system\importer\ImportHandler; use wcf\system\WCF; /** @@ -49,6 +50,9 @@ class ImportWorker extends AbstractWorker { // set data $this->exporter->setData($this->importData['dbHost'], $this->importData['dbUser'], $this->importData['dbPassword'], $this->importData['dbName'], $this->importData['dbPrefix'], $this->importData['fileSystemPath']); $this->exporter->init(); + + // set user merge mode + ImportHandler::getInstance()->setUserMergeMode($this->importData['userMergeMode']); } /** diff --git a/wcfsetup/install/lang/de.xml b/wcfsetup/install/lang/de.xml index 8ed2aa7680..caa2dc926e 100644 --- a/wcfsetup/install/lang/de.xml +++ b/wcfsetup/install/lang/de.xml @@ -189,6 +189,10 @@ {$exception->getMessage()}
{$exception->getErrorDesc()}
]]>
+ + + + diff --git a/wcfsetup/install/lang/en.xml b/wcfsetup/install/lang/en.xml index 3738dd9a8f..bec4e6db22 100644 --- a/wcfsetup/install/lang/en.xml +++ b/wcfsetup/install/lang/en.xml @@ -188,6 +188,10 @@ Examples for medium ID detection: {$exception->getMessage()}
{$exception->getErrorDesc()}
]]>
+ + + +
-- 2.20.1