‪TYPO3CMS  9.5
FileReferenceUpdate.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
24 
30 {
34  public function ‪getIdentifier(): string
35  {
36  return 'fileReferenceUpdate';
37  }
38 
42  public function ‪getTitle(): string
43  {
44  return 'Migrate file references that are stored in a wrong way to correct scheme';
45  }
46 
50  public function ‪getDescription(): string
51  {
52  return 'File references were saved in a wrong way and references are not shown correctly in file list module.';
53  }
54 
58  public function ‪updateNecessary(): bool
59  {
60  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_refindex');
61  return (bool)$queryBuilder->count('hash')
62  ->from('sys_refindex')
63  ->where(
64  $queryBuilder->expr()->eq('ref_table', $queryBuilder->createNamedParameter('_FILE', \PDO::PARAM_STR)),
65  $queryBuilder->expr()->eq('softref_key', $queryBuilder->createNamedParameter('typolink_tag', \PDO::PARAM_STR)),
66  $queryBuilder->expr()->eq('deleted', $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT))
67  )
68  ->execute()
69  ->fetchColumn(0);
70  }
71 
75  public function ‪getPrerequisites(): array
76  {
77  return [
78  DatabaseUpdatedPrerequisite::class
79  ];
80  }
81 
87  public function ‪executeUpdate(): bool
88  {
89  $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('sys_refindex');
90  $queryBuilder = $connection->createQueryBuilder();
91  $statement = $queryBuilder->select('*')
92  ->from('sys_refindex')
93  ->where(
94  $queryBuilder->expr()->eq('ref_table', $queryBuilder->createNamedParameter('_FILE', \PDO::PARAM_STR)),
95  $queryBuilder->expr()->eq('softref_key', $queryBuilder->createNamedParameter('typolink_tag', \PDO::PARAM_STR)),
96  $queryBuilder->expr()->eq('deleted', $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT))
97  )
98  ->execute();
99  while ($record = $statement->fetch()) {
100  $fileReference = 0;
101  if (‪MathUtility::canBeInterpretedAsInteger($record['ref_string'])) {
102  $fileReference = $record['ref_string'];
103  } else {
104  try {
105  $fileObject = ‪ResourceFactory::getInstance()->‪retrieveFileOrFolderObject($record['ref_string']);
106  if ($fileObject instanceof ‪File) {
107  $fileReference = $fileObject->‪getUid();
108  }
109  } catch (‪Exception $e) {
110  // Silently catch if there is no file object
111  }
112  }
113 
114  $updateQueryBuilder = $connection->createQueryBuilder();
115  $updateQueryBuilder->update('sys_refindex')
116  ->where(
117  $updateQueryBuilder->expr()->eq(
118  'hash',
119  $updateQueryBuilder->createNamedParameter($record['hash'], \PDO::PARAM_STR)
120  )
121  );
122 
123  if ($fileReference) {
124  $updateQueryBuilder->set('ref_table', 'sys_file')
125  ->set('ref_uid', $fileReference)
126  ->set('ref_string', '');
127  } else {
128  $updateQueryBuilder->set('deleted', 1);
129  }
130 
131  $updateQueryBuilder->execute();
132  }
133 
134  return true;
135  }
136 }
‪TYPO3\CMS\Core\Utility\MathUtility\canBeInterpretedAsInteger
‪static bool canBeInterpretedAsInteger($var)
Definition: MathUtility.php:73
‪TYPO3\CMS\Install\Updates\FileReferenceUpdate
Definition: FileReferenceUpdate.php:30
‪TYPO3\CMS\Core\Resource\ResourceFactory\getInstance
‪static ResourceFactory getInstance()
Definition: ResourceFactory.php:39
‪TYPO3\CMS\Install\Updates
Definition: AbstractDownloadExtensionUpdate.php:3
‪TYPO3\CMS\Install\Updates\FileReferenceUpdate\getPrerequisites
‪string[] getPrerequisites()
Definition: FileReferenceUpdate.php:75
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:33
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:23
‪TYPO3\CMS\Core\Resource\AbstractFile\getUid
‪int getUid()
Definition: AbstractFile.php:200
‪TYPO3\CMS\Core\Resource\Exception
Definition: Exception.php:21
‪TYPO3\CMS\Install\Updates\UpgradeWizardInterface
Definition: UpgradeWizardInterface.php:22
‪TYPO3\CMS\Install\Updates\FileReferenceUpdate\executeUpdate
‪bool executeUpdate()
Definition: FileReferenceUpdate.php:87
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:21
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:44
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Install\Updates\FileReferenceUpdate\getDescription
‪string getDescription()
Definition: FileReferenceUpdate.php:50
‪TYPO3\CMS\Install\Updates\FileReferenceUpdate\updateNecessary
‪bool updateNecessary()
Definition: FileReferenceUpdate.php:58
‪TYPO3\CMS\Core\Resource\Exception
Definition: AbstractFileOperationException.php:2
‪TYPO3\CMS\Install\Updates\FileReferenceUpdate\getIdentifier
‪string getIdentifier()
Definition: FileReferenceUpdate.php:34
‪TYPO3\CMS\Core\Resource\ResourceFactory\retrieveFileOrFolderObject
‪File Folder null retrieveFileOrFolderObject($input)
Definition: ResourceFactory.php:491
‪TYPO3\CMS\Install\Updates\FileReferenceUpdate\getTitle
‪string getTitle()
Definition: FileReferenceUpdate.php:42