Merge branch 'QA_4_4' into QA_4_5
[phpmyadmin.git] / libraries / display_import.lib.php
blob4446341a20b61ec3694fb171edbe4050da7c1e49
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 /**
5 * functions for displaying import for: server, database and table
7 * @usedby display_import.inc.php
9 * @package PhpMyAdmin
12 if (! defined('PHPMYADMIN')) {
13 exit;
16 /**
17 * Prints Html For Display Import Hidden Input
19 * @param String $import_type Import type: server, database, table
20 * @param String $db Selected DB
21 * @param String $table Selected Table
23 * @return string
25 function PMA_getHtmlForHiddenInputs($import_type, $db, $table)
27 $html = '';
28 if ($import_type == 'server') {
29 $html .= PMA_URL_getHiddenInputs('', '', 1);
30 } elseif ($import_type == 'database') {
31 $html .= PMA_URL_getHiddenInputs($db, '', 1);
32 } else {
33 $html .= PMA_URL_getHiddenInputs($db, $table, 1);
35 $html .= ' <input type="hidden" name="import_type" value="'
36 . $import_type . '" />' . "\n";
38 return $html;
41 /**
42 * Prints Html For Import Javascript
44 * @param int $upload_id The selected upload id
46 * @return string
48 function PMA_getHtmlForImportJS($upload_id)
50 global $SESSION_KEY;
51 $html = '';
52 $html .= '<script type="text/javascript">';
53 $html .= ' //<![CDATA[';
54 //with "\n", so that the following lines won't be commented out by //<![CDATA[
55 $html .= "\n";
56 $html .= ' $( function() {';
57 // add event when user click on "Go" button
58 $html .= ' $("#buttonGo").bind("click", function() {';
59 // hide form
60 $html .= ' $("#upload_form_form").css("display", "none");';
62 if ($_SESSION[$SESSION_KEY]["handler"] != "UploadNoplugin") {
64 $html .= PMA_getHtmlForImportWithPlugin($upload_id);
66 } else { // no plugin available
67 $image_tag = '<img src="' . $GLOBALS['pmaThemeImage']
68 . 'ajax_clock_small.gif" width="16" height="16" alt="ajax clock" /> '
69 . PMA_jsFormat(
70 __(
71 'Please be patient, the file is being uploaded. '
72 . 'Details about the upload are not available.'
74 false
75 ) . PMA_Util::showDocu('faq', 'faq2-9');
76 $html .= " $('#upload_form_status_info').html('" . $image_tag . "');";
77 $html .= ' $("#upload_form_status").css("display", "none");';
78 } // else
80 // onclick
81 $html .= ' });';
82 // domready
83 $html .= ' });';
84 $html .= ' //]]>';
85 //with "\n", so that the following lines won't be commented out by //]]>
86 $html .= "\n";
87 $html .= '</script>';
89 return $html;
92 /**
93 * Prints Html For Display Export options
95 * @param String $import_type Import type: server, database, table
96 * @param String $db Selected DB
97 * @param String $table Selected Table
99 * @return string
101 function PMA_getHtmlForImportOptions($import_type, $db, $table)
103 $html = ' <div class="exportoptions" id="header">';
104 $html .= ' <h2>';
105 $html .= PMA_Util::getImage('b_import.png', __('Import'));
107 if ($import_type == 'server') {
108 $html .= __('Importing into the current server');
109 } elseif ($import_type == 'database') {
110 $import_str = sprintf(
111 __('Importing into the database "%s"'),
112 htmlspecialchars($db)
114 $html .= $import_str;
115 } else {
116 $import_str = sprintf(
117 __('Importing into the table "%s"'),
118 htmlspecialchars($table)
120 $html .= $import_str;
122 $html .= ' </h2>';
123 $html .= ' </div>';
125 return $html;
129 * Prints Html For Display Import options : Compressions
131 * @return string
133 function PMA_getHtmlForImportCompressions()
135 global $cfg;
136 $html = '';
137 // zip, gzip and bzip2 encode features
138 $compressions = array();
140 if ($cfg['GZipDump'] && @function_exists('gzopen')) {
141 $compressions[] = 'gzip';
143 if ($cfg['BZipDump'] && @function_exists('bzopen')) {
144 $compressions[] = 'bzip2';
146 if ($cfg['ZipDump'] && @function_exists('zip_open')) {
147 $compressions[] = 'zip';
149 // We don't have show anything about compression, when no supported
150 if ($compressions != array()) {
151 $html .= '<div class="formelementrow" id="compression_info">';
152 $compress_str = sprintf(
153 __('File may be compressed (%s) or uncompressed.'),
154 implode(", ", $compressions)
156 $html .= $compress_str;
157 $html .= '<br />';
158 $html .= __(
159 'A compressed file\'s name must end in <b>.[format].[compression]</b>. '
160 . 'Example: <b>.sql.zip</b>'
162 $html .= '</div>';
165 return $html;
169 * Prints Html For Display Import charset
171 * @return string
173 function PMA_getHtmlForImportCharset()
175 global $cfg;
176 $html = ' <div class="formelementrow" id="charaset_of_file">';
177 // charset of file
178 if ($GLOBALS['PMA_recoding_engine'] != PMA_CHARSET_NONE) {
179 $html .= '<label for="charset_of_file">' . __('Character set of the file:')
180 . '</label>';
181 reset($cfg['AvailableCharsets']);
182 $html .= '<select id="charset_of_file" name="charset_of_file" size="1">';
183 foreach ($cfg['AvailableCharsets'] as $temp_charset) {
184 $html .= '<option value="' . htmlentities($temp_charset) . '"';
185 if ((empty($cfg['Import']['charset']) && $temp_charset == 'utf-8')
186 || $temp_charset == $cfg['Import']['charset']
188 $html .= ' selected="selected"';
190 $html .= '>' . htmlentities($temp_charset) . '</option>';
192 $html .= ' </select><br />';
193 } else {
194 $html .= '<label for="charset_of_file">' . __('Character set of the file:')
195 . '</label>' . "\n";
196 $html .= PMA_generateCharsetDropdownBox(
197 PMA_CSDROPDOWN_CHARSET,
198 'charset_of_file',
199 'charset_of_file',
200 'utf8',
201 false
203 } // end if (recoding)
205 $html .= ' </div>';
207 return $html;
211 * Prints Html For Display Import options : file property
213 * @param int $max_upload_size Max upload size
214 * @param ImportPlugin[] $import_list import list
215 * @param String $local_import_file from upload directory
217 * @return string
219 function PMA_getHtmlForImportOptionsFile(
220 $max_upload_size, $import_list, $local_import_file
222 global $cfg;
223 $html = ' <div class="importoptions">';
224 $html .= ' <h3>' . __('File to import:') . '</h3>';
225 $html .= PMA_getHtmlForImportCompressions();
226 $html .= ' <div class="formelementrow" id="upload_form">';
228 if ($GLOBALS['is_upload'] && !empty($cfg['UploadDir'])) {
229 $html .= ' <ul>';
230 $html .= ' <li>';
231 $html .= ' <input type="radio" name="file_location" '
232 . 'id="radio_import_file" required="required" />';
233 $html .= PMA_Util::getBrowseUploadFileBlock($max_upload_size);
234 $html .= '<br />' . __('You may also drag and drop a file on any page.');
235 $html .= ' </li>';
236 $html .= ' <li>';
237 $html .= ' <input type="radio" name="file_location" '
238 . 'id="radio_local_import_file"';
239 if (! empty($GLOBALS['timeout_passed'])
240 && ! empty($local_import_file)
242 $html .= ' checked="checked"';
244 $html .= ' />';
245 $html .= PMA_Util::getSelectUploadFileBlock($import_list, $cfg['UploadDir']);
246 $html .= ' </li>';
247 $html .= ' </ul>';
249 } elseif ($GLOBALS['is_upload']) {
250 $html .= PMA_Util::getBrowseUploadFileBlock($max_upload_size);
251 $html .= '<br />' . __('You may also drag and drop a file on any page.');
252 } elseif (!$GLOBALS['is_upload']) {
253 $html .= PMA_Message::notice(
254 __('File uploads are not allowed on this server.')
255 )->getDisplay();
256 } elseif (!empty($cfg['UploadDir'])) {
257 $html .= PMA_Util::getSelectUploadFileBlock($import_list, $cfg['UploadDir']);
258 } // end if (web-server upload directory)
260 $html .= ' </div>';
261 $html .= PMA_getHtmlForImportCharset();
262 $html .= ' </div>';
264 return $html;
268 * Prints Html For Display Import options : Partial Import
270 * @param String $timeout_passed timeout passed
271 * @param String $offset timeout offset
273 * @return string
275 function PMA_getHtmlForImportOptionsPartialImport($timeout_passed, $offset)
277 $html = ' <div class="importoptions">';
278 $html .= ' <h3>' . __('Partial import:') . '</h3>';
280 if (isset($timeout_passed) && $timeout_passed) {
281 $html .= '<div class="formelementrow">' . "\n";
282 $html .= '<input type="hidden" name="skip" value="' . $offset . '" />';
283 $html .= sprintf(
285 'Previous import timed out, after resubmitting '
286 . 'will continue from position %d.'
288 $offset
290 $html .= '</div>' . "\n";
293 $html .= ' <div class="formelementrow">';
294 $html .= ' <input type="checkbox" name="allow_interrupt" value="yes"';
295 $html .= ' id="checkbox_allow_interrupt" '
296 . PMA_pluginCheckboxCheck('Import', 'allow_interrupt') . '/>';
297 $html .= ' <label for="checkbox_allow_interrupt">'
298 . __(
299 'Allow the interruption of an import in case the script detects '
300 . 'it is close to the PHP timeout limit. <i>(This might be a good way'
301 . ' to import large files, however it can break transactions.)</i>'
302 ) . '</label><br />';
303 $html .= ' </div>';
305 if (! (isset($timeout_passed) && $timeout_passed)) {
306 $html .= ' <div class="formelementrow">';
307 $html .= ' <label for="text_skip_queries">'
308 . __(
309 'Skip this number of queries (for SQL) or lines (for other '
310 . 'formats), starting from the first one:'
312 . '</label>';
313 $html .= ' <input type="number" name="skip_queries" value="'
314 . PMA_pluginGetDefault('Import', 'skip_queries')
315 . '" id="text_skip_queries" min="0" />';
316 $html .= ' </div>';
318 } else {
319 // If timeout has passed,
320 // do not show the Skip dialog to avoid the risk of someone
321 // entering a value here that would interfere with "skip"
322 $html .= ' <input type="hidden" name="skip_queries" value="'
323 . PMA_pluginGetDefault('Import', 'skip_queries')
324 . '" id="text_skip_queries" />';
327 $html .= ' </div>';
329 return $html;
333 * Prints Html For Display Import options : Other
335 * @return string
337 function PMA_getHtmlForImportOptionsOther()
339 $html = ' <div class="importoptions">';
340 $html .= ' <h3>' . __('Other options:') . '</h3>';
341 $html .= ' <div class="formelementrow">';
342 $html .= PMA_Util::getFKCheckbox();
343 $html .= ' </div>';
344 $html .= ' </div>';
346 return $html;
350 * Prints Html For Display Import options : Format
352 * @param ImportPlugin[] $import_list import list
354 * @return string
356 function PMA_getHtmlForImportOptionsFormat($import_list)
358 $html = ' <div class="importoptions">';
359 $html .= ' <h3>' . __('Format:') . '</h3>';
360 $html .= PMA_pluginGetChoice('Import', 'format', $import_list);
361 $html .= ' <div id="import_notification"></div>';
362 $html .= ' </div>';
364 $html .= ' <div class="importoptions" id="format_specific_opts">';
365 $html .= ' <h3>' . __('Format-specific options:') . '</h3>';
366 $html .= ' <p class="no_js_msg" id="scroll_to_options_msg">'
367 . 'Scroll down to fill in the options for the selected format '
368 . 'and ignore the options for other formats.</p>';
369 $html .= PMA_pluginGetOptions('Import', $import_list);
370 $html .= ' </div>';
371 $html .= ' <div class="clearfloat"></div>';
373 // Encoding setting form appended by Y.Kawada
374 if (function_exists('PMA_Kanji_encodingForm')) {
375 $html .= ' <div class="importoptions" id="kanji_encoding">';
376 $html .= ' <h3>' . __('Encoding Conversion:') . '</h3>';
377 $html .= PMA_Kanji_encodingForm();
378 $html .= ' </div>';
381 $html .= "\n";
383 return $html;
387 * Prints Html For Display Import options : submit
389 * @return string
391 function PMA_getHtmlForImportOptionsSubmit()
393 $html = ' <div class="importoptions" id="submit">';
394 $html .= ' <input type="submit" value="' . __('Go') . '" id="buttonGo" />';
395 $html .= ' </div>';
397 return $html;
401 * Prints Html For Display Import
403 * @param int $upload_id The selected upload id
404 * @param String $import_type Import type: server, database, table
405 * @param String $db Selected DB
406 * @param String $table Selected Table
407 * @param int $max_upload_size Max upload size
408 * @param ImportPlugin[] $import_list Import list
409 * @param String $timeout_passed Timeout passed
410 * @param String $offset Timeout offset
411 * @param String $local_import_file from upload directory
413 * @return string
415 function PMA_getHtmlForImport(
416 $upload_id, $import_type, $db, $table,
417 $max_upload_size, $import_list, $timeout_passed, $offset, $local_import_file
419 global $SESSION_KEY;
420 $html = '';
421 $html .= '<iframe id="import_upload_iframe" name="import_upload_iframe" '
422 . 'width="1" height="1" style="display: none;"></iframe>';
423 $html .= '<div id="import_form_status" style="display: none;"></div>';
424 $html .= '<div id="importmain">';
425 $html .= ' <img src="' . $GLOBALS['pmaThemeImage'] . 'ajax_clock_small.gif" '
426 . 'width="16" height="16" alt="ajax clock" style="display: none;" />';
428 $html .= PMA_getHtmlForImportJS($upload_id);
430 $html .= ' <form id="import_file_form" action="import.php" method="post" '
431 . 'enctype="multipart/form-data"';
432 $html .= ' name="import"';
433 if ($_SESSION[$SESSION_KEY]["handler"] != "UploadNoplugin") {
434 $html .= ' target="import_upload_iframe"';
436 $html .= ' class="ajax"';
437 $html .= '>';
438 $html .= ' <input type="hidden" name="';
439 $html .= $_SESSION[$SESSION_KEY]['handler']::getIdKey();
440 $html .= '" value="' . $upload_id . '" />';
442 $html .= PMA_getHtmlForHiddenInputs($import_type, $db, $table);
444 $html .= PMA_getHtmlForImportOptions($import_type, $db, $table);
446 $html .= PMA_getHtmlForImportOptionsFile(
447 $max_upload_size, $import_list, $local_import_file
450 $html .= PMA_getHtmlForImportOptionsPartialImport($timeout_passed, $offset);
452 $html .= PMA_getHtmlForImportOptionsOther();
454 $html .= PMA_getHtmlForImportOptionsFormat($import_list);
456 $html .= PMA_getHtmlForImportOptionsSubmit();
458 $html .= '</form>';
459 $html .= '</div>';
461 return $html;
465 * Prints javascript for upload with plugin, upload process bar
467 * @param int $upload_id The selected upload id
469 * @return string
471 function PMA_getHtmlForImportWithPlugin($upload_id)
473 //some variable for javascript
474 $ajax_url = "import_status.php?id=" . $upload_id . "&"
475 . PMA_URL_getCommon(array('import_status'=>1), 'text');
476 $promot_str = PMA_jsFormat(
478 'The file being uploaded is probably larger than '
479 . 'the maximum allowed size or this is a known bug in webkit '
480 . 'based (Safari, Google Chrome, Arora etc.) browsers.'
482 false
484 $statustext_str = PMA_escapeJsString(__('%s of %s'));
485 $upload_str = PMA_jsFormat(__('Uploading your import file…'), false);
486 $second_str = PMA_jsFormat(__('%s/sec.'), false);
487 $remaining_min = PMA_jsFormat(__('About %MIN min. %SEC sec. remaining.'), false);
488 $remaining_second = PMA_jsFormat(__('About %SEC sec. remaining.'), false);
489 $processed_str = PMA_jsFormat(
490 __('The file is being processed, please be patient.'),
491 false
493 $import_url = PMA_URL_getCommon(array('import_status'=>1), 'text');
495 //start output
496 $html = 'var finished = false; ';
497 $html .= 'var percent = 0.0; ';
498 $html .= 'var total = 0; ';
499 $html .= 'var complete = 0; ';
500 $html .= 'var original_title = '
501 . 'parent && parent.document ? parent.document.title : false; ';
502 $html .= 'var import_start; ';
504 $html .= 'var perform_upload = function () { ';
505 $html .= 'new $.getJSON( ';
506 $html .= ' "' . $ajax_url . '", ';
507 $html .= ' {}, ';
508 $html .= ' function(response) { ';
509 $html .= ' finished = response.finished; ';
510 $html .= ' percent = response.percent; ';
511 $html .= ' total = response.total; ';
512 $html .= ' complete = response.complete; ';
514 $html .= ' if (total==0 && complete==0 && percent==0) { ';
515 $img_tag = '<img src="' . $GLOBALS['pmaThemeImage'] . 'ajax_clock_small.gif"';
516 $html .= ' $("#upload_form_status_info").html(\''
517 . $img_tag . ' width="16" height="16" alt="ajax clock" /> '
518 . $promot_str . '\'); ';
519 $html .= ' $("#upload_form_status").css("display", "none"); ';
520 $html .= ' } else { ';
521 $html .= ' var now = new Date(); ';
522 $html .= ' now = Date.UTC( ';
523 $html .= ' now.getFullYear(), ';
524 $html .= ' now.getMonth(), ';
525 $html .= ' now.getDate(), ';
526 $html .= ' now.getHours(), ';
527 $html .= ' now.getMinutes(), ';
528 $html .= ' now.getSeconds()) ';
529 $html .= ' + now.getMilliseconds() - 1000; ';
530 $html .= ' var statustext = PMA_sprintf(';
531 $html .= ' "' . $statustext_str . '", ';
532 $html .= ' formatBytes( ';
533 $html .= ' complete, 1, PMA_messages.strDecimalSeparator';
534 $html .= ' ), ';
535 $html .= ' formatBytes(';
536 $html .= ' total, 1, PMA_messages.strDecimalSeparator';
537 $html .= ' ) ';
538 $html .= ' ); ';
540 $html .= ' if ($("#importmain").is(":visible")) { ';
541 // show progress UI
542 $html .= ' $("#importmain").hide(); ';
543 $html .= ' $("#import_form_status") ';
544 $html .= ' .html(\'<div class="upload_progress">'
545 . '<div class="upload_progress_bar_outer"><div class="percentage">'
546 . '</div><div id="status" class="upload_progress_bar_inner">'
547 . '<div class="percentage"></div></div></div><div>'
548 . '<img src="' . $GLOBALS['pmaThemeImage']
549 . 'ajax_clock_small.gif" width="16" height="16" alt="ajax clock" /> '
550 . $upload_str . '</div><div id="statustext"></div></div>\') ';
551 $html .= ' .show(); ';
552 $html .= ' import_start = now; ';
553 $html .= ' } ';
554 $html .= ' else if (percent > 9 || complete > 2000000) { ';
555 // calculate estimated time
556 $html .= ' var used_time = now - import_start; ';
557 $html .= ' var seconds = '
558 . 'parseInt(((total - complete) / complete) * used_time / 1000); ';
559 $html .= ' var speed = PMA_sprintf("' . $second_str . '"';
560 $html .= ' , formatBytes(complete / used_time * 1000, 1,'
561 . ' PMA_messages.strDecimalSeparator)); ';
563 $html .= ' var minutes = parseInt(seconds / 60); ';
564 $html .= ' seconds %= 60; ';
565 $html .= ' var estimated_time; ';
566 $html .= ' if (minutes > 0) { ';
567 $html .= ' estimated_time = "' . $remaining_min . '"';
568 $html .= ' .replace("%MIN", minutes)';
569 $html .= ' .replace("%SEC", seconds); ';
570 $html .= ' } ';
571 $html .= ' else { ';
572 $html .= ' estimated_time = "' . $remaining_second . '"';
573 $html .= ' .replace("%SEC", seconds); ';
574 $html .= ' } ';
576 $html .= ' statustext += "<br />" + speed + "<br /><br />" '
577 . '+ estimated_time; ';
578 $html .= ' } ';
580 $html .= ' var percent_str = Math.round(percent) + "%"; ';
581 $html .= ' $("#status").animate({width: percent_str}, 150); ';
582 $html .= ' $(".percentage").text(percent_str); ';
584 // show percent in window title
585 $html .= ' if (original_title !== false) { ';
586 $html .= ' parent.document.title ';
587 $html .= ' = percent_str + " - " + original_title; ';
588 $html .= ' } ';
589 $html .= ' else { ';
590 $html .= ' document.title ';
591 $html .= ' = percent_str + " - " + original_title; ';
592 $html .= ' } ';
593 $html .= ' $("#statustext").html(statustext); ';
594 $html .= ' } ';
596 $html .= ' if (finished == true) { ';
597 $html .= ' if (original_title !== false) { ';
598 $html .= ' parent.document.title = original_title; ';
599 $html .= ' } ';
600 $html .= ' else { ';
601 $html .= ' document.title = original_title; ';
602 $html .= ' } ';
603 $html .= ' $("#importmain").hide(); ';
604 // loads the message, either success or mysql error
605 $html .= ' $("#import_form_status") ';
606 $html .= ' .html(\'<img src="' . $GLOBALS['pmaThemeImage']
607 . 'ajax_clock_small.gif" width="16" height="16" alt="ajax clock" /> '
608 . $processed_str . '\')';
609 $html .= ' .show(); ';
610 $html .= ' $("#import_form_status").load("import_status.php?'
611 . 'message=true&' . $import_url . '"); ';
612 $html .= ' PMA_reloadNavigation(); ';
614 // if finished
615 $html .= ' } ';
616 $html .= ' else { ';
617 $html .= ' setTimeout(perform_upload, 1000); ';
618 $html .= ' } ';
619 $html .= '}); ';
620 $html .= '}; ';
621 $html .= 'setTimeout(perform_upload, 1000); ';
623 return $html;
627 * Gets HTML to display import dialogs
629 * @param String $import_type Import type: server|database|table
630 * @param String $db Selected DB
631 * @param String $table Selected Table
632 * @param int $max_upload_size Max upload size
634 * @return string $html
636 function PMA_getImportDisplay($import_type, $db, $table, $max_upload_size)
638 global $SESSION_KEY;
639 include_once './libraries/file_listing.lib.php';
640 include_once './libraries/plugin_interface.lib.php';
641 // this one generates also some globals
642 include_once './libraries/display_import_ajax.lib.php';
644 /* Scan for plugins */
645 /* @var $import_list ImportPlugin[] */
646 $import_list = PMA_getPlugins(
647 "import",
648 'libraries/plugins/import/',
649 $import_type
652 /* Fail if we didn't find any plugin */
653 if (empty($import_list)) {
654 PMA_Message::error(
656 'Could not load import plugins, please check your installation!'
658 )->display();
659 exit;
662 if (PMA_isValid($_REQUEST['offset'], 'numeric')) {
663 $offset = $_REQUEST['offset'];
665 if (isset($_REQUEST['timeout_passed'])) {
666 $timeout_passed = $_REQUEST['timeout_passed'];
669 $local_import_file = '';
670 if (isset($_REQUEST['local_import_file'])) {
671 $local_import_file = $_REQUEST['local_import_file'];
674 $timeout_passed_str = isset($timeout_passed)? $timeout_passed : null;
675 $offset_str = isset($offset)? $offset : null;
676 return PMA_getHtmlForImport(
677 $upload_id,
678 $import_type,
679 $db,
680 $table,
681 $max_upload_size,
682 $import_list,
683 $timeout_passed_str,
684 $offset_str,
685 $local_import_file