TYPO3 CMS  TYPO3_7-6
ProcessedFileChecksumUpdate.php
Go to the documentation of this file.
1 <?php
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 
20 
25 {
29  protected $title = '[Optional] Update sys_file_processedfile records to match new checksum calculation.';
30 
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 
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', ['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, '', [], $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 = [
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', ['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 }
performUpdate(array &$databaseQueries, &$customMessages)