TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
DatabaseUtility.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Extensionmanager\Utility;
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 
19 
24 {
28  const MULTI_LINEBREAKS = '
29 
30 
31 ';
38  public function dumpStaticTables($dbFields)
39  {
40  $out = '';
41  // Traverse the table list and dump each:
42  foreach ($dbFields as $table => $fields) {
43  if (is_array($dbFields[$table]['fields'])) {
44  $header = $this->dumpHeader();
45  $tableHeader = $this->dumpTableHeader($table, $dbFields[$table], true);
46  $insertStatements = $this->dumpTableContent($table, $dbFields[$table]['fields']);
47  $out .= $header . self::MULTI_LINEBREAKS . $tableHeader . self::MULTI_LINEBREAKS . $insertStatements . self::MULTI_LINEBREAKS;
48  }
49  }
50  return $out;
51  }
52 
58  protected function dumpHeader()
59  {
60  return trim('
61 # TYPO3 Extension Manager dump 1.1
62 #
63 # Host: ' . TYPO3_db_host . ' Database: ' . TYPO3_db . '
64 #--------------------------------------------------------
65 ');
66  }
67 
76  protected function dumpTableHeader($table, array $fieldKeyInfo, $dropTableIfExists = false)
77  {
78  $lines = [];
79  $dump = '';
80  // Create field definitions
81  if (is_array($fieldKeyInfo['fields'])) {
82  foreach ($fieldKeyInfo['fields'] as $fieldN => $data) {
83  $lines[] = ' ' . $fieldN . ' ' . $data;
84  }
85  }
86  // Create index key definitions
87  if (is_array($fieldKeyInfo['keys'])) {
88  foreach ($fieldKeyInfo['keys'] as $fieldN => $data) {
89  $lines[] = ' ' . $data;
90  }
91  }
92  // Compile final output:
93  if (!empty($lines)) {
94  $dump = trim('
95 #
96 # Table structure for table "' . $table . '"
97 #
98 ' . ($dropTableIfExists ? 'DROP TABLE IF EXISTS ' . $table . ';
99 ' : '') . 'CREATE TABLE ' . $table . ' (
100 ' . implode((',' . LF), $lines) . '
101 );');
102  }
103  return $dump;
104  }
105 
116  protected function dumpTableContent($table, array $fieldStructure)
117  {
118  // Substitution of certain characters (borrowed from phpMySQL):
119  $search = ['\\', '\'', "\0", "\n", "\r", "\x1A"];
120  $replace = ['\\\\', '\\\'', '\\0', '\\n', '\\r', '\\Z'];
121  $lines = [];
122  // Select all rows from the table:
123  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
124  ->getQueryBuilderForTable($table);
125  $queryBuilder->getRestrictions()
126  ->removeAll();
127  $result = $queryBuilder->select('*')
128  ->from($table)
129  ->execute();
130  // Traverse the selected rows and dump each row as a line in the file:
131  while ($row = $result->fetch()) {
132  $values = [];
133  foreach ($fieldStructure as $field => $structure) {
134  $values[] = isset($row[$field]) ? '\'' . str_replace($search, $replace, $row[$field]) . '\'' : 'NULL';
135  }
136  $lines[] = 'INSERT INTO ' . $table . ' VALUES (' . implode(', ', $values) . ');';
137  }
138  // Implode lines and return:
139  return implode(LF, $lines);
140  }
141 }
dumpTableHeader($table, array $fieldKeyInfo, $dropTableIfExists=false)
static makeInstance($className,...$constructorArguments)