2 namespace TYPO3\CMS\Install\Updates
;
4 /***************************************************************
7 * (c) 2011 Ingmar Schlecht <ingmar@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 * Upgrade wizard which goes through all files referenced in the tt_content.image filed
28 * and creates sys_file records as well as sys_file_reference records for the individual usages.
31 * @author Ingmar Schlecht <ingmar@typo3.org>
32 * @license http://www.gnu.org/copyleft/gpl.html
34 class TceformsUpdateWizard
extends \TYPO3\CMS\Install\Updates\AbstractUpdate
{
39 protected $title = 'Migrate all file relations from tt_content.image and pages.media';
42 * @var \TYPO3\CMS\Core\Resource\ResourceStorage
47 * Initialize the storage repository.
49 public function init() {
50 /** @var $storageRepository \TYPO3\CMS\Core\Resource\StorageRepository */
51 $storageRepository = \TYPO3\CMS\Core\Utility\GeneralUtility
::makeInstance('TYPO3\\CMS\\Core\\Resource\\StorageRepository');
52 $storages = $storageRepository->findAll();
53 $this->storage
= $storages[0];
57 * Checks if an update is needed
59 * @param string &$description: The description for the update
60 * @return boolean TRUE if an update is needed, FALSE otherwise
62 public function checkForUpdate(&$description) {
63 $description = 'This update wizard goes through all files that are referenced in the tt_content.image and pages.media / pages_language_overlay.media filed and adds the files to the new File Index.<br />It also moves the files from uploads/ to the fileadmin/_migrated/ path.<br /><br />This update wizard can be called multiple times in case it didn\'t finish after running once.';
64 // make this wizard always available
69 * Performs the database update.
71 * @param array &$dbQueries: queries done in this update
72 * @param mixed &$customMessages: custom messages
73 * @return boolean TRUE on success, FALSE on error
75 public function performUpdate(array &$dbQueries, &$customMessages) {
77 // Function below copied from sysext/install/updates/class.tx_coreupdates_imagelink.php
79 'tt_content' => array(
81 'sourcePath' => 'uploads/pics/',
82 // Relative to fileadmin
83 'targetPath' => '_migrated/pics/',
84 'titleTexts' => 'titleText',
85 'captions' => 'imagecaption',
86 'links' => 'image_link',
87 'alternativeTexts' => 'altText'
92 'sourcePath' => 'uploads/media/',
93 // Relative to fileadmin
94 'targetPath' => '_migrated/media/'
97 'pages_language_overlay' => array(
99 'sourcePath' => 'uploads/media/',
100 // Relative to fileadmin
101 'targetPath' => '_migrated/media/'
105 // We write down the fields that were migrated. Like this: tt_content:media
106 // so you can check whether a field was already migrated
107 if (isset($GLOBALS['TYPO3_CONF_VARS']['INSTALL']['wizardDone']['Tx_Install_Updates_File_TceformsUpdateWizard'])) {
108 $finishedFields = explode(',', $GLOBALS['TYPO3_CONF_VARS']['INSTALL']['wizardDone']['Tx_Install_Updates_File_TceformsUpdateWizard']);
110 $finishedFields = array();
113 if ($this->versionNumber
>= 6000000) {
116 // - get records from table
117 // - for each record:
120 foreach ($tables as $table => $tableConfiguration) {
121 $fieldsToMigrate = array_keys($tableConfiguration);
122 $fieldsToGet = array();
123 // find all additional fields we should get from the database
124 foreach ($tableConfiguration as $field => $fieldConfiguration) {
125 $fieldKey = $table . ':' . $field;
126 if (array_search($fieldKey, $finishedFields) !== FALSE) {
127 // this field was already migrated
130 $finishedFields[] = $fieldKey;
132 $fieldsToGet[] = $field;
133 if (isset($fieldConfiguration['titleTexts'])) {
134 $fieldsToGet[] = $fieldConfiguration['titleTexts'];
136 if (isset($fieldConfiguration['alternativeTexts'])) {
137 $fieldsToGet[] = $fieldConfiguration['alternativeTexts'];
139 if (isset($fieldConfiguration['captions'])) {
140 $fieldsToGet[] = $fieldConfiguration['captions'];
142 if (isset($fieldConfiguration['links'])) {
143 $fieldsToGet[] = $fieldConfiguration['links'];
146 $records = $this->getRecordsFromTable($table, $fieldsToGet);
147 foreach ($records as $record) {
148 foreach ($fieldsToMigrate as $field) {
149 $dbQueries = array_merge($this->migrateField($table, $record, $field, $tableConfiguration[$field]));
154 $finishedFields = implode(',', $finishedFields);
155 $this->markWizardAsDone($finishedFields);
159 protected function getRecordsFromTable($table, $relationFields) {
160 $fields = implode(',', array_merge($relationFields, array('uid', 'pid')));
161 $records = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows($fields, $table, '');
165 protected function migrateField($table, $row, $fieldname, $fieldConfiguration) {
166 $fieldItems = \TYPO3\CMS\Core\Utility\GeneralUtility
::trimExplode(',', $row[$fieldname], TRUE);
167 if (empty($fieldItems) ||
is_numeric($row[$fieldname])) {
170 if (isset($fieldConfiguration['titleTexts'])) {
171 $titleTextField = $fieldConfiguration['titleTexts'];
172 $titleTextContents = explode(LF
, $row[$titleTextField]);
174 if (isset($fieldConfiguration['alternativeTexts'])) {
175 $alternativeTextField = $fieldConfiguration['alternativeTexts'];
176 $alternativeTextContents = explode(LF
, $row[$alternativeTextField]);
178 if (isset($fieldConfiguration['captions'])) {
179 $captionField = $fieldConfiguration['captions'];
180 $captionContents = explode(LF
, $row[$captionField]);
182 if (isset($fieldConfiguration['links'])) {
183 $linkField = $fieldConfiguration['links'];
184 $linkContents = explode(LF
, $row[$linkField]);
186 $fileadminDirectory = rtrim($GLOBALS['TYPO3_CONF_VARS']['BE']['fileadminDir'], '/') . '/';
189 foreach ($fieldItems as $item) {
191 throw new \
Exception('PATH_site was undefined.');
194 $sourcePath = PATH_site
. $fieldConfiguration['sourcePath'] . $item;
195 $targetPath = PATH_site
. $fileadminDirectory . $fieldConfiguration['targetPath'] . $item;
196 if (!is_dir(dirname($targetPath))) {
197 \TYPO3\CMS\Core\Utility\GeneralUtility
::mkdir_deep(dirname($targetPath));
199 rename($sourcePath, $targetPath);
200 // get the File object
201 $file = $this->storage
->getFile($fieldConfiguration['targetPath'] . $item);
202 if ($file instanceof \TYPO3\CMS\Core\
Resource\File
) {
204 // TODO add sorting/sorting_foreign
205 'fieldname' => $fieldname,
206 'table_local' => 'sys_file',
207 'uid_foreign' => $row['uid'],
208 'uid_local' => $file->getUid(),
209 'tablenames' => $table,
213 if (isset($titleTextField)) {
214 $fields['title'] = trim($titleTextContents[$i]);
216 if (isset($alternativeTextField)) {
217 $fields['alternative'] = trim($alternativeTextContents[$i]);
219 if (isset($captionField)) {
220 $fields['description'] = trim($captionContents[$i]);
222 if (isset($linkField)) {
223 $fields['link'] = trim($linkContents[$i]);
225 $GLOBALS['TYPO3_DB']->exec_INSERTquery('sys_file_reference', $fields);
226 $queries[] = str_replace(LF
, ' ', $GLOBALS['TYPO3_DB']->debug_lastBuiltQuery
);
230 // Update referencing table's original field to now contain the count of references.
231 $GLOBALS['TYPO3_DB']->exec_UPDATEquery($table, 'uid=' . $row['uid'], array($fieldname => $i));
232 $queries[] = str_replace(LF
, ' ', $GLOBALS['TYPO3_DB']->debug_lastBuiltQuery
);