From 19e0199a364aedb1957e98e273c963d49f1a5c7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Mon, 4 Sep 2017 22:35:55 -0300 Subject: [PATCH] Refactor import functions to static methods MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: MaurĂ­cio Meneghini Fauth --- import.php | 18 +- libraries/classes/Import.php | 1696 +++++++++++++++++ .../classes/Plugins/Import/ImportCsv.php | 11 +- .../classes/Plugins/Import/ImportLdi.php | 5 +- .../Plugins/Import/ImportMediawiki.php | 13 +- .../classes/Plugins/Import/ImportOds.php | 25 +- .../classes/Plugins/Import/ImportShp.php | 13 +- .../classes/Plugins/Import/ImportSql.php | 11 +- .../classes/Plugins/Import/ImportXml.php | 23 +- libraries/classes/Plugins/Import/README | 7 +- libraries/import.lib.php | 1690 ---------------- .../ImportTest.php} | 107 +- test/classes/plugin/import/ImportCsvTest.php | 1 - test/classes/plugin/import/ImportLdiTest.php | 1 - .../plugin/import/ImportMediawikiTest.php | 1 - test/classes/plugin/import/ImportOdsTest.php | 1 - test/classes/plugin/import/ImportShpTest.php | 1 - test/classes/plugin/import/ImportSqlTest.php | 1 - test/classes/plugin/import/ImportXmlTest.php | 1 - 19 files changed, 1814 insertions(+), 1812 deletions(-) create mode 100644 libraries/classes/Import.php delete mode 100644 libraries/import.lib.php rename test/{libraries/PMA_import_test.php => classes/ImportTest.php} (79%) diff --git a/import.php b/import.php index 76ed062add..65780c1d34 100644 --- a/import.php +++ b/import.php @@ -10,6 +10,7 @@ use PhpMyAdmin\Bookmark; use PhpMyAdmin\Core; use PhpMyAdmin\Encoding; use PhpMyAdmin\File; +use PhpMyAdmin\Import; use PhpMyAdmin\Plugins\ImportPlugin; use PhpMyAdmin\Response; use PhpMyAdmin\Sql; @@ -30,12 +31,9 @@ if (isset($_REQUEST['show_as_php'])) { $GLOBALS['show_as_php'] = $_REQUEST['show_as_php']; } -// Import functions. -require_once 'libraries/import.lib.php'; - // If there is a request to 'Simulate DML'. if (isset($_REQUEST['simulate_dml'])) { - PMA_handleSimulateDMLRequest(); + Import::handleSimulateDmlRequest(); exit; } @@ -147,7 +145,7 @@ if (! empty($sql_query)) { // If there is a request to ROLLBACK when finished. if (isset($_REQUEST['rollback_query'])) { - PMA_handleRollbackRequest($import_text); + Import::handleRollbackRequest($import_text); } // refresh navigation and main panels @@ -464,12 +462,12 @@ if ($import_file != 'none' && ! $error) { $import_handle = new File($import_file); $import_handle->checkUploadedFile(); if ($import_handle->isError()) { - PMA_stopImport($import_handle->getError()); + Import::stop($import_handle->getError()); } $import_handle->setDecompressContent(true); $import_handle->open(); if ($import_handle->isError()) { - PMA_stopImport($import_handle->getError()); + Import::stop($import_handle->getError()); } } elseif (! $error) { if (! isset($import_text) || empty($import_text)) { @@ -480,7 +478,7 @@ if ($import_file != 'none' && ! $error) { 'by your PHP configuration. See [doc@faq1-16]FAQ 1.16[/doc].' ) ); - PMA_stopImport($message); + Import::stop($message); } } @@ -503,7 +501,7 @@ if (Encoding::isSupported() && isset($charset_of_file)) { if (! $error && isset($_POST['skip'])) { $original_skip = $skip = intval($_POST['skip']); while ($skip > 0 && ! $finished) { - PMA_importGetNextChunk($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; @@ -529,7 +527,7 @@ if (! $error) { $message = PhpMyAdmin\Message::error( __('Could not load import plugins, please check your installation!') ); - PMA_stopImport($message); + Import::stop($message); } else { // Do the real import try { diff --git a/libraries/classes/Import.php b/libraries/classes/Import.php new file mode 100644 index 0000000000..fad14cdf00 --- /dev/null +++ b/libraries/classes/Import.php @@ -0,0 +1,1696 @@ + ($maximum_time - 5)) { + $timeout_passed = true; + return true; + } else { + return false; + } + } + + /** + * Runs query inside import buffer. This is needed to allow displaying + * of last SELECT, SHOW or HANDLER results and similar nice stuff. + * + * @param string $sql query to run + * @param string $full query to display, this might be commented + * @param array &$sql_data SQL parse data storage + * + * @return void + * @access public + */ + public static function executeQuery($sql, $full, &$sql_data) + { + global $go_sql, + $sql_query, $my_die, $error, $reload, + $result, $msg, + $cfg, $sql_query_disabled, $db; + + $result = $GLOBALS['dbi']->tryQuery($sql); + + // USE query changes the database, son need to track + // while running multiple queries + $is_use_query = mb_stripos($sql, "use ") !== false; + + $msg = '# '; + if ($result === false) { // execution failed + if (! isset($my_die)) { + $my_die = array(); + } + $my_die[] = array( + 'sql' => $full, + 'error' => $GLOBALS['dbi']->getError() + ); + + $msg .= __('Error'); + + if (! $cfg['IgnoreMultiSubmitErrors']) { + $error = true; + return; + } + } else { + $a_num_rows = (int)@$GLOBALS['dbi']->numRows($result); + $a_aff_rows = (int)@$GLOBALS['dbi']->affectedRows(); + if ($a_num_rows > 0) { + $msg .= __('Rows') . ': ' . $a_num_rows; + } elseif ($a_aff_rows > 0) { + $message = Message::getMessageForAffectedRows( + $a_aff_rows + ); + $msg .= $message->getMessage(); + } else { + $msg .= __( + 'MySQL returned an empty result set (i.e. zero ' + . 'rows).' + ); + } + + if (($a_num_rows > 0) || $is_use_query) { + $sql_data['valid_sql'][] = $sql; + if (!isset($sql_data['valid_queries'])) { + $sql_data['valid_queries'] = 0; + } + $sql_data['valid_queries']++; + } + } + if (! $sql_query_disabled) { + $sql_query .= $msg . "\n"; + } + + // 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( + $sql, + $db, + $reload + ); + } + + $pattern = '@^[\s]*(DROP|CREATE)[\s]+(IF EXISTS[[:space:]]+)' + . '?(TABLE|DATABASE)[[:space:]]+(.+)@im'; + if ($result != false + && preg_match($pattern, $sql) + ) { + $reload = true; + } + } + + /** + * Runs query inside import buffer. This is needed to allow displaying + * of last SELECT, SHOW or HANDLER results and similar nice stuff. + * + * @param string $sql query to run + * @param string $full query to display, this might be commented + * @param array &$sql_data SQL parse data storage + * + * @return void + * @access public + */ + public static function runQuery($sql = '', $full = '', &$sql_data = array()) + { + global $import_run_buffer, $go_sql, $complete_query, $display_query, + $sql_query, $error, $reload, $result, $msg, + $skip_queries, $executed_queries, $max_sql_len, $read_multiply, + $cfg, $sql_query_disabled, $db, $run_query, $is_superuser; + $read_multiply = 1; + if (!isset($import_run_buffer)) { + // Do we have something to push into buffer? + $import_run_buffer = self::runQueryPost( + $import_run_buffer, $sql, $full + ); + return; + } + + // Should we skip something? + if ($skip_queries > 0) { + $skip_queries--; + // Do we have something to push into buffer? + $import_run_buffer = self::runQueryPost( + $import_run_buffer, $sql, $full + ); + return; + } + + if (! empty($import_run_buffer['sql']) + && trim($import_run_buffer['sql']) != '' + ) { + $max_sql_len = max( + $max_sql_len, + mb_strlen($import_run_buffer['sql']) + ); + if (! $sql_query_disabled) { + $sql_query .= $import_run_buffer['full']; + } + + $executed_queries++; + + if ($run_query && $executed_queries < 50) { + $go_sql = true; + + if (! $sql_query_disabled) { + $complete_query = $sql_query; + $display_query = $sql_query; + } else { + $complete_query = ''; + $display_query = ''; + } + $sql_query = $import_run_buffer['sql']; + $sql_data['valid_sql'][] = $import_run_buffer['sql']; + $sql_data['valid_full'][] = $import_run_buffer['full']; + if (! isset($sql_data['valid_queries'])) { + $sql_data['valid_queries'] = 0; + } + $sql_data['valid_queries']++; + } elseif ($run_query) { + + /* Handle rollback from go_sql */ + if ($go_sql && isset($sql_data['valid_full'])) { + $queries = $sql_data['valid_sql']; + $fulls = $sql_data['valid_full']; + $count = $sql_data['valid_queries']; + $go_sql = false; + + $sql_data['valid_sql'] = array(); + $sql_data['valid_queries'] = 0; + unset($sql_data['valid_full']); + for ($i = 0; $i < $count; $i++) { + self::executeQuery( + $queries[$i], + $fulls[$i], + $sql_data + ); + } + } + + self::executeQuery( + $import_run_buffer['sql'], + $import_run_buffer['full'], + $sql_data + ); + } // end run query + // end non empty query + } elseif (! empty($import_run_buffer['full'])) { + if ($go_sql) { + $complete_query .= $import_run_buffer['full']; + $display_query .= $import_run_buffer['full']; + } else { + if (! $sql_query_disabled) { + $sql_query .= $import_run_buffer['full']; + } + } + } + // check length of query unless we decided to pass it to sql.php + // (if $run_query is false, we are just displaying so show + // the complete query in the textarea) + if (! $go_sql && $run_query) { + if (! empty($sql_query)) { + if (mb_strlen($sql_query) > 50000 + || $executed_queries > 50 + || $max_sql_len > 1000 + ) { + $sql_query = ''; + $sql_query_disabled = true; + } + } + } + + // Do we have something to push into buffer? + $import_run_buffer = self::runQueryPost($import_run_buffer, $sql, $full); + + // In case of ROLLBACK, notify the user. + if (isset($_REQUEST['rollback_query'])) { + $msg .= __('[ROLLBACK occurred.]'); + } + } + + /** + * Return import run buffer + * + * @param array $import_run_buffer Buffer of queries for import + * @param string $sql SQL query + * @param string $full Query to display + * + * @return array Buffer of queries for import + */ + public static function runQueryPost($import_run_buffer, $sql, $full) + { + if (!empty($sql) || !empty($full)) { + $import_run_buffer = array('sql' => $sql, 'full' => $full); + return $import_run_buffer; + } else { + unset($GLOBALS['import_run_buffer']); + return $import_run_buffer; + } + } + + /** + * Looks for the presence of USE to possibly change current db + * + * @param string $buffer buffer to examine + * @param string $db current db + * @param bool $reload reload + * + * @return array (current or new db, whether to reload) + * @access public + */ + public static 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() ) + $db = Util::unQuote($db); + + $reload = true; + } + return(array($db, $reload)); + } + + + /** + * Returns next part of imported file/buffer + * + * @param int $size size of buffer to read + * (this is maximal size function will return) + * + * @return string part of file/buffer + * @access public + */ + public static function getNextChunk($size = 32768) + { + global $compression, $import_handle, $charset_conversion, $charset_of_file, + $read_multiply; + + // Add some progression while reading large amount of data + if ($read_multiply <= 8) { + $size *= $read_multiply; + } else { + $size *= 8; + } + $read_multiply++; + + // We can not read too much + if ($size > $GLOBALS['read_limit']) { + $size = $GLOBALS['read_limit']; + } + + if (self::checkTimeout()) { + return false; + } + if ($GLOBALS['finished']) { + return true; + } + + if ($GLOBALS['import_file'] == 'none') { + // Well this is not yet supported and tested, + // but should return content of textarea + if (mb_strlen($GLOBALS['import_text']) < $size) { + $GLOBALS['finished'] = true; + return $GLOBALS['import_text']; + } else { + $r = mb_substr($GLOBALS['import_text'], 0, $size); + $GLOBALS['offset'] += $size; + $GLOBALS['import_text'] = mb_substr($GLOBALS['import_text'], $size); + return $r; + } + } + + $result = $import_handle->read($size); + $GLOBALS['finished'] = $import_handle->eof(); + $GLOBALS['offset'] += $size; + + if ($charset_conversion) { + return Encoding::convertString($charset_of_file, 'utf-8', $result); + } + + /** + * Skip possible byte order marks (I do not think we need more + * charsets, but feel free to add more, you can use wikipedia for + * reference: ) + * + * @todo BOM could be used for charset autodetection + */ + if ($GLOBALS['offset'] == $size) { + // UTF-8 + if (strncmp($result, "\xEF\xBB\xBF", 3) == 0) { + $result = mb_substr($result, 3); + // UTF-16 BE, LE + } elseif (strncmp($result, "\xFE\xFF", 2) == 0 + || strncmp($result, "\xFF\xFE", 2) == 0 + ) { + $result = mb_substr($result, 2); + } + } + return $result; + } + + /** + * Returns the "Excel" column name (i.e. 1 = "A", 26 = "Z", 27 = "AA", etc.) + * + * This functions uses recursion to build the Excel column name. + * + * The column number (1-26) is converted to the responding + * ASCII character (A-Z) and returned. + * + * If the column number is bigger than 26 (= num of letters in alphabet), + * an extra character needs to be added. To find this extra character, + * the number is divided by 26 and this value is passed to another instance + * of the same function (hence recursion). In that new instance the number is + * evaluated again, and if it is still bigger than 26, it is divided again + * and passed to another instance of the same function. This continues until + * the number is smaller than 26. Then the last called function returns + * the corresponding ASCII character to the function that called it. + * Each time a called function ends an extra character is added to the column name. + * When the first function is reached, the last character is added and the complete + * column name is returned. + * + * @param int $num the column number + * + * @return string The column's "Excel" name + * @access public + */ + public static function getColumnAlphaName($num) + { + $A = 65; // ASCII value for capital "A" + $col_name = ""; + + if ($num > 26) { + $div = (int)($num / 26); + $remain = (int)($num % 26); + + // subtract 1 of divided value in case the modulus is 0, + // this is necessary because A-Z has no 'zero' + if ($remain == 0) { + $div--; + } + + // recursive function call + $col_name = self::getColumnAlphaName($div); + // use modulus as new column number + $num = $remain; + } + + if ($num == 0) { + // use 'Z' if column number is 0, + // this is necessary because A-Z has no 'zero' + $col_name .= mb_chr(($A + 26) - 1); + } else { + // convert column number to ASCII character + $col_name .= mb_chr(($A + $num) - 1); + } + + return $col_name; + } + + /** + * Returns the column number based on the Excel name. + * So "A" = 1, "Z" = 26, "AA" = 27, etc. + * + * Basically this is a base26 (A-Z) to base10 (0-9) conversion. + * It iterates through all characters in the column name and + * calculates the corresponding value, based on character value + * (A = 1, ..., Z = 26) and position in the string. + * + * @param string $name column name(i.e. "A", or "BC", etc.) + * + * @return int The column number + * @access public + */ + public static function getColumnNumberFromName($name) + { + if (empty($name)) { + return 0; + } + + $name = mb_strtoupper($name); + $num_chars = mb_strlen($name); + $column_number = 0; + for ($i = 0; $i < $num_chars; ++$i) { + // read string from back to front + $char_pos = ($num_chars - 1) - $i; + + // convert capital character to ASCII value + // and subtract 64 to get corresponding decimal value + // ASCII value of "A" is 65, "B" is 66, etc. + // Decimal equivalent of "A" is 1, "B" is 2, etc. + $number = (int)(mb_ord($name[$char_pos]) - 64); + + // base26 to base10 conversion : multiply each number + // with corresponding value of the position, in this case + // $i=0 : 1; $i=1 : 26; $i=2 : 676; ... + $column_number += $number * pow(26, $i); + } + return $column_number; + } + + /** + * Obtains the precision (total # of digits) from a size of type decimal + * + * @param string $last_cumulative_size Size of type decimal + * + * @return int Precision of the given decimal size notation + * @access public + */ + public static function getDecimalPrecision($last_cumulative_size) + { + return (int)substr( + $last_cumulative_size, + 0, + strpos($last_cumulative_size, ",") + ); + } + + /** + * Obtains the scale (# of digits to the right of the decimal point) + * from a size of type decimal + * + * @param string $last_cumulative_size Size of type decimal + * + * @return int Scale of the given decimal size notation + * @access public + */ + public static function getDecimalScale($last_cumulative_size) + { + return (int)substr( + $last_cumulative_size, + (strpos($last_cumulative_size, ",") + 1), + (strlen($last_cumulative_size) - strpos($last_cumulative_size, ",")) + ); + } + + /** + * Obtains the decimal size of a given cell + * + * @param string $cell cell content + * + * @return array Contains the precision, scale, and full size + * representation of the given decimal cell + * @access public + */ + public static function getDecimalSize($cell) + { + $curr_size = mb_strlen((string)$cell); + $decPos = mb_strpos($cell, "."); + $decPrecision = ($curr_size - 1) - $decPos; + + $m = $curr_size - 1; + $d = $decPrecision; + + return array($m, $d, ($m . "," . $d)); + } + + /** + * Obtains the size of the given cell + * + * @param string $last_cumulative_size Last cumulative column size + * @param int $last_cumulative_type Last cumulative column type + * (NONE or VARCHAR or DECIMAL or INT or BIGINT) + * @param int $curr_type Type of the current cell + * (NONE or VARCHAR or DECIMAL or INT or BIGINT) + * @param string $cell The current cell + * + * @return string Size of the given cell in the type-appropriate format + * @access public + * + * @todo Handle the error cases more elegantly + */ + public static function detectSize($last_cumulative_size, $last_cumulative_type, + $curr_type, $cell + ) { + $curr_size = mb_strlen((string)$cell); + + /** + * If the cell is NULL, don't treat it as a varchar + */ + if (! strcmp('NULL', $cell)) { + return $last_cumulative_size; + } elseif ($curr_type == self::VARCHAR) { + /** + * What to do if the current cell is of type VARCHAR + */ + /** + * The last cumulative type was VARCHAR + */ + if ($last_cumulative_type == self::VARCHAR) { + if ($curr_size >= $last_cumulative_size) { + return $curr_size; + } else { + return $last_cumulative_size; + } + } elseif ($last_cumulative_type == self::DECIMAL) { + /** + * The last cumulative type was DECIMAL + */ + $oldM = self::getDecimalPrecision($last_cumulative_size); + + if ($curr_size >= $oldM) { + return $curr_size; + } else { + return $oldM; + } + } elseif ($last_cumulative_type == self::BIGINT || $last_cumulative_type == self::INT) { + /** + * The last cumulative type was BIGINT or INT + */ + if ($curr_size >= $last_cumulative_size) { + return $curr_size; + } else { + return $last_cumulative_size; + } + } elseif (! isset($last_cumulative_type) || $last_cumulative_type == self::NONE) { + /** + * This is the first row to be analyzed + */ + return $curr_size; + } else { + /** + * An error has DEFINITELY occurred + */ + /** + * TODO: Handle this MUCH more elegantly + */ + + return -1; + } + } elseif ($curr_type == self::DECIMAL) { + /** + * What to do if the current cell is of type DECIMAL + */ + /** + * The last cumulative type was VARCHAR + */ + if ($last_cumulative_type == self::VARCHAR) { + /* Convert $last_cumulative_size from varchar to decimal format */ + $size = self::getDecimalSize($cell); + + if ($size[self::M] >= $last_cumulative_size) { + return $size[self::M]; + } else { + return $last_cumulative_size; + } + } elseif ($last_cumulative_type == self::DECIMAL) { + /** + * The last cumulative type was DECIMAL + */ + $size = self::getDecimalSize($cell); + + $oldM = self::getDecimalPrecision($last_cumulative_size); + $oldD = self::getDecimalScale($last_cumulative_size); + + /* New val if M or D is greater than current largest */ + if ($size[self::M] > $oldM || $size[self::D] > $oldD) { + /* Take the largest of both types */ + return (string) ((($size[self::M] > $oldM) ? $size[self::M] : $oldM) + . "," . (($size[self::D] > $oldD) ? $size[self::D] : $oldD)); + } else { + return $last_cumulative_size; + } + } elseif ($last_cumulative_type == self::BIGINT || $last_cumulative_type == self::INT) { + /** + * The last cumulative type was BIGINT or INT + */ + /* Convert $last_cumulative_size from int to decimal format */ + $size = self::getDecimalSize($cell); + + if ($size[self::M] >= $last_cumulative_size) { + return $size[self::FULL]; + } else { + return ($last_cumulative_size . "," . $size[self::D]); + } + } elseif (! isset($last_cumulative_type) || $last_cumulative_type == self::NONE) { + /** + * This is the first row to be analyzed + */ + /* First row of the column */ + $size = self::getDecimalSize($cell); + + return $size[self::FULL]; + } else { + /** + * An error has DEFINITELY occurred + */ + /** + * TODO: Handle this MUCH more elegantly + */ + + return -1; + } + } elseif ($curr_type == self::BIGINT || $curr_type == self::INT) { + /** + * What to do if the current cell is of type BIGINT or INT + */ + /** + * The last cumulative type was VARCHAR + */ + if ($last_cumulative_type == self::VARCHAR) { + if ($curr_size >= $last_cumulative_size) { + return $curr_size; + } else { + return $last_cumulative_size; + } + } elseif ($last_cumulative_type == self::DECIMAL) { + /** + * The last cumulative type was DECIMAL + */ + $oldM = self::getDecimalPrecision($last_cumulative_size); + $oldD = self::getDecimalScale($last_cumulative_size); + $oldInt = $oldM - $oldD; + $newInt = mb_strlen((string)$cell); + + /* See which has the larger integer length */ + if ($oldInt >= $newInt) { + /* Use old decimal size */ + return $last_cumulative_size; + } else { + /* Use $newInt + $oldD as new M */ + return (($newInt + $oldD) . "," . $oldD); + } + } elseif ($last_cumulative_type == self::BIGINT || $last_cumulative_type == self::INT) { + /** + * The last cumulative type was BIGINT or INT + */ + if ($curr_size >= $last_cumulative_size) { + return $curr_size; + } else { + return $last_cumulative_size; + } + } elseif (! isset($last_cumulative_type) || $last_cumulative_type == self::NONE) { + /** + * This is the first row to be analyzed + */ + return $curr_size; + } else { + /** + * An error has DEFINITELY occurred + */ + /** + * TODO: Handle this MUCH more elegantly + */ + + return -1; + } + } else { + /** + * An error has DEFINITELY occurred + */ + /** + * TODO: Handle this MUCH more elegantly + */ + + return -1; + } + } + + /** + * Determines what MySQL type a cell is + * + * @param int $last_cumulative_type Last cumulative column type + * (VARCHAR or INT or BIGINT or DECIMAL or NONE) + * @param string $cell String representation of the cell for which + * a best-fit type is to be determined + * + * @return int The MySQL type representation + * (VARCHAR or INT or BIGINT or DECIMAL or NONE) + * @access public + */ + public static function detectType($last_cumulative_type, $cell) + { + /** + * If numeric, determine if decimal, int or bigint + * Else, we call it varchar for simplicity + */ + + if (! strcmp('NULL', $cell)) { + if ($last_cumulative_type === null || $last_cumulative_type == self::NONE) { + return self::NONE; + } + + return $last_cumulative_type; + } + + if (!is_numeric($cell)) { + return self::VARCHAR; + } + + if ($cell == (string)(float)$cell + && mb_strpos($cell, ".") !== false + && mb_substr_count($cell, ".") == 1 + ) { + return self::DECIMAL; + } + + if (abs($cell) > 2147483647) { + return self::BIGINT; + } + + return self::INT; + } + + /** + * Determines if the column types are int, decimal, or string + * + * @param array &$table array(string $table_name, array $col_names, array $rows) + * + * @return array array(array $types, array $sizes) + * @access public + * + * @link https://wiki.phpmyadmin.net/pma/Import + * + * @todo Handle the error case more elegantly + */ + public static function analyzeTable(&$table) + { + /* Get number of rows in table */ + $numRows = count($table[self::ROWS]); + /* Get number of columns */ + $numCols = count($table[self::COL_NAMES]); + /* Current type for each column */ + $types = array(); + $sizes = array(); + + /* Initialize $sizes to all 0's */ + for ($i = 0; $i < $numCols; ++$i) { + $sizes[$i] = 0; + } + + /* Initialize $types to NONE */ + for ($i = 0; $i < $numCols; ++$i) { + $types[$i] = self::NONE; + } + + /* If the passed array is not of the correct form, do not process it */ + if (!is_array($table) + || is_array($table[self::TBL_NAME]) + || !is_array($table[self::COL_NAMES]) + || !is_array($table[self::ROWS]) + ) { + /** + * TODO: Handle this better + */ + + return false; + } + + /* Analyze each column */ + for ($i = 0; $i < $numCols; ++$i) { + /* 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]); + /* Determine size of the current cell */ + $sizes[$i] = self::detectSize( + $sizes[$i], + $types[$i], + $curr_type, + $table[self::ROWS][$j][$i] + ); + + /** + * If a type for this column has already been declared, + * only alter it if it was a number and a varchar was found + */ + if ($curr_type != self::NONE) { + if ($curr_type == self::VARCHAR) { + $types[$i] = self::VARCHAR; + } else if ($curr_type == self::DECIMAL) { + if ($types[$i] != self::VARCHAR) { + $types[$i] = self::DECIMAL; + } + } else if ($curr_type == self::BIGINT) { + if ($types[$i] != self::VARCHAR && $types[$i] != self::DECIMAL) { + $types[$i] = self::BIGINT; + } + } else if ($curr_type == self::INT) { + if ($types[$i] != self::VARCHAR + && $types[$i] != self::DECIMAL + && $types[$i] != self::BIGINT + ) { + $types[$i] = self::INT; + } + } + } + } + } + + /* Check to ensure that all types are valid */ + $len = count($types); + for ($n = 0; $n < $len; ++$n) { + if (! strcmp(self::NONE, $types[$n])) { + $types[$n] = self::VARCHAR; + $sizes[$n] = '10'; + } + } + + return array($types, $sizes); + } + + /** + * Builds and executes SQL statements to create the database and tables + * as necessary, as well as insert all the data. + * + * @param string $db_name Name of the database + * @param array &$tables Array of tables for the specified database + * @param array &$analyses Analyses of the tables + * @param array &$additional_sql Additional SQL statements to be executed + * @param array $options Associative array of options + * @param array &$sql_data 2-element array with sql data + * + * @return void + * @access public + * + * @link https://wiki.phpmyadmin.net/pma/Import + */ + public static function buildSql($db_name, &$tables, &$analyses = null, + &$additional_sql = null, $options = null, &$sql_data + ) { + /* Needed to quell the beast that is Message */ + $import_notice = null; + + /* Take care of the options */ + if (isset($options['db_collation'])&& ! is_null($options['db_collation'])) { + $collation = $options['db_collation']; + } else { + $collation = "utf8_general_ci"; + } + + if (isset($options['db_charset']) && ! is_null($options['db_charset'])) { + $charset = $options['db_charset']; + } else { + $charset = "utf8"; + } + + if (isset($options['create_db'])) { + $create_db = $options['create_db']; + } else { + $create_db = true; + } + + /* Create SQL code to handle the database */ + $sql = array(); + + if ($create_db) { + $sql[] = "CREATE DATABASE IF NOT EXISTS " . Util::backquote($db_name) + . " DEFAULT CHARACTER SET " . $charset . " COLLATE " . $collation + . ";"; + } + + /** + * The calling plug-in should include this statement, + * if necessary, in the $additional_sql parameter + * + * $sql[] = "USE " . backquote($db_name); + */ + + /* 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); + } + + /* No longer needed */ + unset($sql); + + /* Run the $additional_sql statements supplied by the caller plug-in */ + if ($additional_sql != null) { + /* Clean the SQL first */ + $additional_sql_len = count($additional_sql); + + /** + * Only match tables for now, because CREATE IF NOT EXISTS + * syntax is lacking or nonexisting for views, triggers, + * functions, and procedures. + * + * See: https://bugs.mysql.com/bug.php?id=15287 + * + * To the best of my knowledge this is still an issue. + * + * $pattern = 'CREATE (TABLE|VIEW|TRIGGER|FUNCTION|PROCEDURE)'; + */ + $pattern = '/CREATE [^`]*(TABLE)/'; + $replacement = 'CREATE \\1 IF NOT EXISTS'; + + /* Change CREATE statements to CREATE IF NOT EXISTS to support + * inserting into existing structures + */ + for ($i = 0; $i < $additional_sql_len; ++$i) { + $additional_sql[$i] = preg_replace( + $pattern, + $replacement, + $additional_sql[$i] + ); + /* Execute the resulting statements */ + self::runQuery($additional_sql[$i], $additional_sql[$i], $sql_data); + } + } + + if ($analyses != null) { + $type_array = array( + self::NONE => "NULL", + self::VARCHAR => "varchar", + self::INT => "int", + self::DECIMAL => "decimal", + self::BIGINT => "bigint", + self::GEOMETRY => 'geometry' + ); + + /* TODO: Do more checking here to make sure they really are matched */ + if (count($tables) != count($analyses)) { + exit(); + } + + /* Create SQL code to create the tables */ + $num_tables = count($tables); + for ($i = 0; $i < $num_tables; ++$i) { + $num_cols = count($tables[$i][self::COL_NAMES]); + $tempSQLStr = "CREATE TABLE IF NOT EXISTS " + . Util::backquote($db_name) + . '.' . Util::backquote($tables[$i][self::TBL_NAME]) . " ("; + for ($j = 0; $j < $num_cols; ++$j) { + $size = $analyses[$i][self::SIZES][$j]; + if ((int)$size == 0) { + $size = 10; + } + + $tempSQLStr .= Util::backquote( + $tables[$i][self::COL_NAMES][$j] + ) . " " + . $type_array[$analyses[$i][self::TYPES][$j]]; + if ($analyses[$i][self::TYPES][$j] != self::GEOMETRY) { + $tempSQLStr .= "(" . $size . ")"; + } + + if ($j != (count($tables[$i][self::COL_NAMES]) - 1)) { + $tempSQLStr .= ", "; + } + } + $tempSQLStr .= ") DEFAULT CHARACTER SET " . $charset + . " COLLATE " . $collation . ";"; + + /** + * Each SQL statement is executed immediately + * after it is formed so that we don't have + * to store them in a (possibly large) buffer + */ + self::runQuery($tempSQLStr, $tempSQLStr, $sql_data); + } + } + + /** + * Create the SQL statements to insert all the data + * + * Only one insert query is formed for each table + */ + $tempSQLStr = ""; + $col_count = 0; + $num_tables = count($tables); + for ($i = 0; $i < $num_tables; ++$i) { + $num_cols = count($tables[$i][self::COL_NAMES]); + $num_rows = count($tables[$i][self::ROWS]); + + $tempSQLStr = "INSERT INTO " . Util::backquote($db_name) . '.' + . Util::backquote($tables[$i][self::TBL_NAME]) . " ("; + + for ($m = 0; $m < $num_cols; ++$m) { + $tempSQLStr .= Util::backquote($tables[$i][self::COL_NAMES][$m]); + + if ($m != ($num_cols - 1)) { + $tempSQLStr .= ", "; + } + } + + $tempSQLStr .= ") VALUES "; + + for ($j = 0; $j < $num_rows; ++$j) { + $tempSQLStr .= "("; + + for ($k = 0; $k < $num_cols; ++$k) { + // If fully formatted SQL, no need to enclose + // with apostrophes, add slashes etc. + if ($analyses != null + && isset($analyses[$i][self::FORMATTEDSQL][$col_count]) + && $analyses[$i][self::FORMATTEDSQL][$col_count] == true + ) { + $tempSQLStr .= (string) $tables[$i][self::ROWS][$j][$k]; + } else { + if ($analyses != null) { + $is_varchar = ($analyses[$i][self::TYPES][$col_count] === self::VARCHAR); + } else { + $is_varchar = ! is_numeric($tables[$i][self::ROWS][$j][$k]); + } + + /* Don't put quotes around NULL fields */ + if (! strcmp($tables[$i][self::ROWS][$j][$k], 'NULL')) { + $is_varchar = false; + } + + $tempSQLStr .= (($is_varchar) ? "'" : ""); + $tempSQLStr .= $GLOBALS['dbi']->escapeString( + (string) $tables[$i][self::ROWS][$j][$k] + ); + $tempSQLStr .= (($is_varchar) ? "'" : ""); + } + + if ($k != ($num_cols - 1)) { + $tempSQLStr .= ", "; + } + + if ($col_count == ($num_cols - 1)) { + $col_count = 0; + } else { + $col_count++; + } + + /* Delete the cell after we are done with it */ + unset($tables[$i][self::ROWS][$j][$k]); + } + + $tempSQLStr .= ")"; + + if ($j != ($num_rows - 1)) { + $tempSQLStr .= ",\n "; + } + + $col_count = 0; + /* Delete the row after we are done with it */ + unset($tables[$i][self::ROWS][$j]); + } + + $tempSQLStr .= ";"; + + /** + * Each SQL statement is executed immediately + * after it is formed so that we don't have + * to store them in a (possibly large) buffer + */ + self::runQuery($tempSQLStr, $tempSQLStr, $sql_data); + } + + /* No longer needed */ + unset($tempSQLStr); + + /** + * A work in progress + */ + + /* Add the viewable structures from $additional_sql + * to $tables so they are also displayed + */ + $view_pattern = '@VIEW `[^`]+`\.`([^`]+)@'; + $table_pattern = '@CREATE TABLE IF NOT EXISTS `([^`]+)`@'; + /* Check a third pattern to make sure its not a "USE `db_name`;" statement */ + + $regs = array(); + + $inTables = false; + + $additional_sql_len = is_null($additional_sql) ? 0 : count($additional_sql); + for ($i = 0; $i < $additional_sql_len; ++$i) { + preg_match($view_pattern, $additional_sql[$i], $regs); + + if (count($regs) == 0) { + preg_match($table_pattern, $additional_sql[$i], $regs); + } + + if (count($regs)) { + for ($n = 0; $n < $num_tables; ++$n) { + if (! strcmp($regs[1], $tables[$n][self::TBL_NAME])) { + $inTables = true; + break; + } + } + + if (! $inTables) { + $tables[] = array(self::TBL_NAME => $regs[1]); + } + } + + /* Reset the array */ + $regs = array(); + $inTables = false; + } + + $params = array('db' => (string)$db_name); + $db_url = 'db_structure.php' . Url::getCommon($params); + $db_ops_url = 'db_operations.php' . Url::getCommon($params); + + $message = '

'; + $message .= '' . __( + 'The following structures have either been created or altered. Here you can:' + ) . '
'; + $message .= '
  • ' . __( + "View a structure's contents by clicking on its name." + ) . '
  • '; + $message .= '
  • ' . __( + 'Change any of its settings by clicking the corresponding "Options" link.' + ) . '
  • '; + $message .= '
  • ' . __('Edit structure by following the "Structure" link.') + . '
  • '; + $message .= sprintf( + '
  • %s (' + . __('Options') . ')
  • ', + $db_url, + sprintf( + __('Go to database: %s'), + htmlspecialchars(Util::backquote($db_name)) + ), + htmlspecialchars($db_name), + $db_ops_url, + sprintf( + __('Edit settings for %s'), + htmlspecialchars(Util::backquote($db_name)) + ) + ); + + $message .= '
      '; + + unset($params); + + $num_tables = count($tables); + for ($i = 0; $i < $num_tables; ++$i) { + $params = array( + 'db' => (string) $db_name, + 'table' => (string) $tables[$i][self::TBL_NAME] + ); + $tbl_url = 'sql.php' . Url::getCommon($params); + $tbl_struct_url = 'tbl_structure.php' . Url::getCommon($params); + $tbl_ops_url = 'tbl_operations.php' . Url::getCommon($params); + + unset($params); + + $_table = new Table($tables[$i][self::TBL_NAME], $db_name); + if (! $_table->isView()) { + $message .= sprintf( + '
    • %s (' . __( + 'Structure' + ) . ') (' . __('Options') . ')
    • ', + $tbl_url, + sprintf( + __('Go to table: %s'), + htmlspecialchars( + Util::backquote($tables[$i][self::TBL_NAME]) + ) + ), + htmlspecialchars($tables[$i][self::TBL_NAME]), + $tbl_struct_url, + sprintf( + __('Structure of %s'), + htmlspecialchars( + Util::backquote($tables[$i][self::TBL_NAME]) + ) + ), + $tbl_ops_url, + sprintf( + __('Edit settings for %s'), + htmlspecialchars( + Util::backquote($tables[$i][self::TBL_NAME]) + ) + ) + ); + } else { + $message .= sprintf( + '
    • %s
    • ', + $tbl_url, + sprintf( + __('Go to view: %s'), + htmlspecialchars( + Util::backquote($tables[$i][self::TBL_NAME]) + ) + ), + htmlspecialchars($tables[$i][self::TBL_NAME]) + ); + } + } + + $message .= '
'; + + global $import_notice; + $import_notice = $message; + + unset($tables); + } + + + /** + * Stops the import on (mostly upload/file related) error + * + * @param Message $error_message The error message + * + * @return void + * @access public + * + */ + public static function stop(Message $error_message) + { + global $import_handle, $file_to_unlink; + + // Close open handles + if ($import_handle !== false && $import_handle !== null) { + $import_handle->close(); + } + + // Delete temporary file + if ($file_to_unlink != '') { + unlink($file_to_unlink); + } + $msg = $error_message->getDisplay(); + $_SESSION['Import_message']['message'] = $msg; + + $response = Response::getInstance(); + $response->setRequestStatus(false); + $response->addJSON('message', Message::error($msg)); + + exit; + } + + /** + * Handles request for Simulation of UPDATE/DELETE queries. + * + * @return void + */ + public static function handleSimulateDmlRequest() + { + $response = Response::getInstance(); + $error = false; + $error_msg = __('Only single-table UPDATE and DELETE queries can be simulated.'); + $sql_delimiter = $_REQUEST['sql_delimiter']; + $sql_data = array(); + $queries = explode($sql_delimiter, $GLOBALS['sql_query']); + foreach ($queries as $sql_query) { + if (empty($sql_query)) { + continue; + } + + // Parsing the query. + $parser = new Parser($sql_query); + + if (empty($parser->statements[0])) { + continue; + } + + $statement = $parser->statements[0]; + + $analyzed_sql_results = array( + 'query' => $sql_query, + 'parser' => $parser, + 'statement' => $statement, + ); + + if ((!(($statement instanceof UpdateStatement) + || ($statement instanceof DeleteStatement))) + || (!empty($statement->join)) + ) { + $error = $error_msg; + break; + } + + $tables = Query::getTables($statement); + if (count($tables) > 1) { + $error = $error_msg; + break; + } + + // Get the matched rows for the query. + $result = self::getMatchedRows($analyzed_sql_results); + if (! $error = $GLOBALS['dbi']->getError()) { + $sql_data[] = $result; + } else { + break; + } + } + + if ($error) { + $message = Message::rawError($error); + $response->addJSON('message', $message); + $response->addJSON('sql_data', false); + } else { + $response->addJSON('sql_data', $sql_data); + } + } + + /** + * Find the matching rows for UPDATE/DELETE query. + * + * @param array $analyzed_sql_results Analyzed SQL results from parser. + * + * @return mixed + */ + public static function getMatchedRows($analyzed_sql_results = array()) + { + $statement = $analyzed_sql_results['statement']; + + $matched_row_query = ''; + if ($statement instanceof DeleteStatement) { + $matched_row_query = self::getSimulatedDeleteQuery($analyzed_sql_results); + } elseif ($statement instanceof UpdateStatement) { + $matched_row_query = self::getSimulatedUpdateQuery($analyzed_sql_results); + } + + // Execute the query and get the number of matched rows. + $matched_rows = self::executeMatchedRowQuery($matched_row_query); + + // URL to matched rows. + $_url_params = array( + 'db' => $GLOBALS['db'], + 'sql_query' => $matched_row_query + ); + $matched_rows_url = 'sql.php' . Url::getCommon($_url_params); + + return array( + 'sql_query' => Util::formatSql($analyzed_sql_results['query']), + 'matched_rows' => $matched_rows, + 'matched_rows_url' => $matched_rows_url + ); + } + + /** + * Transforms a UPDATE query into SELECT statement. + * + * @param array $analyzed_sql_results Analyzed SQL results from parser. + * + * @return string SQL query + */ + public static function getSimulatedUpdateQuery($analyzed_sql_results) + { + $table_references = Query::getTables( + $analyzed_sql_results['statement'] + ); + + $where = Query::getClause( + $analyzed_sql_results['statement'], + $analyzed_sql_results['parser']->list, + 'WHERE' + ); + + if (empty($where)) { + $where = '1'; + } + + $columns = array(); + $diff = array(); + foreach ($analyzed_sql_results['statement']->set as $set) { + $columns[] = $set->column; + $diff[] = $set->column . ' <> ' . $set->value; + } + if (!empty($diff)) { + $where .= ' AND (' . implode(' OR ', $diff) . ')'; + } + + $order_and_limit = ''; + + if (!empty($analyzed_sql_results['statement']->order)) { + $order_and_limit .= ' ORDER BY ' . Query::getClause( + $analyzed_sql_results['statement'], + $analyzed_sql_results['parser']->list, + 'ORDER BY' + ); + } + + if (!empty($analyzed_sql_results['statement']->limit)) { + $order_and_limit .= ' LIMIT ' . Query::getClause( + $analyzed_sql_results['statement'], + $analyzed_sql_results['parser']->list, + 'LIMIT' + ); + } + + return 'SELECT ' . implode(', ', $columns) . + ' FROM ' . implode(', ', $table_references) . + ' WHERE ' . $where . $order_and_limit; + } + + /** + * Transforms a DELETE query into SELECT statement. + * + * @param array $analyzed_sql_results Analyzed SQL results from parser. + * + * @return string SQL query + */ + public static function getSimulatedDeleteQuery($analyzed_sql_results) + { + $table_references = Query::getTables( + $analyzed_sql_results['statement'] + ); + + $where = Query::getClause( + $analyzed_sql_results['statement'], + $analyzed_sql_results['parser']->list, + 'WHERE' + ); + + if (empty($where)) { + $where = '1'; + } + + $order_and_limit = ''; + + if (!empty($analyzed_sql_results['statement']->order)) { + $order_and_limit .= ' ORDER BY ' . Query::getClause( + $analyzed_sql_results['statement'], + $analyzed_sql_results['parser']->list, + 'ORDER BY' + ); + } + + if (!empty($analyzed_sql_results['statement']->limit)) { + $order_and_limit .= ' LIMIT ' . Query::getClause( + $analyzed_sql_results['statement'], + $analyzed_sql_results['parser']->list, + 'LIMIT' + ); + } + + return 'SELECT * FROM ' . implode(', ', $table_references) . + ' WHERE ' . $where . $order_and_limit; + } + + /** + * Executes the matched_row_query and returns the resultant row count. + * + * @param string $matched_row_query SQL query + * + * @return integer Number of rows returned + */ + public static function executeMatchedRowQuery($matched_row_query) + { + $GLOBALS['dbi']->selectDb($GLOBALS['db']); + // Execute the query. + $result = $GLOBALS['dbi']->tryQuery($matched_row_query); + // Count the number of rows in the result set. + $result = $GLOBALS['dbi']->numRows($result); + + return $result; + } + + /** + * Handles request for ROLLBACK. + * + * @param string $sql_query SQL query(s) + * + * @return void + */ + public static function handleRollbackRequest($sql_query) + { + $sql_delimiter = $_REQUEST['sql_delimiter']; + $queries = explode($sql_delimiter, $sql_query); + $error = false; + $error_msg = __( + 'Only INSERT, UPDATE, DELETE and REPLACE ' + . 'SQL queries containing transactional engine tables can be rolled back.' + ); + foreach ($queries as $sql_query) { + if (empty($sql_query)) { + continue; + } + + // Check each query for ROLLBACK support. + if (! self::checkIfRollbackPossible($sql_query)) { + $global_error = $GLOBALS['dbi']->getError(); + if ($global_error) { + $error = $global_error; + } else { + $error = $error_msg; + } + break; + } + } + + if ($error) { + unset($_REQUEST['rollback_query']); + $response = Response::getInstance(); + $message = Message::rawError($error); + $response->addJSON('message', $message); + exit; + } else { + // If everything fine, START a transaction. + $GLOBALS['dbi']->query('START TRANSACTION'); + } + } + + /** + * Checks if ROLLBACK is possible for a SQL query or not. + * + * @param string $sql_query SQL query + * + * @return bool + */ + public static function checkIfRollbackPossible($sql_query) + { + $parser = new Parser($sql_query); + + if (empty($parser->statements[0])) { + return false; + } + + $statement = $parser->statements[0]; + + // Check if query is supported. + if (!(($statement instanceof InsertStatement) + || ($statement instanceof UpdateStatement) + || ($statement instanceof DeleteStatement) + || ($statement instanceof ReplaceStatement)) + ) { + return false; + } + + // Get table_references from the query. + $tables = Query::getTables($statement); + + // Check if each table is 'InnoDB'. + foreach ($tables as $table) { + if (! self::isTableTransactional($table)) { + return false; + } + } + + return true; + } + + /** + * Checks if a table is 'InnoDB' or not. + * + * @param string $table Table details + * + * @return bool + */ + public static function isTableTransactional($table) + { + $table = explode('.', $table); + if (count($table) == 2) { + $db = Util::unQuote($table[0]); + $table = Util::unQuote($table[1]); + } else { + $db = $GLOBALS['db']; + $table = Util::unQuote($table[0]); + } + + // Query to check if table exists. + $check_table_query = 'SELECT * FROM ' . Util::backquote($db) + . '.' . Util::backquote($table) . ' ' + . 'LIMIT 1'; + + $result = $GLOBALS['dbi']->tryQuery($check_table_query); + + if (! $result) { + return false; + } + + // List of Transactional Engines. + $transactional_engines = array( + 'INNODB', + 'FALCON', + 'NDB', + 'INFINIDB', + 'TOKUDB', + 'XTRADB', + 'SEQUENCE', + 'BDB' + ); + + // Query to check if table is 'Transactional'. + $check_query = 'SELECT `ENGINE` FROM `information_schema`.`tables` ' + . 'WHERE `table_name` = "' . $table . '" ' + . 'AND `table_schema` = "' . $db . '" ' + . 'AND UPPER(`engine`) IN ("' + . implode('", "', $transactional_engines) + . '")'; + + $result = $GLOBALS['dbi']->tryQuery($check_query); + + if ($GLOBALS['dbi']->numRows($result) == 1) { + return true; + } else { + return false; + } + } +} diff --git a/libraries/classes/Plugins/Import/ImportCsv.php b/libraries/classes/Plugins/Import/ImportCsv.php index fbd830b664..492b90190e 100644 --- a/libraries/classes/Plugins/Import/ImportCsv.php +++ b/libraries/classes/Plugins/Import/ImportCsv.php @@ -9,6 +9,7 @@ */ namespace PhpMyAdmin\Plugins\Import; +use PhpMyAdmin\Import; use PhpMyAdmin\Message; use PhpMyAdmin\Properties\Options\Items\BoolPropertyItem; use PhpMyAdmin\Properties\Options\Items\TextPropertyItem; @@ -241,7 +242,7 @@ class ImportCsv extends AbstractImportCsv $max_cols = 0; $csv_terminated_len = mb_strlen($csv_terminated); while (!($finished && $i >= $len) && !$error && !$timeout_passed) { - $data = PMA_importGetNextChunk(); + $data = Import::getNextChunk(); if ($data === false) { // subtract data we didn't handle yet and stop processing $GLOBALS['offset'] -= strlen($buffer); @@ -572,7 +573,7 @@ class ImportCsv extends AbstractImportCsv * @todo maybe we could add original line to verbose * SQL in comment */ - PMA_importRunQuery($sql, $sql, $sql_data); + Import::runQuery($sql, $sql, $sql_data); } $line++; @@ -625,7 +626,7 @@ class ImportCsv extends AbstractImportCsv /* Obtain the best-fit MySQL types for each column */ $analyses = array(); - $analyses[] = PMA_analyzeTable($tables[0]); + $analyses[] = Import::analyzeTable($tables[0]); /** * string $db_name (no backquotes) @@ -648,14 +649,14 @@ class ImportCsv extends AbstractImportCsv $create = null; /* Created and execute necessary SQL statements from data */ - PMA_buildSQL($db_name, $tables, $analyses, $create, $options, $sql_data); + Import::buildSql($db_name, $tables, $analyses, $create, $options, $sql_data); unset($tables); unset($analyses); } // Commit any possible data in buffers - PMA_importRunQuery('', '', $sql_data); + 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 77e79bafd8..157f7bde24 100644 --- a/libraries/classes/Plugins/Import/ImportLdi.php +++ b/libraries/classes/Plugins/Import/ImportLdi.php @@ -8,6 +8,7 @@ */ namespace PhpMyAdmin\Plugins\Import; +use PhpMyAdmin\Import; use PhpMyAdmin\Message; use PhpMyAdmin\Plugins\Import\AbstractImportCsv; use PhpMyAdmin\Properties\Options\Items\BoolPropertyItem; @@ -169,8 +170,8 @@ class ImportLdi extends AbstractImportCsv $sql .= ')'; } - PMA_importRunQuery($sql, $sql, $sql_data); - PMA_importRunQuery('', '', $sql_data); + Import::runQuery($sql, $sql, $sql_data); + Import::runQuery('', '', $sql_data); $finished = true; } } diff --git a/libraries/classes/Plugins/Import/ImportMediawiki.php b/libraries/classes/Plugins/Import/ImportMediawiki.php index ccf64ee48f..943d0c1cdd 100644 --- a/libraries/classes/Plugins/Import/ImportMediawiki.php +++ b/libraries/classes/Plugins/Import/ImportMediawiki.php @@ -8,6 +8,7 @@ */ namespace PhpMyAdmin\Plugins\Import; +use PhpMyAdmin\Import; use PhpMyAdmin\Message; use PhpMyAdmin\Plugins\ImportPlugin; use PhpMyAdmin\Properties\Plugins\ImportPluginProperties; @@ -92,7 +93,7 @@ class ImportMediawiki extends ImportPlugin $cur_table_name = ""; while (!$finished && !$error && !$timeout_passed) { - $data = PMA_importGetNextChunk(); + $data = Import::getNextChunk(); if ($data === false) { // Subtract data we didn't handle yet and stop processing @@ -307,19 +308,19 @@ class ImportMediawiki extends ImportPlugin // Set generic names for table headers if they don't exist $this->_setTableHeaders($table[1], $table[2][0]); - // Create the tables array to be used in PMA_buildSQL() + // Create the tables array to be used in Import::buildSql() $tables = array(); $tables [] = array($table[0], $table[1], $table[2]); // Obtain the best-fit MySQL types for each column $analyses = array(); - $analyses [] = PMA_analyzeTable($tables[0]); + $analyses [] = Import::analyzeTable($tables[0]); $this->_executeImportTables($tables, $analyses, $sql_data); } // Commit any possible data in buffers - PMA_importRunQuery('', '', $sql_data); + Import::runQuery('', '', $sql_data); } /** @@ -360,7 +361,7 @@ class ImportMediawiki extends ImportPlugin } /** - * Sets the database name and additional options and calls PMA_buildSQL() + * Sets the database name and additional options and calls Import::buildSql() * Used in PMA_importDataAllTables() and $this->_importDataOneTable() * * @param array &$tables structure: @@ -392,7 +393,7 @@ class ImportMediawiki extends ImportPlugin $create = null; // Create and execute necessary SQL statements from data - PMA_buildSQL($db_name, $tables, $analyses, $create, $options, $sql_data); + 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 e6e378a772..f0c627613e 100644 --- a/libraries/classes/Plugins/Import/ImportOds.php +++ b/libraries/classes/Plugins/Import/ImportOds.php @@ -10,6 +10,7 @@ */ namespace PhpMyAdmin\Plugins\Import; +use PhpMyAdmin\Import; use PhpMyAdmin\Message; use PhpMyAdmin\Plugins\ImportPlugin; use PhpMyAdmin\Properties\Plugins\ImportPluginProperties; @@ -108,11 +109,11 @@ class ImportOds extends ImportPlugin $buffer = ""; /** - * Read in the file via PMA_importGetNextChunk so that + * Read in the file via Import::getNextChunk so that * it can process compressed files */ while (!($finished && $i >= $len) && !$error && !$timeout_passed) { - $data = PMA_importGetNextChunk(); + $data = Import::getNextChunk(); if ($data === false) { /* subtract data we didn't handle yet and stop processing */ $GLOBALS['offset'] -= strlen($buffer); @@ -235,7 +236,7 @@ class ImportOds extends ImportPlugin } } else { for ($i = 0; $i < $num_null; ++$i) { - $col_names[] = PMA_getColumnAlphaName( + $col_names[] = Import::getColumnAlphaName( $col_count + 1 ); ++$col_count; @@ -245,7 +246,7 @@ class ImportOds extends ImportPlugin if (!$col_names_in_first_row) { $tempRow[] = 'NULL'; } else { - $col_names[] = PMA_getColumnAlphaName( + $col_names[] = Import::getColumnAlphaName( $col_count + 1 ); } @@ -294,7 +295,7 @@ class ImportOds extends ImportPlugin /* Fill out column names */ for ($i = count($col_names); $i < $max_cols; ++$i) { - $col_names[] = PMA_getColumnAlphaName($i + 1); + $col_names[] = Import::getColumnAlphaName($i + 1); } /* Fill out all rows */ @@ -329,15 +330,15 @@ class ImportOds extends ImportPlugin for ($i = 0; $i < $num_tables; ++$i) { $num_rows = count($rows); for ($j = 0; $j < $num_rows; ++$j) { - if (strcmp($tables[$i][TBL_NAME], $rows[$j][TBL_NAME])) { + if (strcmp($tables[$i][Import::TBL_NAME], $rows[$j][Import::TBL_NAME])) { continue; } - if (!isset($tables[$i][COL_NAMES])) { - $tables[$i][] = $rows[$j][COL_NAMES]; + if (!isset($tables[$i][Import::COL_NAMES])) { + $tables[$i][] = $rows[$j][Import::COL_NAMES]; } - $tables[$i][ROWS] = $rows[$j][ROWS]; + $tables[$i][Import::ROWS] = $rows[$j][Import::ROWS]; } } @@ -349,7 +350,7 @@ class ImportOds extends ImportPlugin $len = count($tables); for ($i = 0; $i < $len; ++$i) { - $analyses[] = PMA_analyzeTable($tables[$i]); + $analyses[] = Import::analyzeTable($tables[$i]); } /** @@ -373,13 +374,13 @@ class ImportOds extends ImportPlugin $create = null; /* Created and execute necessary SQL statements from data */ - PMA_buildSQL($db_name, $tables, $analyses, $create, $options, $sql_data); + Import::buildSql($db_name, $tables, $analyses, $create, $options, $sql_data); unset($tables); unset($analyses); /* Commit any possible data in buffers */ - PMA_importRunQuery('', '', $sql_data); + Import::runQuery('', '', $sql_data); } /** diff --git a/libraries/classes/Plugins/Import/ImportShp.php b/libraries/classes/Plugins/Import/ImportShp.php index 67c719753d..9cfcee6345 100644 --- a/libraries/classes/Plugins/Import/ImportShp.php +++ b/libraries/classes/Plugins/Import/ImportShp.php @@ -13,6 +13,7 @@ use PhpMyAdmin\Gis\GisMultiLineString; use PhpMyAdmin\Gis\GisMultiPoint; use PhpMyAdmin\Gis\GisPoint; use PhpMyAdmin\Gis\GisPolygon; +use PhpMyAdmin\Import; use PhpMyAdmin\Message; use PhpMyAdmin\Plugins\ImportPlugin; use PhpMyAdmin\Properties\Plugins\ImportPluginProperties; @@ -250,12 +251,12 @@ class ImportShp extends ImportPlugin // Use data from shape file to chose best-fit MySQL types for each column $analyses = array(); - $analyses[] = PMA_analyzeTable($tables[0]); + $analyses[] = Import::analyzeTable($tables[0]); $table_no = 0; $spatial_col = 0; - $analyses[$table_no][TYPES][$spatial_col] = GEOMETRY; - $analyses[$table_no][FORMATTEDSQL][$spatial_col] = true; + $analyses[$table_no][Import::TYPES][$spatial_col] = Import::GEOMETRY; + $analyses[$table_no][Import::FORMATTEDSQL][$spatial_col] = true; // Set database name to the currently selected one, if applicable if (strlen($db) > 0) { @@ -268,7 +269,7 @@ class ImportShp extends ImportPlugin // Created and execute necessary SQL statements from data $null_param = null; - PMA_buildSQL($db_name, $tables, $analyses, $null_param, $options, $sql_data); + Import::buildSql($db_name, $tables, $analyses, $null_param, $options, $sql_data); unset($tables); unset($analyses); @@ -277,7 +278,7 @@ class ImportShp extends ImportPlugin $error = false; // Commit any possible data in buffers - PMA_importRunQuery('', '', $sql_data); + Import::runQuery('', '', $sql_data); } /** @@ -298,7 +299,7 @@ class ImportShp extends ImportPlugin if ($GLOBALS['finished']) { $eof = true; } else { - $buffer .= PMA_importGetNextChunk(); + $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 db92dd37ec..8a20650a89 100644 --- a/libraries/classes/Plugins/Import/ImportSql.php +++ b/libraries/classes/Plugins/Import/ImportSql.php @@ -8,6 +8,7 @@ */ namespace PhpMyAdmin\Plugins\Import; +use PhpMyAdmin\Import; use PhpMyAdmin\Plugins\ImportPlugin; use PhpMyAdmin\Properties\Plugins\ImportPluginProperties; use PhpMyAdmin\Properties\Options\Groups\OptionsPropertyMainGroup; @@ -116,7 +117,7 @@ class ImportSql extends ImportPlugin } /** - * Will be set in PMA_importGetNextChunk(). + * Will be set in Import::getNextChunk(). * * @global bool $GLOBALS ['finished'] */ @@ -132,7 +133,7 @@ class ImportSql extends ImportPlugin if (empty($statement)) { // Importing new data. - $newData = PMA_importGetNextChunk(); + $newData = Import::getNextChunk(); // Subtract data we didn't handle yet and stop processing. if ($newData === false) { @@ -154,19 +155,19 @@ class ImportSql extends ImportPlugin } // Executing the query. - PMA_importRunQuery($statement, $statement, $sql_data); + Import::runQuery($statement, $statement, $sql_data); } // Extracting remaining statements. while ((!$error) && (!$timeout_passed) && (!empty($bq->query))) { $statement = $bq->extract(true); if (!empty($statement)) { - PMA_importRunQuery($statement, $statement, $sql_data); + Import::runQuery($statement, $statement, $sql_data); } } // Finishing. - PMA_importRunQuery('', '', $sql_data); + Import::runQuery('', '', $sql_data); } /** diff --git a/libraries/classes/Plugins/Import/ImportXml.php b/libraries/classes/Plugins/Import/ImportXml.php index 5c04a43bf1..83a75b3b6f 100644 --- a/libraries/classes/Plugins/Import/ImportXml.php +++ b/libraries/classes/Plugins/Import/ImportXml.php @@ -10,6 +10,7 @@ namespace PhpMyAdmin\Plugins\Import; +use PhpMyAdmin\Import; use PhpMyAdmin\Message; use PhpMyAdmin\Plugins\ImportPlugin; use PhpMyAdmin\Properties\Plugins\ImportPluginProperties; @@ -66,11 +67,11 @@ class ImportXml extends ImportPlugin $buffer = ""; /** - * Read in the file via PMA_importGetNextChunk so that + * Read in the file via Import::getNextChunk so that * it can process compressed files */ while (!($finished && $i >= $len) && !$error && !$timeout_passed) { - $data = PMA_importGetNextChunk(); + $data = Import::getNextChunk(); if ($data === false) { /* subtract data we didn't handle yet and stop processing */ $GLOBALS['offset'] -= strlen($buffer); @@ -249,7 +250,7 @@ class ImportXml extends ImportPlugin $isInTables = false; $num_tables = count($tables); for ($i = 0; $i < $num_tables; ++$i) { - if (!strcmp($tables[$i][TBL_NAME], (string)$tbl_attr['name'])) { + if (!strcmp($tables[$i][Import::TBL_NAME], (string)$tbl_attr['name'])) { $isInTables = true; break; } @@ -284,12 +285,12 @@ class ImportXml extends ImportPlugin for ($i = 0; $i < $num_tables; ++$i) { $num_rows = count($rows); for ($j = 0; $j < $num_rows; ++$j) { - if (!strcmp($tables[$i][TBL_NAME], $rows[$j][TBL_NAME])) { - if (!isset($tables[$i][COL_NAMES])) { - $tables[$i][] = $rows[$j][COL_NAMES]; + if (!strcmp($tables[$i][Import::TBL_NAME], $rows[$j][Import::TBL_NAME])) { + if (!isset($tables[$i][Import::COL_NAMES])) { + $tables[$i][] = $rows[$j][Import::COL_NAMES]; } - $tables[$i][ROWS][] = $rows[$j][ROWS]; + $tables[$i][Import::ROWS][] = $rows[$j][Import::ROWS]; } } } @@ -301,7 +302,7 @@ class ImportXml extends ImportPlugin $len = count($tables); for ($i = 0; $i < $len; ++$i) { - $analyses[] = PMA_analyzeTable($tables[$i]); + $analyses[] = Import::analyzeTable($tables[$i]); } } } @@ -316,7 +317,7 @@ class ImportXml extends ImportPlugin if ($data_present) { /** * Set values to NULL if they were not present - * to maintain PMA_buildSQL() call integrity + * to maintain Import::buildSql() call integrity */ if (!isset($analyses)) { $analyses = null; @@ -358,13 +359,13 @@ class ImportXml extends ImportPlugin } /* Created and execute necessary SQL statements from data */ - PMA_buildSQL($db_name, $tables, $analyses, $create, $options, $sql_data); + Import::buildSql($db_name, $tables, $analyses, $create, $options, $sql_data); unset($analyses); unset($tables); unset($create); /* Commit any possible data in buffers */ - PMA_importRunQuery('', '', $sql_data); + Import::runQuery('', '', $sql_data); } } diff --git a/libraries/classes/Plugins/Import/README b/libraries/classes/Plugins/Import/README index 1064040c53..20b856eeb6 100644 --- a/libraries/classes/Plugins/Import/README +++ b/libraries/classes/Plugins/Import/README @@ -12,6 +12,7 @@ gettext mechanism, see https://wiki.phpmyadmin.net/pma/Gettext_for_developers. */ namespace PhpMyAdmin\Plugins\Import; +use PhpMyAdmin\Import; use PhpMyAdmin\Plugins\ImportPlugin; /** @@ -104,7 +105,7 @@ class Import[Name] extends ImportPlugin $buffer = ''; while (! ($finished && $i >= $len) && ! $error && ! $timeout_passed) { - $data = PMA_importGetNextChunk(); + $data = Import::getNextChunk(); if ($data === false) { // subtract data we didn't handle yet and stop processing $GLOBALS['offset'] -= strlen($buffer); @@ -116,10 +117,10 @@ class Import[Name] extends ImportPlugin $buffer .= $data; } // PARSE $buffer here, post sql queries using: - PMA_importRunQuery($sql, $verbose_sql_with_comments, $sql_data); + Import::runQuery($sql, $verbose_sql_with_comments, $sql_data); } // End of import loop // Commit any possible data in buffers - PMA_importRunQuery('', '', $sql_data); + Import::runQuery('', '', $sql_data); } diff --git a/libraries/import.lib.php b/libraries/import.lib.php deleted file mode 100644 index dc58a9fb6b..0000000000 --- a/libraries/import.lib.php +++ /dev/null @@ -1,1690 +0,0 @@ - ($maximum_time - 5)) { - $timeout_passed = true; - return true; - } else { - return false; - } -} - -/** - * Runs query inside import buffer. This is needed to allow displaying - * of last SELECT, SHOW or HANDLER results and similar nice stuff. - * - * @param string $sql query to run - * @param string $full query to display, this might be commented - * @param array &$sql_data SQL parse data storage - * - * @return void - * @access public - */ -function PMA_executeQuery($sql, $full, &$sql_data) -{ - global $go_sql, - $sql_query, $my_die, $error, $reload, - $result, $msg, - $cfg, $sql_query_disabled, $db; - - $result = $GLOBALS['dbi']->tryQuery($sql); - - // USE query changes the database, son need to track - // while running multiple queries - $is_use_query = mb_stripos($sql, "use ") !== false; - - $msg = '# '; - if ($result === false) { // execution failed - if (! isset($my_die)) { - $my_die = array(); - } - $my_die[] = array( - 'sql' => $full, - 'error' => $GLOBALS['dbi']->getError() - ); - - $msg .= __('Error'); - - if (! $cfg['IgnoreMultiSubmitErrors']) { - $error = true; - return; - } - } else { - $a_num_rows = (int)@$GLOBALS['dbi']->numRows($result); - $a_aff_rows = (int)@$GLOBALS['dbi']->affectedRows(); - if ($a_num_rows > 0) { - $msg .= __('Rows') . ': ' . $a_num_rows; - } elseif ($a_aff_rows > 0) { - $message = Message::getMessageForAffectedRows( - $a_aff_rows - ); - $msg .= $message->getMessage(); - } else { - $msg .= __( - 'MySQL returned an empty result set (i.e. zero ' - . 'rows).' - ); - } - - if (($a_num_rows > 0) || $is_use_query) { - $sql_data['valid_sql'][] = $sql; - if (!isset($sql_data['valid_queries'])) { - $sql_data['valid_queries'] = 0; - } - $sql_data['valid_queries']++; - } - } - if (! $sql_query_disabled) { - $sql_query .= $msg . "\n"; - } - - // 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) = PMA_lookForUse( - $sql, - $db, - $reload - ); - } - - $pattern = '@^[\s]*(DROP|CREATE)[\s]+(IF EXISTS[[:space:]]+)' - . '?(TABLE|DATABASE)[[:space:]]+(.+)@im'; - if ($result != false - && preg_match($pattern, $sql) - ) { - $reload = true; - } -} - -/** - * Runs query inside import buffer. This is needed to allow displaying - * of last SELECT, SHOW or HANDLER results and similar nice stuff. - * - * @param string $sql query to run - * @param string $full query to display, this might be commented - * @param array &$sql_data SQL parse data storage - * - * @return void - * @access public - */ -function PMA_importRunQuery($sql = '', $full = '', &$sql_data = array()) -{ - global $import_run_buffer, $go_sql, $complete_query, $display_query, - $sql_query, $error, $reload, $result, $msg, - $skip_queries, $executed_queries, $max_sql_len, $read_multiply, - $cfg, $sql_query_disabled, $db, $run_query, $is_superuser; - $read_multiply = 1; - if (!isset($import_run_buffer)) { - // Do we have something to push into buffer? - $import_run_buffer = PMA_ImportRunQuery_post( - $import_run_buffer, $sql, $full - ); - return; - } - - // Should we skip something? - if ($skip_queries > 0) { - $skip_queries--; - // Do we have something to push into buffer? - $import_run_buffer = PMA_ImportRunQuery_post( - $import_run_buffer, $sql, $full - ); - return; - } - - if (! empty($import_run_buffer['sql']) - && trim($import_run_buffer['sql']) != '' - ) { - $max_sql_len = max( - $max_sql_len, - mb_strlen($import_run_buffer['sql']) - ); - if (! $sql_query_disabled) { - $sql_query .= $import_run_buffer['full']; - } - - $executed_queries++; - - if ($run_query && $executed_queries < 50) { - $go_sql = true; - - if (! $sql_query_disabled) { - $complete_query = $sql_query; - $display_query = $sql_query; - } else { - $complete_query = ''; - $display_query = ''; - } - $sql_query = $import_run_buffer['sql']; - $sql_data['valid_sql'][] = $import_run_buffer['sql']; - $sql_data['valid_full'][] = $import_run_buffer['full']; - if (! isset($sql_data['valid_queries'])) { - $sql_data['valid_queries'] = 0; - } - $sql_data['valid_queries']++; - } elseif ($run_query) { - - /* Handle rollback from go_sql */ - if ($go_sql && isset($sql_data['valid_full'])) { - $queries = $sql_data['valid_sql']; - $fulls = $sql_data['valid_full']; - $count = $sql_data['valid_queries']; - $go_sql = false; - - $sql_data['valid_sql'] = array(); - $sql_data['valid_queries'] = 0; - unset($sql_data['valid_full']); - for ($i = 0; $i < $count; $i++) { - PMA_executeQuery( - $queries[$i], - $fulls[$i], - $sql_data - ); - } - } - - PMA_executeQuery( - $import_run_buffer['sql'], - $import_run_buffer['full'], - $sql_data - ); - } // end run query - // end non empty query - } elseif (! empty($import_run_buffer['full'])) { - if ($go_sql) { - $complete_query .= $import_run_buffer['full']; - $display_query .= $import_run_buffer['full']; - } else { - if (! $sql_query_disabled) { - $sql_query .= $import_run_buffer['full']; - } - } - } - // check length of query unless we decided to pass it to sql.php - // (if $run_query is false, we are just displaying so show - // the complete query in the textarea) - if (! $go_sql && $run_query) { - if (! empty($sql_query)) { - if (mb_strlen($sql_query) > 50000 - || $executed_queries > 50 - || $max_sql_len > 1000 - ) { - $sql_query = ''; - $sql_query_disabled = true; - } - } - } - - // Do we have something to push into buffer? - $import_run_buffer = PMA_ImportRunQuery_post($import_run_buffer, $sql, $full); - - // In case of ROLLBACK, notify the user. - if (isset($_REQUEST['rollback_query'])) { - $msg .= __('[ROLLBACK occurred.]'); - } -} - -/** - * Return import run buffer - * - * @param array $import_run_buffer Buffer of queries for import - * @param string $sql SQL query - * @param string $full Query to display - * - * @return array Buffer of queries for import - */ -function PMA_ImportRunQuery_post($import_run_buffer, $sql, $full) -{ - if (!empty($sql) || !empty($full)) { - $import_run_buffer = array('sql' => $sql, 'full' => $full); - return $import_run_buffer; - } else { - unset($GLOBALS['import_run_buffer']); - return $import_run_buffer; - } -} - -/** - * Looks for the presence of USE to possibly change current db - * - * @param string $buffer buffer to examine - * @param string $db current db - * @param bool $reload reload - * - * @return array (current or new db, whether to reload) - * @access public - */ -function PMA_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 PMA_buildSQL() as: backquote($db_name), and then called - // in PMA_importRunQuery() which in turn calls PMA_lookForUse() ) - $db = PhpMyAdmin\Util::unQuote($db); - - $reload = true; - } - return(array($db, $reload)); -} - - -/** - * Returns next part of imported file/buffer - * - * @param int $size size of buffer to read - * (this is maximal size function will return) - * - * @return string part of file/buffer - * @access public - */ -function PMA_importGetNextChunk($size = 32768) -{ - global $compression, $import_handle, $charset_conversion, $charset_of_file, - $read_multiply; - - // Add some progression while reading large amount of data - if ($read_multiply <= 8) { - $size *= $read_multiply; - } else { - $size *= 8; - } - $read_multiply++; - - // We can not read too much - if ($size > $GLOBALS['read_limit']) { - $size = $GLOBALS['read_limit']; - } - - if (PMA_checkTimeout()) { - return false; - } - if ($GLOBALS['finished']) { - return true; - } - - if ($GLOBALS['import_file'] == 'none') { - // Well this is not yet supported and tested, - // but should return content of textarea - if (mb_strlen($GLOBALS['import_text']) < $size) { - $GLOBALS['finished'] = true; - return $GLOBALS['import_text']; - } else { - $r = mb_substr($GLOBALS['import_text'], 0, $size); - $GLOBALS['offset'] += $size; - $GLOBALS['import_text'] = mb_substr($GLOBALS['import_text'], $size); - return $r; - } - } - - $result = $import_handle->read($size); - $GLOBALS['finished'] = $import_handle->eof(); - $GLOBALS['offset'] += $size; - - if ($charset_conversion) { - return Encoding::convertString($charset_of_file, 'utf-8', $result); - } - - /** - * Skip possible byte order marks (I do not think we need more - * charsets, but feel free to add more, you can use wikipedia for - * reference: ) - * - * @todo BOM could be used for charset autodetection - */ - if ($GLOBALS['offset'] == $size) { - // UTF-8 - if (strncmp($result, "\xEF\xBB\xBF", 3) == 0) { - $result = mb_substr($result, 3); - // UTF-16 BE, LE - } elseif (strncmp($result, "\xFE\xFF", 2) == 0 - || strncmp($result, "\xFF\xFE", 2) == 0 - ) { - $result = mb_substr($result, 2); - } - } - return $result; -} - -/** - * Returns the "Excel" column name (i.e. 1 = "A", 26 = "Z", 27 = "AA", etc.) - * - * This functions uses recursion to build the Excel column name. - * - * The column number (1-26) is converted to the responding - * ASCII character (A-Z) and returned. - * - * If the column number is bigger than 26 (= num of letters in alphabet), - * an extra character needs to be added. To find this extra character, - * the number is divided by 26 and this value is passed to another instance - * of the same function (hence recursion). In that new instance the number is - * evaluated again, and if it is still bigger than 26, it is divided again - * and passed to another instance of the same function. This continues until - * the number is smaller than 26. Then the last called function returns - * the corresponding ASCII character to the function that called it. - * Each time a called function ends an extra character is added to the column name. - * When the first function is reached, the last character is added and the complete - * column name is returned. - * - * @param int $num the column number - * - * @return string The column's "Excel" name - * @access public - */ -function PMA_getColumnAlphaName($num) -{ - $A = 65; // ASCII value for capital "A" - $col_name = ""; - - if ($num > 26) { - $div = (int)($num / 26); - $remain = (int)($num % 26); - - // subtract 1 of divided value in case the modulus is 0, - // this is necessary because A-Z has no 'zero' - if ($remain == 0) { - $div--; - } - - // recursive function call - $col_name = PMA_getColumnAlphaName($div); - // use modulus as new column number - $num = $remain; - } - - if ($num == 0) { - // use 'Z' if column number is 0, - // this is necessary because A-Z has no 'zero' - $col_name .= mb_chr(($A + 26) - 1); - } else { - // convert column number to ASCII character - $col_name .= mb_chr(($A + $num) - 1); - } - - return $col_name; -} - -/** - * Returns the column number based on the Excel name. - * So "A" = 1, "Z" = 26, "AA" = 27, etc. - * - * Basically this is a base26 (A-Z) to base10 (0-9) conversion. - * It iterates through all characters in the column name and - * calculates the corresponding value, based on character value - * (A = 1, ..., Z = 26) and position in the string. - * - * @param string $name column name(i.e. "A", or "BC", etc.) - * - * @return int The column number - * @access public - */ -function PMA_getColumnNumberFromName($name) -{ - if (empty($name)) { - return 0; - } - - $name = mb_strtoupper($name); - $num_chars = mb_strlen($name); - $column_number = 0; - for ($i = 0; $i < $num_chars; ++$i) { - // read string from back to front - $char_pos = ($num_chars - 1) - $i; - - // convert capital character to ASCII value - // and subtract 64 to get corresponding decimal value - // ASCII value of "A" is 65, "B" is 66, etc. - // Decimal equivalent of "A" is 1, "B" is 2, etc. - $number = (int)(mb_ord($name[$char_pos]) - 64); - - // base26 to base10 conversion : multiply each number - // with corresponding value of the position, in this case - // $i=0 : 1; $i=1 : 26; $i=2 : 676; ... - $column_number += $number * pow(26, $i); - } - return $column_number; -} - -/** - * Constants definitions - */ - -/* MySQL type defs */ -define("NONE", 0); -define("VARCHAR", 1); -define("INT", 2); -define("DECIMAL", 3); -define("BIGINT", 4); -define("GEOMETRY", 5); - -/* Decimal size defs */ -define("M", 0); -define("D", 1); -define("FULL", 2); - -/* Table array defs */ -define("TBL_NAME", 0); -define("COL_NAMES", 1); -define("ROWS", 2); - -/* Analysis array defs */ -define("TYPES", 0); -define("SIZES", 1); -define("FORMATTEDSQL", 2); - -/** - * Obtains the precision (total # of digits) from a size of type decimal - * - * @param string $last_cumulative_size Size of type decimal - * - * @return int Precision of the given decimal size notation - * @access public - */ -function PMA_getDecimalPrecision($last_cumulative_size) -{ - return (int)substr( - $last_cumulative_size, - 0, - strpos($last_cumulative_size, ",") - ); -} - -/** - * Obtains the scale (# of digits to the right of the decimal point) - * from a size of type decimal - * - * @param string $last_cumulative_size Size of type decimal - * - * @return int Scale of the given decimal size notation - * @access public - */ -function PMA_getDecimalScale($last_cumulative_size) -{ - return (int)substr( - $last_cumulative_size, - (strpos($last_cumulative_size, ",") + 1), - (strlen($last_cumulative_size) - strpos($last_cumulative_size, ",")) - ); -} - -/** - * Obtains the decimal size of a given cell - * - * @param string $cell cell content - * - * @return array Contains the precision, scale, and full size - * representation of the given decimal cell - * @access public - */ -function PMA_getDecimalSize($cell) -{ - $curr_size = mb_strlen((string)$cell); - $decPos = mb_strpos($cell, "."); - $decPrecision = ($curr_size - 1) - $decPos; - - $m = $curr_size - 1; - $d = $decPrecision; - - return array($m, $d, ($m . "," . $d)); -} - -/** - * Obtains the size of the given cell - * - * @param string $last_cumulative_size Last cumulative column size - * @param int $last_cumulative_type Last cumulative column type - * (NONE or VARCHAR or DECIMAL or INT or BIGINT) - * @param int $curr_type Type of the current cell - * (NONE or VARCHAR or DECIMAL or INT or BIGINT) - * @param string $cell The current cell - * - * @return string Size of the given cell in the type-appropriate format - * @access public - * - * @todo Handle the error cases more elegantly - */ -function PMA_detectSize($last_cumulative_size, $last_cumulative_type, - $curr_type, $cell -) { - $curr_size = mb_strlen((string)$cell); - - /** - * If the cell is NULL, don't treat it as a varchar - */ - if (! strcmp('NULL', $cell)) { - return $last_cumulative_size; - } elseif ($curr_type == VARCHAR) { - /** - * What to do if the current cell is of type VARCHAR - */ - /** - * The last cumulative type was VARCHAR - */ - if ($last_cumulative_type == VARCHAR) { - if ($curr_size >= $last_cumulative_size) { - return $curr_size; - } else { - return $last_cumulative_size; - } - } elseif ($last_cumulative_type == DECIMAL) { - /** - * The last cumulative type was DECIMAL - */ - $oldM = PMA_getDecimalPrecision($last_cumulative_size); - - if ($curr_size >= $oldM) { - return $curr_size; - } else { - return $oldM; - } - } elseif ($last_cumulative_type == BIGINT || $last_cumulative_type == INT) { - /** - * The last cumulative type was BIGINT or INT - */ - if ($curr_size >= $last_cumulative_size) { - return $curr_size; - } else { - return $last_cumulative_size; - } - } elseif (! isset($last_cumulative_type) || $last_cumulative_type == NONE) { - /** - * This is the first row to be analyzed - */ - return $curr_size; - } else { - /** - * An error has DEFINITELY occurred - */ - /** - * TODO: Handle this MUCH more elegantly - */ - - return -1; - } - } elseif ($curr_type == DECIMAL) { - /** - * What to do if the current cell is of type DECIMAL - */ - /** - * The last cumulative type was VARCHAR - */ - if ($last_cumulative_type == VARCHAR) { - /* Convert $last_cumulative_size from varchar to decimal format */ - $size = PMA_getDecimalSize($cell); - - if ($size[M] >= $last_cumulative_size) { - return $size[M]; - } else { - return $last_cumulative_size; - } - } elseif ($last_cumulative_type == DECIMAL) { - /** - * The last cumulative type was DECIMAL - */ - $size = PMA_getDecimalSize($cell); - - $oldM = PMA_getDecimalPrecision($last_cumulative_size); - $oldD = PMA_getDecimalScale($last_cumulative_size); - - /* New val if M or D is greater than current largest */ - if ($size[M] > $oldM || $size[D] > $oldD) { - /* Take the largest of both types */ - return (string) ((($size[M] > $oldM) ? $size[M] : $oldM) - . "," . (($size[D] > $oldD) ? $size[D] : $oldD)); - } else { - return $last_cumulative_size; - } - } elseif ($last_cumulative_type == BIGINT || $last_cumulative_type == INT) { - /** - * The last cumulative type was BIGINT or INT - */ - /* Convert $last_cumulative_size from int to decimal format */ - $size = PMA_getDecimalSize($cell); - - if ($size[M] >= $last_cumulative_size) { - return $size[FULL]; - } else { - return ($last_cumulative_size . "," . $size[D]); - } - } elseif (! isset($last_cumulative_type) || $last_cumulative_type == NONE) { - /** - * This is the first row to be analyzed - */ - /* First row of the column */ - $size = PMA_getDecimalSize($cell); - - return $size[FULL]; - } else { - /** - * An error has DEFINITELY occurred - */ - /** - * TODO: Handle this MUCH more elegantly - */ - - return -1; - } - } elseif ($curr_type == BIGINT || $curr_type == INT) { - /** - * What to do if the current cell is of type BIGINT or INT - */ - /** - * The last cumulative type was VARCHAR - */ - if ($last_cumulative_type == VARCHAR) { - if ($curr_size >= $last_cumulative_size) { - return $curr_size; - } else { - return $last_cumulative_size; - } - } elseif ($last_cumulative_type == DECIMAL) { - /** - * The last cumulative type was DECIMAL - */ - $oldM = PMA_getDecimalPrecision($last_cumulative_size); - $oldD = PMA_getDecimalScale($last_cumulative_size); - $oldInt = $oldM - $oldD; - $newInt = mb_strlen((string)$cell); - - /* See which has the larger integer length */ - if ($oldInt >= $newInt) { - /* Use old decimal size */ - return $last_cumulative_size; - } else { - /* Use $newInt + $oldD as new M */ - return (($newInt + $oldD) . "," . $oldD); - } - } elseif ($last_cumulative_type == BIGINT || $last_cumulative_type == INT) { - /** - * The last cumulative type was BIGINT or INT - */ - if ($curr_size >= $last_cumulative_size) { - return $curr_size; - } else { - return $last_cumulative_size; - } - } elseif (! isset($last_cumulative_type) || $last_cumulative_type == NONE) { - /** - * This is the first row to be analyzed - */ - return $curr_size; - } else { - /** - * An error has DEFINITELY occurred - */ - /** - * TODO: Handle this MUCH more elegantly - */ - - return -1; - } - } else { - /** - * An error has DEFINITELY occurred - */ - /** - * TODO: Handle this MUCH more elegantly - */ - - return -1; - } -} - -/** - * Determines what MySQL type a cell is - * - * @param int $last_cumulative_type Last cumulative column type - * (VARCHAR or INT or BIGINT or DECIMAL or NONE) - * @param string $cell String representation of the cell for which - * a best-fit type is to be determined - * - * @return int The MySQL type representation - * (VARCHAR or INT or BIGINT or DECIMAL or NONE) - * @access public - */ -function PMA_detectType($last_cumulative_type, $cell) -{ - /** - * If numeric, determine if decimal, int or bigint - * Else, we call it varchar for simplicity - */ - - if (! strcmp('NULL', $cell)) { - if ($last_cumulative_type === null || $last_cumulative_type == NONE) { - return NONE; - } - - return $last_cumulative_type; - } - - if (!is_numeric($cell)) { - return VARCHAR; - } - - if ($cell == (string)(float)$cell - && mb_strpos($cell, ".") !== false - && mb_substr_count($cell, ".") == 1 - ) { - return DECIMAL; - } - - if (abs($cell) > 2147483647) { - return BIGINT; - } - - return INT; -} - -/** - * Determines if the column types are int, decimal, or string - * - * @param array &$table array(string $table_name, array $col_names, array $rows) - * - * @return array array(array $types, array $sizes) - * @access public - * - * @link https://wiki.phpmyadmin.net/pma/Import - * - * @todo Handle the error case more elegantly - */ -function PMA_analyzeTable(&$table) -{ - /* Get number of rows in table */ - $numRows = count($table[ROWS]); - /* Get number of columns */ - $numCols = count($table[COL_NAMES]); - /* Current type for each column */ - $types = array(); - $sizes = array(); - - /* Initialize $sizes to all 0's */ - for ($i = 0; $i < $numCols; ++$i) { - $sizes[$i] = 0; - } - - /* Initialize $types to NONE */ - for ($i = 0; $i < $numCols; ++$i) { - $types[$i] = NONE; - } - - /* If the passed array is not of the correct form, do not process it */ - if (!is_array($table) - || is_array($table[TBL_NAME]) - || !is_array($table[COL_NAMES]) - || !is_array($table[ROWS]) - ) { - /** - * TODO: Handle this better - */ - - return false; - } - - /* Analyze each column */ - for ($i = 0; $i < $numCols; ++$i) { - /* Analyze the column in each row */ - for ($j = 0; $j < $numRows; ++$j) { - /* Determine type of the current cell */ - $curr_type = PMA_detectType($types[$i], $table[ROWS][$j][$i]); - /* Determine size of the current cell */ - $sizes[$i] = PMA_detectSize( - $sizes[$i], - $types[$i], - $curr_type, - $table[ROWS][$j][$i] - ); - - /** - * If a type for this column has already been declared, - * only alter it if it was a number and a varchar was found - */ - if ($curr_type != NONE) { - if ($curr_type == VARCHAR) { - $types[$i] = VARCHAR; - } else if ($curr_type == DECIMAL) { - if ($types[$i] != VARCHAR) { - $types[$i] = DECIMAL; - } - } else if ($curr_type == BIGINT) { - if ($types[$i] != VARCHAR && $types[$i] != DECIMAL) { - $types[$i] = BIGINT; - } - } else if ($curr_type == INT) { - if ($types[$i] != VARCHAR - && $types[$i] != DECIMAL - && $types[$i] != BIGINT - ) { - $types[$i] = INT; - } - } - } - } - } - - /* Check to ensure that all types are valid */ - $len = count($types); - for ($n = 0; $n < $len; ++$n) { - if (! strcmp(NONE, $types[$n])) { - $types[$n] = VARCHAR; - $sizes[$n] = '10'; - } - } - - return array($types, $sizes); -} - -/* Needed to quell the beast that is Message */ -$import_notice = null; - -/** - * Builds and executes SQL statements to create the database and tables - * as necessary, as well as insert all the data. - * - * @param string $db_name Name of the database - * @param array &$tables Array of tables for the specified database - * @param array &$analyses Analyses of the tables - * @param array &$additional_sql Additional SQL statements to be executed - * @param array $options Associative array of options - * @param array &$sql_data 2-element array with sql data - * - * @return void - * @access public - * - * @link https://wiki.phpmyadmin.net/pma/Import - */ -function PMA_buildSQL($db_name, &$tables, &$analyses = null, - &$additional_sql = null, $options = null, &$sql_data -) { - /* Take care of the options */ - if (isset($options['db_collation'])&& ! is_null($options['db_collation'])) { - $collation = $options['db_collation']; - } else { - $collation = "utf8_general_ci"; - } - - if (isset($options['db_charset']) && ! is_null($options['db_charset'])) { - $charset = $options['db_charset']; - } else { - $charset = "utf8"; - } - - if (isset($options['create_db'])) { - $create_db = $options['create_db']; - } else { - $create_db = true; - } - - /* Create SQL code to handle the database */ - $sql = array(); - - if ($create_db) { - $sql[] = "CREATE DATABASE IF NOT EXISTS " . Util::backquote($db_name) - . " DEFAULT CHARACTER SET " . $charset . " COLLATE " . $collation - . ";"; - } - - /** - * The calling plug-in should include this statement, - * if necessary, in the $additional_sql parameter - * - * $sql[] = "USE " . backquote($db_name); - */ - - /* Execute the SQL statements create above */ - $sql_len = count($sql); - for ($i = 0; $i < $sql_len; ++$i) { - PMA_importRunQuery($sql[$i], $sql[$i], $sql_data); - } - - /* No longer needed */ - unset($sql); - - /* Run the $additional_sql statements supplied by the caller plug-in */ - if ($additional_sql != null) { - /* Clean the SQL first */ - $additional_sql_len = count($additional_sql); - - /** - * Only match tables for now, because CREATE IF NOT EXISTS - * syntax is lacking or nonexisting for views, triggers, - * functions, and procedures. - * - * See: https://bugs.mysql.com/bug.php?id=15287 - * - * To the best of my knowledge this is still an issue. - * - * $pattern = 'CREATE (TABLE|VIEW|TRIGGER|FUNCTION|PROCEDURE)'; - */ - $pattern = '/CREATE [^`]*(TABLE)/'; - $replacement = 'CREATE \\1 IF NOT EXISTS'; - - /* Change CREATE statements to CREATE IF NOT EXISTS to support - * inserting into existing structures - */ - for ($i = 0; $i < $additional_sql_len; ++$i) { - $additional_sql[$i] = preg_replace( - $pattern, - $replacement, - $additional_sql[$i] - ); - /* Execute the resulting statements */ - PMA_importRunQuery($additional_sql[$i], $additional_sql[$i], $sql_data); - } - } - - if ($analyses != null) { - $type_array = array( - NONE => "NULL", - VARCHAR => "varchar", - INT => "int", - DECIMAL => "decimal", - BIGINT => "bigint", - GEOMETRY => 'geometry' - ); - - /* TODO: Do more checking here to make sure they really are matched */ - if (count($tables) != count($analyses)) { - exit(); - } - - /* Create SQL code to create the tables */ - $num_tables = count($tables); - for ($i = 0; $i < $num_tables; ++$i) { - $num_cols = count($tables[$i][COL_NAMES]); - $tempSQLStr = "CREATE TABLE IF NOT EXISTS " - . PhpMyAdmin\Util::backquote($db_name) - . '.' . PhpMyAdmin\Util::backquote($tables[$i][TBL_NAME]) . " ("; - for ($j = 0; $j < $num_cols; ++$j) { - $size = $analyses[$i][SIZES][$j]; - if ((int)$size == 0) { - $size = 10; - } - - $tempSQLStr .= PhpMyAdmin\Util::backquote( - $tables[$i][COL_NAMES][$j] - ) . " " - . $type_array[$analyses[$i][TYPES][$j]]; - if ($analyses[$i][TYPES][$j] != GEOMETRY) { - $tempSQLStr .= "(" . $size . ")"; - } - - if ($j != (count($tables[$i][COL_NAMES]) - 1)) { - $tempSQLStr .= ", "; - } - } - $tempSQLStr .= ") DEFAULT CHARACTER SET " . $charset - . " COLLATE " . $collation . ";"; - - /** - * Each SQL statement is executed immediately - * after it is formed so that we don't have - * to store them in a (possibly large) buffer - */ - PMA_importRunQuery($tempSQLStr, $tempSQLStr, $sql_data); - } - } - - /** - * Create the SQL statements to insert all the data - * - * Only one insert query is formed for each table - */ - $tempSQLStr = ""; - $col_count = 0; - $num_tables = count($tables); - for ($i = 0; $i < $num_tables; ++$i) { - $num_cols = count($tables[$i][COL_NAMES]); - $num_rows = count($tables[$i][ROWS]); - - $tempSQLStr = "INSERT INTO " . PhpMyAdmin\Util::backquote($db_name) . '.' - . PhpMyAdmin\Util::backquote($tables[$i][TBL_NAME]) . " ("; - - for ($m = 0; $m < $num_cols; ++$m) { - $tempSQLStr .= PhpMyAdmin\Util::backquote($tables[$i][COL_NAMES][$m]); - - if ($m != ($num_cols - 1)) { - $tempSQLStr .= ", "; - } - } - - $tempSQLStr .= ") VALUES "; - - for ($j = 0; $j < $num_rows; ++$j) { - $tempSQLStr .= "("; - - for ($k = 0; $k < $num_cols; ++$k) { - // If fully formatted SQL, no need to enclose - // with apostrophes, add slashes etc. - if ($analyses != null - && isset($analyses[$i][FORMATTEDSQL][$col_count]) - && $analyses[$i][FORMATTEDSQL][$col_count] == true - ) { - $tempSQLStr .= (string) $tables[$i][ROWS][$j][$k]; - } else { - if ($analyses != null) { - $is_varchar = ($analyses[$i][TYPES][$col_count] === VARCHAR); - } else { - $is_varchar = ! is_numeric($tables[$i][ROWS][$j][$k]); - } - - /* Don't put quotes around NULL fields */ - if (! strcmp($tables[$i][ROWS][$j][$k], 'NULL')) { - $is_varchar = false; - } - - $tempSQLStr .= (($is_varchar) ? "'" : ""); - $tempSQLStr .= $GLOBALS['dbi']->escapeString( - (string) $tables[$i][ROWS][$j][$k] - ); - $tempSQLStr .= (($is_varchar) ? "'" : ""); - } - - if ($k != ($num_cols - 1)) { - $tempSQLStr .= ", "; - } - - if ($col_count == ($num_cols - 1)) { - $col_count = 0; - } else { - $col_count++; - } - - /* Delete the cell after we are done with it */ - unset($tables[$i][ROWS][$j][$k]); - } - - $tempSQLStr .= ")"; - - if ($j != ($num_rows - 1)) { - $tempSQLStr .= ",\n "; - } - - $col_count = 0; - /* Delete the row after we are done with it */ - unset($tables[$i][ROWS][$j]); - } - - $tempSQLStr .= ";"; - - /** - * Each SQL statement is executed immediately - * after it is formed so that we don't have - * to store them in a (possibly large) buffer - */ - PMA_importRunQuery($tempSQLStr, $tempSQLStr, $sql_data); - } - - /* No longer needed */ - unset($tempSQLStr); - - /** - * A work in progress - */ - - /* Add the viewable structures from $additional_sql - * to $tables so they are also displayed - */ - $view_pattern = '@VIEW `[^`]+`\.`([^`]+)@'; - $table_pattern = '@CREATE TABLE IF NOT EXISTS `([^`]+)`@'; - /* Check a third pattern to make sure its not a "USE `db_name`;" statement */ - - $regs = array(); - - $inTables = false; - - $additional_sql_len = is_null($additional_sql) ? 0 : count($additional_sql); - for ($i = 0; $i < $additional_sql_len; ++$i) { - preg_match($view_pattern, $additional_sql[$i], $regs); - - if (count($regs) == 0) { - preg_match($table_pattern, $additional_sql[$i], $regs); - } - - if (count($regs)) { - for ($n = 0; $n < $num_tables; ++$n) { - if (! strcmp($regs[1], $tables[$n][TBL_NAME])) { - $inTables = true; - break; - } - } - - if (! $inTables) { - $tables[] = array(TBL_NAME => $regs[1]); - } - } - - /* Reset the array */ - $regs = array(); - $inTables = false; - } - - $params = array('db' => (string)$db_name); - $db_url = 'db_structure.php' . Url::getCommon($params); - $db_ops_url = 'db_operations.php' . Url::getCommon($params); - - $message = '

'; - $message .= '' . __( - 'The following structures have either been created or altered. Here you can:' - ) . '
'; - $message .= '
  • ' . __( - "View a structure's contents by clicking on its name." - ) . '
  • '; - $message .= '
  • ' . __( - 'Change any of its settings by clicking the corresponding "Options" link.' - ) . '
  • '; - $message .= '
  • ' . __('Edit structure by following the "Structure" link.') - . '
  • '; - $message .= sprintf( - '
  • %s (' - . __('Options') . ')
  • ', - $db_url, - sprintf( - __('Go to database: %s'), - htmlspecialchars(PhpMyAdmin\Util::backquote($db_name)) - ), - htmlspecialchars($db_name), - $db_ops_url, - sprintf( - __('Edit settings for %s'), - htmlspecialchars(PhpMyAdmin\Util::backquote($db_name)) - ) - ); - - $message .= '
      '; - - unset($params); - - $num_tables = count($tables); - for ($i = 0; $i < $num_tables; ++$i) { - $params = array( - 'db' => (string) $db_name, - 'table' => (string) $tables[$i][TBL_NAME] - ); - $tbl_url = 'sql.php' . Url::getCommon($params); - $tbl_struct_url = 'tbl_structure.php' . Url::getCommon($params); - $tbl_ops_url = 'tbl_operations.php' . Url::getCommon($params); - - unset($params); - - $_table = new Table($tables[$i][TBL_NAME], $db_name); - if (! $_table->isView()) { - $message .= sprintf( - '
    • %s (' . __( - 'Structure' - ) . ') (' . __('Options') . ')
    • ', - $tbl_url, - sprintf( - __('Go to table: %s'), - htmlspecialchars( - PhpMyAdmin\Util::backquote($tables[$i][TBL_NAME]) - ) - ), - htmlspecialchars($tables[$i][TBL_NAME]), - $tbl_struct_url, - sprintf( - __('Structure of %s'), - htmlspecialchars( - PhpMyAdmin\Util::backquote($tables[$i][TBL_NAME]) - ) - ), - $tbl_ops_url, - sprintf( - __('Edit settings for %s'), - htmlspecialchars( - PhpMyAdmin\Util::backquote($tables[$i][TBL_NAME]) - ) - ) - ); - } else { - $message .= sprintf( - '
    • %s
    • ', - $tbl_url, - sprintf( - __('Go to view: %s'), - htmlspecialchars( - PhpMyAdmin\Util::backquote($tables[$i][TBL_NAME]) - ) - ), - htmlspecialchars($tables[$i][TBL_NAME]) - ); - } - } - - $message .= '
'; - - global $import_notice; - $import_notice = $message; - - unset($tables); -} - - -/** - * Stops the import on (mostly upload/file related) error - * - * @param PhpMyAdmin\Message $error_message The error message - * - * @return void - * @access public - * - */ -function PMA_stopImport( Message $error_message ) -{ - global $import_handle, $file_to_unlink; - - // Close open handles - if ($import_handle !== false && $import_handle !== null) { - $import_handle->close(); - } - - // Delete temporary file - if ($file_to_unlink != '') { - unlink($file_to_unlink); - } - $msg = $error_message->getDisplay(); - $_SESSION['Import_message']['message'] = $msg; - - $response = Response::getInstance(); - $response->setRequestStatus(false); - $response->addJSON('message', PhpMyAdmin\Message::error($msg)); - - exit; -} - -/** - * Handles request for Simulation of UPDATE/DELETE queries. - * - * @return void - */ -function PMA_handleSimulateDMLRequest() -{ - $response = Response::getInstance(); - $error = false; - $error_msg = __('Only single-table UPDATE and DELETE queries can be simulated.'); - $sql_delimiter = $_REQUEST['sql_delimiter']; - $sql_data = array(); - $queries = explode($sql_delimiter, $GLOBALS['sql_query']); - foreach ($queries as $sql_query) { - if (empty($sql_query)) { - continue; - } - - // Parsing the query. - $parser = new PhpMyAdmin\SqlParser\Parser($sql_query); - - if (empty($parser->statements[0])) { - continue; - } - - $statement = $parser->statements[0]; - - $analyzed_sql_results = array( - 'query' => $sql_query, - 'parser' => $parser, - 'statement' => $statement, - ); - - if ((!(($statement instanceof PhpMyAdmin\SqlParser\Statements\UpdateStatement) - || ($statement instanceof PhpMyAdmin\SqlParser\Statements\DeleteStatement))) - || (!empty($statement->join)) - ) { - $error = $error_msg; - break; - } - - $tables = PhpMyAdmin\SqlParser\Utils\Query::getTables($statement); - if (count($tables) > 1) { - $error = $error_msg; - break; - } - - // Get the matched rows for the query. - $result = PMA_getMatchedRows($analyzed_sql_results); - if (! $error = $GLOBALS['dbi']->getError()) { - $sql_data[] = $result; - } else { - break; - } - } - - if ($error) { - $message = Message::rawError($error); - $response->addJSON('message', $message); - $response->addJSON('sql_data', false); - } else { - $response->addJSON('sql_data', $sql_data); - } -} - -/** - * Find the matching rows for UPDATE/DELETE query. - * - * @param array $analyzed_sql_results Analyzed SQL results from parser. - * - * @return mixed - */ -function PMA_getMatchedRows($analyzed_sql_results = array()) -{ - $statement = $analyzed_sql_results['statement']; - - $matched_row_query = ''; - if ($statement instanceof PhpMyAdmin\SqlParser\Statements\DeleteStatement) { - $matched_row_query = PMA_getSimulatedDeleteQuery($analyzed_sql_results); - } elseif ($statement instanceof PhpMyAdmin\SqlParser\Statements\UpdateStatement) { - $matched_row_query = PMA_getSimulatedUpdateQuery($analyzed_sql_results); - } - - // Execute the query and get the number of matched rows. - $matched_rows = PMA_executeMatchedRowQuery($matched_row_query); - - // URL to matched rows. - $_url_params = array( - 'db' => $GLOBALS['db'], - 'sql_query' => $matched_row_query - ); - $matched_rows_url = 'sql.php' . Url::getCommon($_url_params); - - return array( - 'sql_query' => PhpMyAdmin\Util::formatSql($analyzed_sql_results['query']), - 'matched_rows' => $matched_rows, - 'matched_rows_url' => $matched_rows_url - ); -} - -/** - * Transforms a UPDATE query into SELECT statement. - * - * @param array $analyzed_sql_results Analyzed SQL results from parser. - * - * @return string SQL query - */ -function PMA_getSimulatedUpdateQuery($analyzed_sql_results) -{ - $table_references = PhpMyAdmin\SqlParser\Utils\Query::getTables( - $analyzed_sql_results['statement'] - ); - - $where = PhpMyAdmin\SqlParser\Utils\Query::getClause( - $analyzed_sql_results['statement'], - $analyzed_sql_results['parser']->list, - 'WHERE' - ); - - if (empty($where)) { - $where = '1'; - } - - $columns = array(); - $diff = array(); - foreach ($analyzed_sql_results['statement']->set as $set) { - $columns[] = $set->column; - $diff[] = $set->column . ' <> ' . $set->value; - } - if (!empty($diff)) { - $where .= ' AND (' . implode(' OR ', $diff) . ')'; - } - - $order_and_limit = ''; - - if (!empty($analyzed_sql_results['statement']->order)) { - $order_and_limit .= ' ORDER BY ' . PhpMyAdmin\SqlParser\Utils\Query::getClause( - $analyzed_sql_results['statement'], - $analyzed_sql_results['parser']->list, - 'ORDER BY' - ); - } - - if (!empty($analyzed_sql_results['statement']->limit)) { - $order_and_limit .= ' LIMIT ' . PhpMyAdmin\SqlParser\Utils\Query::getClause( - $analyzed_sql_results['statement'], - $analyzed_sql_results['parser']->list, - 'LIMIT' - ); - } - - return 'SELECT ' . implode(', ', $columns) . - ' FROM ' . implode(', ', $table_references) . - ' WHERE ' . $where . $order_and_limit; -} - -/** - * Transforms a DELETE query into SELECT statement. - * - * @param array $analyzed_sql_results Analyzed SQL results from parser. - * - * @return string SQL query - */ -function PMA_getSimulatedDeleteQuery($analyzed_sql_results) -{ - $table_references = PhpMyAdmin\SqlParser\Utils\Query::getTables( - $analyzed_sql_results['statement'] - ); - - $where = PhpMyAdmin\SqlParser\Utils\Query::getClause( - $analyzed_sql_results['statement'], - $analyzed_sql_results['parser']->list, - 'WHERE' - ); - - if (empty($where)) { - $where = '1'; - } - - $order_and_limit = ''; - - if (!empty($analyzed_sql_results['statement']->order)) { - $order_and_limit .= ' ORDER BY ' . PhpMyAdmin\SqlParser\Utils\Query::getClause( - $analyzed_sql_results['statement'], - $analyzed_sql_results['parser']->list, - 'ORDER BY' - ); - } - - if (!empty($analyzed_sql_results['statement']->limit)) { - $order_and_limit .= ' LIMIT ' . PhpMyAdmin\SqlParser\Utils\Query::getClause( - $analyzed_sql_results['statement'], - $analyzed_sql_results['parser']->list, - 'LIMIT' - ); - } - - return 'SELECT * FROM ' . implode(', ', $table_references) . - ' WHERE ' . $where . $order_and_limit; -} - -/** - * Executes the matched_row_query and returns the resultant row count. - * - * @param string $matched_row_query SQL query - * - * @return integer Number of rows returned - */ -function PMA_executeMatchedRowQuery($matched_row_query) -{ - $GLOBALS['dbi']->selectDb($GLOBALS['db']); - // Execute the query. - $result = $GLOBALS['dbi']->tryQuery($matched_row_query); - // Count the number of rows in the result set. - $result = $GLOBALS['dbi']->numRows($result); - - return $result; -} - -/** - * Handles request for ROLLBACK. - * - * @param string $sql_query SQL query(s) - * - * @return void - */ -function PMA_handleRollbackRequest($sql_query) -{ - $sql_delimiter = $_REQUEST['sql_delimiter']; - $queries = explode($sql_delimiter, $sql_query); - $error = false; - $error_msg = __( - 'Only INSERT, UPDATE, DELETE and REPLACE ' - . 'SQL queries containing transactional engine tables can be rolled back.' - ); - foreach ($queries as $sql_query) { - if (empty($sql_query)) { - continue; - } - - // Check each query for ROLLBACK support. - if (! PMA_checkIfRollbackPossible($sql_query)) { - $global_error = $GLOBALS['dbi']->getError(); - if ($global_error) { - $error = $global_error; - } else { - $error = $error_msg; - } - break; - } - } - - if ($error) { - unset($_REQUEST['rollback_query']); - $response = Response::getInstance(); - $message = Message::rawError($error); - $response->addJSON('message', $message); - exit; - } else { - // If everything fine, START a transaction. - $GLOBALS['dbi']->query('START TRANSACTION'); - } -} - -/** - * Checks if ROLLBACK is possible for a SQL query or not. - * - * @param string $sql_query SQL query - * - * @return bool - */ -function PMA_checkIfRollbackPossible($sql_query) -{ - $parser = new PhpMyAdmin\SqlParser\Parser($sql_query); - - if (empty($parser->statements[0])) { - return false; - } - - $statement = $parser->statements[0]; - - // Check if query is supported. - if (!(($statement instanceof PhpMyAdmin\SqlParser\Statements\InsertStatement) - || ($statement instanceof PhpMyAdmin\SqlParser\Statements\UpdateStatement) - || ($statement instanceof PhpMyAdmin\SqlParser\Statements\DeleteStatement) - || ($statement instanceof PhpMyAdmin\SqlParser\Statements\ReplaceStatement)) - ) { - return false; - } - - // Get table_references from the query. - $tables = PhpMyAdmin\SqlParser\Utils\Query::getTables($statement); - - // Check if each table is 'InnoDB'. - foreach ($tables as $table) { - if (! PMA_isTableTransactional($table)) { - return false; - } - } - - return true; -} - -/** - * Checks if a table is 'InnoDB' or not. - * - * @param string $table Table details - * - * @return bool - */ -function PMA_isTableTransactional($table) -{ - $table = explode('.', $table); - if (count($table) == 2) { - $db = PhpMyAdmin\Util::unQuote($table[0]); - $table = PhpMyAdmin\Util::unQuote($table[1]); - } else { - $db = $GLOBALS['db']; - $table = PhpMyAdmin\Util::unQuote($table[0]); - } - - // Query to check if table exists. - $check_table_query = 'SELECT * FROM ' . PhpMyAdmin\Util::backquote($db) - . '.' . PhpMyAdmin\Util::backquote($table) . ' ' - . 'LIMIT 1'; - - $result = $GLOBALS['dbi']->tryQuery($check_table_query); - - if (! $result) { - return false; - } - - // List of Transactional Engines. - $transactional_engines = array( - 'INNODB', - 'FALCON', - 'NDB', - 'INFINIDB', - 'TOKUDB', - 'XTRADB', - 'SEQUENCE', - 'BDB' - ); - - // Query to check if table is 'Transactional'. - $check_query = 'SELECT `ENGINE` FROM `information_schema`.`tables` ' - . 'WHERE `table_name` = "' . $table . '" ' - . 'AND `table_schema` = "' . $db . '" ' - . 'AND UPPER(`engine`) IN ("' - . implode('", "', $transactional_engines) - . '")'; - - $result = $GLOBALS['dbi']->tryQuery($check_query); - - if ($GLOBALS['dbi']->numRows($result) == 1) { - return true; - } else { - return false; - } -} diff --git a/test/libraries/PMA_import_test.php b/test/classes/ImportTest.php similarity index 79% rename from test/libraries/PMA_import_test.php rename to test/classes/ImportTest.php index 85adf87484..eb91f4919d 100644 --- a/test/libraries/PMA_import_test.php +++ b/test/classes/ImportTest.php @@ -1,16 +1,16 @@ assertFalse(PMA_checkTimeout()); + $this->assertFalse(Import::checkTimeout()); //Reinit values. $timestamp = time(); $maximum_time = 0; $timeout_passed = true; - $this->assertFalse(PMA_checkTimeout()); + $this->assertFalse(Import::checkTimeout()); //Reinit values. $timestamp = time(); $maximum_time = 30; $timeout_passed = true; - $this->assertTrue(PMA_checkTimeout()); + $this->assertTrue(Import::checkTimeout()); //Reinit values. $timestamp = time()-15; $maximum_time = 30; $timeout_passed = false; - $this->assertFalse(PMA_checkTimeout()); + $this->assertFalse(Import::checkTimeout()); //Reinit values. $timestamp = time()-60; $maximum_time = 30; $timeout_passed = false; - $this->assertTrue(PMA_checkTimeout()); + $this->assertTrue(Import::checkTimeout()); } /** - * Test for PMA_lookForUse + * Test for Import::lookForUse * * @return void */ @@ -97,42 +94,42 @@ class PMA_Import_Test extends PHPUnit_Framework_TestCase { $this->assertEquals( array(null, null), - PMA_lookForUse(null, null, null) + Import::lookForUse(null, null, null) ); $this->assertEquals( array('myDb', null), - PMA_lookForUse(null, 'myDb', null) + Import::lookForUse(null, 'myDb', null) ); $this->assertEquals( array('myDb', true), - PMA_lookForUse(null, 'myDb', true) + Import::lookForUse(null, 'myDb', true) ); $this->assertEquals( array('myDb', true), - PMA_lookForUse('select 1 from myTable', 'myDb', true) + Import::lookForUse('select 1 from myTable', 'myDb', true) ); $this->assertEquals( array('anotherDb', true), - PMA_lookForUse('use anotherDb', 'myDb', false) + Import::lookForUse('use anotherDb', 'myDb', false) ); $this->assertEquals( array('anotherDb', true), - PMA_lookForUse('use anotherDb', 'myDb', true) + Import::lookForUse('use anotherDb', 'myDb', true) ); $this->assertEquals( array('anotherDb', true), - PMA_lookForUse('use `anotherDb`;', 'myDb', true) + Import::lookForUse('use `anotherDb`;', 'myDb', true) ); } /** - * Test for PMA_getColumnAlphaName + * Test for Import::getColumnAlphaName * * @param string $expected Expected result of the function * @param int $num The column number @@ -143,7 +140,7 @@ class PMA_Import_Test extends PHPUnit_Framework_TestCase */ function testGetColumnAlphaName($expected, $num) { - $this->assertEquals($expected, PMA_getColumnAlphaName($num)); + $this->assertEquals($expected, Import::getColumnAlphaName($num)); } /** @@ -164,7 +161,7 @@ class PMA_Import_Test extends PHPUnit_Framework_TestCase } /** - * Test for PMA_getColumnNumberFromName + * Test for Import::getColumnNumberFromName * * @param int $expected Expected result of the function * @param string|null $name column name(i.e. "A", or "BC", etc.) @@ -175,7 +172,7 @@ class PMA_Import_Test extends PHPUnit_Framework_TestCase */ function testGetColumnNumberFromName($expected, $name) { - $this->assertEquals($expected, PMA_getColumnNumberFromName($name)); + $this->assertEquals($expected, Import::getColumnNumberFromName($name)); } /** @@ -196,7 +193,7 @@ class PMA_Import_Test extends PHPUnit_Framework_TestCase } /** - * Test for PMA_getDecimalPrecision + * Test for Import::getDecimalPrecision * * @param int $expected Expected result of the function * @param string|null $size Size of field @@ -207,7 +204,7 @@ class PMA_Import_Test extends PHPUnit_Framework_TestCase */ function testGetDecimalPrecision($expected, $size) { - $this->assertEquals($expected, PMA_getDecimalPrecision($size)); + $this->assertEquals($expected, Import::getDecimalPrecision($size)); } /** @@ -226,7 +223,7 @@ class PMA_Import_Test extends PHPUnit_Framework_TestCase } /** - * Test for PMA_getDecimalScale + * Test for Import::getDecimalScale * * @param int $expected Expected result of the function * @param string|null $size Size of field @@ -237,7 +234,7 @@ class PMA_Import_Test extends PHPUnit_Framework_TestCase */ function testGetDecimalScale($expected, $size) { - $this->assertEquals($expected, PMA_getDecimalScale($size)); + $this->assertEquals($expected, Import::getDecimalScale($size)); } /** @@ -256,7 +253,7 @@ class PMA_Import_Test extends PHPUnit_Framework_TestCase } /** - * Test for PMA_getDecimalSize + * Test for Import::getDecimalSize * * @param array $expected Expected result of the function * @param string|null $cell Cell content @@ -267,7 +264,7 @@ class PMA_Import_Test extends PHPUnit_Framework_TestCase */ function testGetDecimalSize($expected, $cell) { - $this->assertEquals($expected, PMA_getDecimalSize($cell)); + $this->assertEquals($expected, Import::getDecimalSize($cell)); } /** @@ -286,7 +283,7 @@ class PMA_Import_Test extends PHPUnit_Framework_TestCase } /** - * Test for PMA_detectType + * Test for Import::detectType * * @param int $expected Expected result of the function * @param int|null $type Last cumulative column type (VARCHAR or INT or @@ -300,7 +297,7 @@ class PMA_Import_Test extends PHPUnit_Framework_TestCase */ function testDetectType($expected, $type, $cell) { - $this->assertEquals($expected, PMA_detectType($type, $cell)); + $this->assertEquals($expected, Import::detectType($type, $cell)); } /** @@ -311,24 +308,24 @@ class PMA_Import_Test extends PHPUnit_Framework_TestCase function provDetectType() { return array( - array(NONE, null, 'NULL'), - array(NONE, NONE, 'NULL'), - array(INT, INT, 'NULL'), - array(VARCHAR, VARCHAR, 'NULL'), - array(VARCHAR, null, null), - array(VARCHAR, INT, null), - array(INT, INT, '10'), - array(DECIMAL, DECIMAL, '10.2'), - array(DECIMAL, INT, '10.2'), - array(BIGINT, BIGINT, '2147483648'), - array(BIGINT, INT, '2147483648'), - array(VARCHAR, VARCHAR, 'test'), - array(VARCHAR, INT, 'test'), + array(Import::NONE, null, 'NULL'), + array(Import::NONE, Import::NONE, 'NULL'), + array(Import::INT, Import::INT, 'NULL'), + array(Import::VARCHAR, Import::VARCHAR, 'NULL'), + array(Import::VARCHAR, null, null), + array(Import::VARCHAR, Import::INT, null), + array(Import::INT, Import::INT, '10'), + array(Import::DECIMAL, Import::DECIMAL, '10.2'), + array(Import::DECIMAL, Import::INT, '10.2'), + array(Import::BIGINT, Import::BIGINT, '2147483648'), + array(Import::BIGINT, Import::INT, '2147483648'), + array(Import::VARCHAR, Import::VARCHAR, 'test'), + array(Import::VARCHAR, Import::INT, 'test'), ); } /** - * Test for PMA_getMatchedRows. + * Test for Import::getMatchedRows. * * @return void */ @@ -385,14 +382,14 @@ class PMA_Import_Test extends PHPUnit_Framework_TestCase */ function simulatedQueryTest($sql_query, $simulated_query) { - $parser = new PhpMyAdmin\SqlParser\Parser($sql_query); + $parser = new Parser($sql_query); $analyzed_sql_results = array( 'query' => $sql_query, 'parser' => $parser, 'statement' => $parser->statements[0], ); - $simulated_data = PMA_getMatchedRows($analyzed_sql_results); + $simulated_data = Import::getMatchedRows($analyzed_sql_results); // URL to matched rows. $_url_params = array( @@ -403,7 +400,7 @@ class PMA_Import_Test extends PHPUnit_Framework_TestCase $this->assertEquals( array( - 'sql_query' => PhpMyAdmin\Util::formatSql( + 'sql_query' => Util::formatSql( $analyzed_sql_results['query'] ), 'matched_rows' => 2, @@ -414,7 +411,7 @@ class PMA_Import_Test extends PHPUnit_Framework_TestCase } /** - * Test for PMA_checkIfRollbackPossible + * Test for Import::checkIfRollbackPossible * * @return void */ @@ -482,6 +479,6 @@ class PMA_Import_Test extends PHPUnit_Framework_TestCase . 'SET `table_1`.`id` = `table_2`.`id` ' . 'WHERE 1'; - $this->assertEquals(true, PMA_checkIfRollbackPossible($sql_query)); + $this->assertEquals(true, Import::checkIfRollbackPossible($sql_query)); } } diff --git a/test/classes/plugin/import/ImportCsvTest.php b/test/classes/plugin/import/ImportCsvTest.php index 8545aa0a41..7135d9a343 100644 --- a/test/classes/plugin/import/ImportCsvTest.php +++ b/test/classes/plugin/import/ImportCsvTest.php @@ -19,7 +19,6 @@ $GLOBALS['server'] = 0; * Include to test. */ require_once 'libraries/database_interface.inc.php'; -require_once 'libraries/import.lib.php'; require_once 'test/PMATestCase.php'; /** diff --git a/test/classes/plugin/import/ImportLdiTest.php b/test/classes/plugin/import/ImportLdiTest.php index 201ce1abe6..11d791618b 100644 --- a/test/classes/plugin/import/ImportLdiTest.php +++ b/test/classes/plugin/import/ImportLdiTest.php @@ -19,7 +19,6 @@ $GLOBALS['plugin_param'] = "table"; * Include to test. */ require_once 'libraries/database_interface.inc.php'; -require_once 'libraries/import.lib.php'; require_once 'test/PMATestCase.php'; /** diff --git a/test/classes/plugin/import/ImportMediawikiTest.php b/test/classes/plugin/import/ImportMediawikiTest.php index 3c1b0b5c20..ccdd70075e 100644 --- a/test/classes/plugin/import/ImportMediawikiTest.php +++ b/test/classes/plugin/import/ImportMediawikiTest.php @@ -17,7 +17,6 @@ $GLOBALS['server'] = 0; * Include to test. */ require_once 'libraries/database_interface.inc.php'; -require_once 'libraries/import.lib.php'; require_once 'test/PMATestCase.php'; /** diff --git a/test/classes/plugin/import/ImportOdsTest.php b/test/classes/plugin/import/ImportOdsTest.php index 4b2b85013f..18e730746f 100644 --- a/test/classes/plugin/import/ImportOdsTest.php +++ b/test/classes/plugin/import/ImportOdsTest.php @@ -18,7 +18,6 @@ $GLOBALS['server'] = 0; * Include to test. */ require_once 'libraries/database_interface.inc.php'; -require_once 'libraries/import.lib.php'; require_once 'test/PMATestCase.php'; /** diff --git a/test/classes/plugin/import/ImportShpTest.php b/test/classes/plugin/import/ImportShpTest.php index 51de09a478..019e933d0d 100644 --- a/test/classes/plugin/import/ImportShpTest.php +++ b/test/classes/plugin/import/ImportShpTest.php @@ -15,7 +15,6 @@ use PhpMyAdmin\File; $GLOBALS['server'] = 0; require_once 'libraries/database_interface.inc.php'; -require_once 'libraries/import.lib.php'; require_once 'test/PMATestCase.php'; /** diff --git a/test/classes/plugin/import/ImportSqlTest.php b/test/classes/plugin/import/ImportSqlTest.php index ceae3f5f70..cadd1ea11b 100644 --- a/test/classes/plugin/import/ImportSqlTest.php +++ b/test/classes/plugin/import/ImportSqlTest.php @@ -18,7 +18,6 @@ $GLOBALS['server'] = 0; * Include to test. */ require_once 'libraries/database_interface.inc.php'; -require_once 'libraries/import.lib.php'; require_once 'test/PMATestCase.php'; /** diff --git a/test/classes/plugin/import/ImportXmlTest.php b/test/classes/plugin/import/ImportXmlTest.php index 2b2475e8e8..dac5013494 100644 --- a/test/classes/plugin/import/ImportXmlTest.php +++ b/test/classes/plugin/import/ImportXmlTest.php @@ -18,7 +18,6 @@ $GLOBALS['server'] = 0; * Include to test. */ require_once 'libraries/database_interface.inc.php'; -require_once 'libraries/import.lib.php'; require_once 'test/PMATestCase.php'; /**