TYPO3 CMS  TYPO3_7-6
DatabaseUtility.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 
21 {
25  const MULTI_LINEBREAKS = '
26 
27 
28 ';
35  public function dumpStaticTables($dbFields)
36  {
37  $out = '';
38  // Traverse the table list and dump each:
39  foreach ($dbFields as $table => $fields) {
40  if (is_array($dbFields[$table]['fields'])) {
41  $header = $this->dumpHeader();
42  $tableHeader = $this->dumpTableHeader($table, $dbFields[$table], true);
43  $insertStatements = $this->dumpTableContent($table, $dbFields[$table]['fields']);
44  $out .= $header . self::MULTI_LINEBREAKS . $tableHeader . self::MULTI_LINEBREAKS . $insertStatements . self::MULTI_LINEBREAKS;
45  }
46  }
47  return $out;
48  }
49 
55  protected function dumpHeader()
56  {
57  return trim('
58 # TYPO3 Extension Manager dump 1.1
59 #
60 # Host: ' . TYPO3_db_host . ' Database: ' . TYPO3_db . '
61 #--------------------------------------------------------
62 ');
63  }
64 
73  protected function dumpTableHeader($table, array $fieldKeyInfo, $dropTableIfExists = false)
74  {
75  $lines = [];
76  $dump = '';
77  // Create field definitions
78  if (is_array($fieldKeyInfo['fields'])) {
79  foreach ($fieldKeyInfo['fields'] as $fieldN => $data) {
80  $lines[] = ' ' . $fieldN . ' ' . $data;
81  }
82  }
83  // Create index key definitions
84  if (is_array($fieldKeyInfo['keys'])) {
85  foreach ($fieldKeyInfo['keys'] as $fieldN => $data) {
86  $lines[] = ' ' . $data;
87  }
88  }
89  // Compile final output:
90  if (!empty($lines)) {
91  $dump = trim('
92 #
93 # Table structure for table "' . $table . '"
94 #
95 ' . ($dropTableIfExists ? 'DROP TABLE IF EXISTS ' . $table . ';
96 ' : '') . 'CREATE TABLE ' . $table . ' (
97 ' . implode((',' . LF), $lines) . '
98 );');
99  }
100  return $dump;
101  }
102 
113  protected function dumpTableContent($table, array $fieldStructure)
114  {
115  // Substitution of certain characters (borrowed from phpMySQL):
116  $search = ['\\', '\'', "\0", "\n", "\r", "\x1A"];
117  $replace = ['\\\\', '\\\'', '\\0', '\\n', '\\r', '\\Z'];
118  $lines = [];
119  // Select all rows from the table:
120  $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', $table, '');
121  // Traverse the selected rows and dump each row as a line in the file:
122  while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result)) {
123  $values = [];
124  foreach ($fieldStructure as $field => $structure) {
125  $values[] = isset($row[$field]) ? '\'' . str_replace($search, $replace, $row[$field]) . '\'' : 'NULL';
126  }
127  $lines[] = 'INSERT INTO ' . $table . ' VALUES (' . implode(', ', $values) . ');';
128  }
129  // Free DB result:
130  $GLOBALS['TYPO3_DB']->sql_free_result($result);
131  // Implode lines and return:
132  return implode(LF, $lines);
133  }
134 }
dumpTableHeader($table, array $fieldKeyInfo, $dropTableIfExists=false)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']