Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Pull Request — dev-extbase-fluid (#754)
by Alexander
02:39
created

DocumentRepository::searchSolr()   C

Complexity

Conditions 16
Paths 140

Size

Total Lines 94
Code Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 16
eloc 54
c 2
b 0
f 0
nc 140
nop 2
dl 0
loc 94
rs 5.2333

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * (c) Kitodo. Key to digital objects e.V. <[email protected]>
5
 *
6
 * This file is part of the Kitodo and TYPO3 projects.
7
 *
8
 * @license GNU General Public License version 3 or later.
9
 * For the full copyright and license information, please read the
10
 * LICENSE.txt file that was distributed with this source code.
11
 */
12
13
namespace Kitodo\Dlf\Domain\Repository;
14
15
use Kitodo\Dlf\Common\Doc;
16
use Kitodo\Dlf\Common\Helper;
17
use Kitodo\Dlf\Common\Solr;
18
use Kitodo\Dlf\Domain\Model\Document;
19
use Kitodo\Dlf\Common\SolrSearchResult\ResultDocument;
20
use TYPO3\CMS\Core\Cache\CacheManager;
21
use TYPO3\CMS\Core\Database\ConnectionPool;
22
use TYPO3\CMS\Core\Database\Connection;
23
use TYPO3\CMS\Core\Utility\GeneralUtility;
24
use TYPO3\CMS\Core\Utility\MathUtility;
25
use TYPO3\CMS\Extbase\Persistence\QueryInterface;
26
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']);
0 ignored issues
show
Bug introduced by
The method findOneByRecordId() does not exist on Kitodo\Dlf\Domain\Repository\DocumentRepository. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

61
            /** @scrutinizer ignore-call */ 
62
            $document = $this->findOneByRecordId($parameters['recordId']);
Loading history...
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);
0 ignored issues
show
Bug introduced by
The method getLocation() does not exist on TYPO3\CMS\Extbase\Persistence\QueryResultInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

80
            $doc = Doc::getInstance($document->/** @scrutinizer ignore-call */ getLocation(), [], true);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
81
        }
82
83
        if ($doc !== null) {
84
            $document->setDoc($doc);
0 ignored issues
show
Bug introduced by
The method setDoc() does not exist on TYPO3\CMS\Extbase\Persistence\QueryResultInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

84
            $document->/** @scrutinizer ignore-call */ 
85
                       setDoc($doc);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
85
        }
86
87
        return $document;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $document also could return the type TYPO3\CMS\Extbase\Persis...y<mixed,object>|integer which is incompatible with the documented return type Kitodo\Dlf\Domain\Model\Document|null.
Loading history...
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()
108
    {
109
        $query = $this->createQuery();
110
111
        $query->setOrderings(['tstamp' => QueryInterface::ORDER_ASCENDING]);
112
        $query->setLimit(1);
113
114
        return $query->execute()->getFirst();
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 = [])
0 ignored issues
show
Unused Code introduced by
The parameter $settings is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

144
    public function findOneByIdAndSettings($uid, /** @scrutinizer ignore-unused */ $settings = [])

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
145
    {
146
        $settings = ['documentSets' => $uid];
147
148
        return $this->findDocumentsBySettings($settings)->getFirst();
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)
190
    {
191
        $query = $this->createQuery();
192
193
        // order by start_date -> start_time...
194
        $query->setOrderings(
195
            ['tstamp' => QueryInterface::ORDER_DESCENDING]
196
        );
197
198
        $constraints = [];
199
        if ($collections) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $collections of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
200
            $constraints[] = $query->in('collections.uid', $collections);
201
        }
202
203
        if (count($constraints)) {
204
            $query->matching(
205
                $query->logicalAnd($constraints)
206
            );
207
        }
208
209
        if ($limit > 0) {
210
            $query->setLimit((int) $limit);
211
        }
212
213
        return $query->execute();
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)
438
    {
439
        $where = '';
440
441
        if (!$settings['show_userdefined']) {
442
            $where .= 'AND tx_dlf_collections.fe_cruser_id=0 ';
443
        }
444
445
        $connection = GeneralUtility::makeInstance(ConnectionPool::class)
446
            ->getConnectionForTable('tx_dlf_documents');
447
448
        $sql = 'SELECT `tx_dlf_documents`.*, GROUP_CONCAT(DISTINCT `tx_dlf_collections`.`oai_name` ORDER BY `tx_dlf_collections`.`oai_name` SEPARATOR " ") AS `collections` ' .
449
            'FROM `tx_dlf_documents` ' .
450
            'INNER JOIN `tx_dlf_relations` ON `tx_dlf_relations`.`uid_local` = `tx_dlf_documents`.`uid` ' .
451
            'INNER JOIN `tx_dlf_collections` ON `tx_dlf_collections`.`uid` = `tx_dlf_relations`.`uid_foreign` ' .
452
            'WHERE `tx_dlf_documents`.`record_id` = ? ' .
453
            'AND `tx_dlf_relations`.`ident`="docs_colls" ' .
454
            $where;
455
456
        $values = [
457
            $parameters['identifier']
458
        ];
459
460
        $types = [
461
            Connection::PARAM_STR
462
        ];
463
464
        // Create a prepared statement for the passed SQL query, bind the given params with their binding types and execute the query
465
        $statement = $connection->executeQuery($sql, $values, $types);
466
467
        return $statement->fetch();
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)
0 ignored issues
show
Unused Code introduced by
The parameter $settings is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

478
    public function getOaiDocumentList(/** @scrutinizer ignore-unused */ $settings, $documentsToProcess)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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)
514
    {
515
        // get all documents from db we are talking about
516
        $connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
517
        $queryBuilder = $connectionPool->getQueryBuilderForTable('tx_dlf_documents');
518
        // Fetch document info for UIDs in $documentSet from DB
519
        $kitodoDocuments = $queryBuilder
520
            ->select(
521
                'tx_dlf_documents.uid AS uid',
522
                'tx_dlf_documents.title AS title',
523
                'tx_dlf_documents.structure AS structure',
524
                'tx_dlf_documents.thumbnail AS thumbnail',
525
                'tx_dlf_documents.volume_sorting AS volumeSorting',
526
                'tx_dlf_documents.mets_orderlabel AS metsOrderlabel',
527
                'tx_dlf_documents.partof AS partOf'
528
            )
529
            ->from('tx_dlf_documents')
530
            ->where(
531
                $queryBuilder->expr()->in('tx_dlf_documents.pid', $this->settings['storagePid']),
532
                $queryBuilder->expr()->in('tx_dlf_documents.uid', $uids)
533
            )
534
            ->addOrderBy('tx_dlf_documents.volume_sorting', 'asc')
535
            ->addOrderBy('tx_dlf_documents.mets_orderlabel', 'asc')
536
            ->execute();
537
538
        $allDocuments = [];
539
        $documentStructures = Helper::getDocumentStructures($this->settings['storagePid']);
540
        // Process documents in a usable array structure
541
        while ($resArray = $kitodoDocuments->fetch()) {
542
            $resArray['structure'] = $documentStructures[$resArray['structure']];
543
            $allDocuments[$resArray['uid']] = $resArray;
544
        }
545
546
        return $allDocuments;
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)
0 ignored issues
show
Unused Code introduced by
The parameter $collections is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

558
    public function findSolrByCollection(/** @scrutinizer ignore-unused */ $collections, $settings, $searchParams, $listedMetadata = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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($settings['fulltext']) && !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);
0 ignored issues
show
Bug introduced by
The method findByPartof() does not exist on Kitodo\Dlf\Domain\Repository\DocumentRepository. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

635
            /** @scrutinizer ignore-call */ 
636
            $children = $this->findByPartof($documentSet);
Loading history...
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 = [])
708
    {
709
        // Prepare query parameters.
710
        $params = [];
711
        $metadataArray = [];
712
713
        // Set some query parameters.
714
        $params['query'] = 'uid:' . $uid;
715
        $params['start'] = 0;
716
        $params['rows'] = 1;
717
        $params['sort'] = ['score' => 'desc'];
718
        $params['listMetadataRecords'] = [];
719
720
        // Restrict the fields to the required ones.
721
        $params['fields'] = 'uid,toplevel';
722
723
        if ($listedMetadata) {
724
            foreach ($listedMetadata as $metadata) {
725
                if ($metadata->getIndexIndexed()) {
726
                    $listMetadataRecord = $metadata->getIndexName() . '_' . ($metadata->getIndexTokenized() ? 't' : 'u') . ($metadata->getIndexStored() ? 's' : 'u') . 'i';
727
                    $params['fields'] .= ',' . $listMetadataRecord;
728
                    $params['listMetadataRecords'][$metadata->getIndexName()] = $listMetadataRecord;
729
                }
730
            }
731
        }
732
        // Set filter query to just get toplevel documents.
733
        $params['filterquery'][] = ['query' => 'toplevel:true'];
734
735
        // Perform search.
736
        $result = $this->searchSolr($params, true);
737
738
        if ($result['numFound'] > 0) {
739
            // There is only one result found because of toplevel:true.
740
            if (isset($result['documents'][0]['metadata'])) {
741
                $metadataArray = $result['documents'][0]['metadata'];
742
            }
743
        }
744
        return $metadataArray;
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)
758
    {
759
        // Set additional query parameters.
760
        $parameters['start'] = 0;
761
        // Set query.
762
        $parameters['query'] = isset($parameters['query']) ? $parameters['query'] : '*';
763
        $parameters['filterquery'] = isset($parameters['filterquery']) ? $parameters['filterquery'] : [];
764
765
        // Perform Solr query.
766
        // Instantiate search object.
767
        $solr = Solr::getInstance($this->settings['solrcore']);
768
        if (!$solr->ready) {
769
            Helper::log('Apache Solr not available', LOG_SEVERITY_ERROR);
770
            return [];
771
        }
772
773
        $cacheIdentifier = '';
774
        $cache = null;
775
        // Calculate cache identifier.
776
        if ($enableCache === true) {
777
            $cacheIdentifier = Helper::digest($solr->core . print_r($parameters, true));
0 ignored issues
show
Bug introduced by
Are you sure print_r($parameters, true) of type string|true can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

777
            $cacheIdentifier = Helper::digest($solr->core . /** @scrutinizer ignore-type */ print_r($parameters, true));
Loading history...
778
            $cache = GeneralUtility::makeInstance(CacheManager::class)->getCache('tx_dlf_solr');
779
        }
780
        $resultSet = [
781
            'documents' => [],
782
            'numFound' => 0,
783
        ];
784
        if ($enableCache === false || ($entry = $cache->get($cacheIdentifier)) === false) {
785
            $selectQuery = $solr->service->createSelect($parameters);
786
787
            if ($parameters['fulltext'] === true) {
788
                // get highlighting component and apply settings
789
                $selectQuery->getHighlighting();
790
            }
791
792
            $solrRequest = $solr->service->createRequest($selectQuery);
793
794
            if ($parameters['fulltext'] === true) {
795
                // If it is a fulltext search, enable highlighting.
796
                // field for which highlighting is going to be performed,
797
                // is required if you want to have OCR highlighting
798
                $solrRequest->addParam('hl.ocr.fl', 'fulltext');
799
                // return the coordinates of highlighted search as absolute coordinates
800
                $solrRequest->addParam('hl.ocr.absoluteHighlights', 'on');
801
                // max amount of snippets for a single page
802
                $solrRequest->addParam('hl.snippets', 20);
803
                // we store the fulltext on page level and can disable this option
804
                $solrRequest->addParam('hl.ocr.trackPages', 'off');
805
            }
806
807
            // Perform search for all documents with the same uid that either fit to the search or marked as toplevel.
808
            $response = $solr->service->executeRequest($solrRequest);
809
            $result = $solr->service->createResult($selectQuery, $response);
810
811
            /** @scrutinizer ignore-call */
812
            $resultSet['numFound'] = $result->getNumFound();
813
            $highlighting = [];
814
            if ($parameters['fulltext'] === true) {
815
                $data = $result->getData();
816
                $highlighting = $data['ocrHighlighting'];
817
            }
818
            $fields = Solr::getFields();
819
820
            foreach ($result as $record) {
821
                $resultDocument = new ResultDocument($record, $highlighting, $fields);
822
823
                $document = [
824
                    'id' => $resultDocument->getId(),
825
                    'page' => $resultDocument->getPage(),
826
                    'snippet' => $resultDocument->getSnippets(),
827
                    'thumbnail' => $resultDocument->getThumbnail(),
828
                    'title' => $resultDocument->getTitle(),
829
                    'toplevel' => $resultDocument->getToplevel(),
830
                    'type' => $resultDocument->getType(),
831
                    'uid' => !empty($resultDocument->getUid()) ? $resultDocument->getUid() : $parameters['uid'],
832
                    'highlight' => $resultDocument->getHighlightsIds(),
833
                ];
834
                foreach ($parameters['listMetadataRecords'] as $indexName => $solrField) {
835
                    if (!empty($record->$solrField)) {
836
                        $document['metadata'][$indexName] = $record->$solrField;
837
                    }
838
                }
839
                $resultSet['documents'][] = $document;
840
            }
841
842
            // Save value in cache.
843
            if (!empty($resultSet) && $enableCache === true) {
844
                $cache->set($cacheIdentifier, $resultSet);
845
            }
846
        } else {
847
            // Return cache hit.
848
            $resultSet = $entry;
849
        }
850
        return $resultSet;
851
    }
852
853
}
854