Completed
Push — master ( ded3c9...01c6cd )
by
unknown
28:49 queued 13:48
created

WorkspaceVersionRecordsMigration::getTitle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
namespace TYPO3\CMS\Install\Updates\RowUpdater;
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
use Psr\Log\LoggerAwareInterface;
19
use Psr\Log\LoggerAwareTrait;
20
use TYPO3\CMS\Backend\Utility\BackendUtility;
21
use TYPO3\CMS\Core\Versioning\VersionState;
22
23
/**
24
 * Migrate all records that have "pid=-1" to their proper equivalents.
25
 * t3_wsid=0 AND pid=-1 ---> discarded records or archived records. Since we have no connection to the original anymore, we remove them (hard delete)
26
 * t3_wsid>0 AND pid=-1 AND t3ver_oid>0 -> find the live version and take the PID from the live version, and replace the PID
27
 * Since the move pointer (t3ver_state=3) is not affected, as it contains the future live PID, there is no need to touch these records.
28
 *
29
 * @internal This class is only meant to be used within EXT:install and is not part of the TYPO3 Core API.
30
 */
31
class WorkspaceVersionRecordsMigration implements RowUpdaterInterface, LoggerAwareInterface
32
{
33
    use LoggerAwareTrait;
34
35
    public function getTitle(): string
36
    {
37
        return 'Scan for versioned records and fix their pid, or if no connection to a workspace is given, remove them completely to avoid having them shown on the live website.';
38
    }
39
40
    /**
41
     * @param string $tableName Table name to check
42
     * @return bool Return true if a table has workspace enabled
43
     */
44
    public function hasPotentialUpdateForTable(string $tableName): bool
45
    {
46
        return BackendUtility::isTableWorkspaceEnabled($tableName);
47
    }
48
49
    /**
50
     * Update "pid" field or delete record completely
51
     *
52
     * @param string $tableName Table name
53
     * @param array $row Given row data
54
     * @return array Modified row data
55
     */
56
    public function updateTableRow(string $tableName, array $row): array
57
    {
58
        // We only modify records with "pid=-1"
59
        if ((int)$row['pid'] !== -1) {
60
            return $row;
61
        }
62
        // pid=-1 and live workspace => this may be very old "previous live" records that should be discarded
63
        if ((int)$row['t3ver_wsid'] === 0) {
64
            $row['deleted'] = 1;
65
            // continue processing versions
66
        }
67
        // regular versions and placeholders (t3ver_state one of -1, 0, 2, 4 - but not 3) having t3ver_oid set
68
        if ((int)$row['t3ver_oid'] > 0 && (int)$row['t3ver_state'] !== VersionState::MOVE_PLACEHOLDER) {
69
            // We have a live version, let's connect that one
70
            $liveRecord = $this->fetchPageId($tableName, (int)$row['t3ver_oid']);
71
            if (is_array($liveRecord)) {
72
                $row['pid'] = (int)$liveRecord['pid'];
73
                return $row;
74
            }
75
        }
76
        // move placeholder (t3ver_state=3) pointing to live version in t3ver_move_id
77
        if ((int)$row['t3ver_move_id'] > 0 && (int)$row['t3ver_state'] === VersionState::MOVE_PLACEHOLDER) {
78
            // We have a live version, let's connect that one
79
            $liveRecord = $this->fetchPageId($tableName, (int)$row['t3ver_move_id']);
80
            if (is_array($liveRecord)) {
81
                $row['pid'] = (int)$liveRecord['pid'];
82
                return $row;
83
            }
84
        }
85
        // No live version available
86
        return $row;
87
    }
88
89
    protected function fetchPageId(string $tableName, int $id): ?array
90
    {
91
        return BackendUtility::getRecord($tableName, $id, 'pid');
92
    }
93
}
94