[TASK] Re-work/simplify copyright header in PHP files - Part 1
[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 * @author Stephan GroƟberndt <stephan@grossberndt.de>
21 */
22 class InitialDatabaseSchemaUpdate extends AbstractDatabaseSchemaUpdate {
23
24 /**
25 * Constructor function.
26 */
27 public function __construct() {
28 parent::__construct();
29 $this->title = 'Update database schema: Create tables and fields';
30 }
31
32 /**
33 * Checks if an update is needed
34 *
35 * @param string &$description The description for the update
36 * @return bool TRUE if an update is needed, FALSE otherwise
37 */
38 public function checkForUpdate(&$description) {
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 $result = '';
56
57 $databaseDifferences = $this->getDatabaseDifferences();
58 $updateSuggestions = $this->schemaMigrationService->getUpdateSuggestions($databaseDifferences);
59
60 if (isset($updateSuggestions['create_table'])) {
61 $list = '
62 <p>
63 Add the following tables:
64 </p>
65 <fieldset>
66 <ol class="t3-install-form-label-after">%s</ol>
67 </fieldset>';
68 $item = '
69 <li class="labelAfter">
70 <label><strong>%1$s</strong></label>
71 </li>';
72
73 $items = array();
74 foreach ($databaseDifferences['extra'] as $tableName => $difference) {
75 if ($difference['whole_table'] == 1) {
76 $items[] = sprintf($item, $tableName);
77 }
78 }
79 $result .= sprintf($list, implode('', $items));
80 }
81
82 if (isset($updateSuggestions['add'])) {
83 $fieldsList = '
84 <p>
85 Add the following fields to tables:
86 </p>
87 <fieldset>
88 <ol class="t3-install-form-label-after">%s</ol>
89 </fieldset>';
90 $keysList = '
91 <p>
92 Add the following keys to tables:
93 </p>
94 <fieldset>
95 <ol class="t3-install-form-label-after">%s</ol>
96 </fieldset>';
97 $item = '
98 <li class="labelAfter">
99 <label><strong>%1$s</strong>: %2$s</label>
100 </li>';
101
102 $fieldItems = array();
103 $keyItems = array();
104 foreach ($databaseDifferences['extra'] as $tableName => $difference) {
105 if ($difference['whole_table'] != 1) {
106 if ($difference['fields']) {
107 $fieldNames = array();
108 foreach ($difference['fields'] as $fieldName => $sql) {
109 $fieldNames[] = $fieldName;
110 }
111 $fieldItems[] = sprintf($item, $tableName, implode(', ', $fieldNames));
112 }
113 if ($difference['keys']) {
114 $keyNames = array();
115 foreach ($difference['keys'] as $keyName => $sql) {
116 $keyNames[] = $keyName;
117 }
118 $keyItems[] = sprintf($item, $tableName, implode(', ', $keyNames));
119 }
120 }
121 }
122 if (!empty($fieldItems)) {
123 $result .= sprintf($fieldsList, implode('', $fieldItems));
124 }
125 if (!empty($keyItems)) {
126 $result .= sprintf($keysList, implode('', $keyItems));
127 }
128 }
129
130 return $result;
131 }
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 // First perform all add update statements to database
144 $databaseDifferences = $this->getDatabaseDifferences();
145 $updateStatements = $this->schemaMigrationService->getUpdateSuggestions($databaseDifferences);
146
147 foreach ((array)$updateStatements['create_table'] as $query) {
148 $GLOBALS['TYPO3_DB']->admin_query($query);
149 $dbQueries[] = $query;
150 if ($GLOBALS['TYPO3_DB']->sql_error()) {
151 $customMessages = 'SQL-ERROR: ' . htmlspecialchars($GLOBALS['TYPO3_DB']->sql_error());
152 return FALSE;
153 }
154 }
155
156 foreach ((array)$updateStatements['add'] as $query) {
157 $GLOBALS['TYPO3_DB']->admin_query($query);
158 $dbQueries[] = $query;
159 if ($GLOBALS['TYPO3_DB']->sql_error()) {
160 $customMessages = 'SQL-ERROR: ' . htmlspecialchars($GLOBALS['TYPO3_DB']->sql_error());
161 return FALSE;
162 }
163 }
164
165 return TRUE;
166 }
167 }