From 2638eb920796bf85c15c9f0752855c6271180507 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Sat, 31 Mar 2018 19:28:25 -0300 Subject: [PATCH 1/2] Refactor PhpMyAdmin\Export static methods MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replaces static methods with instance methods. Signed-off-by: MaurĂ­cio Meneghini Fauth --- db_export.php | 10 ++- export.php | 46 +++++------ libraries/classes/Display/Results.php | 2 +- libraries/classes/Export.php | 76 +++++++++---------- .../classes/Plugins/Export/ExportCodegen.php | 5 +- .../classes/Plugins/Export/ExportCsv.php | 5 +- .../classes/Plugins/Export/ExportHtmlword.php | 18 ++--- .../classes/Plugins/Export/ExportJson.php | 17 +++-- .../classes/Plugins/Export/ExportLatex.php | 26 +++---- .../Plugins/Export/ExportMediawiki.php | 5 +- .../classes/Plugins/Export/ExportOds.php | 3 +- .../classes/Plugins/Export/ExportOdt.php | 3 +- .../classes/Plugins/Export/ExportPdf.php | 5 +- .../classes/Plugins/Export/ExportPhparray.php | 7 +- .../classes/Plugins/Export/ExportSql.php | 53 +++++++------ .../classes/Plugins/Export/ExportTexytext.php | 10 +-- .../classes/Plugins/Export/ExportXml.php | 13 ++-- .../classes/Plugins/Export/ExportYaml.php | 7 +- libraries/classes/Plugins/ExportPlugin.php | 7 ++ schema_export.php | 3 +- test/classes/ExportTest.php | 21 ++++- 21 files changed, 189 insertions(+), 153 deletions(-) diff --git a/db_export.php b/db_export.php index b04a8b0193..05df8d9767 100644 --- a/db_export.php +++ b/db_export.php @@ -24,6 +24,8 @@ $header = $response->getHeader(); $scripts = $header->getScripts(); $scripts->addFile('export.js'); +$export = new Export(); + // $sub_part is used in Util::getDbInfo() to see if we are coming from // db_export.php, in which case we don't obey $cfg['MaxTableList'] $sub_part = '_export'; @@ -95,25 +97,25 @@ foreach (array('table_select', 'table_structure', 'table_data') as $one_key) { foreach ($tables as $each_table) { if (isset($_GET['table_select']) && is_array($_GET['table_select'])) { - $is_checked = Export::getCheckedClause( + $is_checked = $export->getCheckedClause( $each_table['Name'], $_GET['table_select'] ); } elseif (isset($table_select)) { - $is_checked = Export::getCheckedClause( + $is_checked = $export->getCheckedClause( $each_table['Name'], $table_select ); } else { $is_checked = ' checked="checked"'; } if (isset($_GET['table_structure']) && is_array($_GET['table_structure'])) { - $structure_checked = Export::getCheckedClause( + $structure_checked = $export->getCheckedClause( $each_table['Name'], $_GET['table_structure'] ); } else { $structure_checked = $is_checked; } if (isset($_GET['table_data']) && is_array($_GET['table_data'])) { - $data_checked = Export::getCheckedClause( + $data_checked = $export->getCheckedClause( $each_table['Name'], $_GET['table_data'] ); } else { diff --git a/export.php b/export.php index 376bd020c2..e1255ae631 100644 --- a/export.php +++ b/export.php @@ -35,6 +35,8 @@ $header = $response->getHeader(); $scripts = $header->getScripts(); $scripts->addFile('export_output.js'); +$export = new Export(); + //check if it's the GET request to check export time out if (isset($_GET['check_time_out'])) { if (isset($_SESSION['pma_export_error'])) { @@ -298,7 +300,7 @@ if ((!empty($parser->statements[0])) $aliases = \PhpMyAdmin\SqlParser\Utils\Misc::getAliases($parser->statements[0], $db); } if (!empty($_REQUEST['aliases'])) { - $aliases = Export::mergeAliases($aliases, $_REQUEST['aliases']); + $aliases = $export->mergeAliases($aliases, $_REQUEST['aliases']); $_SESSION['tmpval']['aliases'] = $_REQUEST['aliases']; } @@ -309,7 +311,7 @@ Util::setTimeLimit(); if (! empty($cfg['MemoryLimit'])) { ini_set('memory_limit', $cfg['MemoryLimit']); } -register_shutdown_function('PhpMyAdmin\Export::shutdown'); +register_shutdown_function([$export, 'shutdown']); // Start with empty buffer $dump_buffer = ''; $dump_buffer_len = 0; @@ -339,7 +341,7 @@ $output_charset_conversion = $asfile $GLOBALS['onfly_compression'] = $GLOBALS['cfg']['CompressOnFly'] && $compression == 'gzip'; if ($GLOBALS['onfly_compression']) { - $GLOBALS['memory_limit'] = Export::getMemoryLimit(); + $GLOBALS['memory_limit'] = $export->getMemoryLimit(); } // Generate filename and mime type if needed @@ -347,7 +349,7 @@ if ($asfile) { if (empty($remember_template)) { $remember_template = ''; } - list($filename, $mime_type) = Export::getFilenameAndMimetype( + list($filename, $mime_type) = $export->getFilenameAndMimetype( $export_type, $remember_template, $export_plugin, $compression, $filename_template ); @@ -357,13 +359,13 @@ if ($asfile) { // Open file on server if needed if ($save_on_server) { - list($save_filename, $message, $file_handle) = Export::openFile( + list($save_filename, $message, $file_handle) = $export->openFile( $filename, $quick_export ); // problem opening export file on server? if (! empty($message)) { - Export::showPage($db, $table, $export_type); + $export->showPage($db, $table, $export_type); } } else { /** @@ -391,7 +393,7 @@ if ($save_on_server) { exit(); } } - list($html, $back_button, $refreshButton) = Export::getHtmlForDisplayedExportHeader( + list($html, $back_button, $refreshButton) = $export->getHtmlForDisplayedExportHeader( $export_type, $db, $table ); echo $html; @@ -434,7 +436,7 @@ do { if (! isset($db_select)) { $db_select = ''; } - Export::exportServer( + $export->exportServer( $db_select, $whatStrucOrData, $export_plugin, $crlf, $err_url, $export_type, $do_relation, $do_comments, $do_mime, $do_dates, $aliases, $separate_files @@ -451,19 +453,19 @@ do { $table_data = $tables; } if (isset($lock_tables)) { - Export::lockTables($db, $tables, "READ"); + $export->lockTables($db, $tables, "READ"); try { - Export::exportDatabase( + $export->exportDatabase( $db, $tables, $whatStrucOrData, $table_structure, $table_data, $export_plugin, $crlf, $err_url, $export_type, $do_relation, $do_comments, $do_mime, $do_dates, $aliases, $separate_files ); } finally { - Export::unlockTables(); + $export->unlockTables(); } } else { - Export::exportDatabase( + $export->exportDatabase( $db, $tables, $whatStrucOrData, $table_structure, $table_data, $export_plugin, $crlf, $err_url, $export_type, $do_relation, $do_comments, $do_mime, $do_dates, $aliases, $separate_files @@ -483,18 +485,18 @@ do { } if (isset($lock_tables)) { try { - Export::lockTables($db, array($table), "READ"); - Export::exportTable( + $export->lockTables($db, array($table), "READ"); + $export->exportTable( $db, $table, $whatStrucOrData, $export_plugin, $crlf, $err_url, $export_type, $do_relation, $do_comments, $do_mime, $do_dates, $allrows, $limit_to, $limit_from, $sql_query, $aliases ); } finally { - Export::unlockTables(); + $export->unlockTables(); } } else { - Export::exportTable( + $export->exportTable( $db, $table, $whatStrucOrData, $export_plugin, $crlf, $err_url, $export_type, $do_relation, $do_comments, $do_mime, $do_dates, $allrows, $limit_to, $limit_from, $sql_query, $aliases @@ -509,14 +511,14 @@ do { // End of fake loop if ($save_on_server && ! empty($message)) { - Export::showPage($db, $table, $export_type); + $export->showPage($db, $table, $export_type); } /** * Send the dump as a file... */ if (empty($asfile)) { - echo Export::getHtmlForDisplayedExportFooter($back_button, $refreshButton); + echo $export->getHtmlForDisplayedExportFooter($back_button, $refreshButton); return; } // end if @@ -532,21 +534,21 @@ if ($output_charset_conversion) { // Compression needed? if ($compression) { if (! empty($separate_files)) { - $dump_buffer = Export::compress( + $dump_buffer = $export->compress( $dump_buffer_objects, $compression, $filename ); } else { - $dump_buffer = Export::compress($dump_buffer, $compression, $filename); + $dump_buffer = $export->compress($dump_buffer, $compression, $filename); } } /* If we saved on server, we have to close file now */ if ($save_on_server) { - $message = Export::closeFile( + $message = $export->closeFile( $file_handle, $dump_buffer, $save_filename ); - Export::showPage($db, $table, $export_type); + $export->showPage($db, $table, $export_type); } else { echo $dump_buffer; } diff --git a/libraries/classes/Display/Results.php b/libraries/classes/Display/Results.php index 7b772145bc..20619259ed 100644 --- a/libraries/classes/Display/Results.php +++ b/libraries/classes/Display/Results.php @@ -4938,7 +4938,7 @@ class Results // Export link // (the url_query has extra parameters that won't be used to export) - // (the single_table parameter is used in Export::getDisplay() + // (the single_table parameter is used in \PhpMyAdmin\Export->getDisplay() // to hide the SQL and the structure export dialogs) // If the parser found a PROCEDURE clause // (most probably PROCEDURE ANALYSE()) it makes no sense to diff --git a/libraries/classes/Export.php b/libraries/classes/Export.php index 1f48b5f0c9..71cbfe50e2 100644 --- a/libraries/classes/Export.php +++ b/libraries/classes/Export.php @@ -30,7 +30,7 @@ class Export * * @return void */ - public static function shutdown() + public function shutdown() { $error = error_get_last(); if ($error != null && mb_strpos($error['message'], "execution time")) { @@ -44,7 +44,7 @@ class Export * * @return bool */ - public static function isGzHandlerEnabled() + public function isGzHandlerEnabled() { return in_array('ob_gzhandler', ob_list_handlers()); } @@ -55,7 +55,7 @@ class Export * * @return bool Whether gzencode is needed */ - public static function gzencodeNeeded() + public function gzencodeNeeded() { /* * We should gzencode only if the function exists @@ -69,7 +69,7 @@ class Export if (function_exists('gzencode') && ((! ini_get('zlib.output_compression') - && ! self::isGzHandlerEnabled()) + && ! $this->isGzHandlerEnabled()) || $GLOBALS['save_on_server'] || $chromeAndGreaterThan43) ) { @@ -87,7 +87,7 @@ class Export * * @return bool Whether output succeeded */ - public static function outputHandler($line) + public function outputHandler($line) { global $time_start, $dump_buffer, $dump_buffer_len, $save_filename; @@ -117,7 +117,7 @@ class Export ); } if ($GLOBALS['compression'] == 'gzip' - && self::gzencodeNeeded() + && $this->gzencodeNeeded() ) { // as a gzipped file // without the optional parameter level because it bugs @@ -184,7 +184,7 @@ class Export } } return true; - } // end of the 'self::outputHandler()' function + } /** * Returns HTML containing the footer for a displayed export @@ -194,7 +194,7 @@ class Export * * @return string $html the HTML output */ - public static function getHtmlForDisplayedExportFooter($back_button, $refreshButton) + public function getHtmlForDisplayedExportFooter($back_button, $refreshButton) { /** * Close the html tags and add the footers for on-screen export @@ -222,7 +222,7 @@ class Export * * @return int $memory_limit the memory limit */ - public static function getMemoryLimit() + public function getMemoryLimit() { $memory_limit = trim(ini_get('memory_limit')); $memory_limit_num = (int)substr($memory_limit, 0, -1); @@ -263,7 +263,7 @@ class Export * * @return string[] the filename template and mime type */ - public static function getFilenameAndMimetype( + public function getFilenameAndMimetype( $export_type, $remember_template, $export_plugin, $compression, $filename_template ) { @@ -332,7 +332,7 @@ class Export * * @return array the full save filename, possible message and the file handle */ - public static function openFile($filename, $quick_export) + public function openFile($filename, $quick_export) { $file_handle = null; $message = ''; @@ -381,7 +381,7 @@ class Export * * @return Message $message a message object (or empty string) */ - public static function closeFile($file_handle, $dump_buffer, $save_filename) + public function closeFile($file_handle, $dump_buffer, $save_filename) { $write_result = @fwrite($file_handle, $dump_buffer); fclose($file_handle); @@ -414,13 +414,13 @@ class Export * * @return object $message a message object (or empty string) */ - public static function compress($dump_buffer, $compression, $filename) + public function compress($dump_buffer, $compression, $filename) { if ($compression == 'zip' && function_exists('gzcompress')) { $zipExtension = new ZipExtension(); $filename = substr($filename, 0, -4); // remove extension (.zip) $dump_buffer = $zipExtension->createFile($dump_buffer, $filename); - } elseif ($compression == 'gzip' && self::gzencodeNeeded()) { + } elseif ($compression == 'gzip' && $this->gzencodeNeeded()) { // without the optional parameter level because it bugs $dump_buffer = gzencode($dump_buffer); } @@ -436,7 +436,7 @@ class Export * * @return void */ - public static function saveObjectInBuffer($object_name, $append = false) + public function saveObjectInBuffer($object_name, $append = false) { global $dump_buffer_objects, $dump_buffer, $dump_buffer_len; @@ -464,7 +464,7 @@ class Export * * @return string[] the generated HTML and back button */ - public static function getHtmlForDisplayedExportHeader($export_type, $db, $table) + public function getHtmlForDisplayedExportHeader($export_type, $db, $table) { $html = '
'; @@ -555,7 +555,7 @@ class Export * * @return void */ - public static function exportServer( + public function exportServer( $db_select, $whatStrucOrData, $export_plugin, $crlf, $err_url, $export_type, $do_relation, $do_comments, $do_mime, $do_dates, array $aliases, $separate_files @@ -570,14 +570,14 @@ class Export && mb_strpos(' ' . $tmp_select, '|' . $current_db . '|') ) { $tables = $GLOBALS['dbi']->getTables($current_db); - self::exportDatabase( + $this->exportDatabase( $current_db, $tables, $whatStrucOrData, $tables, $tables, $export_plugin, $crlf, $err_url, $export_type, $do_relation, $do_comments, $do_mime, $do_dates, $aliases, $separate_files == 'database' ? $separate_files : '' ); if ($separate_files == 'server') { - self::saveObjectInBuffer($current_db); + $this->saveObjectInBuffer($current_db); } } } // end foreach database @@ -604,7 +604,7 @@ class Export * * @return void */ - public static function exportDatabase( + public function exportDatabase( $db, array $tables, $whatStrucOrData, array $table_structure, array $table_data, $export_plugin, $crlf, $err_url, $export_type, $do_relation, $do_comments, $do_mime, $do_dates, array $aliases, $separate_files @@ -619,7 +619,7 @@ class Export return; } if ($separate_files == 'database') { - self::saveObjectInBuffer('database', true); + $this->saveObjectInBuffer('database', true); } if (($GLOBALS['sql_structure_or_data'] == 'structure' @@ -629,7 +629,7 @@ class Export $export_plugin->exportRoutines($db, $aliases); if ($separate_files == 'database') { - self::saveObjectInBuffer('routines'); + $this->saveObjectInBuffer('routines'); } } @@ -712,7 +712,7 @@ class Export // this buffer was filled, we save it and go to the next one if ($separate_files == 'database') { - self::saveObjectInBuffer('table_' . $table); + $this->saveObjectInBuffer('table_' . $table); } // now export the triggers (needs to be done after the data because @@ -730,7 +730,7 @@ class Export } if ($separate_files == 'database') { - self::saveObjectInBuffer('table_' . $table, true); + $this->saveObjectInBuffer('table_' . $table, true); } } @@ -752,7 +752,7 @@ class Export } if ($separate_files == 'database') { - self::saveObjectInBuffer('view_' . $view); + $this->saveObjectInBuffer('view_' . $view); } } } @@ -767,16 +767,16 @@ class Export if (isset($GLOBALS['sql_metadata'])) { // Types of metadata to export. // In the future these can be allowed to be selected by the user - $metadataTypes = self::getMetadataTypes(); + $metadataTypes = $this->getMetadataTypes(); $export_plugin->exportMetadata($db, $tables, $metadataTypes); if ($separate_files == 'database') { - self::saveObjectInBuffer('metadata'); + $this->saveObjectInBuffer('metadata'); } } if ($separate_files == 'database') { - self::saveObjectInBuffer('extra'); + $this->saveObjectInBuffer('extra'); } if (($GLOBALS['sql_structure_or_data'] == 'structure' @@ -786,7 +786,7 @@ class Export $export_plugin->exportEvents($db); if ($separate_files == 'database') { - self::saveObjectInBuffer('events'); + $this->saveObjectInBuffer('events'); } } } @@ -813,7 +813,7 @@ class Export * * @return void */ - public static function exportTable( + public function exportTable( $db, $table, $whatStrucOrData, $export_plugin, $crlf, $err_url, $export_type, $do_relation, $do_comments, $do_mime, $do_dates, $allrows, $limit_to, $limit_from, $sql_query, array $aliases @@ -915,7 +915,7 @@ class Export if (isset($GLOBALS['sql_metadata'])) { // Types of metadata to export. // In the future these can be allowed to be selected by the user - $metadataTypes = self::getMetadataTypes(); + $metadataTypes = $this->getMetadataTypes(); $export_plugin->exportMetadata($db, $table, $metadataTypes); } } @@ -929,7 +929,7 @@ class Export * * @return void */ - public static function showPage($db, $table, $export_type) + public function showPage($db, $table, $export_type) { global $cfg; if ($export_type == 'server') { @@ -955,7 +955,7 @@ class Export * * @return array resultant merged aliases info */ - public static function mergeAliases(array $aliases1, array $aliases2) + public function mergeAliases(array $aliases1, array $aliases2) { // First do a recursive array merge // on aliases arrays. @@ -1008,7 +1008,7 @@ class Export * * @return mixed result of the query */ - public static function lockTables($db, array $tables, $lockType = "WRITE") + public function lockTables($db, array $tables, $lockType = "WRITE") { $locks = array(); foreach ($tables as $table) { @@ -1025,7 +1025,7 @@ class Export * * @return mixed result of the query */ - public static function unlockTables() + public function unlockTables() { return $GLOBALS['dbi']->tryQuery("UNLOCK TABLES"); } @@ -1035,7 +1035,7 @@ class Export * * @return string[] metadata types. */ - public static function getMetadataTypes() + public function getMetadataTypes() { return array( 'column_info', @@ -1059,7 +1059,7 @@ class Export * * @return string the checked clause */ - public static function getCheckedClause($key, array $array) + public function getCheckedClause($key, array $array) { if (in_array($key, $array)) { return ' checked="checked"'; @@ -1076,7 +1076,7 @@ class Export * * @return void */ - public static function processExportSchema($export_type) + public function processExportSchema($export_type) { /** * default is PDF, otherwise validate it's only letters a-z diff --git a/libraries/classes/Plugins/Export/ExportCodegen.php b/libraries/classes/Plugins/Export/ExportCodegen.php index b2a2763fd4..e00f22462b 100644 --- a/libraries/classes/Plugins/Export/ExportCodegen.php +++ b/libraries/classes/Plugins/Export/ExportCodegen.php @@ -44,6 +44,7 @@ class ExportCodegen extends ExportPlugin */ public function __construct() { + parent::__construct(); // initialize the specific export CodeGen variables $this->initSpecificVariables(); $this->setProperties(); @@ -196,12 +197,12 @@ class ExportCodegen extends ExportPlugin if (isset($CG_FORMATS[$format])) { $method = $CG_HANDLERS[$format]; - return Export::outputHandler( + return $this->export->outputHandler( $this->$method($db, $table, $crlf, $aliases) ); } - return Export::outputHandler(sprintf("%s is not supported.", $format)); + return $this->export->outputHandler(sprintf("%s is not supported.", $format)); } /** diff --git a/libraries/classes/Plugins/Export/ExportCsv.php b/libraries/classes/Plugins/Export/ExportCsv.php index 13f2c87bad..40effaf1ce 100644 --- a/libraries/classes/Plugins/Export/ExportCsv.php +++ b/libraries/classes/Plugins/Export/ExportCsv.php @@ -32,6 +32,7 @@ class ExportCsv extends ExportPlugin */ public function __construct() { + parent::__construct(); $this->setProperties(); } @@ -256,7 +257,7 @@ class ExportCsv extends ExportPlugin $schema_insert .= $csv_separator; } // end for $schema_insert = trim(mb_substr($schema_insert, 0, -1)); - if (!Export::outputHandler($schema_insert . $csv_terminated)) { + if (!$this->export->outputHandler($schema_insert . $csv_terminated)) { return false; } } // end if @@ -321,7 +322,7 @@ class ExportCsv extends ExportPlugin } } // end for - if (!Export::outputHandler($schema_insert . $csv_terminated)) { + if (!$this->export->outputHandler($schema_insert . $csv_terminated)) { return false; } } // end while diff --git a/libraries/classes/Plugins/Export/ExportHtmlword.php b/libraries/classes/Plugins/Export/ExportHtmlword.php index 5aa72935c9..fda8fa1e6a 100644 --- a/libraries/classes/Plugins/Export/ExportHtmlword.php +++ b/libraries/classes/Plugins/Export/ExportHtmlword.php @@ -109,7 +109,7 @@ class ExportHtmlword extends ExportPlugin { global $charset; - return Export::outputHandler( + return $this->export->outputHandler( ' @@ -132,7 +132,7 @@ class ExportHtmlword extends ExportPlugin */ public function exportFooter() { - return Export::outputHandler(''); + return $this->export->outputHandler(''); } /** @@ -149,7 +149,7 @@ class ExportHtmlword extends ExportPlugin $db_alias = $db; } - return Export::outputHandler( + return $this->export->outputHandler( '

' . __('Database') . ' ' . htmlspecialchars($db_alias) . '

' ); } @@ -206,7 +206,7 @@ class ExportHtmlword extends ExportPlugin $table_alias = $table; $this->initAlias($aliases, $db_alias, $table_alias); - if (!Export::outputHandler( + if (!$this->export->outputHandler( '

' . __('Dumping data for table') . ' ' . htmlspecialchars($table_alias) . '

' @@ -214,7 +214,7 @@ class ExportHtmlword extends ExportPlugin ) { return false; } - if (!Export::outputHandler( + if (!$this->export->outputHandler( '' ) ) { @@ -243,7 +243,7 @@ class ExportHtmlword extends ExportPlugin . ''; } // end for $schema_insert .= ''; - if (!Export::outputHandler($schema_insert)) { + if (!$this->export->outputHandler($schema_insert)) { return false; } } // end if @@ -264,13 +264,13 @@ class ExportHtmlword extends ExportPlugin . ''; } // end for $schema_insert .= ''; - if (!Export::outputHandler($schema_insert)) { + if (!$this->export->outputHandler($schema_insert)) { return false; } } // end while $GLOBALS['dbi']->freeResult($result); - return Export::outputHandler('
'); + return $this->export->outputHandler(''); } /** @@ -604,7 +604,7 @@ class ExportHtmlword extends ExportPlugin $dump .= $this->getTableDefStandIn($db, $table, $crlf, $aliases); } // end switch - return Export::outputHandler($dump); + return $this->export->outputHandler($dump); } /** diff --git a/libraries/classes/Plugins/Export/ExportJson.php b/libraries/classes/Plugins/Export/ExportJson.php index 326a371b84..70ebcc8bd6 100644 --- a/libraries/classes/Plugins/Export/ExportJson.php +++ b/libraries/classes/Plugins/Export/ExportJson.php @@ -32,6 +32,7 @@ class ExportJson extends ExportPlugin */ public function __construct() { + parent::__construct(); $this->setProperties(); } @@ -119,7 +120,7 @@ class ExportJson extends ExportPlugin 'comment' => 'Export to JSON plugin for PHPMyAdmin', ); - return Export::outputHandler( + return $this->export->outputHandler( '[' . $crlf . $this->encode($meta) . ',' . $crlf ); } @@ -133,7 +134,7 @@ class ExportJson extends ExportPlugin { global $crlf; - return Export::outputHandler(']' . $crlf); + return $this->export->outputHandler(']' . $crlf); } /** @@ -157,7 +158,7 @@ class ExportJson extends ExportPlugin 'name' => $db_alias ); - return Export::outputHandler( + return $this->export->outputHandler( $this->encode($meta) . ',' . $crlf ); } @@ -213,7 +214,7 @@ class ExportJson extends ExportPlugin $this->initAlias($aliases, $db_alias, $table_alias); if (! $this->first) { - if (!Export::outputHandler(',')) { + if (!$this->export->outputHandler(',')) { return false; } } else { @@ -230,7 +231,7 @@ class ExportJson extends ExportPlugin ); list($header, $footer) = explode('"@@DATA@@"', $buffer); - if (!Export::outputHandler($header . $crlf . '[' . $crlf)) { + if (!$this->export->outputHandler($header . $crlf . '[' . $crlf)) { return false; } @@ -257,7 +258,7 @@ class ExportJson extends ExportPlugin // Output table name as comment if this is the first record of the table if ($record_cnt > 1) { - if (!Export::outputHandler(',' . $crlf)) { + if (!$this->export->outputHandler(',' . $crlf)) { return false; } } @@ -268,12 +269,12 @@ class ExportJson extends ExportPlugin $data[$columns[$i]] = $record[$i]; } - if (!Export::outputHandler($this->encode($data))) { + if (!$this->export->outputHandler($this->encode($data))) { return false; } } - if (!Export::outputHandler($crlf . ']' . $crlf . $footer . $crlf)) { + if (!$this->export->outputHandler($crlf . ']' . $crlf . $footer . $crlf)) { return false; } diff --git a/libraries/classes/Plugins/Export/ExportLatex.php b/libraries/classes/Plugins/Export/ExportLatex.php index f9cebe18ab..c915dac97e 100644 --- a/libraries/classes/Plugins/Export/ExportLatex.php +++ b/libraries/classes/Plugins/Export/ExportLatex.php @@ -223,7 +223,7 @@ class ExportLatex extends ExportPlugin . '% ' . __('Server version:') . ' ' . $GLOBALS['dbi']->getVersionString() . $crlf . '% ' . __('PHP Version:') . ' ' . phpversion() . $crlf; - return Export::outputHandler($head); + return $this->export->outputHandler($head); } /** @@ -254,7 +254,7 @@ class ExportLatex extends ExportPlugin . '% ' . __('Database:') . ' ' . '\'' . $db_alias . '\'' . $crlf . '% ' . $crlf; - return Export::outputHandler($head); + return $this->export->outputHandler($head); } /** @@ -351,7 +351,7 @@ class ExportLatex extends ExportPlugin ) . '} \\\\'; } - if (!Export::outputHandler($buffer)) { + if (!$this->export->outputHandler($buffer)) { return false; } @@ -364,11 +364,11 @@ class ExportLatex extends ExportPlugin } $buffer = mb_substr($buffer, 0, -2) . '\\\\ \\hline \hline '; - if (!Export::outputHandler($buffer . ' \\endfirsthead ' . $crlf)) { + if (!$this->export->outputHandler($buffer . ' \\endfirsthead ' . $crlf)) { return false; } if (isset($GLOBALS['latex_caption'])) { - if (!Export::outputHandler( + if (!$this->export->outputHandler( '\\caption{' . Util::expandUserString( $GLOBALS['latex_data_continued_caption'], @@ -384,11 +384,11 @@ class ExportLatex extends ExportPlugin return false; } } - if (!Export::outputHandler($buffer . '\\endhead \\endfoot' . $crlf)) { + if (!$this->export->outputHandler($buffer . '\\endhead \\endfoot' . $crlf)) { return false; } } else { - if (!Export::outputHandler('\\\\ \hline')) { + if (!$this->export->outputHandler('\\\\ \hline')) { return false; } } @@ -417,13 +417,13 @@ class ExportLatex extends ExportPlugin } } $buffer .= ' \\\\ \\hline ' . $crlf; - if (!Export::outputHandler($buffer)) { + if (!$this->export->outputHandler($buffer)) { return false; } } $buffer = ' \\end{longtable}' . $crlf; - if (!Export::outputHandler($buffer)) { + if (!$this->export->outputHandler($buffer)) { return false; } @@ -506,7 +506,7 @@ class ExportLatex extends ExportPlugin */ $buffer = $crlf . '%' . $crlf . '% ' . __('Structure:') . ' ' . $table_alias . $crlf . '%' . $crlf . ' \\begin{longtable}{'; - if (!Export::outputHandler($buffer)) { + if (!$this->export->outputHandler($buffer)) { return false; } @@ -575,7 +575,7 @@ class ExportLatex extends ExportPlugin } $buffer .= $header . ' \\\\ \\hline \\hline \\endhead \\endfoot ' . $crlf; - if (!Export::outputHandler($buffer)) { + if (!$this->export->outputHandler($buffer)) { return false; } @@ -648,14 +648,14 @@ class ExportLatex extends ExportPlugin $buffer = str_replace("\000", ' & ', $local_buffer); $buffer .= ' \\\\ \\hline ' . $crlf; - if (!Export::outputHandler($buffer)) { + if (!$this->export->outputHandler($buffer)) { return false; } } // end while $buffer = ' \\end{longtable}' . $crlf; - return Export::outputHandler($buffer); + return $this->export->outputHandler($buffer); } // end of the 'exportStructure' method /** diff --git a/libraries/classes/Plugins/Export/ExportMediawiki.php b/libraries/classes/Plugins/Export/ExportMediawiki.php index 93210d5168..efe09f4f33 100644 --- a/libraries/classes/Plugins/Export/ExportMediawiki.php +++ b/libraries/classes/Plugins/Export/ExportMediawiki.php @@ -32,6 +32,7 @@ class ExportMediawiki extends ExportPlugin */ public function __construct() { + parent::__construct(); $this->setProperties(); } @@ -262,7 +263,7 @@ class ExportMediawiki extends ExportPlugin break; } // end switch - return Export::outputHandler($output); + return $this->export->outputHandler($output); } /** @@ -347,7 +348,7 @@ class ExportMediawiki extends ExportPlugin // End table construction $output .= "|}" . str_repeat($this->_exportCRLF(), 2); - return Export::outputHandler($output); + return $this->export->outputHandler($output); } /** diff --git a/libraries/classes/Plugins/Export/ExportOds.php b/libraries/classes/Plugins/Export/ExportOds.php index ca822b7890..5a444140a5 100644 --- a/libraries/classes/Plugins/Export/ExportOds.php +++ b/libraries/classes/Plugins/Export/ExportOds.php @@ -32,6 +32,7 @@ class ExportOds extends ExportPlugin */ public function __construct() { + parent::__construct(); $GLOBALS['ods_buffer'] = ''; $this->setProperties(); } @@ -149,7 +150,7 @@ class ExportOds extends ExportPlugin . '' . ''; - return Export::outputHandler( + return $this->export->outputHandler( OpenDocument::create( 'application/vnd.oasis.opendocument.spreadsheet', $GLOBALS['ods_buffer'] diff --git a/libraries/classes/Plugins/Export/ExportOdt.php b/libraries/classes/Plugins/Export/ExportOdt.php index 9f840d4dd2..a7a0566219 100644 --- a/libraries/classes/Plugins/Export/ExportOdt.php +++ b/libraries/classes/Plugins/Export/ExportOdt.php @@ -18,7 +18,6 @@ use PhpMyAdmin\Properties\Options\Groups\OptionsPropertyRootGroup; use PhpMyAdmin\Properties\Options\Items\BoolPropertyItem; use PhpMyAdmin\Properties\Options\Items\RadioPropertyItem; use PhpMyAdmin\Properties\Options\Items\TextPropertyItem; -use PhpMyAdmin\Relation; use PhpMyAdmin\Transformations; use PhpMyAdmin\Util; @@ -168,7 +167,7 @@ class ExportOdt extends ExportPlugin $GLOBALS['odt_buffer'] .= '' . '' . ''; - if (!Export::outputHandler( + if (!$this->export->outputHandler( OpenDocument::create( 'application/vnd.oasis.opendocument.text', $GLOBALS['odt_buffer'] diff --git a/libraries/classes/Plugins/Export/ExportPdf.php b/libraries/classes/Plugins/Export/ExportPdf.php index ff08fbc9ab..8441a907f8 100644 --- a/libraries/classes/Plugins/Export/ExportPdf.php +++ b/libraries/classes/Plugins/Export/ExportPdf.php @@ -39,6 +39,7 @@ class ExportPdf extends ExportPlugin * @var Pdf */ private $_pdf; + /** * PDF Report Title * @@ -51,6 +52,8 @@ class ExportPdf extends ExportPlugin */ public function __construct() { + parent::__construct(); + // initialize the specific export PDF variables $this->initSpecificVariables(); @@ -151,7 +154,7 @@ class ExportPdf extends ExportPlugin $pdf = $this->_getPdf(); // instead of $pdf->Output(): - return Export::outputHandler($pdf->getPDFData()); + return $this->export->outputHandler($pdf->getPDFData()); } /** diff --git a/libraries/classes/Plugins/Export/ExportPhparray.php b/libraries/classes/Plugins/Export/ExportPhparray.php index 051ee3bfad..3199ec588b 100644 --- a/libraries/classes/Plugins/Export/ExportPhparray.php +++ b/libraries/classes/Plugins/Export/ExportPhparray.php @@ -31,6 +31,7 @@ class ExportPhparray extends ExportPlugin */ public function __construct() { + parent::__construct(); $this->setProperties(); } @@ -87,7 +88,7 @@ class ExportPhparray extends ExportPlugin */ public function exportHeader() { - Export::outputHandler( + $this->export->outputHandler( 'export->outputHandler( '/**' . $GLOBALS['crlf'] . ' * Database ' . $this->commentString(Util::backquote($db_alias)) . $GLOBALS['crlf'] . ' */' . $GLOBALS['crlf'] @@ -246,7 +247,7 @@ class ExportPhparray extends ExportPlugin } $buffer .= $crlf . ');' . $crlf; - if (!Export::outputHandler($buffer)) { + if (!$this->export->outputHandler($buffer)) { return false; } diff --git a/libraries/classes/Plugins/Export/ExportSql.php b/libraries/classes/Plugins/Export/ExportSql.php index d9a9f6d82b..92ced9d10d 100644 --- a/libraries/classes/Plugins/Export/ExportSql.php +++ b/libraries/classes/Plugins/Export/ExportSql.php @@ -22,7 +22,6 @@ use PhpMyAdmin\Properties\Options\Items\NumberPropertyItem; use PhpMyAdmin\Properties\Options\Items\RadioPropertyItem; use PhpMyAdmin\Properties\Options\Items\SelectPropertyItem; use PhpMyAdmin\Properties\Options\Items\TextPropertyItem; -use PhpMyAdmin\Relation; use PhpMyAdmin\SqlParser\Components\CreateDefinition; use PhpMyAdmin\SqlParser\Context; use PhpMyAdmin\SqlParser\Parser; @@ -581,7 +580,7 @@ class ExportSql extends ExportPlugin } if (!empty($text)) { - return Export::outputHandler($text); + return $this->export->outputHandler($text); } return false; @@ -667,7 +666,7 @@ class ExportSql extends ExportPlugin $GLOBALS['dbi']->query('SET time_zone = "' . $GLOBALS['old_tz'] . '"'); } - return Export::outputHandler($foot); + return $this->export->outputHandler($foot); } /** @@ -774,7 +773,7 @@ class ExportSql extends ExportPlugin $this->_sent_charset = true; } - return Export::outputHandler($head); + return $this->export->outputHandler($head); } /** @@ -799,7 +798,7 @@ class ExportSql extends ExportPlugin $compat = 'NONE'; } if (isset($GLOBALS['sql_drop_database'])) { - if (!Export::outputHandler( + if (!$this->export->outputHandler( 'DROP DATABASE IF EXISTS ' . Util::backquoteCompat( $db_alias, @@ -835,7 +834,7 @@ class ExportSql extends ExportPlugin $create_query .= ' DEFAULT CHARACTER SET ' . $collation; } $create_query .= ';' . $crlf; - if (!Export::outputHandler($create_query)) { + if (!$this->export->outputHandler($create_query)) { return false; } @@ -857,7 +856,7 @@ class ExportSql extends ExportPlugin if (isset($GLOBALS['sql_compatibility']) && $GLOBALS['sql_compatibility'] == 'NONE' ) { - $result = Export::outputHandler( + $result = $this->export->outputHandler( 'USE ' . Util::backquoteCompat( $db, @@ -867,7 +866,7 @@ class ExportSql extends ExportPlugin . ';' . $crlf ); } else { - $result = Export::outputHandler('USE ' . $db . ';' . $crlf); + $result = $this->export->outputHandler('USE ' . $db . ';' . $crlf); } return $result; @@ -902,7 +901,7 @@ class ExportSql extends ExportPlugin ) . $this->_exportComment(); - return Export::outputHandler($head); + return $this->export->outputHandler($head); } /** @@ -920,17 +919,17 @@ class ExportSql extends ExportPlugin //add indexes to the sql dump file if (isset($GLOBALS['sql_indexes'])) { - $result = Export::outputHandler($GLOBALS['sql_indexes']); + $result = $this->export->outputHandler($GLOBALS['sql_indexes']); unset($GLOBALS['sql_indexes']); } //add auto increments to the sql dump file if (isset($GLOBALS['sql_auto_increments'])) { - $result = Export::outputHandler($GLOBALS['sql_auto_increments']); + $result = $this->export->outputHandler($GLOBALS['sql_auto_increments']); unset($GLOBALS['sql_auto_increments']); } //add constraints to the sql dump file if (isset($GLOBALS['sql_constraints'])) { - $result = Export::outputHandler($GLOBALS['sql_constraints']); + $result = $this->export->outputHandler($GLOBALS['sql_constraints']); unset($GLOBALS['sql_constraints']); } @@ -979,7 +978,7 @@ class ExportSql extends ExportPlugin } if (!empty($text)) { - return Export::outputHandler($text); + return $this->export->outputHandler($text); } return false; @@ -1009,7 +1008,7 @@ class ExportSql extends ExportPlugin . $this->_exportComment() . $this->_exportComment(__('Metadata')) . $this->_exportComment(); - if (!Export::outputHandler($comment)) { + if (!$this->export->outputHandler($comment)) { return false; } @@ -1092,7 +1091,7 @@ class ExportSql extends ExportPlugin $comment .= $this->_exportComment(); - if (!Export::outputHandler($comment)) { + if (!$this->export->outputHandler($comment)) { return false; } @@ -1142,7 +1141,7 @@ class ExportSql extends ExportPlugin $lastPage = $GLOBALS['crlf'] . "SET @LAST_PAGE = LAST_INSERT_ID();" . $GLOBALS['crlf']; - if (!Export::outputHandler($lastPage)) { + if (!$this->export->outputHandler($lastPage)) { return false; } @@ -2132,7 +2131,7 @@ class ExportSql extends ExportPlugin // but not in the case of export unset($GLOBALS['sql_constraints_query']); - return Export::outputHandler($dump); + return $this->export->outputHandler($dump); } /** @@ -2190,7 +2189,7 @@ class ExportSql extends ExportPlugin . $this->_exportComment() . $this->_possibleCRLF(); - return Export::outputHandler($head); + return $this->export->outputHandler($head); } $result = $GLOBALS['dbi']->tryQuery( @@ -2206,7 +2205,7 @@ class ExportSql extends ExportPlugin if (! defined('TESTSUITE')) { trigger_error($message, E_USER_ERROR); } - return Export::outputHandler( + return $this->export->outputHandler( $this->_exportComment($message) ); } @@ -2296,8 +2295,8 @@ class ExportSql extends ExportPlugin ) . $this->_exportComment() . $crlf; - Export::outputHandler($truncatehead); - Export::outputHandler($truncate); + $this->export->outputHandler($truncatehead); + $this->export->outputHandler($truncate); } // scheme for inserting fields @@ -2348,7 +2347,7 @@ class ExportSql extends ExportPlugin ) . $this->_exportComment() . $crlf; - if (!Export::outputHandler($head)) { + if (!$this->export->outputHandler($head)) { return false; } } @@ -2357,7 +2356,7 @@ class ExportSql extends ExportPlugin && $GLOBALS['sql_compatibility'] == 'MSSQL' && $current_row == 0 ) { - if (!Export::outputHandler( + if (!$this->export->outputHandler( 'SET IDENTITY_INSERT ' . Util::backquoteCompat( $table_alias, @@ -2468,7 +2467,7 @@ class ExportSql extends ExportPlugin && $sql_max_size > 0 && $query_size + $insertLineSize > $sql_max_size ) { - if (!Export::outputHandler(';' . $crlf)) { + if (!$this->export->outputHandler(';' . $crlf)) { return false; } $query_size = 0; @@ -2485,7 +2484,7 @@ class ExportSql extends ExportPlugin } unset($values); - if (!Export::outputHandler( + if (!$this->export->outputHandler( ($current_row == 1 ? '' : $separator . $crlf) . $insert_line ) @@ -2495,7 +2494,7 @@ class ExportSql extends ExportPlugin } // end while if ($current_row > 0) { - if (!Export::outputHandler(';' . $crlf)) { + if (!$this->export->outputHandler(';' . $crlf)) { return false; } } @@ -2505,7 +2504,7 @@ class ExportSql extends ExportPlugin && $GLOBALS['sql_compatibility'] == 'MSSQL' && $current_row > 0 ) { - $outputSucceeded = Export::outputHandler( + $outputSucceeded = $this->export->outputHandler( $crlf . 'SET IDENTITY_INSERT ' . Util::backquoteCompat( $table_alias, diff --git a/libraries/classes/Plugins/Export/ExportTexytext.php b/libraries/classes/Plugins/Export/ExportTexytext.php index 3100f3aa87..ac39db79fa 100644 --- a/libraries/classes/Plugins/Export/ExportTexytext.php +++ b/libraries/classes/Plugins/Export/ExportTexytext.php @@ -133,7 +133,7 @@ class ExportTexytext extends ExportPlugin $db_alias = $db; } - return Export::outputHandler( + return $this->export->outputHandler( '===' . __('Database') . ' ' . $db_alias . "\n\n" ); } @@ -190,7 +190,7 @@ class ExportTexytext extends ExportPlugin $table_alias = $table; $this->initAlias($aliases, $db_alias, $table_alias); - if (!Export::outputHandler( + if (!$this->export->outputHandler( '== ' . __('Dumping data for table') . ' ' . $table_alias . "\n\n" ) ) { @@ -217,7 +217,7 @@ class ExportTexytext extends ExportPlugin . htmlspecialchars(stripslashes($col_as)); } // end for $text_output .= "\n|------\n"; - if (!Export::outputHandler($text_output)) { + if (!$this->export->outputHandler($text_output)) { return false; } } // end if @@ -241,7 +241,7 @@ class ExportTexytext extends ExportPlugin ); } // end for $text_output .= "\n"; - if (!Export::outputHandler($text_output)) { + if (!$this->export->outputHandler($text_output)) { return false; } } // end while @@ -561,7 +561,7 @@ class ExportTexytext extends ExportPlugin $dump .= $this->getTableDefStandIn($db, $table, $crlf, $aliases); } // end switch - return Export::outputHandler($dump); + return $this->export->outputHandler($dump); } /** diff --git a/libraries/classes/Plugins/Export/ExportXml.php b/libraries/classes/Plugins/Export/ExportXml.php index 80731fb965..821026a2ed 100644 --- a/libraries/classes/Plugins/Export/ExportXml.php +++ b/libraries/classes/Plugins/Export/ExportXml.php @@ -49,6 +49,7 @@ class ExportXml extends ExportPlugin */ public function __construct() { + parent::__construct(); $this->setProperties(); } @@ -368,7 +369,7 @@ class ExportXml extends ExportPlugin } } - return Export::outputHandler($head); + return $this->export->outputHandler($head); } /** @@ -380,7 +381,7 @@ class ExportXml extends ExportPlugin { $foot = ''; - return Export::outputHandler($foot); + return $this->export->outputHandler($foot); } /** @@ -407,7 +408,7 @@ class ExportXml extends ExportPlugin . ' -->' . $crlf . ' ' . $crlf; - return Export::outputHandler($head); + return $this->export->outputHandler($head); } return true; @@ -427,7 +428,7 @@ class ExportXml extends ExportPlugin if (isset($GLOBALS['xml_export_contents']) && $GLOBALS['xml_export_contents'] ) { - return Export::outputHandler(' ' . $crlf); + return $this->export->outputHandler(' ' . $crlf); } return true; @@ -493,7 +494,7 @@ class ExportXml extends ExportPlugin $buffer = ' ' . $crlf; - if (!Export::outputHandler($buffer)) { + if (!$this->export->outputHandler($buffer)) { return false; } @@ -519,7 +520,7 @@ class ExportXml extends ExportPlugin } $buffer .= ' ' . $crlf; - if (!Export::outputHandler($buffer)) { + if (!$this->export->outputHandler($buffer)) { return false; } } diff --git a/libraries/classes/Plugins/Export/ExportYaml.php b/libraries/classes/Plugins/Export/ExportYaml.php index 0e49fc65e1..ea3ab279b5 100644 --- a/libraries/classes/Plugins/Export/ExportYaml.php +++ b/libraries/classes/Plugins/Export/ExportYaml.php @@ -30,6 +30,7 @@ class ExportYaml extends ExportPlugin */ public function __construct() { + parent::__construct(); $this->setProperties(); } @@ -74,7 +75,7 @@ class ExportYaml extends ExportPlugin */ public function exportHeader() { - Export::outputHandler( + $this->export->outputHandler( '%YAML 1.1' . $GLOBALS['crlf'] . '---' . $GLOBALS['crlf'] ); @@ -88,7 +89,7 @@ class ExportYaml extends ExportPlugin */ public function exportFooter() { - Export::outputHandler('...' . $GLOBALS['crlf']); + $this->export->outputHandler('...' . $GLOBALS['crlf']); return true; } @@ -207,7 +208,7 @@ class ExportYaml extends ExportPlugin $buffer .= ' ' . $columns[$i] . ': "' . $record[$i] . '"' . $crlf; } - if (!Export::outputHandler($buffer)) { + if (!$this->export->outputHandler($buffer)) { return false; } } diff --git a/libraries/classes/Plugins/ExportPlugin.php b/libraries/classes/Plugins/ExportPlugin.php index 56710ad733..6185d9324c 100644 --- a/libraries/classes/Plugins/ExportPlugin.php +++ b/libraries/classes/Plugins/ExportPlugin.php @@ -8,6 +8,7 @@ namespace PhpMyAdmin\Plugins; +use PhpMyAdmin\Export; use PhpMyAdmin\Properties\Plugins\ExportPluginProperties; use PhpMyAdmin\Relation; @@ -34,12 +35,18 @@ abstract class ExportPlugin */ protected $relation; + /** + * @var Export $export + */ + protected $export; + /** * Constructor */ public function __construct() { $this->relation = new Relation(); + $this->export = new Export(); } /** diff --git a/schema_export.php b/schema_export.php index d25657e8c2..2a5d4c62d1 100644 --- a/schema_export.php +++ b/schema_export.php @@ -30,4 +30,5 @@ if (! isset($_REQUEST['export_type'])) { * Include the appropriate Schema Class depending on $export_type * default is PDF */ -Export::processExportSchema($_REQUEST['export_type']); +$export = new Export(); +$export->processExportSchema($_REQUEST['export_type']); diff --git a/test/classes/ExportTest.php b/test/classes/ExportTest.php index 7f0709c226..0f27626bc6 100644 --- a/test/classes/ExportTest.php +++ b/test/classes/ExportTest.php @@ -21,11 +21,26 @@ use PHPUnit\Framework\TestCase; class ExportTest extends TestCase { /** - * Test for Export::mergeAliases + * @var Export + */ + private $export; + + /** + * Sets up the fixture * * @return void */ - public function testPMAMergeAliases() + protected function setUp() + { + $this->export = new Export(); + } + + /** + * Test for mergeAliases + * + * @return void + */ + public function testMergeAliases() { $aliases1 = array( 'test_db' => array( @@ -91,7 +106,7 @@ class ExportTest extends TestCase ) ) ); - $actual = Export::mergeAliases($aliases1, $aliases2); + $actual = $this->export->mergeAliases($aliases1, $aliases2); $this->assertEquals($expected, $actual); } } From 1a82d6f0d20c2a0c94de2c08cc521165513498e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Sat, 31 Mar 2018 21:32:18 -0300 Subject: [PATCH 2/2] Refactor PhpMyAdmin\Import static methods MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replaces static methods with instance methods. Signed-off-by: MaurĂ­cio Meneghini Fauth --- db_import.php | 4 +- import.php | 16 +-- libraries/classes/Import.php | 110 +++++++++--------- .../Plugins/Import/AbstractImportCsv.php | 8 ++ .../classes/Plugins/Import/ImportCsv.php | 11 +- .../classes/Plugins/Import/ImportLdi.php | 5 +- .../Plugins/Import/ImportMediawiki.php | 9 +- .../classes/Plugins/Import/ImportOds.php | 15 +-- .../classes/Plugins/Import/ImportShp.php | 11 +- .../classes/Plugins/Import/ImportSql.php | 9 +- .../classes/Plugins/Import/ImportXml.php | 9 +- libraries/classes/Plugins/Import/README | 7 +- libraries/classes/Plugins/ImportPlugin.php | 14 +++ server_import.php | 4 +- tbl_import.php | 4 +- test/classes/ImportTest.php | 73 ++++++------ 16 files changed, 175 insertions(+), 134 deletions(-) diff --git a/db_import.php b/db_import.php index 0ec1d49ef6..e61bae6131 100644 --- a/db_import.php +++ b/db_import.php @@ -19,6 +19,8 @@ $header = $response->getHeader(); $scripts = $header->getScripts(); $scripts->addFile('import.js'); +$import = new Import(); + /** * Gets tables information and displays top links */ @@ -38,7 +40,7 @@ list( $response = Response::getInstance(); $response->addHTML( - Import::get( + $import->get( 'database', $db, $table, $max_upload_size ) ); diff --git a/import.php b/import.php index 75ba6d685c..d53de967fb 100644 --- a/import.php +++ b/import.php @@ -29,13 +29,15 @@ if (isset($_POST['format']) && $_POST['format'] == 'ldi') { */ require_once 'libraries/common.inc.php'; +$import = new Import(); + if (isset($_REQUEST['show_as_php'])) { $GLOBALS['show_as_php'] = $_REQUEST['show_as_php']; } // If there is a request to 'Simulate DML'. if (isset($_REQUEST['simulate_dml'])) { - Import::handleSimulateDmlRequest(); + $import->handleSimulateDmlRequest(); exit; } @@ -154,7 +156,7 @@ if (! empty($sql_query)) { // If there is a request to ROLLBACK when finished. if (isset($_REQUEST['rollback_query'])) { - Import::handleRollbackRequest($import_text); + $import->handleRollbackRequest($import_text); } // refresh navigation and main panels @@ -483,12 +485,12 @@ if ($import_file != 'none' && ! $error) { $import_handle = new File($import_file); $import_handle->checkUploadedFile(); if ($import_handle->isError()) { - Import::stop($import_handle->getError()); + $import->stop($import_handle->getError()); } $import_handle->setDecompressContent(true); $import_handle->open(); if ($import_handle->isError()) { - Import::stop($import_handle->getError()); + $import->stop($import_handle->getError()); } } elseif (! $error) { if (! isset($import_text) || empty($import_text)) { @@ -499,7 +501,7 @@ if ($import_file != 'none' && ! $error) { 'by your PHP configuration. See [doc@faq1-16]FAQ 1.16[/doc].' ) ); - Import::stop($message); + $import->stop($message); } } @@ -522,7 +524,7 @@ if (Encoding::isSupported() && isset($charset_of_file)) { if (! $error && isset($_POST['skip'])) { $original_skip = $skip = intval($_POST['skip']); while ($skip > 0 && ! $finished) { - Import::getNextChunk($skip < $read_limit ? $skip : $read_limit); + $import->getNextChunk($skip < $read_limit ? $skip : $read_limit); // Disable read progressivity, otherwise we eat all memory! $read_multiply = 1; $skip -= $read_limit; @@ -546,7 +548,7 @@ if (! $error) { $message = PhpMyAdmin\Message::error( __('Could not load import plugins, please check your installation!') ); - Import::stop($message); + $import->stop($message); } else { // Do the real import try { diff --git a/libraries/classes/Import.php b/libraries/classes/Import.php index 7456685401..10726da8a8 100644 --- a/libraries/classes/Import.php +++ b/libraries/classes/Import.php @@ -59,7 +59,7 @@ class Import * @return boolean true if timeout is close * @access public */ - public static function checkTimeout() + public function checkTimeout() { global $timestamp, $maximum_time, $timeout_passed; if ($maximum_time == 0) { @@ -86,7 +86,7 @@ class Import * @return void * @access public */ - public static function executeQuery($sql, $full, array &$sql_data) + public function executeQuery($sql, $full, array &$sql_data) { global $go_sql, $sql_query, $my_die, $error, $reload, @@ -147,7 +147,7 @@ class Import // If a 'USE ' SQL-clause was found and the query // succeeded, set our current $db to the new one if ($result != false) { - list($db, $reload) = self::lookForUse( + list($db, $reload) = $this->lookForUse( $sql, $db, $reload @@ -174,7 +174,7 @@ class Import * @return void * @access public */ - public static function runQuery($sql = '', $full = '', array &$sql_data = array()) + public function runQuery($sql = '', $full = '', array &$sql_data = array()) { global $import_run_buffer, $go_sql, $complete_query, $display_query, $sql_query, $error, $reload, $result, $msg, @@ -183,7 +183,7 @@ class Import $read_multiply = 1; if (!isset($import_run_buffer)) { // Do we have something to push into buffer? - $import_run_buffer = self::runQueryPost( + $import_run_buffer = $this->runQueryPost( $import_run_buffer, $sql, $full ); return; @@ -193,7 +193,7 @@ class Import if ($skip_queries > 0) { $skip_queries--; // Do we have something to push into buffer? - $import_run_buffer = self::runQueryPost( + $import_run_buffer = $this->runQueryPost( $import_run_buffer, $sql, $full ); return; @@ -242,7 +242,7 @@ class Import $sql_data['valid_queries'] = 0; unset($sql_data['valid_full']); for ($i = 0; $i < $count; $i++) { - self::executeQuery( + $this->executeQuery( $queries[$i], $fulls[$i], $sql_data @@ -250,7 +250,7 @@ class Import } } - self::executeQuery( + $this->executeQuery( $import_run_buffer['sql'], $import_run_buffer['full'], $sql_data @@ -283,7 +283,7 @@ class Import } // Do we have something to push into buffer? - $import_run_buffer = self::runQueryPost($import_run_buffer, $sql, $full); + $import_run_buffer = $this->runQueryPost($import_run_buffer, $sql, $full); // In case of ROLLBACK, notify the user. if (isset($_REQUEST['rollback_query'])) { @@ -300,7 +300,7 @@ class Import * * @return array Buffer of queries for import */ - public static function runQueryPost($import_run_buffer, $sql, $full) + public function runQueryPost($import_run_buffer, $sql, $full) { if (!empty($sql) || !empty($full)) { $import_run_buffer = array('sql' => $sql, 'full' => $full); @@ -321,15 +321,15 @@ class Import * @return array (current or new db, whether to reload) * @access public */ - public static function lookForUse($buffer, $db, $reload) + public function lookForUse($buffer, $db, $reload) { if (preg_match('@^[\s]*USE[[:space:]]+([\S]+)@i', $buffer, $match)) { $db = trim($match[1]); $db = trim($db, ';'); // for example, USE abc; // $db must not contain the escape characters generated by backquote() - // ( used in self::buildSql() as: backquote($db_name), and then called - // in self::runQuery() which in turn calls self::lookForUse() ) + // ( used in buildSql() as: backquote($db_name), and then called + // in runQuery() which in turn calls lookForUse() ) $db = Util::unQuote($db); $reload = true; @@ -347,7 +347,7 @@ class Import * @return string part of file/buffer * @access public */ - public static function getNextChunk($size = 32768) + public function getNextChunk($size = 32768) { global $compression, $import_handle, $charset_conversion, $charset_of_file, $read_multiply; @@ -365,7 +365,7 @@ class Import $size = $GLOBALS['read_limit']; } - if (self::checkTimeout()) { + if ($this->checkTimeout()) { return false; } if ($GLOBALS['finished']) { @@ -440,7 +440,7 @@ class Import * @return string The column's "Excel" name * @access public */ - public static function getColumnAlphaName($num) + public function getColumnAlphaName($num) { $A = 65; // ASCII value for capital "A" $col_name = ""; @@ -456,7 +456,7 @@ class Import } // recursive function call - $col_name = self::getColumnAlphaName($div); + $col_name = $this->getColumnAlphaName($div); // use modulus as new column number $num = $remain; } @@ -487,7 +487,7 @@ class Import * @return int The column number * @access public */ - public static function getColumnNumberFromName($name) + public function getColumnNumberFromName($name) { if (empty($name)) { return 0; @@ -522,7 +522,7 @@ class Import * @return int Precision of the given decimal size notation * @access public */ - public static function getDecimalPrecision($last_cumulative_size) + public function getDecimalPrecision($last_cumulative_size) { return (int)substr( $last_cumulative_size, @@ -540,7 +540,7 @@ class Import * @return int Scale of the given decimal size notation * @access public */ - public static function getDecimalScale($last_cumulative_size) + public function getDecimalScale($last_cumulative_size) { return (int)substr( $last_cumulative_size, @@ -558,7 +558,7 @@ class Import * representation of the given decimal cell * @access public */ - public static function getDecimalSize($cell) + public function getDecimalSize($cell) { $curr_size = mb_strlen((string)$cell); $decPos = mb_strpos($cell, "."); @@ -585,7 +585,7 @@ class Import * * @todo Handle the error cases more elegantly */ - public static function detectSize($last_cumulative_size, $last_cumulative_type, + public function detectSize($last_cumulative_size, $last_cumulative_type, $curr_type, $cell ) { $curr_size = mb_strlen((string)$cell); @@ -612,7 +612,7 @@ class Import /** * The last cumulative type was DECIMAL */ - $oldM = self::getDecimalPrecision($last_cumulative_size); + $oldM = $this->getDecimalPrecision($last_cumulative_size); if ($curr_size >= $oldM) { return $curr_size; @@ -652,7 +652,7 @@ class Import */ if ($last_cumulative_type == self::VARCHAR) { /* Convert $last_cumulative_size from varchar to decimal format */ - $size = self::getDecimalSize($cell); + $size = $this->getDecimalSize($cell); if ($size[self::M] >= $last_cumulative_size) { return $size[self::M]; @@ -663,10 +663,10 @@ class Import /** * The last cumulative type was DECIMAL */ - $size = self::getDecimalSize($cell); + $size = $this->getDecimalSize($cell); - $oldM = self::getDecimalPrecision($last_cumulative_size); - $oldD = self::getDecimalScale($last_cumulative_size); + $oldM = $this->getDecimalPrecision($last_cumulative_size); + $oldD = $this->getDecimalScale($last_cumulative_size); /* New val if M or D is greater than current largest */ if ($size[self::M] > $oldM || $size[self::D] > $oldD) { @@ -681,7 +681,7 @@ class Import * The last cumulative type was BIGINT or INT */ /* Convert $last_cumulative_size from int to decimal format */ - $size = self::getDecimalSize($cell); + $size = $this->getDecimalSize($cell); if ($size[self::M] >= $last_cumulative_size) { return $size[self::FULL]; @@ -693,7 +693,7 @@ class Import * This is the first row to be analyzed */ /* First row of the column */ - $size = self::getDecimalSize($cell); + $size = $this->getDecimalSize($cell); return $size[self::FULL]; } @@ -723,8 +723,8 @@ class Import /** * The last cumulative type was DECIMAL */ - $oldM = self::getDecimalPrecision($last_cumulative_size); - $oldD = self::getDecimalScale($last_cumulative_size); + $oldM = $this->getDecimalPrecision($last_cumulative_size); + $oldD = $this->getDecimalScale($last_cumulative_size); $oldInt = $oldM - $oldD; $newInt = mb_strlen((string)$cell); @@ -784,7 +784,7 @@ class Import * (VARCHAR or INT or BIGINT or DECIMAL or NONE) * @access public */ - public static function detectType($last_cumulative_type, $cell) + public function detectType($last_cumulative_type, $cell) { /** * If numeric, determine if decimal, int or bigint @@ -829,7 +829,7 @@ class Import * * @todo Handle the error case more elegantly */ - public static function analyzeTable(array &$table) + public function analyzeTable(array &$table) { /* Get number of rows in table */ $numRows = count($table[self::ROWS]); @@ -867,9 +867,9 @@ class Import /* Analyze the column in each row */ for ($j = 0; $j < $numRows; ++$j) { /* Determine type of the current cell */ - $curr_type = self::detectType($types[$i], $table[self::ROWS][$j][$i]); + $curr_type = $this->detectType($types[$i], $table[self::ROWS][$j][$i]); /* Determine size of the current cell */ - $sizes[$i] = self::detectSize( + $sizes[$i] = $this->detectSize( $sizes[$i], $types[$i], $curr_type, @@ -931,7 +931,7 @@ class Import * * @link https://wiki.phpmyadmin.net/pma/Import */ - public static function buildSql($db_name, array &$tables, &$analyses = null, + public function buildSql($db_name, array &$tables, &$analyses = null, &$additional_sql = null, $options = null, array &$sql_data ) { /* Needed to quell the beast that is Message */ @@ -975,7 +975,7 @@ class Import /* Execute the SQL statements create above */ $sql_len = count($sql); for ($i = 0; $i < $sql_len; ++$i) { - self::runQuery($sql[$i], $sql[$i], $sql_data); + $this->runQuery($sql[$i], $sql[$i], $sql_data); } /* No longer needed */ @@ -1010,7 +1010,7 @@ class Import $additional_sql[$i] ); /* Execute the resulting statements */ - self::runQuery($additional_sql[$i], $additional_sql[$i], $sql_data); + $this->runQuery($additional_sql[$i], $additional_sql[$i], $sql_data); } } @@ -1062,7 +1062,7 @@ class Import * after it is formed so that we don't have * to store them in a (possibly large) buffer */ - self::runQuery($tempSQLStr, $tempSQLStr, $sql_data); + $this->runQuery($tempSQLStr, $tempSQLStr, $sql_data); } } @@ -1153,7 +1153,7 @@ class Import * after it is formed so that we don't have * to store them in a (possibly large) buffer */ - self::runQuery($tempSQLStr, $tempSQLStr, $sql_data); + $this->runQuery($tempSQLStr, $tempSQLStr, $sql_data); } /* No longer needed */ @@ -1310,7 +1310,7 @@ class Import * @access public * */ - public static function stop(Message $error_message) + public function stop(Message $error_message) { global $import_handle, $file_to_unlink; @@ -1338,7 +1338,7 @@ class Import * * @return void */ - public static function handleSimulateDmlRequest() + public function handleSimulateDmlRequest() { $response = Response::getInstance(); $error = false; @@ -1381,7 +1381,7 @@ class Import } // Get the matched rows for the query. - $result = self::getMatchedRows($analyzed_sql_results); + $result = $this->getMatchedRows($analyzed_sql_results); if (! $error = $GLOBALS['dbi']->getError()) { $sql_data[] = $result; } else { @@ -1405,19 +1405,19 @@ class Import * * @return mixed */ - public static function getMatchedRows(array $analyzed_sql_results = array()) + public function getMatchedRows(array $analyzed_sql_results = array()) { $statement = $analyzed_sql_results['statement']; $matched_row_query = ''; if ($statement instanceof DeleteStatement) { - $matched_row_query = self::getSimulatedDeleteQuery($analyzed_sql_results); + $matched_row_query = $this->getSimulatedDeleteQuery($analyzed_sql_results); } elseif ($statement instanceof UpdateStatement) { - $matched_row_query = self::getSimulatedUpdateQuery($analyzed_sql_results); + $matched_row_query = $this->getSimulatedUpdateQuery($analyzed_sql_results); } // Execute the query and get the number of matched rows. - $matched_rows = self::executeMatchedRowQuery($matched_row_query); + $matched_rows = $this->executeMatchedRowQuery($matched_row_query); // URL to matched rows. $_url_params = array( @@ -1440,7 +1440,7 @@ class Import * * @return string SQL query */ - public static function getSimulatedUpdateQuery(array $analyzed_sql_results) + public function getSimulatedUpdateQuery(array $analyzed_sql_results) { $table_references = Query::getTables( $analyzed_sql_results['statement'] @@ -1496,7 +1496,7 @@ class Import * * @return string SQL query */ - public static function getSimulatedDeleteQuery(array $analyzed_sql_results) + public function getSimulatedDeleteQuery(array $analyzed_sql_results) { $table_references = Query::getTables( $analyzed_sql_results['statement'] @@ -1541,7 +1541,7 @@ class Import * * @return integer Number of rows returned */ - public static function executeMatchedRowQuery($matched_row_query) + public function executeMatchedRowQuery($matched_row_query) { $GLOBALS['dbi']->selectDb($GLOBALS['db']); // Execute the query. @@ -1559,7 +1559,7 @@ class Import * * @return void */ - public static function handleRollbackRequest($sql_query) + public function handleRollbackRequest($sql_query) { $sql_delimiter = $_REQUEST['sql_delimiter']; $queries = explode($sql_delimiter, $sql_query); @@ -1574,7 +1574,7 @@ class Import } // Check each query for ROLLBACK support. - if (! self::checkIfRollbackPossible($sql_query)) { + if (! $this->checkIfRollbackPossible($sql_query)) { $global_error = $GLOBALS['dbi']->getError(); if ($global_error) { $error = $global_error; @@ -1604,7 +1604,7 @@ class Import * * @return bool */ - public static function checkIfRollbackPossible($sql_query) + public function checkIfRollbackPossible($sql_query) { $parser = new Parser($sql_query); @@ -1628,7 +1628,7 @@ class Import // Check if each table is 'InnoDB'. foreach ($tables as $table) { - if (! self::isTableTransactional($table)) { + if (! $this->isTableTransactional($table)) { return false; } } @@ -1643,7 +1643,7 @@ class Import * * @return bool */ - public static function isTableTransactional($table) + public function isTableTransactional($table) { $table = explode('.', $table); if (count($table) == 2) { diff --git a/libraries/classes/Plugins/Import/AbstractImportCsv.php b/libraries/classes/Plugins/Import/AbstractImportCsv.php index 25cdcbc943..14ed2358e8 100644 --- a/libraries/classes/Plugins/Import/AbstractImportCsv.php +++ b/libraries/classes/Plugins/Import/AbstractImportCsv.php @@ -23,6 +23,14 @@ use PhpMyAdmin\Properties\Options\Items\TextPropertyItem; */ abstract class AbstractImportCsv extends ImportPlugin { + /** + * AbstractImportCsv constructor. + */ + public function __construct() + { + parent::__construct(); + } + /** * Sets the import plugin properties. * Called in the constructor. diff --git a/libraries/classes/Plugins/Import/ImportCsv.php b/libraries/classes/Plugins/Import/ImportCsv.php index b68fcea06d..f5a8feeb45 100644 --- a/libraries/classes/Plugins/Import/ImportCsv.php +++ b/libraries/classes/Plugins/Import/ImportCsv.php @@ -35,6 +35,7 @@ class ImportCsv extends AbstractImportCsv */ public function __construct() { + parent::__construct(); $this->setProperties(); } @@ -242,7 +243,7 @@ class ImportCsv extends AbstractImportCsv $max_cols = 0; $csv_terminated_len = mb_strlen($csv_terminated); while (!($finished && $i >= $len) && !$error && !$timeout_passed) { - $data = Import::getNextChunk(); + $data = $this->import->getNextChunk(); if ($data === false) { // subtract data we didn't handle yet and stop processing $GLOBALS['offset'] -= strlen($buffer); @@ -573,7 +574,7 @@ class ImportCsv extends AbstractImportCsv * @todo maybe we could add original line to verbose * SQL in comment */ - Import::runQuery($sql, $sql, $sql_data); + $this->import->runQuery($sql, $sql, $sql_data); } $line++; @@ -626,7 +627,7 @@ class ImportCsv extends AbstractImportCsv /* Obtain the best-fit MySQL types for each column */ $analyses = array(); - $analyses[] = Import::analyzeTable($tables[0]); + $analyses[] = $this->import->analyzeTable($tables[0]); /** * string $db_name (no backquotes) @@ -649,14 +650,14 @@ class ImportCsv extends AbstractImportCsv $create = null; /* Created and execute necessary SQL statements from data */ - Import::buildSql($db_name, $tables, $analyses, $create, $options, $sql_data); + $this->import->buildSql($db_name, $tables, $analyses, $create, $options, $sql_data); unset($tables); unset($analyses); } // Commit any possible data in buffers - Import::runQuery('', '', $sql_data); + $this->import->runQuery('', '', $sql_data); if (count($values) != 0 && !$error) { $message = Message::error( diff --git a/libraries/classes/Plugins/Import/ImportLdi.php b/libraries/classes/Plugins/Import/ImportLdi.php index e41e6f02f8..59a27b9e62 100644 --- a/libraries/classes/Plugins/Import/ImportLdi.php +++ b/libraries/classes/Plugins/Import/ImportLdi.php @@ -39,6 +39,7 @@ class ImportLdi extends AbstractImportCsv */ public function __construct() { + parent::__construct(); $this->setProperties(); } @@ -170,8 +171,8 @@ class ImportLdi extends AbstractImportCsv $sql .= ')'; } - Import::runQuery($sql, $sql, $sql_data); - Import::runQuery('', '', $sql_data); + $this->import->runQuery($sql, $sql, $sql_data); + $this->import->runQuery('', '', $sql_data); $finished = true; } } diff --git a/libraries/classes/Plugins/Import/ImportMediawiki.php b/libraries/classes/Plugins/Import/ImportMediawiki.php index 852110e42b..f0dc0ea60e 100644 --- a/libraries/classes/Plugins/Import/ImportMediawiki.php +++ b/libraries/classes/Plugins/Import/ImportMediawiki.php @@ -33,6 +33,7 @@ class ImportMediawiki extends ImportPlugin */ public function __construct() { + parent::__construct(); $this->setProperties(); } @@ -93,7 +94,7 @@ class ImportMediawiki extends ImportPlugin $cur_table_name = ""; while (!$finished && !$error && !$timeout_passed) { - $data = Import::getNextChunk(); + $data = $this->import->getNextChunk(); if ($data === false) { // Subtract data we didn't handle yet and stop processing @@ -314,13 +315,13 @@ class ImportMediawiki extends ImportPlugin // Obtain the best-fit MySQL types for each column $analyses = array(); - $analyses [] = Import::analyzeTable($tables[0]); + $analyses [] = $this->import->analyzeTable($tables[0]); $this->_executeImportTables($tables, $analyses, $sql_data); } // Commit any possible data in buffers - Import::runQuery('', '', $sql_data); + $this->import->runQuery('', '', $sql_data); } /** @@ -393,7 +394,7 @@ class ImportMediawiki extends ImportPlugin $create = null; // Create and execute necessary SQL statements from data - Import::buildSql($db_name, $tables, $analyses, $create, $options, $sql_data); + $this->import->buildSql($db_name, $tables, $analyses, $create, $options, $sql_data); unset($tables); unset($analyses); diff --git a/libraries/classes/Plugins/Import/ImportOds.php b/libraries/classes/Plugins/Import/ImportOds.php index 7100fadcb8..1c4ae8f40a 100644 --- a/libraries/classes/Plugins/Import/ImportOds.php +++ b/libraries/classes/Plugins/Import/ImportOds.php @@ -32,6 +32,7 @@ class ImportOds extends ImportPlugin */ public function __construct() { + parent::__construct(); $this->setProperties(); } @@ -113,7 +114,7 @@ class ImportOds extends ImportPlugin * it can process compressed files */ while (!($finished && $i >= $len) && !$error && !$timeout_passed) { - $data = Import::getNextChunk(); + $data = $this->import->getNextChunk(); if ($data === false) { /* subtract data we didn't handle yet and stop processing */ $GLOBALS['offset'] -= strlen($buffer); @@ -236,7 +237,7 @@ class ImportOds extends ImportPlugin } } else { for ($i = 0; $i < $num_null; ++$i) { - $col_names[] = Import::getColumnAlphaName( + $col_names[] = $this->import->getColumnAlphaName( $col_count + 1 ); ++$col_count; @@ -246,7 +247,7 @@ class ImportOds extends ImportPlugin if (!$col_names_in_first_row) { $tempRow[] = 'NULL'; } else { - $col_names[] = Import::getColumnAlphaName( + $col_names[] = $this->import->getColumnAlphaName( $col_count + 1 ); } @@ -295,7 +296,7 @@ class ImportOds extends ImportPlugin /* Fill out column names */ for ($i = count($col_names); $i < $max_cols; ++$i) { - $col_names[] = Import::getColumnAlphaName($i + 1); + $col_names[] = $this->import->getColumnAlphaName($i + 1); } /* Fill out all rows */ @@ -350,7 +351,7 @@ class ImportOds extends ImportPlugin $len = count($tables); for ($i = 0; $i < $len; ++$i) { - $analyses[] = Import::analyzeTable($tables[$i]); + $analyses[] = $this->import->analyzeTable($tables[$i]); } /** @@ -374,13 +375,13 @@ class ImportOds extends ImportPlugin $create = null; /* Created and execute necessary SQL statements from data */ - Import::buildSql($db_name, $tables, $analyses, $create, $options, $sql_data); + $this->import->buildSql($db_name, $tables, $analyses, $create, $options, $sql_data); unset($tables); unset($analyses); /* Commit any possible data in buffers */ - Import::runQuery('', '', $sql_data); + $this->import->runQuery('', '', $sql_data); } /** diff --git a/libraries/classes/Plugins/Import/ImportShp.php b/libraries/classes/Plugins/Import/ImportShp.php index c5d8b52cac..ec2477d79d 100644 --- a/libraries/classes/Plugins/Import/ImportShp.php +++ b/libraries/classes/Plugins/Import/ImportShp.php @@ -38,6 +38,7 @@ class ImportShp extends ImportPlugin */ public function __construct() { + parent::__construct(); $this->setProperties(); if (extension_loaded('zip')) { $this->zipExtension = new ZipExtension(); @@ -259,7 +260,7 @@ class ImportShp extends ImportPlugin // Use data from shape file to chose best-fit MySQL types for each column $analyses = array(); - $analyses[] = Import::analyzeTable($tables[0]); + $analyses[] = $this->import->analyzeTable($tables[0]); $table_no = 0; $spatial_col = 0; @@ -277,7 +278,7 @@ class ImportShp extends ImportPlugin // Created and execute necessary SQL statements from data $null_param = null; - Import::buildSql($db_name, $tables, $analyses, $null_param, $options, $sql_data); + $this->import->buildSql($db_name, $tables, $analyses, $null_param, $options, $sql_data); unset($tables); unset($analyses); @@ -286,7 +287,7 @@ class ImportShp extends ImportPlugin $error = false; // Commit any possible data in buffers - Import::runQuery('', '', $sql_data); + $this->import->runQuery('', '', $sql_data); } /** @@ -303,11 +304,13 @@ class ImportShp extends ImportPlugin { global $buffer, $eof; + $import = new Import(); + if (strlen($buffer) < $length) { if ($GLOBALS['finished']) { $eof = true; } else { - $buffer .= Import::getNextChunk(); + $buffer .= $import->getNextChunk(); } } $result = substr($buffer, 0, $length); diff --git a/libraries/classes/Plugins/Import/ImportSql.php b/libraries/classes/Plugins/Import/ImportSql.php index 240b8ba36a..d8a48e9dcc 100644 --- a/libraries/classes/Plugins/Import/ImportSql.php +++ b/libraries/classes/Plugins/Import/ImportSql.php @@ -30,6 +30,7 @@ class ImportSql extends ImportPlugin */ public function __construct() { + parent::__construct(); $this->setProperties(); } @@ -133,7 +134,7 @@ class ImportSql extends ImportPlugin if (empty($statement)) { // Importing new data. - $newData = Import::getNextChunk(); + $newData = $this->import->getNextChunk(); // Subtract data we didn't handle yet and stop processing. if ($newData === false) { @@ -155,19 +156,19 @@ class ImportSql extends ImportPlugin } // Executing the query. - Import::runQuery($statement, $statement, $sql_data); + $this->import->runQuery($statement, $statement, $sql_data); } // Extracting remaining statements. while ((!$error) && (!$timeout_passed) && (!empty($bq->query))) { $statement = $bq->extract(true); if (!empty($statement)) { - Import::runQuery($statement, $statement, $sql_data); + $this->import->runQuery($statement, $statement, $sql_data); } } // Finishing. - Import::runQuery('', '', $sql_data); + $this->import->runQuery('', '', $sql_data); } /** diff --git a/libraries/classes/Plugins/Import/ImportXml.php b/libraries/classes/Plugins/Import/ImportXml.php index b511582a7e..0b84484ce0 100644 --- a/libraries/classes/Plugins/Import/ImportXml.php +++ b/libraries/classes/Plugins/Import/ImportXml.php @@ -30,6 +30,7 @@ class ImportXml extends ImportPlugin */ public function __construct() { + parent::__construct(); $this->setProperties(); } @@ -71,7 +72,7 @@ class ImportXml extends ImportPlugin * it can process compressed files */ while (!($finished && $i >= $len) && !$error && !$timeout_passed) { - $data = Import::getNextChunk(); + $data = $this->import->getNextChunk(); if ($data === false) { /* subtract data we didn't handle yet and stop processing */ $GLOBALS['offset'] -= strlen($buffer); @@ -302,7 +303,7 @@ class ImportXml extends ImportPlugin $len = count($tables); for ($i = 0; $i < $len; ++$i) { - $analyses[] = Import::analyzeTable($tables[$i]); + $analyses[] = $this->import->analyzeTable($tables[$i]); } } } @@ -359,13 +360,13 @@ class ImportXml extends ImportPlugin } /* Created and execute necessary SQL statements from data */ - Import::buildSql($db_name, $tables, $analyses, $create, $options, $sql_data); + $this->import->buildSql($db_name, $tables, $analyses, $create, $options, $sql_data); unset($analyses); unset($tables); unset($create); /* Commit any possible data in buffers */ - Import::runQuery('', '', $sql_data); + $this->import->runQuery('', '', $sql_data); } } diff --git a/libraries/classes/Plugins/Import/README b/libraries/classes/Plugins/Import/README index 20b856eeb6..0bc6c415cf 100644 --- a/libraries/classes/Plugins/Import/README +++ b/libraries/classes/Plugins/Import/README @@ -34,6 +34,7 @@ class Import[Name] extends ImportPlugin */ public function __construct() { + parent::__construct(); $this->setProperties(); } @@ -105,7 +106,7 @@ class Import[Name] extends ImportPlugin $buffer = ''; while (! ($finished && $i >= $len) && ! $error && ! $timeout_passed) { - $data = Import::getNextChunk(); + $data = $this->import->getNextChunk(); if ($data === false) { // subtract data we didn't handle yet and stop processing $GLOBALS['offset'] -= strlen($buffer); @@ -117,10 +118,10 @@ class Import[Name] extends ImportPlugin $buffer .= $data; } // PARSE $buffer here, post sql queries using: - Import::runQuery($sql, $verbose_sql_with_comments, $sql_data); + $this->import->runQuery($sql, $verbose_sql_with_comments, $sql_data); } // End of import loop // Commit any possible data in buffers - Import::runQuery('', '', $sql_data); + $this->import->runQuery('', '', $sql_data); } diff --git a/libraries/classes/Plugins/ImportPlugin.php b/libraries/classes/Plugins/ImportPlugin.php index 42ed2286f4..924fe9a6b4 100644 --- a/libraries/classes/Plugins/ImportPlugin.php +++ b/libraries/classes/Plugins/ImportPlugin.php @@ -8,6 +8,7 @@ namespace PhpMyAdmin\Plugins; +use PhpMyAdmin\Import; use PhpMyAdmin\Properties\Plugins\ImportPluginProperties; /** @@ -25,6 +26,19 @@ abstract class ImportPlugin */ protected $properties; + /** + * @var Import + */ + protected $import; + + /** + * ImportPlugin constructor. + */ + public function __construct() + { + $this->import = new Import(); + } + /** * Handles the whole import logic * diff --git a/server_import.php b/server_import.php index 25d7c172e0..49487d7f4d 100644 --- a/server_import.php +++ b/server_import.php @@ -26,9 +26,11 @@ $scripts->addFile('import.js'); */ require 'libraries/server_common.inc.php'; +$import = new Import(); + $response = Response::getInstance(); $response->addHTML( - Import::get( + $import->get( 'server', $db, $table, $max_upload_size ) ); diff --git a/tbl_import.php b/tbl_import.php index e52543037a..2d66879b9a 100644 --- a/tbl_import.php +++ b/tbl_import.php @@ -21,6 +21,8 @@ $header = $response->getHeader(); $scripts = $header->getScripts(); $scripts->addFile('import.js'); +$import = new Import(); + /** * Gets tables information and displays top links */ @@ -28,7 +30,7 @@ require_once 'libraries/tbl_common.inc.php'; $url_query .= '&goto=tbl_import.php&back=tbl_import.php'; $response->addHTML( - Import::get( + $import->get( 'table', $db, $table, $max_upload_size ) ); diff --git a/test/classes/ImportTest.php b/test/classes/ImportTest.php index 43d03ce1b2..d006a6d001 100644 --- a/test/classes/ImportTest.php +++ b/test/classes/ImportTest.php @@ -13,12 +13,6 @@ use PhpMyAdmin\Url; use PhpMyAdmin\Util; use PHPUnit\Framework\TestCase; -/* - * we must set $GLOBALS['server'] here - * since 'check_user_privileges.inc.php' will use it globally - */ -$GLOBALS['server'] = 0; - /** * Tests for import functions * @@ -26,6 +20,11 @@ $GLOBALS['server'] = 0; */ class ImportTest extends TestCase { + /** + * @var Import $import + */ + private $import; + /** * Prepares environment for the test. * @@ -33,11 +32,13 @@ class ImportTest extends TestCase */ public function setUp() { + $GLOBALS['server'] = 0; $GLOBALS['cfg']['ServerDefault'] = ''; + $this->import = new Import(); } /** - * Test for Import::checkTimeout + * Test for checkTimeout * * @return void */ @@ -50,39 +51,39 @@ class ImportTest extends TestCase $maximum_time = 0; $timeout_passed = false; - $this->assertFalse(Import::checkTimeout()); + $this->assertFalse($this->import->checkTimeout()); //Reinit values. $timestamp = time(); $maximum_time = 0; $timeout_passed = true; - $this->assertFalse(Import::checkTimeout()); + $this->assertFalse($this->import->checkTimeout()); //Reinit values. $timestamp = time(); $maximum_time = 30; $timeout_passed = true; - $this->assertTrue(Import::checkTimeout()); + $this->assertTrue($this->import->checkTimeout()); //Reinit values. $timestamp = time()-15; $maximum_time = 30; $timeout_passed = false; - $this->assertFalse(Import::checkTimeout()); + $this->assertFalse($this->import->checkTimeout()); //Reinit values. $timestamp = time()-60; $maximum_time = 30; $timeout_passed = false; - $this->assertTrue(Import::checkTimeout()); + $this->assertTrue($this->import->checkTimeout()); } /** - * Test for Import::lookForUse + * Test for lookForUse * * @return void */ @@ -90,42 +91,42 @@ class ImportTest extends TestCase { $this->assertEquals( array(null, null), - Import::lookForUse(null, null, null) + $this->import->lookForUse(null, null, null) ); $this->assertEquals( array('myDb', null), - Import::lookForUse(null, 'myDb', null) + $this->import->lookForUse(null, 'myDb', null) ); $this->assertEquals( array('myDb', true), - Import::lookForUse(null, 'myDb', true) + $this->import->lookForUse(null, 'myDb', true) ); $this->assertEquals( array('myDb', true), - Import::lookForUse('select 1 from myTable', 'myDb', true) + $this->import->lookForUse('select 1 from myTable', 'myDb', true) ); $this->assertEquals( array('anotherDb', true), - Import::lookForUse('use anotherDb', 'myDb', false) + $this->import->lookForUse('use anotherDb', 'myDb', false) ); $this->assertEquals( array('anotherDb', true), - Import::lookForUse('use anotherDb', 'myDb', true) + $this->import->lookForUse('use anotherDb', 'myDb', true) ); $this->assertEquals( array('anotherDb', true), - Import::lookForUse('use `anotherDb`;', 'myDb', true) + $this->import->lookForUse('use `anotherDb`;', 'myDb', true) ); } /** - * Test for Import::getColumnAlphaName + * Test for getColumnAlphaName * * @param string $expected Expected result of the function * @param int $num The column number @@ -136,7 +137,7 @@ class ImportTest extends TestCase */ function testGetColumnAlphaName($expected, $num) { - $this->assertEquals($expected, Import::getColumnAlphaName($num)); + $this->assertEquals($expected, $this->import->getColumnAlphaName($num)); } /** @@ -157,7 +158,7 @@ class ImportTest extends TestCase } /** - * Test for Import::getColumnNumberFromName + * Test for getColumnNumberFromName * * @param int $expected Expected result of the function * @param string|null $name column name(i.e. "A", or "BC", etc.) @@ -168,7 +169,7 @@ class ImportTest extends TestCase */ function testGetColumnNumberFromName($expected, $name) { - $this->assertEquals($expected, Import::getColumnNumberFromName($name)); + $this->assertEquals($expected, $this->import->getColumnNumberFromName($name)); } /** @@ -189,7 +190,7 @@ class ImportTest extends TestCase } /** - * Test for Import::getDecimalPrecision + * Test for getDecimalPrecision * * @param int $expected Expected result of the function * @param string|null $size Size of field @@ -200,7 +201,7 @@ class ImportTest extends TestCase */ function testGetDecimalPrecision($expected, $size) { - $this->assertEquals($expected, Import::getDecimalPrecision($size)); + $this->assertEquals($expected, $this->import->getDecimalPrecision($size)); } /** @@ -219,7 +220,7 @@ class ImportTest extends TestCase } /** - * Test for Import::getDecimalScale + * Test for getDecimalScale * * @param int $expected Expected result of the function * @param string|null $size Size of field @@ -230,7 +231,7 @@ class ImportTest extends TestCase */ function testGetDecimalScale($expected, $size) { - $this->assertEquals($expected, Import::getDecimalScale($size)); + $this->assertEquals($expected, $this->import->getDecimalScale($size)); } /** @@ -249,7 +250,7 @@ class ImportTest extends TestCase } /** - * Test for Import::getDecimalSize + * Test for getDecimalSize * * @param array $expected Expected result of the function * @param string|null $cell Cell content @@ -260,7 +261,7 @@ class ImportTest extends TestCase */ function testGetDecimalSize($expected, $cell) { - $this->assertEquals($expected, Import::getDecimalSize($cell)); + $this->assertEquals($expected, $this->import->getDecimalSize($cell)); } /** @@ -279,7 +280,7 @@ class ImportTest extends TestCase } /** - * Test for Import::detectType + * Test for detectType * * @param int $expected Expected result of the function * @param int|null $type Last cumulative column type (VARCHAR or INT or @@ -293,7 +294,7 @@ class ImportTest extends TestCase */ function testDetectType($expected, $type, $cell) { - $this->assertEquals($expected, Import::detectType($type, $cell)); + $this->assertEquals($expected, $this->import->detectType($type, $cell)); } /** @@ -321,7 +322,7 @@ class ImportTest extends TestCase } /** - * Test for Import::getMatchedRows. + * Test for getMatchedRows. * * @return void */ @@ -385,7 +386,7 @@ class ImportTest extends TestCase 'statement' => $parser->statements[0], ); - $simulated_data = Import::getMatchedRows($analyzed_sql_results); + $simulated_data = $this->import->getMatchedRows($analyzed_sql_results); // URL to matched rows. $_url_params = array( @@ -407,7 +408,7 @@ class ImportTest extends TestCase } /** - * Test for Import::checkIfRollbackPossible + * Test for checkIfRollbackPossible * * @return void */ @@ -475,6 +476,6 @@ class ImportTest extends TestCase . 'SET `table_1`.`id` = `table_2`.`id` ' . 'WHERE 1'; - $this->assertEquals(true, Import::checkIfRollbackPossible($sql_query)); + $this->assertEquals(true, $this->import->checkIfRollbackPossible($sql_query)); } }