We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 97 | 
| Total Lines | 824 | 
| Duplicated Lines | 0 % | 
| Changes | 9 | ||
| Bugs | 0 | Features | 0 | 
Complex classes like DocumentRepository 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 DocumentRepository, and based on these observations, apply Extract Interface, too.
| 1 | <?php  | 
            ||
| 27 | class DocumentRepository extends \TYPO3\CMS\Extbase\Persistence\Repository  | 
            ||
| 28 | { | 
            ||
| 29 | /**  | 
            ||
| 30 | * The controller settings passed to the repository for some special actions.  | 
            ||
| 31 | *  | 
            ||
| 32 | * @var array  | 
            ||
| 33 | * @access protected  | 
            ||
| 34 | */  | 
            ||
| 35 | protected $settings;  | 
            ||
| 36 | |||
| 37 | /**  | 
            ||
| 38 | * Find one document by given parameters  | 
            ||
| 39 | *  | 
            ||
| 40 | * GET parameters may be:  | 
            ||
| 41 | *  | 
            ||
| 42 | * - 'id': the uid of the document  | 
            ||
| 43 | * - 'location': the URL of the location of the XML file  | 
            ||
| 44 | * - 'recordId': the record_id of the document  | 
            ||
| 45 | *  | 
            ||
| 46 | * @param array $parameters  | 
            ||
| 47 | *  | 
            ||
| 48 | * @return \Kitodo\Dlf\Domain\Model\Document|null  | 
            ||
| 49 | */  | 
            ||
| 50 | public function findOneByParameters($parameters)  | 
            ||
| 51 |     { | 
            ||
| 52 | $doc = null;  | 
            ||
| 53 | $document = null;  | 
            ||
| 54 | |||
| 55 |         if (isset($parameters['id']) && MathUtility::canBeInterpretedAsInteger($parameters['id'])) { | 
            ||
| 56 | |||
| 57 | $document = $this->findOneByIdAndSettings($parameters['id']);  | 
            ||
| 58 | |||
| 59 |         } else if (isset($parameters['recordId'])) { | 
            ||
| 60 | |||
| 61 | $document = $this->findOneByRecordId($parameters['recordId']);  | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 62 | |||
| 63 |         } else if (isset($parameters['location']) && GeneralUtility::isValidUrl($parameters['location'])) { | 
            ||
| 64 | |||
| 65 | $doc = Doc::getInstance($parameters['location'], [], true);  | 
            ||
| 66 | |||
| 67 |             if ($doc->recordId) { | 
            ||
| 68 | $document = $this->findOneByRecordId($doc->recordId);  | 
            ||
| 69 | }  | 
            ||
| 70 | |||
| 71 |             if ($document === null) { | 
            ||
| 72 | // create new (dummy) Document object  | 
            ||
| 73 | $document = GeneralUtility::makeInstance(Document::class);  | 
            ||
| 74 | $document->setLocation($parameters['location']);  | 
            ||
| 75 | }  | 
            ||
| 76 | |||
| 77 | }  | 
            ||
| 78 | |||
| 79 |         if ($document !== null && $doc === null) { | 
            ||
| 80 | $doc = Doc::getInstance($document->getLocation(), [], true);  | 
            ||
| 81 | }  | 
            ||
| 82 | |||
| 83 |         if ($doc !== null) { | 
            ||
| 84 | $document->setDoc($doc);  | 
            ||
| 85 | }  | 
            ||
| 86 | |||
| 87 | return $document;  | 
            ||
| 88 | |||
| 89 | }  | 
            ||
| 90 | |||
| 91 | |||
| 92 | public function findByUidAndPartOf($uid, $partOf)  | 
            ||
| 93 |     { | 
            ||
| 94 | $query = $this->createQuery();  | 
            ||
| 95 | |||
| 96 |         $query->matching($query->equals('uid', $uid)); | 
            ||
| 97 |         $query->matching($query->equals('partof', $partOf)); | 
            ||
| 98 | |||
| 99 | return $query->execute();  | 
            ||
| 100 | }  | 
            ||
| 101 | |||
| 102 | /**  | 
            ||
| 103 | * Find the oldest document  | 
            ||
| 104 | *  | 
            ||
| 105 | * @return \Kitodo\Dlf\Domain\Model\Document|null  | 
            ||
| 106 | */  | 
            ||
| 107 | public function findOldestDocument()  | 
            ||
| 115 | }  | 
            ||
| 116 | |||
| 117 | /**  | 
            ||
| 118 | * @param int $partOf  | 
            ||
| 119 | * @param string $structure  | 
            ||
| 120 | * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface  | 
            ||
| 121 | */  | 
            ||
| 122 | public function getChildrenOfYearAnchor($partOf, $structure)  | 
            ||
| 123 |     { | 
            ||
| 124 | $query = $this->createQuery();  | 
            ||
| 125 | |||
| 126 |         $query->matching($query->equals('structure', Helper::getUidFromIndexName($structure, 'tx_dlf_structures'))); | 
            ||
| 127 |         $query->matching($query->equals('partof', $partOf)); | 
            ||
| 128 | |||
| 129 | $query->setOrderings([  | 
            ||
| 130 | 'mets_orderlabel' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING  | 
            ||
| 131 | ]);  | 
            ||
| 132 | |||
| 133 | return $query->execute();  | 
            ||
| 134 | }  | 
            ||
| 135 | |||
| 136 | /**  | 
            ||
| 137 | * Finds all documents for the given settings  | 
            ||
| 138 | *  | 
            ||
| 139 | * @param int $uid  | 
            ||
| 140 | * @param array $settings  | 
            ||
| 141 | *  | 
            ||
| 142 | * @return \Kitodo\Dlf\Domain\Model\Document|null  | 
            ||
| 143 | */  | 
            ||
| 144 | public function findOneByIdAndSettings($uid, $settings = [])  | 
            ||
| 149 | }  | 
            ||
| 150 | |||
| 151 | /**  | 
            ||
| 152 | * Finds all documents for the given settings  | 
            ||
| 153 | *  | 
            ||
| 154 | * @param array $settings  | 
            ||
| 155 | *  | 
            ||
| 156 | * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface  | 
            ||
| 157 | */  | 
            ||
| 158 | public function findDocumentsBySettings($settings = [])  | 
            ||
| 159 |     { | 
            ||
| 160 | $query = $this->createQuery();  | 
            ||
| 161 | |||
| 162 | $constraints = [];  | 
            ||
| 163 | |||
| 164 |         if ($settings['documentSets']) { | 
            ||
| 165 |             $constraints[] = $query->in('uid', GeneralUtility::intExplode(',', $settings['documentSets'])); | 
            ||
| 166 | }  | 
            ||
| 167 | |||
| 168 |         if (isset($settings['excludeOther']) && (int) $settings['excludeOther'] === 0) { | 
            ||
| 169 | $query->getQuerySettings()->setRespectStoragePage(false);  | 
            ||
| 170 | }  | 
            ||
| 171 | |||
| 172 |         if (count($constraints)) { | 
            ||
| 173 | $query->matching(  | 
            ||
| 174 | $query->logicalAnd($constraints)  | 
            ||
| 175 | );  | 
            ||
| 176 | }  | 
            ||
| 177 | |||
| 178 | return $query->execute();  | 
            ||
| 179 | }  | 
            ||
| 180 | |||
| 181 | /**  | 
            ||
| 182 | * Finds all documents for the given collections  | 
            ||
| 183 | *  | 
            ||
| 184 | * @param array $collections  | 
            ||
| 185 | * @param int $limit  | 
            ||
| 186 | *  | 
            ||
| 187 | * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface  | 
            ||
| 188 | */  | 
            ||
| 189 | public function findAllByCollectionsLimited($collections, $limit = 50)  | 
            ||
| 214 | }  | 
            ||
| 215 | |||
| 216 | /**  | 
            ||
| 217 | * Find all the titles  | 
            ||
| 218 | *  | 
            ||
| 219 | * documents with partof == 0  | 
            ||
| 220 | *  | 
            ||
| 221 | * @param array $settings  | 
            ||
| 222 | *  | 
            ||
| 223 | * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface  | 
            ||
| 224 | */  | 
            ||
| 225 | public function findAllTitles($settings = [])  | 
            ||
| 226 |     { | 
            ||
| 227 | $query = $this->createQuery();  | 
            ||
| 228 | |||
| 229 | $constraints = [];  | 
            ||
| 230 |         $constraints[] = $query->equals('partof', 0); | 
            ||
| 231 | |||
| 232 |         if ($settings['collections']) { | 
            ||
| 233 |             $constraints[] = $query->in('collections.uid', GeneralUtility::intExplode(',', $settings['collections'])); | 
            ||
| 234 | }  | 
            ||
| 235 | |||
| 236 |         if (count($constraints)) { | 
            ||
| 237 | $query->matching(  | 
            ||
| 238 | $query->logicalAnd($constraints)  | 
            ||
| 239 | );  | 
            ||
| 240 | }  | 
            ||
| 241 | |||
| 242 | return $query->execute();  | 
            ||
| 243 | }  | 
            ||
| 244 | |||
| 245 | /**  | 
            ||
| 246 | * Count the titles  | 
            ||
| 247 | *  | 
            ||
| 248 | * documents with partof == 0  | 
            ||
| 249 | *  | 
            ||
| 250 | * @param array $settings  | 
            ||
| 251 | *  | 
            ||
| 252 | * @return int  | 
            ||
| 253 | */  | 
            ||
| 254 | public function countAllTitles($settings = [])  | 
            ||
| 255 |     { | 
            ||
| 256 | return $this->findAllTitles($settings)->count();  | 
            ||
| 257 | }  | 
            ||
| 258 | |||
| 259 | /**  | 
            ||
| 260 | * Count the volumes  | 
            ||
| 261 | *  | 
            ||
| 262 | * documents with partof != 0  | 
            ||
| 263 | *  | 
            ||
| 264 | * @param array $settings  | 
            ||
| 265 | *  | 
            ||
| 266 | * @return int  | 
            ||
| 267 | */  | 
            ||
| 268 | public function countAllVolumes($settings = [])  | 
            ||
| 269 |     { | 
            ||
| 270 | $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)  | 
            ||
| 271 |             ->getQueryBuilderForTable('tx_dlf_documents'); | 
            ||
| 272 | $subQueryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)  | 
            ||
| 273 |             ->getQueryBuilderForTable('tx_dlf_documents'); | 
            ||
| 274 | |||
| 275 | $subQuery = $subQueryBuilder  | 
            ||
| 276 |             ->select('tx_dlf_documents.partof') | 
            ||
| 277 |             ->from('tx_dlf_documents') | 
            ||
| 278 | ->where(  | 
            ||
| 279 |                 $subQueryBuilder->expr()->neq('tx_dlf_documents.partof', 0) | 
            ||
| 280 | )  | 
            ||
| 281 |             ->groupBy('tx_dlf_documents.partof') | 
            ||
| 282 | ->getSQL();  | 
            ||
| 283 | |||
| 284 | $countVolumes = $queryBuilder  | 
            ||
| 285 |             ->count('tx_dlf_documents.uid') | 
            ||
| 286 |             ->from('tx_dlf_documents') | 
            ||
| 287 | ->where(  | 
            ||
| 288 |                 $queryBuilder->expr()->eq('tx_dlf_documents.pid', intval($settings['pages'])), | 
            ||
| 289 |                 $queryBuilder->expr()->notIn('tx_dlf_documents.uid', $subQuery) | 
            ||
| 290 | )  | 
            ||
| 291 | ->execute()  | 
            ||
| 292 | ->fetchColumn(0);  | 
            ||
| 293 | |||
| 294 | return $countVolumes;  | 
            ||
| 295 | }  | 
            ||
| 296 | |||
| 297 | public function getStatisticsForSelectedCollection($settings)  | 
            ||
| 298 |     { | 
            ||
| 299 | // Include only selected collections.  | 
            ||
| 300 | $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)  | 
            ||
| 301 |             ->getQueryBuilderForTable('tx_dlf_documents'); | 
            ||
| 302 | |||
| 303 | $countTitles = $queryBuilder  | 
            ||
| 304 |             ->count('tx_dlf_documents.uid') | 
            ||
| 305 |             ->from('tx_dlf_documents') | 
            ||
| 306 | ->innerJoin(  | 
            ||
| 307 | 'tx_dlf_documents',  | 
            ||
| 308 | 'tx_dlf_relations',  | 
            ||
| 309 | 'tx_dlf_relations_joins',  | 
            ||
| 310 | $queryBuilder->expr()->eq(  | 
            ||
| 311 | 'tx_dlf_relations_joins.uid_local',  | 
            ||
| 312 | 'tx_dlf_documents.uid'  | 
            ||
| 313 | )  | 
            ||
| 314 | )  | 
            ||
| 315 | ->innerJoin(  | 
            ||
| 316 | 'tx_dlf_relations_joins',  | 
            ||
| 317 | 'tx_dlf_collections',  | 
            ||
| 318 | 'tx_dlf_collections_join',  | 
            ||
| 319 | $queryBuilder->expr()->eq(  | 
            ||
| 320 | 'tx_dlf_relations_joins.uid_foreign',  | 
            ||
| 321 | 'tx_dlf_collections_join.uid'  | 
            ||
| 322 | )  | 
            ||
| 323 | )  | 
            ||
| 324 | ->where(  | 
            ||
| 325 |                 $queryBuilder->expr()->eq('tx_dlf_documents.pid', intval($settings['pages'])), | 
            ||
| 326 |                 $queryBuilder->expr()->eq('tx_dlf_collections_join.pid', intval($settings['pages'])), | 
            ||
| 327 |                 $queryBuilder->expr()->eq('tx_dlf_documents.partof', 0), | 
            ||
| 328 |                 $queryBuilder->expr()->in('tx_dlf_collections_join.uid', | 
            ||
| 329 |                     $queryBuilder->createNamedParameter(GeneralUtility::intExplode(',', | 
            ||
| 330 | $settings['collections']), Connection::PARAM_INT_ARRAY)),  | 
            ||
| 331 |                 $queryBuilder->expr()->eq('tx_dlf_relations_joins.ident', | 
            ||
| 332 |                     $queryBuilder->createNamedParameter('docs_colls')) | 
            ||
| 333 | )  | 
            ||
| 334 | ->execute()  | 
            ||
| 335 | ->fetchColumn(0);  | 
            ||
| 336 | |||
| 337 | $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)  | 
            ||
| 338 |             ->getQueryBuilderForTable('tx_dlf_documents'); | 
            ||
| 339 | $subQueryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)  | 
            ||
| 340 |             ->getQueryBuilderForTable('tx_dlf_documents'); | 
            ||
| 341 | |||
| 342 | $subQuery = $subQueryBuilder  | 
            ||
| 343 |             ->select('tx_dlf_documents.partof') | 
            ||
| 344 |             ->from('tx_dlf_documents') | 
            ||
| 345 | ->where(  | 
            ||
| 346 |                 $subQueryBuilder->expr()->neq('tx_dlf_documents.partof', 0) | 
            ||
| 347 | )  | 
            ||
| 348 |             ->groupBy('tx_dlf_documents.partof') | 
            ||
| 349 | ->getSQL();  | 
            ||
| 350 | |||
| 351 | $countVolumes = $queryBuilder  | 
            ||
| 352 |             ->count('tx_dlf_documents.uid') | 
            ||
| 353 |             ->from('tx_dlf_documents') | 
            ||
| 354 | ->innerJoin(  | 
            ||
| 355 | 'tx_dlf_documents',  | 
            ||
| 356 | 'tx_dlf_relations',  | 
            ||
| 357 | 'tx_dlf_relations_joins',  | 
            ||
| 358 | $queryBuilder->expr()->eq(  | 
            ||
| 359 | 'tx_dlf_relations_joins.uid_local',  | 
            ||
| 360 | 'tx_dlf_documents.uid'  | 
            ||
| 361 | )  | 
            ||
| 362 | )  | 
            ||
| 363 | ->innerJoin(  | 
            ||
| 364 | 'tx_dlf_relations_joins',  | 
            ||
| 365 | 'tx_dlf_collections',  | 
            ||
| 366 | 'tx_dlf_collections_join',  | 
            ||
| 367 | $queryBuilder->expr()->eq(  | 
            ||
| 368 | 'tx_dlf_relations_joins.uid_foreign',  | 
            ||
| 369 | 'tx_dlf_collections_join.uid'  | 
            ||
| 370 | )  | 
            ||
| 371 | )  | 
            ||
| 372 | ->where(  | 
            ||
| 373 |                 $queryBuilder->expr()->eq('tx_dlf_documents.pid', intval($settings['pages'])), | 
            ||
| 374 |                 $queryBuilder->expr()->eq('tx_dlf_collections_join.pid', intval($settings['pages'])), | 
            ||
| 375 |                 $queryBuilder->expr()->notIn('tx_dlf_documents.uid', $subQuery), | 
            ||
| 376 |                 $queryBuilder->expr()->in('tx_dlf_collections_join.uid', | 
            ||
| 377 |                     $queryBuilder->createNamedParameter(GeneralUtility::intExplode(',', | 
            ||
| 378 | $settings['collections']), Connection::PARAM_INT_ARRAY)),  | 
            ||
| 379 |                 $queryBuilder->expr()->eq('tx_dlf_relations_joins.ident', | 
            ||
| 380 |                     $queryBuilder->createNamedParameter('docs_colls')) | 
            ||
| 381 | )  | 
            ||
| 382 | ->execute()  | 
            ||
| 383 | ->fetchColumn(0);  | 
            ||
| 384 | |||
| 385 | return ['titles' => $countTitles, 'volumes' => $countVolumes];  | 
            ||
| 386 | }  | 
            ||
| 387 | |||
| 388 | public function getTableOfContentsFromDb($uid, $pid, $settings)  | 
            ||
| 389 |     { | 
            ||
| 390 | // Build table of contents from database.  | 
            ||
| 391 | $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)  | 
            ||
| 392 |             ->getQueryBuilderForTable('tx_dlf_documents'); | 
            ||
| 393 | |||
| 394 | $excludeOtherWhere = '';  | 
            ||
| 395 |         if ($settings['excludeOther']) { | 
            ||
| 396 | $excludeOtherWhere = 'tx_dlf_documents.pid=' . intval($settings['pages']);  | 
            ||
| 397 | }  | 
            ||
| 398 | // Check if there are any metadata to suggest.  | 
            ||
| 399 | $result = $queryBuilder  | 
            ||
| 400 | ->select(  | 
            ||
| 401 | 'tx_dlf_documents.uid AS uid',  | 
            ||
| 402 | 'tx_dlf_documents.title AS title',  | 
            ||
| 403 | 'tx_dlf_documents.volume AS volume',  | 
            ||
| 404 | 'tx_dlf_documents.mets_label AS mets_label',  | 
            ||
| 405 | 'tx_dlf_documents.mets_orderlabel AS mets_orderlabel',  | 
            ||
| 406 | 'tx_dlf_structures_join.index_name AS type'  | 
            ||
| 407 | )  | 
            ||
| 408 | ->innerJoin(  | 
            ||
| 409 | 'tx_dlf_documents',  | 
            ||
| 410 | 'tx_dlf_structures',  | 
            ||
| 411 | 'tx_dlf_structures_join',  | 
            ||
| 412 | $queryBuilder->expr()->eq(  | 
            ||
| 413 | 'tx_dlf_structures_join.uid',  | 
            ||
| 414 | 'tx_dlf_documents.structure'  | 
            ||
| 415 | )  | 
            ||
| 416 | )  | 
            ||
| 417 |             ->from('tx_dlf_documents') | 
            ||
| 418 | ->where(  | 
            ||
| 419 |                 $queryBuilder->expr()->eq('tx_dlf_documents.partof', intval($uid)), | 
            ||
| 420 |                 $queryBuilder->expr()->eq('tx_dlf_structures_join.pid', intval($pid)), | 
            ||
| 421 | $excludeOtherWhere  | 
            ||
| 422 | )  | 
            ||
| 423 |             ->addOrderBy('tx_dlf_documents.volume_sorting') | 
            ||
| 424 |             ->addOrderBy('tx_dlf_documents.mets_orderlabel') | 
            ||
| 425 | ->execute();  | 
            ||
| 426 | return $result;  | 
            ||
| 427 | }  | 
            ||
| 428 | |||
| 429 | /**  | 
            ||
| 430 | * Find one document by given settings and identifier  | 
            ||
| 431 | *  | 
            ||
| 432 | * @param array $settings  | 
            ||
| 433 | * @param array $parameters  | 
            ||
| 434 | *  | 
            ||
| 435 | * @return array The found document object  | 
            ||
| 436 | */  | 
            ||
| 437 | public function getOaiRecord($settings, $parameters)  | 
            ||
| 468 | }  | 
            ||
| 469 | |||
| 470 | /**  | 
            ||
| 471 | * Finds all documents for the given settings  | 
            ||
| 472 | *  | 
            ||
| 473 | * @param array $settings  | 
            ||
| 474 | * @param array $documentsToProcess  | 
            ||
| 475 | *  | 
            ||
| 476 | * @return array The found document objects  | 
            ||
| 477 | */  | 
            ||
| 478 | public function getOaiDocumentList($settings, $documentsToProcess)  | 
            ||
| 479 |     { | 
            ||
| 480 | $connection = GeneralUtility::makeInstance(ConnectionPool::class)  | 
            ||
| 481 |             ->getConnectionForTable('tx_dlf_documents'); | 
            ||
| 482 | |||
| 483 | $sql = 'SELECT `tx_dlf_documents`.*, GROUP_CONCAT(DISTINCT `tx_dlf_collections`.`oai_name` ORDER BY `tx_dlf_collections`.`oai_name` SEPARATOR " ") AS `collections` ' .  | 
            ||
| 484 | 'FROM `tx_dlf_documents` ' .  | 
            ||
| 485 | 'INNER JOIN `tx_dlf_relations` ON `tx_dlf_relations`.`uid_local` = `tx_dlf_documents`.`uid` ' .  | 
            ||
| 486 | 'INNER JOIN `tx_dlf_collections` ON `tx_dlf_collections`.`uid` = `tx_dlf_relations`.`uid_foreign` ' .  | 
            ||
| 487 | 'WHERE `tx_dlf_documents`.`uid` IN ( ? ) ' .  | 
            ||
| 488 | 'AND `tx_dlf_relations`.`ident`="docs_colls" ' .  | 
            ||
| 489 |             'AND ' . Helper::whereExpression('tx_dlf_collections') . ' ' . | 
            ||
| 490 | 'GROUP BY `tx_dlf_documents`.`uid` ';  | 
            ||
| 491 | |||
| 492 | $values = [  | 
            ||
| 493 | $documentsToProcess,  | 
            ||
| 494 | ];  | 
            ||
| 495 | |||
| 496 | $types = [  | 
            ||
| 497 | Connection::PARAM_INT_ARRAY,  | 
            ||
| 498 | ];  | 
            ||
| 499 | |||
| 500 | // Create a prepared statement for the passed SQL query, bind the given params with their binding types and execute the query  | 
            ||
| 501 | $documents = $connection->executeQuery($sql, $values, $types);  | 
            ||
| 502 | |||
| 503 | return $documents;  | 
            ||
| 504 | }  | 
            ||
| 505 | |||
| 506 | /**  | 
            ||
| 507 | * Finds all documents with given uids  | 
            ||
| 508 | *  | 
            ||
| 509 | * @param array $uids  | 
            ||
| 510 | *  | 
            ||
| 511 | * @return array  | 
            ||
| 512 | */  | 
            ||
| 513 | private function findAllByUids($uids)  | 
            ||
| 547 | }  | 
            ||
| 548 | |||
| 549 | /**  | 
            ||
| 550 | * Find all documents with given collection from Solr  | 
            ||
| 551 | *  | 
            ||
| 552 | * @param \TYPO3\CMS\Extbase\Persistence\Generic\QueryResult $collections  | 
            ||
| 553 | * @param array $settings  | 
            ||
| 554 | * @param array $searchParams  | 
            ||
| 555 | * @param \TYPO3\CMS\Extbase\Persistence\Generic\QueryResult $listedMetadata  | 
            ||
| 556 | * @return array  | 
            ||
| 557 | */  | 
            ||
| 558 | public function findSolrByCollection($collections, $settings, $searchParams, $listedMetadata = null)  | 
            ||
| 559 |     { | 
            ||
| 560 | // set settings global inside this repository  | 
            ||
| 561 | $this->settings = $settings;  | 
            ||
| 562 | |||
| 563 | // Prepare query parameters.  | 
            ||
| 564 | $params = [];  | 
            ||
| 565 | $matches = [];  | 
            ||
| 566 | // Set search query.  | 
            ||
| 567 | if (  | 
            ||
| 568 | (!empty($searchParams['fulltext']))  | 
            ||
| 569 |             || preg_match('/fulltext:\((.*)\)/', trim($searchParams['query']), $matches) | 
            ||
| 570 |         ) { | 
            ||
| 571 | // If the query already is a fulltext query e.g using the facets  | 
            ||
| 572 | $searchParams['query'] = empty($matches[1]) ? $searchParams['query'] : $matches[1];  | 
            ||
| 573 | // Search in fulltext field if applicable. Query must not be empty!  | 
            ||
| 574 |             if (!empty($searchParams['query'])) { | 
            ||
| 575 |                 $query = 'fulltext:(' . Solr::escapeQuery(trim($searchParams['query'])) . ')'; | 
            ||
| 576 | }  | 
            ||
| 577 | $params['fulltext'] = true;  | 
            ||
| 578 | |||
| 579 |         } else { | 
            ||
| 580 | // Retain given search field if valid.  | 
            ||
| 581 | $query = Solr::escapeQueryKeepField(trim($searchParams['query']), $settings['storagePid']);  | 
            ||
| 582 | }  | 
            ||
| 583 | |||
| 584 | // Set some query parameters.  | 
            ||
| 585 | $params['query'] = !empty($query) ? $query : '*';  | 
            ||
| 586 | $params['start'] = 0;  | 
            ||
| 587 | $params['rows'] = 10000;  | 
            ||
| 588 | |||
| 589 | // order the results as given or by title as default  | 
            ||
| 590 |         if (!empty($searchParams['orderBy'])) { | 
            ||
| 591 | $querySort = [  | 
            ||
| 592 | $searchParams['orderBy'] => $searchParams['order']  | 
            ||
| 593 | ];  | 
            ||
| 594 |         } else { | 
            ||
| 595 | $querySort = [  | 
            ||
| 596 | 'year_sorting' => 'asc',  | 
            ||
| 597 | 'title_sorting' => 'asc'  | 
            ||
| 598 | ];  | 
            ||
| 599 | }  | 
            ||
| 600 | |||
| 601 | $params['sort'] = $querySort;  | 
            ||
| 602 | $params['listMetadataRecords'] = [];  | 
            ||
| 603 | |||
| 604 | // Restrict the fields to the required ones.  | 
            ||
| 605 | $params['fields'] = 'uid,id,page,title,thumbnail,partof,toplevel,type';  | 
            ||
| 606 | |||
| 607 |         if ($listedMetadata) { | 
            ||
| 608 |             foreach ($listedMetadata as $metadata) { | 
            ||
| 609 |                 if ($metadata->getIndexStored() || $metadata->getIndexIndexed()) { | 
            ||
| 610 | $listMetadataRecord = $metadata->getIndexName() . '_' . ($metadata->getIndexTokenized() ? 't' : 'u') . ($metadata->getIndexStored() ? 's' : 'u') . ($metadata->getIndexIndexed() ? 'i' : 'u');  | 
            ||
| 611 | $params['fields'] .= ',' . $listMetadataRecord;  | 
            ||
| 612 | $params['listMetadataRecords'][$metadata->getIndexName()] = $listMetadataRecord;  | 
            ||
| 613 | }  | 
            ||
| 614 | }  | 
            ||
| 615 | }  | 
            ||
| 616 | |||
| 617 | // Perform search.  | 
            ||
| 618 | $result = $this->searchSolr($params, true);  | 
            ||
| 619 | |||
| 620 | // Initialize values  | 
            ||
| 621 | $numberOfToplevels = 0;  | 
            ||
| 622 | $documents = [];  | 
            ||
| 623 | |||
| 624 |         if ($result['numFound'] > 0) { | 
            ||
| 625 | // flat array with uids from Solr search  | 
            ||
| 626 | $documentSet = array_unique(array_column($result['documents'], 'uid'));  | 
            ||
| 627 | |||
| 628 |             if (empty($documentSet)) { | 
            ||
| 629 | // return nothing found  | 
            ||
| 630 | return ['solrResults' => [], 'documents' => []];  | 
            ||
| 631 | }  | 
            ||
| 632 | |||
| 633 | // get the Extbase document objects for all uids  | 
            ||
| 634 | $allDocuments = $this->findAllByUids($documentSet);  | 
            ||
| 635 | $children = $this->findByPartof($documentSet);  | 
            ||
| 636 | |||
| 637 |             foreach ($result['documents'] as $doc) { | 
            ||
| 638 |                 if (empty($documents[$doc['uid']]) && $allDocuments[$doc['uid']]) { | 
            ||
| 639 | $documents[$doc['uid']] = $allDocuments[$doc['uid']];  | 
            ||
| 640 | }  | 
            ||
| 641 |                 if ($documents[$doc['uid']]) { | 
            ||
| 642 |                     if ($doc['toplevel'] === false) { | 
            ||
| 643 | // this maybe a chapter, article, ..., year  | 
            ||
| 644 |                         if ($doc['type'] === 'year') { | 
            ||
| 645 | continue;  | 
            ||
| 646 | }  | 
            ||
| 647 |                         if (!empty($doc['page'])) { | 
            ||
| 648 | // it's probably a fulltext or metadata search  | 
            ||
| 649 | $searchResult = [];  | 
            ||
| 650 | $searchResult['page'] = $doc['page'];  | 
            ||
| 651 | $searchResult['thumbnail'] = $doc['thumbnail'];  | 
            ||
| 652 | $searchResult['structure'] = $doc['type'];  | 
            ||
| 653 | $searchResult['title'] = $doc['title'];  | 
            ||
| 654 |                             foreach ($params['listMetadataRecords'] as $indexName => $solrField) { | 
            ||
| 655 |                                 if (isset($doc['metadata'][$indexName])) { | 
            ||
| 656 | $documents[$doc['uid']]['metadata'][$indexName] = $doc['metadata'][$indexName];  | 
            ||
| 657 | }  | 
            ||
| 658 | }  | 
            ||
| 659 |                             if ($searchParams['fulltext'] == '1') { | 
            ||
| 660 | $searchResult['snippet'] = $doc['snippet'];  | 
            ||
| 661 | $searchResult['highlight'] = $doc['highlight'];  | 
            ||
| 662 | $searchResult['highlight_word'] = $searchParams['query'];  | 
            ||
| 663 | }  | 
            ||
| 664 | $documents[$doc['uid']]['searchResults'][] = $searchResult;  | 
            ||
| 665 | }  | 
            ||
| 666 |                     } else if ($doc['toplevel'] === true) { | 
            ||
| 667 | $numberOfToplevels++;  | 
            ||
| 668 |                         foreach ($params['listMetadataRecords'] as $indexName => $solrField) { | 
            ||
| 669 |                             if (isset($doc['metadata'][$indexName])) { | 
            ||
| 670 | $documents[$doc['uid']]['metadata'][$indexName] = $doc['metadata'][$indexName];  | 
            ||
| 671 | }  | 
            ||
| 672 | }  | 
            ||
| 673 |                         if ($searchParams['fulltext'] != '1') { | 
            ||
| 674 | $documents[$doc['uid']]['page'] = 1;  | 
            ||
| 675 |                             if (empty($searchParams['query'])) { | 
            ||
| 676 | // find all child documents but not on active search  | 
            ||
| 677 |                                 if (is_array($children[$documents[$doc['uid']]['uid']])) { | 
            ||
| 678 | $documents[$doc['uid']]['children'] = $this->findAllByUids($children[$documents[$doc['uid']]['uid']]);  | 
            ||
| 679 | }  | 
            ||
| 680 | }  | 
            ||
| 681 | }  | 
            ||
| 682 | }  | 
            ||
| 683 |                     if (empty($documents[$doc['uid']]['metadata'])) { | 
            ||
| 684 | $documents[$doc['uid']]['metadata'] = $this->fetchMetadataFromSolr($doc['uid'], $listedMetadata);  | 
            ||
| 685 | }  | 
            ||
| 686 | // get title of parent if empty  | 
            ||
| 687 |                     if (empty($documents[$doc['uid']]['title']) && ($documents[$doc['uid']]['partOf'] > 0)) { | 
            ||
| 688 | $parentDocument = $this->findByUid($documents[$doc['uid']]['partOf']);  | 
            ||
| 689 |                         if ($parentDocument) { | 
            ||
| 690 | $documents[$doc['uid']]['title'] = $parentDocument->getTitle();  | 
            ||
| 691 | }  | 
            ||
| 692 | }  | 
            ||
| 693 | }  | 
            ||
| 694 | }  | 
            ||
| 695 | }  | 
            ||
| 696 | |||
| 697 | return ['solrResults' => $result, 'numberOfToplevels' => $numberOfToplevels, 'documents' => $documents];  | 
            ||
| 698 | }  | 
            ||
| 699 | |||
| 700 | /**  | 
            ||
| 701 | * Find all listed metadata for given document  | 
            ||
| 702 | *  | 
            ||
| 703 | * @param int $uid the uid of the document  | 
            ||
| 704 | * @param \TYPO3\CMS\Extbase\Persistence\Generic\QueryResult $listedMetadata  | 
            ||
| 705 | * @return array  | 
            ||
| 706 | */  | 
            ||
| 707 | protected function fetchMetadataFromSolr($uid, $listedMetadata = [])  | 
            ||
| 745 | }  | 
            ||
| 746 | |||
| 747 | /**  | 
            ||
| 748 | * Processes a search request  | 
            ||
| 749 | *  | 
            ||
| 750 | * @access public  | 
            ||
| 751 | *  | 
            ||
| 752 | * @param array $parameters: Additional search parameters  | 
            ||
| 753 | * @param boolean $enableCache: Enable caching of Solr requests  | 
            ||
| 754 | *  | 
            ||
| 755 | * @return array The Apache Solr Documents that were fetched  | 
            ||
| 756 | */  | 
            ||
| 757 | protected function searchSolr($parameters = [], $enableCache = true)  | 
            ||
| 851 | }  | 
            ||
| 852 | |||
| 854 |