[TASK] Move install update flags to system registry
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Classes / Updates / AbstractUpdate.php
1 <?php
2 namespace 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
17 use TYPO3\CMS\Core\Configuration\ConfigurationManager;
18 use TYPO3\CMS\Core\Registry;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20 use TYPO3\CMS\Install\Controller\Action\Tool\UpgradeWizard;
21
22 /**
23 * Generic class that every update wizard class inherits from.
24 * Used by the update wizard in the install tool.
25 */
26 abstract class AbstractUpdate
27 {
28 /**
29 * The human-readable title of the upgrade wizard
30 *
31 * @var string
32 */
33 protected $title;
34
35 /**
36 * The update wizard identifier
37 *
38 * @var string
39 */
40 protected $identifier;
41
42 /**
43 * Parent object
44 *
45 * @var UpgradeWizard
46 */
47 public $pObj;
48
49 /**
50 * User input, set from outside
51 *
52 * @var string
53 */
54 public $userInput;
55
56 /**
57 * Current TYPO3 version number, set from outside
58 * Version number coming from \TYPO3\CMS\Core\Utility\VersionNumberUtility::convertVersionNumberToInteger()
59 *
60 * @var int
61 */
62 public $versionNumber;
63
64 /**
65 * Returns the title attribute
66 *
67 * @return string The title of this update wizard
68 */
69 public function getTitle()
70 {
71 if ($this->title) {
72 return $this->title;
73 } else {
74 return $this->identifier;
75 }
76 }
77
78 /**
79 * Sets the title attribute
80 *
81 * @param string $title The title of this update wizard
82 * @return void
83 */
84 public function setTitle($title)
85 {
86 $this->title = $title;
87 }
88
89 /**
90 * Returns the identifier of this class
91 *
92 * @return string The identifier of this update wizard
93 */
94 public function getIdentifier()
95 {
96 return $this->identifier;
97 }
98
99 /**
100 * Sets the identifier attribute
101 *
102 * @param string $identifier The identifier of this update wizard
103 * @return void
104 */
105 public function setIdentifier($identifier)
106 {
107 $this->identifier = $identifier;
108 }
109
110 /**
111 * Simple wrapper function that helps dealing with the compatibility
112 * layer that some update wizards don't have a second parameter
113 * thus, it evaluates everything already
114 *
115 * @return bool If the wizard should be shown at all on the overview page
116 * @see checkForUpdate()
117 */
118 public function shouldRenderWizard()
119 {
120 $showUpdate = 0;
121 $explanation = '';
122 $result = $this->checkForUpdate($explanation, $showUpdate);
123 return $showUpdate > 0 || $result == true;
124 }
125
126 /**
127 * Simple wrapper function that helps to check whether (if)
128 * this feature is cool if you want to tell the user that the update wizard
129 * is working fine, just as output (useful for the character set / utf8 wizard)
130 *
131 * @return bool If the wizard should render the Next() button on the overview page
132 * @see checkForUpdate()
133 */
134 public function shouldRenderNextButton()
135 {
136 $showUpdate = 0;
137 $explanation = '';
138 $result = $this->checkForUpdate($explanation, $showUpdate);
139 return $showUpdate != 2 || $result;
140 }
141
142 /**
143 * Check if given table exists
144 *
145 * @param string $table
146 * @return bool
147 */
148 public function checkIfTableExists($table)
149 {
150 $databaseTables = $this->getDatabaseConnection()->admin_get_tables();
151 if (array_key_exists($table, $databaseTables)) {
152 return true;
153 }
154 return false;
155 }
156
157 /**
158 * Checks whether updates are required.
159 *
160 * @param string &$description The description for the update
161 * @return bool Whether an update is required (TRUE) or not (FALSE)
162 */
163 abstract public function checkForUpdate(&$description);
164
165 /**
166 * Performs the accordant updates.
167 *
168 * @param array &$dbQueries Queries done in this update
169 * @param mixed &$customMessages Custom messages
170 * @return bool Whether everything went smoothly or not
171 */
172 abstract public function performUpdate(array &$dbQueries, &$customMessages);
173
174 /**
175 * This method can be called to install extensions following all proper processes
176 * (e.g. installing in extList, respecting priority, etc.)
177 *
178 * @param array $extensionKeys List of keys of extensions to install
179 * @return void
180 */
181 protected function installExtensions(array $extensionKeys)
182 {
183 /** @var $installUtility \TYPO3\CMS\Extensionmanager\Utility\InstallUtility */
184 $installUtility = GeneralUtility::makeInstance(
185 \TYPO3\CMS\Extensionmanager\Utility\InstallUtility::class
186 );
187 foreach ($extensionKeys as $extension) {
188 $installUtility->install($extension);
189 }
190 }
191
192 /**
193 * Marks some wizard as being "seen" so that it not shown again.
194 *
195 * Writes the info in LocalConfiguration.php
196 *
197 * @param mixed $confValue The configuration is set to this value
198 * @return void
199 */
200 protected function markWizardAsDone($confValue = 1)
201 {
202 GeneralUtility::makeInstance(Registry::class)->set('installUpdate', get_class($this), $confValue);
203 }
204
205 /**
206 * Checks if this wizard has been "done" before
207 *
208 * @return bool TRUE if wizard has been done before, FALSE otherwise
209 */
210 protected function isWizardDone()
211 {
212 $wizardClassName = get_class($this);
213 $done = GeneralUtility::makeInstance(Registry::class)->get('installUpdate', $wizardClassName, false);
214
215 // Fall back in case the wizard for migration of "wizard done" flags to system registry was not run yet
216 if (!$done) {
217 try {
218 GeneralUtility::makeInstance(ConfigurationManager::class)
219 ->getLocalConfigurationValueByPath('INSTALL/wizardDone/' . $wizardClassName);
220 $done = true;
221 } catch (\RuntimeException $e) {
222 }
223 }
224
225 return $done;
226 }
227
228 /**
229 * @return \TYPO3\CMS\Core\Database\DatabaseConnection
230 */
231 protected function getDatabaseConnection()
232 {
233 return $GLOBALS['TYPO3_DB'];
234 }
235 }