TYPO3 CMS  TYPO3_8-7
FileReferenceUpdate.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
3 
5 
6 /*
7  * This file is part of the TYPO3 CMS project.
8  *
9  * It is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License, either version 2
11  * of the License, or any later version.
12  *
13  * For the full copyright and license information, please read the
14  * LICENSE.txt file that was distributed with this source code.
15  *
16  * The TYPO3 project - inspiring people to share!
17  */
18 
25 
30 {
34  protected $title = 'Migrate file references that are stored in a wrong way to correct scheme';
35 
42  public function checkForUpdate(&$description)
43  {
44  if ($this->isWizardDone()) {
45  return false;
46  }
47 
48  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_refindex');
49  $count = $queryBuilder->count('hash')
50  ->from('sys_refindex')
51  ->where(
52  $queryBuilder->expr()->eq('ref_table', $queryBuilder->createNamedParameter('_FILE', \PDO::PARAM_STR)),
53  $queryBuilder->expr()->eq('softref_key', $queryBuilder->createNamedParameter('typolink_tag', \PDO::PARAM_STR)),
54  $queryBuilder->expr()->eq('deleted', $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT))
55  )
56  ->execute()->fetchColumn(0);
57 
58  if ($count) {
59  $description = 'File references were saved in a wrong way and references aren\'t shown correctly in file list module.';
60  }
61 
62  return (bool)$count;
63  }
64 
72  public function performUpdate(array &$databaseQueries, &$customMessage)
73  {
74  $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('sys_refindex');
75  $queryBuilder = $connection->createQueryBuilder();
76  $statement = $queryBuilder->select('*')
77  ->from('sys_refindex')
78  ->where(
79  $queryBuilder->expr()->eq('ref_table', $queryBuilder->createNamedParameter('_FILE', \PDO::PARAM_STR)),
80  $queryBuilder->expr()->eq('softref_key', $queryBuilder->createNamedParameter('typolink_tag', \PDO::PARAM_STR)),
81  $queryBuilder->expr()->eq('deleted', $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT))
82  )
83  ->execute();
84  while ($record = $statement->fetch()) {
85  $fileReference = 0;
86  if (MathUtility::canBeInterpretedAsInteger($record['ref_string'])) {
87  $fileReference = $record['ref_string'];
88  } else {
89  try {
90  $fileObject = ResourceFactory::getInstance()->retrieveFileOrFolderObject($record['ref_string']);
91  if ($fileObject instanceof File) {
92  $fileReference = $fileObject->getUid();
93  }
94  } catch (Exception $e) {
95  }
96  }
97 
98  $updateQueryBuilder = $connection->createQueryBuilder();
99  $updateQueryBuilder->update('sys_refindex')
100  ->where(
101  $updateQueryBuilder->expr()->eq(
102  'hash',
103  $updateQueryBuilder->createNamedParameter($record['hash'], \PDO::PARAM_STR)
104  )
105  );
106 
107  if ($fileReference) {
108  $updateQueryBuilder->set('ref_table', 'sys_file')
109  ->set('ref_uid', $fileReference)
110  ->set('ref_string', '');
111  } else {
112  $updateQueryBuilder->set('deleted', 1);
113  }
114 
115  $databaseQueries[] = $updateQueryBuilder->getSQL();
116  $updateQueryBuilder->execute();
117  }
118 
119  $this->markWizardAsDone();
120 
121  return true;
122  }
123 }
static makeInstance($className,... $constructorArguments)
performUpdate(array &$databaseQueries, &$customMessage)