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