[TASK] Re-work/simplify copyright header in PHP files - Part 9
[Packages/TYPO3.CMS.git] / typo3 / sysext / extensionmanager / Classes / Utility / DatabaseUtility.php
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 /**
17 * Utility for dealing with database related operations
18 *
19 * @author Susanne Moog <susanne.moog@typo3.org>
20 */
21 class DatabaseUtility implements \TYPO3\CMS\Core\SingletonInterface {
22
23 /**
24 * @var string
25 */
26 const MULTI_LINEBREAKS = '
27
28
29 ';
30 /**
31 * Dump content for static tables
32 *
33 * @param array $dbFields
34 * @return string
35 */
36 public function dumpStaticTables($dbFields) {
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
50 /**
51 * Header comments of the SQL dump file
52 *
53 * @return string Table header
54 */
55 protected function dumpHeader() {
56 return trim('
57 # TYPO3 Extension Manager dump 1.1
58 #
59 # Host: ' . TYPO3_db_host . ' Database: ' . TYPO3_db . '
60 #--------------------------------------------------------
61 ');
62 }
63
64 /**
65 * Dump CREATE TABLE definition
66 *
67 * @param string $table
68 * @param array $fieldKeyInfo
69 * @param boolean $dropTableIfExists
70 * @return string
71 */
72 protected function dumpTableHeader($table, array $fieldKeyInfo, $dropTableIfExists = FALSE) {
73 $lines = array();
74 $dump = '';
75 // Create field definitions
76 if (is_array($fieldKeyInfo['fields'])) {
77 foreach ($fieldKeyInfo['fields'] as $fieldN => $data) {
78 $lines[] = ' ' . $fieldN . ' ' . $data;
79 }
80 }
81 // Create index key definitions
82 if (is_array($fieldKeyInfo['keys'])) {
83 foreach ($fieldKeyInfo['keys'] as $fieldN => $data) {
84 $lines[] = ' ' . $data;
85 }
86 }
87 // Compile final output:
88 if (count($lines)) {
89 $dump = trim('
90 #
91 # Table structure for table "' . $table . '"
92 #
93 ' . ($dropTableIfExists ? 'DROP TABLE IF EXISTS ' . $table . ';
94 ' : '') . 'CREATE TABLE ' . $table . ' (
95 ' . implode((',' . LF), $lines) . '
96 );');
97 }
98 return $dump;
99 }
100
101 /**
102 * Dump table content
103 * Is DBAL compliant, but the dump format is written as MySQL standard.
104 * If the INSERT statements should be imported in a DBMS using other
105 * quoting than MySQL they must first be translated.
106 *
107 * @param string $table Table name
108 * @param array $fieldStructure Field structure
109 * @return string SQL Content of dump (INSERT statements)
110 */
111 protected function dumpTableContent($table, array $fieldStructure) {
112 // Substitution of certain characters (borrowed from phpMySQL):
113 $search = array('\\', '\'', "\0", "\n", "\r", "\x1A");
114 $replace = array('\\\\', '\\\'', '\\0', '\\n', '\\r', '\\Z');
115 $lines = array();
116 // Select all rows from the table:
117 $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', $table, '');
118 // Traverse the selected rows and dump each row as a line in the file:
119 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result)) {
120 $values = array();
121 foreach ($fieldStructure as $field => $structure) {
122 $values[] = isset($row[$field]) ? '\'' . str_replace($search, $replace, $row[$field]) . '\'' : 'NULL';
123 }
124 $lines[] = 'INSERT INTO ' . $table . ' VALUES (' . implode(', ', $values) . ');';
125 }
126 // Free DB result:
127 $GLOBALS['TYPO3_DB']->sql_free_result($result);
128 // Implode lines and return:
129 return implode(LF, $lines);
130 }
131
132 }