Fixed bug #14021: Simplify the code to get nested GET Parameters with TS
[Packages/TYPO3.CMS.git] / typo3 / sysext / cms / tslib / class.tslib_search.php
old mode 100755 (executable)
new mode 100644 (file)
index ce30e35..92ff3e2
@@ -2,7 +2,7 @@
 /***************************************************************
 *  Copyright notice
 *
-*  (c) 1999-2004 Kasper Skaarhoj (kasper@typo3.com)
+*  (c) 1999-2009 Kasper Skaarhoj (kasperYYYY@typo3.com)
 *  All rights reserved
 *
 *  This script is part of the TYPO3 project. The TYPO3 project is
@@ -32,7 +32,7 @@
  * $Id$
  * Revised for TYPO3 3.6 June/2003 by Kasper Skaarhoj
  *
- * @author     Kasper Skaarhoj <kasper@typo3.com>
+ * @author     Kasper Skaarhoj <kasperYYYY@typo3.com>
  * @author     Rene Fritz      <r.fritz@colorcube.de>
  */
 /**
  *
  *
  *   88: class tslib_search
- *  130:     function register_tables_and_columns($requestedCols,$allowedCols)
- *  171:     function explodeCols($in)
- *  196:     function register_and_explode_search_string($sword)
- *  229:     function split($origSword, $specchars='+-', $delchars='+-.,')
- *  272:     function quotemeta($str)
- *  288:     function build_search_query($endClause)
- *  374:     function build_search_query_for_searchwords()
- *  416:     function get_operator($operator)
- *  439:     function count_query()
- *  452:     function execute_query()
- *  465:     function get_searchwords()
- *  480:     function get_searchwordsArray()
+ *  127:     function register_tables_and_columns($requestedCols,$allowedCols)
+ *  168:     function explodeCols($in)
+ *  193:     function register_and_explode_search_string($sword)
+ *  226:     function split($origSword, $specchars='+-', $delchars='+.,-')
+ *  269:     function quotemeta($str)
+ *  285:     function build_search_query($endClause)
+ *  371:     function build_search_query_for_searchwords()
+ *  413:     function get_operator($operator)
+ *  436:     function count_query()
+ *  449:     function execute_query()
+ *  462:     function get_searchwords()
+ *  477:     function get_searchwordsArray()
  *
  * TOTAL FUNCTIONS: 12
  * (This index is automatically created/updated by the extension "extdeveval")
@@ -80,7 +80,7 @@
 /**
  * Search class used for the content object SEARCHRESULT
  *
- * @author     Kasper Skaarhoj <kasper@typo3.com>
+ * @author     Kasper Skaarhoj <kasperYYYY@typo3.com>
  * @package TYPO3
  * @subpackage tslib
  * @see        tslib_cObj::SEARCHRESULT()
@@ -90,18 +90,15 @@ class tslib_search {
 
        var $group_by = 'PRIMARY_KEY';                                                  // Alternatively 'PRIMARY_KEY'; sorting by primary key
        var $default_operator = 'AND';                                                  // Standard SQL-operator between words
-       var $operator_translate_table_caseinsensitive = '1';
+       var $operator_translate_table_caseinsensitive = TRUE;
        var $operator_translate_table = Array (                                 // case-sensitiv. Defineres the words, which will be operators between words
                Array ('+' , 'AND'),
+               Array ('|' , 'AND'),
                Array ('-' , 'AND NOT'),
                        // english
-               Array ('AND' , 'AND'),
-               Array ('OR' , 'OR'),
-               Array ('NOT' , 'AND NOT'),
-                       // danish
-               Array ('OG' , 'AND'),
-               Array ('ELLER' , 'OR'),
-               Array ('UDEN' , 'AND NOT')
+               Array ('and' , 'AND'),
+               Array ('or' , 'OR'),
+               Array ('not' , 'AND NOT'),
        );
 
        // Internal
@@ -229,33 +226,32 @@ class tslib_search {
        function split($origSword, $specchars='+-', $delchars='+.,-')   {
                $sword = $origSword;
                $specs = '['.$this->quotemeta($specchars).']';
-               $delchars = '['.$this->quotemeta($delchars).']';
 
                        // As long as $sword is true (that means $sword MUST be reduced little by little until its empty inside the loop!)
                while ($sword)  {
-                       if (ereg('^"',$sword))  {               // There was a double-quote and we will then look for the ending quote.
-                               $sword = ereg_replace('^"','',$sword);          // Removes first double-quote
-                               ereg('^[^"]*',$sword,$reg);  // Removes everything till next double-quote
+                       if (preg_match('/^"/',$sword))  {               // There was a double-quote and we will then look for the ending quote.
+                               $sword = preg_replace('/^"/','',$sword);                // Removes first double-quote
+                               preg_match('/^[^"]*/',$sword,$reg);  // Removes everything till next double-quote
                                $value[] = $reg[0];  // reg[0] is the value, should not be trimmed
-                               $sword = ereg_replace('^'.$this->quotemeta($reg[0]),'',$sword);
-                               $sword = trim(ereg_replace('^"','',$sword));            // Removes last double-quote
-                       } elseif (ereg('^'.$specs,$sword,$reg)) {
+                               $sword = preg_replace('/^'.$this->quotemeta($reg[0]).'/','',$sword);
+                               $sword = trim(preg_replace('/^"/','',$sword));          // Removes last double-quote
+                       } elseif (preg_match('/^'.$specs.'/',$sword,$reg)) {
                                $value[] = $reg[0];
-                               $sword = trim(ereg_replace('^'.$specs,'',$sword));              // Removes = sign
-                       } elseif (ereg('[\+\-]',$sword)) {      // Check if $sword contains + or -
+                               $sword = trim(preg_replace('/^'.$specs.'/','',$sword));         // Removes = sign
+                       } elseif (preg_match('/[\+\-]/',$sword)) {      // Check if $sword contains + or -
                                        // + and - shall only be interpreted as $specchars when there's whitespace before it
                                        // otherwise it's included in the searchword (e.g. "know-how")
                                $a_sword = explode(' ',$sword); // explode $sword to single words
                                $word = array_shift($a_sword);  // get first word
-                               $word = ereg_replace($delchars.'$','',$word);           // Delete $delchars at end of string
+                               $word = rtrim($word, $delchars);                // Delete $delchars at end of string
                                $value[] = $word;       // add searchword to values
                                $sword = implode(' ',$a_sword); // re-build $sword
                        } else {
                                        // There are no double-quotes around the value. Looking for next (space) or special char.
-                               ereg('^[^ '.$this->quotemeta($specchars).']*',$sword,$reg);
-                               $word = ereg_replace($delchars.'$','',trim($reg[0]));           // Delete $delchars at end of string
+                               preg_match('/^[^ '.$this->quotemeta($specchars).']*/',$sword,$reg);
+                               $word = rtrim(trim($reg[0]), $delchars);                // Delete $delchars at end of string
                                $value[] = $word;
-                               $sword = trim(ereg_replace('^'.$this->quotemeta($reg[0]),'',$sword));
+                               $sword = trim(preg_replace('/^'.$this->quotemeta($reg[0]).'/','',$sword));
                        }
                }
 
@@ -388,7 +384,7 @@ class tslib_search {
                                        if (is_array($searchfields))    {
                                                foreach($searchfields as $key2 => $val2)        {
                                                        $this->listOfSearchFields.= $key3.'.'.$val2.',';
-                                                       $sub_query_part[] = $key3.'.'.$val2.' LIKE "%'.$GLOBALS['TYPO3_DB']->quoteStr($s_sword, $key3).'%"';
+                                                       $sub_query_part[] = $key3.'.'.$val2.' LIKE \'%'.$GLOBALS['TYPO3_DB']->quoteStr($s_sword, $key3).'%\'';
                                                }
                                        }
                                }
@@ -418,12 +414,12 @@ class tslib_search {
                $op_array = $this->operator_translate_table;
                reset ($op_array);
                if ($this->operator_translate_table_caseinsensitive)    {
-                       $operator = strtoupper($operator);
+                       $operator = strtolower($operator);      // case-conversion is charset insensitive, but it doesn't spoil anything if input string AND operator table is already converted
                }
                while (list($key,$val) = each($op_array))       {
                        $item = $op_array[$key][0];
                        if ($this->operator_translate_table_caseinsensitive)    {
-                               $item = strtoupper($item);
+                               $item = strtolower($item);      // See note above.
                        }
                        if ($operator==$item)   {
                                return $op_array[$key][1];
@@ -463,10 +459,10 @@ class tslib_search {
         * @return      string          URL-parameters with the searchwords
         */
        function get_searchwords()      {
-               $SWORD_PARAMS='';
+               $SWORD_PARAMS = '';
                if (is_array($this->sword_array))       {
                        foreach($this->sword_array as $key => $val)     {
-                               $SWORD_PARAMS.='&sword_list[]='.rawurlencode($val['sword']);
+                               $SWORD_PARAMS.= '&sword_list[]='.rawurlencode($val['sword']);
                        }
                }
                return $SWORD_PARAMS;
@@ -480,7 +476,7 @@ class tslib_search {
        function get_searchwordsArray() {
                if (is_array($this->sword_array))       {
                        foreach($this->sword_array as $key => $val)     {
-                               $swords[]=$val['sword'];
+                               $swords[] = $val['sword'];
                        }
                }
                return $swords;
@@ -494,4 +490,4 @@ if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['tslib/class
        include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['tslib/class.tslib_search.php']);
 }
 
-?>
+?>
\ No newline at end of file