[!!!][TASK] Rewrite install tool
[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 $tables = array(
93 'tt_content' => array(
94 'image' => array(
95 'sourcePath' => 'uploads/pics/',
96 // Relative to fileadmin
97 'targetPath' => '_migrated/pics/',
98 'titleTexts' => 'titleText',
99 'captions' => 'imagecaption',
100 'links' => 'image_link',
101 'alternativeTexts' => 'altText'
102 )
103 ),
104 'pages' => array(
105 'media' => array(
106 'sourcePath' => 'uploads/media/',
107 // Relative to fileadmin
108 'targetPath' => '_migrated/media/'
109 )
110 ),
111 'pages_language_overlay' => array(
112 'media' => array(
113 'sourcePath' => 'uploads/media/',
114 // Relative to fileadmin
115 'targetPath' => '_migrated/media/'
116 )
117 )
118 );
119 // We write down the fields that were migrated. Like this: tt_content:media
120 // so you can check whether a field was already migrated
121 if (isset($GLOBALS['TYPO3_CONF_VARS']['INSTALL']['wizardDone']['Tx_Install_Updates_File_TceformsUpdateWizard'])) {
122 $finishedFields = explode(',', $GLOBALS['TYPO3_CONF_VARS']['INSTALL']['wizardDone']['Tx_Install_Updates_File_TceformsUpdateWizard']);
123 } else {
124 $finishedFields = array();
125 }
126 $result = TRUE;
127 if ($this->versionNumber >= 6000000) {
128 // @todo
129 // - for each table:
130 // - get records from table
131 // - for each record:
132 // - for each field:
133 // - migrate field
134 foreach ($tables as $table => $tableConfiguration) {
135 $fieldsToMigrate = array_keys($tableConfiguration);
136 $fieldsToGet = array();
137 // find all additional fields we should get from the database
138 foreach ($tableConfiguration as $field => $fieldConfiguration) {
139 $fieldKey = $table . ':' . $field;
140 if (array_search($fieldKey, $finishedFields) !== FALSE) {
141 // this field was already migrated
142 continue;
143 } else {
144 $finishedFields[] = $fieldKey;
145 }
146 $fieldsToGet[] = $field;
147 if (isset($fieldConfiguration['titleTexts'])) {
148 $fieldsToGet[] = $fieldConfiguration['titleTexts'];
149 }
150 if (isset($fieldConfiguration['alternativeTexts'])) {
151 $fieldsToGet[] = $fieldConfiguration['alternativeTexts'];
152 }
153 if (isset($fieldConfiguration['captions'])) {
154 $fieldsToGet[] = $fieldConfiguration['captions'];
155 }
156 if (isset($fieldConfiguration['links'])) {
157 $fieldsToGet[] = $fieldConfiguration['links'];
158 }
159 }
160 $records = $this->getRecordsFromTable($table, $fieldsToGet);
161 foreach ($records as $record) {
162 foreach ($fieldsToMigrate as $field) {
163 $dbQueries = array_merge($this->migrateField($table, $record, $field, $tableConfiguration[$field], $customMessages));
164 }
165 }
166 }
167 }
168 $finishedFields = implode(',', $finishedFields);
169 $this->markWizardAsDone($finishedFields);
170 return $result;
171 }
172
173 protected function getRecordsFromTable($table, $relationFields) {
174 $fields = implode(',', array_merge($relationFields, array('uid', 'pid')));
175 $records = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows($fields, $table, '');
176 return $records;
177 }
178
179 /**
180 * Migrates a single field.
181 *
182 * @param string $table
183 * @param array $row
184 * @param string $fieldname
185 * @param array $fieldConfiguration
186 * @param string $customMessages
187 * @return array A list of performed database queries
188 * @throws \Exception
189 */
190 protected function migrateField($table, $row, $fieldname, $fieldConfiguration, &$customMessages) {
191 $fieldItems = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $row[$fieldname], TRUE);
192 if (empty($fieldItems) || is_numeric($row[$fieldname])) {
193 return array();
194 }
195 if (isset($fieldConfiguration['titleTexts'])) {
196 $titleTextField = $fieldConfiguration['titleTexts'];
197 $titleTextContents = explode(LF, $row[$titleTextField]);
198 }
199 if (isset($fieldConfiguration['alternativeTexts'])) {
200 $alternativeTextField = $fieldConfiguration['alternativeTexts'];
201 $alternativeTextContents = explode(LF, $row[$alternativeTextField]);
202 }
203 if (isset($fieldConfiguration['captions'])) {
204 $captionField = $fieldConfiguration['captions'];
205 $captionContents = explode(LF, $row[$captionField]);
206 }
207 if (isset($fieldConfiguration['links'])) {
208 $linkField = $fieldConfiguration['links'];
209 $linkContents = explode(LF, $row[$linkField]);
210 }
211 $fileadminDirectory = rtrim($GLOBALS['TYPO3_CONF_VARS']['BE']['fileadminDir'], '/') . '/';
212 $queries = array();
213 $i = 0;
214 foreach ($fieldItems as $item) {
215 if (!PATH_site) {
216 throw new \Exception('PATH_site was undefined.');
217 }
218
219 $sourcePath = PATH_site . $fieldConfiguration['sourcePath'] . $item;
220 $targetPath = PATH_site . $fileadminDirectory . $fieldConfiguration['targetPath'] . $item;
221
222 // if the source file does not exist, we should just continue, but leave a message in the docs;
223 // ideally, the user would be informed after the update as well.
224 if (!file_exists($sourcePath)) {
225 $this->logger->notice('File ' . $fieldConfiguration['sourcePath'] . $item . ' does not exist. Reference was not migrated.', array('table' => $table, 'record' => $row, 'field' => $fieldname));
226
227 $format = 'File \'%s\' does not exist. Referencing field: %s.%d.%s. The reference was not migrated.';
228 $message = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('\TYPO3\CMS\Core\Messaging\FlashMessage',
229 sprintf($format, $fieldConfiguration['sourcePath'] . $item, $table, $row['uid'], $fieldname),
230 '', \TYPO3\CMS\Core\Messaging\FlashMessage::WARNING
231 );
232 /** @var \TYPO3\CMS\Core\Messaging\FlashMessage $message */
233 $customMessages .= '<br />' . $message->render();
234
235 continue;
236 }
237
238 if (!is_dir(dirname($targetPath))) {
239 \TYPO3\CMS\Core\Utility\GeneralUtility::mkdir_deep(dirname($targetPath));
240 }
241 rename($sourcePath, $targetPath);
242 // get the File object
243 $file = $this->storage->getFile($fieldConfiguration['targetPath'] . $item);
244 if ($file instanceof \TYPO3\CMS\Core\Resource\File) {
245 $fields = array(
246 // TODO add sorting/sorting_foreign
247 'fieldname' => $fieldname,
248 'table_local' => 'sys_file',
249 // the sys_file_reference record should always placed on the same page
250 // as the record to link to, see issue #46497
251 'pid' => ($table === 'pages' ? $row['uid'] : $row['pid']),
252 'uid_foreign' => $row['uid'],
253 'uid_local' => $file->getUid(),
254 'tablenames' => $table,
255 'crdate' => time(),
256 'tstamp' => time()
257 );
258 if (isset($titleTextField)) {
259 $fields['title'] = trim($titleTextContents[$i]);
260 }
261 if (isset($alternativeTextField)) {
262 $fields['alternative'] = trim($alternativeTextContents[$i]);
263 }
264 if (isset($captionField)) {
265 $fields['description'] = trim($captionContents[$i]);
266 }
267 if (isset($linkField)) {
268 $fields['link'] = trim($linkContents[$i]);
269 }
270 $GLOBALS['TYPO3_DB']->exec_INSERTquery('sys_file_reference', $fields);
271 $queries[] = str_replace(LF, ' ', $GLOBALS['TYPO3_DB']->debug_lastBuiltQuery);
272 ++$i;
273 }
274 }
275 // Update referencing table's original field to now contain the count of references.
276 $GLOBALS['TYPO3_DB']->exec_UPDATEquery($table, 'uid=' . $row['uid'], array($fieldname => $i));
277 $queries[] = str_replace(LF, ' ', $GLOBALS['TYPO3_DB']->debug_lastBuiltQuery);
278 return $queries;
279 }
280
281 }
282
283
284 ?>