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
Push — master ( ac3719...d0e528 )
by Sebastian
04:12 queued 11s
created

IndexCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 23
c 1
b 0
f 1
dl 0
loc 28
rs 9.552
cc 1
nc 1
nop 0
1
<?php
2
namespace Kitodo\Dlf\Command;
3
4
/**
5
 * (c) Kitodo. Key to digital objects e.V. <[email protected]>
6
 *
7
 * This file is part of the Kitodo and TYPO3 projects.
8
 *
9
 * @license GNU General Public License version 3 or later.
10
 * For the full copyright and license information, please read the
11
 * LICENSE.txt file that was distributed with this source code.
12
 */
13
use Symfony\Component\Console\Command\Command;
14
use Symfony\Component\Console\Input\InputInterface;
15
use Symfony\Component\Console\Input\InputOption;
16
use Symfony\Component\Console\Output\OutputInterface;
17
use Symfony\Component\Console\Style\SymfonyStyle;
18
use TYPO3\CMS\Core\Core\Bootstrap;
19
use TYPO3\CMS\Core\Utility\GeneralUtility;
20
use TYPO3\CMS\Core\Utility\MathUtility;
21
use TYPO3\CMS\Core\Database\ConnectionPool;
22
use Kitodo\Dlf\Common\Document;
23
use Kitodo\Dlf\Common\Helper;
24
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);
1 ignored issue
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
88
        }
89
90
        if ($input->getOption('solr')) {
91
            $allSolrCores = $this->getSolrCores($startingPoint);
92
            if (MathUtility::canBeInterpretedAsInteger($input->getOption('solr'))) {
93
                $solrCoreUid = MathUtility::forceIntegerInRange((int)$input->getOption('solr'), 0);
94
            } else {
95
                $solrCoreName = $input->getOption('solr');
96
                $solrCoreUid = $allSolrCores[$solrCoreName];
97
            }
98
            // Abort if solrCoreUid is empty or not in the array of allowed solr cores.
99
            if (empty($solrCoreUid) || !in_array($solrCoreUid, $allSolrCores)) {
100
                foreach ($allSolrCores as $index_name => $uid) {
101
                    $output_solrCores .= ' ' . $uid . ' : ' . $index_name ."\n";
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $output_solrCores does not exist. Did you maybe mean $output?
Loading history...
102
                }
103
                if (empty($output_solrCores)) {
104
                    $io->error('ERROR: No valid solr core ("'. $input->getOption('solr') . '") given. ' . "No valid cores found on PID " . $startingPoint .".\n" . $output_solrCores);
105
                    exit(1);
1 ignored issue
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
106
                } else {
107
                    $io->error('ERROR: No valid solr core ("'. $input->getOption('solr') . '") given. ' . "Valid cores are (<uid>:<index_name>):\n" . $output_solrCores);
108
                    exit(1);
1 ignored issue
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
109
                }
110
            }
111
        } else {
112
            $io->error('ERROR: Required parameter --solr|-s is missing.');
113
            exit(1);
1 ignored issue
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
114
        }
115
116
        if (!MathUtility::canBeInterpretedAsInteger($input->getOption('doc'))
117
            && !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

117
            && !GeneralUtility::isValidUrl(/** @scrutinizer ignore-type */ $input->getOption('doc'))) {
Loading history...
118
            $io->error('ERROR: "' . $input->getOption('doc') . '" is not a valid document UID or URL for --doc|-d.');
119
            exit(1);
1 ignored issue
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
120
        }
121
122
        // Get the document...
123
        $doc = Document::getInstance($input->getOption('doc'), $startingPoint, TRUE);
124
        if ($doc->ready) {
0 ignored issues
show
Bug Best Practice introduced by
The property $ready is declared protected in Kitodo\Dlf\Common\Document. Since you implement __get, consider adding a @property or @property-read.
Loading history...
125
            if ($dryRun) {
126
                $io->section('DRY RUN: Would index ' . $doc->uid . ' '. $doc->location . ' on UID ' . $startingPoint . ' and solr core ' . $solrCoreUid .'.');
0 ignored issues
show
Bug Best Practice introduced by
The property $location is declared protected in Kitodo\Dlf\Common\Document. Since you implement __get, consider adding a @property or @property-read.
Loading history...
Bug Best Practice introduced by
The property $uid is declared protected in Kitodo\Dlf\Common\Document. Since you implement __get, consider adding a @property or @property-read.
Loading history...
127
            } else {
128
                if ($io->isVerbose()) {
129
                    $io->section('Will index ' . $doc->uid . ' ' . $doc->location . ' on UID ' . $startingPoint . ' and solr core ' . $solrCoreUid .'.');
130
                }
131
                // ...and save it to the database...
132
                if (!$doc->save($startingPoint, $solrCoreUid)) {
133
                    $io->error('ERROR: Document "'.$input->getOption('doc').'" not saved and indexed.');
134
                    exit(1);
1 ignored issue
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
135
                }
136
            }
137
        } else {
138
            $io->error('ERROR: Document "'.$input->getOption('doc').'" could not be loaded.');
139
            exit(1);
1 ignored issue
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
140
        }
141
142
        $io->success('All done!');
143
    }
144
145
146
    /**
147
     * Fetches all records of tx_dlf_solrcores on given page.
148
     *
149
     * @param int $pageId the uid of the solr record
150
     *
151
     * @return array array of valid solr cores
152
     */
153
    protected function getSolrCores(int $pageId): array
154
    {
155
        /** @var QueryBuilder $queryBuilder */
156
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
157
            ->getQueryBuilderForTable('tx_dlf_solrcores');
158
159
        $solrCores = [];
160
        $pageId = (int)$pageId;
161
        $result = $queryBuilder
162
            ->select('uid', 'index_name')
163
            ->from('tx_dlf_solrcores')
164
            ->where(
165
                $queryBuilder->expr()->eq(
166
                    'pid',
167
                    $queryBuilder->createNamedParameter($pageId, \PDO::PARAM_INT)
168
                )
169
            )
170
            ->execute();
171
172
        while ($record = $result->fetch()) {
173
            $solrCores[$record['index_name']] = $record['uid'];
174
        }
175
176
        return $solrCores;
177
    }
178
}
179