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 (#737)
by
unknown
02:48
created

DocumentRepository::getOaiDocumentList()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 16
c 2
b 0
f 0
nc 1
nop 2
dl 0
loc 26
rs 9.7333
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 TYPO3\CMS\Core\Utility\GeneralUtility;
16
use TYPO3\CMS\Core\Database\ConnectionPool;
17
use Kitodo\Dlf\Common\Helper;
18
use TYPO3\CMS\Core\Database\Connection;
19
use TYPO3\CMS\Extbase\Persistence\QueryInterface;
20
21
class DocumentRepository extends \TYPO3\CMS\Extbase\Persistence\Repository
22
{
23
24
    /**
25
     * Array of all document structures
26
     *
27
     * @var array
28
     */
29
    protected $documentStructures;
30
31
32
    public function findByUidAndPartOf($uid, $partOf)
33
    {
34
        $query = $this->createQuery();
35
36
        $query->matching($query->equals('uid', $uid));
37
        $query->matching($query->equals('partof', $partOf));
38
39
        return $query->execute();
40
    }
41
42
    /**
43
     * Find the oldest document
44
     *
45
     * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
46
     */
47
    public function findOldestDocument()
48
    {
49
        $query = $this->createQuery();
50
51
        $query->setOrderings(['tstamp' => QueryInterface::ORDER_ASCENDING]);
52
        $query->setLimit(1);
53
54
        return $query->execute()->getFirst();
55
    }
56
57
    /**
58
     * @param int $partOf
59
     * @param string $structure
60
     * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
61
     */
62
    public function getChildrenOfYearAnchor($partOf, $structure)
63
    {
64
        $this->documentStructures = $this->getDocumentStructures();
65
66
        $query = $this->createQuery();
67
68
        $query->matching($query->equals('structure', $this->documentStructures[$structure]));
69
        $query->matching($query->equals('partof', $partOf));
70
71
        $query->setOrderings([
72
            'mets_orderlabel' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING
73
        ]);
74
75
        return $query->execute();
76
    }
77
78
    /**
79
     * Finds all documents for the given settings
80
     *
81
     * @param int $uid
82
     * @param array $settings
83
     *
84
     * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
85
     */
86
    public function findOneByIdAndSettings($uid, $settings = [])
87
    {
88
        $settings['documentSets'] = $uid;
89
90
        return $this->findDocumentsBySettings($settings)->getFirst();
91
    }
92
93
    /**
94
     * Finds all documents for the given settings
95
     *
96
     * @param array $settings
97
     *
98
     * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
99
     */
100
    public function findDocumentsBySettings($settings = [])
101
    {
102
        $query = $this->createQuery();
103
104
        $constraints = [];
105
106
        if ($settings['documentSets']) {
107
            $constraints[] = $query->in('uid', GeneralUtility::intExplode(',', $settings['documentSets']));
108
        }
109
110
        if (isset($settings['excludeOther']) && (int) $settings['excludeOther'] === 0) {
111
            $query->getQuerySettings()->setRespectStoragePage(false);
112
        }
113
114
        if (count($constraints)) {
115
            $query->matching(
116
                $query->logicalAnd($constraints)
117
            );
118
        }
119
120
        return $query->execute();
121
    }
122
123
    /**
124
     * Finds all documents for the given collections
125
     *
126
     * @param array $collections separated by comma
127
     * @param int $limit
128
     *
129
     * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
130
     */
131
    public function findAllByCollectionsLimited($collections, $limit = 50)
132
    {
133
        $query = $this->createQuery();
134
135
        // order by start_date -> start_time...
136
        $query->setOrderings(
137
            ['tstamp' => QueryInterface::ORDER_DESCENDING]
138
        );
139
140
        $constraints = [];
141
        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...
142
            $constraints[] = $query->in('collections.uid', $collections);
143
        }
144
145
        if (count($constraints)) {
146
            $query->matching(
147
                $query->logicalAnd($constraints)
148
            );
149
        }
150
        $query->setLimit((int) $limit);
151
152
        return $query->execute();
153
    }
154
155
    /**
156
     * Find all the titles
157
     *
158
     * documents with partof == 0
159
     *
160
     * @param array $settings
161
     *
162
     * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
163
     */
164
    public function findAllTitles($settings = [])
165
    {
166
        $query = $this->createQuery();
167
168
        $constraints = [];
169
        $constraints[] = $query->equals('partof', 0);
170
171
        if ($settings['collections']) {
172
            $constraints[] = $query->in('collections.uid', GeneralUtility::intExplode(',', $settings['collections']));
173
        }
174
175
        if (count($constraints)) {
176
            $query->matching(
177
                $query->logicalAnd($constraints)
178
            );
179
        }
180
181
        return $query->execute();
182
    }
183
184
    /**
185
     * Count the titles
186
     *
187
     * documents with partof == 0
188
     *
189
     * @param array $settings
190
     *
191
     * @return int
192
     */
193
    public function countAllTitles($settings = [])
194
    {
195
        return $this->findAllTitles($settings)->count();
196
    }
197
198
    /**
199
     * Count the volumes
200
     *
201
     * documents with partof != 0
202
     *
203
     * @param array $settings
204
     *
205
     * @return int
206
     */
207
    public function countAllVolumes($settings = [])
208
    {
209
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
210
            ->getQueryBuilderForTable('tx_dlf_documents');
211
        $subQueryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
212
            ->getQueryBuilderForTable('tx_dlf_documents');
213
214
        $subQuery = $subQueryBuilder
215
            ->select('tx_dlf_documents.partof')
216
            ->from('tx_dlf_documents')
217
            ->where(
218
                $subQueryBuilder->expr()->neq('tx_dlf_documents.partof', 0)
219
            )
220
            ->groupBy('tx_dlf_documents.partof')
221
            ->getSQL();
222
223
        $countVolumes = $queryBuilder
224
            ->count('tx_dlf_documents.uid')
225
            ->from('tx_dlf_documents')
226
            ->where(
227
                $queryBuilder->expr()->eq('tx_dlf_documents.pid', intval($settings['pages'])),
228
                $queryBuilder->expr()->notIn('tx_dlf_documents.uid', $subQuery)
229
            )
230
            ->execute()
231
            ->fetchColumn(0);
232
233
        return $countVolumes;
234
    }
235
236
    public function getStatisticsForSelectedCollection($settings)
237
    {
238
        // Include only selected collections.
239
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
240
            ->getQueryBuilderForTable('tx_dlf_documents');
241
242
        $countTitles = $queryBuilder
243
            ->count('tx_dlf_documents.uid')
244
            ->from('tx_dlf_documents')
245
            ->innerJoin(
246
                'tx_dlf_documents',
247
                'tx_dlf_relations',
248
                'tx_dlf_relations_joins',
249
                $queryBuilder->expr()->eq(
250
                    'tx_dlf_relations_joins.uid_local',
251
                    'tx_dlf_documents.uid'
252
                )
253
            )
254
            ->innerJoin(
255
                'tx_dlf_relations_joins',
256
                'tx_dlf_collections',
257
                'tx_dlf_collections_join',
258
                $queryBuilder->expr()->eq(
259
                    'tx_dlf_relations_joins.uid_foreign',
260
                    'tx_dlf_collections_join.uid'
261
                )
262
            )
263
            ->where(
264
                $queryBuilder->expr()->eq('tx_dlf_documents.pid', intval($settings['pages'])),
265
                $queryBuilder->expr()->eq('tx_dlf_collections_join.pid', intval($settings['pages'])),
266
                $queryBuilder->expr()->eq('tx_dlf_documents.partof', 0),
267
                $queryBuilder->expr()->in('tx_dlf_collections_join.uid',
268
                    $queryBuilder->createNamedParameter(GeneralUtility::intExplode(',',
269
                        $settings['collections']), Connection::PARAM_INT_ARRAY)),
270
                $queryBuilder->expr()->eq('tx_dlf_relations_joins.ident',
271
                    $queryBuilder->createNamedParameter('docs_colls'))
272
            )
273
            ->execute()
274
            ->fetchColumn(0);
275
276
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
277
            ->getQueryBuilderForTable('tx_dlf_documents');
278
        $subQueryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
279
            ->getQueryBuilderForTable('tx_dlf_documents');
280
281
        $subQuery = $subQueryBuilder
282
            ->select('tx_dlf_documents.partof')
283
            ->from('tx_dlf_documents')
284
            ->where(
285
                $subQueryBuilder->expr()->neq('tx_dlf_documents.partof', 0)
286
            )
287
            ->groupBy('tx_dlf_documents.partof')
288
            ->getSQL();
289
290
        $countVolumes = $queryBuilder
291
            ->count('tx_dlf_documents.uid')
292
            ->from('tx_dlf_documents')
293
            ->innerJoin(
294
                'tx_dlf_documents',
295
                'tx_dlf_relations',
296
                'tx_dlf_relations_joins',
297
                $queryBuilder->expr()->eq(
298
                    'tx_dlf_relations_joins.uid_local',
299
                    'tx_dlf_documents.uid'
300
                )
301
            )
302
            ->innerJoin(
303
                'tx_dlf_relations_joins',
304
                'tx_dlf_collections',
305
                'tx_dlf_collections_join',
306
                $queryBuilder->expr()->eq(
307
                    'tx_dlf_relations_joins.uid_foreign',
308
                    'tx_dlf_collections_join.uid'
309
                )
310
            )
311
            ->where(
312
                $queryBuilder->expr()->eq('tx_dlf_documents.pid', intval($settings['pages'])),
313
                $queryBuilder->expr()->eq('tx_dlf_collections_join.pid', intval($settings['pages'])),
314
                $queryBuilder->expr()->notIn('tx_dlf_documents.uid', $subQuery),
315
                $queryBuilder->expr()->in('tx_dlf_collections_join.uid',
316
                    $queryBuilder->createNamedParameter(GeneralUtility::intExplode(',',
317
                        $settings['collections']), Connection::PARAM_INT_ARRAY)),
318
                $queryBuilder->expr()->eq('tx_dlf_relations_joins.ident',
319
                    $queryBuilder->createNamedParameter('docs_colls'))
320
            )
321
            ->execute()
322
            ->fetchColumn(0);
323
324
        return ['titles' => $countTitles, 'volumes' => $countVolumes];
325
    }
326
327
    public function getTableOfContentsFromDb($uid, $pid, $settings)
328
    {
329
        // Build table of contents from database.
330
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
331
            ->getQueryBuilderForTable('tx_dlf_documents');
332
333
        $excludeOtherWhere = '';
334
        if ($settings['excludeOther']) {
335
            $excludeOtherWhere = 'tx_dlf_documents.pid=' . intval($settings['pages']);
336
        }
337
        // Check if there are any metadata to suggest.
338
        $result = $queryBuilder
339
            ->select(
340
                'tx_dlf_documents.uid AS uid',
341
                'tx_dlf_documents.title AS title',
342
                'tx_dlf_documents.volume AS volume',
343
                'tx_dlf_documents.mets_label AS mets_label',
344
                'tx_dlf_documents.mets_orderlabel AS mets_orderlabel',
345
                'tx_dlf_structures_join.index_name AS type'
346
            )
347
            ->innerJoin(
348
                'tx_dlf_documents',
349
                'tx_dlf_structures',
350
                'tx_dlf_structures_join',
351
                $queryBuilder->expr()->eq(
352
                    'tx_dlf_structures_join.uid',
353
                    'tx_dlf_documents.structure'
354
                )
355
            )
356
            ->from('tx_dlf_documents')
357
            ->where(
358
                $queryBuilder->expr()->eq('tx_dlf_documents.partof', intval($uid)),
359
                $queryBuilder->expr()->eq('tx_dlf_structures_join.pid', intval($pid)),
360
                $excludeOtherWhere
361
            )
362
            ->addOrderBy('tx_dlf_documents.volume_sorting')
363
            ->addOrderBy('tx_dlf_documents.mets_orderlabel')
364
            ->execute();
365
        return $result;
366
    }
367
368
    /**
369
     * Find one document by given settings and identifier
370
     *
371
     * @param array $settings
372
     * @param array $parameters
373
     *
374
     * @return array The found document object
375
     */
376
    public function getOaiRecord($settings, $parameters)
377
    {
378
        $where = '';
379
380
        if (!$settings['show_userdefined']) {
381
            $where .= 'AND tx_dlf_collections.fe_cruser_id=0 ';
382
        }
383
384
        $connection = GeneralUtility::makeInstance(ConnectionPool::class)
385
            ->getConnectionForTable('tx_dlf_documents');
386
387
        $sql = 'SELECT `tx_dlf_documents`.*, GROUP_CONCAT(DISTINCT `tx_dlf_collections`.`oai_name` ORDER BY `tx_dlf_collections`.`oai_name` SEPARATOR " ") AS `collections` ' .
388
            'FROM `tx_dlf_documents` ' .
389
            'INNER JOIN `tx_dlf_relations` ON `tx_dlf_relations`.`uid_local` = `tx_dlf_documents`.`uid` ' .
390
            'INNER JOIN `tx_dlf_collections` ON `tx_dlf_collections`.`uid` = `tx_dlf_relations`.`uid_foreign` ' .
391
            'WHERE `tx_dlf_documents`.`record_id` = ? ' .
392
            'AND `tx_dlf_relations`.`ident`="docs_colls" ' .
393
            $where;
394
395
        $values = [
396
            $parameters['identifier']
397
        ];
398
399
        $types = [
400
            Connection::PARAM_STR
401
        ];
402
403
        // Create a prepared statement for the passed SQL query, bind the given params with their binding types and execute the query
404
        $statement = $connection->executeQuery($sql, $values, $types);
405
406
        return $statement->fetch();
407
    }
408
409
    /**
410
     * Finds all documents for the given settings
411
     *
412
     * @param array $settings
413
     * @param array $documentsToProcess
414
     *
415
     * @return array The found document objects
416
     */
417
    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

417
    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...
418
    {
419
        $connection = GeneralUtility::makeInstance(ConnectionPool::class)
420
            ->getConnectionForTable('tx_dlf_documents');
421
422
        $sql = 'SELECT `tx_dlf_documents`.*, GROUP_CONCAT(DISTINCT `tx_dlf_collections`.`oai_name` ORDER BY `tx_dlf_collections`.`oai_name` SEPARATOR " ") AS `collections` ' .
423
            'FROM `tx_dlf_documents` ' .
424
            'INNER JOIN `tx_dlf_relations` ON `tx_dlf_relations`.`uid_local` = `tx_dlf_documents`.`uid` ' .
425
            'INNER JOIN `tx_dlf_collections` ON `tx_dlf_collections`.`uid` = `tx_dlf_relations`.`uid_foreign` ' .
426
            'WHERE `tx_dlf_documents`.`uid` IN ( ? ) ' .
427
            'AND `tx_dlf_relations`.`ident`="docs_colls" ' .
428
            'AND ' . Helper::whereExpression('tx_dlf_collections') . ' ' .
429
            'GROUP BY `tx_dlf_documents`.`uid` ';
430
431
        $values = [
432
            $documentsToProcess,
433
        ];
434
435
        $types = [
436
            Connection::PARAM_INT_ARRAY,
437
        ];
438
439
        // Create a prepared statement for the passed SQL query, bind the given params with their binding types and execute the query
440
        $documents = $connection->executeQuery($sql, $values, $types);
441
442
        return $documents;
443
    }
444
445
    /**
446
     * Get all document structures as array
447
     *
448
     * @return array
449
     */
450
    private function getDocumentStructures()
451
    {
452
        // make lookup-table of structures uid -> indexName
453
        $connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
454
        $queryBuilder = $connectionPool->getQueryBuilderForTable('tx_dlf_structures');
455
        // Fetch document info for UIDs in $documentSet from DB
456
        $kitodoStructures = $queryBuilder
457
            ->select(
458
                'tx_dlf_structures.uid AS uid',
459
                'tx_dlf_structures.index_name AS indexName'
460
            )
461
            ->from('tx_dlf_structures')
462
            ->execute();
463
464
        $allStructures = $kitodoStructures->fetchAll();
465
        // make lookup-table uid -> indexName
466
        $allStructures = array_column($allStructures, 'indexName', 'uid');
467
468
        return $allStructures;
469
    }
470
}
471