90f68eade5a30a38ca8891bb7030c539cda51707
[Packages/TYPO3.CMS.git] / typo3 / sysext / extensionmanager / Classes / Utility / DatabaseUtility.php
1 <?php
2 namespace TYPO3\CMS\Extensionmanager\Utility;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2012-2013 Susanne Moog, <susanne.moog@typo3.org>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the text file GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29 /**
30 * Utility for dealing with database related operations
31 *
32 * @author Susanne Moog <susanne.moog@typo3.org>
33 */
34 class DatabaseUtility implements \TYPO3\CMS\Core\SingletonInterface {
35
36 /**
37 * @var string
38 */
39 const MULTI_LINEBREAKS = '
40
41
42 ';
43 /**
44 * Dump content for static tables
45 *
46 * @param array $dbFields
47 * @return string
48 */
49 public function dumpStaticTables($dbFields) {
50 $out = '';
51 // Traverse the table list and dump each:
52 foreach ($dbFields as $table => $fields) {
53 if (is_array($dbFields[$table]['fields'])) {
54 $header = $this->dumpHeader();
55 $tableHeader = $this->dumpTableHeader($table, $dbFields[$table], TRUE);
56 $insertStatements = $this->dumpTableContent($table, $dbFields[$table]['fields']);
57 $out .= $header . self::MULTI_LINEBREAKS . $tableHeader . self::MULTI_LINEBREAKS . $insertStatements . self::MULTI_LINEBREAKS;
58 }
59 }
60 return $out;
61 }
62
63 /**
64 * Header comments of the SQL dump file
65 *
66 * @return string Table header
67 */
68 protected function dumpHeader() {
69 return trim('
70 # TYPO3 Extension Manager dump 1.1
71 #
72 # Host: ' . TYPO3_db_host . ' Database: ' . TYPO3_db . '
73 #--------------------------------------------------------
74 ');
75 }
76
77 /**
78 * Dump CREATE TABLE definition
79 *
80 * @param string $table
81 * @param array $fieldKeyInfo
82 * @param boolean $dropTableIfExists
83 * @return string
84 */
85 protected function dumpTableHeader($table, array $fieldKeyInfo, $dropTableIfExists = FALSE) {
86 $lines = array();
87 $dump = '';
88 // Create field definitions
89 if (is_array($fieldKeyInfo['fields'])) {
90 foreach ($fieldKeyInfo['fields'] as $fieldN => $data) {
91 $lines[] = ' ' . $fieldN . ' ' . $data;
92 }
93 }
94 // Create index key definitions
95 if (is_array($fieldKeyInfo['keys'])) {
96 foreach ($fieldKeyInfo['keys'] as $fieldN => $data) {
97 $lines[] = ' ' . $data;
98 }
99 }
100 // Compile final output:
101 if (count($lines)) {
102 $dump = trim('
103 #
104 # Table structure for table "' . $table . '"
105 #
106 ' . ($dropTableIfExists ? 'DROP TABLE IF EXISTS ' . $table . ';
107 ' : '') . 'CREATE TABLE ' . $table . ' (
108 ' . implode((',' . LF), $lines) . '
109 );');
110 }
111 return $dump;
112 }
113
114 /**
115 * Dump table content
116 * Is DBAL compliant, but the dump format is written as MySQL standard.
117 * If the INSERT statements should be imported in a DBMS using other
118 * quoting than MySQL they must first be translated.
119 *
120 * @param string $table Table name
121 * @param array $fieldStructure Field structure
122 * @return string SQL Content of dump (INSERT statements)
123 */
124 protected function dumpTableContent($table, array $fieldStructure) {
125 // Substitution of certain characters (borrowed from phpMySQL):
126 $search = array('\\', '\'', "\0", "\n", "\r", "\x1A");
127 $replace = array('\\\\', '\\\'', '\\0', '\\n', '\\r', '\\Z');
128 $lines = array();
129 // Select all rows from the table:
130 $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', $table, '');
131 // Traverse the selected rows and dump each row as a line in the file:
132 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result)) {
133 $values = array();
134 foreach ($fieldStructure as $field => $structure) {
135 $values[] = isset($row[$field]) ? '\'' . str_replace($search, $replace, $row[$field]) . '\'' : 'NULL';
136 }
137 $lines[] = 'INSERT INTO ' . $table . ' VALUES (' . implode(', ', $values) . ');';
138 }
139 // Free DB result:
140 $GLOBALS['TYPO3_DB']->sql_free_result($result);
141 // Implode lines and return:
142 return implode(LF, $lines);
143 }
144
145 }