TYPO3  7.6
IndexedSearchUtility.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\IndexedSearch\Utility;
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
22 {
31  public static function isTableUsed($tableName)
32  {
33  $tableList = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['indexed_search']['use_tables'];
34  return \TYPO3\CMS\Core\Utility\GeneralUtility::inList($tableList, $tableName);
35  }
36 
44  public static function md5inthash($stringToHash)
45  {
46  return hexdec(substr(md5($stringToHash), 0, 7));
47  }
48 
49 
59  public static function getExplodedSearchString($sword, $defaultOperator, $operatorTranslateTable)
60  {
61  $swordArray = array();
62  $sword = trim($sword);
63  if ($sword) {
64  $components = self::split($sword);
65  if (is_array($components)) {
66  $i = 0;
67  $lastoper = '';
68  foreach ($components as $key => $val) {
69  $operator = self::getOperator($val, $operatorTranslateTable);
70  if ($operator) {
71  $lastoper = $operator;
72  } elseif (strlen($val) > 1) {
73  // A searchword MUST be at least two characters long!
74  $swordArray[$i]['sword'] = $val;
75  $swordArray[$i]['oper'] = $lastoper ?: $defaultOperator;
76  $lastoper = '';
77  $i++;
78  }
79  }
80  }
81  }
82  return $swordArray;
83  }
84 
94  protected static function split($origSword, $specchars = '+-', $delchars = '+.,-')
95  {
96  $value = null;
97  $sword = $origSword;
98  $specs = '[' . preg_quote($specchars, '/') . ']';
99  // As long as $sword is TRUE (that means $sword MUST be reduced little by little until its empty inside the loop!)
100  while ($sword) {
101  // There was a double-quote and we will then look for the ending quote.
102  if (preg_match('/^"/', $sword)) {
103  // Removes first double-quote
104  $sword = preg_replace('/^"/', '', $sword);
105  // Removes everything till next double-quote
106  preg_match('/^[^"]*/', $sword, $reg);
107  // reg[0] is the value, should not be trimmed
108  $value[] = $reg[0];
109  $sword = preg_replace('/^' . preg_quote($reg[0], '/') . '/', '', $sword);
110  // Removes last double-quote
111  $sword = trim(preg_replace('/^"/', '', $sword));
112  } elseif (preg_match('/^' . $specs . '/', $sword, $reg)) {
113  $value[] = $reg[0];
114  // Removes = sign
115  $sword = trim(preg_replace('/^' . $specs . '/', '', $sword));
116  } elseif (preg_match('/[\\+\\-]/', $sword)) {
117  // Check if $sword contains + or -
118  // + and - shall only be interpreted as $specchars when there's whitespace before it
119  // otherwise it's included in the searchword (e.g. "know-how")
120  // explode $sword to single words
121  $a_sword = explode(' ', $sword);
122  // get first word
123  $word = array_shift($a_sword);
124  // Delete $delchars at end of string
125  $word = rtrim($word, $delchars);
126  // add searchword to values
127  $value[] = $word;
128  // re-build $sword
129  $sword = implode(' ', $a_sword);
130  } else {
131  // There are no double-quotes around the value. Looking for next (space) or special char.
132  preg_match('/^[^ ' . preg_quote($specchars, '/') . ']*/', $sword, $reg);
133  // Delete $delchars at end of string
134  $word = rtrim(trim($reg[0]), $delchars);
135  $value[] = $word;
136  $sword = trim(preg_replace('/^' . preg_quote($reg[0], '/') . '/', '', $sword));
137  }
138  }
139  return $value;
140  }
141 
149  protected static function getOperator($operator, $operatorTranslateTable)
150  {
151  $operator = trim($operator);
152  // case-conversion is charset insensitive, but it doesn't spoil
153  // anything if input string AND operator table is already converted
154  $operator = strtolower($operator);
155  foreach ($operatorTranslateTable as $key => $val) {
156  $item = $operatorTranslateTable[$key][0];
157  // See note above.
158  $item = strtolower($item);
159  if ($operator == $item) {
160  return $operatorTranslateTable[$key][1];
161  }
162  }
163  }
164 }