Last active
May 25, 2016 17:24
-
-
Save mhuber84/bfc3e80ed3a9976ca0bc to your computer and use it in GitHub Desktop.
Upgrade Images to FAL-Images in EXT:ws_flexslider 1.4
This file contains hidden or 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 | |
namespace WapplerSystems\WsFlexslider; | |
/** | |
* Class for updating ws_flexslider to FAL | |
* | |
* @author Marco Huber <[email protected] | |
* @package TYPO3 | |
* @subpackage tx_wsflexslider | |
*/ | |
class ext_update | |
{ | |
/** | |
* Main function, returning the HTML content of the module | |
* | |
* @return string HTML | |
*/ | |
public function main() | |
{ | |
$content = ''; | |
$content .= $this->updateImagesToFal(); | |
//\TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($GLOBALS, __FILE__ . " " . __LINE__); | |
if (strlen($content) == 0) { | |
$content = 'Nothing to do.'; | |
} | |
return $content; | |
} | |
/** | |
* Checks how many rows are found and returns true if there are any | |
* | |
* @return boolean true if user have access, otherwise false | |
*/ | |
public function access() | |
{ | |
/** @var \WapplerSystems\WsFlexslider\Updates\ImagesToFalUpdateWizard $updateObject */ | |
$imageToFalUpdateObject = \TYPO3\CMS\Core\Utility\GeneralUtility::getUserObj(\WapplerSystems\WsFlexslider\Updates\ImagesToFalUpdateWizard::class); | |
// We cannot update before the extension is installed: required tables are not yet in TCA | |
if (\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('ws_flexslider') | |
&& $imageToFalUpdateObject->shouldRenderWizard()) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
protected function updateImagesToFal() | |
{ | |
$content = ''; | |
/** @var \WapplerSystems\WsFlexslider\Updates\ImagesToFalUpdateWizard $updateObject */ | |
$updateObject = \TYPO3\CMS\Core\Utility\GeneralUtility::getUserObj(\WapplerSystems\WsFlexslider\Updates\ImagesToFalUpdateWizard::class); | |
if ($updateObject->shouldRenderWizard()) { | |
$content .= '<h2>Images to FAL update wizard</h2>'; | |
if (\TYPO3\CMS\Core\Utility\GeneralUtility::_GP('performImagesToFalUpdate')) { | |
// Both variables are used by reference in performUpdate() | |
$customOutput = ''; | |
$databaseQueries = []; | |
if ($updateObject->performUpdate($databaseQueries, $customOutput)) { | |
/** @var \TYPO3\CMS\Core\Messaging\FlashMessage $message */ | |
$message = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance( | |
\TYPO3\CMS\Core\Messaging\FlashMessage::class, | |
'', | |
'Update successful!', | |
\TYPO3\CMS\Core\Messaging\FlashMessage::OK | |
); | |
$content .= $message->render(); | |
} else { | |
/** @var \TYPO3\CMS\Core\Messaging\FlashMessage $message */ | |
$message = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance( | |
\TYPO3\CMS\Core\Messaging\FlashMessage::class, | |
'', | |
'Update failed!', | |
\TYPO3\CMS\Core\Messaging\FlashMessage::OK | |
); | |
if ($customOutput) { | |
$message->setMessage($customOutput); | |
} | |
$content .= $message->render(); | |
} | |
} else { | |
// $explanation is changed by reference in upgrade objects! | |
$explanation = ''; | |
$updateObject->checkForUpdate($explanation); | |
$content .= $explanation; | |
/** @var \TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder $uriBuilder */ | |
$uriBuilder = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder::class); | |
$uriBuilder->setAddQueryString(true); | |
$uriBuilder->setAddQueryStringMethod('GET'); | |
$uriBuilder->setArguments(array('performImagesToFalUpdate' => true)); | |
$content .= '<p><a href="' . $uriBuilder->buildBackendUri() . '">-> Start wizard!</a></p>'; | |
} | |
} | |
return $content; | |
} | |
} | |
if (defined('TYPO3_MODE') && $GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/ws_flexslider/class.ext_update.php']) { | |
include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/ws_flexslider/class.ext_update.php']); | |
} | |
?> |
This file contains hidden or 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 | |
namespace WapplerSystems\WsFlexslider\Updates; | |
/** | |
* Upgrade wizard that moves all images (usually in uploads/tx_wsflexslider) | |
* to the default storage (usually fileadmin/_migrated/tx_wsflexslider/) | |
* and also updates the according fields (e.g. tx_wsflexslider_domain_model_image:123:image) with the new string, and updates | |
* the softreference index | |
* Based on \TYPO3\CMS\Install\Updates\RteMagicImagesUpdateWizard | |
* | |
* @author Marco Huber <[email protected]> | |
*/ | |
class ImagesToFalUpdateWizard extends \TYPO3\CMS\Install\Updates\AbstractUpdate | |
{ | |
/** | |
* Title of the update wizard | |
* | |
* @var string | |
*/ | |
protected $title = 'Migrate all images from uploads/tx_wsflexslider/* to fileadmin/_migrated/tx_wsflexslider/'; | |
/** | |
* @var \TYPO3\CMS\Core\Database\DatabaseConnection | |
*/ | |
protected $db; | |
/** | |
* The default storage | |
* | |
* @var \TYPO3\CMS\Core\Resource\ResourceStorage | |
*/ | |
protected $storage; | |
/** | |
* The old location of the file name, e.g. "uploads/tx_wsflexslider/" | |
* | |
* @var string | |
*/ | |
protected $oldPrefix = null; | |
/** | |
* @var \TYPO3\CMS\Core\Log\Logger | |
*/ | |
protected $logger; | |
/** | |
* Constructor | |
*/ | |
public function __construct() | |
{ | |
/** @var $logManager \TYPO3\CMS\Core\Log\LogManager */ | |
$logManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Log\\LogManager'); | |
$this->db = $GLOBALS['TYPO3_DB']; | |
$this->logger = $logManager->getLogger(__CLASS__); | |
$this->oldPrefix = 'uploads/tx_wsflexslider/'; | |
} | |
/** | |
* Initialize the storage repository. | |
* | |
* @return void | |
*/ | |
public function init() | |
{ | |
/** @var $storageRepository \TYPO3\CMS\Core\Resource\StorageRepository */ | |
$storageRepository = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\StorageRepository::class); | |
$storages = $storageRepository->findAll(); | |
$this->storage = $storages[0]; | |
} | |
/** | |
* Checks if an update is needed | |
* | |
* @param string $description The description for the update | |
* @return boolean TRUE if an update is needed, FALSE otherwise | |
*/ | |
public function checkForUpdate(&$description) | |
{ | |
$description = '<p>This update wizard goes through all images, located in "' . htmlspecialchars($this->oldPrefix) . '", and moves the files to fileadmin/_migrated/tx_wsflexslider/.'; | |
$description .= '<br />It also moves the files from uploads/tx_wsflexslider/ to the fileadmin/_migrated/tx_wsflexslider/ path.</p>'; | |
// Issue warning about sys_refindex needing to be up to date | |
/** @var \TYPO3\CMS\Core\Messaging\FlashMessage $message */ | |
$message = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance( | |
\TYPO3\CMS\Core\Messaging\FlashMessage::class, | |
'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.', | |
'Updating the reference index', | |
\TYPO3\CMS\Core\Messaging\FlashMessage::WARNING | |
); | |
$description .= $message->render(); | |
// Wizard is only available if oldPrefix set | |
if ($this->oldPrefix) { | |
$oldRecords = $this->findImagesInOldLocation(); | |
if (count($oldRecords) > 0) { | |
$description .= '<p>There are currently <strong>' . count($oldRecords) . '</strong> images in the old directory.</p>'; | |
return true; | |
} | |
} | |
// Disable the update wizard if there are no old images | |
return false; | |
} | |
/** | |
* Performs the database update. | |
* | |
* @param array $dbQueries queries done in this update | |
* @param mixed $customMessages custom messages | |
* @return boolean TRUE on success, FALSE on error | |
*/ | |
public function performUpdate(array &$dbQueries, &$customMessages) | |
{ | |
$this->init(); | |
if (!PATH_site) { | |
throw new \Exception('PATH_site was undefined.'); | |
} | |
$fileadminDirectory = rtrim($GLOBALS['TYPO3_CONF_VARS']['BE']['fileadminDir'], '/'); | |
$targetDirectory = '/_migrated/tx_wsflexslider/'; | |
$fullTargetDirectory = PATH_site . $fileadminDirectory . $targetDirectory; | |
// Create the directory, if necessary | |
if (!is_dir($fullTargetDirectory)) { | |
\TYPO3\CMS\Core\Utility\GeneralUtility::mkdir_deep($fullTargetDirectory); | |
} | |
$oldRecords = $this->findImagesInOldLocation(); | |
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']); | |
// Full directory | |
$fullTargetFileName = $fullTargetDirectory . \TYPO3\CMS\Core\Utility\PathUtility::basename($refRecord['ref_string']); | |
// maybe the file has been moved previously | |
if (!file_exists($fullTargetFileName)) { | |
// 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)) { | |
$this->logger->notice('File ' . $sourceFileName . ' does not exist. Reference was not migrated.', | |
array()); | |
$format = 'File \'%s\' does not exist. Referencing field: %s.%d.%s. The reference was not migrated.'; | |
$message = sprintf($format, $sourceFileName, $refRecord['tablename'], $refRecord['recuid'], | |
$refRecord['field']); | |
$customMessages .= PHP_EOL . $message; | |
continue; | |
} | |
rename($fullSourceFileName, $fullTargetFileName); | |
} | |
// Get the File object | |
$file = $this->storage->getFile($targetFileName); | |
if ($file instanceof \TYPO3\CMS\Core\Resource\File) { | |
// And now update the referencing field | |
$targetRecord = $this->db->exec_SELECTgetSingleRow( | |
'*', | |
$refRecord['tablename'], | |
'uid=' . (int)$refRecord['recuid'] | |
); | |
if ($targetRecord) { | |
// Update the record | |
$this->db->exec_UPDATEquery( | |
$refRecord['tablename'], | |
'uid=' . (int)$refRecord['recuid'], | |
array($refRecord['field'] => 1) | |
); | |
$queries[] = str_replace(LF, ' ', $this->db->debug_lastBuiltQuery); | |
// Insert the sys_file_reference | |
$this->db->exec_INSERTquery( | |
'sys_file_reference', | |
array( | |
'pid' => $targetRecord['pid'], | |
'tstamp' => $targetRecord['tstamp'], | |
'crdate' => $targetRecord['crdate'], | |
'cruser_id' => $targetRecord['cruser_id'], | |
'uid_local' => (int)$file->getUid(), | |
'uid_foreign' => $targetRecord['uid'], | |
'tablenames' => $refRecord['tablename'], | |
'fieldname' => $refRecord['field'], | |
'table_local' => 'sys_file', | |
) | |
); | |
$queries[] = str_replace(LF, ' ', $this->db->debug_lastBuiltQuery); | |
/** @var $refIndexObj \TYPO3\CMS\Core\Database\ReferenceIndex */ | |
$refIndexObj = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Database\\ReferenceIndex'); | |
if (\TYPO3\CMS\Backend\Utility\BackendUtility::isTableWorkspaceEnabled($refRecord['tablename'])) { | |
$refIndexObj->setWorkspaceId($GLOBALS['BE_USER']->workspace); | |
} | |
$refIndexObj->updateRefIndexTable($refRecord['tablename'], (int)$refRecord['recuid']); | |
$queries[] = str_replace(LF, ' ', $this->db->debug_lastBuiltQuery); | |
if (\TYPO3\CMS\Backend\Utility\BackendUtility::isTableWorkspaceEnabled('sys_file')) { | |
$refIndexObj->setWorkspaceId($GLOBALS['BE_USER']->workspace); | |
} | |
$refIndexObj->updateRefIndexTable('sys_file', (int)$file->getUid()); | |
$queries[] = str_replace(LF, ' ', $this->db->debug_lastBuiltQuery); | |
} | |
} | |
} | |
return true; | |
} | |
/** | |
* Go through the soft refindex and find all occurences where the old filename | |
* is still written in the ref_string | |
* | |
* @return array Entries from sys_refindex | |
*/ | |
protected function findImagesInOldLocation() | |
{ | |
$records = $this->db->exec_SELECTgetRows( | |
'hash, tablename, recuid, field, ref_table, ref_uid, ref_string', | |
'sys_refindex', | |
'ref_string LIKE ' . $this->db->fullQuoteStr($this->db->escapeStrForLike($this->oldPrefix, | |
'sys_refindex') . '%', 'sys_refindex'), | |
'', | |
'ref_string ASC' | |
); | |
return $records; | |
} | |
} |
If you update to v 1.5 you first have to update to 1.4, run the skript above and then update to 1.5 and execute this SQL query UPDATE
sys_file_referenceSET
fieldname= 'fal_image' WHERE
tablenames= 'tx_wsflexslider_domain_model_image';
and run the cli refindex updater: typo3/cli_dispatch.phpsh lowlevel_refindex -e
Installed 1.4.0, inserted files, wizard icon not showing in Extmanager. Any idea?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Save class.ext_update.php in the extension's root directory and ImagesToFalUpdateWizard.php in Classes/Updates/. Then you can run the wizard in the extension manager.