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)); } }