[TASK] Remove superfluous parenthesis in sysexts
[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 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 * @package TYPO3
31 * @author Ingmar Schlecht <ingmar@typo3.org>
32 * @license http://www.gnu.org/copyleft/gpl.html
33 */
34 class TceformsUpdateWizard extends \TYPO3\CMS\Install\Updates\AbstractUpdate {
35
36 /**
37 * @var string
38 */
39 protected $title = 'Migrate all file relations from tt_content.image and pages.media';
40
41 /**
42 * @var \TYPO3\CMS\Core\Resource\ResourceStorage
43 */
44 protected $storage;
45
46 /**
47 * Initialize the storage repository.
48 */
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];
54 }
55
56 /**
57 * Checks if an update is needed
58 *
59 * @param string &$description: The description for the update
60 * @return boolean TRUE if an update is needed, FALSE otherwise
61 */
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
65 return TRUE;
66 }
67
68 /**
69 * Performs the database update.
70 *
71 * @param array &$dbQueries: queries done in this update
72 * @param mixed &$customMessages: custom messages
73 * @return boolean TRUE on success, FALSE on error
74 */
75 public function performUpdate(array &$dbQueries, &$customMessages) {
76 $this->init();
77 // Function below copied from sysext/install/updates/class.tx_coreupdates_imagelink.php
78 $tables = array(
79 'tt_content' => array(
80 'image' => 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'
88 )
89 ),
90 'pages' => array(
91 'media' => array(
92 'sourcePath' => 'uploads/media/',
93 // Relative to fileadmin
94 'targetPath' => '_migrated/media/'
95 )
96 ),
97 'pages_language_overlay' => array(
98 'media' => array(
99 'sourcePath' => 'uploads/media/',
100 // Relative to fileadmin
101 'targetPath' => '_migrated/media/'
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 $result = TRUE;
113 if ($this->versionNumber >= 6000000) {
114 /** TODO
115
116 - for each table:
117 - get records from table
118 - for each record:
119 - for each field:
120 - migrate field */
121 foreach ($tables as $table => $tableConfiguration) {
122 $fieldsToMigrate = array_keys($tableConfiguration);
123 $fieldsToGet = array();
124 // find all additional fields we should get from the database
125 foreach ($tableConfiguration as $field => $fieldConfiguration) {
126 $fieldKey = $table . ':' . $field;
127 if (array_search($fieldKey, $finishedFields) !== FALSE) {
128 // this field was already migrated
129 continue;
130 } else {
131 $finishedFields[] = $fieldKey;
132 }
133 $fieldsToGet[] = $field;
134 if (isset($fieldConfiguration['titleTexts'])) {
135 $fieldsToGet[] = $fieldConfiguration['titleTexts'];
136 }
137 if (isset($fieldConfiguration['alternativeTexts'])) {
138 $fieldsToGet[] = $fieldConfiguration['alternativeTexts'];
139 }
140 if (isset($fieldConfiguration['captions'])) {
141 $fieldsToGet[] = $fieldConfiguration['captions'];
142 }
143 if (isset($fieldConfiguration['links'])) {
144 $fieldsToGet[] = $fieldConfiguration['links'];
145 }
146 }
147 $records = $this->getRecordsFromTable($table, $fieldsToGet);
148 foreach ($records as $record) {
149 foreach ($fieldsToMigrate as $field) {
150 $dbQueries = array_merge($this->migrateField($table, $record, $field, $tableConfiguration[$field]));
151 }
152 }
153 }
154 }
155 $finishedFields = implode(',', $finishedFields);
156 $this->markWizardAsDone($finishedFields);
157 return $result;
158 }
159
160 protected function getRecordsFromTable($table, $relationFields) {
161 $fields = implode(',', array_merge($relationFields, array('uid', 'pid')));
162 $records = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows($fields, $table, '');
163 return $records;
164 }
165
166 protected function migrateField($table, $row, $fieldname, $fieldConfiguration) {
167 $fieldItems = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $row[$fieldname], TRUE);
168 if (empty($fieldItems) || is_numeric($row[$fieldname])) {
169 return array();
170 }
171 if (isset($fieldConfiguration['titleTexts'])) {
172 $titleTextField = $fieldConfiguration['titleTexts'];
173 $titleTextContents = explode(LF, $row[$titleTextField]);
174 }
175 if (isset($fieldConfiguration['alternativeTexts'])) {
176 $alternativeTextField = $fieldConfiguration['alternativeTexts'];
177 $alternativeTextContents = explode(LF, $row[$alternativeTextField]);
178 }
179 if (isset($fieldConfiguration['captions'])) {
180 $captionField = $fieldConfiguration['captions'];
181 $captionContents = explode(LF, $row[$captionField]);
182 }
183 if (isset($fieldConfiguration['links'])) {
184 $linkField = $fieldConfiguration['links'];
185 $linkContents = explode(LF, $row[$linkField]);
186 }
187 $fileadminDirectory = rtrim($GLOBALS['TYPO3_CONF_VARS']['BE']['fileadminDir'], '/') . '/';
188 $queries = array();
189 $i = 0;
190 foreach ($fieldItems as $item) {
191 if (!PATH_site) {
192 throw new \Exception('PATH_site was undefined.');
193 }
194 // copy file
195 $sourcePath = PATH_site . $fieldConfiguration['sourcePath'] . $item;
196 $targetPath = PATH_site . $fileadminDirectory . $fieldConfiguration['targetPath'] . $item;
197 if (!is_dir(dirname($targetPath))) {
198 \TYPO3\CMS\Core\Utility\GeneralUtility::mkdir_deep(dirname($targetPath));
199 }
200 rename($sourcePath, $targetPath);
201 // get the File object
202 $file = $this->storage->getFile($fieldConfiguration['targetPath'] . $item);
203 if ($file instanceof \TYPO3\CMS\Core\Resource\File) {
204 $fields = array(
205 // TODO add sorting/sorting_foreign
206 'fieldname' => $fieldname,
207 'table_local' => 'sys_file',
208 'uid_foreign' => $row['uid'],
209 'uid_local' => $file->getUid(),
210 'tablenames' => $table,
211 'crdate' => time(),
212 'tstamp' => time()
213 );
214 if (isset($titleTextField)) {
215 $fields['title'] = trim($titleTextContents[$i]);
216 }
217 if (isset($alternativeTextField)) {
218 $fields['alternative'] = trim($alternativeTextContents[$i]);
219 }
220 if (isset($captionField)) {
221 $fields['description'] = trim($captionContents[$i]);
222 }
223 if (isset($linkField)) {
224 $fields['link'] = trim($linkContents[$i]);
225 }
226 $GLOBALS['TYPO3_DB']->exec_INSERTquery('sys_file_reference', $fields);
227 $queries[] = str_replace(LF, ' ', $GLOBALS['TYPO3_DB']->debug_lastBuiltQuery);
228 ++$i;
229 }
230 }
231 // Update referencing table's original field to now contain the count of references.
232 $GLOBALS['TYPO3_DB']->exec_UPDATEquery($table, 'uid=' . $row['uid'], array($fieldname => $i));
233 $queries[] = str_replace(LF, ' ', $GLOBALS['TYPO3_DB']->debug_lastBuiltQuery);
234 return $queries;
235 }
236
237 }
238
239
240 ?>