We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 86 |
| Total Lines | 554 |
| Duplicated Lines | 0 % |
| Changes | 11 | ||
| 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 |
||
| 28 | class Indexer |
||
| 29 | { |
||
| 30 | /** |
||
| 31 | * The extension key |
||
| 32 | * |
||
| 33 | * @var string |
||
| 34 | * @access public |
||
| 35 | */ |
||
| 36 | public static $extKey = 'dlf'; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Array of metadata fields' configuration |
||
| 40 | * @see loadIndexConf() |
||
| 41 | * |
||
| 42 | * @var array |
||
| 43 | * @access protected |
||
| 44 | */ |
||
| 45 | protected static $fields = [ |
||
| 46 | 'autocomplete' => [], |
||
| 47 | 'facets' => [], |
||
| 48 | 'sortables' => [], |
||
| 49 | 'indexed' => [], |
||
| 50 | 'stored' => [], |
||
| 51 | 'tokenized' => [], |
||
| 52 | 'fieldboost' => [] |
||
| 53 | ]; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Is the index configuration loaded? |
||
| 57 | * @see $fields |
||
| 58 | * |
||
| 59 | * @var bool |
||
| 60 | * @access protected |
||
| 61 | */ |
||
| 62 | protected static $fieldsLoaded = false; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * List of already processed documents |
||
| 66 | * |
||
| 67 | * @var array |
||
| 68 | * @access protected |
||
| 69 | */ |
||
| 70 | protected static $processedDocs = []; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Instance of \Kitodo\Dlf\Common\Solr class |
||
| 74 | * |
||
| 75 | * @var \Kitodo\Dlf\Common\Solr |
||
| 76 | * @access protected |
||
| 77 | */ |
||
| 78 | protected static $solr; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Insert given document into Solr index |
||
| 82 | * |
||
| 83 | * @access public |
||
| 84 | * |
||
| 85 | * @param \Kitodo\Dlf\Common\Document &$doc: The document to add |
||
| 86 | * @param int $core: UID of the Solr core to use |
||
| 87 | * |
||
| 88 | * @return int 0 on success or 1 on failure |
||
| 89 | */ |
||
| 90 | public static function add(Document &$doc, $core = 0) |
||
| 197 | } |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Returns the dynamic index field name for the given metadata field. |
||
| 202 | * |
||
| 203 | * @access public |
||
| 204 | * |
||
| 205 | * @param string $index_name: The metadata field's name in database |
||
| 206 | * @param int $pid: UID of the configuration page |
||
| 207 | * |
||
| 208 | * @return string The field's dynamic index name |
||
| 209 | */ |
||
| 210 | public static function getIndexFieldName($index_name, $pid = 0) |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Load indexing configuration |
||
| 230 | * |
||
| 231 | * @access protected |
||
| 232 | * |
||
| 233 | * @param int $pid: The configuration page's UID |
||
| 234 | * |
||
| 235 | * @return void |
||
| 236 | */ |
||
| 237 | protected static function loadIndexConf($pid) |
||
| 238 | { |
||
| 239 | if (!self::$fieldsLoaded) { |
||
| 240 | $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) |
||
| 241 | ->getQueryBuilderForTable('tx_dlf_metadata'); |
||
| 242 | |||
| 243 | // Get the metadata indexing options. |
||
| 244 | $result = $queryBuilder |
||
| 245 | ->select( |
||
| 246 | 'tx_dlf_metadata.index_name AS index_name', |
||
| 247 | 'tx_dlf_metadata.index_tokenized AS index_tokenized', |
||
| 248 | 'tx_dlf_metadata.index_stored AS index_stored', |
||
| 249 | 'tx_dlf_metadata.index_indexed AS index_indexed', |
||
| 250 | 'tx_dlf_metadata.is_sortable AS is_sortable', |
||
| 251 | 'tx_dlf_metadata.is_facet AS is_facet', |
||
| 252 | 'tx_dlf_metadata.is_listed AS is_listed', |
||
| 253 | 'tx_dlf_metadata.index_autocomplete AS index_autocomplete', |
||
| 254 | 'tx_dlf_metadata.index_boost AS index_boost' |
||
| 255 | ) |
||
| 256 | ->from('tx_dlf_metadata') |
||
| 257 | ->where( |
||
| 258 | $queryBuilder->expr()->eq('tx_dlf_metadata.pid', intval($pid)), |
||
| 259 | Helper::whereExpression('tx_dlf_metadata') |
||
| 260 | ) |
||
| 261 | ->execute(); |
||
| 262 | |||
| 263 | while ($indexing = $result->fetch()) { |
||
| 264 | if ($indexing['index_tokenized']) { |
||
| 265 | self::$fields['tokenized'][] = $indexing['index_name']; |
||
| 266 | } |
||
| 267 | if ( |
||
| 268 | $indexing['index_stored'] |
||
| 269 | || $indexing['is_listed'] |
||
| 270 | ) { |
||
| 271 | self::$fields['stored'][] = $indexing['index_name']; |
||
| 272 | } |
||
| 273 | if ( |
||
| 274 | $indexing['index_indexed'] |
||
| 275 | || $indexing['index_autocomplete'] |
||
| 276 | ) { |
||
| 277 | self::$fields['indexed'][] = $indexing['index_name']; |
||
| 278 | } |
||
| 279 | if ($indexing['is_sortable']) { |
||
| 280 | self::$fields['sortables'][] = $indexing['index_name']; |
||
| 281 | } |
||
| 282 | if ($indexing['is_facet']) { |
||
| 283 | self::$fields['facets'][] = $indexing['index_name']; |
||
| 284 | } |
||
| 285 | if ($indexing['index_autocomplete']) { |
||
| 286 | self::$fields['autocomplete'][] = $indexing['index_name']; |
||
| 287 | } |
||
| 288 | if ($indexing['index_boost'] > 0.0) { |
||
| 289 | self::$fields['fieldboost'][$indexing['index_name']] = floatval($indexing['index_boost']); |
||
| 290 | } else { |
||
| 291 | self::$fields['fieldboost'][$indexing['index_name']] = false; |
||
| 292 | } |
||
| 293 | } |
||
| 294 | self::$fieldsLoaded = true; |
||
| 295 | } |
||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Processes a logical unit (and its children) for the Solr index |
||
| 300 | * |
||
| 301 | * @access protected |
||
| 302 | * |
||
| 303 | * @param \Kitodo\Dlf\Common\Document &$doc: The METS document |
||
| 304 | * @param array $logicalUnit: Array of the logical unit to process |
||
| 305 | * |
||
| 306 | * @return int 0 on success or 1 on failure |
||
| 307 | */ |
||
| 308 | protected static function processLogical(Document &$doc, array $logicalUnit) |
||
| 309 | { |
||
| 310 | $errors = 0; |
||
| 311 | // Get metadata for logical unit. |
||
| 312 | $metadata = $doc->metadataArray[$logicalUnit['id']]; |
||
| 313 | if (!empty($metadata)) { |
||
| 314 | // Remove appended "valueURI" from authors' names for indexing. |
||
| 315 | if (is_array($metadata['author'])) { |
||
| 316 | foreach ($metadata['author'] as $i => $author) { |
||
| 317 | $splitName = explode(chr(31), $author); |
||
| 318 | $metadata['author'][$i] = $splitName[0]; |
||
| 319 | } |
||
| 320 | } |
||
| 321 | // Create new Solr document. |
||
| 322 | $updateQuery = self::$solr->service->createUpdate(); |
||
| 323 | $solrDoc = $updateQuery->createDocument(); |
||
| 324 | // Create unique identifier from document's UID and unit's XML ID. |
||
| 325 | $solrDoc->setField('id', $doc->uid . $logicalUnit['id']); |
||
| 326 | $solrDoc->setField('uid', $doc->uid); |
||
| 327 | $solrDoc->setField('pid', $doc->pid); |
||
| 328 | if (\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($logicalUnit['points'])) { |
||
| 329 | $solrDoc->setField('page', $logicalUnit['points']); |
||
| 330 | } |
||
| 331 | if ($logicalUnit['id'] == $doc->toplevelId) { |
||
| 332 | $solrDoc->setField('thumbnail', $doc->thumbnail); |
||
| 333 | } elseif (!empty($logicalUnit['thumbnailId'])) { |
||
| 334 | $solrDoc->setField('thumbnail', $doc->getFileLocation($logicalUnit['thumbnailId'])); |
||
| 335 | } |
||
| 336 | $solrDoc->setField('partof', $doc->parentId); |
||
| 337 | $solrDoc->setField('root', $doc->rootId); |
||
| 338 | $solrDoc->setField('sid', $logicalUnit['id']); |
||
| 339 | // There can be only one toplevel unit per UID, independently of backend configuration |
||
| 340 | $solrDoc->setField('toplevel', $logicalUnit['id'] == $doc->toplevelId ? true : false); |
||
| 341 | $solrDoc->setField('type', $logicalUnit['type'], self::$fields['fieldboost']['type']); |
||
| 342 | $solrDoc->setField('title', $metadata['title'][0], self::$fields['fieldboost']['title']); |
||
| 343 | $solrDoc->setField('volume', $metadata['volume'][0], self::$fields['fieldboost']['volume']); |
||
| 344 | $solrDoc->setField('record_id', $metadata['record_id'][0]); |
||
| 345 | $solrDoc->setField('purl', $metadata['purl'][0]); |
||
| 346 | $solrDoc->setField('location', $doc->location); |
||
| 347 | $solrDoc->setField('urn', $metadata['urn']); |
||
| 348 | $solrDoc->setField('license', $metadata['license']); |
||
| 349 | $solrDoc->setField('terms', $metadata['terms']); |
||
| 350 | $solrDoc->setField('restrictions', $metadata['restrictions']); |
||
| 351 | $solrDoc->setField('collection', $doc->metadataArray[$doc->toplevelId]['collection']); |
||
| 352 | $coordinates = json_decode($metadata['coordinates'][0]); |
||
| 353 | if (is_object($coordinates)) { |
||
| 354 | $solrDoc->setField('geom', json_encode($coordinates->features[0])); |
||
| 355 | } |
||
| 356 | $autocomplete = []; |
||
| 357 | foreach ($metadata as $index_name => $data) { |
||
| 358 | if ( |
||
| 359 | !empty($data) |
||
| 360 | && substr($index_name, -8) !== '_sorting' |
||
| 361 | ) { |
||
| 362 | $solrDoc->setField(self::getIndexFieldName($index_name, $doc->pid), $data, self::$fields['fieldboost'][$index_name]); |
||
| 363 | if (in_array($index_name, self::$fields['sortables'])) { |
||
| 364 | // Add sortable fields to index. |
||
| 365 | $solrDoc->setField($index_name . '_sorting', $metadata[$index_name . '_sorting'][0]); |
||
| 366 | } |
||
| 367 | if (in_array($index_name, self::$fields['facets'])) { |
||
| 368 | // Add facets to index. |
||
| 369 | $solrDoc->setField($index_name . '_faceting', $data); |
||
| 370 | } |
||
| 371 | if (in_array($index_name, self::$fields['autocomplete'])) { |
||
| 372 | $autocomplete = array_merge($autocomplete, $data); |
||
| 373 | } |
||
| 374 | } |
||
| 375 | } |
||
| 376 | // Add autocomplete values to index. |
||
| 377 | if (!empty($autocomplete)) { |
||
| 378 | $solrDoc->setField('autocomplete', $autocomplete); |
||
| 379 | } |
||
| 380 | // Add collection information to logical sub-elements if applicable. |
||
| 381 | if ( |
||
| 382 | in_array('collection', self::$fields['facets']) |
||
| 383 | && empty($metadata['collection']) |
||
| 384 | && !empty($doc->metadataArray[$doc->toplevelId]['collection']) |
||
| 385 | ) { |
||
| 386 | $solrDoc->setField('collection_faceting', $doc->metadataArray[$doc->toplevelId]['collection']); |
||
| 387 | } |
||
| 388 | try { |
||
| 389 | $updateQuery->addDocument($solrDoc); |
||
| 390 | self::$solr->service->update($updateQuery); |
||
| 391 | } catch (\Exception $e) { |
||
| 392 | if (!(\TYPO3_REQUESTTYPE & \TYPO3_REQUESTTYPE_CLI)) { |
||
| 393 | Helper::addMessage( |
||
| 394 | Helper::getMessage('flash.solrException', true) . '<br />' . htmlspecialchars($e->getMessage()), |
||
| 395 | Helper::getMessage('flash.error', true), |
||
| 396 | \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR, |
||
| 397 | true, |
||
| 398 | 'core.template.flashMessages' |
||
| 399 | ); |
||
| 400 | } |
||
| 401 | return 1; |
||
| 402 | } |
||
| 403 | } |
||
| 404 | // Check for child elements... |
||
| 405 | if (!empty($logicalUnit['children'])) { |
||
| 406 | foreach ($logicalUnit['children'] as $child) { |
||
| 407 | if (!$errors) { |
||
| 408 | // ...and process them, too. |
||
| 409 | $errors = self::processLogical($doc, $child); |
||
| 410 | } else { |
||
| 411 | break; |
||
| 412 | } |
||
| 413 | } |
||
| 414 | } |
||
| 415 | return $errors; |
||
| 416 | } |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Processes a physical unit for the Solr index |
||
| 420 | * |
||
| 421 | * @access protected |
||
| 422 | * |
||
| 423 | * @param \Kitodo\Dlf\Common\Document &$doc: The METS document |
||
| 424 | * @param int $page: The page number |
||
| 425 | * @param array $physicalUnit: Array of the physical unit to process |
||
| 426 | * |
||
| 427 | * @return int 0 on success or 1 on failure |
||
| 428 | */ |
||
| 429 | protected static function processPhysical(Document &$doc, $page, array $physicalUnit) |
||
| 546 | } |
||
| 547 | |||
| 548 | /** |
||
| 549 | * Connects to Solr server. |
||
| 550 | * |
||
| 551 | * @access protected |
||
| 552 | * |
||
| 553 | * @param int $core: UID of the Solr core |
||
| 554 | * @param int $pid: UID of the configuration page |
||
| 555 | * |
||
| 556 | * @return bool true on success or false on failure |
||
| 557 | */ |
||
| 558 | protected static function solrConnect($core, $pid = 0) |
||
| 573 | } |
||
| 574 | |||
| 575 | /** |
||
| 576 | * Prevent instantiation by hiding the constructor |
||
| 577 | * |
||
| 578 | * @access private |
||
| 579 | */ |
||
| 580 | private function __construct() |
||
| 585 |