[TASK] Migrate easy part of ExtensionManagerTables-Update
[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\Database\ConnectionPool;
18 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20
21 /**
22 * Contains the update class for filling the basic repository record of the extension manager
23 */
24 class ExtensionManagerTables extends AbstractUpdate
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 = file_get_contents(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 = GeneralUtility::makeInstance(ConnectionPool::class)
85 ->getConnectionForTable('tx_extensionmanager_domain_model_repository')
86 ->count('*', 'tx_extensionmanager_domain_model_repository', []);
87 if ($count === 0) {
88 $result = true;
89 }
90 } else {
91 $result = true;
92 }
93
94 return $result;
95 }
96
97 /**
98 * @param mixed &$customMessages Custom messages
99 *
100 * @return bool
101 */
102 protected function hasError(&$customMessages)
103 {
104 $result = false;
105 if ($this->getDatabaseConnection()->sql_error()) {
106 $customMessages .= '<br /><br />SQL-ERROR: ' . htmlspecialchars($this->getDatabaseConnection()->sql_error());
107 $result = true;
108 }
109
110 return $result;
111 }
112
113 /**
114 * Performs the database update.
115 *
116 * @param array &$dbQueries Queries done in this update
117 * @param mixed &$customMessages Custom messages
118 * @return bool Whether it worked (TRUE) or not (FALSE)
119 */
120 public function performUpdate(array &$dbQueries, &$customMessages)
121 {
122 $result = false;
123
124 // First perform all create, add and change queries
125 $updateStatements = $this->getUpdateStatements();
126 foreach ((array)$updateStatements['add'] as $string) {
127 $this->getDatabaseConnection()->admin_query($string);
128 $dbQueries[] = $string;
129 $result = ($result || $this->hasError($customMessages));
130 }
131 foreach ((array)$updateStatements['change'] as $string) {
132 $this->getDatabaseConnection()->admin_query($string);
133 $dbQueries[] = $string;
134 $result = ($result || $this->hasError($customMessages));
135 }
136 foreach ((array)$updateStatements['create_table'] as $string) {
137 $this->getDatabaseConnection()->admin_query($string);
138 $dbQueries[] = $string;
139 $result = ($result || $this->hasError($customMessages));
140 }
141
142 // Perform statics import anyway
143 $rawDefinitions = file_get_contents(ExtensionManagementUtility::extPath('extensionmanager', 'ext_tables_static+adt.sql'));
144 $statements = $this->getInstallToolSqlParser()->getStatementArray($rawDefinitions, 1);
145 foreach ($statements as $statement) {
146 if (trim($statement) !== '') {
147 $this->getDatabaseConnection()->admin_query($statement);
148 $dbQueries[] = $statement;
149 $result = ($result || $this->hasError($customMessages));
150 }
151 }
152
153 return !$result;
154 }
155 }