[BUGFIX] SQL error in install tool if no root page available
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Classes / Updates / CscSplitUpdate.php
1 <?php
2 namespace TYPO3\CMS\Install\Updates;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 1999-2013 Susanne Moog <typo3@susanne-moog.de>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the textfile GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29
30 /**
31 * Contains the update class for the split of css styled content templates. Used by the update wizard in the install tool.
32 *
33 * @author Susanne Moog <typo3@susanne-moog.de>
34 */
35 class CscSplitUpdate extends AbstractUpdate {
36
37 /**
38 * @var string
39 */
40 protected $title = 'Split TypoScript Templates from CSS Styled Content';
41
42 /**
43 * Function which checks if update is needed. Called in the beginning of an update process.
44 *
45 * @param string &$description Pointer to description for the update
46 * @return boolean TRUE if update is needs to be performed, FALSE otherwise.
47 */
48 public function checkForUpdate(&$description) {
49 $templates = $this->getTemplatesWithCsc($dbQueries, $customMessages);
50 $templates = $this->findUpdateableTemplatesWithCsc($templates);
51 if (count($templates)) {
52 $description = '<p>Run this wizard if you use CSS styled content in your templates, as the inclusion of the static templates changed. </p>' . '<p>You are currently using CSS styled content in <strong>' . count($templates) . '&nbsp;templates</strong> (including deleted and hidden),' . ' so if you did not run this wizard before, <strong>do it now</strong>.</p>' . '<p>The wizard will automatically choose the right template according to your compatibility version. So if you want to ' . 'change the rendering back to an older version, you will have to use the changeCompatibilityVersion wizard above ' . 'first, and then return back to this one.</p>';
53 return TRUE;
54 }
55 return FALSE;
56 }
57
58 /**
59 * Performs the update itself
60 *
61 * @param array &$dbQueries Pointer where to insert all DB queries made, so they can be shown to the user if wanted
62 * @param string &$customMessages Pointer to output custom messages
63 * @return boolean TRUE if update succeeded, FALSE otherwise
64 */
65 public function performUpdate(array &$dbQueries, &$customMessages) {
66 $templates = $this->getTemplatesWithCsc($dbQueries, $customMessages);
67 $templates = $this->findUpdateableTemplatesWithCsc($templates);
68 $this->updateCscTemplates($templates, $dbQueries, $customMessages);
69 if ($customMessages) {
70 return FALSE;
71 } else {
72 return TRUE;
73 }
74 }
75
76 /**
77 * Gets the templates that include the static css styled content template
78 *
79 * @param array &$dbQueries Pointer where to insert all DB queries made, so they can be shown to the user if wanted
80 * @param string &$customMessages Pointer to output custom messages
81 * @return array uid and inclusion string for the templates, that include csc
82 */
83 protected function getTemplatesWithCsc(&$dbQueries, &$customMessages) {
84 $fields = 'uid, include_static_file';
85 $table = 'sys_template';
86 $where = 'include_static_file LIKE "%EXT:css_styled_content/static/%"';
87 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery($fields, $table, $where);
88 $dbQueries[] = str_replace(chr(10), ' ', $GLOBALS['TYPO3_DB']->debug_lastBuiltQuery);
89 if ($GLOBALS['TYPO3_DB']->sql_error()) {
90 $customMessages = 'SQL-ERROR: ' . htmlspecialchars($GLOBALS['TYPO3_DB']->sql_error());
91 }
92 $templates = array();
93 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
94 $templates[] = $row;
95 }
96 return $templates;
97 }
98
99 /**
100 * Take a list of templates and filter them if they need an update or not
101 *
102 * @param array $allTemplates uid and inclusion string for the templates, that include csc
103 * @return array uid and inclusion string for the templates, that include csc and need an update
104 */
105 protected function findUpdateableTemplatesWithCsc($allTemplates) {
106 $compatVersion = \TYPO3\CMS\Core\Utility\VersionNumberUtility::convertVersionNumberToInteger($GLOBALS['TYPO3_CONF_VARS']['SYS']['compat_version']);
107 $currentVersion = \TYPO3\CMS\Core\Utility\VersionNumberUtility::convertVersionNumberToInteger(TYPO3_branch);
108 $templatesCount = count($allTemplates);
109 $updateableTemplates = array();
110 for ($i = 0; $i < $templatesCount; $i++) {
111 $templateNeedsUpdate = FALSE;
112 $includedTemplates = explode(',', $allTemplates[$i]['include_static_file']);
113 $includedTemplatesCount = count($includedTemplates);
114 // loop through every entry in the "include static file"
115 for ($j = 0; $j < $includedTemplatesCount; $j++) {
116 if (strpos($includedTemplates[$j], 'css_styled_content') !== FALSE) {
117 if ($compatVersion <= \TYPO3\CMS\Core\Utility\VersionNumberUtility::convertVersionNumberToInteger('3.8')) {
118 if ($includedTemplates[$j] != 'EXT:css_styled_content/static/v3.8/') {
119 $includedTemplates[$j] = 'EXT:css_styled_content/static/v3.8/';
120 $templateNeedsUpdate = TRUE;
121 }
122 } elseif ($compatVersion <= \TYPO3\CMS\Core\Utility\VersionNumberUtility::convertVersionNumberToInteger('4.1')) {
123 if ($includedTemplates[$j] != 'EXT:css_styled_content/static/v3.9/') {
124 $includedTemplates[$j] = 'EXT:css_styled_content/static/v3.9/';
125 $templateNeedsUpdate = TRUE;
126 }
127 } elseif ($compatVersion <= \TYPO3\CMS\Core\Utility\VersionNumberUtility::convertVersionNumberToInteger('4.2')) {
128 if ($includedTemplates[$j] != 'EXT:css_styled_content/static/v4.2/') {
129 $includedTemplates[$j] = 'EXT:css_styled_content/static/v4.2/';
130 $templateNeedsUpdate = TRUE;
131 }
132 } elseif ($compatVersion <= \TYPO3\CMS\Core\Utility\VersionNumberUtility::convertVersionNumberToInteger('4.3')) {
133 if ($includedTemplates[$j] != 'EXT:css_styled_content/static/v4.3/') {
134 $includedTemplates[$j] = 'EXT:css_styled_content/static/v4.3/';
135 $templateNeedsUpdate = TRUE;
136 }
137 } elseif ($compatVersion <= \TYPO3\CMS\Core\Utility\VersionNumberUtility::convertVersionNumberToInteger('4.4')) {
138 if ($includedTemplates[$j] != 'EXT:css_styled_content/static/v4.4/') {
139 $includedTemplates[$j] = 'EXT:css_styled_content/static/v4.4/';
140 $templateNeedsUpdate = TRUE;
141 }
142 } elseif ($compatVersion <= \TYPO3\CMS\Core\Utility\VersionNumberUtility::convertVersionNumberToInteger('4.5')) {
143 if ($includedTemplates[$j] != 'EXT:css_styled_content/static/v4.5/') {
144 $includedTemplates[$j] = 'EXT:css_styled_content/static/v4.5/';
145 $templateNeedsUpdate = TRUE;
146 }
147 } elseif ($compatVersion === $currentVersion || $compatVersion > '4.6') {
148 if ($includedTemplates[$j] != 'EXT:css_styled_content/static/') {
149 $includedTemplates[$j] = 'EXT:css_styled_content/static/';
150 $templateNeedsUpdate = TRUE;
151 }
152 }
153 }
154 }
155 $allTemplates[$i]['include_static_file'] = implode(',', $includedTemplates);
156 if ($templateNeedsUpdate) {
157 $updateableTemplates[] = $allTemplates[$i];
158 }
159 }
160 return $updateableTemplates;
161 }
162
163 /**
164 * updates the template records to include the new css styled content templates, according to the current compat version
165 *
166 * @param array $templates Template records to update, fetched by getTemplates() and filtered by
167 * @param array &$dbQueries Pointer where to insert all DB queries made, so they can be shown to the user if wanted
168 * @param string &$customMessages Pointer to output custom messages
169 */
170 protected function updateCscTemplates($templates, &$dbQueries, &$customMessages) {
171 foreach ($templates as $template) {
172 $table = 'sys_template';
173 $where = 'uid =' . $template['uid'];
174 $field_values = array(
175 'include_static_file' => $template['include_static_file']
176 );
177 $GLOBALS['TYPO3_DB']->exec_UPDATEquery($table, $where, $field_values);
178 $dbQueries[] = str_replace(chr(10), ' ', $GLOBALS['TYPO3_DB']->debug_lastBuiltQuery);
179 if ($GLOBALS['TYPO3_DB']->sql_error()) {
180 $customMessages = 'SQL-ERROR: ' . htmlspecialchars($GLOBALS['TYPO3_DB']->sql_error());
181 }
182 }
183 }
184
185 }
186
187 ?>