[BUGFIX] Avoid SQL error in ProcessedFileChecksumUpdate
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Classes / Updates / ProcessedFileChecksumUpdate.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 use TYPO3\CMS\Core\Resource\ProcessedFile;
18 use TYPO3\CMS\Core\Resource\ResourceFactory;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20
21 /**
22 * Updates the checksum of sys_file_processedfile records to avoid regeneration of the thumbnails
23 */
24 class ProcessedFileChecksumUpdate extends AbstractUpdate
25 {
26 /**
27 * @var string
28 */
29 protected $title = '[Optional] Update sys_file_processedfile records to match new checksum calculation.';
30
31 /**
32 * Checks if an update is needed
33 *
34 * @param string &$description The description for the update
35 * @return bool Whether an update is needed (TRUE) or not (FALSE)
36 */
37 public function checkForUpdate(&$description)
38 {
39 if ($this->isWizardDone()) {
40 return false;
41 }
42
43 $join = 'sys_file_processedfile LEFT JOIN sys_registry ON CAST(entry_key AS CHAR) = CAST(sys_file_processedfile.uid AS CHAR) AND entry_namespace = \'ProcessedFileChecksumUpdate\'';
44 $count = $this->getDatabaseConnection()->exec_SELECTcountRows('*', $join, '(entry_key IS NULL AND sys_file_processedfile.identifier <> \'\') OR sys_file_processedfile.width IS NULL');
45 if (!$count) {
46 return false;
47 }
48
49 $description = 'The checksum calculation for processed files (image thumbnails) has been changed with TYPO3 CMS 7.3 and 6.2.13.
50 This means that your processed files need to be updated, if you update from versions <strong>below TYPO3 CMS 7.3 or 6.2.13</strong>.<br />
51 This can either happen on demand, when the processed file is first needed, or by executing this wizard, which updates all processed images at once.<br />
52 <strong>Important:</strong> If you have lots of processed files, you should prefer using this wizard, otherwise this might cause a lot of work for your server.';
53
54 return true;
55 }
56
57 /**
58 * Performs the update
59 *
60 * @param array &$databaseQueries Queries done in this update
61 * @param mixed &$customMessages Custom messages
62 * @return bool
63 */
64 public function performUpdate(array &$databaseQueries, &$customMessages)
65 {
66 $db = $this->getDatabaseConnection();
67
68 // remove all invalid records which hold NULL values
69 $db->exec_DELETEquery('sys_file_processedfile', 'width IS NULL or height IS NULL');
70
71 $factory = GeneralUtility::makeInstance(ResourceFactory::class);
72
73 $join = 'sys_file_processedfile LEFT JOIN sys_registry ON entry_key = CAST(sys_file_processedfile.uid AS CHAR) AND entry_namespace = \'ProcessedFileChecksumUpdate\'';
74 $res = $db->exec_SELECTquery('sys_file_processedfile.*', $join, 'entry_key IS NULL AND sys_file_processedfile.identifier <> \'\'');
75 while ($processedFileRow = $db->sql_fetch_assoc($res)) {
76 try {
77 $storage = $factory->getStorageObject($processedFileRow['storage']);
78 } catch (\InvalidArgumentException $e) {
79 $storage = null;
80 }
81 if (!$storage) {
82 // invalid storage, delete record, we can't take care of the associated file
83 $db->exec_DELETEquery('sys_file_processedfile', 'uid=' . $processedFileRow['uid']);
84 continue;
85 }
86
87 if ($storage->getDriverType() !== 'Local') {
88 // non-local storage, we can't treat this, skip the record and mark it done
89 $db->exec_INSERTquery('sys_registry', array('entry_namespace' => 'ProcessedFileChecksumUpdate', 'entry_key' => $processedFileRow['uid']));
90 continue;
91 }
92
93 $configuration = $storage->getConfiguration();
94 if ($configuration['pathType'] === 'relative') {
95 $absoluteBasePath = PATH_site . $configuration['basePath'];
96 } else {
97 $absoluteBasePath = $configuration['basePath'];
98 }
99 $filePath = rtrim($absoluteBasePath, '/') . '/' . ltrim($processedFileRow['identifier'], '/');
100
101 try {
102 $originalFile = $factory->getFileObject($processedFileRow['original']);
103 } catch (\Exception $e) {
104 // no original file there anymore, delete local file
105 @unlink($filePath);
106 $db->exec_DELETEquery('sys_file_processedfile', 'uid=' . $processedFileRow['uid']);
107 continue;
108 }
109
110 $processedFileObject = new ProcessedFile($originalFile, '', array(), $processedFileRow);
111
112 // calculate new checksum and name
113 $newChecksum = $processedFileObject->calculateChecksum();
114
115 // if the checksum already matches, there is nothing to do
116 if ($newChecksum !== $processedFileRow['checksum']) {
117 $newName = str_replace($processedFileRow['checksum'], $newChecksum, $processedFileRow['name']);
118 $newIdentifier = str_replace($processedFileRow['checksum'], $newChecksum, $processedFileRow['identifier']);
119 $newFilePath = str_replace($processedFileRow['checksum'], $newChecksum, $filePath);
120
121 // rename file
122 if (@rename($filePath, $newFilePath)) {
123 // save result back into database
124 $fields = array(
125 'tstamp' => time(),
126 'identifier' => $newIdentifier,
127 'name' => $newName,
128 'checksum' => $newChecksum
129 );
130 $db->exec_UPDATEquery('sys_file_processedfile', 'uid=' . $processedFileRow['uid'], $fields);
131 }
132 // if the rename of the file failed, keep the record, but do not bother with it again
133 }
134
135 // remember we finished this record
136 $db->exec_INSERTquery('sys_registry', array('entry_namespace' => 'ProcessedFileChecksumUpdate', 'entry_key' => $processedFileRow['uid']));
137 }
138
139 $db->exec_DELETEquery('sys_registry', 'entry_namespace = \'ProcessedFileChecksumUpdate\'');
140 $this->markWizardAsDone();
141 return true;
142 }
143 }