We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Total Complexity | 64 |
Total Lines | 664 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like Solr often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Solr, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
45 | class Solr implements LoggerAwareInterface |
||
46 | { |
||
47 | use LoggerAwareTrait; |
||
48 | |||
49 | /** |
||
50 | * @access protected |
||
51 | * @var array This holds the Solr configuration |
||
52 | */ |
||
53 | protected array $config = []; |
||
54 | |||
55 | /** |
||
56 | * @access protected |
||
57 | * @var string|null This holds the core name |
||
58 | */ |
||
59 | protected ?string $core = null; |
||
60 | |||
61 | /** |
||
62 | * @access protected |
||
63 | * @var int This holds the PID for the configuration |
||
64 | */ |
||
65 | protected int $configPid = 0; |
||
66 | |||
67 | /** |
||
68 | * @access public |
||
69 | * @static |
||
70 | * @var string The extension key |
||
71 | */ |
||
72 | public static string $extKey = 'dlf'; |
||
73 | |||
74 | /** |
||
75 | * @access public |
||
76 | * @static |
||
77 | * @var array The fields for SOLR index |
||
78 | */ |
||
79 | public static array $fields = []; |
||
80 | |||
81 | /** |
||
82 | * @access protected |
||
83 | * @var int This holds the max results |
||
84 | */ |
||
85 | protected int $limit = 50000; |
||
86 | |||
87 | /** |
||
88 | * @access protected |
||
89 | * @var int This holds the number of hits for last search |
||
90 | */ |
||
91 | protected int $numberOfHits = 0; |
||
92 | |||
93 | /** |
||
94 | * @access protected |
||
95 | * @var array This holds the additional query parameters |
||
96 | */ |
||
97 | protected array $params = []; |
||
98 | |||
99 | /** |
||
100 | * @access protected |
||
101 | * @var bool Is the search instantiated successfully? |
||
102 | */ |
||
103 | protected bool $ready = false; |
||
104 | |||
105 | /** |
||
106 | * @access protected |
||
107 | * @var array(Solr) This holds the singleton search objects with their core as array key |
||
108 | */ |
||
109 | protected static array $registry = []; |
||
110 | |||
111 | /** |
||
112 | * @access protected |
||
113 | * @var Client This holds the Solr service object |
||
114 | */ |
||
115 | protected Client $service; |
||
116 | |||
117 | /** |
||
118 | * Add a new core to Apache Solr |
||
119 | * |
||
120 | * @access public |
||
121 | * |
||
122 | * @param string $core The name of the new core. If empty, the next available core name is used. |
||
123 | * |
||
124 | * @return string The name of the new core |
||
125 | */ |
||
126 | public static function createCore($core = ''): string |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * Escape special characters in a query string |
||
167 | * |
||
168 | * @access public |
||
169 | * |
||
170 | * @param string $query The query string |
||
171 | * |
||
172 | * @return string The escaped query string |
||
173 | */ |
||
174 | public static function escapeQuery(string $query): string |
||
175 | { |
||
176 | // Escape query by disallowing range and field operators |
||
177 | // Permit operators: wildcard, boolean, fuzzy, proximity, boost, grouping |
||
178 | // https://solr.apache.org/guide/solr/latest/query-guide/standard-query-parser.html |
||
179 | return preg_replace('/(\{|}|\[|]|:|\/|\\\)/', '\\\$1', $query); |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * Escape all special characters in a query string while retaining valid field queries |
||
184 | * |
||
185 | * @access public |
||
186 | * |
||
187 | * @param string $query The query string |
||
188 | * @param int $pid The PID for the field configuration |
||
189 | * |
||
190 | * @return string The escaped query string |
||
191 | */ |
||
192 | public static function escapeQueryKeepField(string $query, int $pid): string |
||
193 | { |
||
194 | // Is there a field query? |
||
195 | if (preg_match('/^[[:alnum:]]+_[tu][su]i:\(?.*\)?$/', $query)) { |
||
196 | $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) |
||
197 | ->getQueryBuilderForTable('tx_dlf_metadata'); |
||
198 | |||
199 | // Get all indexed fields. |
||
200 | $fields = []; |
||
201 | $result = $queryBuilder |
||
202 | ->select( |
||
203 | 'tx_dlf_metadata.index_name AS index_name', |
||
204 | 'tx_dlf_metadata.index_tokenized AS index_tokenized', |
||
205 | 'tx_dlf_metadata.index_stored AS index_stored' |
||
206 | ) |
||
207 | ->from('tx_dlf_metadata') |
||
208 | ->where( |
||
209 | $queryBuilder->expr()->eq('tx_dlf_metadata.index_indexed', 1), |
||
210 | $queryBuilder->expr()->eq('tx_dlf_metadata.pid', (int) $pid), |
||
211 | $queryBuilder->expr()->orX( |
||
212 | $queryBuilder->expr()->in('tx_dlf_metadata.sys_language_uid', [-1, 0]), |
||
213 | $queryBuilder->expr()->eq('tx_dlf_metadata.l18n_parent', 0) |
||
214 | ), |
||
215 | Helper::whereExpression('tx_dlf_metadata') |
||
216 | ) |
||
217 | ->execute(); |
||
218 | |||
219 | while ($resArray = $result->fetchAssociative()) { |
||
220 | $fields[] = $resArray['index_name'] . '_' . ($resArray['index_tokenized'] ? 't' : 'u') . ($resArray['index_stored'] ? 's' : 'u') . 'i'; |
||
221 | } |
||
222 | |||
223 | // Check if queried field is valid. |
||
224 | $splitQuery = explode(':', $query, 2); |
||
225 | if (in_array($splitQuery[0], $fields)) { |
||
226 | $query = $splitQuery[0] . ':(' . self::escapeQuery(trim($splitQuery[1], '()')) . ')'; |
||
227 | } else { |
||
228 | $query = self::escapeQuery($query); |
||
229 | } |
||
230 | } else { |
||
231 | $query = self::escapeQuery($query); |
||
232 | } |
||
233 | return $query; |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * Get fields for index. |
||
238 | * |
||
239 | * @access public |
||
240 | * |
||
241 | * @return array fields |
||
242 | */ |
||
243 | public static function getFields(): array |
||
244 | { |
||
245 | if (empty(self::$fields)) { |
||
246 | $conf = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get(self::$extKey, 'solr'); |
||
247 | $solrFields = $conf['fields']; |
||
248 | self::$fields['id'] = $solrFields['id']; |
||
249 | self::$fields['uid'] = $solrFields['uid']; |
||
250 | self::$fields['pid'] = $solrFields['pid']; |
||
251 | self::$fields['page'] = $solrFields['page']; |
||
252 | self::$fields['partof'] = $solrFields['partof']; |
||
253 | self::$fields['root'] = $solrFields['root']; |
||
254 | self::$fields['sid'] = $solrFields['sid']; |
||
255 | self::$fields['toplevel'] = $solrFields['toplevel']; |
||
256 | self::$fields['type'] = $solrFields['type']; |
||
257 | self::$fields['title'] = $solrFields['title']; |
||
258 | self::$fields['volume'] = $solrFields['volume']; |
||
259 | self::$fields['date'] = $solrFields['date']; |
||
260 | self::$fields['thumbnail'] = $solrFields['thumbnail']; |
||
261 | self::$fields['default'] = $solrFields['default']; |
||
262 | self::$fields['timestamp'] = $solrFields['timestamp']; |
||
263 | self::$fields['autocomplete'] = $solrFields['autocomplete']; |
||
264 | self::$fields['fulltext'] = $solrFields['fulltext']; |
||
265 | self::$fields['record_id'] = $solrFields['recordId']; |
||
266 | self::$fields['purl'] = $solrFields['purl']; |
||
267 | self::$fields['urn'] = $solrFields['urn']; |
||
268 | self::$fields['location'] = $solrFields['location']; |
||
269 | self::$fields['collection'] = $solrFields['collection']; |
||
270 | self::$fields['license'] = $solrFields['license']; |
||
271 | self::$fields['terms'] = $solrFields['terms']; |
||
272 | self::$fields['restrictions'] = $solrFields['restrictions']; |
||
273 | self::$fields['geom'] = $solrFields['geom']; |
||
274 | } |
||
275 | |||
276 | return self::$fields; |
||
277 | } |
||
278 | |||
279 | /** |
||
280 | * This is a singleton class, thus instances must be created by this method |
||
281 | * |
||
282 | * @access public |
||
283 | * |
||
284 | * @param mixed $core Name or UID of the core to load or null to get core admin endpoint |
||
285 | * |
||
286 | * @return Solr Instance of this class |
||
287 | */ |
||
288 | public static function getInstance($core = null): Solr |
||
289 | { |
||
290 | // Get core name if UID is given. |
||
291 | if (MathUtility::canBeInterpretedAsInteger($core)) { |
||
292 | $core = Helper::getIndexNameFromUid($core, 'tx_dlf_solrcores'); |
||
|
|||
293 | } |
||
294 | // Check if core is set or null. |
||
295 | if ( |
||
296 | empty($core) |
||
297 | && $core !== null |
||
298 | ) { |
||
299 | Helper::log('Invalid core UID or name given for Apache Solr', LOG_SEVERITY_ERROR); |
||
300 | } |
||
301 | if (!empty($core)) { |
||
302 | // Check if there is an instance in the registry already. |
||
303 | if ( |
||
304 | array_key_exists($core, self::$registry) |
||
305 | && self::$registry[$core] instanceof self |
||
306 | ) { |
||
307 | // Return singleton instance if available. |
||
308 | return self::$registry[$core]; |
||
309 | } |
||
310 | } |
||
311 | // Create new instance... |
||
312 | $instance = new self($core); |
||
313 | // ...and save it to registry. |
||
314 | if (!empty($instance->core)) { |
||
315 | self::$registry[$instance->core] = $instance; |
||
316 | } |
||
317 | return $instance; |
||
318 | } |
||
319 | |||
320 | /** |
||
321 | * Get next unused Solr core number |
||
322 | * |
||
323 | * @access public |
||
324 | * |
||
325 | * @param int $number Number to start with |
||
326 | * |
||
327 | * @return int First unused core number found |
||
328 | */ |
||
329 | public static function getNextCoreNumber(int $number = 0): int |
||
330 | { |
||
331 | $number = max($number, 0); |
||
332 | // Check if core already exists. |
||
333 | $solr = self::getInstance('dlfCore' . $number); |
||
334 | if (!$solr->ready) { |
||
335 | return $number; |
||
336 | } else { |
||
337 | return self::getNextCoreNumber($number + 1); |
||
338 | } |
||
339 | } |
||
340 | |||
341 | /** |
||
342 | * Sets the connection information for Solr |
||
343 | * |
||
344 | * @access protected |
||
345 | * |
||
346 | * @return void |
||
347 | */ |
||
348 | protected function loadSolrConnectionInfo(): void |
||
349 | { |
||
350 | if (empty($this->config)) { |
||
351 | $config = []; |
||
352 | // Extract extension configuration. |
||
353 | $conf = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get(self::$extKey, 'solr'); |
||
354 | // Derive Solr scheme |
||
355 | $config['scheme'] = empty($conf['https']) ? 'http' : 'https'; |
||
356 | // Derive Solr host name. |
||
357 | $config['host'] = ($conf['host'] ? $conf['host'] : '127.0.0.1'); |
||
358 | // Set username and password. |
||
359 | $config['username'] = $conf['user']; |
||
360 | $config['password'] = $conf['pass']; |
||
361 | // Set port if not set. |
||
362 | $config['port'] = MathUtility::forceIntegerInRange($conf['port'], 1, 65535, 8983); |
||
363 | // Trim path of slashes and (re-)add trailing slash if path not empty. |
||
364 | $config['path'] = trim($conf['path'], '/'); |
||
365 | if (!empty($config['path'])) { |
||
366 | $config['path'] .= '/'; |
||
367 | } |
||
368 | |||
369 | // Set connection timeout lower than PHP's max_execution_time. |
||
370 | $maxExecutionTime = (int) ini_get('max_execution_time') ? : 30; |
||
371 | $config['timeout'] = MathUtility::forceIntegerInRange($conf['timeout'], 1, $maxExecutionTime, 10); |
||
372 | $this->config = $config; |
||
373 | } |
||
374 | } |
||
375 | |||
376 | /** |
||
377 | * Processes a search request and returns the raw Apache Solr Documents. |
||
378 | * |
||
379 | * @access public |
||
380 | * |
||
381 | * @param array $parameters Additional search parameters |
||
382 | * |
||
383 | * @return array The Apache Solr Documents that were fetched |
||
384 | */ |
||
385 | public function searchRaw(array $parameters = []): array |
||
408 | } |
||
409 | |||
410 | /** |
||
411 | * This returns $this->core via __get() |
||
412 | * |
||
413 | * @access protected |
||
414 | * |
||
415 | * @return string|null The core name of the current query endpoint or null if core admin endpoint |
||
416 | */ |
||
417 | protected function magicGetCore(): ?string |
||
418 | { |
||
419 | return $this->core; |
||
420 | } |
||
421 | |||
422 | /** |
||
423 | * This returns $this->limit via __get() |
||
424 | * |
||
425 | * @access protected |
||
426 | * |
||
427 | * @return int The max number of results |
||
428 | */ |
||
429 | protected function magicGetLimit(): int |
||
430 | { |
||
431 | return $this->limit; |
||
432 | } |
||
433 | |||
434 | /** |
||
435 | * This returns $this->numberOfHits via __get() |
||
436 | * |
||
437 | * @access protected |
||
438 | * |
||
439 | * @return int Total number of hits for last search |
||
440 | */ |
||
441 | protected function magicGetNumberOfHits(): int |
||
442 | { |
||
443 | return $this->numberOfHits; |
||
444 | } |
||
445 | |||
446 | /** |
||
447 | * This returns $this->ready via __get() |
||
448 | * |
||
449 | * @access protected |
||
450 | * |
||
451 | * @return bool Is the search instantiated successfully? |
||
452 | */ |
||
453 | protected function magicGetReady(): bool |
||
454 | { |
||
455 | return $this->ready; |
||
456 | } |
||
457 | |||
458 | /** |
||
459 | * This returns $this->service via __get() |
||
460 | * |
||
461 | * @access protected |
||
462 | * |
||
463 | * @return Client Apache Solr service object |
||
464 | */ |
||
465 | protected function magicGetService(): Client |
||
468 | } |
||
469 | |||
470 | /** |
||
471 | * This sets $this->configPid via __set() |
||
472 | * |
||
473 | * @access protected |
||
474 | * |
||
475 | * @param int $value The new PID for the metadata definitions |
||
476 | * |
||
477 | * @return void |
||
478 | */ |
||
479 | protected function magicSetConfigPid(int $value): void |
||
480 | { |
||
481 | $this->configPid = max($value, 0); |
||
482 | } |
||
483 | |||
484 | /** |
||
485 | * This sets $this->limit via __set() |
||
486 | * |
||
487 | * @access protected |
||
488 | * |
||
489 | * @param int $value The max number of results |
||
490 | * |
||
491 | * @return void |
||
492 | */ |
||
493 | protected function magicSetLimit(int $value): void |
||
494 | { |
||
495 | $this->limit = max($value, 0); |
||
496 | } |
||
497 | |||
498 | /** |
||
499 | * This sets $this->params via __set() |
||
500 | * |
||
501 | * @access protected |
||
502 | * |
||
503 | * @param array $value The query parameters |
||
504 | * |
||
505 | * @return void |
||
506 | */ |
||
507 | protected function magicSetParams(array $value): void |
||
508 | { |
||
509 | $this->params = $value; |
||
510 | } |
||
511 | |||
512 | /** |
||
513 | * This magic method is called each time an invisible property is referenced from the object |
||
514 | * |
||
515 | * @access public |
||
516 | * |
||
517 | * @param string $var Name of variable to get |
||
518 | * |
||
519 | * @return mixed Value of $this->$var |
||
520 | */ |
||
521 | public function __get(string $var) |
||
522 | { |
||
523 | $method = 'magicGet' . ucfirst($var); |
||
524 | if ( |
||
525 | !property_exists($this, $var) |
||
526 | || !method_exists($this, $method) |
||
527 | ) { |
||
528 | $this->logger->warning('There is no getter function for property "' . $var . '"'); |
||
529 | return null; |
||
530 | } else { |
||
531 | return $this->$method(); |
||
532 | } |
||
533 | } |
||
534 | |||
535 | /** |
||
536 | * This magic method is called each time an invisible property is checked for isset() or empty() |
||
537 | * |
||
538 | * @access public |
||
539 | * |
||
540 | * @param string $var Name of variable to check |
||
541 | * |
||
542 | * @return bool true if variable is set and not empty, false otherwise |
||
543 | */ |
||
544 | public function __isset(string $var): bool |
||
547 | } |
||
548 | |||
549 | /** |
||
550 | * This magic method is called each time an invisible property is referenced from the object |
||
551 | * |
||
552 | * @access public |
||
553 | * |
||
554 | * @param string $var Name of variable to set |
||
555 | * @param mixed $value New value of variable |
||
556 | * |
||
557 | * @return void |
||
558 | */ |
||
559 | public function __set(string $var, $value): void |
||
569 | } |
||
570 | } |
||
571 | |||
572 | /** |
||
573 | * This is a singleton class, thus the constructor should be private/protected |
||
574 | * |
||
575 | * @access protected |
||
576 | * |
||
577 | * @param string|null $core The name of the core to use or null for core admin endpoint |
||
578 | * |
||
579 | * @return void |
||
580 | */ |
||
581 | protected function __construct(?string $core) |
||
651 | // Nothing to do here. |
||
652 | } |
||
653 | } |
||
654 | |||
655 | /** |
||
656 | * Sends the commit and optimize command to the index. |
||
657 | * |
||
658 | * @access public |
||
659 | * |
||
660 | * @param bool $commit If true, the commit command is sent to the index |
||
661 | * @param bool $optimize If true, the optimize command is sent to the index |
||
662 | * |
||
663 | * @return bool true if executing the command worked |
||
664 | */ |
||
665 | public function optimize(bool $commit, bool $optimize): bool |
||
688 | } |
||
689 | |||
690 | /** |
||
691 | * Sends the suggest.build=true command to the index. |
||
692 | * |
||
693 | * @access public |
||
694 | * |
||
695 | * @return bool true if executing the command worked |
||
696 | */ |
||
697 | public function suggestBuild(): bool |
||
711 |