We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 57 |
| Total Lines | 620 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| 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 |
||
| 40 | class Solr implements LoggerAwareInterface |
||
| 41 | { |
||
| 42 | use LoggerAwareTrait; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * This holds the Solr configuration |
||
| 46 | * |
||
| 47 | * @var array |
||
| 48 | * @access protected |
||
| 49 | */ |
||
| 50 | protected $config = []; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * This holds the core name |
||
| 54 | * |
||
| 55 | * @var string|null |
||
| 56 | * @access protected |
||
| 57 | */ |
||
| 58 | protected $core = null; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * This holds the PID for the configuration |
||
| 62 | * |
||
| 63 | * @var int |
||
| 64 | * @access protected |
||
| 65 | */ |
||
| 66 | protected $cPid = 0; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * The extension key |
||
| 70 | * |
||
| 71 | * @var string |
||
| 72 | * @access public |
||
| 73 | */ |
||
| 74 | public static $extKey = 'dlf'; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * This holds the max results |
||
| 78 | * |
||
| 79 | * @var int |
||
| 80 | * @access protected |
||
| 81 | */ |
||
| 82 | protected $limit = 50000; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * This holds the number of hits for last search |
||
| 86 | * |
||
| 87 | * @var int |
||
| 88 | * @access protected |
||
| 89 | */ |
||
| 90 | protected $numberOfHits = 0; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * This holds the additional query parameters |
||
| 94 | * |
||
| 95 | * @var array |
||
| 96 | * @access protected |
||
| 97 | */ |
||
| 98 | protected $params = []; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Is the search instantiated successfully? |
||
| 102 | * |
||
| 103 | * @var bool |
||
| 104 | * @access protected |
||
| 105 | */ |
||
| 106 | protected $ready = false; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * This holds the singleton search objects with their core as array key |
||
| 110 | * |
||
| 111 | * @var array (\Kitodo\Dlf\Common\Solr) |
||
| 112 | * @access protected |
||
| 113 | */ |
||
| 114 | protected static $registry = []; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * This holds the Solr service object |
||
| 118 | * |
||
| 119 | * @var \Solarium\Client |
||
| 120 | * @access protected |
||
| 121 | */ |
||
| 122 | protected $service; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Add a new core to Apache Solr |
||
| 126 | * |
||
| 127 | * @access public |
||
| 128 | * |
||
| 129 | * @param string $core: The name of the new core. If empty, the next available core name is used. |
||
| 130 | * |
||
| 131 | * @return string The name of the new core |
||
| 132 | */ |
||
| 133 | public static function createCore($core = '') |
||
| 134 | { |
||
| 135 | // Get next available core name if none given. |
||
| 136 | if (empty($core)) { |
||
| 137 | $core = 'dlfCore' . self::getNextCoreNumber(); |
||
| 138 | } |
||
| 139 | // Get Solr service instance. |
||
| 140 | $solr = self::getInstance($core); |
||
| 141 | // Create new core if core with given name doesn't exist. |
||
| 142 | if ($solr->ready) { |
||
| 143 | // Core already exists. |
||
| 144 | return $core; |
||
| 145 | } else { |
||
| 146 | // Core doesn't exist yet. |
||
| 147 | $solrAdmin = self::getInstance(); |
||
| 148 | if ($solrAdmin->ready) { |
||
| 149 | $query = $solrAdmin->service->createCoreAdmin(); |
||
| 150 | $action = $query->createCreate(); |
||
| 151 | $action->setConfigSet('dlf'); |
||
| 152 | $action->setCore($core); |
||
| 153 | $action->setDataDir('data'); |
||
| 154 | $action->setInstanceDir($core); |
||
| 155 | $query->setAction($action); |
||
| 156 | try { |
||
| 157 | $response = $solrAdmin->service->coreAdmin($query); |
||
| 158 | if ($response->getWasSuccessful()) { |
||
| 159 | // Core successfully created. |
||
| 160 | return $core; |
||
| 161 | } |
||
| 162 | } catch (\Exception $e) { |
||
| 163 | // Nothing to do here. |
||
| 164 | } |
||
| 165 | } else { |
||
| 166 | Helper::log('Apache Solr not available', LOG_SEVERITY_ERROR); |
||
| 167 | } |
||
| 168 | } |
||
| 169 | return ''; |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Escape all special characters in a query string |
||
| 174 | * |
||
| 175 | * @access public |
||
| 176 | * |
||
| 177 | * @param string $query: The query string |
||
| 178 | * |
||
| 179 | * @return string The escaped query string |
||
| 180 | */ |
||
| 181 | public static function escapeQuery($query) |
||
| 182 | { |
||
| 183 | $helper = GeneralUtility::makeInstance(\Solarium\Core\Query\Helper::class); |
||
| 184 | // Escape query phrase or term. |
||
| 185 | if (preg_match('/^".*"$/', $query)) { |
||
| 186 | return $helper->escapePhrase(trim($query, '"')); |
||
| 187 | } else { |
||
| 188 | // Using a modified escape function here to retain whitespace, '*' and '?' for search truncation. |
||
| 189 | // @see https://github.com/solariumphp/solarium/blob/5.x/src/Core/Query/Helper.php#L70 for reference |
||
| 190 | /* return $helper->escapeTerm($query); */ |
||
| 191 | return preg_replace('/(\+|-|&&|\|\||!|\(|\)|\{|}|\[|]|\^|"|~|:|\/|\\\)/', '\\\$1', $query); |
||
| 192 | } |
||
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Escape all special characters in a query string while retaining valid field queries |
||
| 197 | * |
||
| 198 | * @access public |
||
| 199 | * |
||
| 200 | * @param string $query: The query string |
||
| 201 | * @param int $pid: The PID for the field configuration |
||
| 202 | * |
||
| 203 | * @return string The escaped query string |
||
| 204 | */ |
||
| 205 | public static function escapeQueryKeepField($query, $pid) |
||
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Get fields for index. |
||
| 251 | * |
||
| 252 | * @access public |
||
| 253 | * |
||
| 254 | * @return array fields |
||
| 255 | */ |
||
| 256 | public static function getFields() |
||
| 288 | } |
||
| 289 | |||
| 290 | /** |
||
| 291 | * This is a singleton class, thus instances must be created by this method |
||
| 292 | * |
||
| 293 | * @access public |
||
| 294 | * |
||
| 295 | * @param mixed $core: Name or UID of the core to load or null to get core admin endpoint |
||
| 296 | * |
||
| 297 | * @return \Kitodo\Dlf\Common\Solr Instance of this class |
||
| 298 | */ |
||
| 299 | public static function getInstance($core = null) |
||
| 329 | } |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Get next unused Solr core number |
||
| 333 | * |
||
| 334 | * @access public |
||
| 335 | * |
||
| 336 | * @param int $number: Number to start with |
||
| 337 | * |
||
| 338 | * @return int First unused core number found |
||
| 339 | */ |
||
| 340 | public static function getNextCoreNumber($number = 0) |
||
| 341 | { |
||
| 342 | $number = max(intval($number), 0); |
||
| 343 | // Check if core already exists. |
||
| 344 | $solr = self::getInstance('dlfCore' . $number); |
||
| 345 | if (!$solr->ready) { |
||
| 346 | return $number; |
||
| 347 | } else { |
||
| 348 | return self::getNextCoreNumber($number + 1); |
||
| 349 | } |
||
| 350 | } |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Sets the connection information for Solr |
||
| 354 | * |
||
| 355 | * @access protected |
||
| 356 | * |
||
| 357 | * @return void |
||
| 358 | */ |
||
| 359 | protected function loadSolrConnectionInfo() |
||
| 388 | } |
||
| 389 | } |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Processes a search request and returns the raw Apache Solr Documents. |
||
| 393 | * |
||
| 394 | * @access public |
||
| 395 | * |
||
| 396 | * @param array $parameters: Additional search parameters |
||
| 397 | * |
||
| 398 | * @return array The Apache Solr Documents that were fetched |
||
| 399 | */ |
||
| 400 | public function search_raw($parameters = []) |
||
| 401 | { |
||
| 402 | // Set additional query parameters. |
||
| 403 | $parameters['start'] = 0; |
||
| 404 | $parameters['rows'] = $this->limit; |
||
| 405 | // Calculate cache identifier. |
||
| 406 | $cacheIdentifier = Helper::digest($this->core . print_r(array_merge($this->params, $parameters), true)); |
||
|
|
|||
| 407 | $cache = GeneralUtility::makeInstance(CacheManager::class)->getCache('tx_dlf_solr'); |
||
| 408 | $resultSet = []; |
||
| 409 | if (($entry = $cache->get($cacheIdentifier)) === false) { |
||
| 410 | $selectQuery = $this->service->createSelect(array_merge($this->params, $parameters)); |
||
| 411 | $result = $this->service->select($selectQuery); |
||
| 412 | foreach ($result as $doc) { |
||
| 413 | $resultSet[] = $doc; |
||
| 414 | } |
||
| 415 | // Save value in cache. |
||
| 416 | $cache->set($cacheIdentifier, $resultSet); |
||
| 417 | } else { |
||
| 418 | // Return cache hit. |
||
| 419 | $resultSet = $entry; |
||
| 420 | } |
||
| 421 | return $resultSet; |
||
| 422 | } |
||
| 423 | |||
| 424 | /** |
||
| 425 | * This returns $this->core via __get() |
||
| 426 | * |
||
| 427 | * @access protected |
||
| 428 | * |
||
| 429 | * @return string|null The core name of the current query endpoint or null if core admin endpoint |
||
| 430 | */ |
||
| 431 | protected function _getCore() |
||
| 432 | { |
||
| 433 | return $this->core; |
||
| 434 | } |
||
| 435 | |||
| 436 | /** |
||
| 437 | * This returns $this->limit via __get() |
||
| 438 | * |
||
| 439 | * @access protected |
||
| 440 | * |
||
| 441 | * @return int The max number of results |
||
| 442 | */ |
||
| 443 | protected function _getLimit() |
||
| 444 | { |
||
| 445 | return $this->limit; |
||
| 446 | } |
||
| 447 | |||
| 448 | /** |
||
| 449 | * This returns $this->numberOfHits via __get() |
||
| 450 | * |
||
| 451 | * @access protected |
||
| 452 | * |
||
| 453 | * @return int Total number of hits for last search |
||
| 454 | */ |
||
| 455 | protected function _getNumberOfHits() |
||
| 456 | { |
||
| 457 | return $this->numberOfHits; |
||
| 458 | } |
||
| 459 | |||
| 460 | /** |
||
| 461 | * This returns $this->ready via __get() |
||
| 462 | * |
||
| 463 | * @access protected |
||
| 464 | * |
||
| 465 | * @return bool Is the search instantiated successfully? |
||
| 466 | */ |
||
| 467 | protected function _getReady() |
||
| 468 | { |
||
| 469 | return $this->ready; |
||
| 470 | } |
||
| 471 | |||
| 472 | /** |
||
| 473 | * This returns $this->service via __get() |
||
| 474 | * |
||
| 475 | * @access protected |
||
| 476 | * |
||
| 477 | * @return \Solarium\Client Apache Solr service object |
||
| 478 | */ |
||
| 479 | protected function _getService() |
||
| 480 | { |
||
| 481 | return $this->service; |
||
| 482 | } |
||
| 483 | |||
| 484 | /** |
||
| 485 | * This sets $this->cPid via __set() |
||
| 486 | * |
||
| 487 | * @access protected |
||
| 488 | * |
||
| 489 | * @param int $value: The new PID for the metadata definitions |
||
| 490 | * |
||
| 491 | * @return void |
||
| 492 | */ |
||
| 493 | protected function _setCPid($value) |
||
| 494 | { |
||
| 495 | $this->cPid = max(intval($value), 0); |
||
| 496 | } |
||
| 497 | |||
| 498 | /** |
||
| 499 | * This sets $this->limit via __set() |
||
| 500 | * |
||
| 501 | * @access protected |
||
| 502 | * |
||
| 503 | * @param int $value: The max number of results |
||
| 504 | * |
||
| 505 | * @return void |
||
| 506 | */ |
||
| 507 | protected function _setLimit($value) |
||
| 508 | { |
||
| 509 | $this->limit = max(intval($value), 0); |
||
| 510 | } |
||
| 511 | |||
| 512 | /** |
||
| 513 | * This sets $this->params via __set() |
||
| 514 | * |
||
| 515 | * @access protected |
||
| 516 | * |
||
| 517 | * @param array $value: The query parameters |
||
| 518 | * |
||
| 519 | * @return void |
||
| 520 | */ |
||
| 521 | protected function _setParams(array $value) |
||
| 522 | { |
||
| 523 | $this->params = $value; |
||
| 524 | } |
||
| 525 | |||
| 526 | /** |
||
| 527 | * This magic method is called each time an invisible property is referenced from the object |
||
| 528 | * |
||
| 529 | * @access public |
||
| 530 | * |
||
| 531 | * @param string $var: Name of variable to get |
||
| 532 | * |
||
| 533 | * @return mixed Value of $this->$var |
||
| 534 | */ |
||
| 535 | public function __get($var) |
||
| 536 | { |
||
| 537 | $method = '_get' . ucfirst($var); |
||
| 538 | if ( |
||
| 539 | !property_exists($this, $var) |
||
| 540 | || !method_exists($this, $method) |
||
| 541 | ) { |
||
| 542 | $this->logger->warning('There is no getter function for property "' . $var . '"'); |
||
| 543 | return; |
||
| 544 | } else { |
||
| 545 | return $this->$method(); |
||
| 546 | } |
||
| 547 | } |
||
| 548 | |||
| 549 | /** |
||
| 550 | * This magic method is called each time an invisible property is checked for isset() or empty() |
||
| 551 | * |
||
| 552 | * @access public |
||
| 553 | * |
||
| 554 | * @param string $var: Name of variable to check |
||
| 555 | * |
||
| 556 | * @return bool true if variable is set and not empty, false otherwise |
||
| 557 | */ |
||
| 558 | public function __isset($var) |
||
| 559 | { |
||
| 560 | return !empty($this->__get($var)); |
||
| 561 | } |
||
| 562 | |||
| 563 | /** |
||
| 564 | * This magic method is called each time an invisible property is referenced from the object |
||
| 565 | * |
||
| 566 | * @access public |
||
| 567 | * |
||
| 568 | * @param string $var: Name of variable to set |
||
| 569 | * @param mixed $value: New value of variable |
||
| 570 | * |
||
| 571 | * @return void |
||
| 572 | */ |
||
| 573 | public function __set($var, $value) |
||
| 583 | } |
||
| 584 | } |
||
| 585 | |||
| 586 | /** |
||
| 587 | * This is a singleton class, thus the constructor should be private/protected |
||
| 588 | * |
||
| 589 | * @access protected |
||
| 590 | * |
||
| 591 | * @param string|null $core: The name of the core to use or null for core admin endpoint |
||
| 592 | * |
||
| 593 | * @return void |
||
| 594 | */ |
||
| 595 | protected function __construct($core) |
||
| 660 | // Nothing to do here. |
||
| 661 | } |
||
| 662 | } |
||
| 663 | } |
||
| 664 |