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