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.
Completed
Push — master ( f601e6...3e6cda )
by Sebastian
17s
created

ext_update   A

Complexity

Total Complexity 38

Size/Duplication

Total Lines 401
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 154
dl 0
loc 401
rs 9.36
c 0
b 0
f 0
wmc 38

8 Methods

Rating   Name   Duplication   Size   Complexity  
A renameIndexRelatedColumns() 0 34 2
A access() 0 17 4
A main() 0 23 4
A getMetadataConfig() 0 34 4
B oldIndexRelatedTableNames() 0 20 7
B doSolariumSolrUpdate() 0 85 9
A solariumSolrUpdateRequired() 0 26 3
A updateMetadataConfig() 0 67 5
1
<?php
2
/**
3
 * (c) Kitodo. Key to digital objects e.V. <[email protected]>
4
 *
5
 * This file is part of the Kitodo and TYPO3 projects.
6
 *
7
 * @license GNU General Public License version 3 or later.
8
 * For the full copyright and license information, please read the
9
 * LICENSE.txt file that was distributed with this source code.
10
 */
11
12
/**
13
 * Update class 'ext_update' for the 'dlf' extension.
14
 *
15
 * @author	Sebastian Meyer <[email protected]>
16
 * @package	TYPO3
17
 * @subpackage	tx_dlf
18
 * @access	public
19
 */
20
class ext_update {
21
22
    /**
23
     * This holds the output ready to return
24
     *
25
     * @var	string
26
     * @access protected
27
     */
28
    protected $content = '';
29
30
    /**
31
     * Triggers the update option in the extension manager
32
     *
33
     * @access	public
34
     *
35
     * @return	boolean		Should the update option be shown?
36
     */
37
    public function access() {
38
39
        if (count($this->getMetadataConfig())) {
40
41
            return TRUE;
42
43
        } else if ($this->oldIndexRelatedTableNames()) {
44
45
            return TRUE;
46
47
        } else if ($this->solariumSolrUpdateRequired()) {
48
49
            return TRUE;
50
51
        }
52
53
        return FALSE;
54
55
    }
56
57
    /**
58
     * Get all outdated metadata configuration records
59
     *
60
     * @access	protected
61
     *
62
     * @return	array		Array of UIDs of outdated records
63
     */
64
    protected function getMetadataConfig() {
65
66
        $uids = array ();
67
68
        // check if tx_dlf_metadata.xpath exists anyhow
69
        $fieldsInDatabase = $GLOBALS['TYPO3_DB']->admin_get_fields('tx_dlf_metadata');
70
71
        if (!in_array('xpath', array_keys($fieldsInDatabase))) {
72
73
            return $uids;
74
75
        }
76
77
        // Get all records with outdated configuration.
78
        $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
79
            'tx_dlf_metadata.uid AS uid',
80
            'tx_dlf_metadata',
81
            'tx_dlf_metadata.format=0 AND NOT tx_dlf_metadata.xpath=\'\''.tx_dlf_helper::whereClause('tx_dlf_metadata'),
82
            '',
83
            '',
84
            ''
85
        );
86
87
        if ($GLOBALS['TYPO3_DB']->sql_num_rows($result)) {
88
89
            while ($resArray = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result)) {
90
91
                $uids[] = intval($resArray['uid']);
92
93
            }
94
95
        }
96
97
        return $uids;
98
99
    }
100
101
    /**
102
     * The main method of the class
103
     *
104
     * @access	public
105
     *
106
     * @return	string		The content that is displayed on the website
107
     */
108
    public function main() {
109
110
        // Load localization file.
111
        $GLOBALS['LANG']->includeLLFile('EXT:dlf/locallang.xml');
112
113
        // Update the metadata configuration.
114
        if (count($this->getMetadataConfig())) {
115
            $this->updateMetadataConfig();
116
        }
117
118
        if ($this->oldIndexRelatedTableNames()) {
119
120
            $this->renameIndexRelatedColumns();
121
122
        }
123
124
        if ($this->solariumSolrUpdateRequired()) {
125
126
            $this->doSolariumSolrUpdate();
127
128
        }
129
130
        return $this->content;
131
132
    }
133
134
    /**
135
     * Check for old index related colums
136
     *
137
     * @access	protected
138
     *
139
     * @return	boolean		true if old index related columns exist
140
     */
141
    protected function oldIndexRelatedTableNames() {
142
143
        $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
144
            'column_name',
145
            'INFORMATION_SCHEMA.COLUMNS',
146
            'TABLE_NAME = "tx_dlf_metadata"',
147
            '',
148
            '',
149
            ''
150
            );
151
152
        while ($resArray = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result)) {
153
154
            if ($resArray['column_name'] == 'tokenized'
155
                || $resArray['column_name'] == 'stored'
156
                || $resArray['column_name'] == 'indexed'
157
                || $resArray['column_name'] == 'boost'
158
                || $resArray['column_name'] == 'autocomplete') {
159
160
                    return TRUE;
161
162
            }
163
164
        }
165
166
    }
167
168
    /**
169
     * Copy the data of the old index related columns to the new columns
170
     *
171
     * @access	protected
172
     *
173
     * @return	void
174
     */
175
    protected function renameIndexRelatedColumns() {
176
177
        $sqlQuery = "UPDATE tx_dlf_metadata SET `index_tokenized` = `tokenized`
178
											, `index_stored` = `stored`
179
											, `index_indexed` = `indexed`
180
											, `index_boost` = `boost`
181
											, `index_autocomplete` = `autocomplete`";
182
183
        // Copy the content of the old tables to the new ones
184
        $result = $GLOBALS['TYPO3_DB']->sql_query($sqlQuery);
185
186
        if ($result) {
187
188
            $message = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
189
                'TYPO3\\CMS\\Core\\Messaging\\FlashMessage',
190
                $GLOBALS['LANG']->getLL('update.copyIndexRelatedColumnsOkay', TRUE),
191
                $GLOBALS['LANG']->getLL('update.copyIndexRelatedColumns', TRUE),
192
                \TYPO3\CMS\Core\Messaging\FlashMessage::OK,
193
                FALSE
194
                );
195
196
        } else {
197
198
            $message = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
199
                'TYPO3\\CMS\\Core\\Messaging\\FlashMessage',
200
                $GLOBALS['LANG']->getLL('update.copyIndexRelatedColumnsNotOkay', TRUE),
201
                $GLOBALS['LANG']->getLL('update.copyIndexRelatedColumns', TRUE),
202
                \TYPO3\CMS\Core\Messaging\FlashMessage::WARNING,
203
                FALSE
204
                );
205
206
        }
207
208
        $this->content .= $message->render();
209
210
    }
211
212
    /**
213
     * Update all outdated metadata configuration records
214
     *
215
     * @access	protected
216
     *
217
     * @return	void
218
     */
219
    protected function updateMetadataConfig() {
220
221
        $metadataUids = $this->getMetadataConfig();
222
223
        if (!empty($metadataUids)) {
224
225
            $data = array ();
226
227
            // Get all old metadata configuration records.
228
            $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
229
                'tx_dlf_metadata.uid AS uid,tx_dlf_metadata.pid AS pid,tx_dlf_metadata.cruser_id AS cruser_id,tx_dlf_metadata.encoded AS encoded,tx_dlf_metadata.xpath AS xpath,tx_dlf_metadata.xpath_sorting AS xpath_sorting',
230
                'tx_dlf_metadata',
231
                'tx_dlf_metadata.uid IN ('.implode(',', $metadataUids).')'.tx_dlf_helper::whereClause('tx_dlf_metadata'),
232
                '',
233
                '',
234
                ''
235
            );
236
237
            while ($resArray = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result)) {
238
239
                $newId = uniqid('NEW');
240
241
                // Copy record to new table.
242
                $data['tx_dlf_metadataformat'][$newId] = array (
243
                    'pid' => $resArray['pid'],
244
                    'cruser_id' => $resArray['cruser_id'],
245
                    'parent_id' => $resArray['uid'],
246
                    'encoded' => $resArray['encoded'],
247
                    'xpath' => $resArray['xpath'],
248
                    'xpath_sorting' => $resArray['xpath_sorting']
249
                );
250
251
                // Add reference to old table.
252
                $data['tx_dlf_metadata'][$resArray['uid']]['format'] = $newId;
253
254
            }
255
256
            if (!empty($data)) {
257
258
                // Process datamap.
259
                $substUids = tx_dlf_helper::processDBasAdmin($data);
260
261
                unset ($data);
262
263
                if (!empty($substUids)) {
264
265
                    $message = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
266
                        'TYPO3\\CMS\\Core\\Messaging\\FlashMessage',
267
                        $GLOBALS['LANG']->getLL('update.metadataConfigOkay', TRUE),
268
                        $GLOBALS['LANG']->getLL('update.metadataConfig', TRUE),
269
                        \TYPO3\CMS\Core\Messaging\FlashMessage::OK,
270
                        FALSE
271
                    );
272
273
                } else {
274
275
                    $message = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
276
                        'TYPO3\\CMS\\Core\\Messaging\\FlashMessage',
277
                        $GLOBALS['LANG']->getLL('update.metadataConfigNotOkay', TRUE),
278
                        $GLOBALS['LANG']->getLL('update.metadataConfig', TRUE),
279
                        \TYPO3\CMS\Core\Messaging\FlashMessage::WARNING,
280
                        FALSE
281
                    );
282
283
                }
284
285
                $this->content .= $message->render();
286
287
            }
288
289
        }
290
291
    }
292
293
    /**
294
     * Check all configured Solr cores
295
     *
296
     * @access	protected
297
     *
298
     * @return	boolean
299
     */
300
    protected function solariumSolrUpdateRequired() {
301
302
        // Get all Solr cores that were not deleted.
303
        $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
304
            'index_name',
305
            'tx_dlf_solrcores',
306
            'deleted = 0',
307
            '',
308
            '',
309
            ''
310
            );
311
312
        while ($resArray = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result)) {
313
314
            // Instantiate search object.
315
            $solr = tx_dlf_solr::getInstance($resArray['index_name']);
316
317
            if (!$solr->ready) {
0 ignored issues
show
Bug Best Practice introduced by
The property $ready is declared protected in tx_dlf_solr. Since you implement __get, consider adding a @property or @property-read.
Loading history...
318
319
                return TRUE;
320
321
            }
322
323
        }
324
325
        return FALSE;
326
327
    }
328
329
    /**
330
     * Create all configured Solr cores
331
     *
332
     * @access	protected
333
     *
334
     * @return	void
335
     */
336
    protected function doSolariumSolrUpdate() {
337
338
        // Get all Solr cores that were not deleted.
339
        $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
340
            'index_name',
341
            'tx_dlf_solrcores',
342
            'deleted = 0',
343
            '',
344
            '',
345
            ''
346
            );
347
348
        while ($resArray = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result)) {
349
350
            // Instantiate search object.
351
            $solr = tx_dlf_solr::getInstance($resArray['index_name']);
352
353
            if (!$solr->ready) {
0 ignored issues
show
Bug Best Practice introduced by
The property $ready is declared protected in tx_dlf_solr. Since you implement __get, consider adding a @property or @property-read.
Loading history...
354
355
                $conf = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['dlf']);
356
357
                $solrInfo = tx_dlf_solr::getSolrConnectionInfo();
358
359
                // Prepend username and password to hostname.
360
                if ($solrInfo['username'] && $solrInfo['password']) {
361
362
                    $host = $solrInfo['username'].':'.$solrInfo['password'].'@'.$solrInfo['host'];
363
364
                } else {
365
366
                    $host = $solrInfo['host'];
367
368
                }
369
370
                $context = stream_context_create(array (
371
                    'http' => array (
372
                        'method' => 'GET',
373
                        'user_agent' => ($conf['useragent'] ? $conf['useragent'] : ini_get('user_agent'))
374
                    )
375
                ));
376
377
                // Build request for adding new Solr core.
378
                // @see http://wiki.apache.org/solr/CoreAdmin
379
                $url = $solrInfo['scheme'].'://'.$host.':'.$solrInfo['port'].'/'.$solrInfo['path'].'/admin/cores?wt=xml&action=CREATE&name='.$resArray['index_name'].'&instanceDir=dlfCore'.$resArray['index_name'].'&dataDir=data&configSet=dlf';
380
381
                $response = @simplexml_load_string(file_get_contents($url, FALSE, $context));
382
383
                // Process response.
384
                if ($response) {
385
386
                    $status = $response->xpath('//lst[@name="responseHeader"]/int[@name="status"]');
387
388
                    if ($status && $status[0] == 0) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $status of type SimpleXMLElement[] 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...
389
390
                        continue;
391
392
                    }
393
394
                }
395
396
                $message = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
397
                    'TYPO3\\CMS\\Core\\Messaging\\FlashMessage',
398
                    $GLOBALS['LANG']->getLL('update.solariumSolrUpdateNotOkay', TRUE),
399
                    sprintf($GLOBALS['LANG']->getLL('update.solariumSolrUpdate', TRUE), $resArray['index_name']),
400
                    \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR,
401
                    FALSE
402
                    );
403
404
                $this->content .= $message->render();
405
406
                return;
407
408
            }
409
410
        }
411
412
        $message = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
413
            'TYPO3\\CMS\\Core\\Messaging\\FlashMessage',
414
            $GLOBALS['LANG']->getLL('update.solariumSolrUpdateOkay', TRUE),
415
            $GLOBALS['LANG']->getLL('update.solariumSolrUpdate', TRUE),
416
            \TYPO3\CMS\Core\Messaging\FlashMessage::OK,
417
            FALSE
418
            );
419
420
        $this->content .= $message->render();
421
422
    }
423
424
}
425