Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 235 lines (226 sloc) 9.521 kB
667bfec Decomposition
jakubvrana authored
1 <?php
79d1902 Substitute table name to $TABLE
jakubvrana authored
2 $TABLE = $_GET["dump"];
251b397 Export signature
jakubvrana authored
3
80bc4b3 Prepare customizable export
jakubvrana authored
4 if ($_POST) {
3f5b683 Reintegrate sqlite branch
jakubvrana authored
5 $cookie = "";
39d5293 @vrana Save dump options to cookie
authored
6 foreach (array("output", "format", "db_style", "routines", "events", "table_style", "auto_increment", "triggers", "data_style") as $key) {
3f5b683 Reintegrate sqlite branch
jakubvrana authored
7 $cookie .= "&$key=" . urlencode($_POST[$key]);
8 }
9 cookie("adminer_export", substr($cookie, 1));
8ab6fff @vrana Centralize dump_headers
authored
10 $ext = dump_headers(($TABLE != "" ? $TABLE : DB), (DB == "" || count((array) $_POST["tables"] + (array) $_POST["data"]) > 1));
3da0f3b @vrana Timestamp in export
authored
11 $is_sql = ($_POST["format"] == "sql");
12 if ($is_sql) {
3f5b683 Reintegrate sqlite branch
jakubvrana authored
13 echo "-- Adminer $VERSION " . $drivers[DRIVER] . " dump
14
e672694 @vrana Rename $driver to $jush
authored
15 " . ($jush != "sql" ? "" : "SET NAMES utf8;
f9cbf50 @vrana Export data settings only with data
authored
16 " . ($_POST["data_style"] ? "SET foreign_key_checks = 0;
7e644b4 @vrana Save bytes ($connection->quote shortcut)
authored
17 SET time_zone = " . q($connection->result("SELECT @@time_zone")) . ";
25f01d3 Compress export and import
jakubvrana authored
18 SET sql_mode = 'NO_AUTO_VALUE_ON_ZERO';
f9cbf50 @vrana Export data settings only with data
authored
19 " : "") . "
3f5b683 Reintegrate sqlite branch
jakubvrana authored
20 ");
7afa763 CSV export
jakubvrana authored
21 }
80bc4b3 Prepare customizable export
jakubvrana authored
22
f507c81 Simplify export
jakubvrana authored
23 $style = $_POST["db_style"];
3f5b683 Reintegrate sqlite branch
jakubvrana authored
24 $databases = array(DB);
25 if (DB == "") {
26 $databases = $_POST["databases"];
27 if (is_string($databases)) {
28 $databases = explode("\n", rtrim(str_replace("\r", "", $databases), "\n"));
29 }
30 }
31 foreach ((array) $databases as $db) {
64d616c Rename get_dbh to connection
jakubvrana authored
32 if ($connection->select_db($db)) {
3da0f3b @vrana Timestamp in export
authored
33 if ($is_sql && ereg('CREATE', $style) && ($create = $connection->result("SHOW CREATE DATABASE " . idf_escape($db), 1))) {
f507c81 Simplify export
jakubvrana authored
34 if ($style == "DROP+CREATE") {
89a60e7 Use output buffering for dump compression
jakubvrana authored
35 echo "DROP DATABASE IF EXISTS " . idf_escape($db) . ";\n";
9890b6c Export Output in select.inc.php
jakubvrana authored
36 }
89a60e7 Use output buffering for dump compression
jakubvrana authored
37 echo ($style == "CREATE+ALTER" ? preg_replace('~^CREATE DATABASE ~', '\\0IF NOT EXISTS ', $create) : $create) . ";\n";
9cb4e22 Simplify dump.inc.php
jakubvrana authored
38 }
3da0f3b @vrana Timestamp in export
authored
39 if ($is_sql) {
122252e Routines and events checkboxes (thanks to cvicebni ubor)
jakubvrana authored
40 if ($style) {
58c80e3 Driver specific USE
jakubvrana authored
41 echo use_sql($db) . ";\n\n";
4bf68cd Set @adminer_alter with table alter
jakubvrana authored
42 }
43 if (in_array("CREATE+ALTER", array($style, $_POST["table_style"]))) {
44 echo "SET @adminer_alter = '';\n\n";
122252e Routines and events checkboxes (thanks to cvicebni ubor)
jakubvrana authored
45 }
ff97ef9 Events (MySQL 5.1)
jakubvrana authored
46 $out = "";
122252e Routines and events checkboxes (thanks to cvicebni ubor)
jakubvrana authored
47 if ($_POST["routines"]) {
9cb4e22 Simplify dump.inc.php
jakubvrana authored
48 foreach (array("FUNCTION", "PROCEDURE") as $routine) {
96544ba @vrana Report errors in get_rows()
authored
49 foreach (get_rows("SHOW $routine STATUS WHERE Db = " . q($db), null, "-- ") as $row) {
50 $out .= ($style != 'DROP+CREATE' ? "DROP $routine IF EXISTS " . idf_escape($row["Name"]) . ";;\n" : "")
a4e38a2 @vrana Remove definer from routine and event
authored
51 . remove_definer($connection->result("SHOW CREATE $routine " . idf_escape($row["Name"]), 2)) . ";;\n\n";
9cb4e22 Simplify dump.inc.php
jakubvrana authored
52 }
53 }
54 }
122252e Routines and events checkboxes (thanks to cvicebni ubor)
jakubvrana authored
55 if ($_POST["events"]) {
96544ba @vrana Report errors in get_rows()
authored
56 foreach (get_rows("SHOW EVENTS", null, "-- ") as $row) {
57 $out .= ($style != 'DROP+CREATE' ? "DROP EVENT IF EXISTS " . idf_escape($row["Name"]) . ";;\n" : "")
a4e38a2 @vrana Remove definer from routine and event
authored
58 . remove_definer($connection->result("SHOW CREATE EVENT " . idf_escape($row["Name"]), 3)) . ";;\n\n";
ff97ef9 Events (MySQL 5.1)
jakubvrana authored
59 }
60 }
25f01d3 Compress export and import
jakubvrana authored
61 if ($out) {
89a60e7 Use output buffering for dump compression
jakubvrana authored
62 echo "DELIMITER ;;\n\n$out" . "DELIMITER ;\n\n";
25f01d3 Compress export and import
jakubvrana authored
63 }
9cb4e22 Simplify dump.inc.php
jakubvrana authored
64 }
65
f507c81 Simplify export
jakubvrana authored
66 if ($_POST["table_style"] || $_POST["data_style"]) {
9cb4e22 Simplify dump.inc.php
jakubvrana authored
67 $views = array();
5275ed8 @vrana Rename variable
authored
68 foreach (table_status() as $table_status) {
69 $table = (DB == "" || in_array($table_status["Name"], (array) $_POST["tables"]));
70 $data = (DB == "" || in_array($table_status["Name"], (array) $_POST["data"]));
f507c81 Simplify export
jakubvrana authored
71 if ($table || $data) {
5275ed8 @vrana Rename variable
authored
72 if (!is_view($table_status)) {
9cb4e22 Simplify dump.inc.php
jakubvrana authored
73 if ($ext == "tar") {
74 ob_start();
75 }
5275ed8 @vrana Rename variable
authored
76 $adminer->dumpTable($table_status["Name"], ($table ? $_POST["table_style"] : ""));
f507c81 Simplify export
jakubvrana authored
77 if ($data) {
5275ed8 @vrana Rename variable
authored
78 $adminer->dumpData($table_status["Name"], $_POST["data_style"], "SELECT * FROM " . table($table_status["Name"]));
f507c81 Simplify export
jakubvrana authored
79 }
5275ed8 @vrana Rename variable
authored
80 if ($is_sql && $_POST["triggers"] && $table && ($triggers = trigger_sql($table_status["Name"], $_POST["table_style"]))) {
5b8361a @vrana Don't export triggers without table (bug #3193489)
authored
81 echo "\nDELIMITER ;;\n$triggers\nDELIMITER ;\n";
da874a3 Export triggers after data
jakubvrana authored
82 }
9cb4e22 Simplify dump.inc.php
jakubvrana authored
83 if ($ext == "tar") {
5275ed8 @vrana Rename variable
authored
84 echo tar_file((DB != "" ? "" : "$db/") . "$table_status[Name].csv", ob_get_clean());
3da0f3b @vrana Timestamp in export
authored
85 } elseif ($is_sql) {
89a60e7 Use output buffering for dump compression
jakubvrana authored
86 echo "\n";
9cb4e22 Simplify dump.inc.php
jakubvrana authored
87 }
3da0f3b @vrana Timestamp in export
authored
88 } elseif ($is_sql) {
5275ed8 @vrana Rename variable
authored
89 $views[] = $table_status["Name"];
9cb4e22 Simplify dump.inc.php
jakubvrana authored
90 }
91 }
92 }
f507c81 Simplify export
jakubvrana authored
93 foreach ($views as $view) {
095d472 @vrana Customizable export
authored
94 $adminer->dumpTable($view, $_POST["table_style"], true);
9890b6c Export Output in select.inc.php
jakubvrana authored
95 }
65c9f4b Add tar ending (thanks to Marco Sharper)
jakubvrana authored
96 if ($ext == "tar") {
89a60e7 Use output buffering for dump compression
jakubvrana authored
97 echo pack("x512");
65c9f4b Add tar ending (thanks to Marco Sharper)
jakubvrana authored
98 }
7afa763 CSV export
jakubvrana authored
99 }
4951444 Alter database
jakubvrana authored
100
3da0f3b @vrana Timestamp in export
authored
101 if ($style == "CREATE+ALTER" && $is_sql) {
64ba924 Comments
jakubvrana authored
102 // drop old tables
4951444 Alter database
jakubvrana authored
103 $query = "SELECT TABLE_NAME, ENGINE, TABLE_COLLATION, TABLE_COMMENT FROM information_schema.TABLES WHERE TABLE_SCHEMA = DATABASE()";
89a60e7 Use output buffering for dump compression
jakubvrana authored
104 echo "DELIMITER ;;
d74159b Print ALTER command instead of running it in export
jakubvrana authored
105 CREATE PROCEDURE adminer_alter (INOUT alter_command text) BEGIN
4951444 Alter database
jakubvrana authored
106 DECLARE _table_name, _engine, _table_collation varchar(64);
107 DECLARE _table_comment varchar(64);
108 DECLARE done bool DEFAULT 0;
25f01d3 Compress export and import
jakubvrana authored
109 DECLARE tables CURSOR FOR $query;
4951444 Alter database
jakubvrana authored
110 DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
111 OPEN tables;
112 REPEAT
113 FETCH tables INTO _table_name, _engine, _table_collation, _table_comment;
114 IF NOT done THEN
89a60e7 Use output buffering for dump compression
jakubvrana authored
115 CASE _table_name";
b0d637b @vrana Avoid fatal errors
authored
116 foreach (get_rows($query) as $row) {
7e644b4 @vrana Save bytes ($connection->quote shortcut)
authored
117 $comment = q($row["ENGINE"] == "InnoDB" ? preg_replace('~(?:(.+); )?InnoDB free: .*~', '\\1', $row["TABLE_COMMENT"]) : $row["TABLE_COMMENT"]);
89a60e7 Use output buffering for dump compression
jakubvrana authored
118 echo "
7e644b4 @vrana Save bytes ($connection->quote shortcut)
authored
119 WHEN " . q($row["TABLE_NAME"]) . " THEN
36a3656 Change escape_string to quote
jakubvrana authored
120 " . (isset($row["ENGINE"]) ? "IF _engine != '$row[ENGINE]' OR _table_collation != '$row[TABLE_COLLATION]' OR _table_comment != $comment THEN
121 ALTER TABLE " . idf_escape($row["TABLE_NAME"]) . " ENGINE=$row[ENGINE] COLLATE=$row[TABLE_COLLATION] COMMENT=$comment;
89a60e7 Use output buffering for dump compression
jakubvrana authored
122 END IF" : "BEGIN END") . ";";
87703b1 USE right after CREATE
jakubvrana authored
123 }
89a60e7 Use output buffering for dump compression
jakubvrana authored
124 echo "
4951444 Alter database
jakubvrana authored
125 ELSE
d74159b Print ALTER command instead of running it in export
jakubvrana authored
126 SET alter_command = CONCAT(alter_command, 'DROP TABLE `', REPLACE(_table_name, '`', '``'), '`;\\n');
4951444 Alter database
jakubvrana authored
127 END CASE;
128 END IF;
129 UNTIL done END REPEAT;
130 CLOSE tables;
131 END;;
132 DELIMITER ;
d74159b Print ALTER command instead of running it in export
jakubvrana authored
133 CALL adminer_alter(@adminer_alter);
134 DROP PROCEDURE adminer_alter;
89a60e7 Use output buffering for dump compression
jakubvrana authored
135 ";
4951444 Alter database
jakubvrana authored
136 }
3da0f3b @vrana Timestamp in export
authored
137 if (in_array("CREATE+ALTER", array($style, $_POST["table_style"])) && $is_sql) {
89a60e7 Use output buffering for dump compression
jakubvrana authored
138 echo "SELECT @adminer_alter;\n";
d74159b Print ALTER command instead of running it in export
jakubvrana authored
139 }
a65c0f1 Customizable export
jakubvrana authored
140 }
80bc4b3 Prepare customizable export
jakubvrana authored
141 }
3da0f3b @vrana Timestamp in export
authored
142 if ($is_sql) {
143 echo "-- " . $connection->result("SELECT NOW()") . "\n";
144 }
80bc4b3 Prepare customizable export
jakubvrana authored
145 exit;
146 }
006ddc1 Disable Foreign key checks
jakubvrana authored
147
7352c28 Replace strlen() by != ""
jakubvrana authored
148 page_header(lang('Export'), "", ($_GET["export"] != "" ? array("table" => $_GET["export"]) : array()), DB);
80bc4b3 Prepare customizable export
jakubvrana authored
149 ?>
150
151 <form action="" method="post">
f507c81 Simplify export
jakubvrana authored
152 <table cellspacing="0">
03231f9 Prepare export simplification
jakubvrana authored
153 <?php
fae2938 Compact dump output
jakubvrana authored
154 $db_style = array('', 'USE', 'DROP+CREATE', 'CREATE');
155 $table_style = array('', 'DROP+CREATE', 'CREATE');
782921b @vrana Finish SQLite
authored
156 $data_style = array('', 'TRUNCATE+INSERT', 'INSERT');
e672694 @vrana Rename $driver to $jush
authored
157 if ($jush == "sql") {
f507c81 Simplify export
jakubvrana authored
158 $db_style[] = 'CREATE+ALTER';
159 $table_style[] = 'CREATE+ALTER';
782921b @vrana Finish SQLite
authored
160 $data_style[] = 'INSERT+UPDATE';
f507c81 Simplify export
jakubvrana authored
161 }
3f5b683 Reintegrate sqlite branch
jakubvrana authored
162 parse_str($_COOKIE["adminer_export"], $row);
163 if (!$row) {
164 $row = array("output" => "text", "format" => "sql", "db_style" => (DB != "" ? "" : "CREATE"), "table_style" => "DROP+CREATE", "data_style" => "INSERT");
165 }
39d5293 @vrana Save dump options to cookie
authored
166 if (!isset($row["events"])) { // backwards compatibility
167 $row["routines"] = $row["events"] = ($_GET["dump"] == "");
88d473a @vrana Disable auto_increment by default
authored
168 $row["triggers"] = $row["table_style"];
39d5293 @vrana Save dump options to cookie
authored
169 }
be3410f @vrana Simplify dumpOutput, dumpFormat and dumpData methods
authored
170 echo "<tr><th>" . lang('Output') . "<td>" . html_select("output", $adminer->dumpOutput(), $row["output"], 0) . "\n"; // 0 - radio
171 echo "<tr><th>" . lang('Format') . "<td>" . html_select("format", $adminer->dumpFormat(), $row["format"], 0) . "\n"; // 0 - radio
782921b @vrana Finish SQLite
authored
172 echo ($jush == "sqlite" ? "" : "<tr><th>" . lang('Database') . "<td>" . html_select('db_style', $db_style, $row["db_style"])
39d5293 @vrana Save dump options to cookie
authored
173 . (support("routine") ? checkbox("routines", 1, $row["routines"], lang('Routines')) : "")
174 . (support("event") ? checkbox("events", 1, $row["events"], lang('Events')) : "")
782921b @vrana Finish SQLite
authored
175 );
d68a88a Ability to not export triggers (thanks to paranoiq)
jakubvrana authored
176 echo "<tr><th>" . lang('Tables') . "<td>" . html_select('table_style', $table_style, $row["table_style"])
39d5293 @vrana Save dump options to cookie
authored
177 . checkbox("auto_increment", 1, $row["auto_increment"], lang('Auto Increment'))
178 . (support("trigger") ? checkbox("triggers", 1, $row["triggers"], lang('Triggers')) : "")
d68a88a Ability to not export triggers (thanks to paranoiq)
jakubvrana authored
179 ;
3f5b683 Reintegrate sqlite branch
jakubvrana authored
180 echo "<tr><th>" . lang('Data') . "<td>" . html_select('data_style', $data_style, $row["data_style"]);
03231f9 Prepare export simplification
jakubvrana authored
181 ?>
182 </table>
c6813ad IE8 spaces
jakubvrana authored
183 <p><input type="submit" value="<?php echo lang('Export'); ?>">
80bc4b3 Prepare customizable export
jakubvrana authored
184
ee25260 Join ifs
jakubvrana authored
185 <table cellspacing="0">
80bc4b3 Prepare customizable export
jakubvrana authored
186 <?php
cf98a33 Ability to check table prefix
jakubvrana authored
187 $prefixes = array();
7352c28 Replace strlen() by != ""
jakubvrana authored
188 if (DB != "") {
189 $checked = ($TABLE != "" ? "" : " checked");
0875518 Separate echo
jakubvrana authored
190 echo "<thead><tr>";
3c5c0f0 Use camelCase in JavaScript
jakubvrana authored
191 echo "<th style='text-align: left;'><label><input type='checkbox' id='check-tables'$checked onclick='formCheck(this, /^tables\\[/);'>" . lang('Tables') . "</label>";
192 echo "<th style='text-align: right;'><label>" . lang('Data') . "<input type='checkbox' id='check-data'$checked onclick='formCheck(this, /^data\\[/);'></label>";
ace55ed HTML instead of XHTML
jakubvrana authored
193 echo "</thead>\n";
03231f9 Prepare export simplification
jakubvrana authored
194 $views = "";
d68a88a Ability to not export triggers (thanks to paranoiq)
jakubvrana authored
195 //! defer number of rows to JavaScript
5275ed8 @vrana Rename variable
authored
196 foreach (table_status() as $table_status) {
197 $name = $table_status["Name"];
cf98a33 Ability to check table prefix
jakubvrana authored
198 $prefix = ereg_replace("_.*", "", $name);
199 $checked = ($TABLE == "" || $TABLE == (substr($TABLE, -1) == "%" ? "$prefix%" : $name)); //! % may be part of table name
e5b1c99 @vrana Support Shift+click in export
authored
200 $print = "<tr><td>" . checkbox("tables[]", $name, $checked, $name, "checkboxClick(event, this); formUncheck('check-tables');");
5275ed8 @vrana Rename variable
authored
201 if (is_view($table_status)) {
ace55ed HTML instead of XHTML
jakubvrana authored
202 $views .= "$print\n";
03231f9 Prepare export simplification
jakubvrana authored
203 } else {
e5b1c99 @vrana Support Shift+click in export
authored
204 echo "$print<td align='right'><label>" . ($table_status["Engine"] == "InnoDB" && $table_status["Rows"] ? "~ " : "") . $table_status["Rows"] . checkbox("data[]", $name, $checked, "", "checkboxClick(event, this); formUncheck('check-data');") . "</label>\n";
748379e Prepare customizable export
jakubvrana authored
205 }
cf98a33 Ability to check table prefix
jakubvrana authored
206 $prefixes[$prefix]++;
80bc4b3 Prepare customizable export
jakubvrana authored
207 }
ee25260 Join ifs
jakubvrana authored
208 echo $views;
209 } else {
cf98a33 Ability to check table prefix
jakubvrana authored
210 echo "<thead><tr><th style='text-align: left;'><label><input type='checkbox' id='check-databases'" . ($TABLE == "" ? " checked" : "") . " onclick='formCheck(this, /^databases\\[/);'>" . lang('Database') . "</label></thead>\n";
c7f1a63 @vrana Extensible list of databases
authored
211 $databases = $adminer->databases();
3f5b683 Reintegrate sqlite branch
jakubvrana authored
212 if ($databases) {
213 foreach ($databases as $db) {
214 if (!information_schema($db)) {
215 $prefix = ereg_replace("_.*", "", $db);
216 echo "<tr><td>" . checkbox("databases[]", $db, $TABLE == "" || $TABLE == "$prefix%", $db, "formUncheck('check-databases');") . "</label>\n";
217 $prefixes[$prefix]++;
218 }
ee25260 Join ifs
jakubvrana authored
219 }
3f5b683 Reintegrate sqlite branch
jakubvrana authored
220 } else {
7d152fb @vrana Utilize bodyKeydown function
authored
221 echo "<tr><td><textarea name='databases' rows='10' cols='20'></textarea>";
ee25260 Join ifs
jakubvrana authored
222 }
80bc4b3 Prepare customizable export
jakubvrana authored
223 }
224 ?>
ee25260 Join ifs
jakubvrana authored
225 </table>
80bc4b3 Prepare customizable export
jakubvrana authored
226 </form>
cf98a33 Ability to check table prefix
jakubvrana authored
227 <?php
228 $first = true;
229 foreach ($prefixes as $key => $val) {
230 if ($key != "" && $val > 1) {
231 echo ($first ? "<p>" : " ") . "<a href='" . h(ME) . "dump=" . urlencode("$key%") . "'>" . h($key) . "</a>";
232 $first = false;
233 }
234 }
Something went wrong with that request. Please try again.