Completed
Push — master ( f7be59...48f6ed )
by
unknown
30:23 queued 15:45
created

L18nDiffsourceToJsonMigration   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 27
rs 10
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A hasPotentialUpdateForTable() 0 3 2
A getTitle() 0 3 1
A updateTableRow() 0 15 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace TYPO3\CMS\Install\Updates\RowUpdater;
6
7
/*
8
 * This file is part of the TYPO3 CMS project.
9
 *
10
 * It is free software; you can redistribute it and/or modify it under
11
 * the terms of the GNU General Public License, either version 2
12
 * of the License, or any later version.
13
 *
14
 * For the full copyright and license information, please read the
15
 * LICENSE.txt file that was distributed with this source code.
16
 *
17
 * The TYPO3 project - inspiring people to share!
18
 */
19
20
use TYPO3\CMS\Backend\Utility\BackendUtility;
21
22
/**
23
 * Migrate any translatable table rows from transOrigDiffSourceField being stored as serialized
24
 * string to json_encoded string.
25
 *
26
 * @internal This class is only meant to be used within EXT:install and is not part of the TYPO3 Core API.
27
 */
28
class L18nDiffsourceToJsonMigration implements RowUpdaterInterface
29
{
30
    public function getTitle(): string
31
    {
32
        return 'Migrate transOrigDiffSourceField field to json encoded string.';
33
    }
34
35
    public function hasPotentialUpdateForTable(string $tableName): bool
36
    {
37
        return BackendUtility::isTableLocalizable($tableName) && !empty($GLOBALS['TCA'][$tableName]['ctrl']['transOrigDiffSourceField']);
38
    }
39
40
    public function updateTableRow(string $tableName, array $row): array
41
    {
42
        $fieldName = $GLOBALS['TCA'][$tableName]['ctrl']['transOrigDiffSourceField'];
43
        if (
44
            !empty($row[$fieldName])
45
            && is_scalar($row[$fieldName])
46
            && ($fieldContent = @unserialize((string)$row[$fieldName], ['allowed_classes' => false])) !== false
47
        ) {
48
            if (is_object($fieldContent)) {
49
                $row[$fieldName] = null;
50
            } else {
51
                $row[$fieldName] = json_encode($fieldContent);
52
            }
53
        }
54
        return $row;
55
    }
56
}
57