Passed
Push — master ( 3040b9...5d3eff )
by
unknown
16:25
created

SysLogChannel::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
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
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;
19
20
use TYPO3\CMS\Core\Database\Connection;
21
use TYPO3\CMS\Core\Database\ConnectionPool;
22
use TYPO3\CMS\Core\SysLog\Type;
23
use TYPO3\CMS\Core\Utility\GeneralUtility;
24
25
class SysLogChannel implements UpgradeWizardInterface
26
{
27
    protected Connection $sysLogTable;
28
29
    public function __construct()
30
    {
31
        $this->sysLogTable = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('sys_log');
32
    }
33
34
    /**
35
     * @inheritDoc
36
     */
37
    public function getIdentifier(): string
38
    {
39
        return 'sysLogChannel';
40
    }
41
42
    /**
43
     * @inheritDoc
44
     */
45
    public function getTitle(): string
46
    {
47
        return 'Populates a new channel column of the sys_log table.';
48
    }
49
50
    /**
51
     * @inheritDoc
52
     */
53
    public function getDescription(): string
54
    {
55
        return <<<END
56
The logging system is migrating toward string-based channels rather than int-based types. This update populates the new column of existing log entries.
57
END;
58
    }
59
60
    /**
61
     * @inheritDoc
62
     */
63
    public function executeUpdate(): bool
64
    {
65
        $statement = $this->sysLogTable->prepare('UPDATE sys_log SET channel = ? WHERE type = ?');
66
        foreach (Type::channelMap() as $type => $channel) {
67
            $statement->executeQuery([$channel, $type]);
68
        }
69
70
        $statement = $this->sysLogTable->prepare('UPDATE sys_log SET level = ? WHERE type = ?');
71
        foreach (Type::levelMap() as $type => $level) {
72
            $statement->executeQuery([$level, $type]);
73
        }
74
75
        return true;
76
    }
77
78
    /**
79
     * If all log entries have a default channel, assume we've not mapped anything yet.
80
     */
81
    public function updateNecessary(): bool
82
    {
83
        $result = $this->sysLogTable->executeQuery('SELECT count(channel) FROM sys_log WHERE NOT channel="default"');
84
        return !$result->fetchOne();
85
    }
86
87
    /**
88
     * @inheritDoc
89
     */
90
    public function getPrerequisites(): array
91
    {
92
        // we need to make sure the new DB column was already added.
93
        return [
94
            DatabaseUpdatedPrerequisite::class
95
        ];
96
    }
97
}
98