4ef1e0c323ff634497b27c7d9c191e665aa8f20e
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Classes / Updates / File / TceformsUpdateWizard.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2011 Ingmar Schlecht <ingmar@typo3.org>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25 /**
26 * Upgrade wizard which goes through all files referenced in the tt_content.image filed
27 * and creates sys_file records as well as sys_file_reference records for the individual usages.
28 *
29 * @package TYPO3
30 * @author Ingmar Schlecht <ingmar@typo3.org>
31 * @license http://www.gnu.org/copyleft/gpl.html
32 */
33 class Tx_Install_Updates_File_TceformsUpdateWizard extends Tx_Install_Updates_Base {
34 /**
35 * @var string
36 */
37 protected $title = 'Migrate file relations';
38
39 /**
40 * @var t3lib_file_Storage
41 */
42 protected $storage;
43
44 /**
45 * Initialize the storage repository.
46 */
47 public function init() {
48 /** @var $storageRepository t3lib_file_Repository_StorageRepository */
49 $storageRepository = t3lib_div::makeInstance('t3lib_file_Repository_StorageRepository');
50 $storages = $storageRepository->findAll();
51 $this->storage = $storages[0]; // take first found storage
52 }
53
54 /**
55 * Checks if an update is needed
56 *
57 * @param string &$description: The description for the update
58 * @return boolean TRUE if an update is needed, FALSE otherwise
59 */
60 public function checkForUpdate(&$description) {
61 // @todo Function below copied from sysext/install/updates/class.tx_coreupdates_imagelink.php, needs to be adopted
62
63 $description = 'TODO add description of FAL migration';
64
65 // make this wizard always available
66 return TRUE;
67 }
68
69 /**
70 * Performs the database update.
71 *
72 * @param array &$dbQueries: queries done in this update
73 * @param mixed &$customMessages: custom messages
74 * @return boolean TRUE on success, FALSE on error
75 */
76 public function performUpdate(&$dbQueries, &$customMessages) {
77 $this->init();
78
79 // Function below copied from sysext/install/updates/class.tx_coreupdates_imagelink.php
80
81 $tables = array(
82 'tt_content' => array(
83 'image' => array(
84 'sourcePath' => 'uploads/pics/',
85 // Relative to fileadmin
86 'targetPath' => '_migrated/pics/',
87 'titleTexts' => 'titleText',
88 'captions' => 'imagecaption',
89 'links' => 'image_link',
90 'alternativeTexts' => 'altText',
91 ),
92 ),
93 'pages' => array(
94 'media' => array(
95 'sourcePath' => 'uploads/media/',
96 // Relative to fileadmin
97 'targetPath' => '_migrated/media/',
98 ),
99 ),
100 'pages_language_overlay' => array(
101 'media' => array(
102 'sourcePath' => 'uploads/media/',
103 // Relative to fileadmin
104 'targetPath' => '_migrated/media/',
105 ),
106 ),
107 );
108 // We write down the fields that were migrated. Like this: tt_content:media
109 // so you can check whether a field was already migrated
110 if (isset($GLOBALS['TYPO3_CONF_VARS']['INSTALL']['wizardDone']['Tx_Install_Updates_File_TceformsUpdateWizard'])) {
111 $finishedFields = explode(',', $GLOBALS['TYPO3_CONF_VARS']['INSTALL']['wizardDone']['Tx_Install_Updates_File_TceformsUpdateWizard']);
112 } else {
113 $finishedFields = array();
114 }
115
116 $result = TRUE;
117 if ($this->versionNumber >= 6000000) {
118 /**
119 * TODO
120 *
121 * - for each table:
122 * - get records from table
123 * - for each record:
124 * - for each field:
125 * - migrate field
126 */
127 foreach ($tables as $table => $tableConfiguration) {
128 $fieldsToMigrate = array_keys($tableConfiguration);
129 $fieldsToGet = array();
130 // find all additional fields we should get from the database
131 foreach ($tableConfiguration as $field => $fieldConfiguration) {
132 $fieldKey = $table . ':' . $field;
133 if (array_search($fieldKey, $finishedFields) !== FALSE) {
134 // this field was already migrated
135 continue;
136 } else {
137 $finishedFields[] = $fieldKey;
138 }
139
140 $fieldsToGet[] = $field;
141 if (isset($fieldConfiguration['titleTexts'])) {
142 $fieldsToGet[] = $fieldConfiguration['titleTexts'];
143 }
144 if (isset($fieldConfiguration['alternativeTexts'])) {
145 $fieldsToGet[] = $fieldConfiguration['alternativeTexts'];
146 }
147 if (isset($fieldConfiguration['captions'])) {
148 $fieldsToGet[] = $fieldConfiguration['captions'];
149 }
150 if (isset($fieldConfiguration['links'])) {
151 $fieldsToGet[] = $fieldConfiguration['links'];
152 }
153
154 }
155
156 $records = $this->getRecordsFromTable($table, $fieldsToGet);
157
158 foreach ($records as $record) {
159 foreach ($fieldsToMigrate as $field) {
160 $dbQueries = array_merge($this->migrateField($table, $record, $field, $tableConfiguration[$field]));
161 }
162 }
163 }
164 }
165
166 $finishedFields = implode(',', $finishedFields);
167
168 $this->markWizardAsDone($finishedFields);
169
170 return $result;
171 }
172
173 protected function getRecordsFromTable($table, $relationFields) {
174 $fields = implode(',', array_merge($relationFields, array('uid', 'pid')));
175
176 $records = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows($fields, $table, '');
177
178 return $records;
179 }
180
181 protected function migrateField($table, $row, $fieldname, $fieldConfiguration) {
182 $fieldItems = t3lib_div::trimExplode(',', $row[$fieldname], TRUE);
183
184 if (empty($fieldItems) || is_numeric($row[$fieldname])) {
185 return array();
186 }
187
188 if (isset($fieldConfiguration['titleTexts'])) {
189 $titleTextField = $fieldConfiguration['titleTexts'];
190 $titleTextContents = explode(LF, $row[$titleTextField]);
191 }
192 if (isset($fieldConfiguration['alternativeTexts'])) {
193 $alternativeTextField = $fieldConfiguration['alternativeTexts'];
194 $alternativeTextContents = explode(LF, $row[$alternativeTextField]);
195 }
196 if (isset($fieldConfiguration['captions'])) {
197 $captionField = $fieldConfiguration['captions'];
198 $captionContents = explode(LF, $row[$captionField]);
199 }
200 if (isset($fieldConfiguration['links'])) {
201 $linkField = $fieldConfiguration['links'];
202 $linkContents = explode(LF, $row[$linkField]);
203 }
204
205 $fileadminDirectory = rtrim($GLOBALS['TYPO3_CONF_VARS']['BE']['fileadminDir'], '/') . '/';
206
207 $queries = array();
208 $i = 0;
209 foreach ($fieldItems as $item) {
210
211 if(!PATH_site) {
212 throw new Exception('PATH_site was undefined.');
213 }
214
215 // copy file
216 $sourcePath = PATH_site . $fieldConfiguration['sourcePath'] . $item;
217 $targetPath = PATH_site . $fileadminDirectory . $fieldConfiguration['targetPath'] . $item;
218 if(!is_dir(dirname($targetPath))) {
219 t3lib_div::mkdir_deep(dirname($targetPath));
220 }
221
222 rename($sourcePath, $targetPath);
223
224 // get the File object
225 $file = $this->storage->getFile($fieldConfiguration['targetPath'] . $item);
226
227 if($file instanceof t3lib_file_File) {
228 $fields = array(
229 // TODO add sorting/sorting_foreign
230 'fieldname' => $fieldname,
231 'table_local' => 'sys_file',
232 'uid_foreign' => $row['uid'],
233 'uid_local' => $file->getUid(),
234 'tablenames' => $table,
235 'crdate' => time(),
236 'tstamp' => time(),
237 );
238 if (isset($titleTextField)) {
239 $fields['title'] = trim($titleTextContents[$i]);
240 }
241 if (isset($alternativeTextField)) {
242 $fields['alternative'] = trim($alternativeTextContents[$i]);
243 }
244 if (isset($captionField)) {
245 $fields['description'] = trim($captionContents[$i]);
246 }
247 if (isset($linkField)) {
248 $fields['link'] = trim($linkContents[$i]);
249 }
250
251 $GLOBALS['TYPO3_DB']->exec_INSERTquery('sys_file_reference', $fields);
252 $queries[] = str_replace(LF, ' ', $GLOBALS['TYPO3_DB']->debug_lastBuiltQuery);
253
254 ++$i;
255 }
256 }
257
258 $GLOBALS['TYPO3_DB']->exec_UPDATEquery($table, 'uid=' . $row['uid'], array($fieldname => ''));
259 $queries[] = str_replace(LF, ' ', $GLOBALS['TYPO3_DB']->debug_lastBuiltQuery);
260
261 return $queries;
262 // TODO update original row
263 }
264 }
265
266 ?>