[TASK] Merge submodule extbase into core
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Classes / Updates / TceformsUpdateWizard.php
1 <?php
2 namespace TYPO3\CMS\Install\Updates;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2011-2013 Ingmar Schlecht <ingmar@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 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.
29 *
30 * @author Ingmar Schlecht <ingmar@typo3.org>
31 * @license http://www.gnu.org/copyleft/gpl.html
32 */
33 use TYPO3\CMS\Core\Utility\GeneralUtility;
34
35 /**
36 * Updates TCEform references from direct file relations to FAL.
37 */
38 class TceformsUpdateWizard extends \TYPO3\CMS\Install\Updates\AbstractUpdate {
39
40 /**
41 * @var string
42 */
43 protected $title = 'Migrate all file relations from tt_content.image and pages.media';
44
45 /**
46 * @var \TYPO3\CMS\Core\Resource\ResourceStorage
47 */
48 protected $storage;
49
50 /**
51 * @var \TYPO3\CMS\Core\Log\Logger
52 */
53 protected $logger;
54
55 public function __construct() {
56 /** @var $logManager \TYPO3\CMS\Core\Log\LogManager */
57 $logManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Log\\LogManager');
58 $this->logger = $logManager->getLogger(__CLASS__);
59 }
60
61 /**
62 * Initialize the storage repository.
63 */
64 public function init() {
65 /** @var $storageRepository \TYPO3\CMS\Core\Resource\StorageRepository */
66 $storageRepository = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\StorageRepository');
67 $storages = $storageRepository->findAll();
68 $this->storage = $storages[0];
69 }
70
71 /**
72 * Checks if an update is needed
73 *
74 * @param string &$description: The description for the update
75 * @return boolean TRUE if an update is needed, FALSE otherwise
76 */
77 public function checkForUpdate(&$description) {
78 $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.';
79 // make this wizard always available
80 return TRUE;
81 }
82
83 /**
84 * Performs the database update.
85 *
86 * @param array &$dbQueries: queries done in this update
87 * @param mixed &$customMessages: custom messages
88 * @return boolean TRUE on success, FALSE on error
89 */
90 public function performUpdate(array &$dbQueries, &$customMessages) {
91 $this->init();
92 // Function below copied from sysext/install/updates/class.tx_coreupdates_imagelink.php
93 $tables = array(
94 'tt_content' => array(
95 'image' => array(
96 'sourcePath' => 'uploads/pics/',
97 // Relative to fileadmin
98 'targetPath' => '_migrated/pics/',
99 'titleTexts' => 'titleText',
100 'captions' => 'imagecaption',
101 'links' => 'image_link',
102 'alternativeTexts' => 'altText'
103 )
104 ),
105 'pages' => array(
106 'media' => array(
107 'sourcePath' => 'uploads/media/',
108 // Relative to fileadmin
109 'targetPath' => '_migrated/media/'
110 )
111 ),
112 'pages_language_overlay' => array(
113 'media' => array(
114 'sourcePath' => 'uploads/media/',
115 // Relative to fileadmin
116 'targetPath' => '_migrated/media/'
117 )
118 )
119 );
120 // We write down the fields that were migrated. Like this: tt_content:media
121 // so you can check whether a field was already migrated
122 if (isset($GLOBALS['TYPO3_CONF_VARS']['INSTALL']['wizardDone']['Tx_Install_Updates_File_TceformsUpdateWizard'])) {
123 $finishedFields = explode(',', $GLOBALS['TYPO3_CONF_VARS']['INSTALL']['wizardDone']['Tx_Install_Updates_File_TceformsUpdateWizard']);
124 } else {
125 $finishedFields = array();
126 }
127 $result = TRUE;
128 if ($this->versionNumber >= 6000000) {
129 // @todo
130 // - for each table:
131 // - get records from table
132 // - for each record:
133 // - for each field:
134 // - migrate field
135 foreach ($tables as $table => $tableConfiguration) {
136 $fieldsToMigrate = array_keys($tableConfiguration);
137 $fieldsToGet = array();
138 // find all additional fields we should get from the database
139 foreach ($tableConfiguration as $field => $fieldConfiguration) {
140 $fieldKey = $table . ':' . $field;
141 if (array_search($fieldKey, $finishedFields) !== FALSE) {
142 // this field was already migrated
143 continue;
144 } else {
145 $finishedFields[] = $fieldKey;
146 }
147 $fieldsToGet[] = $field;
148 if (isset($fieldConfiguration['titleTexts'])) {
149 $fieldsToGet[] = $fieldConfiguration['titleTexts'];
150 }
151 if (isset($fieldConfiguration['alternativeTexts'])) {
152 $fieldsToGet[] = $fieldConfiguration['alternativeTexts'];
153 }
154 if (isset($fieldConfiguration['captions'])) {
155 $fieldsToGet[] = $fieldConfiguration['captions'];
156 }
157 if (isset($fieldConfiguration['links'])) {
158 $fieldsToGet[] = $fieldConfiguration['links'];
159 }
160 }
161 $records = $this->getRecordsFromTable($table, $fieldsToGet);
162 foreach ($records as $record) {
163 foreach ($fieldsToMigrate as $field) {
164 $dbQueries = array_merge($this->migrateField($table, $record, $field, $tableConfiguration[$field], $customMessages));
165 }
166 }
167 }
168 }
169 $finishedFields = implode(',', $finishedFields);
170 $this->markWizardAsDone($finishedFields);
171 return $result;
172 }
173
174 protected function getRecordsFromTable($table, $relationFields) {
175 $fields = implode(',', array_merge($relationFields, array('uid', 'pid')));
176 $records = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows($fields, $table, '');
177 return $records;
178 }
179
180 /**
181 * Migrates a single field.
182 *
183 * @param string $table
184 * @param array $row
185 * @param string $fieldname
186 * @param array $fieldConfiguration
187 * @param string $customMessages
188 * @return array A list of performed database queries
189 * @throws \Exception
190 */
191 protected function migrateField($table, $row, $fieldname, $fieldConfiguration, &$customMessages) {
192 $fieldItems = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $row[$fieldname], TRUE);
193 if (empty($fieldItems) || is_numeric($row[$fieldname])) {
194 return array();
195 }
196 if (isset($fieldConfiguration['titleTexts'])) {
197 $titleTextField = $fieldConfiguration['titleTexts'];
198 $titleTextContents = explode(LF, $row[$titleTextField]);
199 }
200 if (isset($fieldConfiguration['alternativeTexts'])) {
201 $alternativeTextField = $fieldConfiguration['alternativeTexts'];
202 $alternativeTextContents = explode(LF, $row[$alternativeTextField]);
203 }
204 if (isset($fieldConfiguration['captions'])) {
205 $captionField = $fieldConfiguration['captions'];
206 $captionContents = explode(LF, $row[$captionField]);
207 }
208 if (isset($fieldConfiguration['links'])) {
209 $linkField = $fieldConfiguration['links'];
210 $linkContents = explode(LF, $row[$linkField]);
211 }
212 $fileadminDirectory = rtrim($GLOBALS['TYPO3_CONF_VARS']['BE']['fileadminDir'], '/') . '/';
213 $queries = array();
214 $i = 0;
215 foreach ($fieldItems as $item) {
216 if (!PATH_site) {
217 throw new \Exception('PATH_site was undefined.');
218 }
219
220 $sourcePath = PATH_site . $fieldConfiguration['sourcePath'] . $item;
221 $targetPath = PATH_site . $fileadminDirectory . $fieldConfiguration['targetPath'] . $item;
222
223 // if the source file does not exist, we should just continue, but leave a message in the docs;
224 // ideally, the user would be informed after the update as well.
225 if (!file_exists($sourcePath)) {
226 $this->logger->notice('File ' . $fieldConfiguration['sourcePath'] . $item . ' does not exist. Reference was not migrated.', array('table' => $table, 'record' => $row, 'field' => $fieldname));
227
228 $format = 'File \'%s\' does not exist. Referencing field: %s.%d.%s. The reference was not migrated.';
229 $message = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('\TYPO3\CMS\Core\Messaging\FlashMessage',
230 sprintf($format, $fieldConfiguration['sourcePath'] . $item, $table, $row['uid'], $fieldname),
231 '', \TYPO3\CMS\Core\Messaging\FlashMessage::WARNING
232 );
233 /** @var \TYPO3\CMS\Core\Messaging\FlashMessage $message */
234 $customMessages .= '<br />' . $message->render();
235
236 continue;
237 }
238
239 if (!is_dir(dirname($targetPath))) {
240 \TYPO3\CMS\Core\Utility\GeneralUtility::mkdir_deep(dirname($targetPath));
241 }
242 rename($sourcePath, $targetPath);
243 // get the File object
244 $file = $this->storage->getFile($fieldConfiguration['targetPath'] . $item);
245 if ($file instanceof \TYPO3\CMS\Core\Resource\File) {
246 $fields = array(
247 // TODO add sorting/sorting_foreign
248 'fieldname' => $fieldname,
249 'table_local' => 'sys_file',
250 // the sys_file_reference record should always placed on the same page
251 // as the record to link to, see issue #46497
252 'pid' => ($table === 'pages' ? $row['uid'] : $row['pid']),
253 'uid_foreign' => $row['uid'],
254 'uid_local' => $file->getUid(),
255 'tablenames' => $table,
256 'crdate' => time(),
257 'tstamp' => time()
258 );
259 if (isset($titleTextField)) {
260 $fields['title'] = trim($titleTextContents[$i]);
261 }
262 if (isset($alternativeTextField)) {
263 $fields['alternative'] = trim($alternativeTextContents[$i]);
264 }
265 if (isset($captionField)) {
266 $fields['description'] = trim($captionContents[$i]);
267 }
268 if (isset($linkField)) {
269 $fields['link'] = trim($linkContents[$i]);
270 }
271 $GLOBALS['TYPO3_DB']->exec_INSERTquery('sys_file_reference', $fields);
272 $queries[] = str_replace(LF, ' ', $GLOBALS['TYPO3_DB']->debug_lastBuiltQuery);
273 ++$i;
274 }
275 }
276 // Update referencing table's original field to now contain the count of references.
277 $GLOBALS['TYPO3_DB']->exec_UPDATEquery($table, 'uid=' . $row['uid'], array($fieldname => $i));
278 $queries[] = str_replace(LF, ' ', $GLOBALS['TYPO3_DB']->debug_lastBuiltQuery);
279 return $queries;
280 }
281
282 }
283
284
285 ?>