[BUGFIX] SQL error in install tool if no root page available
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Classes / Updates / ExtensionManagerTables.php
1 <?php
2 namespace TYPO3\CMS\Install\Updates;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2012-2013 Georg Ringer <typo3@ringerge.org>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the textfile GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29
30 /**
31 * Contains the update class for filling the basic repository record of the extension manager
32 *
33 * @author Georg Ringer <typo3@ringerge.org>
34 */
35 class ExtensionManagerTables extends AbstractUpdate {
36
37 /**
38 * @var string
39 */
40 protected $title = 'Add the default Extension Manager database tables';
41
42 /**
43 * @var NULL|\TYPO3\CMS\Install\Service\SqlSchemaMigrationService
44 */
45 protected $installToolSqlParser = NULL;
46
47 /**
48 * @return \TYPO3\CMS\Install\Service\SqlSchemaMigrationService
49 */
50 protected function getInstallToolSqlParser() {
51 if ($this->installToolSqlParser === NULL) {
52 $this->installToolSqlParser = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Install\\Service\\SqlSchemaMigrationService');
53 }
54
55 return $this->installToolSqlParser;
56 }
57
58 /**
59 * Gets all create, add and change queries from ext_tables.sql
60 *
61 * @return array
62 */
63 protected function getUpdateStatements() {
64 $updateStatements = array();
65
66 // Get all necessary statements for ext_tables.sql file
67 $rawDefinitions = \TYPO3\CMS\Core\Utility\GeneralUtility::getUrl(\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath('extensionmanager') . '/ext_tables.sql');
68 $fieldDefinitionsFromFile = $this->getInstallToolSqlParser()->getFieldDefinitions_fileContent($rawDefinitions);
69 if (count($fieldDefinitionsFromFile)) {
70 $fieldDefinitionsFromCurrentDatabase = $this->getInstallToolSqlParser()->getFieldDefinitions_database();
71 $diff = $this->getInstallToolSqlParser()->getDatabaseExtra($fieldDefinitionsFromFile, $fieldDefinitionsFromCurrentDatabase);
72 $updateStatements = $this->getInstallToolSqlParser()->getUpdateSuggestions($diff);
73 }
74
75 return $updateStatements;
76 }
77
78 /**
79 * Checks if an update is needed
80 *
81 * @param string &$description The description for the update
82 * @return boolean Whether an update is needed (TRUE) or not (FALSE)
83 */
84 public function checkForUpdate(&$description) {
85 $result = FALSE;
86 $description = 'Creates necessary database tables and adds static tables for the new Extension Manager.';
87
88 // First check necessary database update
89 $updateStatements = $this->getUpdateStatements();
90 if (empty($updateStatements)) {
91 // Check for repository database table
92 $databaseTables = $GLOBALS['TYPO3_DB']->admin_get_tables();
93 if (!isset($databaseTables['tx_extensionmanager_domain_model_repository'])) {
94 $result = TRUE;
95 } else {
96 // Get count of rows in repository database table
97 $count = $GLOBALS['TYPO3_DB']->exec_SELECTcountRows('*', 'tx_extensionmanager_domain_model_repository');
98 if ($count === 0) {
99 $result = TRUE;
100 }
101 }
102 } else {
103 $result = TRUE;
104 }
105
106 return $result;
107 }
108
109 /**
110 * @param mixed &$customMessages Custom messages
111 *
112 * @return boolean
113 */
114 protected function hasError(&$customMessages) {
115 $result = FALSE;
116 if ($GLOBALS['TYPO3_DB']->sql_error()) {
117 $customMessages .= '<br /><br />SQL-ERROR: ' . htmlspecialchars($GLOBALS['TYPO3_DB']->sql_error());
118 $result = TRUE;
119 }
120
121 return $result;
122 }
123
124 /**
125 * Performs the database update.
126 *
127 * @param array &$dbQueries Queries done in this update
128 * @param mixed &$customMessages Custom messages
129 * @return boolean Whether it worked (TRUE) or not (FALSE)
130 */
131 public function performUpdate(array &$dbQueries, &$customMessages) {
132 $result = FALSE;
133
134 // First perform all create, add and change queries
135 $updateStatements = $this->getUpdateStatements();
136 foreach ((array) $updateStatements['add'] as $string) {
137 $GLOBALS['TYPO3_DB']->admin_query($string);
138 $dbQueries[] = $string;
139 $result = ($result || $this->hasError($customMessages));
140 }
141 foreach ((array) $updateStatements['change'] as $string) {
142 $GLOBALS['TYPO3_DB']->admin_query($string);
143 $dbQueries[] = $string;
144 $result = ($result || $this->hasError($customMessages));
145 }
146 foreach ((array) $updateStatements['create_table'] as $string) {
147 $GLOBALS['TYPO3_DB']->admin_query($string);
148 $dbQueries[] = $string;
149 $result = ($result || $this->hasError($customMessages));
150 }
151
152 // Perform statis import anyway
153 $rawDefinitions = \TYPO3\CMS\Core\Utility\GeneralUtility::getUrl(\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath('extensionmanager') . 'ext_tables_static+adt.sql');
154 $statements = $this->getInstallToolSqlParser()->getStatementarray($rawDefinitions, 1);
155 foreach ($statements as $statement) {
156 if (trim($statement) !== '') {
157 $GLOBALS['TYPO3_DB']->admin_query($statement);
158 $dbQueries[] = $statement;
159 $result = ($result || $this->hasError($customMessages));
160 }
161 }
162
163 return !$result;
164 }
165
166 }
167
168 ?>