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:53
created

DocumentRepository::findAllTitles()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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

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