We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 80 |
| Total Lines | 540 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Indexer 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 Indexer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 35 | class Indexer |
||
| 36 | { |
||
| 37 | /** |
||
| 38 | * The extension key |
||
| 39 | * |
||
| 40 | * @var string |
||
| 41 | * @access public |
||
| 42 | */ |
||
| 43 | public static $extKey = 'dlf'; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Array of metadata fields' configuration |
||
| 47 | * @see loadIndexConf() |
||
| 48 | * |
||
| 49 | * @var array |
||
| 50 | * @access protected |
||
| 51 | */ |
||
| 52 | protected static $fields = [ |
||
| 53 | 'autocomplete' => [], |
||
| 54 | 'facets' => [], |
||
| 55 | 'sortables' => [], |
||
| 56 | 'indexed' => [], |
||
| 57 | 'stored' => [], |
||
| 58 | 'tokenized' => [], |
||
| 59 | 'fieldboost' => [] |
||
| 60 | ]; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Is the index configuration loaded? |
||
| 64 | * @see $fields |
||
| 65 | * |
||
| 66 | * @var bool |
||
| 67 | * @access protected |
||
| 68 | */ |
||
| 69 | protected static $fieldsLoaded = false; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * List of already processed documents |
||
| 73 | * |
||
| 74 | * @var array |
||
| 75 | * @access protected |
||
| 76 | */ |
||
| 77 | protected static $processedDocs = []; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Instance of \Kitodo\Dlf\Common\Solr class |
||
| 81 | * |
||
| 82 | * @var \Kitodo\Dlf\Common\Solr |
||
| 83 | * @access protected |
||
| 84 | */ |
||
| 85 | protected static $solr; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Insert given document into Solr index |
||
| 89 | * |
||
| 90 | * @access public |
||
| 91 | * |
||
| 92 | * @param \Kitodo\Dlf\Domain\Model\Document $document: The document to add |
||
| 93 | * |
||
| 94 | * @return bool true on success or false on failure |
||
| 95 | */ |
||
| 96 | public static function add(Document $document) |
||
| 97 | { |
||
| 98 | if (in_array($document->getUid(), self::$processedDocs)) { |
||
| 99 | return true; |
||
| 100 | } elseif (self::solrConnect($document->getSolrcore(), $document->getPid())) { |
||
| 101 | $success = true; |
||
| 102 | Helper::getLanguageService()->includeLLFile('EXT:dlf/Resources/Private/Language/locallang_be.xlf'); |
||
| 103 | // Handle multi-volume documents. |
||
| 104 | if ($parentId = $document->getPartof()) { |
||
| 105 | // initialize documentRepository |
||
| 106 | // TODO: When we drop support for TYPO3v9, we needn't/shouldn't use ObjectManager anymore |
||
| 107 | $objectManager = GeneralUtility::makeInstance(ObjectManager::class); |
||
| 108 | $documentRepository = $objectManager->get(DocumentRepository::class); |
||
| 109 | // get parent document |
||
| 110 | $parent = $documentRepository->findByUid($parentId); |
||
| 111 | if ($parent) { |
||
| 112 | // get XML document of parent |
||
| 113 | $doc = Doc::getInstance($parent->getLocation(), ['storagePid' => $parent->getPid()], true); |
||
| 114 | if ($doc !== null) { |
||
| 115 | $parent->setDoc($doc); |
||
| 116 | $success = self::add($parent); |
||
| 117 | } else { |
||
| 118 | Helper::log('Could not load parent document with UID ' . $document->getDoc()->parentId, LOG_SEVERITY_ERROR); |
||
| 119 | return false; |
||
| 120 | } |
||
| 121 | } |
||
| 122 | } |
||
| 123 | try { |
||
| 124 | // Add document to list of processed documents. |
||
| 125 | self::$processedDocs[] = $document->getUid(); |
||
| 126 | // Delete old Solr documents. |
||
| 127 | $updateQuery = self::$solr->service->createUpdate(); |
||
| 128 | $updateQuery->addDeleteQuery('uid:' . $document->getUid()); |
||
| 129 | self::$solr->service->update($updateQuery); |
||
| 130 | |||
| 131 | // Index every logical unit as separate Solr document. |
||
| 132 | foreach ($document->getDoc()->tableOfContents as $logicalUnit) { |
||
| 133 | if ($success) { |
||
| 134 | $success = self::processLogical($document, $logicalUnit); |
||
| 135 | } else { |
||
| 136 | break; |
||
| 137 | } |
||
| 138 | } |
||
| 139 | // Index full text files if available. |
||
| 140 | if ($document->getDoc()->hasFulltext) { |
||
| 141 | foreach ($document->getDoc()->physicalStructure as $pageNumber => $xmlId) { |
||
| 142 | if ($success) { |
||
| 143 | $success = self::processPhysical($document, $pageNumber, $document->getDoc()->physicalStructureInfo[$xmlId]); |
||
| 144 | } else { |
||
| 145 | break; |
||
| 146 | } |
||
| 147 | } |
||
| 148 | } |
||
| 149 | // Commit all changes. |
||
| 150 | $updateQuery = self::$solr->service->createUpdate(); |
||
| 151 | $updateQuery->addCommit(); |
||
| 152 | self::$solr->service->update($updateQuery); |
||
| 153 | |||
| 154 | if (!(\TYPO3_REQUESTTYPE & \TYPO3_REQUESTTYPE_CLI)) { |
||
| 155 | if ($success) { |
||
| 156 | Helper::addMessage( |
||
| 157 | sprintf(Helper::getLanguageService()->getLL('flash.documentIndexed'), $document->getTitle(), $document->getUid()), |
||
| 158 | Helper::getLanguageService()->getLL('flash.done'), |
||
| 159 | FlashMessage::OK, |
||
| 160 | true, |
||
| 161 | 'core.template.flashMessages' |
||
| 162 | ); |
||
| 163 | } else { |
||
| 164 | Helper::addMessage( |
||
| 165 | sprintf(Helper::getLanguageService()->getLL('flash.documentNotIndexed'), $document->getTitle(), $document->getUid()), |
||
| 166 | Helper::getLanguageService()->getLL('flash.error'), |
||
| 167 | FlashMessage::ERROR, |
||
| 168 | true, |
||
| 169 | 'core.template.flashMessages' |
||
| 170 | ); |
||
| 171 | } |
||
| 172 | } |
||
| 173 | return $success; |
||
| 174 | } catch (\Exception $e) { |
||
| 175 | if (!(\TYPO3_REQUESTTYPE & \TYPO3_REQUESTTYPE_CLI)) { |
||
| 176 | Helper::addMessage( |
||
| 177 | Helper::getLanguageService()->getLL('flash.solrException') . ' ' . htmlspecialchars($e->getMessage()), |
||
| 178 | Helper::getLanguageService()->getLL('flash.error'), |
||
| 179 | FlashMessage::ERROR, |
||
| 180 | true, |
||
| 181 | 'core.template.flashMessages' |
||
| 182 | ); |
||
| 183 | } |
||
| 184 | Helper::log('Apache Solr threw exception: "' . $e->getMessage() . '"', LOG_SEVERITY_ERROR); |
||
| 185 | return false; |
||
| 186 | } |
||
| 187 | } else { |
||
| 188 | if (!(\TYPO3_REQUESTTYPE & \TYPO3_REQUESTTYPE_CLI)) { |
||
| 189 | Helper::addMessage( |
||
| 190 | Helper::getLanguageService()->getLL('flash.solrNoConnection'), |
||
| 191 | Helper::getLanguageService()->getLL('flash.warning'), |
||
| 192 | FlashMessage::WARNING, |
||
| 193 | true, |
||
| 194 | 'core.template.flashMessages' |
||
| 195 | ); |
||
| 196 | } |
||
| 197 | Helper::log('Could not connect to Apache Solr server', LOG_SEVERITY_ERROR); |
||
| 198 | return false; |
||
| 199 | } |
||
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Returns the dynamic index field name for the given metadata field. |
||
| 204 | * |
||
| 205 | * @access public |
||
| 206 | * |
||
| 207 | * @param string $index_name: The metadata field's name in database |
||
| 208 | * @param int $pid: UID of the configuration page |
||
| 209 | * |
||
| 210 | * @return string The field's dynamic index name |
||
| 211 | */ |
||
| 212 | public static function getIndexFieldName($index_name, $pid = 0) |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Load indexing configuration |
||
| 232 | * |
||
| 233 | * @access protected |
||
| 234 | * |
||
| 235 | * @param int $pid: The configuration page's UID |
||
| 236 | * |
||
| 237 | * @return void |
||
| 238 | */ |
||
| 239 | protected static function loadIndexConf($pid) |
||
| 297 | } |
||
| 298 | } |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Processes a logical unit (and its children) for the Solr index |
||
| 302 | * |
||
| 303 | * @access protected |
||
| 304 | * |
||
| 305 | * @param \Kitodo\Dlf\Domain\Model\Document $document: The METS document |
||
| 306 | * @param array $logicalUnit: Array of the logical unit to process |
||
| 307 | * |
||
| 308 | * @return bool true on success or false on failure |
||
| 309 | */ |
||
| 310 | protected static function processLogical(Document $document, array $logicalUnit) |
||
| 311 | { |
||
| 312 | $success = true; |
||
| 313 | $doc = $document->getDoc(); |
||
| 314 | $doc->cPid = $document->getPid(); |
||
| 315 | // Get metadata for logical unit. |
||
| 316 | $metadata = $doc->metadataArray[$logicalUnit['id']]; |
||
| 317 | if (!empty($metadata)) { |
||
| 318 | $metadata['author'] = self::removeAppendsFromAuthor($metadata['author']); |
||
| 319 | // set Owner if available |
||
| 320 | if ($document->getOwner()) { |
||
| 321 | $metadata['owner'][0] = $document->getOwner()->getIndexName(); |
||
| 322 | } |
||
| 323 | // Create new Solr document. |
||
| 324 | $updateQuery = self::$solr->service->createUpdate(); |
||
| 325 | $solrDoc = $updateQuery->createDocument(); |
||
|
|
|||
| 326 | $solrDoc = self::getSolrDocument($updateQuery, $document, $logicalUnit); |
||
| 327 | if (MathUtility::canBeInterpretedAsInteger($logicalUnit['points'])) { |
||
| 328 | $solrDoc->setField('page', $logicalUnit['points']); |
||
| 329 | } |
||
| 330 | if ($logicalUnit['id'] == $doc->toplevelId) { |
||
| 331 | $solrDoc->setField('thumbnail', $doc->thumbnail); |
||
| 332 | } elseif (!empty($logicalUnit['thumbnailId'])) { |
||
| 333 | $solrDoc->setField('thumbnail', $doc->getFileLocation($logicalUnit['thumbnailId'])); |
||
| 334 | } |
||
| 335 | // There can be only one toplevel unit per UID, independently of backend configuration |
||
| 336 | $solrDoc->setField('toplevel', $logicalUnit['id'] == $doc->toplevelId ? true : false); |
||
| 337 | $solrDoc->setField('title', $metadata['title'][0], self::$fields['fieldboost']['title']); |
||
| 338 | $solrDoc->setField('volume', $metadata['volume'][0], self::$fields['fieldboost']['volume']); |
||
| 339 | $solrDoc->setField('record_id', $metadata['record_id'][0]); |
||
| 340 | $solrDoc->setField('purl', $metadata['purl'][0]); |
||
| 341 | $solrDoc->setField('location', $document->getLocation()); |
||
| 342 | $solrDoc->setField('urn', $metadata['urn']); |
||
| 343 | $solrDoc->setField('license', $metadata['license']); |
||
| 344 | $solrDoc->setField('terms', $metadata['terms']); |
||
| 345 | $solrDoc->setField('restrictions', $metadata['restrictions']); |
||
| 346 | $coordinates = json_decode($metadata['coordinates'][0]); |
||
| 347 | if (is_object($coordinates)) { |
||
| 348 | $solrDoc->setField('geom', json_encode($coordinates->features[0])); |
||
| 349 | } |
||
| 350 | $autocomplete = []; |
||
| 351 | foreach ($metadata as $index_name => $data) { |
||
| 352 | if ( |
||
| 353 | !empty($data) |
||
| 354 | && substr($index_name, -8) !== '_sorting' |
||
| 355 | ) { |
||
| 356 | $solrDoc->setField(self::getIndexFieldName($index_name, $document->getPid()), $data, self::$fields['fieldboost'][$index_name]); |
||
| 357 | if (in_array($index_name, self::$fields['sortables'])) { |
||
| 358 | // Add sortable fields to index. |
||
| 359 | $solrDoc->setField($index_name . '_sorting', $metadata[$index_name . '_sorting'][0]); |
||
| 360 | } |
||
| 361 | if (in_array($index_name, self::$fields['facets'])) { |
||
| 362 | // Add facets to index. |
||
| 363 | $solrDoc->setField($index_name . '_faceting', $data); |
||
| 364 | } |
||
| 365 | if (in_array($index_name, self::$fields['autocomplete'])) { |
||
| 366 | $autocomplete = array_merge($autocomplete, $data); |
||
| 367 | } |
||
| 368 | } |
||
| 369 | } |
||
| 370 | // Add autocomplete values to index. |
||
| 371 | if (!empty($autocomplete)) { |
||
| 372 | $solrDoc->setField('autocomplete', $autocomplete); |
||
| 373 | } |
||
| 374 | // Add collection information to logical sub-elements if applicable. |
||
| 375 | if ( |
||
| 376 | in_array('collection', self::$fields['facets']) |
||
| 377 | && empty($metadata['collection']) |
||
| 378 | && !empty($doc->metadataArray[$doc->toplevelId]['collection']) |
||
| 379 | ) { |
||
| 380 | $solrDoc->setField('collection_faceting', $doc->metadataArray[$doc->toplevelId]['collection']); |
||
| 381 | } |
||
| 382 | try { |
||
| 383 | $updateQuery->addDocument($solrDoc); |
||
| 384 | self::$solr->service->update($updateQuery); |
||
| 385 | } catch (\Exception $e) { |
||
| 386 | if (!(\TYPO3_REQUESTTYPE & \TYPO3_REQUESTTYPE_CLI)) { |
||
| 387 | Helper::addMessage( |
||
| 388 | Helper::getLanguageService()->getLL('flash.solrException') . '<br />' . htmlspecialchars($e->getMessage()), |
||
| 389 | Helper::getLanguageService()->getLL('flash.error'), |
||
| 390 | FlashMessage::ERROR, |
||
| 391 | true, |
||
| 392 | 'core.template.flashMessages' |
||
| 393 | ); |
||
| 394 | } |
||
| 395 | Helper::log('Apache Solr threw exception: "' . $e->getMessage() . '"', LOG_SEVERITY_ERROR); |
||
| 396 | return false; |
||
| 397 | } |
||
| 398 | } |
||
| 399 | // Check for child elements... |
||
| 400 | if (!empty($logicalUnit['children'])) { |
||
| 401 | foreach ($logicalUnit['children'] as $child) { |
||
| 402 | if ($success) { |
||
| 403 | // ...and process them, too. |
||
| 404 | $success = self::processLogical($document, $child); |
||
| 405 | } else { |
||
| 406 | break; |
||
| 407 | } |
||
| 408 | } |
||
| 409 | } |
||
| 410 | return $success; |
||
| 411 | } |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Processes a physical unit for the Solr index |
||
| 415 | * |
||
| 416 | * @access protected |
||
| 417 | * |
||
| 418 | * @param \Kitodo\Dlf\Domain\Model\Document $document: The METS document |
||
| 419 | * @param int $page: The page number |
||
| 420 | * @param array $physicalUnit: Array of the physical unit to process |
||
| 421 | * |
||
| 422 | * @return bool true on success or false on failure |
||
| 423 | */ |
||
| 424 | protected static function processPhysical(Document $document, $page, array $physicalUnit) |
||
| 491 | } |
||
| 492 | |||
| 493 | /** |
||
| 494 | * Connects to Solr server. |
||
| 495 | * |
||
| 496 | * @access protected |
||
| 497 | * |
||
| 498 | * @param int $core: UID of the Solr core |
||
| 499 | * @param int $pid: UID of the configuration page |
||
| 500 | * |
||
| 501 | * @return bool true on success or false on failure |
||
| 502 | */ |
||
| 503 | protected static function solrConnect($core, $pid = 0) |
||
| 520 | } |
||
| 521 | |||
| 522 | /** |
||
| 523 | * Get SOLR document with set standard fields (identical for logical and physical unit) |
||
| 524 | * |
||
| 525 | * @access private |
||
| 526 | * |
||
| 527 | * @param \Solarium\QueryType\Update\Query\Query $updateQuery solarium query |
||
| 528 | * @param \Kitodo\Dlf\Domain\Model\Document $document: The METS document |
||
| 529 | * @param array $unit: Array of the logical or physical unit to process |
||
| 530 | * @param string $fullText: Text containing full text for indexing |
||
| 531 | * |
||
| 532 | * @return \Solarium\Core\Query\DocumentInterface |
||
| 533 | */ |
||
| 534 | private static function getSolrDocument($updateQuery, $document, $unit, $fullText = '') { |
||
| 547 | } |
||
| 548 | |||
| 549 | /** |
||
| 550 | * Remove appended "valueURI" from authors' names for indexing. |
||
| 551 | * |
||
| 552 | * @access private |
||
| 553 | * |
||
| 554 | * @param array|string $authors: Array or string containing author/authors |
||
| 555 | * |
||
| 556 | * @return array|string |
||
| 557 | */ |
||
| 558 | private static function removeAppendsFromAuthor($authors) { |
||
| 559 | if (is_array($authors)) { |
||
| 560 | foreach ($authors as $i => $author) { |
||
| 561 | $splitName = explode(chr(31), $author); |
||
| 562 | $authors[$i] = $splitName[0]; |
||
| 563 | } |
||
| 564 | } |
||
| 565 | return $authors; |
||
| 566 | } |
||
| 567 | |||
| 568 | /** |
||
| 569 | * Prevent instantiation by hiding the constructor |
||
| 570 | * |
||
| 571 | * @access private |
||
| 572 | */ |
||
| 573 | private function __construct() |
||
| 575 | // This is a static class, thus no instances should be created. |
||
| 576 | } |
||
| 578 |