[TASK] Re-work/simplify copyright header in PHP files - Part 1
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Classes / Updates / RteMagicImagesUpdateWizard.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 * Upgrade wizard that moves all RTE magic images (usually in uploads/)
19 * that have the prefix RTEmagicC_* to the default storage (usually fileadmin/_migrated/RTE/)
20 * and also updates the according fields (e.g. tt_content:123:bodytext) with the new string, and updates
21 * the softreference index
22 *
23 * @author Benjamin Mack <benni@typo3.org>
24 */
25 class RteMagicImagesUpdateWizard extends AbstractUpdate {
26
27 /**
28 * Title of the update wizard
29 * @var string
30 */
31 protected $title = 'Migrate all RTE magic images from uploads/RTEmagicC_* to fileadmin/_migrated/RTE/';
32
33 /**
34 * The default storage
35 * @var \TYPO3\CMS\Core\Resource\ResourceStorage
36 */
37 protected $storage;
38
39 /**
40 * The old location of the file name, e.g. "uploads/RTEmagicC_"
41 * @var string
42 */
43 protected $oldPrefix = NULL;
44
45 /**
46 * @var \TYPO3\CMS\Core\Log\Logger
47 */
48 protected $logger;
49
50 /**
51 * Constructor
52 */
53 public function __construct() {
54 /** @var $logManager \TYPO3\CMS\Core\Log\LogManager */
55 $logManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Log\\LogManager');
56 $this->logger = $logManager->getLogger(__CLASS__);
57
58 // Set it to uploads/RTEmagicC_*
59 if (!empty($GLOBALS['TYPO3_CONF_VARS']['BE']['RTE_imageStorageDir'])) {
60 $this->oldPrefix = $GLOBALS['TYPO3_CONF_VARS']['BE']['RTE_imageStorageDir'] . 'RTEmagicC_';
61 }
62 }
63
64 /**
65 * Initialize the storage repository.
66 *
67 * @return void
68 */
69 public function init() {
70 /** @var $storageRepository \TYPO3\CMS\Core\Resource\StorageRepository */
71 $storageRepository = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\StorageRepository');
72 $storages = $storageRepository->findAll();
73 $this->storage = $storages[0];
74 }
75
76 /**
77 * Checks if an update is needed
78 *
79 * @param string $description The description for the update
80 * @return boolean TRUE if an update is needed, FALSE otherwise
81 */
82 public function checkForUpdate(&$description) {
83 $description = 'This update wizard goes through all magic images, located in "' . htmlspecialchars($this->oldPrefix) . '", and moves the files to fileadmin/_migrated/RTE/.';
84 $description .= '<br />It also moves the files from uploads/ to the fileadmin/_migrated/ path.';
85 // Issue warning about sys_refindex needing to be up to date
86 /** @var \TYPO3\CMS\Core\Messaging\FlashMessage $message */
87 $message = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
88 'TYPO3\\CMS\\Core\\Messaging\\FlashMessage',
89 'This script bases itself on the references contained in the general reference index (sys_refindex). It is strongly advised to update it before running this wizard.',
90 'Updating the reference index',
91 \TYPO3\CMS\Core\Messaging\FlashMessage::WARNING
92 );
93 $description .= $message->render();
94
95 // Wizard is only available if oldPrefix set
96 if ($this->oldPrefix) {
97 $oldRecords = $this->findMagicImagesInOldLocation();
98 if (count($oldRecords) > 0) {
99 $description .= '<br />There are currently <strong>' . count($oldRecords) . '</strong> magic images in the old directory.<br />';
100 return TRUE;
101 }
102 }
103
104 // Disable the update wizard if there are no old RTE magic images
105 return FALSE;
106 }
107
108 /**
109 * Performs the database update.
110 *
111 * @param array $dbQueries queries done in this update
112 * @param mixed $customMessages custom messages
113 * @return boolean TRUE on success, FALSE on error
114 */
115 public function performUpdate(array &$dbQueries, &$customMessages) {
116 $this->init();
117
118 if (!PATH_site) {
119 throw new \Exception('PATH_site was undefined.');
120 }
121
122 $fileadminDirectory = rtrim($GLOBALS['TYPO3_CONF_VARS']['BE']['fileadminDir'], '/');
123 $targetDirectory = '/_migrated/RTE/';
124 $fullTargetDirectory = PATH_site . $fileadminDirectory . $targetDirectory;
125
126 // Create the directory, if necessary
127 if (!is_dir($fullTargetDirectory)) {
128 \TYPO3\CMS\Core\Utility\GeneralUtility::mkdir_deep($fullTargetDirectory);
129 }
130
131
132 $oldRecords = $this->findMagicImagesInOldLocation();
133 foreach ($oldRecords as $refRecord) {
134
135 // Is usually uploads/RTE_magicC_123423324.png.png
136 $sourceFileName = $refRecord['ref_string'];
137 // Absolute path/filename
138 $fullSourceFileName = PATH_site . $refRecord['ref_string'];
139 $targetFileName = $targetDirectory . \TYPO3\CMS\Core\Utility\PathUtility::basename($refRecord['ref_string']);
140 // Full directory
141 $fullTargetFileName = $fullTargetDirectory . \TYPO3\CMS\Core\Utility\PathUtility::basename($refRecord['ref_string']);
142
143 // maybe the file has been moved previously
144 if (!file_exists($fullTargetFileName)) {
145 // If the source file does not exist, we should just continue, but leave a message in the docs;
146 // ideally, the user would be informed after the update as well.
147 if (!file_exists(PATH_site . $sourceFileName)) {
148 $this->logger->notice('File ' . $sourceFileName . ' does not exist. Reference was not migrated.', array());
149
150 $format = 'File \'%s\' does not exist. Referencing field: %s.%d.%s. The reference was not migrated.';
151 $message = sprintf($format, $sourceFileName, $refRecord['tablename'], $refRecord['recuid'], $refRecord['field']);
152 $customMessages .= PHP_EOL . $message;
153
154 continue;
155 }
156
157 rename($fullSourceFileName, $fullTargetFileName);
158 }
159
160 // Get the File object
161 $file = $this->storage->getFile($targetFileName);
162 if ($file instanceof \TYPO3\CMS\Core\Resource\File) {
163 // And now update the referencing field
164 $targetFieldName = $refRecord['field'];
165 $targetRecord = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow(
166 'uid, ' . $targetFieldName,
167 $refRecord['tablename'],
168 'uid=' . (int)$refRecord['recuid']
169 );
170 if ($targetRecord) {
171 // Replace the old filename with the new one, and add data-* attributes used by the RTE
172 $searchString = 'src="' . $sourceFileName . '"';
173 $replacementString = 'src="' . $fileadminDirectory . $targetFileName . '"';
174 $replacementString .= ' data-htmlarea-file-uid="' . $file->getUid() . '"';
175 $replacementString .= ' data-htmlarea-file-table="sys_file"';
176 $targetRecord[$targetFieldName] = str_replace(
177 $searchString,
178 $replacementString,
179 $targetRecord[$targetFieldName]
180 );
181 // Update the record
182 $GLOBALS['TYPO3_DB']->exec_UPDATEquery(
183 $refRecord['tablename'],
184 'uid=' . (int)$refRecord['recuid'],
185 array($targetFieldName => $targetRecord[$targetFieldName])
186 );
187 $queries[] = str_replace(LF, ' ', $GLOBALS['TYPO3_DB']->debug_lastBuiltQuery);
188
189 // Finally, update the sys_refindex table as well
190 $GLOBALS['TYPO3_DB']->exec_UPDATEquery(
191 'sys_refindex',
192 'hash=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($refRecord['hash'], 'sys_refindex'),
193 array(
194 'ref_table' => 'sys_file',
195 'softref_key' => 'rtehtmlarea_images',
196 'ref_uid' => $file->getUid(),
197 'ref_string' => $fileadminDirectory . $targetFileName
198 )
199 );
200 $queries[] = str_replace(LF, ' ', $GLOBALS['TYPO3_DB']->debug_lastBuiltQuery);
201 }
202
203 }
204 }
205
206 return TRUE;
207 }
208
209 /**
210 * Go through the soft refindex and find all occurences where the old filename
211 * is still written in the ref_string
212 *
213 * @return array Entries from sys_refindex
214 */
215 protected function findMagicImagesInOldLocation() {
216 $records = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
217 'hash, tablename, recuid, field, ref_table, ref_uid, ref_string',
218 'sys_refindex',
219 'ref_string LIKE "' . $GLOBALS['TYPO3_DB']->escapeStrForLike($this->oldPrefix, 'sys_refindex') . '%"',
220 '',
221 'ref_string ASC'
222 );
223 return $records;
224 }
225
226 }