| 1 |
<?php
|
| 2 |
// $Id: database_mysql_dump.inc,v 1.13 2009/06/11 01:20:37 sun Exp $
|
| 3 |
|
| 4 |
// Some older mysql client libs are missing this constant.
|
| 5 |
if (!defined('MYSQLI_BINARY_FLAG')) {
|
| 6 |
define('MYSQLI_BINARY_FLAG', 128);
|
| 7 |
}
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Dump active database.
|
| 11 |
*/
|
| 12 |
function demo_dump_db($filename, $exclude = array()) {
|
| 13 |
// Make sure we have permission to save our backup file.
|
| 14 |
$directory = dirname($filename);
|
| 15 |
if (!file_prepare_directory($directory, FILE_CREATE_DIRECTORY)) {
|
| 16 |
return FALSE;
|
| 17 |
}
|
| 18 |
|
| 19 |
if ($fp = fopen($filename, 'wb')) {
|
| 20 |
$header = "-- Demo.module database dump (version " . DEMO_DUMP_VERSION . ")\n";
|
| 21 |
$header .= "-- http://drupal.org/project/demo\n";
|
| 22 |
$header .= "--\n";
|
| 23 |
$header .= "-- Database: " . _demo_get_database() . "\n";
|
| 24 |
$header .= "-- Date: " . format_date(REQUEST_TIME, 'large') . "\n\n";
|
| 25 |
// Avoid auto value for zero values (required for user id 0).
|
| 26 |
$header .= "SET SQL_MODE=\"NO_AUTO_VALUE_ON_ZERO\";\n";
|
| 27 |
// Temporarily disable foreign key checks for the time of import.
|
| 28 |
$header .= "SET FOREIGN_KEY_CHECKS = 0;\n";
|
| 29 |
fwrite($fp, $header);
|
| 30 |
|
| 31 |
foreach (demo_enum_tables() as $table) {
|
| 32 |
// Always export structure to allow creating a new site
|
| 33 |
// from a database dump
|
| 34 |
fwrite($fp, _demo_dump_table_structure($table));
|
| 35 |
|
| 36 |
if (!in_array($table, $exclude)) {
|
| 37 |
fwrite($fp, _demo_dump_table_data($table));
|
| 38 |
}
|
| 39 |
}
|
| 40 |
|
| 41 |
// Re-enable foreign key checks.
|
| 42 |
fwrite($fp, "\nSET FOREIGN_KEY_CHECKS = 1;\n");
|
| 43 |
|
| 44 |
fclose($fp);
|
| 45 |
return TRUE;
|
| 46 |
}
|
| 47 |
|
| 48 |
return FALSE;
|
| 49 |
}
|
| 50 |
|
| 51 |
/**
|
| 52 |
* Returns the name of the active database.
|
| 53 |
*/
|
| 54 |
function _demo_get_database() {
|
| 55 |
$database = array_keys(db_query('SHOW TABLES')->fetchAssoc());
|
| 56 |
$database = preg_replace('/^Tables_in_/i', '', $database[0]);
|
| 57 |
return $database;
|
| 58 |
}
|
| 59 |
|
| 60 |
/**
|
| 61 |
* Dump table structure.
|
| 62 |
*/
|
| 63 |
function _demo_dump_table_structure($table) {
|
| 64 |
$output = "\n";
|
| 65 |
$output .= "--\n";
|
| 66 |
$output .= "-- Table structure for table '$table'\n";
|
| 67 |
$output .= "--\n\n";
|
| 68 |
|
| 69 |
$data = db_query("SHOW CREATE TABLE `$table`")->fetchAssoc();
|
| 70 |
$output .= preg_replace(
|
| 71 |
array('/^CREATE TABLE/', '/"/'),
|
| 72 |
array('CREATE TABLE IF NOT EXISTS', '`'),
|
| 73 |
$data['create table']
|
| 74 |
) . ";\n";
|
| 75 |
|
| 76 |
return $output;
|
| 77 |
}
|
| 78 |
|
| 79 |
/**
|
| 80 |
* Dump table data.
|
| 81 |
*
|
| 82 |
* This code has largely been stolen from the phpMyAdmin project.
|
| 83 |
*/
|
| 84 |
function _demo_dump_table_data($table) {
|
| 85 |
$output = "\n";
|
| 86 |
$output .= "--\n";
|
| 87 |
$output .= "-- Dumping data for table '$table'\n";
|
| 88 |
$output .= "--\n\n";
|
| 89 |
|
| 90 |
// Dump table data
|
| 91 |
$result = db_query("SELECT * FROM `$table`", array(), array('fetch' => PDO::FETCH_ASSOC));
|
| 92 |
|
| 93 |
// Get table fields.
|
| 94 |
if ($fields = _demo_get_fields($result)) {
|
| 95 |
// Disable indices to speed up import.
|
| 96 |
$output .= "/*!40000 ALTER TABLE $table DISABLE KEYS */;\n";
|
| 97 |
|
| 98 |
// Escape backslashes, PHP code, special chars
|
| 99 |
$search = array('\\', "'", "\x00", "\x0a", "\x0d", "\x1a");
|
| 100 |
$replace = array('\\\\', "''", '\0', '\n', '\r', '\Z');
|
| 101 |
|
| 102 |
$insert_cmd = "INSERT INTO `$table` VALUES\n";
|
| 103 |
$insert_buffer = '';
|
| 104 |
$current_row = 0;
|
| 105 |
$query_size = 0;
|
| 106 |
|
| 107 |
foreach ($result as $row) {
|
| 108 |
$current_row++;
|
| 109 |
$values = array();
|
| 110 |
$field = 0;
|
| 111 |
foreach ($row as $value) {
|
| 112 |
// NULL
|
| 113 |
if (!isset($value) || is_null($value)) {
|
| 114 |
$values[] = 'NULL';
|
| 115 |
}
|
| 116 |
// A number
|
| 117 |
// timestamp is numeric on some MySQL 4.1, BLOBs are sometimes numeric
|
| 118 |
else if ($fields[$field]->numeric && !$fields[$field]->timestamp && !$fields[$field]->blob) {
|
| 119 |
$values[] = $value;
|
| 120 |
}
|
| 121 |
// A true BLOB
|
| 122 |
// - mysqldump only generates hex data when the --hex-blob
|
| 123 |
// option is used, for fields having the binary attribute
|
| 124 |
// no hex is generated
|
| 125 |
// - a TEXT field returns type blob but a real blob
|
| 126 |
// returns also the 'binary' flag
|
| 127 |
else if ($fields[$field]->binary && $fields[$field]->blob) {
|
| 128 |
// Empty blobs need to be different, but '0' is also empty :-(
|
| 129 |
if (empty($value) && $value != '0') {
|
| 130 |
$values[] = "''";
|
| 131 |
}
|
| 132 |
else {
|
| 133 |
$values[] = '0x' . bin2hex($value);
|
| 134 |
}
|
| 135 |
}
|
| 136 |
// Something else -> treat as a string
|
| 137 |
else {
|
| 138 |
$values[] = "'" . str_replace($search, $replace, $value) . "'";
|
| 139 |
}
|
| 140 |
$field++;
|
| 141 |
}
|
| 142 |
|
| 143 |
if ($current_row == 1) {
|
| 144 |
$insert_buffer = $insert_cmd . '(' . implode(', ', $values) . ')';
|
| 145 |
}
|
| 146 |
else {
|
| 147 |
$insert_buffer = '(' . implode(', ', $values) . ')';
|
| 148 |
if ($query_size + strlen($insert_buffer) > 50000) {
|
| 149 |
$output .= ";\n";
|
| 150 |
$current_row = 1;
|
| 151 |
$query_size = 0;
|
| 152 |
$insert_buffer = $insert_cmd . $insert_buffer;
|
| 153 |
}
|
| 154 |
}
|
| 155 |
$query_size += strlen($insert_buffer);
|
| 156 |
|
| 157 |
$output .= ($current_row == 1 ? '' : ",\n") . $insert_buffer;
|
| 158 |
}
|
| 159 |
|
| 160 |
if ($current_row > 0) {
|
| 161 |
$output .= ";\n";
|
| 162 |
}
|
| 163 |
|
| 164 |
// Enable indices again.
|
| 165 |
$output .= "/*!40000 ALTER TABLE $table ENABLE KEYS */;\n";
|
| 166 |
}
|
| 167 |
|
| 168 |
return $output;
|
| 169 |
}
|
| 170 |
|
| 171 |
/**
|
| 172 |
* Return table fields and their properties.
|
| 173 |
*/
|
| 174 |
function _demo_get_fields($result) {
|
| 175 |
$fields = array();
|
| 176 |
|
| 177 |
switch (db_driver()) {
|
| 178 |
case 'mysql':
|
| 179 |
$i = 0;
|
| 180 |
while ($meta = $result->getColumnMeta($i)) {
|
| 181 |
settype($meta, 'object');
|
| 182 |
// pdo_mysql does not add a native type for INT fields.
|
| 183 |
if (isset($meta->native_type)) {
|
| 184 |
// Enhance the field definition for mysql-extension compatibilty.
|
| 185 |
$meta->numeric = (strtolower($meta->native_type) == 'short');
|
| 186 |
$meta->blob = (strtolower($meta->native_type) == 'blob');
|
| 187 |
// Add custom properties.
|
| 188 |
$meta->timestamp = (strtolower($meta->native_type) == 'long');
|
| 189 |
}
|
| 190 |
else {
|
| 191 |
$meta->numeric = $meta->blob = $meta->timestamp = FALSE;
|
| 192 |
}
|
| 193 |
$meta->binary = (array_search('not_null', $meta->flags));
|
| 194 |
$fields[] = $meta;
|
| 195 |
$i++;
|
| 196 |
}
|
| 197 |
break;
|
| 198 |
}
|
| 199 |
|
| 200 |
return $fields;
|
| 201 |
}
|
| 202 |
|