Added user merge
authorMarcel Werk <burntime@woltlab.com>
Tue, 9 Jul 2013 22:28:19 +0000 (00:28 +0200)
committerMarcel Werk <burntime@woltlab.com>
Tue, 9 Jul 2013 22:28:19 +0000 (00:28 +0200)
wcfsetup/install/files/acp/templates/dataImport.tpl
wcfsetup/install/files/lib/acp/form/DataImportForm.class.php
wcfsetup/install/files/lib/system/importer/ImportHandler.class.php
wcfsetup/install/files/lib/system/importer/UserImporter.class.php
wcfsetup/install/files/lib/system/worker/ImportWorker.class.php
wcfsetup/install/lang/de.xml
wcfsetup/install/lang/en.xml

index a581c817fda9a86045ca59c22de46bfe82b7e192..034b2b98985cb4a501026a77ffb31e40e788a64e 100644 (file)
                                                </dd>
                                        </dl>
                                {/foreach}
+                               
+                               {event name='dataFields'}
                        </fieldset>
                        
-                       {*<fieldset>
+                       <fieldset>
                                <legend>{lang}wcf.acp.dataImport.configure.settings{/lang}</legend>
                                
+                               <dl>
+                                       <dt><label for="userMergeMode">{lang}wcf.acp.dataImport.configure.settings.userMergeMode{/lang}</label></dt>
+                                       <dd>
+                                               <label><input type="radio" id="userMergeMode" name="userMergeMode" value="1" {if $userMergeMode == 1}checked="checked" {/if}/> {lang}wcf.acp.dataImport.configure.settings.userMergeMode.1{/lang}</label>
+                                               <label><input type="radio" name="userMergeMode" value="2" {if $userMergeMode == 2}checked="checked" {/if}/> {lang}wcf.acp.dataImport.configure.settings.userMergeMode.2{/lang}</label>
+                                               <label><input type="radio" name="userMergeMode" value="3" {if $userMergeMode == 3}checked="checked" {/if}/> {lang}wcf.acp.dataImport.configure.settings.userMergeMode.3{/lang}</label>
+                                       </dd>
+                               </dl>
                                
-                       </fieldset>*}
+                               {event name='settingFields'}
+                       </fieldset>
                        
                        <fieldset{if $errorField == 'database'} class="formError"{/if}>
                                <legend>{lang}wcf.acp.dataImport.configure.database{/lang}</legend>
                                                {/if}
                                        </dd>
                                </dl>
+                               
+                               {event name='databaseFields'}
                        </fieldset>
                        
                        <fieldset>
                                                <small>{lang}wcf.acp.dataImport.configure.fileSystem.path.description{/lang}</small>
                                        </dd>
                                </dl>
+                               
+                               {event name='fileSystemFields'}
                        </fieldset>
+                       
+                       {event name='fieldsets'}
                </div>
        
                <div class="formSubmit">
index c65438580f586953b7fced3d24ae885891910a46..081fdc14a852633734560558b2eaab4029e14df7 100644 (file)
@@ -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
                ));
        }
 }
index 8cd75a40596bee738622cf6cc153f909198d2736..eded45a9504423239b9125bfbcb0cbd56f956f39 100644 (file)
@@ -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;
+       }
 }
index 5bc39b6eb3735db761c4bedcd8afc503c6b1c7b8..cf81659237c599640ad8b1c2634a7fa319e999de 100644 (file)
@@ -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;
+       }
 }
index 6176b07094fe09ee947f6176c311aae049f1d51d..aa48b34c7700a5c10c48b2598a539102f3ffb702 100644 (file)
@@ -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']);
        }
        
        /**
index 8ed2aa76807bd62fb6bfc2ca09307e496c3fe7eb..caa2dc926e07c146342caa110b6ae892050e7508 100644 (file)
                <item name="wcf.acp.dataImport.configure.database.error"><![CDATA[Beim Verbindungsversuch mit der Datenbank ist folgender Fehler aufgetreten:
                <br /><strong>{$exception->getMessage()}<br />{$exception->getErrorDesc()}</strong>]]></item>
                <item name="wcf.acp.dataImport.configure.fileSystem.path.error"><![CDATA[Es wurde keine Installation unter dem angegeben Pfad gefunden.]]></item>
+               <item name="wcf.acp.dataImport.configure.settings.userMergeMode"><![CDATA[Verhalten bei sich überschneidenden Benutzernamen]]></item>
+               <item name="wcf.acp.dataImport.configure.settings.userMergeMode.1"><![CDATA[Benutzeraccounts umbennen]]></item>
+               <item name="wcf.acp.dataImport.configure.settings.userMergeMode.2"><![CDATA[Benutzeraccounts zusammenlegen]]></item>
+               <item name="wcf.acp.dataImport.configure.settings.userMergeMode.3"><![CDATA[Benutzeraccounts zusammenlegen, wenn auch die E-Mail-Adresse identisch ist]]></item>
        </category>
        
        <category name="wcf.acp.exceptionLog">
index 3738dd9a8f167821f0eb4ccf344dae79da7e5005..bec4e6db22a19445ac0b102b232cb30f7eed11b5 100644 (file)
@@ -188,6 +188,10 @@ Examples for medium ID detection:
                <item name="wcf.acp.dataImport.configure.database.error"><![CDATA[TODO: Beim Verbindungsversuch mit der Datenbank ist folgender Fehler aufgetreten:
                <br /><strong>{$exception->getMessage()}<br />{$exception->getErrorDesc()}</strong>]]></item>
                <item name="wcf.acp.dataImport.configure.fileSystem.path.error"><![CDATA[TODO: Es wurde keine Installation unter dem angegeben Pfad gefunden.]]></item>
+               <item name="wcf.acp.dataImport.configure.settings.userMergeMode"><![CDATA[TODO: Verhalten bei sich überschneidenden Benutzernamen]]></item>
+               <item name="wcf.acp.dataImport.configure.settings.userMergeMode.1"><![CDATA[TODO: Benutzeraccounts umbennen]]></item>
+               <item name="wcf.acp.dataImport.configure.settings.userMergeMode.2"><![CDATA[TODO: Benutzeraccounts zusammenlegen]]></item>
+               <item name="wcf.acp.dataImport.configure.settings.userMergeMode.3"><![CDATA[TODO: Benutzeraccounts zusammenlegen, wenn auch die E-Mail-Adresse identisch ist]]></item>
        </category>
        
        <category name="wcf.acp.exceptionLog">