Removing special characters from search strings
authorAlexander Ebert <ebert@woltlab.com>
Tue, 9 Sep 2014 11:28:50 +0000 (13:28 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Tue, 9 Sep 2014 11:28:50 +0000 (13:28 +0200)
wcfsetup/install/files/lib/system/search/AbstractSearchEngine.class.php
wcfsetup/install/files/lib/system/search/ISearchEngine.class.php
wcfsetup/install/files/lib/system/search/SearchEngine.class.php
wcfsetup/install/files/lib/system/search/mysql/MysqlSearchEngine.class.php

index 214e2546c1d5369f35902fc7cbf7fca1b3dcaf94..348c258626e474db93d5fbd84e20d25399c4d6ff 100644 (file)
@@ -21,6 +21,12 @@ abstract class AbstractSearchEngine extends SingletonFactory implements ISearchE
         */
        protected $conditionBuilderClassName = 'wcf\system\database\util\PreparedStatementConditionBuilder';
        
+       /**
+        * list of engine-specific special characters
+        * @var array<string>
+        */
+       protected $specialCharacters = array();
+       
        /**
         * @see \wcf\system\search\ISearchEngine::getConditionBuilderClassName()
         */
@@ -123,4 +129,15 @@ abstract class AbstractSearchEngine extends SingletonFactory implements ISearchE
         * @return      integer
         */
        abstract protected function getFulltextMinimumWordLength();
+       
+       /**
+        * @see \wcf\system\search\ISearchEngine::removeSpecialCharacters()
+        */
+       public function removeSpecialCharacters($string) {
+               if (!empty($this->specialCharacters)) {
+                       return str_replace($this->specialCharacters, '', $string);
+               }
+               
+               return $string;
+       }
 }
index 5dfbbf48ba63f6a9030e2d049541c5f76b161ed6..18e33c79239b72abf93a437ec4e1ef5dc60841bb 100644 (file)
@@ -39,6 +39,13 @@ interface ISearchEngine {
         */
        public function getInnerJoin($objectTypeName, $q, $subjectOnly = false, PreparedStatementConditionBuilder $searchIndexCondition = null, $orderBy = 'time DESC', $limit = 1000);
        
+       /**
+        * Removes engine-specific special characters from a string.
+        * 
+        * @param       string          $string
+        */
+       public function removeSpecialCharacters($string);
+       
        /**
         * Searches for the given string and returns the data of the found messages.
         *
index 2405166b103eec0e06f093561e7c78f73c3b74ac..7696d996e60015e15e29f652c40b896ef1cb6855 100644 (file)
@@ -115,4 +115,11 @@ class SearchEngine extends SingletonFactory implements ISearchEngine {
        public function getConditionBuilderClassName() {
                return $this->getSearchEngine()->getConditionBuilderClassName();
        }
+       
+       /**
+        * @see \wcf\system\search\ISearchEngine::removeSpecialCharacters()
+        */
+       public function removeSpecialCharacters($string) {
+               return $this->getSearchEngine()->removeSpecialCharacters($string);
+       }
 }
index a8d4274d176eaf82fea55b7b1f1d585109658d42..a8371e51f7313c31546ffbca5e3e2a1f53fb5f35 100644 (file)
@@ -25,6 +25,11 @@ class MysqlSearchEngine extends AbstractSearchEngine {
         */
        protected $ftMinWordLen = null;
        
+       /**
+        * @see \wcf\system\search\AbstractSearchEngine::$specialCharacters
+        */
+       protected $specialCharacters = array('(', ')', '@', '+', '-', '"', '<', '>', '~', '*');
+       
        /**
         * @see \wcf\system\search\ISearchEngine::search()
         */