TYPO3 CMS  TYPO3_7-6
InitialDatabaseSchemaUpdate.php
Go to the documentation of this file.
1 <?php
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 
21 {
25  public function __construct()
26  {
27  parent::__construct();
28  $this->title = 'Update database schema: Create tables and fields';
29  }
30 
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 
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 = [];
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 = [];
104  $keyItems = [];
105  foreach ($databaseDifferences['extra'] as $tableName => $difference) {
106  if ($difference['whole_table'] != 1) {
107  if ($difference['fields']) {
108  $fieldNames = [];
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 = [];
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 
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 }
$sql
Definition: server.php:84