[BUGFIX] Upgrade Wizard "Split menu types.." fails
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Classes / Updates / SplitMenusUpdate.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\Database\ConnectionPool;
18 use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20
21 /**
22 * Split menu types into dedicated content elements
23 */
24 class SplitMenusUpdate extends AbstractUpdate
25 {
26 /**
27 * @var string
28 */
29 protected $title = 'Split menu types into dedicated content elements';
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 $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('tt_content');
43 $tableColumns = $connection->getSchemaManager()->listTableColumns('tt_content');
44 // Only proceed if menu_type field still exists
45 if (!isset($tableColumns['menu_type'])) {
46 return false;
47 }
48 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tt_content');
49 $queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
50 $elementCount = $queryBuilder->count('uid')
51 ->from('tt_content')
52 ->where(
53 $queryBuilder->expr()->eq('CType', $queryBuilder->createNamedParameter('menu', \PDO::PARAM_STR))
54 )
55 ->execute()->fetchColumn(0);
56 if ($elementCount) {
57 $description = 'Menus have been splitted into dedicated content elements to provide '
58 . 'a better maintainability and more easy to adjustable template with single '
59 . 'responsibility for the rendering.';
60 }
61 return (bool)$elementCount;
62 }
63
64 /**
65 * Performs the database update
66 *
67 * @param array &$databaseQueries Queries done in this update
68 * @param string &$customMessage Custom message
69 * @return bool
70 */
71 public function performUpdate(array &$databaseQueries, &$customMessage)
72 {
73 $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('tt_content');
74 $queryBuilder = $connection->createQueryBuilder();
75 $queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
76 $statement = $queryBuilder->select('uid', 'header', 'menu_type')
77 ->from('tt_content')
78 ->where(
79 $queryBuilder->expr()->eq(
80 'CType',
81 $queryBuilder->createNamedParameter('menu', \PDO::PARAM_STR)
82 )
83 )
84 ->execute();
85 while ($record = $statement->fetch()) {
86 $queryBuilder = $connection->createQueryBuilder();
87 $queryBuilder->update('tt_content')
88 ->where(
89 $queryBuilder->expr()->eq(
90 'uid',
91 $queryBuilder->createNamedParameter($record['uid'], \PDO::PARAM_INT)
92 )
93 )
94 ->set('CType', $this->mapMenuTypes($record['menu_type']));
95 $databaseQueries[] = $queryBuilder->getSQL();
96 $queryBuilder->execute();
97 }
98 $this->markWizardAsDone();
99 return true;
100 }
101
102 /**
103 * Map the old to the new values
104 *
105 * @param string $menuType The content of the FlexForm
106 * @return string The equivalent CType
107 */
108 protected function mapMenuTypes($menuType)
109 {
110 $mapping = [
111 0 => 'menu_pages',
112 1 => 'menu_subpages',
113 2 => 'menu_sitemap',
114 3 => 'menu_section',
115 4 => 'menu_abstract',
116 5 => 'menu_recently_updated',
117 6 => 'menu_related_pages',
118 7 => 'menu_section_pages',
119 8 => 'menu_sitemap_pages',
120 'categorized_pages' => 'menu_categorized_pages',
121 'categorized_content' => 'menu_categorized_content'
122 ];
123 if (array_key_exists($menuType, $mapping)) {
124 return $mapping[$menuType];
125 }
126 return 'menu_' . $menuType;
127 }
128 }