We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 79 |
| Total Lines | 534 |
| Duplicated Lines | 0 % |
| Changes | 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 |
||
| 22 | class Indexer { |
||
| 23 | /** |
||
| 24 | * The extension key |
||
| 25 | * |
||
| 26 | * @var string |
||
| 27 | * @access public |
||
| 28 | */ |
||
| 29 | public static $extKey = 'dlf'; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Array of metadata fields' configuration |
||
| 33 | * @see loadIndexConf() |
||
| 34 | * |
||
| 35 | * @var array |
||
| 36 | * @access protected |
||
| 37 | */ |
||
| 38 | protected static $fields = [ |
||
| 39 | 'autocomplete' => [], |
||
| 40 | 'facets' => [], |
||
| 41 | 'sortables' => [], |
||
| 42 | 'indexed' => [], |
||
| 43 | 'stored' => [], |
||
| 44 | 'tokenized' => [], |
||
| 45 | 'fieldboost' => [] |
||
| 46 | ]; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Is the index configuration loaded? |
||
| 50 | * @see $fields |
||
| 51 | * |
||
| 52 | * @var boolean |
||
| 53 | * @access protected |
||
| 54 | */ |
||
| 55 | protected static $fieldsLoaded = FALSE; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * List of already processed documents |
||
| 59 | * |
||
| 60 | * @var array |
||
| 61 | * @access protected |
||
| 62 | */ |
||
| 63 | protected static $processedDocs = []; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Instance of \Kitodo\Dlf\Common\Solr class |
||
| 67 | * |
||
| 68 | * @var \Kitodo\Dlf\Common\Solr |
||
| 69 | * @access protected |
||
| 70 | */ |
||
| 71 | protected static $solr; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Insert given document into Solr index |
||
| 75 | * |
||
| 76 | * @access public |
||
| 77 | * |
||
| 78 | * @param \Kitodo\Dlf\Common\Document &$doc: The document to add |
||
| 79 | * @param integer $core: UID of the Solr core to use |
||
| 80 | * |
||
| 81 | * @return integer 0 on success or 1 on failure |
||
| 82 | */ |
||
| 83 | public static function add(Document &$doc, $core = 0) { |
||
| 179 | } |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Delete document from Solr index |
||
| 184 | * |
||
| 185 | * @access public |
||
| 186 | * |
||
| 187 | * @param integer $uid: UID of the document to delete |
||
| 188 | * |
||
| 189 | * @return integer 0 on success or 1 on failure |
||
| 190 | */ |
||
| 191 | public static function delete($uid) { |
||
| 192 | // Sanitize input. |
||
| 193 | $uid = max(intval($uid), 0); |
||
| 194 | // Get Solr core for document. |
||
| 195 | $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery( |
||
| 196 | 'tx_dlf_solrcores.uid AS uid,tx_dlf_documents.title AS title', |
||
| 197 | 'tx_dlf_solrcores,tx_dlf_documents', |
||
| 198 | 'tx_dlf_solrcores.uid=tx_dlf_documents.solrcore' |
||
| 199 | .' AND tx_dlf_documents.uid='.$uid |
||
| 200 | .Helper::whereClause('tx_dlf_solrcores'), |
||
| 201 | '', |
||
| 202 | '', |
||
| 203 | '1' |
||
| 204 | ); |
||
| 205 | if ($GLOBALS['TYPO3_DB']->sql_num_rows($result)) { |
||
| 206 | list ($core, $title) = $GLOBALS['TYPO3_DB']->sql_fetch_row($result); |
||
| 207 | // Establish Solr connection. |
||
| 208 | if (self::solrConnect($core)) { |
||
| 209 | try { |
||
| 210 | // Delete Solr document. |
||
| 211 | $updateQuery = self::$solr->service->createUpdate(); |
||
| 212 | $updateQuery->addDeleteQuery('uid:'.$uid); |
||
| 213 | $updateQuery->addCommit(); |
||
| 214 | self::$solr->service->update($updateQuery); |
||
| 215 | } catch (\Exception $e) { |
||
| 216 | if ((TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_CLI) == FALSE) { |
||
| 217 | Helper::addMessage( |
||
| 218 | Helper::getMessage('flash.solrException', TRUE).'<br />'.htmlspecialchars($e->getMessage()), |
||
| 219 | Helper::getMessage('flash.error', TRUE), |
||
| 220 | \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR, |
||
| 221 | TRUE |
||
| 222 | ); |
||
| 223 | } |
||
| 224 | Helper::devLog('Apache Solr threw exception: "'.$e->getMessage().'"', DEVLOG_SEVERITY_ERROR); |
||
| 225 | return 1; |
||
| 226 | } |
||
| 227 | } else { |
||
| 228 | if ((TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_CLI) == FALSE) { |
||
| 229 | Helper::addMessage( |
||
| 230 | Helper::getMessage('flash.solrNoConnection', TRUE), |
||
| 231 | Helper::getMessage('flash.error', TRUE), |
||
| 232 | \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR, |
||
| 233 | TRUE |
||
| 234 | ); |
||
| 235 | } |
||
| 236 | Helper::devLog('Could not connect to Apache Solr server', DEVLOG_SEVERITY_ERROR); |
||
| 237 | return 1; |
||
| 238 | } |
||
| 239 | if ((TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_CLI) == FALSE) { |
||
| 240 | Helper::addMessage( |
||
| 241 | htmlspecialchars(sprintf(Helper::getMessage('flash.documentDeleted'), $title, $uid)), |
||
| 242 | Helper::getMessage('flash.done', TRUE), |
||
| 243 | \TYPO3\CMS\Core\Messaging\FlashMessage::OK, |
||
| 244 | TRUE |
||
| 245 | ); |
||
| 246 | } |
||
| 247 | return 0; |
||
| 248 | } else { |
||
| 249 | Helper::devLog('Invalid UID '.$uid.' for document deletion', DEVLOG_SEVERITY_ERROR); |
||
| 250 | return 1; |
||
| 251 | } |
||
| 252 | } |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Returns the dynamic index field name for the given metadata field. |
||
| 256 | * |
||
| 257 | * @access public |
||
| 258 | * |
||
| 259 | * @param string $index_name: The metadata field's name in database |
||
| 260 | * @param integer $pid: UID of the configuration page |
||
| 261 | * |
||
| 262 | * @return string The field's dynamic index name |
||
| 263 | */ |
||
| 264 | public static function getIndexFieldName($index_name, $pid = 0) { |
||
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Load indexing configuration |
||
| 283 | * |
||
| 284 | * @access protected |
||
| 285 | * |
||
| 286 | * @param integer $pid: The configuration page's UID |
||
| 287 | * |
||
| 288 | * @return void |
||
| 289 | */ |
||
| 290 | protected static function loadIndexConf($pid) { |
||
| 291 | if (!self::$fieldsLoaded) { |
||
| 292 | // Get the metadata indexing options. |
||
| 293 | $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery( |
||
| 294 | 'tx_dlf_metadata.index_name AS index_name,tx_dlf_metadata.index_tokenized AS index_tokenized,tx_dlf_metadata.index_stored AS index_stored,tx_dlf_metadata.index_indexed AS index_indexed,tx_dlf_metadata.is_sortable AS is_sortable,tx_dlf_metadata.is_facet AS is_facet,tx_dlf_metadata.is_listed AS is_listed,tx_dlf_metadata.index_autocomplete AS index_autocomplete,tx_dlf_metadata.index_boost AS index_boost', |
||
| 295 | 'tx_dlf_metadata', |
||
| 296 | 'tx_dlf_metadata.pid='.intval($pid) |
||
| 297 | .Helper::whereClause('tx_dlf_metadata'), |
||
| 298 | '', |
||
| 299 | '', |
||
| 300 | '' |
||
| 301 | ); |
||
| 302 | while ($indexing = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result)) { |
||
| 303 | if ($indexing['index_tokenized']) { |
||
| 304 | self::$fields['tokenized'][] = $indexing['index_name']; |
||
| 305 | } |
||
| 306 | if ($indexing['index_stored'] |
||
| 307 | || $indexing['is_listed']) { |
||
| 308 | self::$fields['stored'][] = $indexing['index_name']; |
||
| 309 | } |
||
| 310 | if ($indexing['index_indexed'] |
||
| 311 | || $indexing['index_autocomplete']) { |
||
| 312 | self::$fields['indexed'][] = $indexing['index_name']; |
||
| 313 | } |
||
| 314 | if ($indexing['is_sortable']) { |
||
| 315 | self::$fields['sortables'][] = $indexing['index_name']; |
||
| 316 | } |
||
| 317 | if ($indexing['is_facet']) { |
||
| 318 | self::$fields['facets'][] = $indexing['index_name']; |
||
| 319 | } |
||
| 320 | if ($indexing['index_autocomplete']) { |
||
| 321 | self::$fields['autocomplete'][] = $indexing['index_name']; |
||
| 322 | } |
||
| 323 | if ($indexing['index_boost'] > 0.0) { |
||
| 324 | self::$fields['fieldboost'][$indexing['index_name']] = floatval($indexing['index_boost']); |
||
| 325 | } else { |
||
| 326 | self::$fields['fieldboost'][$indexing['index_name']] = FALSE; |
||
| 327 | } |
||
| 328 | } |
||
| 329 | self::$fieldsLoaded = TRUE; |
||
| 330 | } |
||
| 331 | } |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Processes a logical unit (and its children) for the Solr index |
||
| 335 | * |
||
| 336 | * @access protected |
||
| 337 | * |
||
| 338 | * @param \Kitodo\Dlf\Common\Document &$doc: The METS document |
||
| 339 | * @param array $logicalUnit: Array of the logical unit to process |
||
| 340 | * |
||
| 341 | * @return integer 0 on success or 1 on failure |
||
| 342 | */ |
||
| 343 | protected static function processLogical(Document &$doc, array $logicalUnit) { |
||
| 344 | $errors = 0; |
||
| 345 | // Get metadata for logical unit. |
||
| 346 | $metadata = $doc->metadataArray[$logicalUnit['id']]; |
||
| 347 | if (!empty($metadata)) { |
||
| 348 | // Create new Solr document. |
||
| 349 | $updateQuery = self::$solr->service->createUpdate(); |
||
| 350 | $solrDoc = $updateQuery->createDocument(); |
||
| 351 | // Create unique identifier from document's UID and unit's XML ID. |
||
| 352 | $solrDoc->setField('id', $doc->uid.$logicalUnit['id']); |
||
| 353 | $solrDoc->setField('uid', $doc->uid); |
||
| 354 | $solrDoc->setField('pid', $doc->pid); |
||
| 355 | if (\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($logicalUnit['points'])) { |
||
| 356 | $solrDoc->setField('page', $logicalUnit['points']); |
||
| 357 | } |
||
| 358 | if ($logicalUnit['id'] == $doc->toplevelId) { |
||
| 359 | $solrDoc->setField('thumbnail', $doc->thumbnail); |
||
| 360 | } elseif (!empty($logicalUnit['thumbnailId'])) { |
||
| 361 | $solrDoc->setField('thumbnail', $doc->getFileLocation($logicalUnit['thumbnailId'])); |
||
| 362 | } |
||
| 363 | $solrDoc->setField('partof', $doc->parentId); |
||
| 364 | $solrDoc->setField('root', $doc->rootId); |
||
| 365 | $solrDoc->setField('sid', $logicalUnit['id']); |
||
| 366 | // There can be only one toplevel unit per UID, independently of backend configuration |
||
| 367 | $solrDoc->setField('toplevel', $logicalUnit['id'] == $doc->toplevelId ? TRUE : FALSE); |
||
| 368 | $solrDoc->setField('type', $logicalUnit['type'], self::$fields['fieldboost']['type']); |
||
| 369 | $solrDoc->setField('title', $metadata['title'][0], self::$fields['fieldboost']['title']); |
||
| 370 | $solrDoc->setField('volume', $metadata['volume'][0], self::$fields['fieldboost']['volume']); |
||
| 371 | $solrDoc->setField('record_id', $metadata['record_id'][0]); |
||
| 372 | $solrDoc->setField('purl', $metadata['purl'][0]); |
||
| 373 | $solrDoc->setField('location', $doc->location); |
||
| 374 | $solrDoc->setField('urn', $metadata['urn']); |
||
| 375 | $solrDoc->setField('collection', $doc->metadataArray[$doc->toplevelId]['collection']); |
||
| 376 | $autocomplete = []; |
||
| 377 | foreach ($metadata as $index_name => $data) { |
||
| 378 | if (!empty($data) |
||
| 379 | && substr($index_name, -8) !== '_sorting') { |
||
| 380 | $solrDoc->setField(self::getIndexFieldName($index_name, $doc->pid), $data, self::$fields['fieldboost'][$index_name]); |
||
| 381 | if (in_array($index_name, self::$fields['sortables'])) { |
||
| 382 | // Add sortable fields to index. |
||
| 383 | $solrDoc->setField($index_name.'_sorting', $metadata[$index_name.'_sorting'][0]); |
||
| 384 | } |
||
| 385 | if (in_array($index_name, self::$fields['facets'])) { |
||
| 386 | // Add facets to index. |
||
| 387 | $solrDoc->setField($index_name.'_faceting', $data); |
||
| 388 | } |
||
| 389 | if (in_array($index_name, self::$fields['autocomplete'])) { |
||
| 390 | $autocomplete = array_merge($autocomplete, $data); |
||
| 391 | } |
||
| 392 | } |
||
| 393 | } |
||
| 394 | // Add autocomplete values to index. |
||
| 395 | if (!empty($autocomplete)) { |
||
| 396 | $solrDoc->setField('autocomplete', $autocomplete); |
||
| 397 | } |
||
| 398 | // Add collection information to logical sub-elements if applicable. |
||
| 399 | if (in_array('collection', self::$fields['facets']) |
||
| 400 | && empty($metadata['collection']) |
||
| 401 | && !empty($doc->metadataArray[$doc->toplevelId]['collection'])) { |
||
| 402 | $solrDoc->setField('collection_faceting', $doc->metadataArray[$doc->toplevelId]['collection']); |
||
| 403 | } |
||
| 404 | try { |
||
| 405 | $updateQuery->addDocument($solrDoc); |
||
| 406 | self::$solr->service->update($updateQuery); |
||
| 407 | } catch (\Exception $e) { |
||
| 408 | if ((TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_CLI) == FALSE) { |
||
| 409 | Helper::addMessage( |
||
| 410 | Helper::getMessage('flash.solrException', TRUE).'<br />'.htmlspecialchars($e->getMessage()), |
||
| 411 | Helper::getMessage('flash.error', TRUE), |
||
| 412 | \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR, |
||
| 413 | TRUE |
||
| 414 | ); |
||
| 415 | } |
||
| 416 | return 1; |
||
| 417 | } |
||
| 418 | } |
||
| 419 | // Check for child elements... |
||
| 420 | if (!empty($logicalUnit['children'])) { |
||
| 421 | foreach ($logicalUnit['children'] as $child) { |
||
| 422 | if (!$errors) { |
||
| 423 | // ...and process them, too. |
||
| 424 | $errors = self::processLogical($doc, $child); |
||
| 425 | } else { |
||
| 426 | break; |
||
| 427 | } |
||
| 428 | } |
||
| 429 | } |
||
| 430 | return $errors; |
||
| 431 | } |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Processes a physical unit for the Solr index |
||
| 435 | * |
||
| 436 | * @access protected |
||
| 437 | * |
||
| 438 | * @param \Kitodo\Dlf\Common\Document &$doc: The METS document |
||
| 439 | * @param integer $page: The page number |
||
| 440 | * @param array $physicalUnit: Array of the physical unit to process |
||
| 441 | * |
||
| 442 | * @return integer 0 on success or 1 on failure |
||
| 443 | */ |
||
| 444 | protected static function processPhysical(Document &$doc, $page, array $physicalUnit) { |
||
| 522 | } |
||
| 523 | |||
| 524 | /** |
||
| 525 | * Connects to Solr server. |
||
| 526 | * |
||
| 527 | * @access protected |
||
| 528 | * |
||
| 529 | * @param integer $core: UID of the Solr core |
||
| 530 | * @param integer $pid: UID of the configuration page |
||
| 531 | * |
||
| 532 | * @return boolean TRUE on success or FALSE on failure |
||
| 533 | */ |
||
| 534 | protected static function solrConnect($core, $pid = 0) { |
||
| 548 | } |
||
| 549 | |||
| 550 | /** |
||
| 551 | * This is a static class, thus no instances should be created |
||
| 552 | * |
||
| 553 | * @access private |
||
| 554 | */ |
||
| 555 | private function __construct() {} |
||
| 556 | } |
||
| 557 |