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