‪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 
22 
29 {
36  public static function ‪getExplodedSearchString(string $sword, string $defaultOperator, array $operatorTranslateTable): array
37  {
38  $swordArray = [];
39  $sword = trim($sword);
40  if ($sword) {
41  $components = ‪self::split($sword);
42  if (is_array($components)) {
43  $i = 0;
44  $lastoper = '';
45  foreach ($components as $key => $val) {
46  $operator = ‪self::getOperator($val, $operatorTranslateTable);
47  if ($operator) {
48  $lastoper = $operator;
49  } elseif (strlen($val) > 1) {
50  // A searchword MUST be at least two characters long!
51  $swordArray[$i]['sword'] = $val;
52  $swordArray[$i]['oper'] = $lastoper ?: $defaultOperator;
53  $lastoper = '';
54  $i++;
55  }
56  }
57  }
58  }
59  return $swordArray;
60  }
61 
71  protected static function ‪split(string $origSword, string $specchars = '+-', string $delchars = '+.,-'): ?array
72  {
73  $value = null;
74  $sword = $origSword;
75  $specs = '[' . preg_quote($specchars, '/') . ']';
76  // As long as $sword is TRUE (that means $sword MUST be reduced little by little until its empty inside the loop!)
77  while ($sword) {
78  // There was a double-quote and we will then look for the ending quote.
79  if (preg_match('/^"/', $sword)) {
80  // Removes first double-quote
81  $sword = (string)preg_replace('/^"/', '', $sword);
82  // Removes everything till next double-quote
83  preg_match('/^[^"]*/', $sword, $reg);
84  // reg[0] is the value, should not be trimmed
85  $value[] = (string)$reg[0];
86  $sword = (string)preg_replace('/^' . preg_quote($reg[0], '/') . '/', '', $sword);
87  // Removes last double-quote
88  $sword = trim((string)preg_replace('/^"/', '', $sword));
89  } elseif (preg_match('/^' . $specs . '/', $sword, $reg)) {
90  $value[] = (string)$reg[0];
91  // Removes = sign
92  $sword = trim((string)preg_replace('/^' . $specs . '/', '', $sword));
93  } elseif (preg_match('/[\\+\\-]/', $sword)) {
94  // Check if $sword contains + or -
95  // + and - shall only be interpreted as $specchars when there's whitespace before it
96  // otherwise it's included in the searchword (e.g. "know-how")
97  // explode $sword to single words
98  $a_sword = explode(' ', $sword);
99  // get first word
100  $word = (string)array_shift($a_sword);
101  // Delete $delchars at end of string
102  $word = rtrim($word, $delchars);
103  // add searchword to values
104  $value[] = $word;
105  // re-build $sword
106  $sword = implode(' ', $a_sword);
107  } else {
108  // There are no double-quotes around the value. Looking for next (space) or special char.
109  preg_match('/^[^ ' . preg_quote($specchars, '/') . ']*/', $sword, $reg);
110  // Delete $delchars at end of string
111  $word = rtrim(trim($reg[0]), $delchars);
112  $value[] = $word;
113  $sword = trim((string)preg_replace('/^' . preg_quote($reg[0], '/') . '/', '', $sword));
114  }
115  }
116  return $value;
117  }
118 
126  protected static function ‪getOperator(string $operator, array $operatorTranslateTable): ?string
127  {
128  $operator = trim($operator);
129  // case-conversion is charset insensitive, but it doesn't spoil
130  // anything if input string AND operator table is already converted
131  $operator = strtolower($operator);
132  foreach ($operatorTranslateTable as $key => $val) {
133  $item = $operatorTranslateTable[$key][0];
134  // See note above.
135  $item = strtolower($item);
136  if ($operator == $item) {
137  return $operatorTranslateTable[$key][1];
138  }
139  }
140 
141  return null;
142  }
143 
148  public static function ‪isMysqlFullTextEnabled(): bool
149  {
150  $extConf = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get('indexed_search');
151  return (bool)($extConf['useMysqlFulltext'] ?? false);
152  }
153 }
‪TYPO3\CMS\Core\Configuration\ExtensionConfiguration
Definition: ExtensionConfiguration.php:47
‪TYPO3\CMS\IndexedSearch\Utility\IndexedSearchUtility\isMysqlFullTextEnabled
‪static isMysqlFullTextEnabled()
Definition: IndexedSearchUtility.php:148
‪TYPO3\CMS\IndexedSearch\Utility
Definition: IndexedSearchUtility.php:18
‪TYPO3\CMS\IndexedSearch\Utility\IndexedSearchUtility\getOperator
‪static string null getOperator(string $operator, array $operatorTranslateTable)
Definition: IndexedSearchUtility.php:126
‪TYPO3\CMS\IndexedSearch\Utility\IndexedSearchUtility\split
‪static array< string > null split(string $origSword, string $specchars='+-', string $delchars='+.,-')
Definition: IndexedSearchUtility.php:71
‪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:36
‪TYPO3\CMS\IndexedSearch\Utility\IndexedSearchUtility
Definition: IndexedSearchUtility.php:29