1<?php
2
3/*
4 * This file is part of the TYPO3 CMS project.
5 *
6 * It is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU General Public License, either version 2
8 * of the License, or any later version.
9 *
10 * For the full copyright and license information, please read the
11 * LICENSE.txt file that was distributed with this source code.
12 *
13 * The TYPO3 project - inspiring people to share!
14 */
15
16namespace TYPO3\CMS\Install\Updates;
17
18use TYPO3\CMS\Core\Database\ConnectionPool;
19use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
20use TYPO3\CMS\Core\Utility\GeneralUtility;
21
22/**
23 * Installs and downloads EXT:rdct if cache_md5params is filled
24 * @internal This class is only meant to be used within EXT:install and is not part of the TYPO3 Core API.
25 */
26class RedirectExtractionUpdate extends AbstractDownloadExtensionUpdate
27{
28    /**
29     * @var \TYPO3\CMS\Install\Updates\ExtensionModel
30     */
31    protected $extension;
32
33    /**
34     * @var \TYPO3\CMS\Install\Updates\Confirmation
35     */
36    protected $confirmation;
37
38    public function __construct()
39    {
40        $this->extension = new ExtensionModel(
41            'rdct',
42            'Redirects based on &RDCT parameter',
43            '1.0.0',
44            'friendsoftypo3/rdct',
45            'The extension provides redirects based on "cache_md5params" and the GET parameter &RDCT for extensions that still rely on it.'
46        );
47
48        $this->confirmation = new Confirmation(
49            'Are you sure?',
50            'You should install the Redirects extension only if needed. ' . $this->extension->getDescription(),
51            false
52        );
53    }
54
55    /**
56     * Return a confirmation message instance
57     *
58     * @return \TYPO3\CMS\Install\Updates\Confirmation
59     */
60    public function getConfirmation(): Confirmation
61    {
62        return $this->confirmation;
63    }
64
65    /**
66     * Return the identifier for this wizard
67     * This should be the same string as used in the ext_localconf class registration
68     *
69     * @return string
70     */
71    public function getIdentifier(): string
72    {
73        return 'rdctExtension';
74    }
75
76    /**
77     * Return the speaking name of this wizard
78     *
79     * @return string
80     */
81    public function getTitle(): string
82    {
83        return 'Install extension "rdct" from TER if DB table cache_md5params is filled';
84    }
85
86    /**
87     * Return the description for this wizard
88     *
89     * @return string
90     */
91    public function getDescription(): string
92    {
93        return 'The extension "rdct" includes redirects based on the GET parameter &RDCT. The functionality has been extracted to'
94               . ' the TYPO3 Extension Repository. This update downloads the TYPO3 extension from the TER.'
95               . ' Use this if you are dealing with extensions in the instance that rely on this kind of redirects.';
96    }
97
98    /**
99     * Is an update necessary?
100     * Is used to determine whether a wizard needs to be run.
101     *
102     * @return bool
103     */
104    public function updateNecessary(): bool
105    {
106        return !ExtensionManagementUtility::isLoaded('rdct') && $this->checkIfWizardIsRequired();
107    }
108
109    /**
110     * Check if the database table "cache_md5params" exists and if so, if there are entries in the DB table.
111     *
112     * @return bool
113     * @throws \InvalidArgumentException
114     */
115    protected function checkIfWizardIsRequired(): bool
116    {
117        $connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
118        $connection = $connectionPool->getConnectionByName('Default');
119        $tableNames = $connection->getSchemaManager()->listTableNames();
120        if (in_array('cache_md5params', $tableNames, true)) {
121            // table is available, now check if there are entries in it
122            $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
123                ->getQueryBuilderForTable('cache_md5params');
124            $numberOfEntries = $queryBuilder->count('*')
125                ->from('cache_md5params')
126                ->execute()
127                ->fetchColumn();
128            return (bool)$numberOfEntries;
129        }
130
131        return false;
132    }
133
134    /**
135     * Returns an array of class names of Prerequisite classes
136     * This way a wizard can define dependencies like "database up-to-date" or
137     * "reference index updated"
138     *
139     * @return string[]
140     */
141    public function getPrerequisites(): array
142    {
143        return [
144            DatabaseUpdatedPrerequisite::class
145        ];
146    }
147}
148