Properly handle duplicate smileyCodes in SmileyImporter
authorTim Düsterhus <duesterhus@woltlab.com>
Fri, 19 Jul 2013 14:32:15 +0000 (16:32 +0200)
committerTim Düsterhus <duesterhus@woltlab.com>
Fri, 19 Jul 2013 14:32:15 +0000 (16:32 +0200)
wcfsetup/install/files/lib/system/importer/SmileyImporter.class.php

index 498a1fee9e3706bbe19625968a91ae4d5014fbc4..551c2fdd8616e07b6c967c71ff918879984a5b72 100644 (file)
@@ -1,11 +1,13 @@
 <?php
 namespace wcf\system\importer;
 use wcf\data\smiley\SmileyEditor;
+use wcf\system\WCF;
+use wcf\util\StringUtil;
 
 /**
  * Imports smilies.
  *
- * @author     Marcel Werk
+ * @author     Tim Duesterhus, Marcel Werk
  * @copyright  2001-2013 WoltLab GmbH
  * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
  * @package    com.woltlab.wcf
@@ -13,6 +15,35 @@ use wcf\data\smiley\SmileyEditor;
  * @category   Community Framework
  */
 class SmileyImporter implements IImporter {
+       /**
+        * known smiley codes
+        * 
+        * @var array<string>
+        */
+       public $knownCodes = array();
+       
+       /**
+        * Reads out known smiley codes.
+        */
+       public function __construct() {
+               $sql = "SELECT  smileyID, smileyCode, aliases
+                       FROM    wcf".WCF_N."_smiley";
+               $statement = WCF::getDB()->prepareStatement($sql);
+               $statement->execute();
+               
+               while ($row = $statement->fetchArray()) {
+                       $known = array();
+                       if (!empty($row['aliases'])) {
+                               $known = explode("\n", $row['aliases']);
+                       }
+                       $known[] = $row['smileyCode'];
+                               
+                       foreach ($known as $smileyCode) {
+                               $this->knownCodes[StringUtil::toLowerCase($smileyCode)] = $row['smileyID'];
+                       }
+               }
+       }
+       
        /**
         * @see wcf\system\importer\IImporter::import()
         */
@@ -21,11 +52,31 @@ class SmileyImporter implements IImporter {
                $data['smileyPath'] = 'images/smilies/'.basename($additionalData['fileLocation']);
                if (!@copy($additionalData['fileLocation'], WCF_DIR.$data['smileyPath'])) return 0;
                
+               // check smileycode
+               if (isset($this->knownCodes[StringUtil::toLowerCase($data['smileyCode'])])) return $this->knownCodes[StringUtil::toLowerCase($data['smileyCode'])];
+               
                $data['packageID'] = 1;
+               if (!isset($data['aliases'])) $data['aliases'] = '';
+               
+               // check aliases
+               $aliases = array();
+               if (!empty($data['aliases'])) {
+                       $aliases = explode("\n", StringUtil::unifyNewlines($data['aliases']));
+                       foreach ($aliases as $key => $alias) {
+                               if (isset($this->knownCodes[StringUtil::toLowerCase($alias)])) unset($aliases[$key]);
+                       }
+                       $data['aliases'] = implode("\n", $aliases);
+               }
                
                // save smiley
                $smiley = SmileyEditor::create($data);
                
+               // add smileyCode + aliases to knownCodes
+               $this->knownCodes[StringUtil::toLowerCase($data['smileyCode'])] = $smiley->smileyID;
+               foreach ($aliases as $alias) {
+                       $this->knownCodes[StringUtil::toLowerCase($alias)] = $smiley->smileyID;
+               }
+               
                return $smiley->smileyID;
        }
 }