| Total Complexity | 86 |
| Total Lines | 867 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like tx_dlf_indexing 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 tx_dlf_indexing, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class tx_dlf_indexing { |
||
| 21 | |||
| 22 | /** |
||
| 23 | * The extension key |
||
| 24 | * |
||
| 25 | * @var string |
||
| 26 | * @access public |
||
| 27 | */ |
||
| 28 | public static $extKey = 'dlf'; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Array of metadata fields' configuration |
||
| 32 | * @see loadIndexConf() |
||
| 33 | * |
||
| 34 | * @var array |
||
| 35 | * @access protected |
||
| 36 | */ |
||
| 37 | protected static $fields = array ( |
||
| 38 | 'autocomplete' => array (), |
||
| 39 | 'facets' => array (), |
||
| 40 | 'sortables' => array (), |
||
| 41 | 'indexed' => array (), |
||
| 42 | 'stored' => array (), |
||
| 43 | 'tokenized' => array (), |
||
| 44 | 'fieldboost' => array () |
||
| 45 | ); |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Is the index configuration loaded? |
||
| 49 | * @see $fields |
||
| 50 | * |
||
| 51 | * @var boolean |
||
| 52 | * @access protected |
||
| 53 | */ |
||
| 54 | protected static $fieldsLoaded = FALSE; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * List of already processed documents |
||
| 58 | * |
||
| 59 | * @var array |
||
| 60 | * @access protected |
||
| 61 | */ |
||
| 62 | protected static $processedDocs = array (); |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Instance of Apache_Solr_Service class |
||
| 66 | * |
||
| 67 | * @var Apache_Solr_Service |
||
| 68 | * @access protected |
||
| 69 | */ |
||
| 70 | protected static $solr; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Array of toplevel structure elements |
||
| 74 | * @see loadIndexConf() |
||
| 75 | * |
||
| 76 | * @var array |
||
| 77 | * @access protected |
||
| 78 | */ |
||
| 79 | protected static $toplevel = array (); |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Insert given document into Solr index |
||
| 83 | * |
||
| 84 | * @access public |
||
| 85 | * |
||
| 86 | * @param tx_dlf_document &$doc: The document to add |
||
| 87 | * @param integer $core: UID of the Solr core to use |
||
| 88 | * |
||
| 89 | * @return integer 0 on success or 1 on failure |
||
| 90 | */ |
||
| 91 | public static function add(tx_dlf_document &$doc, $core = 0) { |
||
| 92 | |||
| 93 | if (in_array($doc->uid, self::$processedDocs)) { |
||
|
|
|||
| 94 | |||
| 95 | return 0; |
||
| 96 | |||
| 97 | } elseif (self::solrConnect($core, $doc->pid)) { |
||
| 98 | |||
| 99 | $errors = 0; |
||
| 100 | |||
| 101 | // Handle multi-volume documents. |
||
| 102 | if ($doc->parentId) { |
||
| 103 | |||
| 104 | $parent = & tx_dlf_document::getInstance($doc->parentId, 0, TRUE); |
||
| 105 | |||
| 106 | if ($parent->ready) { |
||
| 107 | |||
| 108 | $errors = self::add($parent, $core); |
||
| 109 | |||
| 110 | } else { |
||
| 111 | |||
| 112 | if (TYPO3_DLOG) { |
||
| 113 | |||
| 114 | \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('[tx_dlf_indexing->add(['.$doc->uid.'], '.$core.')] Could not load parent document with UID "'.$doc->parentId.'"', self::$extKey, SYSLOG_SEVERITY_ERROR); |
||
| 115 | |||
| 116 | } |
||
| 117 | |||
| 118 | return 1; |
||
| 119 | |||
| 120 | } |
||
| 121 | |||
| 122 | } |
||
| 123 | |||
| 124 | try { |
||
| 125 | |||
| 126 | // Add document to list of processed documents. |
||
| 127 | self::$processedDocs[] = $doc->uid; |
||
| 128 | |||
| 129 | // Delete old Solr documents. |
||
| 130 | self::$solr->service->deleteByQuery('uid:'.$doc->uid); |
||
| 131 | |||
| 132 | // Index every logical unit as separate Solr document. |
||
| 133 | foreach ($doc->tableOfContents as $logicalUnit) { |
||
| 134 | |||
| 135 | if (!$errors) { |
||
| 136 | |||
| 137 | $errors = self::processLogical($doc, $logicalUnit); |
||
| 138 | |||
| 139 | } else { |
||
| 140 | |||
| 141 | break; |
||
| 142 | |||
| 143 | } |
||
| 144 | |||
| 145 | } |
||
| 146 | |||
| 147 | // Index fulltext files if available. |
||
| 148 | if ($doc->hasFulltext) { |
||
| 149 | |||
| 150 | foreach ($doc->physicalStructure as $pageNumber => $xmlId) { |
||
| 151 | |||
| 152 | if (!$errors) { |
||
| 153 | |||
| 154 | $errors = self::processPhysical($doc, $pageNumber, $doc->physicalStructureInfo[$xmlId]); |
||
| 155 | |||
| 156 | } else { |
||
| 157 | |||
| 158 | break; |
||
| 159 | |||
| 160 | } |
||
| 161 | |||
| 162 | } |
||
| 163 | |||
| 164 | } |
||
| 165 | |||
| 166 | self::$solr->service->commit(); |
||
| 167 | |||
| 168 | // Get document title from database. |
||
| 169 | $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery( |
||
| 170 | 'tx_dlf_documents.title AS title', |
||
| 171 | 'tx_dlf_documents', |
||
| 172 | 'tx_dlf_documents.uid='.intval($doc->uid).tx_dlf_helper::whereClause('tx_dlf_documents'), |
||
| 173 | '', |
||
| 174 | '', |
||
| 175 | '1' |
||
| 176 | ); |
||
| 177 | |||
| 178 | $resArray = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result); |
||
| 179 | |||
| 180 | if (!defined('TYPO3_cliMode')) { |
||
| 181 | |||
| 182 | if (!$errors) { |
||
| 183 | |||
| 184 | $message = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance( |
||
| 185 | 'TYPO3\\CMS\\Core\\Messaging\\FlashMessage', |
||
| 186 | htmlspecialchars(sprintf(tx_dlf_helper::getLL('flash.documentIndexed'), $resArray['title'], $doc->uid)), |
||
| 187 | tx_dlf_helper::getLL('flash.done', TRUE), |
||
| 188 | \TYPO3\CMS\Core\Messaging\FlashMessage::OK, |
||
| 189 | TRUE |
||
| 190 | ); |
||
| 191 | |||
| 192 | } else { |
||
| 193 | |||
| 194 | $message = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance( |
||
| 195 | 'TYPO3\\CMS\\Core\\Messaging\\FlashMessage', |
||
| 196 | htmlspecialchars(sprintf(tx_dlf_helper::getLL('flash.documentNotIndexed'), $resArray['title'], $doc->uid)), |
||
| 197 | tx_dlf_helper::getLL('flash.error', TRUE), |
||
| 198 | \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR, |
||
| 199 | TRUE |
||
| 200 | ); |
||
| 201 | |||
| 202 | } |
||
| 203 | |||
| 204 | tx_dlf_helper::addMessage($message); |
||
| 205 | |||
| 206 | } |
||
| 207 | |||
| 208 | return $errors; |
||
| 209 | |||
| 210 | } catch (Exception $e) { |
||
| 211 | |||
| 212 | if (!defined('TYPO3_cliMode')) { |
||
| 213 | |||
| 214 | $message = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance( |
||
| 215 | 'TYPO3\\CMS\\Core\\Messaging\\FlashMessage', |
||
| 216 | tx_dlf_helper::getLL('flash.solrException', TRUE).'<br />'.htmlspecialchars($e->getMessage()), |
||
| 217 | tx_dlf_helper::getLL('flash.error', TRUE), |
||
| 218 | \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR, |
||
| 219 | TRUE |
||
| 220 | ); |
||
| 221 | |||
| 222 | tx_dlf_helper::addMessage($message); |
||
| 223 | |||
| 224 | } |
||
| 225 | |||
| 226 | if (TYPO3_DLOG) { |
||
| 227 | |||
| 228 | \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('[tx_dlf_indexing->add(['.$doc->uid.'], '.$core.')] Apache Solr threw exception: "'.$e->getMessage().'"', self::$extKey, SYSLOG_SEVERITY_ERROR); |
||
| 229 | |||
| 230 | } |
||
| 231 | |||
| 232 | return 1; |
||
| 233 | |||
| 234 | } |
||
| 235 | |||
| 236 | } else { |
||
| 237 | |||
| 238 | if (!defined('TYPO3_cliMode')) { |
||
| 239 | |||
| 240 | $message = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance( |
||
| 241 | 'TYPO3\\CMS\\Core\\Messaging\\FlashMessage', |
||
| 242 | tx_dlf_helper::getLL('flash.solrNoConnection', TRUE), |
||
| 243 | tx_dlf_helper::getLL('flash.warning', TRUE), |
||
| 244 | \TYPO3\CMS\Core\Messaging\FlashMessage::WARNING, |
||
| 245 | TRUE |
||
| 246 | ); |
||
| 247 | |||
| 248 | tx_dlf_helper::addMessage($message); |
||
| 249 | |||
| 250 | } |
||
| 251 | |||
| 252 | if (TYPO3_DLOG) { |
||
| 253 | |||
| 254 | \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('[tx_dlf_indexing->add(['.$doc->uid.'], '.$core.')] Could not connect to Apache Solr server', self::$extKey, SYSLOG_SEVERITY_ERROR); |
||
| 255 | |||
| 256 | } |
||
| 257 | |||
| 258 | return 1; |
||
| 259 | |||
| 260 | } |
||
| 261 | |||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Delete document from Solr index |
||
| 266 | * |
||
| 267 | * @access public |
||
| 268 | * |
||
| 269 | * @param integer $uid: UID of the document to delete |
||
| 270 | * |
||
| 271 | * @return integer 0 on success or 1 on failure |
||
| 272 | */ |
||
| 273 | public static function delete($uid) { |
||
| 274 | |||
| 275 | // Save parameter for logging purposes. |
||
| 276 | $_uid = $uid; |
||
| 277 | |||
| 278 | // Sanitize input. |
||
| 279 | $uid = max(intval($uid), 0); |
||
| 280 | |||
| 281 | // Get Solr core for document. |
||
| 282 | $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery( |
||
| 283 | 'tx_dlf_solrcores.uid AS uid,tx_dlf_documents.title AS title', |
||
| 284 | 'tx_dlf_solrcores,tx_dlf_documents', |
||
| 285 | 'tx_dlf_solrcores.uid=tx_dlf_documents.solrcore AND tx_dlf_documents.uid='.$uid.tx_dlf_helper::whereClause('tx_dlf_solrcores'), |
||
| 286 | '', |
||
| 287 | '', |
||
| 288 | '1' |
||
| 289 | ); |
||
| 290 | |||
| 291 | if ($GLOBALS['TYPO3_DB']->sql_num_rows($result)) { |
||
| 292 | |||
| 293 | list ($core, $title) = $GLOBALS['TYPO3_DB']->sql_fetch_row($result); |
||
| 294 | |||
| 295 | // Establish Solr connection. |
||
| 296 | if (self::solrConnect($core)) { |
||
| 297 | |||
| 298 | try { |
||
| 299 | |||
| 300 | // Delete Solr document. |
||
| 301 | self::$solr->service->deleteByQuery('uid:'.$uid); |
||
| 302 | |||
| 303 | self::$solr->service->commit(); |
||
| 304 | |||
| 305 | } catch (Exception $e) { |
||
| 306 | |||
| 307 | if (!defined('TYPO3_cliMode')) { |
||
| 308 | |||
| 309 | $message = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance( |
||
| 310 | 'TYPO3\\CMS\\Core\\Messaging\\FlashMessage', |
||
| 311 | tx_dlf_helper::getLL('flash.solrException', TRUE).'<br />'.htmlspecialchars($e->getMessage()), |
||
| 312 | tx_dlf_helper::getLL('flash.error', TRUE), |
||
| 313 | \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR, |
||
| 314 | TRUE |
||
| 315 | ); |
||
| 316 | |||
| 317 | tx_dlf_helper::addMessage($message); |
||
| 318 | |||
| 319 | } |
||
| 320 | |||
| 321 | if (TYPO3_DLOG) { |
||
| 322 | |||
| 323 | \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('[tx_dlf_indexing->delete('.$_uid.')] Apache Solr threw exception: "'.$e->getMessage().'"', self::$extKey, SYSLOG_SEVERITY_ERROR); |
||
| 324 | |||
| 325 | } |
||
| 326 | |||
| 327 | return 1; |
||
| 328 | |||
| 329 | } |
||
| 330 | |||
| 331 | } else { |
||
| 332 | |||
| 333 | if (!defined('TYPO3_cliMode')) { |
||
| 334 | |||
| 335 | $message = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance( |
||
| 336 | 'TYPO3\\CMS\\Core\\Messaging\\FlashMessage', |
||
| 337 | tx_dlf_helper::getLL('flash.solrNoConnection', TRUE), |
||
| 338 | tx_dlf_helper::getLL('flash.error', TRUE), |
||
| 339 | \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR, |
||
| 340 | TRUE |
||
| 341 | ); |
||
| 342 | |||
| 343 | tx_dlf_helper::addMessage($message); |
||
| 344 | |||
| 345 | } |
||
| 346 | |||
| 347 | if (TYPO3_DLOG) { |
||
| 348 | |||
| 349 | \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('[tx_dlf_indexing->delete('.$_uid.')] Could not connect to Apache Solr server', self::$extKey, SYSLOG_SEVERITY_ERROR); |
||
| 350 | |||
| 351 | } |
||
| 352 | |||
| 353 | return 1; |
||
| 354 | |||
| 355 | } |
||
| 356 | |||
| 357 | if (!defined('TYPO3_cliMode')) { |
||
| 358 | |||
| 359 | $message = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance( |
||
| 360 | 'TYPO3\\CMS\\Core\\Messaging\\FlashMessage', |
||
| 361 | htmlspecialchars(sprintf(tx_dlf_helper::getLL('flash.documentDeleted'), $title, $uid)), |
||
| 362 | tx_dlf_helper::getLL('flash.done', TRUE), |
||
| 363 | \TYPO3\CMS\Core\Messaging\FlashMessage::OK, |
||
| 364 | TRUE |
||
| 365 | ); |
||
| 366 | |||
| 367 | tx_dlf_helper::addMessage($message); |
||
| 368 | |||
| 369 | } |
||
| 370 | |||
| 371 | return 0; |
||
| 372 | |||
| 373 | } else { |
||
| 374 | |||
| 375 | if (TYPO3_DLOG) { |
||
| 376 | |||
| 377 | \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('[tx_dlf_indexing->delete('.$_uid.')] Invalid UID "'.$uid.'" for document deletion', self::$extKey, SYSLOG_SEVERITY_ERROR); |
||
| 378 | |||
| 379 | } |
||
| 380 | |||
| 381 | return 1; |
||
| 382 | |||
| 383 | } |
||
| 384 | |||
| 385 | } |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Returns the dynamic index field name for the given metadata field. |
||
| 389 | * |
||
| 390 | * @access public |
||
| 391 | * |
||
| 392 | * @param string $index_name: The metadata field's name in database |
||
| 393 | * @param integer $pid: UID of the configuration page |
||
| 394 | * |
||
| 395 | * @return string The field's dynamic index name |
||
| 396 | */ |
||
| 397 | public static function getIndexFieldName($index_name, $pid = 0) { |
||
| 398 | |||
| 399 | // Save parameter for logging purposes. |
||
| 400 | $_pid = $pid; |
||
| 401 | |||
| 402 | // Sanitize input. |
||
| 403 | $pid = max(intval($pid), 0); |
||
| 404 | |||
| 405 | if (!$pid) { |
||
| 406 | |||
| 407 | if (TYPO3_DLOG) { |
||
| 408 | |||
| 409 | \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('[tx_dlf_indexing->getIndexFieldName('.$index_name.', '.$_pid.')] Invalid PID "'.$pid.'" for metadata configuration', self::$extKey, SYSLOG_SEVERITY_ERROR); |
||
| 410 | |||
| 411 | } |
||
| 412 | |||
| 413 | return ''; |
||
| 414 | |||
| 415 | } |
||
| 416 | |||
| 417 | // Load metadata configuration. |
||
| 418 | self::loadIndexConf($pid); |
||
| 419 | |||
| 420 | // Build field's suffix. |
||
| 421 | $suffix = (in_array($index_name, self::$fields['tokenized']) ? 't' : 'u'); |
||
| 422 | |||
| 423 | $suffix .= (in_array($index_name, self::$fields['stored']) ? 's' : 'u'); |
||
| 424 | |||
| 425 | $suffix .= (in_array($index_name, self::$fields['indexed']) ? 'i' : 'u'); |
||
| 426 | |||
| 427 | $index_name .= '_'.$suffix; |
||
| 428 | |||
| 429 | return $index_name; |
||
| 430 | |||
| 431 | } |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Load indexing configuration |
||
| 435 | * |
||
| 436 | * @access protected |
||
| 437 | * |
||
| 438 | * @param integer $pid: The configuration page's UID |
||
| 439 | * |
||
| 440 | * @return void |
||
| 441 | */ |
||
| 442 | protected static function loadIndexConf($pid) { |
||
| 443 | |||
| 444 | if (!self::$fieldsLoaded) { |
||
| 445 | |||
| 446 | // Get the list of toplevel structures. |
||
| 447 | $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery( |
||
| 448 | 'tx_dlf_structures.index_name AS index_name', |
||
| 449 | 'tx_dlf_structures', |
||
| 450 | 'tx_dlf_structures.toplevel=1 AND tx_dlf_structures.pid='.intval($pid).tx_dlf_helper::whereClause('tx_dlf_structures'), |
||
| 451 | '', |
||
| 452 | '', |
||
| 453 | '' |
||
| 454 | ); |
||
| 455 | |||
| 456 | while ($toplevel = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result)) { |
||
| 457 | |||
| 458 | self::$toplevel[] = $toplevel['index_name']; |
||
| 459 | |||
| 460 | } |
||
| 461 | |||
| 462 | // Get the metadata indexing options. |
||
| 463 | $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery( |
||
| 464 | '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', |
||
| 465 | 'tx_dlf_metadata', |
||
| 466 | 'tx_dlf_metadata.pid='.intval($pid).tx_dlf_helper::whereClause('tx_dlf_metadata'), |
||
| 467 | '', |
||
| 468 | '', |
||
| 469 | '' |
||
| 470 | ); |
||
| 471 | |||
| 472 | while ($indexing = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result)) { |
||
| 473 | |||
| 474 | if ($indexing['index_tokenized']) { |
||
| 475 | |||
| 476 | self::$fields['tokenized'][] = $indexing['index_name']; |
||
| 477 | |||
| 478 | } |
||
| 479 | |||
| 480 | if ($indexing['index_stored'] || $indexing['is_listed']) { |
||
| 481 | |||
| 482 | self::$fields['stored'][] = $indexing['index_name']; |
||
| 483 | |||
| 484 | } |
||
| 485 | |||
| 486 | if ($indexing['index_indexed'] || $indexing['index_autocomplete']) { |
||
| 487 | |||
| 488 | self::$fields['indexed'][] = $indexing['index_name']; |
||
| 489 | |||
| 490 | } |
||
| 491 | |||
| 492 | if ($indexing['is_sortable']) { |
||
| 493 | |||
| 494 | self::$fields['sortables'][] = $indexing['index_name']; |
||
| 495 | |||
| 496 | } |
||
| 497 | |||
| 498 | if ($indexing['is_facet']) { |
||
| 499 | |||
| 500 | self::$fields['facets'][] = $indexing['index_name']; |
||
| 501 | |||
| 502 | } |
||
| 503 | |||
| 504 | if ($indexing['index_autocomplete']) { |
||
| 505 | |||
| 506 | self::$fields['autocomplete'][] = $indexing['index_name']; |
||
| 507 | |||
| 508 | } |
||
| 509 | |||
| 510 | if ($indexing['index_boost'] > 0.0) { |
||
| 511 | |||
| 512 | self::$fields['fieldboost'][$indexing['index_name']] = floatval($indexing['index_boost']); |
||
| 513 | |||
| 514 | } else { |
||
| 515 | |||
| 516 | self::$fields['fieldboost'][$indexing['index_name']] = FALSE; |
||
| 517 | |||
| 518 | } |
||
| 519 | |||
| 520 | } |
||
| 521 | |||
| 522 | self::$fieldsLoaded = TRUE; |
||
| 523 | |||
| 524 | } |
||
| 525 | |||
| 526 | } |
||
| 527 | |||
| 528 | /** |
||
| 529 | * Processes a logical unit (and its children) for the Solr index |
||
| 530 | * |
||
| 531 | * @access protected |
||
| 532 | * |
||
| 533 | * @param tx_dlf_document &$doc: The METS document |
||
| 534 | * @param array $logicalUnit: Array of the logical unit to process |
||
| 535 | * |
||
| 536 | * @return integer 0 on success or 1 on failure |
||
| 537 | */ |
||
| 538 | protected static function processLogical(tx_dlf_document &$doc, array $logicalUnit) { |
||
| 539 | |||
| 540 | $errors = 0; |
||
| 541 | |||
| 542 | // Get metadata for logical unit. |
||
| 543 | $metadata = $doc->metadataArray[$logicalUnit['id']]; |
||
| 544 | |||
| 545 | if (!empty($metadata)) { |
||
| 546 | |||
| 547 | // Load class. |
||
| 548 | if (!class_exists('Apache_Solr_Document')) { |
||
| 549 | |||
| 550 | require_once(\TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName('EXT:'.self::$extKey.'/lib/SolrPhpClient/Apache/Solr/Document.php')); |
||
| 551 | |||
| 552 | } |
||
| 553 | |||
| 554 | // Create new Solr document. |
||
| 555 | $solrDoc = new Apache_Solr_Document(); |
||
| 556 | |||
| 557 | // Create unique identifier from document's UID and unit's XML ID. |
||
| 558 | $solrDoc->setField('id', $doc->uid.$logicalUnit['id']); |
||
| 559 | |||
| 560 | $solrDoc->setField('uid', $doc->uid); |
||
| 561 | |||
| 562 | $solrDoc->setField('pid', $doc->pid); |
||
| 563 | |||
| 564 | if (\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($logicalUnit['points'])) { |
||
| 565 | |||
| 566 | $solrDoc->setField('page', $logicalUnit['points']); |
||
| 567 | |||
| 568 | } |
||
| 569 | |||
| 570 | if ($logicalUnit['id'] == $doc->toplevelId) { |
||
| 571 | |||
| 572 | $solrDoc->setField('thumbnail', $doc->thumbnail); |
||
| 573 | |||
| 574 | } elseif (!empty($logicalUnit['thumbnailId'])) { |
||
| 575 | |||
| 576 | $solrDoc->setField('thumbnail', $doc->getFileLocation($logicalUnit['thumbnailId'])); |
||
| 577 | |||
| 578 | } |
||
| 579 | |||
| 580 | $solrDoc->setField('partof', $doc->parentId); |
||
| 581 | |||
| 582 | $solrDoc->setField('root', $doc->rootId); |
||
| 583 | |||
| 584 | $solrDoc->setField('sid', $logicalUnit['id']); |
||
| 585 | |||
| 586 | $solrDoc->setField('toplevel', in_array($logicalUnit['type'], self::$toplevel)); |
||
| 587 | |||
| 588 | $solrDoc->setField('type', $logicalUnit['type'], self::$fields['fieldboost']['type']); |
||
| 589 | |||
| 590 | $solrDoc->setField('title', $metadata['title'][0], self::$fields['fieldboost']['title']); |
||
| 591 | |||
| 592 | $solrDoc->setField('volume', $metadata['volume'][0], self::$fields['fieldboost']['volume']); |
||
| 593 | |||
| 594 | $solrDoc->setField('record_id', $metadata['record_id'][0]); |
||
| 595 | |||
| 596 | $solrDoc->setField('purl', $metadata['purl'][0]); |
||
| 597 | |||
| 598 | $solrDoc->setField('location', $doc->location); |
||
| 599 | |||
| 600 | $solrDoc->setField('urn', $metadata['urn']); |
||
| 601 | |||
| 602 | $solrDoc->setField('collection', $metadata['collection']); |
||
| 603 | |||
| 604 | $autocomplete = array (); |
||
| 605 | |||
| 606 | foreach ($metadata as $index_name => $data) { |
||
| 607 | |||
| 608 | if (!empty($data) && substr($index_name, -8) !== '_sorting') { |
||
| 609 | |||
| 610 | $solrDoc->setField(self::getIndexFieldName($index_name, $doc->pid), $data, self::$fields['fieldboost'][$index_name]); |
||
| 611 | |||
| 612 | if (in_array($index_name, self::$fields['sortables'])) { |
||
| 613 | |||
| 614 | // Add sortable fields to index. |
||
| 615 | $solrDoc->setField($index_name.'_sorting', $metadata[$index_name.'_sorting'][0]); |
||
| 616 | |||
| 617 | } |
||
| 618 | |||
| 619 | if (in_array($index_name, self::$fields['facets'])) { |
||
| 620 | |||
| 621 | // Add facets to index. |
||
| 622 | $solrDoc->setField($index_name.'_faceting', $data); |
||
| 623 | |||
| 624 | } |
||
| 625 | |||
| 626 | if (in_array($index_name, self::$fields['autocomplete'])) { |
||
| 627 | |||
| 628 | $autocomplete = array_merge($autocomplete, $data); |
||
| 629 | |||
| 630 | } |
||
| 631 | |||
| 632 | } |
||
| 633 | |||
| 634 | } |
||
| 635 | |||
| 636 | // Add autocomplete values to index. |
||
| 637 | if (!empty($autocomplete)) { |
||
| 638 | |||
| 639 | $solrDoc->setField('autocomplete', $autocomplete); |
||
| 640 | |||
| 641 | } |
||
| 642 | |||
| 643 | // Add collection information to logical sub-elements if applicable. |
||
| 644 | if (in_array('collection', self::$fields['facets']) |
||
| 645 | && empty($metadata['collection']) |
||
| 646 | && !empty($doc->metadataArray[$doc->toplevelId]['collection'])) { |
||
| 647 | |||
| 648 | $solrDoc->setField('collection_faceting', $doc->metadataArray[$doc->toplevelId]['collection']); |
||
| 649 | |||
| 650 | } |
||
| 651 | |||
| 652 | try { |
||
| 653 | |||
| 654 | self::$solr->service->addDocument($solrDoc); |
||
| 655 | |||
| 656 | } catch (Exception $e) { |
||
| 657 | |||
| 658 | if (!defined('TYPO3_cliMode')) { |
||
| 659 | |||
| 660 | $message = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance( |
||
| 661 | 'TYPO3\\CMS\\Core\\Messaging\\FlashMessage', |
||
| 662 | tx_dlf_helper::getLL('flash.solrException', TRUE).'<br />'.htmlspecialchars($e->getMessage()), |
||
| 663 | tx_dlf_helper::getLL('flash.error', TRUE), |
||
| 664 | \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR, |
||
| 665 | TRUE |
||
| 666 | ); |
||
| 667 | |||
| 668 | tx_dlf_helper::addMessage($message); |
||
| 669 | |||
| 670 | } |
||
| 671 | |||
| 672 | return 1; |
||
| 673 | |||
| 674 | } |
||
| 675 | |||
| 676 | } |
||
| 677 | |||
| 678 | // Check for child elements... |
||
| 679 | if (!empty($logicalUnit['children'])) { |
||
| 680 | |||
| 681 | foreach ($logicalUnit['children'] as $child) { |
||
| 682 | |||
| 683 | if (!$errors) { |
||
| 684 | |||
| 685 | // ...and process them, too. |
||
| 686 | $errors = self::processLogical($doc, $child); |
||
| 687 | |||
| 688 | } else { |
||
| 689 | |||
| 690 | break; |
||
| 691 | |||
| 692 | } |
||
| 693 | |||
| 694 | } |
||
| 695 | |||
| 696 | } |
||
| 697 | |||
| 698 | return $errors; |
||
| 699 | |||
| 700 | } |
||
| 701 | |||
| 702 | /** |
||
| 703 | * Processes a physical unit for the Solr index |
||
| 704 | * |
||
| 705 | * @access protected |
||
| 706 | * |
||
| 707 | * @param tx_dlf_document &$doc: The METS document |
||
| 708 | * @param integer $page: The page number |
||
| 709 | * @param array $physicalUnit: Array of the physical unit to process |
||
| 710 | * |
||
| 711 | * @return integer 0 on success or 1 on failure |
||
| 712 | */ |
||
| 713 | protected static function processPhysical(tx_dlf_document &$doc, $page, array $physicalUnit) { |
||
| 841 | |||
| 842 | } |
||
| 843 | |||
| 844 | /** |
||
| 845 | * Connects to Solr server. |
||
| 846 | * |
||
| 847 | * @access protected |
||
| 848 | * |
||
| 849 | * @param integer $core: UID of the Solr core |
||
| 850 | * @param integer $pid: UID of the configuration page |
||
| 851 | * |
||
| 852 | * @return boolean TRUE on success or FALSE on failure |
||
| 853 | */ |
||
| 854 | protected static function solrConnect($core, $pid = 0) { |
||
| 878 | |||
| 879 | } |
||
| 880 | |||
| 881 | /** |
||
| 882 | * This is a static class, thus no instances should be created |
||
| 883 | * |
||
| 884 | * @access private |
||
| 885 | */ |
||
| 886 | private function __construct() {} |
||
| 887 | |||
| 888 | } |
||
| 889 |