[BUGFIX] Restore update wizard to add TER repository in EM
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Classes / Updates / ExtensionManagerTables.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\Utility\ExtensionManagementUtility;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19
20 /**
21 * Contains the update class for filling the basic repository record of the extension manager
22 */
23 class ExtensionManagerTables extends AbstractUpdate
24 {
25
26 /**
27 * @var string
28 */
29 protected $title = 'Add the default Extension Manager database tables';
30
31 /**
32 * @var NULL|\TYPO3\CMS\Install\Service\SqlSchemaMigrationService
33 */
34 protected $installToolSqlParser = null;
35
36 /**
37 * @return \TYPO3\CMS\Install\Service\SqlSchemaMigrationService
38 */
39 protected function getInstallToolSqlParser()
40 {
41 if ($this->installToolSqlParser === null) {
42 $this->installToolSqlParser = GeneralUtility::makeInstance(\TYPO3\CMS\Install\Service\SqlSchemaMigrationService::class);
43 }
44
45 return $this->installToolSqlParser;
46 }
47
48 /**
49 * Gets all create, add and change queries from ext_tables.sql
50 *
51 * @return array
52 */
53 protected function getUpdateStatements()
54 {
55 $updateStatements = array();
56
57 // Get all necessary statements for ext_tables.sql file
58 $rawDefinitions = GeneralUtility::getUrl(ExtensionManagementUtility::extPath('extensionmanager') . '/ext_tables.sql');
59 $fieldDefinitionsFromFile = $this->getInstallToolSqlParser()->getFieldDefinitions_fileContent($rawDefinitions);
60 if (count($fieldDefinitionsFromFile)) {
61 $fieldDefinitionsFromCurrentDatabase = $this->getInstallToolSqlParser()->getFieldDefinitions_database();
62 $diff = $this->getInstallToolSqlParser()->getDatabaseExtra($fieldDefinitionsFromFile, $fieldDefinitionsFromCurrentDatabase);
63 $updateStatements = $this->getInstallToolSqlParser()->getUpdateSuggestions($diff);
64 }
65
66 return $updateStatements;
67 }
68
69 /**
70 * Checks if an update is needed
71 *
72 * @param string &$description The description for the update
73 * @return bool Whether an update is needed (TRUE) or not (FALSE)
74 */
75 public function checkForUpdate(&$description)
76 {
77 $result = false;
78 $description = 'Creates necessary database tables and adds static data for the Extension Manager.';
79
80 // First check necessary database update
81 $updateStatements = $this->getUpdateStatements();
82 if (empty($updateStatements)) {
83 // Get count of rows in repository database table
84 $count = $this->getDatabaseConnection()->exec_SELECTcountRows('*', 'tx_extensionmanager_domain_model_repository');
85 if ($count === 0) {
86 $result = true;
87 }
88 } else {
89 $result = true;
90 }
91
92 return $result;
93 }
94
95 /**
96 * @param mixed &$customMessages Custom messages
97 *
98 * @return bool
99 */
100 protected function hasError(&$customMessages)
101 {
102 $result = false;
103 if ($this->getDatabaseConnection()->sql_error()) {
104 $customMessages .= '<br /><br />SQL-ERROR: ' . htmlspecialchars($this->getDatabaseConnection()->sql_error());
105 $result = true;
106 }
107
108 return $result;
109 }
110
111 /**
112 * Performs the database update.
113 *
114 * @param array &$dbQueries Queries done in this update
115 * @param mixed &$customMessages Custom messages
116 * @return bool Whether it worked (TRUE) or not (FALSE)
117 */
118 public function performUpdate(array &$dbQueries, &$customMessages)
119 {
120 $result = false;
121
122 // First perform all create, add and change queries
123 $updateStatements = $this->getUpdateStatements();
124 foreach ((array)$updateStatements['add'] as $string) {
125 $this->getDatabaseConnection()->admin_query($string);
126 $dbQueries[] = $string;
127 $result = ($result || $this->hasError($customMessages));
128 }
129 foreach ((array)$updateStatements['change'] as $string) {
130 $this->getDatabaseConnection()->admin_query($string);
131 $dbQueries[] = $string;
132 $result = ($result || $this->hasError($customMessages));
133 }
134 foreach ((array)$updateStatements['create_table'] as $string) {
135 $this->getDatabaseConnection()->admin_query($string);
136 $dbQueries[] = $string;
137 $result = ($result || $this->hasError($customMessages));
138 }
139
140 // Perform statis import anyway
141 $rawDefinitions = GeneralUtility::getUrl(ExtensionManagementUtility::extPath('extensionmanager') . 'ext_tables_static+adt.sql');
142 $statements = $this->getInstallToolSqlParser()->getStatementarray($rawDefinitions, 1);
143 foreach ($statements as $statement) {
144 if (trim($statement) !== '') {
145 $this->getDatabaseConnection()->admin_query($statement);
146 $dbQueries[] = $statement;
147 $result = ($result || $this->hasError($customMessages));
148 }
149 }
150
151 return !$result;
152 }
153 }