SearchApiWrapper::topicSplit()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
nc 2
nop 2
ccs 0
cts 1
cp 0
crap 6
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 dev
14
 *
15
 */
16
17
namespace ElkArte\Search;
18
19
use ElkArte\Errors\Errors;
0 ignored issues
show
Bug introduced by
The type ElkArte\Errors\Errors was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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)
59
	{
60 30
		global $txt;
61
62
		$searchClass = $config->search_index;
0 ignored issues
show
Bug Best Practice introduced by
The property search_index does not exist on ElkArte\Helper\ValuesContainer. Since you implemented __get, consider adding a @property annotation.
Loading history...
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)
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)
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)
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)
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)
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)
175
	{
176
		return $this->_searchAPI->searchQuery($search_words, $excluded_words, $participants);
177
	}
178
179
	/**
180
	 * Wrapper for prepareWord of the SearchAPI
181
	 *
182
	 * @return string
183
	 */
184
	public function prepareWord($phrase, $no_regexp)
185
	{
186
		return $this->_searchAPI->prepareWord($phrase, $no_regexp);
187
	}
188
189
	/**
190 2
	 * Wrapper for supportsExtended
191
	 */
192 2
	public function supportsExtended()
193 2
	{
194
		return $this->_searchAPI->supportsExtended();
195
	}
196
197
	/**
198
	 * Wrapper for setExcludedPhrases of the SearchAPI
199
	 *
200 2
	 * @param string[] $phrase An array of phrases to exclude
201
	 */
202 2
	public function setExcludedPhrases($phrase)
203 2
	{
204
		$this->_searchAPI->setExcludedPhrases($phrase);
205
	}
206
207
	/**
208
	 * Wrapper for setExcludedWords of the SearchAPI
209
	 *
210 2
	 * @param string[] $words An array of words to exclude
211
	 */
212 2
	public function setExcludedWords($words)
213 2
	{
214
		$this->_searchAPI->setExcludedWords($words);
215
	}
216
217
	/**
218
	 * Wrapper for setSearchArray of the SearchAPI
219
	 *
220
	 * @param SearchArray $searchArray
221
	 */
222
	public function setSearchArray(SearchArray $searchArray)
223
	{
224 2
		$this->_searchAPI->setSearchArray($searchArray);
225
	}
226 2
227 2
	/**
228
	 * Wrapper for prepareIndexes of the SearchAPI
229
	 *
230
	 * @param string $word
231
	 * @param string $wordsSearch
232
	 * @param string $wordsExclude
233
	 * @param boolean $isExcluded
234
	 * @param string $excludedSubjectWords
235
	 */
236
	public function prepareIndexes($word, &$wordsSearch, &$wordsExclude, $isExcluded, $excludedSubjectWords)
237
	{
238
		$this->_searchAPI->prepareIndexes($word, $wordsSearch, $wordsExclude, $isExcluded, $excludedSubjectWords);
239
	}
240
241
	/**
242
	 * Wrapper for searchSort of the SearchAPI
243
	 *
244
	 * @param string $a Word A
245
	 * @param string $b Word B
246 2
	 * @return int An integer indicating how the words should be sorted (-1, 0 1)
247
	 */
248 2
	public function searchSort($a, $b)
249 2
	{
250
		return $this->_searchAPI->searchSort($a, $b);
251
	}
252
253
	/**
254
	 * Wrapper for setWeightFactors of the SearchAPI
255
	 *
256 2
	 * @param WeightFactors $weights
257
	 */
258 2
	public function setWeightFactors(WeightFactors $weights)
259 2
	{
260
		$this->_searchAPI->setWeightFactors($weights);
261
	}
262
263
	/**
264
	 * Wrapper for useTemporary of the SearchAPI
265
	 *
266
	 * @param bool $use
267
	 */
268
	public function useTemporary($use = false)
269
	{
270
		$this->_searchAPI->useTemporary($use);
271
	}
272
273
	/**
274
	 * Returns the number of results obtained from the query.
275
	 *
276
	 * @return int
277
	 */
278
	public function getNumResults()
279
	{
280
		return $this->_searchAPI->getNumResults();
281
	}
282
}
283