Translated using Weblate (Slovenian)
[phpmyadmin.git] / libraries / display_import.lib.php
bloba0a5fcf66a6b9133deb3895bb8588cd2823812ca
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
11 use PMA\libraries\Charsets;
12 use PMA\libraries\Encoding;
13 use PMA\libraries\Message;
14 use PMA\libraries\plugins\ImportPlugin;
15 use PMA\libraries\URL;
16 use PMA\libraries\Sanitize;
18 /**
19 * Prints Html For Display Import Hidden Input
21 * @param String $import_type Import type: server, database, table
22 * @param String $db Selected DB
23 * @param String $table Selected Table
25 * @return string
27 function PMA_getHtmlForHiddenInputs($import_type, $db, $table)
29 $html = '';
30 if ($import_type == 'server') {
31 $html .= URL::getHiddenInputs('', '', 1);
32 } elseif ($import_type == 'database') {
33 $html .= URL::getHiddenInputs($db, '', 1);
34 } else {
35 $html .= URL::getHiddenInputs($db, $table, 1);
37 $html .= ' <input type="hidden" name="import_type" value="'
38 . $import_type . '" />' . "\n";
40 return $html;
43 /**
44 * Prints Html For Import Javascript
46 * @param int $upload_id The selected upload id
48 * @return string
50 function PMA_getHtmlForImportJS($upload_id)
52 global $SESSION_KEY;
53 $html = '';
54 $html .= '<script type="text/javascript">';
55 $html .= ' //<![CDATA[';
56 //with "\n", so that the following lines won't be commented out by //<![CDATA[
57 $html .= "\n";
58 $html .= ' $( function() {';
59 // add event when user click on "Go" button
60 $html .= ' $("#buttonGo").bind("click", function() {';
61 // hide form
62 $html .= ' $("#upload_form_form").css("display", "none");';
64 if ($_SESSION[$SESSION_KEY]["handler"] != 'PMA\libraries\plugins\import\upload\UploadNoplugin') {
66 $html .= PMA_getHtmlForImportWithPlugin($upload_id);
68 } else { // no plugin available
69 $image_tag = '<img src="' . $GLOBALS['pmaThemeImage']
70 . 'ajax_clock_small.gif" width="16" height="16" alt="ajax clock" /> '
71 . Sanitize::jsFormat(
72 __(
73 'Please be patient, the file is being uploaded. '
74 . 'Details about the upload are not available.'
76 false
77 ) . PMA\libraries\Util::showDocu('faq', 'faq2-9');
78 $html .= " $('#upload_form_status_info').html('" . $image_tag . "');";
79 $html .= ' $("#upload_form_status").css("display", "none");';
80 } // else
82 // onclick
83 $html .= ' });';
84 // domready
85 $html .= ' });';
86 $html .= ' //]]>';
87 //with "\n", so that the following lines won't be commented out by //]]>
88 $html .= "\n";
89 $html .= '</script>';
91 return $html;
94 /**
95 * Prints Html For Display Export options
97 * @param String $import_type Import type: server, database, table
98 * @param String $db Selected DB
99 * @param String $table Selected Table
101 * @return string
103 function PMA_getHtmlForImportOptions($import_type, $db, $table)
105 $html = ' <div class="exportoptions" id="header">';
106 $html .= ' <h2>';
107 $html .= PMA\libraries\Util::getImage('b_import.png', __('Import'));
109 if ($import_type == 'server') {
110 $html .= __('Importing into the current server');
111 } elseif ($import_type == 'database') {
112 $import_str = sprintf(
113 __('Importing into the database "%s"'),
114 htmlspecialchars($db)
116 $html .= $import_str;
117 } else {
118 $import_str = sprintf(
119 __('Importing into the table "%s"'),
120 htmlspecialchars($table)
122 $html .= $import_str;
124 $html .= ' </h2>';
125 $html .= ' </div>';
127 return $html;
131 * Prints Html For Display Import options : Compressions
133 * @return string
135 function PMA_getHtmlForImportCompressions()
137 global $cfg;
138 $html = '';
139 // zip, gzip and bzip2 encode features
140 $compressions = array();
142 if ($cfg['GZipDump'] && @function_exists('gzopen')) {
143 $compressions[] = 'gzip';
145 if ($cfg['BZipDump'] && @function_exists('bzopen')) {
146 $compressions[] = 'bzip2';
148 if ($cfg['ZipDump'] && @function_exists('zip_open')) {
149 $compressions[] = 'zip';
151 // We don't have show anything about compression, when no supported
152 if ($compressions != array()) {
153 $html .= '<div class="formelementrow" id="compression_info">';
154 $compress_str = sprintf(
155 __('File may be compressed (%s) or uncompressed.'),
156 implode(", ", $compressions)
158 $html .= $compress_str;
159 $html .= '<br />';
160 $html .= __(
161 'A compressed file\'s name must end in <b>.[format].[compression]</b>. '
162 . 'Example: <b>.sql.zip</b>'
164 $html .= '</div>';
167 return $html;
171 * Prints Html For Display Import charset
173 * @return string
175 function PMA_getHtmlForImportCharset()
177 global $cfg;
178 $html = ' <div class="formelementrow" id="charaset_of_file">';
179 // charset of file
180 if (Encoding::isSupported()) {
181 $html .= '<label for="charset_of_file">' . __('Character set of the file:')
182 . '</label>';
183 $html .= '<select id="charset_of_file" name="charset_of_file" size="1">';
184 foreach ($cfg['AvailableCharsets'] as $temp_charset) {
185 $html .= '<option value="' . htmlentities($temp_charset) . '"';
186 if ((empty($cfg['Import']['charset']) && $temp_charset == 'utf-8')
187 || $temp_charset == $cfg['Import']['charset']
189 $html .= ' selected="selected"';
191 $html .= '>' . htmlentities($temp_charset) . '</option>';
193 $html .= ' </select><br />';
194 } else {
195 $html .= '<label for="charset_of_file">' . __('Character set of the file:')
196 . '</label>' . "\n";
197 $html .= Charsets::getCharsetDropdownBox(
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\libraries\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\libraries\Util::getSelectUploadFileBlock(
246 $import_list,
247 $cfg['UploadDir']
249 $html .= ' </li>';
250 $html .= ' </ul>';
252 } elseif ($GLOBALS['is_upload']) {
253 $html .= PMA\libraries\Util::getBrowseUploadFileBlock($max_upload_size);
254 $html .= '<br />' . __('You may also drag and drop a file on any page.');
255 } elseif (!$GLOBALS['is_upload']) {
256 $html .= Message::notice(
257 __('File uploads are not allowed on this server.')
258 )->getDisplay();
259 } elseif (!empty($cfg['UploadDir'])) {
260 $html .= PMA\libraries\Util::getSelectUploadFileBlock(
261 $import_list,
262 $cfg['UploadDir']
264 } // end if (web-server upload directory)
266 $html .= ' </div>';
267 $html .= PMA_getHtmlForImportCharset();
268 $html .= ' </div>';
270 return $html;
274 * Prints Html For Display Import options : Partial Import
276 * @param String $timeout_passed timeout passed
277 * @param String $offset timeout offset
279 * @return string
281 function PMA_getHtmlForImportOptionsPartialImport($timeout_passed, $offset)
283 $html = ' <div class="importoptions">';
284 $html .= ' <h3>' . __('Partial import:') . '</h3>';
286 if (isset($timeout_passed) && $timeout_passed) {
287 $html .= '<div class="formelementrow">' . "\n";
288 $html .= '<input type="hidden" name="skip" value="' . $offset . '" />';
289 $html .= sprintf(
291 'Previous import timed out, after resubmitting '
292 . 'will continue from position %d.'
294 $offset
296 $html .= '</div>' . "\n";
299 $html .= ' <div class="formelementrow">';
300 $html .= ' <input type="checkbox" name="allow_interrupt" value="yes"';
301 $html .= ' id="checkbox_allow_interrupt" '
302 . PMA_pluginCheckboxCheck('Import', 'allow_interrupt') . '/>';
303 $html .= ' <label for="checkbox_allow_interrupt">'
304 . __(
305 'Allow the interruption of an import in case the script detects '
306 . 'it is close to the PHP timeout limit. <i>(This might be a good way'
307 . ' to import large files, however it can break transactions.)</i>'
308 ) . '</label><br />';
309 $html .= ' </div>';
311 if (! (isset($timeout_passed) && $timeout_passed)) {
312 $html .= ' <div class="formelementrow">';
313 $html .= ' <label for="text_skip_queries">'
314 . __(
315 'Skip this number of queries (for SQL) starting from the first one:'
317 . '</label>';
318 $html .= ' <input type="number" name="skip_queries" value="'
319 . PMA_pluginGetDefault('Import', 'skip_queries')
320 . '" id="text_skip_queries" min="0" />';
321 $html .= ' </div>';
323 } else {
324 // If timeout has passed,
325 // do not show the Skip dialog to avoid the risk of someone
326 // entering a value here that would interfere with "skip"
327 $html .= ' <input type="hidden" name="skip_queries" value="'
328 . PMA_pluginGetDefault('Import', 'skip_queries')
329 . '" id="text_skip_queries" />';
332 $html .= ' </div>';
334 return $html;
338 * Prints Html For Display Import options : Other
340 * @return string
342 function PMA_getHtmlForImportOptionsOther()
344 $html = ' <div class="importoptions">';
345 $html .= ' <h3>' . __('Other options:') . '</h3>';
346 $html .= ' <div class="formelementrow">';
347 $html .= PMA\libraries\Util::getFKCheckbox();
348 $html .= ' </div>';
349 $html .= ' </div>';
351 return $html;
355 * Prints Html For Display Import options : Format
357 * @param ImportPlugin[] $import_list import list
359 * @return string
361 function PMA_getHtmlForImportOptionsFormat($import_list)
363 $html = ' <div class="importoptions">';
364 $html .= ' <h3>' . __('Format:') . '</h3>';
365 $html .= PMA_pluginGetChoice('Import', 'format', $import_list);
366 $html .= ' <div id="import_notification"></div>';
367 $html .= ' </div>';
369 $html .= ' <div class="importoptions" id="format_specific_opts">';
370 $html .= ' <h3>' . __('Format-specific options:') . '</h3>';
371 $html .= ' <p class="no_js_msg" id="scroll_to_options_msg">'
372 . 'Scroll down to fill in the options for the selected format '
373 . 'and ignore the options for other formats.</p>';
374 $html .= PMA_pluginGetOptions('Import', $import_list);
375 $html .= ' </div>';
376 $html .= ' <div class="clearfloat"></div>';
378 // Japanese encoding setting
379 if (Encoding::canConvertKanji()) {
380 $html .= ' <div class="importoptions" id="kanji_encoding">';
381 $html .= ' <h3>' . __('Encoding Conversion:') . '</h3>';
382 $html .= Encoding::kanjiEncodingForm();
383 $html .= ' </div>';
386 $html .= "\n";
388 return $html;
392 * Prints Html For Display Import options : submit
394 * @return string
396 function PMA_getHtmlForImportOptionsSubmit()
398 $html = ' <div class="importoptions" id="submit">';
399 $html .= ' <input type="submit" value="' . __('Go') . '" id="buttonGo" />';
400 $html .= ' </div>';
402 return $html;
406 * Prints Html For Display Import
408 * @param int $upload_id The selected upload id
409 * @param String $import_type Import type: server, database, table
410 * @param String $db Selected DB
411 * @param String $table Selected Table
412 * @param int $max_upload_size Max upload size
413 * @param ImportPlugin[] $import_list Import list
414 * @param String $timeout_passed Timeout passed
415 * @param String $offset Timeout offset
416 * @param String $local_import_file from upload directory
418 * @return string
420 function PMA_getHtmlForImport(
421 $upload_id, $import_type, $db, $table,
422 $max_upload_size, $import_list, $timeout_passed, $offset, $local_import_file
424 global $SESSION_KEY;
425 $html = '';
426 $html .= '<iframe id="import_upload_iframe" name="import_upload_iframe" '
427 . 'width="1" height="1" style="display: none;"></iframe>';
428 $html .= '<div id="import_form_status" style="display: none;"></div>';
429 $html .= '<div id="importmain">';
430 $html .= ' <img src="' . $GLOBALS['pmaThemeImage'] . 'ajax_clock_small.gif" '
431 . 'width="16" height="16" alt="ajax clock" style="display: none;" />';
433 $html .= PMA_getHtmlForImportJS($upload_id);
435 $html .= ' <form id="import_file_form" action="import.php" method="post" '
436 . 'enctype="multipart/form-data"';
437 $html .= ' name="import"';
438 if ($_SESSION[$SESSION_KEY]["handler"] != 'PMA\libraries\plugins\import\upload\UploadNoplugin') {
439 $html .= ' target="import_upload_iframe"';
441 $html .= ' class="ajax"';
442 $html .= '>';
443 $html .= ' <input type="hidden" name="';
444 $html .= $_SESSION[$SESSION_KEY]['handler']::getIdKey();
445 $html .= '" value="' . $upload_id . '" />';
447 $html .= PMA_getHtmlForHiddenInputs($import_type, $db, $table);
449 $html .= PMA_getHtmlForImportOptions($import_type, $db, $table);
451 $html .= PMA_getHtmlForImportOptionsFile(
452 $max_upload_size, $import_list, $local_import_file
455 $html .= PMA_getHtmlForImportOptionsPartialImport($timeout_passed, $offset);
457 $html .= PMA_getHtmlForImportOptionsOther();
459 $html .= PMA_getHtmlForImportOptionsFormat($import_list);
461 $html .= PMA_getHtmlForImportOptionsSubmit();
463 $html .= '</form>';
464 $html .= '</div>';
466 return $html;
470 * Prints javascript for upload with plugin, upload process bar
472 * @param int $upload_id The selected upload id
474 * @return string
476 function PMA_getHtmlForImportWithPlugin($upload_id)
478 //some variable for javascript
479 $ajax_url = "import_status.php?id=" . $upload_id . "&"
480 . URL::getCommonRaw(array('import_status'=>1));
481 $promot_str = Sanitize::jsFormat(
483 'The file being uploaded is probably larger than '
484 . 'the maximum allowed size or this is a known bug in webkit '
485 . 'based (Safari, Google Chrome, Arora etc.) browsers.'
487 false
489 $statustext_str = Sanitize::escapeJsString(__('%s of %s'));
490 $upload_str = Sanitize::jsFormat(__('Uploading your import file…'), false);
491 $second_str = Sanitize::jsFormat(__('%s/sec.'), false);
492 $remaining_min = Sanitize::jsFormat(__('About %MIN min. %SEC sec. remaining.'), false);
493 $remaining_second = Sanitize::jsFormat(__('About %SEC sec. remaining.'), false);
494 $processed_str = Sanitize::jsFormat(
495 __('The file is being processed, please be patient.'),
496 false
498 $import_url = URL::getCommonRaw(array('import_status'=>1));
500 //start output
501 $html = 'var finished = false; ';
502 $html .= 'var percent = 0.0; ';
503 $html .= 'var total = 0; ';
504 $html .= 'var complete = 0; ';
505 $html .= 'var original_title = '
506 . 'parent && parent.document ? parent.document.title : false; ';
507 $html .= 'var import_start; ';
509 $html .= 'var perform_upload = function () { ';
510 $html .= 'new $.getJSON( ';
511 $html .= ' "' . $ajax_url . '", ';
512 $html .= ' {}, ';
513 $html .= ' function(response) { ';
514 $html .= ' finished = response.finished; ';
515 $html .= ' percent = response.percent; ';
516 $html .= ' total = response.total; ';
517 $html .= ' complete = response.complete; ';
519 $html .= ' if (total==0 && complete==0 && percent==0) { ';
520 $img_tag = '<img src="' . $GLOBALS['pmaThemeImage'] . 'ajax_clock_small.gif"';
521 $html .= ' $("#upload_form_status_info").html(\''
522 . $img_tag . ' width="16" height="16" alt="ajax clock" /> '
523 . $promot_str . '\'); ';
524 $html .= ' $("#upload_form_status").css("display", "none"); ';
525 $html .= ' } else { ';
526 $html .= ' var now = new Date(); ';
527 $html .= ' now = Date.UTC( ';
528 $html .= ' now.getFullYear(), ';
529 $html .= ' now.getMonth(), ';
530 $html .= ' now.getDate(), ';
531 $html .= ' now.getHours(), ';
532 $html .= ' now.getMinutes(), ';
533 $html .= ' now.getSeconds()) ';
534 $html .= ' + now.getMilliseconds() - 1000; ';
535 $html .= ' var statustext = PMA_sprintf(';
536 $html .= ' "' . $statustext_str . '", ';
537 $html .= ' formatBytes( ';
538 $html .= ' complete, 1, PMA_messages.strDecimalSeparator';
539 $html .= ' ), ';
540 $html .= ' formatBytes(';
541 $html .= ' total, 1, PMA_messages.strDecimalSeparator';
542 $html .= ' ) ';
543 $html .= ' ); ';
545 $html .= ' if ($("#importmain").is(":visible")) { ';
546 // show progress UI
547 $html .= ' $("#importmain").hide(); ';
548 $html .= ' $("#import_form_status") ';
549 $html .= ' .html(\'<div class="upload_progress">'
550 . '<div class="upload_progress_bar_outer"><div class="percentage">'
551 . '</div><div id="status" class="upload_progress_bar_inner">'
552 . '<div class="percentage"></div></div></div><div>'
553 . '<img src="' . $GLOBALS['pmaThemeImage']
554 . 'ajax_clock_small.gif" width="16" height="16" alt="ajax clock" /> '
555 . $upload_str . '</div><div id="statustext"></div></div>\') ';
556 $html .= ' .show(); ';
557 $html .= ' import_start = now; ';
558 $html .= ' } ';
559 $html .= ' else if (percent > 9 || complete > 2000000) { ';
560 // calculate estimated time
561 $html .= ' var used_time = now - import_start; ';
562 $html .= ' var seconds = '
563 . 'parseInt(((total - complete) / complete) * used_time / 1000); ';
564 $html .= ' var speed = PMA_sprintf("' . $second_str . '"';
565 $html .= ' , formatBytes(complete / used_time * 1000, 1,'
566 . ' PMA_messages.strDecimalSeparator)); ';
568 $html .= ' var minutes = parseInt(seconds / 60); ';
569 $html .= ' seconds %= 60; ';
570 $html .= ' var estimated_time; ';
571 $html .= ' if (minutes > 0) { ';
572 $html .= ' estimated_time = "' . $remaining_min . '"';
573 $html .= ' .replace("%MIN", minutes)';
574 $html .= ' .replace("%SEC", seconds); ';
575 $html .= ' } ';
576 $html .= ' else { ';
577 $html .= ' estimated_time = "' . $remaining_second . '"';
578 $html .= ' .replace("%SEC", seconds); ';
579 $html .= ' } ';
581 $html .= ' statustext += "<br />" + speed + "<br /><br />" '
582 . '+ estimated_time; ';
583 $html .= ' } ';
585 $html .= ' var percent_str = Math.round(percent) + "%"; ';
586 $html .= ' $("#status").animate({width: percent_str}, 150); ';
587 $html .= ' $(".percentage").text(percent_str); ';
589 // show percent in window title
590 $html .= ' if (original_title !== false) { ';
591 $html .= ' parent.document.title ';
592 $html .= ' = percent_str + " - " + original_title; ';
593 $html .= ' } ';
594 $html .= ' else { ';
595 $html .= ' document.title ';
596 $html .= ' = percent_str + " - " + original_title; ';
597 $html .= ' } ';
598 $html .= ' $("#statustext").html(statustext); ';
599 $html .= ' } ';
601 $html .= ' if (finished == true) { ';
602 $html .= ' if (original_title !== false) { ';
603 $html .= ' parent.document.title = original_title; ';
604 $html .= ' } ';
605 $html .= ' else { ';
606 $html .= ' document.title = original_title; ';
607 $html .= ' } ';
608 $html .= ' $("#importmain").hide(); ';
609 // loads the message, either success or mysql error
610 $html .= ' $("#import_form_status") ';
611 $html .= ' .html(\'<img src="' . $GLOBALS['pmaThemeImage']
612 . 'ajax_clock_small.gif" width="16" height="16" alt="ajax clock" /> '
613 . $processed_str . '\')';
614 $html .= ' .show(); ';
615 $html .= ' $("#import_form_status").load("import_status.php?'
616 . 'message=true&' . $import_url . '"); ';
617 $html .= ' PMA_reloadNavigation(); ';
619 // if finished
620 $html .= ' } ';
621 $html .= ' else { ';
622 $html .= ' setTimeout(perform_upload, 1000); ';
623 $html .= ' } ';
624 $html .= '}); ';
625 $html .= '}; ';
626 $html .= 'setTimeout(perform_upload, 1000); ';
628 return $html;
632 * Gets HTML to display import dialogs
634 * @param String $import_type Import type: server|database|table
635 * @param String $db Selected DB
636 * @param String $table Selected Table
637 * @param int $max_upload_size Max upload size
639 * @return string $html
641 function PMA_getImportDisplay($import_type, $db, $table, $max_upload_size)
643 global $SESSION_KEY;
644 include_once './libraries/file_listing.lib.php';
645 include_once './libraries/plugin_interface.lib.php';
647 include_once './libraries/display_import_ajax.lib.php';
648 list(
649 $SESSION_KEY,
650 $upload_id,
651 ) = PMA_uploadProgressSetup();
653 /* Scan for plugins */
654 /* @var $import_list ImportPlugin[] */
655 $import_list = PMA_getPlugins(
656 "import",
657 'libraries/plugins/import/',
658 $import_type
661 /* Fail if we didn't find any plugin */
662 if (empty($import_list)) {
663 Message::error(
665 'Could not load import plugins, please check your installation!'
667 )->display();
668 exit;
671 if (PMA_isValid($_REQUEST['offset'], 'numeric')) {
672 $offset = intval($_REQUEST['offset']);
674 if (isset($_REQUEST['timeout_passed'])) {
675 $timeout_passed = $_REQUEST['timeout_passed'];
678 $local_import_file = '';
679 if (isset($_REQUEST['local_import_file'])) {
680 $local_import_file = $_REQUEST['local_import_file'];
683 $timeout_passed_str = isset($timeout_passed)? $timeout_passed : null;
684 $offset_str = isset($offset)? $offset : null;
685 return PMA_getHtmlForImport(
686 $upload_id,
687 $import_type,
688 $db,
689 $table,
690 $max_upload_size,
691 $import_list,
692 $timeout_passed_str,
693 $offset_str,
694 $local_import_file