TYPO3 CMS  TYPO3_8-7
SchemaIndexDefinitionListener.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 
22 
28 {
38  public function onSchemaIndexDefinition(SchemaIndexDefinitionEventArgs $event)
39  {
40  if (strpos($event->getConnection()->getServerVersion(), 'MySQL') !== 0) {
41  return;
42  }
43 
44  $connection = $event->getConnection();
45  $indexName = $event->getTableIndex()['name'];
46  $sql = $event->getDatabasePlatform()->getListTableIndexesSQL(
47  $event->getTable(),
48  $event->getConnection()->getDatabase()
49  );
50  $sql .= ' AND ' . $connection->quoteIdentifier('INDEX_NAME') . ' = ' . $connection->quote($indexName);
51  $tableIndexes = $event->getConnection()->fetchAll($sql);
52 
53  $subPartColumns = array_filter(
54  $tableIndexes,
55  function ($column) {
56  return $column['Sub_Part'];
57  }
58  );
59 
60  if (!empty($subPartColumns)) {
61  $event->setIndex($this->buildIndex($tableIndexes));
62  $event->preventDefault();
63  }
64  }
65 
74  protected function buildIndex(array $tableIndexRows): Index
75  {
76  $data = null;
77  foreach ($tableIndexRows as $tableIndex) {
78  $tableIndex = array_change_key_case($tableIndex, CASE_LOWER);
79 
80  $tableIndex['primary'] = $tableIndex['key_name'] === 'PRIMARY';
81 
82  if (strpos($tableIndex['index_type'], 'FULLTEXT') !== false) {
83  $tableIndex['flags'] = ['FULLTEXT'];
84  } elseif (strpos($tableIndex['index_type'], 'SPATIAL') !== false) {
85  $tableIndex['flags'] = ['SPATIAL'];
86  }
87 
88  $indexName = $tableIndex['key_name'];
89  $columnName = $tableIndex['column_name'];
90 
91  if ($tableIndex['sub_part'] !== null) {
92  $columnName .= '(' . $tableIndex['sub_part'] . ')';
93  }
94 
95  if ($data === null) {
96  $data = [
97  'name' => $indexName,
98  'columns' => [$columnName],
99  'unique' => !$tableIndex['non_unique'],
100  'primary' => $tableIndex['primary'],
101  'flags' => $tableIndex['flags'] ?? [],
102  'options' => isset($tableIndex['where']) ? ['where' => $tableIndex['where']] : [],
103  ];
104  } else {
105  $data['columns'][] = $columnName;
106  }
107  }
108 
110  Index::class,
111  $data['name'],
112  $data['columns'],
113  $data['unique'],
114  $data['primary'],
115  $data['flags'],
116  $data['options']
117  );
118 
119  return $index;
120  }
121 }
static makeInstance($className,... $constructorArguments)