2 namespace TYPO3\CMS\Install\Updates
;
4 /***************************************************************
7 * (c) 2013 Francois Suter <francois@typo3.org>
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.
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
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.
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
27 use TYPO3\CMS\Core\Utility\GeneralUtility
;
30 * Upgrade wizard that rewrites all file links to FAL references.
32 * The content string and the reference index (sys_refindex) are updated accordingly.
34 class RteFileLinksUpdateWizard
extends AbstractUpdate
{
37 * Title of the update wizard
40 protected $title = 'Migrate all file links of RTE-enabled fields to FAL';
43 * @var string Path the to fileadmin directory
45 protected $fileAdminDir;
49 * @var \TYPO3\CMS\Core\Resource\ResourceStorage
54 * @var \TYPO3\CMS\Core\Html\RteHtmlParser
56 protected $rteHtmlParser;
59 * Count of converted links
62 protected $convertedLinkCounter = 0;
65 * Is DBAL installed or not (if not, we can use transactions)
68 protected $isDbalInstalled = FALSE;
71 * Array to store file conversion errors
74 protected $errors = array();
77 * List of update queries
80 protected $queries = array();
83 * Initialize some objects
87 public function init() {
88 $this->rteHtmlParser
= GeneralUtility
::makeInstance('TYPO3\\CMS\\Core\\Html\\RteHtmlParser');
89 /** @var $storageRepository \TYPO3\CMS\Core\Resource\StorageRepository */
90 $storageRepository = GeneralUtility
::makeInstance('TYPO3\\CMS\\Core\\Resource\\StorageRepository');
91 $storages = $storageRepository->findAll();
92 $this->storage
= $storages[0];
93 $this->fileAdminDir
= $GLOBALS['TYPO3_CONF_VARS']['BE']['fileadminDir'];
94 // Check if DBAL is installed or not
95 $this->isDbalInstalled
= \TYPO3\CMS\Core\Utility\ExtensionManagementUtility
::isLoaded('dbal');
99 * Checks if an update is needed
101 * @param string $description The description for the update
102 * @return boolean TRUE if an update is needed, FALSE otherwise
104 public function checkForUpdate(&$description) {
105 $description = 'This update wizard goes through all file links in all rich-text fields and changes them to FAL references.';
106 $description .= 'If the process times out, please run it again.';
107 // Issue warning about sys_refindex needing to be up to date
108 /** @var \TYPO3\CMS\Core\Messaging\FlashMessage $message */
109 $message = GeneralUtility
::makeInstance(
110 'TYPO3\\CMS\\Core\\Messaging\\FlashMessage',
111 '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.',
112 'Updating the reference index',
113 \TYPO3\CMS\Core\Messaging\FlashMessage
::WARNING
115 $description .= $message->render();
117 // Confirm activation only if some old-style links are found
118 $oldRecords = $this->findOldLinks();
119 if (count($oldRecords) > 0) {
120 $description .= '<br />There are currently <strong>' . count($oldRecords) . '</strong> links to update.<br />';
124 // No update needed, disable the wizard
129 * Performs the database update.
131 * @param array $dbQueries queries done in this update
132 * @param mixed $customMessages custom messages
133 * @return boolean TRUE on success, FALSE on error
135 public function performUpdate(array &$dbQueries, &$customMessages) {
138 // Make sure we have a storage
139 if (!$this->storage
) {
140 $customMessages = 'No file resource storage found';
144 // Get the references and migrate them
145 $records = $this->findOldLinks();
146 foreach ($records as $singleRecord) {
147 $this->migrateRecord($singleRecord);
149 $dbQueries = $this->queries
;
151 if (count($this->errors
) > 0) {
152 foreach ($this->errors
as $errorMessage) {
153 $message = GeneralUtility
::makeInstance(
154 'TYPO3\\CMS\\Core\\Messaging\\FlashMessage',
157 \TYPO3\CMS\Core\Messaging\FlashMessage
::WARNING
159 /** @var \TYPO3\CMS\Core\Messaging\FlashMessage $message */
160 $customMessages .= '<br />' . $message->render();
162 if ($this->convertedLinkCounter
== 0) {
163 // no links converted only missing files: UPDATE was not successful
168 if ($this->convertedLinkCounter
> 0) {
169 $customMessages = $this->convertedLinkCounter
. ' links converted.<br />' . $customMessages;
171 $customMessages .= 'No file links found';
177 * Processes each record and updates the database
179 * @param array $reference Reference to a record from sys_refindex
182 protected function migrateRecord(array $reference) {
183 // Get the current record based on the sys_refindex information
184 $record = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow(
185 'uid, ' . $reference['field'],
186 $reference['tablename'],
187 'uid = ' . $reference['recuid']
189 if ($record !== NULL) {
190 $this->convertFileLinks($reference, $record);
192 // Original record could not be found (happens if sys_refindex is not up to date), issue error
193 $this->errors
[] = 'Original record not found for reference to element ' . $reference['recuid'] . ' of table ' . $reference['tablename'] . ' in field ' . $reference['field'] . '. Not migrated.';
198 * The actual transformation of the links
199 * pretty similar to TS_links_rte in RteHtmlParser
201 * @param array $reference sys_refindex information
202 * @param array $record Original record pointed to by the sys_refindex reference
205 protected function convertFileLinks(array $reference, array $record) {
206 // First of all, try to get the referenced file. Continue only if found.
207 $fileObject = $this->fetchReferencedFile($reference['ref_string'], $reference);
208 if ($fileObject instanceof \TYPO3\CMS\Core\
Resource\AbstractFile
) {
209 // Next, match the reference path in the content to be sure it's present inside a <link> tag
210 $content = $record[$reference['field']];
211 $regularExpression = '$<(link ' . $reference['ref_string'] . ').*>$';
213 $result = preg_match($regularExpression, $content, $matches);
215 // Replace the file path with the file reference
216 $modifiedContent = str_replace(
218 'link file:' . $fileObject->getUid(),
219 $record[$reference['field']]
221 // Save the changes and stop looping
222 $this->saveChanges($modifiedContent, $reference, $fileObject);
223 $this->convertedLinkCounter++
;
225 $this->errors
[] = $reference['ref_string'] . ' not found in referenced element (uid: ' . $reference['recuid'] . ' of table ' . $reference['tablename'] . ' in field ' . $reference['field'] . '). Reference index was probably out of date.';
231 * Tries to fetch the file object corresponding to the given path.
233 * @param string $path Path to a file (starting with "fileadmin/")
234 * @param array $reference Corresponding sys_refindex entry
235 * @return null|\TYPO3\CMS\Core\Resource\FileInterface
237 protected function fetchReferencedFile($path, array $reference) {
239 if (@file_exists
(PATH_site
. '/' . $path)) {
241 $fileObject = $this->storage
->getFile(
248 } catch (\TYPO3\CMS\Core\
Resource\Exception\FileDoesNotExistException
$notFoundException) {
249 // This should really not happen, since we are testing existence of the file just before
250 $this->errors
[] = $path . ' not found (referenced in element ' . $reference['recuid'] . ' of table ' . $reference['tablename'] . ' in field ' . $reference['field'] . ')';
253 // Nothing to be done if file not found
254 $this->errors
[] = $path . ' not found (referenced in element ' . $reference['recuid'] . ' of table ' . $reference['tablename'] . ' in field ' . $reference['field'] . ')';
260 * Saves the modified content to the database and updates the sys_refindex accordingly.
262 * @param string $modifiedText Original content with the file links replaced
263 * @param array $reference sys_refindex record
264 * @param \TYPO3\CMS\Core\Resource\AbstractFile $file
267 protected function saveChanges($modifiedText, array $reference, $file) {
269 // If DBAL is not installed, we can start a transaction before saving
270 // This ensures that a possible time out doesn't break the database integrity
271 // by occurring between the two needed DB writes.
272 if (!$this->isDbalInstalled
) {
273 $GLOBALS['TYPO3_DB']->sql_query('START TRANSACTION');
276 // Save the changed field
277 $GLOBALS['TYPO3_DB']->exec_UPDATEquery(
278 $reference['tablename'],
279 'uid = ' . $reference['recuid'],
281 $reference['field'] => $modifiedText
284 $this->queries
[] = htmlspecialchars(str_replace(LF
, ' ', $GLOBALS['TYPO3_DB']->debug_lastBuiltQuery
));
286 // Finally, update the sys_refindex table as well
287 $GLOBALS['TYPO3_DB']->exec_UPDATEquery(
289 'hash = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($reference['hash'], 'sys_refindex'),
291 'ref_table' => 'sys_file',
292 'ref_uid' => $file->getUid(),
296 $this->queries
[] = str_replace(LF
, ' ', $GLOBALS['TYPO3_DB']->debug_lastBuiltQuery
);
298 // Confirm the transaction
299 if (!$this->isDbalInstalled
) {
300 $GLOBALS['TYPO3_DB']->sql_query('COMMIT');
305 * Use sys_refindex to find all links to "old" files in typolink tags.
307 * This will find any RTE-enabled field.
309 * @return array Entries from sys_refindex
311 protected function findOldLinks() {
312 $records = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
313 'hash, tablename, recuid, field, ref_table, ref_uid, ref_string',
315 'softref_key = \'typolink_tag\' AND ref_table = \'_FILE\' '