MigrateDependenciesToJson.php 2.73 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php

declare(strict_types = 1);

namespace T3o\TerFe2\Command;

/*
 * This file is part of the TYPO3 CMS project.
 *
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
 *
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
 *
 * The TYPO3 project - inspiring people to share!
 */

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Utility\GeneralUtility;

/**
 * One time command, can be removed in late 2020.
 */
class MigrateDependenciesToJson extends Command
{
    protected function configure(): void
    {
        $this->setDescription('One-time-migration. Migrates tx_terfe2_domain_model_version.dependencies from serialized array to JSON.');
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $io = new SymfonyStyle($input, $output);
        $io->section('Updating field \'dependencies\' of all \'tx_terfe2_domain_model_version\' rows now');
        $connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
42
43
44
45
46
47
        $queryBuilder = $connectionPool->getQueryBuilderForTable('tx_terfe2_domain_model_version');

        $queryBuilder
            ->getRestrictions()
            ->removeAll();
        $queryBuilder
48
            ->select('uid', 'dependencies')
49
            ->from('tx_terfe2_domain_model_version');
50

51
        $result = $queryBuilder->execute();
52
        $itemsProcessed = 0;
53
54

        while ($row = $result->fetchAssociative()) {
55
56
57
58
59
60
            $dependencies = (string)($row['dependencies'] ?? '');
            if ($dependencies === '' || strpos($dependencies, 'a:') === false) {
                // skip already migrated or not serialized row
                continue;
            }
            try {
61
                $connectionPool
62
63
64
                    ->getConnectionForTable('tx_terfe2_domain_model_version')
                    ->update(
                        'tx_terfe2_domain_model_version',
65
                        ['dependencies' => json_encode((array)(unserialize($dependencies, ['allowed_classes' => false]) ?: []), JSON_THROW_ON_ERROR)],
66
67
68
69
                        ['uid' => $row['uid']]
                    );
                $itemsProcessed++;
            } catch (\JsonException $e) {
70
                $io->error('Could not update record: \'' . $row['uid'] . '\'');
71
72
73
74
75
76
77
            }
        }

        $io->success('Done. Updated ' . $itemsProcessed . ' items.');
        return 0;
    }
}