0635886a376b89c369954f2cf2e4141043cd34c5
[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 'targetPath' => '_migrated/pics/', # relative to fileadmin
86 'titleTexts' => 'titleText',
87 'captions' => 'imagecaption',
88 'links' => 'image_link',
89 'alternativeTexts' => 'altText',
90 ),
91 ),
92 'pages' => array(
93 'media' => array(
94 'sourcePath' => 'uploads/media/',
95 'targetPath' => '_migrated/media/', # relative to fileadmin
96 ),
97 ),
98 'pages_language_overlay' => array(
99 'media' => array(
100 'sourcePath' => 'uploads/media/',
101 'targetPath' => '_migrated/media/', # relative to fileadmin
102 ),
103 ),
104 );
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']);
109 } else {
110 $finishedFields = array();
111 }
112
113 $result = TRUE;
114 if ($this->versionNumber >= 6000000) {
115 /**
116 * TODO
117 *
118 * - for each table:
119 * - get records from table
120 * - for each record:
121 * - for each field:
122 * - migrate field
123 */
124 foreach ($tables as $table => $tableConfiguration) {
125 $fieldsToMigrate = array_keys($tableConfiguration);
126 $fieldsToGet = array();
127 // find all additional fields we should get from the database
128 foreach ($tableConfiguration as $field => $fieldConfiguration) {
129 $fieldKey = $table . ':' . $field;
130 if (array_search($fieldKey, $finishedFields) !== FALSE) {
131 // this field was already migrated
132 continue;
133 } else {
134 $finishedFields[] = $fieldKey;
135 }
136
137 $fieldsToGet[] = $field;
138 if (isset($fieldConfiguration['titleTexts'])) {
139 $fieldsToGet[] = $fieldConfiguration['titleTexts'];
140 }
141 if (isset($fieldConfiguration['alternativeTexts'])) {
142 $fieldsToGet[] = $fieldConfiguration['alternativeTexts'];
143 }
144 if (isset($fieldConfiguration['captions'])) {
145 $fieldsToGet[] = $fieldConfiguration['captions'];
146 }
147 if (isset($fieldConfiguration['links'])) {
148 $fieldsToGet[] = $fieldConfiguration['links'];
149 }
150
151 }
152
153 $records = $this->getRecordsFromTable($table, $fieldsToGet);
154
155 foreach ($records as $record) {
156 foreach ($fieldsToMigrate as $field) {
157 $dbQueries = array_merge($this->migrateField($table, $record, $field, $tableConfiguration[$field]));
158 }
159 }
160 }
161 }
162
163 $finishedFields = implode(',', $finishedFields);
164
165 $this->markWizardAsDone($finishedFields);
166
167 return $result;
168 }
169
170 protected function getRecordsFromTable($table, $relationFields) {
171 $fields = implode(',', array_merge($relationFields, array('uid', 'pid')));
172
173 $records = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows($fields, $table);
174
175 return $records;
176 }
177
178 protected function migrateField($table, $row, $fieldname, $fieldConfiguration) {
179 $fieldItems = t3lib_div::trimExplode(',', $row[$fieldname], TRUE);
180
181 if (empty($fieldItems) || is_numeric($row[$fieldname])) {
182 return array();
183 }
184
185 if (isset($fieldConfiguration['titleTexts'])) {
186 $titleTextField = $fieldConfiguration['titleTexts'];
187 $titleTextContents = explode(LF, $row[$titleTextField]);
188 }
189 if (isset($fieldConfiguration['alternativeTexts'])) {
190 $alternativeTextField = $fieldConfiguration['alternativeTexts'];
191 $alternativeTextContents = explode(LF, $row[$alternativeTextField]);
192 }
193 if (isset($fieldConfiguration['captions'])) {
194 $captionField = $fieldConfiguration['captions'];
195 $captionContents = explode(LF, $row[$captionField]);
196 }
197 if (isset($fieldConfiguration['links'])) {
198 $linkField = $fieldConfiguration['links'];
199 $linkContents = explode(LF, $row[$linkField]);
200 }
201
202 $queries = array();
203 $i = 0;
204 foreach ($fieldItems as $item) {
205
206 if(!PATH_site) throw new Exception('PATH_site was undefined.');
207
208 // copy file
209 $sourcePath = PATH_site . $fieldConfiguration['sourcePath'] . $item;
210 $targetPath = PATH_site.'fileadmin/'.$fieldConfiguration['targetPath']. $item;
211 if(!is_dir(dirname($targetPath))) {
212 t3lib_div::mkdir_deep(dirname($targetPath));
213 }
214
215 rename($sourcePath, $targetPath);
216
217 // get the File object
218 $file = $this->storage->getFile($fieldConfiguration['targetPath'] . $item);
219
220 if($file instanceof t3lib_file_File) {
221 $fields = array(
222 // TODO add sorting/sorting_foreign
223 'fieldname' => $fieldname,
224 'table_local' => 'sys_file',
225 'uid_foreign' => $row['uid'],
226 'uid_local' => $file->getUid(),
227 'tablenames' => $table,
228 'crdate' => time(),
229 'tstamp' => time(),
230 );
231 if (isset($titleTextField)) {
232 $fields['title'] = trim($titleTextContents[$i]);
233 }
234 if (isset($alternativeTextField)) {
235 $fields['alternative'] = trim($alternativeTextContents[$i]);
236 }
237 if (isset($captionField)) {
238 $fields['description'] = trim($captionContents[$i]);
239 }
240 if (isset($linkField)) {
241 $fields['link'] = trim($linkContents[$i]);
242 }
243
244 $GLOBALS['TYPO3_DB']->exec_INSERTquery('sys_file_reference', $fields);
245 $queries[] = str_replace(LF, ' ', $GLOBALS['TYPO3_DB']->debug_lastBuiltQuery);
246
247 ++$i;
248 }
249 }
250
251 $GLOBALS['TYPO3_DB']->exec_UPDATEquery($table, 'uid=' . $row['uid'], array($fieldname => ''));
252 $queries[] = str_replace(LF, ' ', $GLOBALS['TYPO3_DB']->debug_lastBuiltQuery);
253
254 return $queries;
255 // TODO update original row
256 }
257 }
258
259 ?>