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