Completed
Push — master ( 64542d...27c7de )
by
unknown
12:47
created

WorkspaceMovePlaceholderRemovalMigration   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
dl 0
loc 64
rs 10
c 1
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A hasPotentialUpdateForTable() 0 3 1
A updateTableRow() 0 39 4
A getTitle() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the TYPO3 CMS project.
7
 *
8
 * It is free software; you can redistribute it and/or modify it under
9
 * the terms of the GNU General Public License, either version 2
10
 * of the License, or any later version.
11
 *
12
 * For the full copyright and license information, please read the
13
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
 * The TYPO3 project - inspiring people to share!
16
 */
17
18
namespace TYPO3\CMS\Install\Updates\RowUpdater;
19
20
use Psr\Log\LoggerAwareInterface;
21
use Psr\Log\LoggerAwareTrait;
22
use TYPO3\CMS\Backend\Utility\BackendUtility;
23
use TYPO3\CMS\Core\Database\ConnectionPool;
24
use TYPO3\CMS\Core\Utility\GeneralUtility;
25
use TYPO3\CMS\Core\Versioning\VersionState;
26
27
/**
28
 * Removes all records of type MOVE_PLACEHOLDER (t3ver_state = 3) from the system.
29
 * Also makes sure that the important values (that is: pid and sorting) are migrated
30
 * into the connected MOVE_POINTER (t3ver_state=4).
31
 *
32
 * move placeholder (t3ver_state=3) contains t3ver_move_id = UID of live version
33
 * move pointer (t3ver_state=4) contains t3ver_oid = UID of live version
34
 *
35
 * @internal This class is only meant to be used within EXT:install and is not part of the TYPO3 Core API.
36
 */
37
class WorkspaceMovePlaceholderRemovalMigration implements RowUpdaterInterface, LoggerAwareInterface
38
{
39
    use LoggerAwareTrait;
40
41
    public function getTitle(): string
42
    {
43
        return 'Scan for move placeholders of workspaces and remove them from the database.';
44
    }
45
46
    /**
47
     * @param string $tableName Table name to check
48
     * @return bool Return true if a table has workspace enabled
49
     */
50
    public function hasPotentialUpdateForTable(string $tableName): bool
51
    {
52
        return BackendUtility::isTableWorkspaceEnabled($tableName);
53
    }
54
55
    /**
56
     * Update "pid" field and delete the move placeholder record completely
57
     *
58
     * @param string $tableName Table name
59
     * @param array $row Given row data
60
     * @return array Modified row data
61
     */
62
    public function updateTableRow(string $tableName, array $row): array
63
    {
64
        // We only want the information from the move placeholder
65
        if ((int)$row['t3ver_state'] !== 3) {
66
            return $row;
67
        }
68
69
        // Since t3ver_state = 3 and t3ver_state = 4 are not connected, the only way to do this is via the live record
70
        $liveUid = (int)($row['t3ver_move_id'] ?? 0);
71
        $workspaceId = (int)($row['t3ver_wsid'] ?? 0);
72
73
        // Update the move pointer with the pid & sorting values of the move placeholder
74
        if ($liveUid > 0) {
75
            $updatedFieldsForMovePointer = [
76
                'pid' => (int)$row['pid']
77
            ];
78
            $sortByFieldName = $GLOBALS['TCA'][$tableName]['ctrl']['sortby'] ?? null;
79
            if ($sortByFieldName) {
80
                $updatedFieldsForMovePointer[$sortByFieldName] = (int)$row[$sortByFieldName];
81
            }
82
83
            // Update the move pointer record
84
            GeneralUtility::makeInstance(ConnectionPool::class)
85
                ->getConnectionForTable($tableName)
86
                ->update(
87
                    $tableName,
88
                    $updatedFieldsForMovePointer,
89
                    [
90
                        't3ver_oid' => $liveUid,
91
                        't3ver_state' => VersionState::MOVE_POINTER,
92
                        't3ver_wsid' => $workspaceId
93
                    ]
94
                );
95
        }
96
97
        // The "deleted" key marks the information that this record should be deleted
98
        // (with soft-delete or hard-delete) in the RowUpdater main class.
99
        $row['deleted'] = 1;
100
        return $row;
101
    }
102
}
103