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