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 — master (#468)
by Sebastian
03:15
created

ConfigurationForm::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 6
rs 10
cc 1
nc 1
nop 0
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\Hooks;
14
15
use Kitodo\Dlf\Common\Helper;
16
use Kitodo\Dlf\Common\Solr;
17
use TYPO3\CMS\Core\Core\Bootstrap;
18
use TYPO3\CMS\Core\Database\ConnectionPool;
19
use TYPO3\CMS\Core\Utility\GeneralUtility;
20
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
21
22
/**
23
 * Hooks and helper for \TYPO3\CMS\Core\TypoScript\ConfigurationForm
24
 *
25
 * @author Sebastian Meyer <[email protected]>
26
 * @package TYPO3
27
 * @subpackage dlf
28
 * @access public
29
 */
30
class ConfigurationForm
31
{
32
    /**
33
     * This holds the current configuration
34
     *
35
     * @var array
36
     * @access protected
37
     */
38
    protected $conf = [];
39
40
    /**
41
     * Check if a connection to a Solr server could be established with the given credentials.
42
     *
43
     * @access public
44
     *
45
     * @return string Message informing the user of success or failure
46
     */
47
    public function checkSolrConnection()
48
    {
49
        $solrInfo = Solr::getSolrConnectionInfo();
50
        // Prepend username and password to hostname.
51
        if (
52
            !empty($solrInfo['username'])
53
            && !empty($solrInfo['password'])
54
        ) {
55
            $host = $solrInfo['username'] . ':' . $solrInfo['password'] . '@' . $solrInfo['host'];
56
        } else {
57
            $host = $solrInfo['host'];
58
        }
59
        // Build request URI.
60
        $url = $solrInfo['scheme'] . '://' . $host . ':' . $solrInfo['port'] . '/' . $solrInfo['path'] . '/admin/cores?wt=xml';
61
        $context = stream_context_create([
62
            'http' => [
63
                'method' => 'GET',
64
                'user_agent' => (!empty($this->conf['useragent']) ? $this->conf['useragent'] : ini_get('user_agent'))
65
            ]
66
        ]);
67
        // Try to connect to Solr server.
68
        $response = @simplexml_load_string(file_get_contents($url, false, $context));
69
        // Check status code.
70
        if ($response) {
71
            $status = $response->xpath('//lst[@name="responseHeader"]/int[@name="status"]');
72
            if (is_array($status)) {
73
                Helper::addMessage(
74
                    sprintf($GLOBALS['LANG']->getLL('solr.status'), (string) $status[0]),
75
                    $GLOBALS['LANG']->getLL('solr.connected'),
76
                    ($status[0] == 0 ? \TYPO3\CMS\Core\Messaging\FlashMessage::OK : \TYPO3\CMS\Core\Messaging\FlashMessage::WARNING)
77
                );
78
                return Helper::renderFlashMessages();
79
            }
80
        }
81
        Helper::addMessage(
82
            sprintf($GLOBALS['LANG']->getLL('solr.error'), $url),
83
            $GLOBALS['LANG']->getLL('solr.notConnected'),
84
            \TYPO3\CMS\Core\Messaging\FlashMessage::WARNING
85
        );
86
        return Helper::renderFlashMessages();
87
    }
88
89
    /**
90
     * Make sure the essential namespaces are defined.
91
     *
92
     * @access public
93
     *
94
     * @return string Message informing the user of success or failure
95
     */
96
    public function checkMetadataFormats()
97
    {
98
        // We need to do some bootstrapping manually as of TYPO3 9.
99
        if (version_compare(\TYPO3\CMS\Core\Utility\VersionNumberUtility::getNumericTypo3Version(), '9.0.0', '>=')) {
100
            // Load table configuration array into $GLOBALS['TCA'].
101
            ExtensionManagementUtility::loadBaseTca(false);
102
            // Get extension configuration from dlf/ext_localconf.php.
103
            ExtensionManagementUtility::loadExtLocalconf(false);
104
            // Initialize backend user into $GLOBALS['BE_USER'].
105
            Bootstrap::initializeBackendUser();
106
            // Initialize backend and ensure authenticated access.
107
            Bootstrap::initializeBackendAuthentication();
108
        }
109
110
        $nsDefined = [
111
            'MODS' => false,
112
            'TEIHDR' => false,
113
            'ALTO' => false,
114
            'IIIF1' => false,
115
            'IIIF2' => false,
116
            'IIIF3' => false
117
        ];
118
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
119
            ->getQueryBuilderForTable('tx_dlf_formats');
120
121
        // Check existing format specifications.
122
        $result = $queryBuilder
123
            ->select('tx_dlf_formats.type AS type')
124
            ->from('tx_dlf_formats')
125
            ->where(
126
                '1=1'
127
            )
128
            ->execute();
129
130
        while ($resArray = $result->fetch()) {
131
            $nsDefined[$resArray['type']] = true;
132
        }
133
        // Build data array.
134
        $data = [];
135
        // Add MODS namespace.
136
        if (!$nsDefined['MODS']) {
137
            $data['tx_dlf_formats'][uniqid('NEW')] = [
138
                'pid' => 0,
139
                'type' => 'MODS',
140
                'root' => 'mods',
141
                'namespace' => 'http://www.loc.gov/mods/v3',
142
                'class' => 'Kitodo\\Dlf\\Format\\Mods'
143
            ];
144
        }
145
        // Add TEIHDR namespace.
146
        if (!$nsDefined['TEIHDR']) {
147
            $data['tx_dlf_formats'][uniqid('NEW')] = [
148
                'pid' => 0,
149
                'type' => 'TEIHDR',
150
                'root' => 'teiHeader',
151
                'namespace' => 'http://www.tei-c.org/ns/1.0',
152
                'class' => 'Kitodo\\Dlf\\Format\\TeiHeader'
153
            ];
154
        }
155
        // Add ALTO namespace.
156
        if (!$nsDefined['ALTO']) {
157
            $data['tx_dlf_formats'][uniqid('NEW')] = [
158
                'pid' => 0,
159
                'type' => 'ALTO',
160
                'root' => 'alto',
161
                'namespace' => 'http://www.loc.gov/standards/alto/ns-v2#',
162
                'class' => 'Kitodo\\Dlf\\Format\\Alto'
163
            ];
164
        }
165
        // Add IIIF Metadata API 1 context
166
        if (!$nsDefined['IIIF1']) {
167
            $data['tx_dlf_formats'][uniqid('NEW')] = [
168
                'pid' => 0,
169
                'type' => 'IIIF1',
170
                'root' => 'IIIF1',
171
                'namespace' => 'http://www.shared-canvas.org/ns/context.json',
172
                'class' => ''
173
            ];
174
        }
175
        // Add IIIF Presentation 2 context
176
        if (!$nsDefined['IIIF2']) {
177
            $data['tx_dlf_formats'][uniqid('NEW')] = [
178
                'pid' => 0,
179
                'type' => 'IIIF2',
180
                'root' => 'IIIF2',
181
                'namespace' => 'http://iiif.io/api/presentation/2/context.json',
182
                'class' => ''
183
            ];
184
        }
185
        // Add IIIF Presentation 3 context
186
        if (!$nsDefined['IIIF3']) {
187
            $data['tx_dlf_formats'][uniqid('NEW')] = [
188
                'pid' => 0,
189
                'type' => 'IIIF3',
190
                'root' => 'IIIF3',
191
                'namespace' => 'http://iiif.io/api/presentation/3/context.json',
192
                'class' => ''
193
            ];
194
        }
195
        if (!empty($data)) {
196
            // Process changes.
197
            $substUid = Helper::processDBasAdmin($data);
198
            if (!empty($substUid)) {
199
                Helper::addMessage(
200
                    $GLOBALS['LANG']->getLL('metadataFormats.nsCreatedMsg'),
201
                    $GLOBALS['LANG']->getLL('metadataFormats.nsCreated'),
202
                    \TYPO3\CMS\Core\Messaging\FlashMessage::INFO
203
                );
204
            } else {
205
                Helper::addMessage(
206
                    $GLOBALS['LANG']->getLL('metadataFormats.nsNotCreatedMsg'),
207
                    $GLOBALS['LANG']->getLL('metadataFormats.nsNotCreated'),
208
                    \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR
209
                );
210
            }
211
        } else {
212
            Helper::addMessage(
213
                $GLOBALS['LANG']->getLL('metadataFormats.nsOkayMsg'),
214
                $GLOBALS['LANG']->getLL('metadataFormats.nsOkay'),
215
                \TYPO3\CMS\Core\Messaging\FlashMessage::OK
216
            );
217
        }
218
        return Helper::renderFlashMessages();
219
    }
220
221
    /**
222
     * This is the constructor.
223
     *
224
     * @access public
225
     *
226
     * @return void
227
     */
228
    public function __construct()
229
    {
230
        // Load localization file.
231
        $GLOBALS['LANG']->includeLLFile('EXT:dlf/Resources/Private/Language/FlashMessages.xml');
232
        // Get current configuration.
233
        $this->conf = array_merge((array) unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['dlf']), (array) \TYPO3\CMS\Core\Utility\GeneralUtility::_POST('data'));
234
    }
235
}
236