Completed
Push — master ( acff9d...ad01e4 )
by
unknown
38:21 queued 23:29
created

IndexedSearchUtility   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 153
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 55
dl 0
loc 153
rs 10
c 0
b 0
f 0
wmc 18

6 Methods

Rating   Name   Duplication   Size   Complexity  
A split() 0 46 5
A isTableUsed() 0 4 1
A md5inthash() 0 3 1
B getExplodedSearchString() 0 24 7
A getOperator() 0 16 3
A milliseconds() 0 3 1
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
 * Implementation is provided by various people from the TYPO3 community.
20
 * @internal
21
 */
22
class IndexedSearchUtility
23
{
24
    /**
25
     * Check if the tables provided are configured for usage. This becomes
26
     * necessary for extensions that provide additional database functionality
27
     * like indexed_search_mysql.
28
     *
29
     * @param string $tableName Table name to check
30
     * @return bool True if the given table is used
31
     */
32
    public static function isTableUsed($tableName)
33
    {
34
        $tableList = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['indexed_search']['use_tables'];
35
        return \TYPO3\CMS\Core\Utility\GeneralUtility::inList($tableList, $tableName);
36
    }
37
38
    /**
39
     * md5 integer hash
40
     * 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.
41
     *
42
     * @param string $stringToHash String to hash
43
     * @return int Integer interpretation of the md5 hash of input string.
44
     */
45
    public static function md5inthash($stringToHash)
46
    {
47
        return hexdec(substr(md5($stringToHash), 0, 7));
48
    }
49
50
    /**
51
     * Takes a search-string (WITHOUT SLASHES or else it'll be a little spooky , NOW REMEMBER to unslash!!)
52
     * Sets up search words with operators.
53
     *
54
     * @param string $sword The input search-word string.
55
     * @param string $defaultOperator
56
     * @param array $operatorTranslateTable
57
     * @return array
58
     */
59
    public static function getExplodedSearchString($sword, $defaultOperator, $operatorTranslateTable)
60
    {
61
        $swordArray = [];
62
        $sword = trim($sword);
63
        if ($sword) {
64
            $components = self::split($sword);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $components is correct as self::split($sword) targeting TYPO3\CMS\IndexedSearch\...dSearchUtility::split() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
65
            if (is_array($components)) {
0 ignored issues
show
introduced by
The condition is_array($components) is always false.
Loading history...
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
85
    /**
86
     * 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 ""
87
     * This function could be re-written to be more clean and effective - yet it's not that important.
88
     *
89
     * @param string $origSword The raw sword string from outside
90
     * @param string $specchars Special chars which are used as operators (+- is default)
91
     * @param string $delchars Special chars which are deleted if the append the searchword (+-., is default)
92
     * @return mixed Returns an ARRAY if there were search words, otherwise the return value may be unset.
93
     */
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
142
    /**
143
     * This returns an SQL search-operator (eg. AND, OR, NOT) translated from the current localized set of operators (eg. in danish OG, ELLER, IKKE).
144
     *
145
     * @param string $operator The possible operator to find in the internal operator array.
146
     * @param array $operatorTranslateTable an array of possible operators
147
     * @return string|null If found, the SQL operator for the localized input operator.
148
     */
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
        return null;
165
    }
166
167
    /**
168
     * Gets the unixtime as milliseconds.
169
     *
170
     * @return int The unixtime as milliseconds
171
     */
172
    public static function milliseconds()
173
    {
174
        return round(microtime(true) * 1000);
175
    }
176
}
177