[TASK] Removes extra empty lines
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Classes / Updates / FinalDatabaseSchemaUpdate.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 and alter tables, fields and keys to comply to the database schema
19 */
20 class FinalDatabaseSchemaUpdate extends AbstractDatabaseSchemaUpdate
21 {
22 /**
23 * Constructor function.
24 */
25 public function __construct()
26 {
27 parent::__construct();
28 $this->title = 'Update database schema: Modify 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 $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
50 /**
51 * Second step: Show tables, fields and keys to create or update
52 *
53 * @param string $inputPrefix input prefix, all names of form fields have to start with this. Append custom name in [ ... ]
54 * @return string HTML output
55 */
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 = array();
87 $keyItems = array();
88 foreach ($databaseDifferences['diff'] as $tableName => $difference) {
89 if ($difference['fields']) {
90 $fieldNames = array();
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 = array();
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
114 /**
115 * Performs the database update.
116 *
117 * @param array &$dbQueries Queries done in this update
118 * @param mixed &$customMessages Custom messages
119 * @return bool TRUE on success, FALSE on error
120 */
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 = array();
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 }