[TASK] Removes extra empty lines
[Packages/TYPO3.CMS.git] / typo3 / sysext / indexed_search / Classes / Utility / IndexedSearchUtility.php
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
17 /**
18 * Class with common methods used across various classes in the indexed search.
19 * Impementation is provided by various people from the TYPO3 community.
20 */
21 class IndexedSearchUtility
22 {
23 /**
24 * Check if the tables provided are configured for usage. This becomes
25 * necessary for extensions that provide additional database functionality
26 * like indexed_search_mysql.
27 *
28 * @param string $tableName Table name to check
29 * @return bool True if the given table is used
30 */
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
37 /**
38 * md5 integer hash
39 * Using 7 instead of 8 just because that makes the integers lower than 32 bit (28 bit) and so they do not interfere with UNSIGNED integers or PHP-versions which has varying output from the hexdec function.
40 *
41 * @param string $stringToHash String to hash
42 * @return int Integer intepretation of the md5 hash of input string.
43 */
44 public static function md5inthash($stringToHash)
45 {
46 return hexdec(substr(md5($stringToHash), 0, 7));
47 }
48
49 /**
50 * Takes a search-string (WITHOUT SLASHES or else it'll be a little sppooky , NOW REMEMBER to unslash!!)
51 * Sets up search words with operators.
52 *
53 * @param string $sword The input search-word string.
54 * @param string $defaultOperator
55 * @param array $operatorTranslateTable
56 * @return array
57 */
58 public static function getExplodedSearchString($sword, $defaultOperator, $operatorTranslateTable)
59 {
60 $swordArray = array();
61 $sword = trim($sword);
62 if ($sword) {
63 $components = self::split($sword);
64 if (is_array($components)) {
65 $i = 0;
66 $lastoper = '';
67 foreach ($components as $key => $val) {
68 $operator = self::getOperator($val, $operatorTranslateTable);
69 if ($operator) {
70 $lastoper = $operator;
71 } elseif (strlen($val) > 1) {
72 // A searchword MUST be at least two characters long!
73 $swordArray[$i]['sword'] = $val;
74 $swordArray[$i]['oper'] = $lastoper ?: $defaultOperator;
75 $lastoper = '';
76 $i++;
77 }
78 }
79 }
80 }
81 return $swordArray;
82 }
83
84 /**
85 * Used to split a search-word line up into elements to search for. This function will detect boolean words like AND and OR, + and -, and even find sentences encapsulated in ""
86 * This function could be re-written to be more clean and effective - yet it's not that important.
87 *
88 * @param string $origSword The raw sword string from outside
89 * @param string $specchars Special chars which are used as operators (+- is default)
90 * @param string $delchars Special chars which are deleted if the append the searchword (+-., is default)
91 * @return mixed Returns an ARRAY if there were search words, otherwise the return value may be unset.
92 */
93 protected static function split($origSword, $specchars = '+-', $delchars = '+.,-')
94 {
95 $value = null;
96 $sword = $origSword;
97 $specs = '[' . preg_quote($specchars, '/') . ']';
98 // As long as $sword is TRUE (that means $sword MUST be reduced little by little until its empty inside the loop!)
99 while ($sword) {
100 // There was a double-quote and we will then look for the ending quote.
101 if (preg_match('/^"/', $sword)) {
102 // Removes first double-quote
103 $sword = preg_replace('/^"/', '', $sword);
104 // Removes everything till next double-quote
105 preg_match('/^[^"]*/', $sword, $reg);
106 // reg[0] is the value, should not be trimmed
107 $value[] = $reg[0];
108 $sword = preg_replace('/^' . preg_quote($reg[0], '/') . '/', '', $sword);
109 // Removes last double-quote
110 $sword = trim(preg_replace('/^"/', '', $sword));
111 } elseif (preg_match('/^' . $specs . '/', $sword, $reg)) {
112 $value[] = $reg[0];
113 // Removes = sign
114 $sword = trim(preg_replace('/^' . $specs . '/', '', $sword));
115 } elseif (preg_match('/[\\+\\-]/', $sword)) {
116 // Check if $sword contains + or -
117 // + and - shall only be interpreted as $specchars when there's whitespace before it
118 // otherwise it's included in the searchword (e.g. "know-how")
119 // explode $sword to single words
120 $a_sword = explode(' ', $sword);
121 // get first word
122 $word = array_shift($a_sword);
123 // Delete $delchars at end of string
124 $word = rtrim($word, $delchars);
125 // add searchword to values
126 $value[] = $word;
127 // re-build $sword
128 $sword = implode(' ', $a_sword);
129 } else {
130 // There are no double-quotes around the value. Looking for next (space) or special char.
131 preg_match('/^[^ ' . preg_quote($specchars, '/') . ']*/', $sword, $reg);
132 // Delete $delchars at end of string
133 $word = rtrim(trim($reg[0]), $delchars);
134 $value[] = $word;
135 $sword = trim(preg_replace('/^' . preg_quote($reg[0], '/') . '/', '', $sword));
136 }
137 }
138 return $value;
139 }
140
141 /**
142 * This returns an SQL search-operator (eg. AND, OR, NOT) translated from the current localized set of operators (eg. in danish OG, ELLER, IKKE).
143 *
144 * @param string $operator The possible operator to find in the internal operator array.
145 * @param array $operatorTranslateTable an array of possible operators
146 * @return string If found, the SQL operator for the localized input operator.
147 */
148 protected static function getOperator($operator, $operatorTranslateTable)
149 {
150 $operator = trim($operator);
151 // case-conversion is charset insensitive, but it doesn't spoil
152 // anything if input string AND operator table is already converted
153 $operator = strtolower($operator);
154 foreach ($operatorTranslateTable as $key => $val) {
155 $item = $operatorTranslateTable[$key][0];
156 // See note above.
157 $item = strtolower($item);
158 if ($operator == $item) {
159 return $operatorTranslateTable[$key][1];
160 }
161 }
162 }
163 }