TYPO3 CMS  TYPO3_7-6
FinalDatabaseSchemaUpdate.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: Modify tables and fields';
29  }
30 
37  public function checkForUpdate(&$description)
38  {
39  $contextService = $this->objectManager->get(\TYPO3\CMS\Install\Service\ContextService::class);
40  $description = 'There are tables or fields in the database which need to be changed.<br /><br />' .
41  'This update wizard can be run only when there are no other update wizards left to make sure they have all needed fields unchanged.<br /><br />' .
42  'If you want to apply changes selectively, <a href="Install.php?install[action]=importantActions&amp;install[context]=' . $contextService->getContextString() . '&amp;install[controller]=tool">go to Database Analyzer</a>.';
43 
44  $databaseDifferences = $this->getDatabaseDifferences();
45  $updateSuggestions = $this->schemaMigrationService->getUpdateSuggestions($databaseDifferences);
46 
47  return isset($updateSuggestions['change']);
48  }
49 
56  public function getUserInput($inputPrefix)
57  {
58  $result = '';
59 
60  $databaseDifferences = $this->getDatabaseDifferences();
61  $updateSuggestions = $this->schemaMigrationService->getUpdateSuggestions($databaseDifferences);
62 
63  if (!isset($updateSuggestions['change'])) {
64  return $result;
65  }
66 
67  $fieldsList = '
68  <p>
69  Change the following fields in tables:
70  </p>
71  <fieldset>
72  <ol class="t3-install-form-label-after">%s</ol>
73  </fieldset>';
74  $keysList = '
75  <p>
76  Change the following keys in 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>: %2$s</label>
84  </li>';
85 
86  $fieldItems = [];
87  $keyItems = [];
88  foreach ($databaseDifferences['diff'] as $tableName => $difference) {
89  if ($difference['fields']) {
90  $fieldNames = [];
91  foreach ($difference['fields'] as $fieldName => $sql) {
92  $fieldNames[] = $fieldName;
93  }
94  $fieldItems[] = sprintf($item, $tableName, implode(', ', $fieldNames));
95  }
96  if ($difference['keys']) {
97  $keyNames = [];
98  foreach ($difference['keys'] as $keyName => $sql) {
99  $keyNames[] = $keyName;
100  }
101  $keyItems[] = sprintf($item, $tableName, implode(', ', $keyNames));
102  }
103  }
104  if (!empty($fieldItems)) {
105  $result .= sprintf($fieldsList, implode('', $fieldItems));
106  }
107  if (!empty($keyItems)) {
108  $result .= sprintf($keysList, implode('', $keyItems));
109  }
110 
111  return $result;
112  }
113 
121  public function performUpdate(array &$dbQueries, &$customMessages)
122  {
123  // First perform all add update statements to database
124  $databaseDifferences = $this->getDatabaseDifferences();
125  $updateStatements = $this->schemaMigrationService->getUpdateSuggestions($databaseDifferences);
126 
127  $db = $this->getDatabaseConnection();
128  $customMessagesArray = [];
129  foreach ((array)$updateStatements['change'] as $query) {
130  $db->admin_query($query);
131  $dbQueries[] = $query;
132  if ($db->sql_error()) {
133  $customMessagesArray[] = 'SQL-ERROR: ' . htmlspecialchars($db->sql_error());
134  }
135  }
136 
137  if (!empty($customMessagesArray)) {
138  $customMessages = 'Update process not fully processed. This can happen because of dependencies of table fields and ' .
139  'indexes. Please repeat this step! Following errors occurred:' . LF . LF . implode(LF, $customMessagesArray);
140  }
141 
142  return empty($customMessagesArray);
143  }
144 }
$sql
Definition: server.php:84