Refactor PhpMyAdmin\Import static methods

Replaces static methods with instance methods.

Signed-off-by: Maurício Meneghini Fauth <mauriciofauth@gmail.com>
This commit is contained in:
Maurício Meneghini Fauth 2018-03-31 21:32:18 -03:00
parent 2638eb9207
commit 1a82d6f0d2
16 changed files with 175 additions and 134 deletions

View File

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

View File

@ -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 {

View File

@ -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 <db>' 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) {

View File

@ -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.

View File

@ -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(

View File

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

View File

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

View File

@ -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);
}
/**

View File

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

View File

@ -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);
}
/**

View File

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

View File

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

View File

@ -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
*

View File

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

View File

@ -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 .= '&amp;goto=tbl_import.php&amp;back=tbl_import.php';
$response->addHTML(
Import::get(
$import->get(
'table', $db, $table, $max_upload_size
)
);

View File

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