[TASK] GGL violation "Tabs must be used to indent lines"
[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 // - for each table:
116 // - get records from table
117 // - for each record:
118 // - for each field:
119 // - migrate field
120 foreach ($tables as $table => $tableConfiguration) {
121 $fieldsToMigrate = array_keys($tableConfiguration);
122 $fieldsToGet = array();
123 // find all additional fields we should get from the database
124 foreach ($tableConfiguration as $field => $fieldConfiguration) {
125 $fieldKey = $table . ':' . $field;
126 if (array_search($fieldKey, $finishedFields) !== FALSE) {
127 // this field was already migrated
128 continue;
129 } else {
130 $finishedFields[] = $fieldKey;
131 }
132 $fieldsToGet[] = $field;
133 if (isset($fieldConfiguration['titleTexts'])) {
134 $fieldsToGet[] = $fieldConfiguration['titleTexts'];
135 }
136 if (isset($fieldConfiguration['alternativeTexts'])) {
137 $fieldsToGet[] = $fieldConfiguration['alternativeTexts'];
138 }
139 if (isset($fieldConfiguration['captions'])) {
140 $fieldsToGet[] = $fieldConfiguration['captions'];
141 }
142 if (isset($fieldConfiguration['links'])) {
143 $fieldsToGet[] = $fieldConfiguration['links'];
144 }
145 }
146 $records = $this->getRecordsFromTable($table, $fieldsToGet);
147 foreach ($records as $record) {
148 foreach ($fieldsToMigrate as $field) {
149 $dbQueries = array_merge($this->migrateField($table, $record, $field, $tableConfiguration[$field]));
150 }
151 }
152 }
153 }
154 $finishedFields = implode(',', $finishedFields);
155 $this->markWizardAsDone($finishedFields);
156 return $result;
157 }
158
159 protected function getRecordsFromTable($table, $relationFields) {
160 $fields = implode(',', array_merge($relationFields, array('uid', 'pid')));
161 $records = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows($fields, $table, '');
162 return $records;
163 }
164
165 protected function migrateField($table, $row, $fieldname, $fieldConfiguration) {
166 $fieldItems = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $row[$fieldname], TRUE);
167 if (empty($fieldItems) || is_numeric($row[$fieldname])) {
168 return array();
169 }
170 if (isset($fieldConfiguration['titleTexts'])) {
171 $titleTextField = $fieldConfiguration['titleTexts'];
172 $titleTextContents = explode(LF, $row[$titleTextField]);
173 }
174 if (isset($fieldConfiguration['alternativeTexts'])) {
175 $alternativeTextField = $fieldConfiguration['alternativeTexts'];
176 $alternativeTextContents = explode(LF, $row[$alternativeTextField]);
177 }
178 if (isset($fieldConfiguration['captions'])) {
179 $captionField = $fieldConfiguration['captions'];
180 $captionContents = explode(LF, $row[$captionField]);
181 }
182 if (isset($fieldConfiguration['links'])) {
183 $linkField = $fieldConfiguration['links'];
184 $linkContents = explode(LF, $row[$linkField]);
185 }
186 $fileadminDirectory = rtrim($GLOBALS['TYPO3_CONF_VARS']['BE']['fileadminDir'], '/') . '/';
187 $queries = array();
188 $i = 0;
189 foreach ($fieldItems as $item) {
190 if (!PATH_site) {
191 throw new \Exception('PATH_site was undefined.');
192 }
193 // copy file
194 $sourcePath = PATH_site . $fieldConfiguration['sourcePath'] . $item;
195 $targetPath = PATH_site . $fileadminDirectory . $fieldConfiguration['targetPath'] . $item;
196 if (!is_dir(dirname($targetPath))) {
197 \TYPO3\CMS\Core\Utility\GeneralUtility::mkdir_deep(dirname($targetPath));
198 }
199 rename($sourcePath, $targetPath);
200 // get the File object
201 $file = $this->storage->getFile($fieldConfiguration['targetPath'] . $item);
202 if ($file instanceof \TYPO3\CMS\Core\Resource\File) {
203 $fields = array(
204 // TODO add sorting/sorting_foreign
205 'fieldname' => $fieldname,
206 'table_local' => 'sys_file',
207 'uid_foreign' => $row['uid'],
208 'uid_local' => $file->getUid(),
209 'tablenames' => $table,
210 'crdate' => time(),
211 'tstamp' => time()
212 );
213 if (isset($titleTextField)) {
214 $fields['title'] = trim($titleTextContents[$i]);
215 }
216 if (isset($alternativeTextField)) {
217 $fields['alternative'] = trim($alternativeTextContents[$i]);
218 }
219 if (isset($captionField)) {
220 $fields['description'] = trim($captionContents[$i]);
221 }
222 if (isset($linkField)) {
223 $fields['link'] = trim($linkContents[$i]);
224 }
225 $GLOBALS['TYPO3_DB']->exec_INSERTquery('sys_file_reference', $fields);
226 $queries[] = str_replace(LF, ' ', $GLOBALS['TYPO3_DB']->debug_lastBuiltQuery);
227 ++$i;
228 }
229 }
230 // Update referencing table's original field to now contain the count of references.
231 $GLOBALS['TYPO3_DB']->exec_UPDATEquery($table, 'uid=' . $row['uid'], array($fieldname => $i));
232 $queries[] = str_replace(LF, ' ', $GLOBALS['TYPO3_DB']->debug_lastBuiltQuery);
233 return $queries;
234 }
235
236 }
237
238
239 ?>