[TASK] Migrate CTypes text, image and textpic to textmedia
[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 $updateNeeded = TRUE;
37
38 if (
39 !ExtensionManagementUtility::isLoaded('fluid_styled_content')
40 || ExtensionManagementUtility::isLoaded('css_styled_content')
41 || $this->isWizardDone()
42 ) {
43 $updateNeeded = FALSE;
44 } else {
45 $nonTextmediaCount = $this->getDatabaseConnection()->exec_SELECTcountRows(
46 'uid',
47 'tt_content',
48 'CType IN (\'text\', \'image\', \'textpic\')'
49 );
50
51 if ($nonTextmediaCount === 0) {
52 $updateNeeded = FALSE;
53 }
54 }
55
56 $description = 'The extension "fluid_styled_content" is using a new CType, textmedia, ' .
57 'which replaces the CTypes text, image and textpic. ' .
58 'This update wizard migrates these old CTypes to the new one in the database.';
59
60 return $updateNeeded;
61 }
62
63 /**
64 * Performs the database update if old CTypes are available
65 *
66 * @param array &$databaseQueries Queries done in this update
67 * @param mixed &$customMessages Custom messages
68 * @return bool
69 */
70 public function performUpdate(array &$databaseQueries, &$customMessages) {
71 $databaseConnection = $this->getDatabaseConnection();
72
73 // Update 'text' records
74 $query = '
75 UPDATE tt_content
76 SET tt_content.CType = \'textmedia\'
77 WHERE
78 tt_content.CType = \'text\'
79 ';
80 $databaseConnection->sql_query($query);
81
82 // Store last executed query
83 $databaseQueries[] = str_replace(chr(10), ' ', $query);
84 // Check for errors
85 if ($databaseConnection->sql_error()) {
86 $customMessages = 'SQL-ERROR: ' . htmlspecialchars($databaseConnection->sql_error());
87 return FALSE;
88 }
89
90 // Update 'textpic' and 'image' records
91 $query = '
92 UPDATE tt_content
93 LEFT JOIN sys_file_reference
94 ON sys_file_reference.uid_foreign = tt_content.uid
95 AND sys_file_reference.tablenames =\'tt_content\'
96 AND sys_file_reference.fieldname = \'image\'
97 SET tt_content.CType = \'textmedia\',
98 tt_content.media = image,
99 tt_content.image = \'\',
100 sys_file_reference.fieldname = \'media\'
101 WHERE
102 tt_content.CType = \'textpic\'
103 OR tt_content.CType = \'image\'
104 ';
105 $databaseConnection->sql_query($query);
106
107 // Store last executed query
108 $databaseQueries[] = str_replace(chr(10), ' ', $query);
109 // Check for errors
110 if ($databaseConnection->sql_error()) {
111 $customMessages = 'SQL-ERROR: ' . htmlspecialchars($databaseConnection->sql_error());
112 return FALSE;
113 }
114
115 $this->markWizardAsDone();
116
117 return TRUE;
118 }
119 }