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
Branch master (f6821b)
by Sebastian
03:23
created

IndexCommand::getSolrCores()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 15
c 1
b 0
f 1
dl 0
loc 24
rs 9.7666
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Kitodo\Dlf\Command;
4
5
/**
6
 * (c) Kitodo. Key to digital objects e.V. <[email protected]>
7
 *
8
 * This file is part of the Kitodo and TYPO3 projects.
9
 *
10
 * @license GNU General Public License version 3 or later.
11
 * For the full copyright and license information, please read the
12
 * LICENSE.txt file that was distributed with this source code.
13
 */
14
15
use Symfony\Component\Console\Command\Command;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Input\InputOption;
18
use Symfony\Component\Console\Output\OutputInterface;
19
use Symfony\Component\Console\Style\SymfonyStyle;
20
use TYPO3\CMS\Core\Core\Bootstrap;
21
use TYPO3\CMS\Core\Utility\GeneralUtility;
22
use TYPO3\CMS\Core\Utility\MathUtility;
23
use TYPO3\CMS\Core\Database\ConnectionPool;
24
use Kitodo\Dlf\Common\Document;
25
26
/**
27
 * Index single document into database and Solr.
28
 */
29
class IndexCommand extends Command
30
{
31
    /**
32
     * Configure the command by defining the name, options and arguments
33
     */
34
    public function configure()
35
    {
36
        $this
37
            ->setDescription('Index single document into database and Solr.')
38
            ->setHelp('')
39
            ->addOption(
40
                'dry-run',
41
                NULL,
42
                InputOption::VALUE_NONE,
43
                'If this option is set, the files will not actually be processed but the location URI is shown.'
44
            )
45
            ->addOption(
46
                'doc',
47
                'd',
48
                InputOption::VALUE_REQUIRED,
49
                'UID or URL of the document.'
50
            )
51
            ->addOption(
52
                'pid',
53
                'p',
54
                InputOption::VALUE_REQUIRED,
55
                'UID of the page the document should be added to.'
56
            )
57
            ->addOption(
58
                'solr',
59
                's',
60
                InputOption::VALUE_REQUIRED,
61
                '[UID|index_name] of the Solr core the document should be added to.'
62
            );
63
    }
64
65
    /**
66
     * Executes the command to index the given document to db and solr.
67
     *
68
     * @param InputInterface $input
69
     * @param OutputInterface $output
70
     */
71
    protected function execute(InputInterface $input, OutputInterface $output)
72
    {
73
        // Make sure the _cli_ user is loaded
74
        Bootstrap::getInstance()->initializeBackendAuthentication();
75
76
        $dryRun = $input->getOption('dry-run') != FALSE ? TRUE : FALSE;
77
78
        $io = new SymfonyStyle($input, $output);
79
        $io->title($this->getDescription());
80
81
        $startingPoint = 0;
82
        if (MathUtility::canBeInterpretedAsInteger($input->getOption('pid'))) {
83
            $startingPoint = MathUtility::forceIntegerInRange((int) $input->getOption('pid'), 0);
84
        }
85
        if ($startingPoint == 0) {
86
            $io->error('ERROR: No valid PID (' . $startingPoint . ') given.');
87
            exit(1);
88
        }
89
90
        if (
91
            !empty($input->getOption('solr'))
92
            && !is_array($input->getOption('solr'))
93
        ) {
94
            $allSolrCores = $this->getSolrCores($startingPoint);
95
            if (MathUtility::canBeInterpretedAsInteger($input->getOption('solr'))) {
96
                $solrCoreUid = MathUtility::forceIntegerInRange((int) $input->getOption('solr'), 0);
97
            } else {
98
                $solrCoreUid = $allSolrCores[$input->getOption('solr')];
99
            }
100
            // Abort if solrCoreUid is empty or not in the array of allowed solr cores.
101
            if (empty($solrCoreUid) || !in_array($solrCoreUid, $allSolrCores)) {
102
                $output_solrCores = [];
103
                foreach ($allSolrCores as $index_name => $uid) {
104
                    $output_solrCores[] = $uid . ' : ' . $index_name;
105
                }
106
                if (empty($output_solrCores)) {
107
                    $io->error('ERROR: No valid Solr core ("' . $input->getOption('solr') . '") given. No valid cores found on PID ' . $startingPoint . ".\n");
108
                    exit(1);
109
                } else {
110
                    $io->error('ERROR: No valid Solr core ("' . $input->getOption('solr') . '") given. ' . "Valid cores are (<uid>:<index_name>):\n" . implode("\n", $output_solrCores) . "\n");
111
                    exit(1);
112
                }
113
            }
114
        } else {
115
            $io->error('ERROR: Required parameter --solr|-s is missing or array.');
116
            exit(1);
117
        }
118
119
        if (
120
            empty($input->getOption('doc'))
121
            || is_array($input->getOption('doc'))
122
            || (
123
                !MathUtility::canBeInterpretedAsInteger($input->getOption('doc'))
124
                && !GeneralUtility::isValidUrl($input->getOption('doc'))
1 ignored issue
show
Bug introduced by
It seems like $input->getOption('doc') can also be of type string[]; however, parameter $url of TYPO3\CMS\Core\Utility\G...alUtility::isValidUrl() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

124
                && !GeneralUtility::isValidUrl(/** @scrutinizer ignore-type */ $input->getOption('doc'))
Loading history...
125
            )
126
        ) {
127
            $io->error('ERROR: Required parameter --doc|-d is not a valid document UID or URL.');
128
            exit(1);
129
        }
130
131
        // Get the document...
132
        $doc = Document::getInstance($input->getOption('doc'), $startingPoint, TRUE);
133
        if ($doc->ready) {
134
            if ($dryRun) {
135
                $io->section('DRY RUN: Would index ' . $doc->uid . ' ("' . $doc->location . '") on UID ' . $startingPoint . ' and Solr core ' . $solrCoreUid . '.');
136
            } else {
137
                if ($io->isVerbose()) {
138
                    $io->section('Indexing ' . $doc->uid . ' ("' . $doc->location . '") on UID ' . $startingPoint . ' and Solr core ' . $solrCoreUid . '.');
139
                }
140
                // ...and save it to the database...
141
                if (!$doc->save($startingPoint, $solrCoreUid)) {
142
                    $io->error('ERROR: Document "' . $input->getOption('doc') . '" not saved and indexed.');
143
                    exit(1);
144
                }
145
            }
146
        } else {
147
            $io->error('ERROR: Document "' . $input->getOption('doc') . '" could not be loaded.');
148
            exit(1);
149
        }
150
151
        $io->success('All done!');
152
    }
153
154
155
    /**
156
     * Fetches all records of tx_dlf_solrcores on given page.
157
     *
158
     * @param int $pageId the uid of the solr record
159
     *
160
     * @return array array of valid solr cores
161
     */
162
    protected function getSolrCores(int $pageId): array
163
    {
164
        /** @var QueryBuilder $queryBuilder */
165
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
166
            ->getQueryBuilderForTable('tx_dlf_solrcores');
167
168
        $solrCores = [];
169
        $pageId = (int) $pageId;
170
        $result = $queryBuilder
171
            ->select('uid', 'index_name')
172
            ->from('tx_dlf_solrcores')
173
            ->where(
174
                $queryBuilder->expr()->eq(
175
                    'pid',
176
                    $queryBuilder->createNamedParameter($pageId, \PDO::PARAM_INT)
177
                )
178
            )
179
            ->execute();
180
181
        while ($record = $result->fetch()) {
182
            $solrCores[$record['index_name']] = $record['uid'];
183
        }
184
185
        return $solrCores;
186
    }
187
}
188