TYPO3 CMS  TYPO3_8-7
MigrateMediaToAssetsForTextMediaCe.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 = 'Migrate CTypes textmedia database field "media" to "assets"';
30 
37  public function checkForUpdate(&$description)
38  {
39  if ($this->isWizardDone()) {
40  return false;
41  }
42 
43  // No need to join the sys_file_references table here as we can rely on the reference
44  // counter to check if the wizards has any textmedia content elements to upgrade.
45  $queryBuilder= GeneralUtility::makeInstance(ConnectionPool::class)
46  ->getQueryBuilderForTable('tt_content');
47  $queryBuilder->getRestrictions()->removeAll();
48  $numberOfUpgradeableRecords = $queryBuilder->count('uid')
49  ->from('tt_content')
50  ->where(
51  $queryBuilder->expr()->gt('media', $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT)),
52  $queryBuilder->expr()->eq('CType', $queryBuilder->createNamedParameter('textmedia', \PDO::PARAM_STR))
53  )
54  ->execute()
55  ->fetchColumn(0);
56 
57  if ($numberOfUpgradeableRecords > 0) {
58  $description = 'The extension "fluid_styled_content" is using a new database field for mediafile'
59  . ' references. This update wizard migrates these old references to use the new database field.';
60  } else {
61  $this->markWizardAsDone();
62  }
63 
64  return (bool)$numberOfUpgradeableRecords;
65  }
66 
74  public function performUpdate(array &$databaseQueries, &$customMessage)
75  {
76  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
77  ->getQueryBuilderForTable('tt_content');
78  $queryBuilder->getRestrictions()->removeAll();
79  $statement = $queryBuilder->select('uid', 'media')
80  ->from('tt_content')
81  ->where(
82  $queryBuilder->expr()->gt('media', $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT)),
83  $queryBuilder->expr()->eq('CType', $queryBuilder->createNamedParameter('textmedia', \PDO::PARAM_STR))
84  )
85  ->execute();
86 
87  while ($content = $statement->fetch()) {
88  $queryStack = [];
89  // we will split the update in two separate queries, since the two tables
90  // can possibly be on two different databases. We therefore have to care for
91  // a possible rollback
92  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
93  ->getQueryBuilderForTable('tt_content');
94  $queryBuilder->update('tt_content')
95  ->where(
96  $queryBuilder->expr()->eq(
97  'uid',
98  $queryBuilder->createNamedParameter($content['uid'], \PDO::PARAM_INT)
99  )
100  )
101  ->set('media', 0)
102  ->set('assets', (int)$content['media']);
103  $queryStack[] = $queryBuilder->getSQL();
104  try {
105  $queryBuilder->execute();
106  } catch (DBALException $e) {
107  $customMessage = 'MySQL-Error: ' . $queryBuilder->getConnection()->errorInfo();
108  return false;
109  }
110 
111  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
112  ->getQueryBuilderForTable('sys_file_reference');
113  $queryBuilder->update('sys_file_reference')
114  ->where(
115  $queryBuilder->expr()->eq(
116  'uid_foreign',
117  $queryBuilder->createNamedParameter($content['uid'], \PDO::PARAM_INT)
118  ),
119  $queryBuilder->expr()->eq(
120  'tablenames',
121  $queryBuilder->createNamedParameter('tt_content', \PDO::PARAM_STR)
122  ),
123  $queryBuilder->expr()->eq(
124  'fieldname',
125  $queryBuilder->createNamedParameter('media', \PDO::PARAM_STR)
126  )
127  )
128  ->set('fieldname', 'assets');
129  $queryStack[] = $queryBuilder->getSQL();
130  try {
131  $queryBuilder->execute();
132  } catch (DBALException $e) {
133  $customMessage = 'MySQL-Error: ' . $queryBuilder->getConnection()->errorInfo();
134  // if the second query is not successful but the first was we'll have
135  // to get back to a consistent state by rolling back the first query.
136  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
137  ->getQueryBuilderForTable('tt_content');
138  $queryBuilder->update('tt_content')
139  ->where(
140  $queryBuilder->expr()->eq(
141  'uid',
142  $queryBuilder->createNamedParameter($content['uid'], \PDO::PARAM_INT)
143  )
144  )
145  ->set('media', (int)$content['media'])
146  ->execute();
147  return false;
148  }
149  // only if both queries were successful, we add them to the databaseQuery array.
150  $databaseQueries = array_merge($databaseQueries, $queryStack);
151  }
152  $this->markWizardAsDone();
153  return true;
154  }
155 }
static makeInstance($className,... $constructorArguments)