|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Utility class for search functionality. |
|
5
|
|
|
* |
|
6
|
|
|
* @package ElkArte Forum |
|
7
|
|
|
* @copyright ElkArte Forum contributors |
|
8
|
|
|
* @license BSD http://opensource.org/licenses/BSD-3-Clause (see accompanying LICENSE.txt file) |
|
9
|
|
|
* |
|
10
|
|
|
* This file contains code covered by: |
|
11
|
|
|
* copyright: 2011 Simple Machines (http://www.simplemachines.org) |
|
12
|
|
|
* |
|
13
|
|
|
* @version 2.0 Beta 1 |
|
14
|
|
|
* |
|
15
|
|
|
*/ |
|
16
|
|
|
|
|
17
|
|
|
namespace ElkArte\Search; |
|
18
|
|
|
|
|
19
|
|
|
use ElkArte\Errors\Errors; |
|
|
|
|
|
|
20
|
|
|
use ElkArte\Helper\ValuesContainer; |
|
21
|
|
|
use ElkArte\Languages\Txt; |
|
22
|
|
|
use ElkArte\Search\API\AbstractAPI; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Actually do the searches |
|
26
|
|
|
*/ |
|
27
|
|
|
class SearchApiWrapper |
|
28
|
|
|
{ |
|
29
|
|
|
/** @var string when we don't know what to do, use standard search */ |
|
30
|
|
|
public const DEFAULT_API = 'standard'; |
|
31
|
|
|
|
|
32
|
|
|
/** @var object Holds instance of the search api in use such as ElkArte\Search\API\Standard_Search */ |
|
33
|
|
|
protected $_searchAPI; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Constructor |
|
37
|
|
|
* |
|
38
|
|
|
* @param ValuesContainer|string $config The searchAPI |
|
39
|
|
|
* @param SearchParams $searchParams |
|
40
|
|
|
* @package Search |
|
41
|
30 |
|
*/ |
|
42
|
|
|
public function __construct($config, $searchParams = null) |
|
43
|
30 |
|
{ |
|
44
|
|
|
if (!is_object($config)) |
|
45
|
30 |
|
{ |
|
46
|
|
|
$config = new ValuesContainer((array) $config); |
|
47
|
30 |
|
} |
|
48
|
30 |
|
|
|
49
|
|
|
$this->load($config, $searchParams); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Creates a search API and returns the object. |
|
54
|
|
|
* |
|
55
|
|
|
* @param ValuesContainer $config |
|
56
|
30 |
|
* @param SearchParams $searchParams |
|
57
|
|
|
*/ |
|
58
|
30 |
|
protected function load($config, $searchParams): void |
|
59
|
|
|
{ |
|
60
|
30 |
|
global $txt; |
|
61
|
|
|
|
|
62
|
|
|
$searchClass = $config->search_index; |
|
|
|
|
|
|
63
|
30 |
|
|
|
64
|
|
|
require_once(SUBSDIR . '/Package.subs.php'); |
|
65
|
30 |
|
|
|
66
|
|
|
// Load up the search API we are going to use. |
|
67
|
|
|
if (empty($searchClass)) |
|
68
|
|
|
{ |
|
69
|
30 |
|
$searchClass = self::DEFAULT_API; |
|
70
|
30 |
|
} |
|
71
|
|
|
|
|
72
|
|
|
// Try to initialize the API |
|
73
|
30 |
|
$fqcn = '\\ElkArte\\Search\\API\\' . ucfirst($searchClass); |
|
74
|
|
|
|
|
75
|
|
|
if (class_exists($fqcn) && is_a($fqcn, AbstractAPI::class, true)) |
|
76
|
|
|
{ |
|
77
|
30 |
|
// Create an instance of the search API and check it is valid for this version of the software. |
|
78
|
|
|
$this->_searchAPI = new $fqcn($config, $searchParams); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
// An invalid Search API? Log the error and set it to use the standard API |
|
82
|
|
|
if (!$this->_searchAPI || (!$this->_searchAPI->isValid()) || !matchPackageVersion(Search::FORUM_VERSION, $this->_searchAPI->min_elk_version . '-' . $this->_searchAPI->version_compatible)) |
|
83
|
|
|
{ |
|
84
|
|
|
// Log the error. |
|
85
|
30 |
|
Txt::load('Errors'); |
|
86
|
|
|
Errors::instance()->log_error(sprintf($txt['search_api_not_compatible'], $fqcn), 'critical'); |
|
87
|
|
|
|
|
88
|
|
|
$this->_searchAPI = new API\Standard($config, $searchParams); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Wrapper for postCreated of the SearchAPI |
|
94
|
26 |
|
* |
|
95
|
|
|
* @param array $msgOptions |
|
96
|
26 |
|
* @param array $topicOptions |
|
97
|
|
|
* @param array $posterOptions |
|
98
|
|
|
*/ |
|
99
|
|
|
public function postCreated($msgOptions, $topicOptions, $posterOptions): void |
|
100
|
26 |
|
{ |
|
101
|
|
|
if (is_callable([$this->_searchAPI, 'postCreated'])) |
|
102
|
|
|
{ |
|
103
|
|
|
$this->_searchAPI->postCreated($msgOptions, $topicOptions, $posterOptions); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Wrapper for postModified of the SearchAPI |
|
109
|
2 |
|
* |
|
110
|
|
|
* @param array $msgOptions |
|
111
|
2 |
|
* @param array $topicOptions |
|
112
|
|
|
* @param array $posterOptions |
|
113
|
|
|
*/ |
|
114
|
|
|
public function postModified($msgOptions, $topicOptions, $posterOptions): void |
|
115
|
2 |
|
{ |
|
116
|
|
|
if (is_callable([$this->_searchAPI, 'postModified'])) |
|
117
|
|
|
{ |
|
118
|
|
|
$this->_searchAPI->postModified($msgOptions, $topicOptions, $posterOptions); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Wrapper for topicSplit of the SearchAPI |
|
124
|
|
|
* |
|
125
|
|
|
* @param int $split2_ID_TOPIC |
|
126
|
|
|
* @param int[] $splitMessages |
|
127
|
|
|
*/ |
|
128
|
|
|
public function topicSplit($split2_ID_TOPIC, $splitMessages): void |
|
129
|
|
|
{ |
|
130
|
|
|
if (is_callable([$this->_searchAPI, 'topicSplit'])) |
|
131
|
|
|
{ |
|
132
|
|
|
$this->_searchAPI->topicSplit($split2_ID_TOPIC, $splitMessages); |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Wrapper for topicMerge of the SearchAPI |
|
138
|
|
|
* |
|
139
|
|
|
* @param int $id_topic |
|
140
|
|
|
* @param array $topics |
|
141
|
|
|
* @param int[] $affected_msgs |
|
142
|
|
|
* @param string[] $subject array($response_prefix, $target_subject) |
|
143
|
|
|
*/ |
|
144
|
|
|
public function topicMerge($id_topic, $topics, $affected_msgs, $subject): void |
|
145
|
|
|
{ |
|
146
|
|
|
if (is_callable([$this->_searchAPI, 'topicMerge'])) |
|
147
|
|
|
{ |
|
148
|
|
|
$this->_searchAPI->topicMerge($id_topic, $topics, $affected_msgs, $subject); |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
2 |
|
/** |
|
153
|
|
|
* Wrapper for searchSettings of the SearchAPI |
|
154
|
2 |
|
* |
|
155
|
|
|
* @param array $config_vars |
|
156
|
|
|
*/ |
|
157
|
|
|
public function searchSettings(&$config_vars): void |
|
158
|
2 |
|
{ |
|
159
|
|
|
if (is_callable([$this->_searchAPI, 'searchSettings'])) |
|
160
|
|
|
{ |
|
161
|
|
|
$this->_searchAPI->searchSettings($config_vars); |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Wrapper for searchQuery of the SearchAPI |
|
167
|
|
|
* |
|
168
|
|
|
* @param string[] $search_words |
|
169
|
|
|
* @param string[] $excluded_words |
|
170
|
2 |
|
* @param array $participants |
|
171
|
|
|
* |
|
172
|
2 |
|
* @return array |
|
173
|
|
|
*/ |
|
174
|
|
|
public function searchQuery($search_words, $excluded_words, &$participants): array |
|
175
|
|
|
{ |
|
176
|
|
|
return $this->_searchAPI->searchQuery($search_words, $excluded_words, $participants); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Wrapper for prepareWord of the SearchAPI |
|
181
|
|
|
* |
|
182
|
|
|
* @param string $phrase |
|
183
|
|
|
* @param bool $no_regexp |
|
184
|
|
|
* |
|
185
|
|
|
* @return string |
|
186
|
|
|
*/ |
|
187
|
|
|
public function prepareWord($phrase, $no_regexp): string |
|
188
|
|
|
{ |
|
189
|
|
|
return $this->_searchAPI->prepareWord($phrase, $no_regexp); |
|
190
|
2 |
|
} |
|
191
|
|
|
|
|
192
|
2 |
|
/** |
|
193
|
2 |
|
* Wrapper for supportsExtended |
|
194
|
|
|
*/ |
|
195
|
|
|
public function supportsExtended(): bool |
|
196
|
|
|
{ |
|
197
|
|
|
return $this->_searchAPI->supportsExtended(); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
2 |
|
/** |
|
201
|
|
|
* Wrapper for setExcludedPhrases of the SearchAPI |
|
202
|
2 |
|
* |
|
203
|
2 |
|
* @param string[] $phrase An array of phrases to exclude |
|
204
|
|
|
*/ |
|
205
|
|
|
public function setExcludedPhrases($phrase): void |
|
206
|
|
|
{ |
|
207
|
|
|
$this->_searchAPI->setExcludedPhrases($phrase); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
2 |
|
/** |
|
211
|
|
|
* Wrapper for setExcludedWords of the SearchAPI |
|
212
|
2 |
|
* |
|
213
|
2 |
|
* @param string[] $words An array of words to exclude |
|
214
|
|
|
*/ |
|
215
|
|
|
public function setExcludedWords($words): void |
|
216
|
|
|
{ |
|
217
|
|
|
$this->_searchAPI->setExcludedWords($words); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* Wrapper for setSearchArray of the SearchAPI |
|
222
|
|
|
* |
|
223
|
|
|
* @param SearchArray $searchArray |
|
224
|
2 |
|
*/ |
|
225
|
|
|
public function setSearchArray(SearchArray $searchArray): void |
|
226
|
2 |
|
{ |
|
227
|
2 |
|
$this->_searchAPI->setSearchArray($searchArray); |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* Wrapper for prepareIndexes of the SearchAPI |
|
232
|
|
|
* |
|
233
|
|
|
* @param string $word |
|
234
|
|
|
* @param string $wordsSearch |
|
235
|
|
|
* @param string $wordsExclude |
|
236
|
|
|
* @param bool $isExcluded |
|
237
|
|
|
* @param string $excludedSubjectWords |
|
238
|
|
|
*/ |
|
239
|
|
|
public function prepareIndexes($word, &$wordsSearch, &$wordsExclude, $isExcluded, $excludedSubjectWords): void |
|
240
|
|
|
{ |
|
241
|
|
|
$this->_searchAPI->prepareIndexes($word, $wordsSearch, $wordsExclude, $isExcluded, $excludedSubjectWords); |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* Wrapper for searchSort of the SearchAPI |
|
246
|
2 |
|
* |
|
247
|
|
|
* @param string $a Word A |
|
248
|
2 |
|
* @param string $b Word B |
|
249
|
2 |
|
* @return int An integer indicating how the words should be sorted (-1, 0 1) |
|
250
|
|
|
*/ |
|
251
|
|
|
public function searchSort($a, $b): int |
|
252
|
|
|
{ |
|
253
|
|
|
return $this->_searchAPI->searchSort($a, $b); |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
2 |
|
/** |
|
257
|
|
|
* Wrapper for setWeightFactors of the SearchAPI |
|
258
|
2 |
|
* |
|
259
|
2 |
|
* @param WeightFactors $weights |
|
260
|
|
|
*/ |
|
261
|
|
|
public function setWeightFactors(WeightFactors $weights): void |
|
262
|
|
|
{ |
|
263
|
|
|
$this->_searchAPI->setWeightFactors($weights); |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
/** |
|
267
|
|
|
* Wrapper for useTemporary of the SearchAPI |
|
268
|
|
|
* |
|
269
|
|
|
* @param bool $use |
|
270
|
|
|
*/ |
|
271
|
|
|
public function useTemporary($use = false): void |
|
272
|
|
|
{ |
|
273
|
|
|
$this->_searchAPI->useTemporary($use); |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
/** |
|
277
|
|
|
* Returns the number of results obtained from the query. |
|
278
|
|
|
* |
|
279
|
|
|
* @return int |
|
280
|
|
|
*/ |
|
281
|
|
|
public function getNumResults(): int |
|
282
|
|
|
{ |
|
283
|
|
|
return $this->_searchAPI->getNumResults(); |
|
284
|
|
|
} |
|
285
|
|
|
} |
|
286
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths