-
Notifications
You must be signed in to change notification settings - Fork 19
/
bigdump.php
444 lines (379 loc) · 19 KB
/
bigdump.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
<?php
/* Reminder: always indent with 4 spaces (no tabs). */
// +---------------------------------------------------------------------------+
// | Geeklog 2.1 |
// +---------------------------------------------------------------------------+
// | bigdump.php |
// | |
// | Staggered import for large MySQL Dumps |
// +---------------------------------------------------------------------------+
// | Copyright (C) 2008-2009 by the following authors: |
// | |
// | Authors: Alexey Ozerov - alexey AT ozerov DOT de (BigDump author) |
// | Matt West - matt.danger.west AT gmail DOT com |
// | Rouslan Placella - rouslan AT placella DOT com |
// +---------------------------------------------------------------------------+
// | |
// | This program is free software; you can redistribute it and/or |
// | modify it under the terms of the GNU General Public License |
// | as published by the Free Software Foundation; either version 2 |
// | of the License, or (at your option) any later version. |
// | |
// | This program is distributed in the hope that it will be useful, |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
// | GNU General Public License for more details. |
// | |
// | You should have received a copy of the GNU General Public License |
// | along with this program; if not, write to the Free Software Foundation, |
// | Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
// | |
// +---------------------------------------------------------------------------+
//
// BigDump ver. 0.29b from 2008-01-19
// Staggered import of an large MySQL Dump (like phpMyAdmin 2.x Dump)
// Even through the webservers with hard runtime limit and those in safe mode
// Works fine with Internet Explorer 7.0 and Firefox 2.x
//
// Author: Alexey Ozerov (alexey at ozerov dot de)
// AJAX & CSV functionalities: Krzysiek Herod (kr81uni at wp dot pl)
// Copyright: GPL (C) 2003-2008
// More Infos: http://www.ozerov.de/bigdump.php
//
// THIS SCRIPT IS PROVIDED AS IS, WITHOUT ANY WARRANTY OR GUARANTEE OF ANY KIND
//
define('PATH_INSTALL', __DIR__ . '/');
define('PATH_LAYOUT', PATH_INSTALL . 'layout');
define('BASE_FILE', str_replace('\\', '/', __FILE__));
global $_CONF, $_DB_host, $_DB_name, $_DB_user, $_DB_pass, $_DB_charset,
$LANG_BIGDUMP, $LANG_CHARSET, $LANG_DIRECTION, $LANG_ERROR, $LANG_HELP, $LANG_INSTALL,
$LANG_LABEL, $LANG_MIGRATE, $LANG_PLUGINS, $LANG_RESCUE, $LANG_SUCCESS;
require_once __DIR__ . '/classes/installer.class.php';
require_once __DIR__ . '/classes/db.class.php';
require_once __DIR__ . '/../../siteconfig.php';
require_once $_CONF['path'] . 'db-config.php';
$installer = new Installer();
$db_server = $_DB_host;
$db_name = $_DB_name;
$db_username = $_DB_user;
$db_password = $_DB_pass;
$site_url = isset($_REQUEST['site_url']) ? $_REQUEST['site_url'] : '';
$site_admin_url = isset($_REQUEST['site_admin_url']) ? $_REQUEST['site_admin_url'] : '';
// Other settings (optional)
$filename = ''; // Specify the dump filename to suppress the file selection dialog
$linesPerSession = 3000; // Lines to be executed per one import session
$delayPerSession = 0; // You can specify a sleep time in milliseconds after each session
// Works only if JavaScript is activated. Use to reduce server overrun
// Allowed comment delimiters: lines starting with these strings will be dropped by BigDump
$comment[] = '#'; // Standard comment lines are dropped by default
$comment[] = '-- ';
// $comment[]='---'; // Uncomment this line if using proprietary dump created by outdated mysqldump
// $comment[]='CREATE DATABASE'; // Uncomment this line if your dump contains create database queries in order to ignore them
$comment[] = '/*!'; // Or add your own string to leave out other proprietary things
// see http://project.geeklog.net/tracking/view.php?id=955
$comment[] = 'SET character_set_client = @saved_cs_client;';
// Connection character set should be the same as the dump file character set (utf8, latin1, cp1251, koi8r etc.)
// See http://dev.mysql.com/doc/refman/5.0/en/charset-charsets.html for the full list
$db_connection_charset = '';
if (isset($_REQUEST['db_connection_charset'])) {
$db_connection_charset = preg_replace('/[^a-z0-9\-_]/', '', $_REQUEST['db_connection_charset']);
}
// *******************************************************************************************
// If not familiar with PHP please don't change anything below this line
// *******************************************************************************************
//define ('VERSION','0.32b');
define('DATA_CHUNK_LENGTH', 16384); // How many chars are read per time
define('MAX_QUERY_LINES', 300); // How many lines may be considered to be one query (except text lines)
define('TESTMODE', false); // Set to true to process the file without actually accessing the database
@ini_set('auto_detect_line_endings', true);
@set_time_limit(0);
if (function_exists("date_default_timezone_set") && function_exists("date_default_timezone_get")) {
@date_default_timezone_set(@date_default_timezone_get());
}
header('Content-Type: text/html; charset=' . $LANG_CHARSET);
echo Installer::getHeader($LANG_MIGRATE[17]);
$error = false;
$file = false;
// Check if mysql extension is available
$availableDrivers = Geeklog\Db::getDrivers();
if (!$error && (count($availableDrivers) === 0)) {
echo '<p>' . $LANG_BIGDUMP[11] . '</p>' . PHP_EOL;
$error = true;
}
// Get the current directory
$upload_dir = __DIR__;
// Connect to the database
$db = false;
$dbConnection = false;
$errorMessage = '';
$args = array(
'host' => $_DB_host,
'user' => $_DB_user,
'pass' => $_DB_pass,
'name' => $_DB_name,
'charset' => $db_connection_charset,
);
if (!$error && !TESTMODE) {
try {
$dbConnection = Geeklog\Db::connect(Geeklog\Db::DB_MYSQLI, $args);
$db = true;
} catch (\Exception $e) {
try {
$dbConnection = Geeklog\Db::connect(Geeklog\Db::DB_MYSQL, $args);
$db = true;
} catch (\Exception $e) {
$errorMessage = $e->getMessage();
$dbConnection = false;
}
}
if (empty($dbConnection) || !$db) {
echo $installer->getAlertMsg($LANG_ERROR[9] . $errorMessage . '<br>' . $LANG_ERROR[10]);
$error = true;
}
} else {
$dbConnection = false;
}
// Single file mode
if (!$error && !isset($_REQUEST["fn"]) && $filename != "") {
echo '<p><a href="' . $_SERVER['PHP_SELF'] . '?start=1&fn=' . urlencode($filename) . '&foffset=0&totalqueries=0\">' . $LANG_BIGDUMP[0] . '</a>' . $LANG_BIGDUMP[1] . $filename . $LANG_BIGDUMP[2] . $db_name . $LANG_BIGDUMP[3] . $db_server . PHP_EOL;
}
// Open the file
if (!$error && isset($_REQUEST["start"])) {
// Set current filename ($filename overrides $_REQUEST["fn"] if set)
if ($filename != '') {
$currentFileName = $filename;
} elseif (isset($_REQUEST["fn"])) {
$currentFileName = urldecode($_REQUEST["fn"]);
} else {
$currentFileName = '';
}
// Recognize GZip filename
if (preg_match('/\.gz$/i', $currentFileName)) {
$gzipMode = true;
} else {
$gzipMode = false;
}
if ((!$gzipMode && !$file = @fopen($currentFileName, "r")) || ($gzipMode && !$file = @gzopen($currentFileName, "r"))) {
echo $installer->getAlertMsg($LANG_BIGDUMP[5] . $currentFileName . $LANG_BIGDUMP[6]);
$error = true;
} elseif ((!$gzipMode && @fseek($file, 0, SEEK_END) == 0) || ($gzipMode && @gzseek($file, 0) == 0)) {
// Get the file size (can't do it fast on gzipped files, no idea how)
if (!$gzipMode) {
$fileSize = ftell($file);
} else {
$fileSize = gztell($file); // Always zero, ignore
}
} else {
echo $installer->getAlertMsg($LANG_BIGDUMP[4] . $currentFileName);
$error = true;
}
}
// *******************************************************************************************
// START IMPORT SESSION HERE
// *******************************************************************************************
if (!$error && isset($_REQUEST["start"]) && isset($_REQUEST["foffset"]) && preg_match('/(\.(sql|gz))$/i', $currentFileName)) {
// Check start and foffset are numeric values
if (!is_numeric($_REQUEST["start"]) || !is_numeric($_REQUEST["foffset"])) {
echo $installer->getAlertMsg($LANG_BIGDUMP[7]);
$error = true;
}
if (!$error) {
$_REQUEST["start"] = floor($_REQUEST["start"]);
$_REQUEST["foffset"] = floor($_REQUEST["foffset"]);
echo '<p>' . $LANG_BIGDUMP[8] . ' <b>' . $currentFileName . '</b></p>' . PHP_EOL;
}
// Check $_REQUEST["foffset"] upon $fileSize (can't do it on gzipped files)
if (!$error && !$gzipMode && $_REQUEST["foffset"] > $fileSize) {
echo $installer->getAlertMsg($LANG_BIGDUMP[9]);
$error = true;
}
// Set file pointer to $_REQUEST["foffset"]
if (!$error && ((!$gzipMode && fseek($file, $_REQUEST["foffset"]) != 0) || ($gzipMode && gzseek($file, $_REQUEST["foffset"]) != 0))) {
echo $installer->getAlertMsg($LANG_BIGDUMP[10] . $_REQUEST["foffset"]);
$error = true;
}
// Start processing queries from $file
if (!$error) {
$query = "";
$queries = 0;
$totalQueries = $_REQUEST["totalqueries"];
$lineNumber = $_REQUEST["start"];
$queryLines = 0;
$inParents = false;
// Stay processing as long as the $linesPerSession is not reached or the query is still incomplete
while ($lineNumber < $_REQUEST["start"] + $linesPerSession || $query != "") {
// Read the whole next line
$dumpLine = "";
while (!feof($file) && substr($dumpLine, -1) != "\n") {
if (!$gzipMode) {
$dumpLine .= fgets($file, DATA_CHUNK_LENGTH);
} else {
$dumpLine .= gzgets($file, DATA_CHUNK_LENGTH);
}
}
if ($dumpLine === "") {
break;
}
// Handle DOS and Mac encoded linebreaks (I don't know if it will work on Win32 or Mac Servers)
$dumpLine = str_replace("\r\n", "\n", $dumpLine);
$dumpLine = str_replace("\r", "\n", $dumpLine);
// Skip comments and blank lines only if NOT in parents
if (!$inParents) {
$skipLine = false;
reset($comment);
foreach ($comment as $comment_value) {
if (!$inParents && (trim($dumpLine) == "" || strpos($dumpLine, $comment_value) === 0)) {
$skipLine = true;
break;
}
}
if ($skipLine) {
$lineNumber++;
continue;
}
}
// Remove double back-slashes from the dumpline prior to count the quotes ('\\' can only be within strings)
$dumpLine_deslashed = str_replace("\\\\", "", $dumpLine);
// Count ' and \' in the dumpline to avoid query break within a text field ending by ;
// Please don't use double quotes ('"')to surround strings, it wont work
$parents = substr_count($dumpLine_deslashed, "'") - substr_count($dumpLine_deslashed, "\\'");
if ($parents % 2 != 0) {
$inParents = !$inParents;
}
// Add the line to query
$query .= $dumpLine;
// Don't count the line if in parents (text fields may include unlimited linebreaks)
if (!$inParents) {
$queryLines++;
}
// Stop if query contains more lines as defined by MAX_QUERY_LINES
if ($queryLines > MAX_QUERY_LINES) {
echo $installer->getAlertMsg($LANG_BIGDUMP[14] . $lineNumber . $LANG_BIGDUMP[15] . MAX_QUERY_LINES . $LANG_BIGDUMP[16]);
$error = true;
break;
}
// Execute query if end of query detected (; as last character) AND NOT in parents
if (preg_match("/;$/", trim($dumpLine)) && !$inParents) {
if (!TESTMODE) {
if ($dbConnection->query(trim($query)) === false) {
echo $installer->getAlertMsg(
$LANG_BIGDUMP[17] . $lineNumber . ': ' . trim($dumpLine) . '.<br ' . PHP_EOL . '>'
. $LANG_BIGDUMP[18] . trim(Installer::nl2br(htmlentities($query))) . '<br ' . PHP_EOL . '>'
. $LANG_BIGDUMP[19] . $dbConnection->error()
);
$error = true;
break;
}
$totalQueries++;
$queries++;
$query = "";
$queryLines = 0;
}
$lineNumber++;
}
}
// Get the current file position
if (!$error) {
if (!$gzipMode) {
$fOffset = ftell($file);
} else {
$fOffset = gztell($file);
}
if (!$fOffset) {
echo $installer->getAlertMsg($LANG_BIGDUMP[20]);
$error = true;
}
}
// Print statistics
if (!$error) {
$lines_this = $lineNumber - $_REQUEST["start"];
$lines_done = $lineNumber - 1;
$lines_togo = ' ? ';
$lines_tota = ' ? ';
$queries_this = $queries;
$queries_done = $totalQueries;
$queries_togo = ' ? ';
$queries_tota = ' ? ';
$bytes_this = $fOffset - $_REQUEST["foffset"];
$bytes_done = $fOffset;
$kbytes_this = round($bytes_this / 1024, 2);
$kbytes_done = round($bytes_done / 1024, 2);
$mbytes_this = round($kbytes_this / 1024, 2);
$mbytes_done = round($kbytes_done / 1024, 2);
if (!$gzipMode) {
$bytes_togo = $fileSize - $fOffset;
$bytes_tota = $fileSize;
$kbytes_togo = round($bytes_togo / 1024, 2);
$kbytes_tota = round($bytes_tota / 1024, 2);
$mbytes_togo = round($kbytes_togo / 1024, 2);
$mbytes_tota = round($kbytes_tota / 1024, 2);
$pct_this = ceil($bytes_this / $fileSize * 100);
$pct_done = ceil($fOffset / $fileSize * 100);
$pct_togo = 100 - $pct_done;
$pct_tota = 100;
if ($bytes_togo == 0) {
$lines_togo = '0';
$lines_tota = $lineNumber - 1;
$queries_togo = '0';
$queries_tota = $totalQueries;
}
$pct_bar = '<div style="height: 15px; width: ' . $pct_done . '% ;background-color: #000080; margin: 0;"></div>';
} else {
$bytes_togo = ' ? ';
$bytes_tota = ' ? ';
$kbytes_togo = ' ? ';
$kbytes_tota = ' ? ';
$mbytes_togo = ' ? ';
$mbytes_tota = ' ? ';
$pct_this = ' ? ';
$pct_done = ' ? ';
$pct_togo = ' ? ';
$pct_tota = 100;
$pct_bar = str_replace(' ', ' ', '<tt>[ ' . $LANG_BIGDUMP[21] . ' ]</tt>');
}
echo '
<table width="650" border="0" cellpadding="3" cellspacing="1">
<tr><th align="left" width="125">' . $LANG_BIGDUMP[22] . ': ' . $pct_done . '%</th><td colspan="4">' . $pct_bar . '</td></tr>
</table><br>' . PHP_EOL;
// Finish message and restart the script
if ($lineNumber < $_REQUEST["start"] + $linesPerSession) {
echo $installer->getAlertMsg($LANG_BIGDUMP[23], 'success');
/*** Go back to Geeklog installer ***/
echo("<script language=\"JavaScript\" type=\"text/javascript\">window.setTimeout('location.href=\""
. 'index.php?mode=migrate&step=4'
. '&language=' . urlencode($installer->getLanguage())
. '&site_url=' . urlencode($_REQUEST['site_url'])
. '&site_admin_url=' . urlencode($_REQUEST['site_admin_url']) . "\";',3000);</script>\n");
} else {
if ($delayPerSession != 0) {
echo '<p><b>' . $LANG_BIGDUMP[24] . $delayPerSession . $LANG_BIGDUMP[25] . PHP_EOL;
}
// Go to the next step
echo '<script language="JavaScript" type="text/javascript">window.setTimeout(\'location.href="'
. $_SERVER['PHP_SELF'] . '?start=' . $lineNumber . '&fn='
. urlencode($currentFileName) . '&foffset=' . $fOffset . '&totalqueries=' . $totalQueries . '&db_connection_charset=' . $db_connection_charset . '&language=' . $installer->getLanguage() . '&site_url=' . $site_url . '&site_admin_url=' . $site_admin_url . '";\',500+' . $delayPerSession . ');</script>' . PHP_EOL
. '<noscript>' . PHP_EOL
. ' <p><a href="' . $_SERVER['PHP_SELF'] . '?start=' . $lineNumber . '&fn=' . urlencode($currentFileName) . '&foffset=' . $fOffset . '&totalqueries=' . $totalQueries . '&db_connection_charset=' . $db_connection_charset . '&language=' . $installer->getLanguage() . '&site_url=' . $site_url . '&site_admin_url=' . $site_admin_url . '">Continue from the line ' . $lineNumber . '</a></p>' . PHP_EOL
. '</noscript>' . PHP_EOL
. '<p><strong><a href="' . $_SERVER['PHP_SELF'] . '">' . $LANG_BIGDUMP[26] . '</a></strong> ' . $LANG_BIGDUMP[27] . ' <strong>' . $LANG_BIGDUMP[28] . '</strong></p>' . PHP_EOL;
}
} else {
echo $installer->getAlertMsg($LANG_BIGDUMP[29]);
}
}
}
if ($error) {
$backUrl = 'index.php?mode=migrate';
$language = $installer->getLanguage();
if (!empty($language)) {
$backUrl .= '&language=' . $language;
}
echo '<p><a href="' . $backUrl . '">' . $LANG_BIGDUMP[30] . '</a> '
. $LANG_BIGDUMP[31] . '</p>' . PHP_EOL;
}
if ($file && !$gzipMode) {
fclose($file);
} elseif ($file && $gzipMode) {
gzclose($file);
}
echo Installer::getFooter();