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