[TASK] Re-work/simplify copyright header in PHP files - Part 1
[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 * @author Stephan GroƟberndt <stephan@grossberndt.de>
21 */
22 class FinalDatabaseSchemaUpdate extends AbstractDatabaseSchemaUpdate {
23
24 /**
25 * Constructor function.
26 */
27 public function __construct() {
28 parent::__construct();
29 $this->title = 'Update database schema: Modify 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 changed.<br /><br />' .
40 '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 />' .
41 'If you want to apply changes selectively, <a href="Install.php?install[action]=importantActions&amp;install[context]={context}&amp;install[controller]=tool">go to Database Analyzer</a>.';
42
43 $databaseDifferences = $this->getDatabaseDifferences();
44 $updateSuggestions = $this->schemaMigrationService->getUpdateSuggestions($databaseDifferences);
45
46 return isset($updateSuggestions['change']);
47 }
48
49 /**
50 * Second step: Show tables, fields and keys to create or update
51 *
52 * @param string $inputPrefix input prefix, all names of form fields have to start with this. Append custom name in [ ... ]
53 * @return string HTML output
54 */
55 public function getUserInput($inputPrefix) {
56 $result = '';
57
58 $databaseDifferences = $this->getDatabaseDifferences();
59 $updateSuggestions = $this->schemaMigrationService->getUpdateSuggestions($databaseDifferences);
60
61 if (!isset($updateSuggestions['change'])) {
62 return $result;
63 }
64
65 $fieldsList = '
66 <p>
67 Change the following fields in tables:
68 </p>
69 <fieldset>
70 <ol class="t3-install-form-label-after">%s</ol>
71 </fieldset>';
72 $keysList = '
73 <p>
74 Change the following keys in tables:
75 </p>
76 <fieldset>
77 <ol class="t3-install-form-label-after">%s</ol>
78 </fieldset>';
79 $item = '
80 <li class="labelAfter">
81 <label><strong>%1$s</strong>: %2$s</label>
82 </li>';
83
84 $fieldItems = array();
85 $keyItems = array();
86 foreach ($databaseDifferences['diff'] as $tableName => $difference) {
87 if ($difference['fields']) {
88 $fieldNames = array();
89 foreach ($difference['fields'] as $fieldName => $sql) {
90 $fieldNames[] = $fieldName;
91 }
92 $fieldItems[] = sprintf($item, $tableName, implode(', ', $fieldNames));
93 }
94 if ($difference['keys']) {
95 $keyNames = array();
96 foreach ($difference['keys'] as $keyName => $sql) {
97 $keyNames[] = $keyName;
98 }
99 $keyItems[] = sprintf($item, $tableName, implode(', ', $keyNames));
100 }
101 }
102 if (!empty($fieldItems)) {
103 $result .= sprintf($fieldsList, implode('', $fieldItems));
104 }
105 if (!empty($keyItems)) {
106 $result .= sprintf($keysList, implode('', $keyItems));
107 }
108
109 return $result;
110 }
111
112
113 /**
114 * Performs the database update.
115 *
116 * @param array &$dbQueries Queries done in this update
117 * @param mixed &$customMessages Custom messages
118 * @return bool TRUE on success, FALSE on error
119 */
120 public function performUpdate(array &$dbQueries, &$customMessages) {
121 // First perform all add update statements to database
122 $databaseDifferences = $this->getDatabaseDifferences();
123 $updateStatements = $this->schemaMigrationService->getUpdateSuggestions($databaseDifferences);
124
125 $customMessagesArray = array();
126 foreach ((array)$updateStatements['change'] as $query) {
127 $GLOBALS['TYPO3_DB']->admin_query($query);
128 $dbQueries[] = $query;
129 if ($GLOBALS['TYPO3_DB']->sql_error()) {
130 $customMessagesArray[] = 'SQL-ERROR: ' . htmlspecialchars($GLOBALS['TYPO3_DB']->sql_error());
131 }
132 }
133
134 if (!empty($customMessagesArray)) {
135 $customMessages = 'Update process not fully processed. This can happen because of dependencies of table fields and ' .
136 'indexes. Please repeat this step! Following errors occurred:' . LF . LF . implode(LF, $customMessagesArray);
137 }
138
139 return count($customMessagesArray) === 0;
140 }
141 }