ccb1ee1c01014102f2f094aa423fcb93c5ad6255
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Classes / Updates / ContentTypesToTextMediaUpdate.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 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
18
19 /**
20 * Migrate CTypes 'text', 'image' and 'textpic' to 'textmedia' for extension 'frontend'
21 */
22 class ContentTypesToTextMediaUpdate extends AbstractUpdate
23 {
24 /**
25 * @var string
26 */
27 protected $title = 'Migrate CTypes text, image and textpic to textmedia and move file relations from "image" to "media_references"';
28
29 /**
30 * Checks if an update is needed
31 *
32 * @param string &$description The description for the update
33 * @return bool Whether an update is needed (TRUE) or not (FALSE)
34 */
35 public function checkForUpdate(&$description)
36 {
37 $updateNeeded = true;
38
39 if (
40 !ExtensionManagementUtility::isLoaded('fluid_styled_content')
41 || ExtensionManagementUtility::isLoaded('css_styled_content')
42 || $this->isWizardDone()
43 ) {
44 $updateNeeded = false;
45 } else {
46 $nonTextmediaCount = $this->getDatabaseConnection()->exec_SELECTcountRows(
47 'uid',
48 'tt_content',
49 'CType IN (\'text\', \'image\', \'textpic\')'
50 );
51
52 if ($nonTextmediaCount === 0) {
53 $updateNeeded = false;
54 }
55 }
56
57 $description = 'The extension "fluid_styled_content" is using a new CType, textmedia, ' .
58 'which replaces the CTypes text, image and textpic. ' .
59 'This update wizard migrates these old CTypes to the new one in the database.';
60
61 return $updateNeeded;
62 }
63
64 /**
65 * Performs the database update if old CTypes are available
66 *
67 * @param array &$databaseQueries Queries done in this update
68 * @param mixed &$customMessages Custom messages
69 * @return bool
70 */
71 public function performUpdate(array &$databaseQueries, &$customMessages)
72 {
73 $databaseConnection = $this->getDatabaseConnection();
74
75 // Update 'text' records
76 $query = '
77 UPDATE tt_content
78 SET tt_content.CType = \'textmedia\'
79 WHERE
80 tt_content.CType = \'text\'
81 ';
82 $databaseConnection->sql_query($query);
83
84 // Store last executed query
85 $databaseQueries[] = str_replace(chr(10), ' ', $query);
86 // Check for errors
87 if ($databaseConnection->sql_error()) {
88 $customMessages = 'SQL-ERROR: ' . htmlspecialchars($databaseConnection->sql_error());
89 return false;
90 }
91
92 // Update 'textpic' and 'image' records
93 $query = '
94 UPDATE tt_content
95 LEFT JOIN sys_file_reference
96 ON sys_file_reference.uid_foreign = tt_content.uid
97 AND sys_file_reference.tablenames =\'tt_content\'
98 AND sys_file_reference.fieldname = \'image\'
99 SET tt_content.CType = \'textmedia\',
100 tt_content.assets = image,
101 tt_content.image = 0,
102 sys_file_reference.fieldname = \'assets\'
103 WHERE
104 tt_content.CType = \'textpic\'
105 OR tt_content.CType = \'image\'
106 ';
107 $databaseConnection->sql_query($query);
108
109 // Store last executed query
110 $databaseQueries[] = str_replace(chr(10), ' ', $query);
111 // Check for errors
112 if ($databaseConnection->sql_error()) {
113 $customMessages = 'SQL-ERROR: ' . htmlspecialchars($databaseConnection->sql_error());
114 return false;
115 }
116
117 $this->markWizardAsDone();
118
119 return true;
120 }
121 }