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 — dev-extbase-fluid ( 1e77b8...3a04ab )
by Alexander
19s queued 13s
created

DataHandler::getDocumentRepository()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 8
rs 10
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\Doc;
16
use Kitodo\Dlf\Common\Helper;
17
use Kitodo\Dlf\Common\Indexer;
18
use Kitodo\Dlf\Common\Solr;
19
use Kitodo\Dlf\Domain\Repository\DocumentRepository;
20
use Kitodo\Dlf\Domain\Model\Document;
21
use Psr\Log\LoggerAwareInterface;
22
use Psr\Log\LoggerAwareTrait;
23
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
24
use TYPO3\CMS\Core\Database\ConnectionPool;
25
use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
26
use TYPO3\CMS\Core\Utility\GeneralUtility;
27
use TYPO3\CMS\Extbase\Object\ObjectManager;
28
29
/**
30
 * Hooks and helper for \TYPO3\CMS\Core\DataHandling\DataHandler
31
 *
32
 * @author Sebastian Meyer <[email protected]>
33
 * @package TYPO3
34
 * @subpackage dlf
35
 * @access public
36
 */
37
class DataHandler implements LoggerAwareInterface
38
{
39
    use LoggerAwareTrait;
40
41
    /**
42
     * @var DocumentRepository
43
     */
44
    protected $documentRepository;
45
46
    protected function getDocumentRepository()
47
    {
48
        if ($this->documentRepository === null) {
49
            $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
50
            $this->documentRepository = $objectManager->get(DocumentRepository::class);
51
        }
52
53
        return $this->documentRepository;
54
    }
55
56
    /**
57
     * Field post-processing hook for the process_datamap() method.
58
     *
59
     * @access public
60
     *
61
     * @param string $status: 'new' or 'update'
62
     * @param string $table: The destination table
63
     * @param int $id: The uid of the record
64
     * @param array &$fieldArray: Array of field values
65
     *
66
     * @return void
67
     */
68
    public function processDatamap_postProcessFieldArray($status, $table, $id, &$fieldArray)
69
    {
70
        if ($status == 'new') {
71
            switch ($table) {
72
                    // Field post-processing for table "tx_dlf_documents".
73
                case 'tx_dlf_documents':
74
                    // Set sorting field if empty.
75
                    if (
76
                        empty($fieldArray['title_sorting'])
77
                        && !empty($fieldArray['title'])
78
                    ) {
79
                        $fieldArray['title_sorting'] = $fieldArray['title'];
80
                    }
81
                    break;
82
                    // Field post-processing for table "tx_dlf_metadata".
83
                case 'tx_dlf_metadata':
84
                    // Store field in index if it should appear in lists.
85
                    if (!empty($fieldArray['is_listed'])) {
86
                        $fieldArray['index_stored'] = 1;
87
                    }
88
                    // Index field in index if it should be used for auto-completion.
89
                    if (!empty($fieldArray['index_autocomplete'])) {
90
                        $fieldArray['index_indexed'] = 1;
91
                    }
92
                    // Field post-processing for tables "tx_dlf_metadata", "tx_dlf_collections", "tx_dlf_libraries" and "tx_dlf_structures".
93
                case 'tx_dlf_collections':
94
                case 'tx_dlf_libraries':
95
                case 'tx_dlf_structures':
96
                    // Set label as index name if empty.
97
                    if (
98
                        empty($fieldArray['index_name'])
99
                        && !empty($fieldArray['label'])
100
                    ) {
101
                        $fieldArray['index_name'] = $fieldArray['label'];
102
                    }
103
                    // Set index name as label if empty.
104
                    if (
105
                        empty($fieldArray['label'])
106
                        && !empty($fieldArray['index_name'])
107
                    ) {
108
                        $fieldArray['label'] = $fieldArray['index_name'];
109
                    }
110
                    // Ensure that index names don't get mixed up with sorting values.
111
                    if (substr($fieldArray['index_name'], -8) == '_sorting') {
112
                        $fieldArray['index_name'] .= '0';
113
                    }
114
                    break;
115
                    // Field post-processing for table "tx_dlf_solrcores".
116
                case 'tx_dlf_solrcores':
117
                    // Create new Solr core.
118
                    $fieldArray['index_name'] = Solr::createCore($fieldArray['index_name']);
119
                    if (empty($fieldArray['index_name'])) {
120
                        $this->logger->error('Could not create new Apache Solr core');
121
                    }
122
                    break;
123
            }
124
        } elseif ($status == 'update') {
125
            switch ($table) {
126
                    // Field post-processing for table "tx_dlf_metadata".
127
                case 'tx_dlf_metadata':
128
                    $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
129
                        ->getQueryBuilderForTable($table);
130
131
                    // Store field in index if it should appear in lists.
132
                    if (!empty($fieldArray['is_listed'])) {
133
                        $fieldArray['index_stored'] = 1;
134
                    }
135
                    if (
136
                        isset($fieldArray['index_stored'])
137
                        && $fieldArray['index_stored'] == 0
138
                        && !isset($fieldArray['is_listed'])
139
                    ) {
140
                        // Get current configuration.
141
                        $result = $queryBuilder
142
                            ->select($table . '.is_listed AS is_listed')
143
                            ->from($table)
144
                            ->where(
145
                                $queryBuilder->expr()->eq($table . '.uid', intval($id)),
146
                                Helper::whereExpression($table)
147
                            )
148
                            ->setMaxResults(1)
149
                            ->execute();
150
151
                        if ($resArray = $result->fetch()) {
152
                            // Reset storing to current.
153
                            $fieldArray['index_stored'] = $resArray['is_listed'];
154
                        }
155
                    }
156
                    // Index field in index if it should be used for auto-completion.
157
                    if (!empty($fieldArray['index_autocomplete'])) {
158
                        $fieldArray['index_indexed'] = 1;
159
                    }
160
                    if (
161
                        isset($fieldArray['index_indexed'])
162
                        && $fieldArray['index_indexed'] == 0
163
                        && !isset($fieldArray['index_autocomplete'])
164
                    ) {
165
                        // Get current configuration.
166
                        $result = $queryBuilder
167
                            ->select($table . '.index_autocomplete AS index_autocomplete')
168
                            ->from($table)
169
                            ->where(
170
                                $queryBuilder->expr()->eq($table . '.uid', intval($id)),
171
                                Helper::whereExpression($table)
172
                            )
173
                            ->setMaxResults(1)
174
                            ->execute();
175
176
                        if ($resArray = $result->fetch()) {
177
                            // Reset indexing to current.
178
                            $fieldArray['index_indexed'] = $resArray['index_autocomplete'];
179
                        }
180
                    }
181
                    break;
182
            }
183
        }
184
    }
185
186
    /**
187
     * After database operations hook for the process_datamap() method.
188
     *
189
     * @access public
190
     *
191
     * @param string $status: 'new' or 'update'
192
     * @param string $table: The destination table
193
     * @param int $id: The uid of the record
194
     * @param array &$fieldArray: Array of field values
195
     *
196
     * @return void
197
     */
198
    public function processDatamap_afterDatabaseOperations($status, $table, $id, &$fieldArray)
199
    {
200
        if ($status == 'update') {
201
            switch ($table) {
202
                    // After database operations for table "tx_dlf_documents".
203
                case 'tx_dlf_documents':
204
                    // Delete/reindex document in Solr if "hidden" status or collections have changed.
205
                    if (
206
                        isset($fieldArray['hidden'])
207
                        || isset($fieldArray['collections'])
208
                    ) {
209
                        // Get Solr-Core.
210
                        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
211
                            ->getQueryBuilderForTable('tx_dlf_solrcores');
212
213
                        $result = $queryBuilder
214
                            ->select(
215
                                'tx_dlf_solrcores.uid AS core',
216
                                'tx_dlf_solrcores.index_name',
217
                                'tx_dlf_documents_join.hidden AS hidden'
218
                            )
219
                            ->innerJoin(
220
                                'tx_dlf_solrcores',
221
                                'tx_dlf_documents',
222
                                'tx_dlf_documents_join',
223
                                $queryBuilder->expr()->eq(
224
                                    'tx_dlf_documents_join.solrcore',
225
                                    'tx_dlf_solrcores.uid'
226
                                )
227
                            )
228
                            ->from('tx_dlf_solrcores')
229
                            ->where(
230
                                $queryBuilder->expr()->eq(
231
                                    'tx_dlf_documents_join.uid',
232
                                    intval($id)
233
                                )
234
                            )
235
                            ->setMaxResults(1)
236
                            ->execute();
237
238
                        if ($resArray = $result->fetch()) {
239
                            if ($resArray['hidden']) {
240
                                // Establish Solr connection.
241
                                $solr = Solr::getInstance($resArray['core']);
242
                                if ($solr->ready) {
243
                                    // Delete Solr document.
244
                                    $updateQuery = $solr->service->createUpdate();
245
                                    $updateQuery->addDeleteQuery('uid:' . $id);
246
                                    $updateQuery->addCommit();
247
                                    $solr->service->update($updateQuery);
248
                                }
249
                            } else {
250
                                // Reindex document.
251
                                $document = $this->getDocumentRepository()->findByUid($id);
252
                                $doc = Doc::getInstance($document->getLocation(), ['storagePid' => $document->getPid()], true);
253
                                if ($document !== null && $doc !== null) {
254
                                    $document->setDoc($doc);
255
                                    Indexer::add($document);
256
                                } else {
257
                                    $this->logger->error('Failed to re-index document with UID ' . $id);
258
                                }
259
                            }
260
                        }
261
                    }
262
                break;
263
            }
264
        }
265
    }
266
267
    /**
268
     * Post-processing hook for the process_cmdmap() method.
269
     *
270
     * @access public
271
     *
272
     * @param string $command: 'move', 'copy', 'localize', 'inlineLocalizeSynchronize', 'delete' or 'undelete'
273
     * @param string $table: The destination table
274
     * @param int $id: The uid of the record
275
     *
276
     * @return void
277
     */
278
    public function processCmdmap_postProcess($command, $table, $id)
279
    {
280
        if (
281
            in_array($command, ['move', 'delete', 'undelete'])
282
            && $table == 'tx_dlf_documents'
283
        ) {
284
            // Get Solr core.
285
            $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
286
                ->getQueryBuilderForTable('tx_dlf_solrcores');
287
            // Record in "tx_dlf_documents" is already deleted at this point.
288
            $queryBuilder
289
                ->getRestrictions()
290
                ->removeByType(DeletedRestriction::class);
291
292
            $result = $queryBuilder
293
                ->select(
294
                    'tx_dlf_solrcores.uid AS core'
295
                )
296
                ->innerJoin(
297
                    'tx_dlf_solrcores',
298
                    'tx_dlf_documents',
299
                    'tx_dlf_documents_join',
300
                    $queryBuilder->expr()->eq(
301
                        'tx_dlf_documents_join.solrcore',
302
                        'tx_dlf_solrcores.uid'
303
                    )
304
                )
305
                ->from('tx_dlf_solrcores')
306
                ->where(
307
                    $queryBuilder->expr()->eq(
308
                        'tx_dlf_documents_join.uid',
309
                        intval($id)
310
                    )
311
                )
312
                ->setMaxResults(1)
313
                ->execute();
314
315
            if ($resArray = $result->fetch()) {
316
                switch ($command) {
317
                    case 'move':
318
                    case 'delete':
319
                        // Establish Solr connection.
320
                        $solr = Solr::getInstance($resArray['core']);
321
                        if ($solr->ready) {
322
                            // Delete Solr document.
323
                            $updateQuery = $solr->service->createUpdate();
324
                            $updateQuery->addDeleteQuery('uid:' . $id);
325
                            $updateQuery->addCommit();
326
                            $solr->service->update($updateQuery);
327
                            if ($command == 'delete') {
328
                                break;
329
                            }
330
                        }
331
                    case 'undelete':
332
                        // Reindex document.
333
                        $document = $this->getDocumentRepository()->findByUid($id);
334
                        $doc = Doc::getInstance($document->getLocation(), ['storagePid' => $document->getPid()], true);
335
                        if ($document !== null && $doc !== null) {
336
                            $document->setDoc($doc);
337
                            Indexer::add($document);
338
                        } else {
339
                            $this->logger->error('Failed to re-index document with UID ' . $id);
340
                        }
341
                        break;
342
                }
343
            }
344
        }
345
        if (
346
            $command === 'delete'
347
            && $table == 'tx_dlf_solrcores'
348
        ) {
349
            // Is core deletion allowed in extension configuration?
350
            $extConf = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get('dlf');
351
            if (!empty($extConf['solrAllowCoreDelete'])) {
352
                // Delete core from Apache Solr as well.
353
                $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
354
                    ->getQueryBuilderForTable('tx_dlf_solrcores');
355
                // Record in "tx_dlf_solrcores" is already deleted at this point.
356
                $queryBuilder
357
                    ->getRestrictions()
358
                    ->removeByType(DeletedRestriction::class);
359
360
                $result = $queryBuilder
361
                    ->select(
362
                        'tx_dlf_solrcores.index_name AS core'
363
                    )
364
                    ->from('tx_dlf_solrcores')
365
                    ->where($queryBuilder->expr()->eq('tx_dlf_solrcores.uid', intval($id)))
366
                    ->setMaxResults(1)
367
                    ->execute();
368
369
                if ($resArray = $result->fetch()) {
370
                    // Establish Solr connection.
371
                    $solr = Solr::getInstance();
372
                    if ($solr->ready) {
373
                        // Delete Solr core.
374
                        $query = $solr->service->createCoreAdmin();
375
                        $action = $query->createUnload();
376
                        $action->setCore($resArray['core']);
377
                        $action->setDeleteDataDir(true);
378
                        $action->setDeleteIndex(true);
379
                        $action->setDeleteInstanceDir(true);
380
                        $query->setAction($action);
381
                        try {
382
                            $response = $solr->service->coreAdmin($query);
383
                            if ($response->getWasSuccessful()) {
384
                                return;
385
                            }
386
                        } catch (\Exception $e) {
387
                            // Nothing to do here.
388
                        }
389
                    }
390
                    $this->logger->warning('Core ' . $resArray['core'] . ' could not be deleted from Apache Solr');
391
                }
392
            }
393
        }
394
    }
395
}
396