Last active
December 18, 2015 22:59
-
-
Save fsuter/5858132 to your computer and use it in GitHub Desktop.
Upgrading a RTE-enabled field from a custom table with a RTE to FAL. Note that this is keyed to a particular upload path (old and new). Change the class constants to your needs.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/*************************************************************** | |
* Copyright notice | |
* | |
* (c) 2013 Francois Suter <[email protected]> | |
* All rights reserved | |
* | |
* This script is part of the Typo3 project. The Typo3 project is | |
* free software; you can redistribute it and/or modify | |
* it under the terms of the GNU General Public License as published by | |
* the Free Software Foundation; either version 2 of the License, or | |
* (at your option) any later version. | |
* | |
* The GNU General Public License can be found at | |
* http://www.gnu.org/copyleft/gpl.html. | |
* | |
* This script is distributed in the hope that it will be useful, | |
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
* GNU General Public License for more details. | |
* | |
* This copyright notice MUST APPEAR in all copies of the script! | |
***************************************************************/ | |
/** | |
* Class for updating images in RTE field | |
* | |
* @author Francois Suter <[email protected]> | |
* @package TYPO3 | |
* @subpackage tx_latourdoctors | |
*/ | |
class ext_update { | |
const ORIGINAL_DIRECTORY = 'uploads/tx_latourdoctors/rte/'; | |
const TARGET_DIRECTORY = '/_migrated/RTE/latourdoctors/'; | |
/** | |
* The default storage | |
* @var \TYPO3\CMS\Core\Resource\ResourceStorage | |
*/ | |
protected $storage; | |
/** | |
* Performs the updates of RTE images to FAL, based on existing references | |
* | |
* @return string Result of the migration script | |
*/ | |
public function main() { | |
$customMessages = ''; | |
$queries = array(); | |
// Initialize the storage repository | |
/** @var $storageRepository \TYPO3\CMS\Core\Resource\StorageRepository */ | |
$storageRepository = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\StorageRepository'); | |
$storages = $storageRepository->findAll(); | |
$this->storage = $storages[0]; | |
/** @var $fileRepository \TYPO3\CMS\Core\Resource\FileRepository */ | |
$fileRepository = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\FileRepository'); | |
// Create the directory, if necessary | |
$fileadminDirectory = rtrim($GLOBALS['TYPO3_CONF_VARS']['BE']['fileadminDir'], '/'); | |
$targetDirectory = self::TARGET_DIRECTORY; | |
$targetFolder = $this->storage->createFolder($targetDirectory); | |
// Perform update by looping on all existing references | |
$oldRecords = $this->findMagicImagesInOldLocation(); | |
foreach ($oldRecords as $refRecord) { | |
// is usually uploads/RTE_magicC_123423324.png.png | |
$sourceFileName = $refRecord['ref_string']; | |
// absolute path/filename | |
$fullSourceFileName = PATH_site . $refRecord['ref_string']; | |
$targetFileName = $targetDirectory . \TYPO3\CMS\Core\Utility\PathUtility::basename($refRecord['ref_string']); | |
// if the source file does not exist, we should just continue, but leave a message in the docs; | |
// ideally, the user would be informed after the update as well. | |
if (!file_exists(PATH_site . $sourceFileName)) { | |
$format = 'File \'%s\' does not exist. Referencing field: %s.%d.%s. The reference was not migrated.'; | |
$message = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('\TYPO3\CMS\Core\Messaging\FlashMessage', | |
sprintf($format, $sourceFileName, $refRecord['tablename'], $refRecord['recuid'], $refRecord['field']), | |
'', \TYPO3\CMS\Core\Messaging\FlashMessage::WARNING | |
); | |
/** @var \TYPO3\CMS\Core\Messaging\FlashMessage $message */ | |
$customMessages .= '<br />' . $message->render(); | |
continue; | |
} | |
// Check if the file is already known by FAL, if not add it | |
$file = $this->storage->getFile($targetFileName); | |
if (!($file instanceof \TYPO3\CMS\Core\Resource\File)) { | |
// Move the file to the storage and index it (indexing creates the sys_file entry) | |
$file = $this->storage->addFile($fullSourceFileName, $targetFolder); | |
$fileRepository->addToIndex($file); | |
} | |
if ($file instanceof \TYPO3\CMS\Core\Resource\File) { | |
// and now update the referencing field | |
$targetFieldName = $refRecord['field']; | |
$targetRecord = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow( | |
'uid, ' . $targetFieldName, | |
$refRecord['tablename'], | |
'uid = ' . intval($refRecord['recuid']) | |
); | |
if ($targetRecord) { | |
// replace the old filename with the new one, and update the corresponding record | |
$targetRecord[$targetFieldName] = str_replace($sourceFileName, $fileadminDirectory . $targetFileName, $targetRecord[$targetFieldName]); | |
$GLOBALS['TYPO3_DB']->exec_UPDATEquery( | |
$refRecord['tablename'], | |
'uid = ' . intval($refRecord['recuid']), | |
array($targetFieldName => $targetRecord[$targetFieldName]) | |
); | |
$queries[] = str_replace(LF, ' ', $GLOBALS['TYPO3_DB']->debug_lastBuiltQuery); | |
// finally, update the sys_refindex table as well | |
$GLOBALS['TYPO3_DB']->exec_UPDATEquery( | |
'sys_refindex', | |
'hash = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($refRecord['hash'], 'sys_refindex'), | |
array( | |
'ref_table' => 'sys_file', | |
'ref_uid' => $file->getUid(), | |
'ref_string' => $fileadminDirectory . $targetFileName | |
) | |
); | |
$queries[] = str_replace(LF, ' ', $GLOBALS['TYPO3_DB']->debug_lastBuiltQuery); | |
} | |
} | |
} | |
$content = ''; | |
// Display only the count of queries (and not all the queries, since that may be a lot, and containing much | |
// content too. | |
if (count($queries) > 0) { | |
$content .= sprintf('%d queries were performed', count($queries)); | |
} | |
$content .= '<hr>' . $customMessages; | |
return $content; | |
} | |
/** | |
* Go through the soft refindex and find all occurrences where the old filename | |
* is still written in the ref_string | |
* | |
* @return array Entries from sys_refindex | |
*/ | |
protected function findMagicImagesInOldLocation() { | |
$records = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows( | |
'hash, tablename, recuid, field, ref_table, ref_uid, ref_string', | |
'sys_refindex', | |
'ref_string LIKE "' . $GLOBALS['TYPO3_DB']->escapeStrForLike(self::ORIGINAL_DIRECTORY, 'sys_refindex') . '%"', | |
'', | |
'ref_string ASC' | |
); | |
return $records; | |
} | |
/** | |
* Is update needed? Always TRUE | |
* | |
* @param string $what | |
* @return bool | |
*/ | |
public function access($what = 'all') { | |
return TRUE; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment