1<?php
2
3declare(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
18namespace TYPO3\CMS\Install\Updates;
19
20use TYPO3\CMS\Core\Database\ConnectionPool;
21use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
22use TYPO3\CMS\Core\Utility\GeneralUtility;
23
24/**
25 * Installs and downloads EXT:legacy_collections if requested
26 * @internal This class is only meant to be used within EXT:install and is not part of the TYPO3 Core API.
27 */
28class CollectionsExtractionUpdate extends AbstractDownloadExtensionUpdate
29{
30    /**
31     * @var ExtensionModel
32     */
33    protected $extension;
34
35    /**
36     * @var Confirmation
37     */
38    protected $confirmation;
39
40    public function __construct()
41    {
42        $this->extension = new ExtensionModel(
43            'legacy_collections',
44            'sys_collection Database APIs',
45            '1.0.0',
46            'friendsoftypo3/legacy-collections',
47            'Re-Adds previously available sys_collection database tables'
48        );
49
50        $this->confirmation = new Confirmation(
51            'Are you sure?',
52            'This API has not been used very often, only install it if you have entries in your sys_collection database table. ' . $this->extension->getDescription(),
53            false
54        );
55    }
56
57    /**
58     * Return a confirmation message instance
59     *
60     * @return Confirmation
61     */
62    public function getConfirmation(): Confirmation
63    {
64        return $this->confirmation;
65    }
66
67    /**
68     * Return the identifier for this wizard
69     * This should be the same string as used in the ext_localconf class registration
70     *
71     * @return string
72     */
73    public function getIdentifier(): string
74    {
75        return 'legacyCollectionsExtension';
76    }
77
78    /**
79     * Return the speaking name of this wizard
80     *
81     * @return string
82     */
83    public function getTitle(): string
84    {
85        return 'Install extension "legacy_collections" from TER for sys_collection database records';
86    }
87
88    /**
89     * Return the description for this wizard
90     *
91     * @return string
92     */
93    public function getDescription(): string
94    {
95        return 'The extension "legacy_collections" re-adds the database tables sys_collection_* and its TCA definition, if this was previously in use.';
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        // Extension already activated, nothing to do
107        if (ExtensionManagementUtility::isLoaded('legacy_collections')) {
108            return true;
109        }
110        // Check if database table exist
111        $connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
112        $connection = $connectionPool->getConnectionByName('Default');
113        $tableNames = $connection->createSchemaManager()->listTableNames();
114        if (in_array('sys_collection', $tableNames, true)) {
115            // table is available, now check if there are entries in it
116            $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
117                ->getQueryBuilderForTable('sys_collection');
118            $numberOfEntries = $queryBuilder->count('*')
119                ->from('sys_collection')
120                ->executeQuery()
121                ->fetchOne();
122            return (bool)$numberOfEntries;
123        }
124
125        return false;
126    }
127
128    /**
129     * Returns an array of class names of Prerequisite classes
130     * This way a wizard can define dependencies like "database up-to-date" or
131     * "reference index updated"
132     *
133     * @return string[]
134     */
135    public function getPrerequisites(): array
136    {
137        return [
138            DatabaseUpdatedPrerequisite::class,
139        ];
140    }
141}
142