Menu

[r15]: / includes / phpmyadmin / build_dump.lib.php  Maximize  Restore  History

Download this file

542 lines (490 with data), 21.7 kB

  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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
<?php
#Application name: PhpCollab
#Status page: 0
/* $Id: build_dump.lib.php,v 1.1 2003/07/02 14:47:06 fullo Exp $ */
/**
* Set of functions used to build dumps of tables
*/
if (!defined('PMA_BUILD_DUMP_LIB_INCLUDED')){
define('PMA_BUILD_DUMP_LIB_INCLUDED', 1);
/**
* Uses the 'htmlspecialchars()' php function on databases, tables and fields
* name if the dump has to be displayed on screen.
*
* @param string the string to format
*
* @return string the formatted string
*
* @access private
*/
function PMA_htmlFormat($a_string = '')
{
return (empty($GLOBALS['asfile']) ? htmlspecialchars($a_string) : $a_string);
} // end of the 'PMA_htmlFormat()' function
/**
* Returns $table's CREATE definition
*
* Uses the 'PMA_htmlFormat()' function defined in 'tbl_dump.php'
*
* @param string the database name
* @param string the table name
* @param string the end of line sequence
* @param string the url to go back in case of error
*
* @return string the CREATE statement on success
*
* @global boolean whether to add 'drop' statements or not
* @global boolean whether to use backquotes to allow the use of special
* characters in database, table and fields names or not
*
* @see PMA_htmlFormat()
*
* @access public
*/
function PMA_getTableDef($db, $table, $crlf, $error_url)
{
global $drop;
global $use_backquotes;
$schema_create = '';
if (!empty($drop)) {
$schema_create .= 'DROP TABLE IF EXISTS ' . PMA_backquote(PMA_htmlFormat($table), $use_backquotes) . ';' . $crlf;
}
// Steve Alberty's patch for complete table dump,
// modified by Lem9 to allow older MySQL versions to continue to work
if (PMA_MYSQL_INT_VERSION >= 32321) {
// Whether to quote table and fields names or not
if ($use_backquotes) {
mysql_query('SET SQL_QUOTE_SHOW_CREATE = 1');
} else {
mysql_query('SET SQL_QUOTE_SHOW_CREATE = 0');
}
$result = mysql_query('SHOW CREATE TABLE ' . PMA_backquote($db) . '.' . PMA_backquote($table));
if ($result != FALSE && mysql_num_rows($result) > 0) {
$tmpres = mysql_fetch_array($result);
$schema_create .= str_replace("\n", $crlf, PMA_htmlFormat($tmpres[1]));
}
mysql_free_result($result);
return $schema_create;
} // end if MySQL >= 3.23.20
// For MySQL < 3.23.20
$schema_create .= 'CREATE TABLE ' . PMA_htmlFormat(PMA_backquote($table), $use_backquotes) . ' (' . $crlf;
$local_query = 'SHOW FIELDS FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table);
$result = mysql_query($local_query) or PMA_mysqlDie('', $local_query, '', $error_url);
while ($row = mysql_fetch_array($result)) {
$schema_create .= ' ' . PMA_htmlFormat(PMA_backquote($row['Field'], $use_backquotes)) . ' ' . $row['Type'];
if (isset($row['Default']) && $row['Default'] != '') {
$schema_create .= ' DEFAULT \'' . PMA_htmlFormat(PMA_sqlAddslashes($row['Default'])) . '\'';
}
if ($row['Null'] != 'YES') {
$schema_create .= ' NOT NULL';
}
if ($row['Extra'] != '') {
$schema_create .= ' ' . $row['Extra'];
}
$schema_create .= ',' . $crlf;
} // end while
mysql_free_result($result);
$schema_create = ereg_replace(',' . $crlf . '$', '', $schema_create);
$local_query = 'SHOW KEYS FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table);
$result = mysql_query($local_query) or PMA_mysqlDie('', $local_query, '', $error_url);
while ($row = mysql_fetch_array($result))
{
$kname = $row['Key_name'];
$comment = (isset($row['Comment'])) ? $row['Comment'] : '';
$sub_part = (isset($row['Sub_part'])) ? $row['Sub_part'] : '';
if ($kname != 'PRIMARY' && $row['Non_unique'] == 0) {
$kname = "UNIQUE|$kname";
}
if ($comment == 'FULLTEXT') {
$kname = 'FULLTEXT|$kname';
}
if (!isset($index[$kname])) {
$index[$kname] = array();
}
if ($sub_part > 1) {
$index[$kname][] = PMA_htmlFormat(PMA_backquote($row['Column_name'], $use_backquotes)) . '(' . $sub_part . ')';
} else {
$index[$kname][] = PMA_htmlFormat(PMA_backquote($row['Column_name'], $use_backquotes));
}
} // end while
mysql_free_result($result);
while (list($x, $columns) = @each($index)) {
$schema_create .= ',' . $crlf;
if ($x == 'PRIMARY') {
$schema_create .= ' PRIMARY KEY (';
} else if (substr($x, 0, 6) == 'UNIQUE') {
$schema_create .= ' UNIQUE ' . substr($x, 7) . ' (';
} else if (substr($x, 0, 8) == 'FULLTEXT') {
$schema_create .= ' FULLTEXT ' . substr($x, 9) . ' (';
} else {
$schema_create .= ' KEY ' . $x . ' (';
}
$schema_create .= implode($columns, ', ') . ')';
} // end while
$schema_create .= $crlf . ')';
return $schema_create;
} // end of the 'PMA_getTableDef()' function
/**
* php >= 4.0.5 only : get the content of $table as a series of INSERT
* statements.
* After every row, a custom callback function $handler gets called.
*
* Last revision 13 July 2001: Patch for limiting dump size from
* vinay@sanisoft.com & girish@sanisoft.com
*
* @param string the current database name
* @param string the current table name
* @param string the 'limit' clause to use with the sql query
* @param string the name of the handler (function) to use at the end
* of every row. This handler must accept one parameter
* ($sql_insert)
* @param string the url to go back in case of error
*
* @return boolean always true
*
* @global boolean whether to use backquotes to allow the use of special
* characters in database, table and fields names or not
* @global integer the number of records
* @global integer the current record position
*
* @access private
*
* @see PMA_getTableContent()
*
* @author staybyte
*/
function PMA_getTableContentFast($db, $table, $add_query = '', $handler, $error_url)
{
global $use_backquotes;
global $rows_cnt;
global $current_row;
$local_query = 'SELECT * FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table) . $add_query;
$result = mysql_query($local_query) or PMA_mysqlDie('', $local_query, '', $error_url);
if ($result != FALSE) {
$fields_cnt = mysql_num_fields($result);
$rows_cnt = mysql_num_rows($result);
// Checks whether the field is an integer or not
for ($j = 0; $j < $fields_cnt; $j++) {
$field_set[$j] = PMA_backquote(mysql_field_name($result, $j), $use_backquotes);
$type = mysql_field_type($result, $j);
if ($type == 'tinyint' || $type == 'smallint' || $type == 'mediumint' || $type == 'int' ||
$type == 'bigint' ||$type == 'timestamp') {
$field_num[$j] = TRUE;
} else {
$field_num[$j] = FALSE;
}
} // end for
// Sets the scheme
if (isset($GLOBALS['showcolumns'])) {
$fields = implode(', ', $field_set);
$schema_insert = 'INSERT INTO ' . PMA_backquote(PMA_htmlFormat($table), $use_backquotes)
. ' (' . PMA_htmlFormat($fields) . ') VALUES (';
} else {
$schema_insert = 'INSERT INTO ' . PMA_backquote(PMA_htmlFormat($table), $use_backquotes)
. ' VALUES (';
}
$search = array("\x00", "\x0a", "\x0d", "\x1a"); //\x08\\x09, not required
$replace = array('\0', '\n', '\r', '\Z');
$current_row = 0;
@set_time_limit($GLOBALS['cfgExecTimeLimit']);
while ($row = mysql_fetch_row($result)) {
$current_row++;
for ($j = 0; $j < $fields_cnt; $j++) {
if (!isset($row[$j])) {
$values[] = 'NULL';
} else if ($row[$j] == '0' || $row[$j] != '') {
// a number
if ($field_num[$j]) {
$values[] = $row[$j];
}
// a string
else {
$values[] = "'" . str_replace($search, $replace, PMA_sqlAddslashes($row[$j])) . "'";
}
} else {
$values[] = "''";
} // end if
} // end for
// Extended inserts case
if (isset($GLOBALS['extended_ins'])) {
if ($current_row == 1) {
$insert_line = $schema_insert . implode(', ', $values) . ')';
} else {
$insert_line = '(' . implode(', ', $values) . ')';
}
}
// Other inserts case
else {
$insert_line = $schema_insert . implode(', ', $values) . ')';
}
unset($values);
// Call the handler
$handler($insert_line);
// loic1: send a fake header to bypass browser timeout if data
// are bufferized
if (!empty($GLOBALS['ob_mode'])
|| (isset($GLOBALS['zip']) || isset($GLOBALS['bzip']) || isset($GLOBALS['gzip']))) {
header('Expires: 0');
}
} // end while
} // end if ($result != FALSE)
mysql_free_result($result);
return TRUE;
} // end of the 'PMA_getTableContentFast()' function
/**
* php < 4.0.5 only: get the content of $table as a series of INSERT
* statements.
* After every row, a custom callback function $handler gets called.
*
* Last revision 13 July 2001: Patch for limiting dump size from
* vinay@sanisoft.com & girish@sanisoft.com
*
* @param string the current database name
* @param string the current table name
* @param string the 'limit' clause to use with the sql query
* @param string the name of the handler (function) to use at the end
* of every row. This handler must accept one parameter
* ($sql_insert)
* @param string the url to go back in case of error
*
* @return boolean always true
*
* @global boolean whether to use backquotes to allow the use of special
* characters in database, table and fields names or not
* @global integer the number of records
* @global integer the current record position
*
* @access private
*
* @see PMA_getTableContent()
*/
function PMA_getTableContentOld($db, $table, $add_query = '', $handler, $error_url)
{
global $use_backquotes;
global $rows_cnt;
global $current_row;
$local_query = 'SELECT * FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table) . $add_query;
$result = mysql_query($local_query) or PMA_mysqlDie('', $local_query, '', $error_url);
$current_row = 0;
$fields_cnt = mysql_num_fields($result);
$rows_cnt = mysql_num_rows($result);
@set_time_limit($GLOBALS['cfgExecTimeLimit']); // HaRa
while ($row = mysql_fetch_row($result)) {
$current_row++;
$table_list = '(';
for ($j = 0; $j < $fields_cnt; $j++) {
$table_list .= PMA_backquote(mysql_field_name($result, $j), $use_backquotes) . ', ';
}
$table_list = substr($table_list, 0, -2);
$table_list .= ')';
if (isset($GLOBALS['extended_ins']) && $current_row > 1) {
$schema_insert = '(';
} else {
if (isset($GLOBALS['showcolumns'])) {
$schema_insert = 'INSERT INTO ' . PMA_backquote(PMA_htmlFormat($table), $use_backquotes)
. ' ' . PMA_htmlFormat($table_list) . ' VALUES (';
} else {
$schema_insert = 'INSERT INTO ' . PMA_backquote(PMA_htmlFormat($table), $use_backquotes)
. ' VALUES (';
}
$is_first_row = FALSE;
}
for ($j = 0; $j < $fields_cnt; $j++) {
if (!isset($row[$j])) {
$schema_insert .= ' NULL, ';
} else if ($row[$j] == '0' || $row[$j] != '') {
$type = mysql_field_type($result, $j);
// a number
if ($type == 'tinyint' || $type == 'smallint' || $type == 'mediumint' || $type == 'int' ||
$type == 'bigint' ||$type == 'timestamp') {
$schema_insert .= $row[$j] . ', ';
}
// a string
else {
$dummy = '';
$srcstr = $row[$j];
for ($xx = 0; $xx < strlen($srcstr); $xx++) {
$yy = strlen($dummy);
if ($srcstr[$xx] == '\\') $dummy .= '\\\\';
if ($srcstr[$xx] == '\'') $dummy .= '\\\'';
// if ($srcstr[$xx] == '"') $dummy .= '\\"';
if ($srcstr[$xx] == "\x00") $dummy .= '\0';
if ($srcstr[$xx] == "\x0a") $dummy .= '\n';
if ($srcstr[$xx] == "\x0d") $dummy .= '\r';
// if ($srcstr[$xx] == "\x08") $dummy .= '\b';
// if ($srcstr[$xx] == "\t") $dummy .= '\t';
if ($srcstr[$xx] == "\x1a") $dummy .= '\Z';
if (strlen($dummy) == $yy) $dummy .= $srcstr[$xx];
}
$schema_insert .= "'" . $dummy . "', ";
}
} else {
$schema_insert .= "'', ";
} // end if
} // end for
$schema_insert = ereg_replace(', $', '', $schema_insert);
$schema_insert .= ')';
$handler(trim($schema_insert));
// loic1: send a fake header to bypass browser timeout if data are
// bufferized
if (!empty($GLOBALS['ob_mode'])
&& (isset($GLOBALS['zip']) || isset($GLOBALS['bzip']) || isset($GLOBALS['gzip']))) {
header('Expires: 0');
}
} // end while
mysql_free_result($result);
return TRUE;
} // end of the 'PMA_getTableContentOld()' function
/**
* Dispatches between the versions of 'getTableContent' to use depending
* on the php version
*
* Last revision 13 July 2001: Patch for limiting dump size from
* vinay@sanisoft.com & girish@sanisoft.com
*
* @param string the current database name
* @param string the current table name
* @param integer the offset on this table
* @param integer the last row to get
* @param string the name of the handler (function) to use at the end
* of every row. This handler must accept one parameter
* ($sql_insert)
* @param string the url to go back in case of error
*
* @access public
*
* @see PMA_getTableContentFast(), PMA_getTableContentOld()
*
* @author staybyte
*/
function PMA_getTableContent($db, $table, $limit_from = 0, $limit_to = 0, $handler, $error_url)
{
// Defines the offsets to use
if ($limit_from > 0) {
$limit_from--;
} else {
$limit_from = 0;
}
if ($limit_to > 0 && $limit_from >= 0) {
$add_query = " LIMIT $limit_from, $limit_to";
} else {
$add_query = '';
}
// Call the working function depending on the php version
if (PMA_PHP_INT_VERSION >= 40005) {
PMA_getTableContentFast($db, $table, $add_query, $handler, $error_url);
} else {
PMA_getTableContentOld($db, $table, $add_query, $handler, $error_url);
}
} // end of the 'PMA_getTableContent()' function
/**
* Outputs the content of a table in CSV format
*
* Last revision 14 July 2001: Patch for limiting dump size from
* vinay@sanisoft.com & girish@sanisoft.com
*
* @param string the database name
* @param string the table name
* @param integer the offset on this table
* @param integer the last row to get
* @param string the field separator character
* @param string the optionnal "enclosed by" character
* @param string the handler (function) to call. It must accept one
* parameter ($sql_insert)
* @param string the url to go back in case of error
*
* @global string whether to obtain an excel compatible csv format or a
* simple csv one
*
* @return boolean always true
*
* @access public
*/
function PMA_getTableCsv($db, $table, $limit_from = 0, $limit_to = 0, $sep, $enc_by, $esc_by, $handler, $error_url)
{
global $what;
// Handles the "separator" and the optionnal "enclosed by" characters
if ($what == 'excel') {
$sep = ',';
} else if (!isset($sep)) {
$sep = '';
} else {
if (get_magic_quotes_gpc()) {
$sep = stripslashes($sep);
}
$sep = str_replace('\\t', "\011", $sep);
}
if ($what == 'excel') {
$enc_by = '"';
} else if (!isset($enc_by)) {
$enc_by = '';
} else if (get_magic_quotes_gpc()) {
$enc_by = stripslashes($enc_by);
}
if ($what == 'excel'
|| (empty($esc_by) && $enc_by != '')) {
// double the "enclosed by" character
$esc_by = $enc_by;
} else if (!isset($esc_by)) {
$esc_by = '';
} else if (get_magic_quotes_gpc()) {
$esc_by = stripslashes($esc_by);
}
// Defines the offsets to use
if ($limit_from > 0) {
$limit_from--;
} else {
$limit_from = 0;
}
if ($limit_to > 0 && $limit_from >= 0) {
$add_query = " LIMIT $limit_from, $limit_to";
} else {
$add_query = '';
}
// Gets the data from the database
$local_query = 'SELECT * FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table) . $add_query;
$result = mysql_query($local_query) or PMA_mysqlDie('', $local_query, '', $error_url);
$fields_cnt = mysql_num_fields($result);
@set_time_limit($GLOBALS['cfgExecTimeLimit']);
// Format the data
$i = 0;
while ($row = mysql_fetch_row($result)) {
$schema_insert = '';
for ($j = 0; $j < $fields_cnt; $j++) {
if (!isset($row[$j])) {
$schema_insert .= 'NULL';
}
else if ($row[$j] == '0' || $row[$j] != '') {
// loic1 : always enclose fields
if ($what == 'excel') {
$row[$j] = ereg_replace("\015(\012)?", "\012", $row[$j]);
}
if ($enc_by == '') {
$schema_insert .= $row[$j];
} else {
$schema_insert .= $enc_by
. str_replace($enc_by, $esc_by . $enc_by, $row[$j])
. $enc_by;
}
}
else {
$schema_insert .= '';
}
if ($j < $fields_cnt-1) {
$schema_insert .= $sep;
}
} // end for
$handler(trim($schema_insert));
++$i;
// loic1: send a fake header to bypass browser timeout if data are
// bufferized
if (!empty($GLOBALS['ob_mode'])
&& (isset($GLOBALS['zip']) || isset($GLOBALS['bzip']) || isset($GLOBALS['gzip']))) {
header('Expires: 0');
}
} // end while
mysql_free_result($result);
return TRUE;
} // end of the 'PMA_getTableCsv()' function
} // $__PMA_BUILD_DUMP_LIB__
?>
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.