c7ebe3c574a63dbd57dd2bb0209ddfc6d25eb7f8
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Classes / Updates / InitialDatabaseSchemaUpdate.php
1 <?php
2 namespace TYPO3\CMS\Install\Updates;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2014 Stephan Großberndt <stephan@grossberndt.de>
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 text file 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 to create tables, fields and keys to comply to the database schema
32 *
33 * @author Stephan Großberndt <stephan@grossberndt.de>
34 */
35 class InitialDatabaseSchemaUpdate extends AbstractDatabaseSchemaUpdate {
36
37 /**
38 * Constructor function.
39 */
40 public function __construct() {
41 parent::__construct();
42 $this->title = 'Update database schema: Create tables and fields';
43 }
44
45 /**
46 * Checks if an update is needed
47 *
48 * @param string &$description The description for the update
49 * @return bool TRUE if an update is needed, FALSE otherwise
50 */
51 public function checkForUpdate(&$description) {
52 $description = 'There are tables or fields in the database which need to be created.<br /><br />' .
53 'You have to run this update wizard before you can run any other update wizard to make sure all needed tables and fields are present.';
54
55 $databaseDifferences = $this->getDatabaseDifferences();
56 $updateSuggestions = $this->schemaMigrationService->getUpdateSuggestions($databaseDifferences);
57
58 return isset($updateSuggestions['create_table']) || isset($updateSuggestions['add']);
59 }
60
61 /**
62 * Second step: Show tables, fields and keys to be created
63 *
64 * @param string $inputPrefix input prefix, all names of form fields have to start with this. Append custom name in [ ... ]
65 * @return string HTML output
66 */
67 public function getUserInput($inputPrefix) {
68 $result = '';
69
70 $databaseDifferences = $this->getDatabaseDifferences();
71 $updateSuggestions = $this->schemaMigrationService->getUpdateSuggestions($databaseDifferences);
72
73 if (isset($updateSuggestions['create_table'])) {
74 $list = '
75 <p>
76 Add the following tables:
77 </p>
78 <fieldset>
79 <ol class="t3-install-form-label-after">%s</ol>
80 </fieldset>';
81 $item = '
82 <li class="labelAfter">
83 <label><strong>%1$s</strong></label>
84 </li>';
85
86 $items = array();
87 foreach ($databaseDifferences['extra'] as $tableName => $difference) {
88 if ($difference['whole_table'] == 1) {
89 $items[] = sprintf($item, $tableName);
90 }
91 }
92 $result .= sprintf($list, implode('', $items));
93 }
94
95 if (isset($updateSuggestions['add'])) {
96 $fieldsList = '
97 <p>
98 Add the following fields to tables:
99 </p>
100 <fieldset>
101 <ol class="t3-install-form-label-after">%s</ol>
102 </fieldset>';
103 $keysList = '
104 <p>
105 Add the following keys to tables:
106 </p>
107 <fieldset>
108 <ol class="t3-install-form-label-after">%s</ol>
109 </fieldset>';
110 $item = '
111 <li class="labelAfter">
112 <label><strong>%1$s</strong>: %2$s</label>
113 </li>';
114
115 $fieldItems = array();
116 $keyItems = array();
117 foreach ($databaseDifferences['extra'] as $tableName => $difference) {
118 if ($difference['whole_table'] != 1) {
119 if ($difference['fields']) {
120 $fieldNames = array();
121 foreach ($difference['fields'] as $fieldName => $sql) {
122 $fieldNames[] = $fieldName;
123 }
124 $fieldItems[] = sprintf($item, $tableName, implode(', ', $fieldNames));
125 }
126 if ($difference['keys']) {
127 $keyNames = array();
128 foreach ($difference['keys'] as $keyName => $sql) {
129 $keyNames[] = $keyName;
130 }
131 $keyItems[] = sprintf($item, $tableName, implode(', ', $keyNames));
132 }
133 }
134 }
135 if (!empty($fieldItems)) {
136 $result .= sprintf($fieldsList, implode('', $fieldItems));
137 }
138 if (!empty($keyItems)) {
139 $result .= sprintf($keysList, implode('', $keyItems));
140 }
141 }
142
143 return $result;
144 }
145
146
147 /**
148 * Performs the database update.
149 *
150 * @param array &$dbQueries Queries done in this update
151 * @param mixed &$customMessages Custom messages
152 * @return bool TRUE on success, FALSE on error
153 */
154 public function performUpdate(array &$dbQueries, &$customMessages) {
155
156 // First perform all add update statements to database
157 $databaseDifferences = $this->getDatabaseDifferences();
158 $updateStatements = $this->schemaMigrationService->getUpdateSuggestions($databaseDifferences);
159
160 foreach ((array)$updateStatements['create_table'] as $query) {
161 $GLOBALS['TYPO3_DB']->admin_query($query);
162 $dbQueries[] = $query;
163 if ($GLOBALS['TYPO3_DB']->sql_error()) {
164 $customMessages = 'SQL-ERROR: ' . htmlspecialchars($GLOBALS['TYPO3_DB']->sql_error());
165 return FALSE;
166 }
167 }
168
169 foreach ((array)$updateStatements['add'] as $query) {
170 $GLOBALS['TYPO3_DB']->admin_query($query);
171 $dbQueries[] = $query;
172 if ($GLOBALS['TYPO3_DB']->sql_error()) {
173 $customMessages = 'SQL-ERROR: ' . htmlspecialchars($GLOBALS['TYPO3_DB']->sql_error());
174 return FALSE;
175 }
176 }
177
178 return TRUE;
179 }
180 }