[TASK] Re-work/simplify copyright header in PHP files - Part 2
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Classes / Updates / FileTableSplittingUpdate.php
1 <?php
2 namespace TYPO3\CMS\Install\Updates;
3
4 /**
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 /**
18 * Migrate metadata from sys_file table to sys_filemetadata.
19 * Also takes care of custom TCA fields if they have been created beforehand.
20 *
21 * @author Steffen Ritter <steffen.ritter@typo3.org>
22 */
23 class FileTableSplittingUpdate extends AbstractUpdate {
24
25 /**
26 * The table the metadata is to be stored in
27 * @var string
28 */
29 protected $metaDataTable = 'sys_file_metadata';
30
31 /**
32 * @var string
33 */
34 protected $title = 'Migrate file metadata from sys_file to an external metadata table';
35
36 /**
37 * Checks if an update is needed
38 *
39 * @param string &$description The description for the update
40 * @return boolean Whether an update is needed (TRUE) or not (FALSE)
41 */
42 public function checkForUpdate(&$description) {
43 $result = FALSE;
44
45 $description = 'In TYPO3 CMS 6.2 LTS the metadata has been split off to an external table. This wizard will migrate the data. If you have extended the sys_file table manually your custom data will be included, too, if you create TCA and columns in sys_file_metadata before running this wizard.';
46
47 if (!array_key_exists($this->metaDataTable, $GLOBALS['TYPO3_DB']->admin_get_tables())) {
48 $result = TRUE;
49 } else {
50 $fields = $GLOBALS['TYPO3_DB']->admin_get_fields('sys_file');
51 // Check if the field exists on sys_file if not there is no data to migrate (TYPO3 < 6.0 or >= 6.2)
52 if (isset($fields['width']) && isset($fields['height'])) {
53 // Check if the 1:1 integrity is ok, if not we need to migrate the metadata.
54 $sysFileCount = $GLOBALS['TYPO3_DB']->exec_SELECTcountRows('uid', 'sys_file');
55 $sysFileMetaDataCount = $GLOBALS['TYPO3_DB']->exec_SELECTcountRows('uid', $this->metaDataTable);
56 $result = $sysFileCount > $sysFileMetaDataCount;
57 }
58 }
59
60 return $result;
61 }
62
63 /**
64 * Performs the database update. Won't run if the table is not present.
65 * Will stop if the table does not exist to give users the possibility to
66 * migrate custom fields to and therefore move their TCA and sql upfront.
67 *
68 * @param array &$dbQueries Queries done in this update
69 * @param mixed &$customMessages Custom messages
70 * @return boolean Whether it worked (TRUE) or not (FALSE)
71 */
72 public function performUpdate(array &$dbQueries, &$customMessages) {
73
74 if (!array_key_exists($this->metaDataTable, $GLOBALS['TYPO3_DB']->admin_get_tables())) {
75 $customMessages = 'ERROR! Make sure you already created the table. If you added custom metadata to sys_file table add TCA ' .
76 'configuration as well as SQL definitions to sys_file_metadata, too.';
77 return FALSE;
78 }
79
80 $fieldsToMigrate = $this->detectFieldsToMigrate();
81 if (empty($fieldsToMigrate)) {
82 return TRUE;
83 }
84 $fieldsToWrite = $fieldsToMigrate;
85 $fieldsToWrite[] = 'file';
86 $fieldsToSelect = $fieldsToMigrate;
87 $fieldsToSelect[] = 'uid AS file';
88
89 $resultObject = $GLOBALS['TYPO3_DB']->sql_query(
90 'INSERT INTO ' . $this->metaDataTable . ' (' . implode(',', $fieldsToWrite) . ')' .
91 ' SELECT ' . implode(',', $fieldsToSelect) .
92 ' FROM sys_file' .
93 ' WHERE uid NOT IN (SELECT file FROM ' . $this->metaDataTable . ')');
94
95 return $resultObject !== FALSE;
96 }
97
98 /**
99 * Looks at the table sql definitions and checks which fields are present in both tables.
100 * ignories some Management field
101 *
102 * @return array
103 */
104 protected function detectFieldsToMigrate() {
105 $fieldsBlackListed = array('uid', 'deleted', 'sys_language_uid');
106 $fieldsInSysFile = array_keys($GLOBALS['TYPO3_DB']->admin_get_fields('sys_file'));
107 $fieldsInSysFileMetaData = array_keys($GLOBALS['TYPO3_DB']->admin_get_fields($this->metaDataTable));
108
109 $commonFields = array_intersect($fieldsInSysFileMetaData, $fieldsInSysFile);
110 $commonFields = array_diff($commonFields, $fieldsBlackListed);
111
112 return $commonFields;
113
114 }
115 }