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