本文整理汇总了PHP中PMA_importGetNextChunk函数的典型用法代码示例。如果您正苦于以下问题:PHP PMA_importGetNextChunk函数的具体用法?PHP PMA_importGetNextChunk怎么用?PHP PMA_importGetNextChunk使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了PMA_importGetNextChunk函数的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: readFromBuffer
function readFromBuffer($length)
{
global $buffer, $eof;
if (strlen($buffer) < $length) {
if ($GLOBALS['finished']) {
$eof = true;
} else {
$buffer .= PMA_importGetNextChunk();
}
}
$result = substr($buffer, 0, $length);
$buffer = substr($buffer, $length);
return $result;
}
示例2: doImport
/**
* Handles the whole import logic
*
* @param array &$sql_data 2-element array with sql data
*
* @return void
*/
public function doImport(&$sql_data = array())
{
global $error, $timeout_passed;
$buffer = '';
// Defaults for parser
$sql = '';
$start_pos = 0;
$i = 0;
$len = 0;
$big_value = 2147483647;
// include the space because it's mandatory
$delimiter_keyword = 'DELIMITER ';
$length_of_delimiter_keyword = strlen($delimiter_keyword);
if (isset($_POST['sql_delimiter'])) {
$sql_delimiter = $_POST['sql_delimiter'];
} else {
$sql_delimiter = ';';
}
// Handle compatibility options
$sql_modes = array();
if (isset($_REQUEST['sql_compatibility']) && 'NONE' != $_REQUEST['sql_compatibility']) {
$sql_modes[] = $_REQUEST['sql_compatibility'];
}
if (isset($_REQUEST['sql_no_auto_value_on_zero'])) {
$sql_modes[] = 'NO_AUTO_VALUE_ON_ZERO';
}
if (count($sql_modes) > 0) {
$GLOBALS['dbi']->tryQuery('SET SQL_MODE="' . implode(',', $sql_modes) . '"');
}
unset($sql_modes);
/**
* will be set in PMA_importGetNextChunk()
*
* @global boolean $GLOBALS['finished']
*/
$GLOBALS['finished'] = false;
while (!($GLOBALS['finished'] && $i >= $len) && !$error && !$timeout_passed) {
$data = PMA_importGetNextChunk();
if ($data === false) {
// subtract data we didn't handle yet and stop processing
$GLOBALS['offset'] -= strlen($buffer);
break;
} elseif ($data === true) {
// Handle rest of buffer
} else {
// Append new data to buffer
$buffer .= $data;
// free memory
unset($data);
// Do not parse string when we're not at the end
// and don't have ; inside
if (strpos($buffer, $sql_delimiter, $i) === false && !$GLOBALS['finished']) {
continue;
}
}
// Convert CR (but not CRLF) to LF otherwise all queries
// may not get executed on some platforms
$buffer = preg_replace("/\r(\$|[^\n])/", "\n\$1", $buffer);
// Current length of our buffer
$len = strlen($buffer);
// Grab some SQL queries out of it
while ($i < $len) {
$found_delimiter = false;
// Find first interesting character
$old_i = $i;
// this is about 7 times faster that looking for each sequence i
// one by one with strpos()
$match = preg_match('/(\'|"|#|-- |\\/\\*|`|(?i)(?<![A-Z0-9_])' . $delimiter_keyword . ')/', $buffer, $matches, PREG_OFFSET_CAPTURE, $i);
if ($match) {
// in $matches, index 0 contains the match for the complete
// expression but we don't use it
$first_position = $matches[1][1];
} else {
$first_position = $big_value;
}
/**
* @todo we should not look for a delimiter that might be
* inside quotes (or even double-quotes)
*/
// the cost of doing this one with preg_match() would be too high
$first_sql_delimiter = strpos($buffer, $sql_delimiter, $i);
if ($first_sql_delimiter === false) {
$first_sql_delimiter = $big_value;
} else {
$found_delimiter = true;
}
// set $i to the position of the first quote,
// comment.start or delimiter found
$i = min($first_position, $first_sql_delimiter);
if ($i == $big_value) {
// none of the above was found in the string
$i = $old_i;
if (!$GLOBALS['finished']) {
//.........这里部分代码省略.........
示例3: doImport
/**
* Handles the whole import logic
*
* @param array &$sql_data 2-element array with sql data
*
* @return void
*/
public function doImport(&$sql_data = array())
{
global $error, $timeout_passed;
// Handle compatibility options.
$this->_setSQLMode($GLOBALS['dbi'], $_REQUEST);
$bq = new SqlParser\Utils\BufferedQuery();
if (isset($_POST['sql_delimiter'])) {
$bq->setDelimiter($_POST['sql_delimiter']);
}
/**
* Will be set in PMA_importGetNextChunk().
*
* @global bool $GLOBALS ['finished']
*/
$GLOBALS['finished'] = false;
while (!$error && !$timeout_passed) {
// Getting the first statement, the remaining data and the last
// delimiter.
$statement = $bq->extract();
// If there is no full statement, we are looking for more data.
if (empty($statement)) {
// Importing new data.
$newData = PMA_importGetNextChunk();
// Subtract data we didn't handle yet and stop processing.
if ($newData === false) {
$GLOBALS['offset'] -= mb_strlen($bq->query);
break;
}
// Checking if the input buffer has finished.
if ($newData === true) {
$GLOBALS['finished'] = true;
break;
}
// Convert CR (but not CRLF) to LF otherwise all queries may
// not get executed on some platforms.
$bq->query .= preg_replace("/\r(\$|[^\n])/", "\n\$1", $newData);
continue;
}
// Executing the query.
PMA_importRunQuery($statement, $statement, false, $sql_data);
}
// Extracting remaining statements.
while (!$error && !$timeout_passed && !empty($bq->query)) {
$statement = $bq->extract(true);
if (!empty($statement)) {
PMA_importRunQuery($statement, $statement, false, $sql_data);
}
}
// Finishing.
PMA_importRunQuery('', '', false, $sql_data);
}
示例4: elseif
// Convert the file's charset if necessary
if ($GLOBALS['PMA_recoding_engine'] != PMA_CHARSET_NONE && isset($charset_of_file)) {
if ($charset_of_file != $charset) {
$charset_conversion = TRUE;
}
} elseif (isset($charset_of_file) && $charset_of_file != 'utf8') {
PMA_DBI_query('SET NAMES \'' . $charset_of_file . '\'');
// We can not show query in this case, it is in different charset
$sql_query_disabled = TRUE;
$reset_charset = TRUE;
}
// Something to skip?
if (!$error && isset($skip)) {
$original_skip = $skip;
while ($skip > 0) {
PMA_importGetNextChunk($skip < $read_limit ? $skip : $read_limit);
$read_multiply = 1;
// Disable read progresivity, otherwise we eat all memory!
$skip -= $read_limit;
}
unset($skip);
}
if (!$error) {
// Check for file existance
if (!file_exists('./libraries/import/' . $format . '.php')) {
$error = TRUE;
$message = PMA_Message::error(__('Could not load import plugins, please check your installation!'));
} else {
// Do the real import
$plugin_param = $import_type;
require './libraries/import/' . $format . '.php';
示例5: doImport
/**
* Handles the whole import logic
*
* @return void
*/
public function doImport()
{
global $error, $timeout_passed, $finished;
// Defaults for parser
// The buffer that will be used to store chunks read from the imported file
$buffer = '';
// Used as storage for the last part of the current chunk data
// Will be appended to the first line of the next chunk, if there is one
$last_chunk_line = '';
// Remembers whether the current buffer line is part of a comment
$inside_comment = false;
// Remembers whether the current buffer line is part of a data comment
$inside_data_comment = false;
// Remembers whether the current buffer line is part of a structure comment
$inside_structure_comment = false;
// MediaWiki only accepts "\n" as row terminator
$mediawiki_new_line = "\n";
// Initialize the name of the current table
$cur_table_name = "";
while (!$finished && !$error && !$timeout_passed) {
$data = PMA_importGetNextChunk();
if ($data === false) {
// Subtract data we didn't handle yet and stop processing
$GLOBALS['offset'] -= mb_strlen($buffer);
break;
} elseif ($data === true) {
// Handle rest of buffer
} else {
// Append new data to buffer
$buffer = $data;
unset($data);
// Don't parse string if we're not at the end
// and don't have a new line inside
if (mb_strpos($buffer, $mediawiki_new_line) === false) {
continue;
}
}
// Because of reading chunk by chunk, the first line from the buffer
// contains only a portion of an actual line from the imported file.
// Therefore, we have to append it to the last line from the previous
// chunk. If we are at the first chunk, $last_chunk_line should be empty.
$buffer = $last_chunk_line . $buffer;
// Process the buffer line by line
$buffer_lines = explode($mediawiki_new_line, $buffer);
$full_buffer_lines_count = count($buffer_lines);
// If the reading is not finalised, the final line of the current chunk
// will not be complete
if (!$finished) {
$last_chunk_line = $buffer_lines[--$full_buffer_lines_count];
}
for ($line_nr = 0; $line_nr < $full_buffer_lines_count; ++$line_nr) {
$cur_buffer_line = trim($buffer_lines[$line_nr]);
// If the line is empty, go to the next one
if ($cur_buffer_line === '') {
continue;
}
$first_character = $cur_buffer_line[0];
$matches = array();
// Check beginning of comment
if (!strcmp(mb_substr($cur_buffer_line, 0, 4), "<!--")) {
$inside_comment = true;
continue;
} elseif ($inside_comment) {
// Check end of comment
if (!strcmp(mb_substr($cur_buffer_line, 0, 4), "-->")) {
// Only data comments are closed. The structure comments
// will be closed when a data comment begins (in order to
// skip structure tables)
if ($inside_data_comment) {
$inside_data_comment = false;
}
// End comments that are not related to table structure
if (!$inside_structure_comment) {
$inside_comment = false;
}
} else {
// Check table name
$match_table_name = array();
if (preg_match("/^Table data for `(.*)`\$/", $cur_buffer_line, $match_table_name)) {
$cur_table_name = $match_table_name[1];
$inside_data_comment = true;
$inside_structure_comment = $this->_mngInsideStructComm($inside_structure_comment);
} elseif (preg_match("/^Table structure for `(.*)`\$/", $cur_buffer_line, $match_table_name)) {
// The structure comments will be ignored
$inside_structure_comment = true;
}
}
continue;
} elseif (preg_match('/^\\{\\|(.*)$/', $cur_buffer_line, $matches)) {
// Check start of table
// This will store all the column info on all rows from
// the current table read from the buffer
$cur_temp_table = array();
// Will be used as storage for the current row in the buffer
// Once all its columns are read, it will be added to
//.........这里部分代码省略.........
示例6: array
if (isset($plugin_list)) {
$plugin_list['ods'] = array('text' => __('Open Document Spreadsheet'), 'extension' => 'ods', 'options' => array(array('type' => 'begin_group', 'name' => 'general_opts'), array('type' => 'bool', 'name' => 'col_names', 'text' => __('The first line of the file contains the table column names <i>(if this is unchecked, the first line will become part of the data)</i>')), array('type' => 'bool', 'name' => 'empty_rows', 'text' => __('Do not import empty rows')), array('type' => 'bool', 'name' => 'recognize_percentages', 'text' => __('Import percentages as proper decimals <i>(ex. 12.00% to .12)</i>')), array('type' => 'bool', 'name' => 'recognize_currency', 'text' => __('Import currencies <i>(ex. $5.00 to 5.00)</i>')), array('type' => 'end_group')), 'options_text' => __('Options'));
/* We do not define function when plugin is just queried for information above */
return;
}
ini_set('memory_limit', '128M');
set_time_limit(120);
$i = 0;
$len = 0;
$buffer = "";
/**
* Read in the file via PMA_importGetNextChunk so that
* it can process compressed files
*/
while (!($finished && $i >= $len) && !$error && !$timeout_passed) {
$data = PMA_importGetNextChunk();
if ($data === FALSE) {
/* subtract data we didn't handle yet and stop processing */
$offset -= strlen($buffer);
break;
} elseif ($data === TRUE) {
/* Handle rest of buffer */
} else {
/* Append new data to buffer */
$buffer .= $data;
unset($data);
}
}
unset($data);
/**
* Disable loading of external XML entities.
示例7: doImport
//.........这里部分代码省略.........
break;
}
}
if (!$found) {
$message = PMA\libraries\Message::error(__('Invalid column (%s) specified! Ensure that columns' . ' names are spelled correctly, separated by commas' . ', and not enclosed in quotes.'));
$message->addParam($val);
$error = true;
break;
}
$fields[] = $field;
$sql_template .= PMA\libraries\Util::backquote($val);
}
$sql_template .= ') ';
}
$required_fields = count($fields);
$sql_template .= ' VALUES (';
}
// Defaults for parser
$i = 0;
$len = 0;
$lastlen = null;
$line = 1;
$lasti = -1;
$values = array();
$csv_finish = false;
$tempRow = array();
$rows = array();
$col_names = array();
$tables = array();
$col_count = 0;
$max_cols = 0;
$csv_terminated_len = mb_strlen($csv_terminated);
while (!($finished && $i >= $len) && !$error && !$timeout_passed) {
$data = PMA_importGetNextChunk();
if ($data === false) {
// subtract data we didn't handle yet and stop processing
$GLOBALS['offset'] -= strlen($buffer);
break;
} elseif ($data === true) {
// Handle rest of buffer
} else {
// Append new data to buffer
$buffer .= $data;
unset($data);
// Force a trailing new line at EOF to prevent parsing problems
if ($finished && $buffer) {
$finalch = mb_substr($buffer, -1);
if ($csv_new_line == 'auto' && $finalch != "\r" && $finalch != "\n") {
$buffer .= "\n";
} elseif ($csv_new_line != 'auto' && $finalch != $csv_new_line) {
$buffer .= $csv_new_line;
}
}
// Do not parse string when we're not at the end
// and don't have new line inside
if ($csv_new_line == 'auto' && mb_strpos($buffer, "\r") === false && mb_strpos($buffer, "\n") === false || $csv_new_line != 'auto' && mb_strpos($buffer, $csv_new_line) === false) {
continue;
}
}
// Current length of our buffer
$len = mb_strlen($buffer);
// Currently parsed char
$ch = mb_substr($buffer, $i, 1);
if ($csv_terminated_len > 1 && $ch == $csv_terminated[0]) {
$ch = $this->readCsvTerminatedString($buffer, $ch, $i, $csv_terminated_len);
$i += $csv_terminated_len - 1;
示例8: doImport
/**
* Handles the whole import logic
*
* @return void
*/
public function doImport()
{
global $db, $error, $timeout_passed, $finished;
$i = 0;
$len = 0;
$buffer = "";
/**
* Read in the file via PMA_importGetNextChunk so that
* it can process compressed files
*/
while (!($finished && $i >= $len) && !$error && !$timeout_passed) {
$data = PMA_importGetNextChunk();
if ($data === false) {
/* subtract data we didn't handle yet and stop processing */
$GLOBALS['offset'] -= strlen($buffer);
break;
} elseif ($data === true) {
/* Handle rest of buffer */
} else {
/* Append new data to buffer */
$buffer .= $data;
unset($data);
}
}
unset($data);
/**
* Disable loading of external XML entities.
*/
libxml_disable_entity_loader();
/**
* Load the XML string
*
* The option LIBXML_COMPACT is specified because it can
* result in increased performance without the need to
* alter the code in any way. It's basically a freebee.
*/
$xml = simplexml_load_string($buffer, "SimpleXMLElement", LIBXML_COMPACT);
unset($buffer);
if ($xml === false) {
$sheets = array();
$GLOBALS['message'] = PMA_Message::error(__('The XML file specified was either malformed or incomplete.' . ' Please correct the issue and try again.'));
$GLOBALS['error'] = true;
} else {
$root = $xml->children('office', true)->{'body'}->{'spreadsheet'};
if (empty($root)) {
$sheets = array();
$GLOBALS['message'] = PMA_Message::error(__('Could not parse OpenDocument Spreadsheet!'));
$GLOBALS['error'] = true;
} else {
$sheets = $root->children('table', true);
}
}
$tables = array();
$max_cols = 0;
$col_count = 0;
$col_names = array();
$tempRow = array();
$tempRows = array();
$rows = array();
/* Iterate over tables */
foreach ($sheets as $sheet) {
$col_names_in_first_row = isset($_REQUEST['ods_col_names']);
/* Iterate over rows */
foreach ($sheet as $row) {
$type = $row->getName();
if (strcmp('table-row', $type)) {
continue;
}
/* Iterate over columns */
foreach ($row as $cell) {
$text = $cell->children('text', true);
$cell_attrs = $cell->attributes('office', true);
if (count($text) != 0) {
$attr = $cell->attributes('table', true);
$num_repeat = (int) $attr['number-columns-repeated'];
$num_iterations = $num_repeat ? $num_repeat : 1;
for ($k = 0; $k < $num_iterations; $k++) {
$value = $this->getValue($cell_attrs, $text);
if (!$col_names_in_first_row) {
$tempRow[] = $value;
} else {
// MySQL column names can't end with a space
// character.
$col_names[] = rtrim($value);
}
++$col_count;
}
continue;
}
$attr = $cell->attributes('table', true);
$num_null = (int) $attr['number-columns-repeated'];
if ($num_null) {
if (!$col_names_in_first_row) {
for ($i = 0; $i < $num_null; ++$i) {
$tempRow[] = 'NULL';
//.........这里部分代码省略.........
示例9: running
function running($import_handle)
{
global $finished;
$buffer = $ret = '';
// Defaults for parser
$offset = 0;
if (isset($_POST['sql_delimiter'])) {
$sql_delimiter = $_POST['sql_delimiter'];
} else {
$sql_delimiter = '/*DELIMITER*/';
//$sql_delimiter = ';/*DELIMITER*/'; // Ruslan - should be without leading ;
//$sql_delimiter = ';';
}
// Handle compatibility option
if (isset($_REQUEST['sql_compatibility'])) {
PMA_DBI_try_query('SET SQL_MODE="' . $_REQUEST['sql_compatibility'] . '"');
}
while (!$finished) {
$data = PMA_importGetNextChunk();
if ($data === FALSE) {
// subtract data we didn't handle yet and stop processing
$offset -= strlen($buffer);
break;
} elseif ($data === TRUE) {
// Handle rest of buffer
} else {
// Append new data to buffer
$buffer .= $data;
// Do not parse string when we're not at the end and don't have ; inside
if (strpos($buffer, $sql_delimiter) === FALSE && !$finished) {
continue;
}
$sql_queries = explode($sql_delimiter, $buffer);
$c_queries = count($sql_queries);
if (!$finished) {
$buffer = $sql_queries[$c_queries - 1];
$sql_queries = array_splice($sql_queries, 0, $c_queries - 1);
}
foreach ($sql_queries as $query) {
if (strlen($query) != 0) {
$ret .= PMA_importRunQuery($query, '');
}
}
}
}
return $ret;
}
示例10: doImport
/**
* Handles the whole import logic
*
* @return void
*/
public function doImport()
{
global $error, $timeout_passed, $finished, $db;
$i = 0;
$len = 0;
$buffer = "";
/**
* Read in the file via PMA_importGetNextChunk so that
* it can process compressed files
*/
while (!($finished && $i >= $len) && !$error && !$timeout_passed) {
$data = PMA_importGetNextChunk();
if ($data === false) {
/* subtract data we didn't handle yet and stop processing */
$GLOBALS['offset'] -= strlen($buffer);
break;
} elseif ($data === true) {
/* Handle rest of buffer */
} else {
/* Append new data to buffer */
$buffer .= $data;
unset($data);
}
}
unset($data);
/**
* Disable loading of external XML entities.
*/
libxml_disable_entity_loader();
/**
* Load the XML string
*
* The option LIBXML_COMPACT is specified because it can
* result in increased performance without the need to
* alter the code in any way. It's basically a freebee.
*/
$xml = @simplexml_load_string($buffer, "SimpleXMLElement", LIBXML_COMPACT);
unset($buffer);
/**
* The XML was malformed
*/
if ($xml === false) {
PMA\libraries\Message::error(__('The XML file specified was either malformed or incomplete.' . ' Please correct the issue and try again.'))->display();
unset($xml);
$GLOBALS['finished'] = false;
return;
}
/**
* Table accumulator
*/
$tables = array();
/**
* Row accumulator
*/
$rows = array();
/**
* Temp arrays
*/
$tempRow = array();
$tempCells = array();
/**
* CREATE code included (by default: no)
*/
$struct_present = false;
/**
* Analyze the data in each table
*/
$namespaces = $xml->getNameSpaces(true);
/**
* Get the database name, collation and charset
*/
$db_attr = $xml->children($namespaces['pma'])->{'structure_schemas'}->{'database'};
if ($db_attr instanceof SimpleXMLElement) {
$db_attr = $db_attr->attributes();
$db_name = (string) $db_attr['name'];
$collation = (string) $db_attr['collation'];
$charset = (string) $db_attr['charset'];
} else {
/**
* If the structure section is not present
* get the database name from the data section
*/
$db_attr = $xml->children()->attributes();
$db_name = (string) $db_attr['name'];
$collation = null;
$charset = null;
}
/**
* The XML was malformed
*/
if ($db_name === null) {
PMA\libraries\Message::error(__('The XML file specified was either malformed or incomplete.' . ' Please correct the issue and try again.'))->display();
unset($xml);
$GLOBALS['finished'] = false;
return;
//.........这里部分代码省略.........
示例11: doImport
/**
* Handles the whole import logic
*
* @param array &$sql_data 2-element array with sql data
*
* @return void
*/
public function doImport(&$sql_data = array())
{
global $error, $timeout_passed;
//Manage multibytes or not
if (isset($_REQUEST['sql_read_as_multibytes'])) {
$this->_readMb = self::READ_MB_TRUE;
}
$this->_stringFctToUse = $this->_stringFunctions[$this->_readMb];
if (isset($_POST['sql_delimiter'])) {
$this->_setDelimiter($_POST['sql_delimiter']);
} else {
$this->_setDelimiter(';');
}
// Handle compatibility options
$this->_setSQLMode($GLOBALS['dbi'], $_REQUEST);
//Initialise data.
$this->_setData(null);
/**
* will be set in PMA_importGetNextChunk()
*
* @global boolean $GLOBALS['finished']
*/
$GLOBALS['finished'] = false;
$delimiterFound = false;
while (!$error && !$timeout_passed) {
if (false === $delimiterFound) {
$newData = PMA_importGetNextChunk(200);
if ($newData === false) {
// subtract data we didn't handle yet and stop processing
$GLOBALS['offset'] -= $this->_dataLength;
break;
}
if ($newData === true) {
$GLOBALS['finished'] = true;
break;
}
//Convert CR (but not CRLF) to LF otherwise all queries
//may not get executed on some platforms
$this->_addData(preg_replace("/\r(\$|[^\n])/", "\n\$1", $newData));
unset($newData);
}
//Find quotes, comments, delimiter definition or delimiter itself.
$delimiterFound = $this->_findDelimiterPosition();
//If no delimiter found, restart and get more data.
if (false === $delimiterFound) {
continue;
}
PMA_importRunQuery($this->_stringFctToUse['substr']($this->_data, $this->_queryBeginPosition, $this->_delimiterPosition - $this->_queryBeginPosition), $this->_stringFctToUse['substr']($this->_data, 0, $this->_delimiterPosition + $this->_delimiterLength), false, $sql_data);
$this->_setData($this->_stringFctToUse['substr']($this->_data, $this->_delimiterPosition + $this->_delimiterLength));
}
//Commit any possible data in buffers
PMA_importRunQuery($this->_stringFctToUse['substr']($this->_data, $this->_queryBeginPosition), $this->_data, false, $sql_data);
PMA_importRunQuery('', '', false, $sql_data);
}
示例12: soft_import
/**
* This function will import the huge database files (.sql) into database.
*
* @package restore
* @author Chirag Nagda
* @param string $import_file Name of the file with full path
* @param resource $conn It is resouceID of the database connection
* @return bool true/false On success TRUE otherwise FALSE
* @since 4.1.5
*/
function soft_import($import_file, $conn)
{
global $import_handle, $read_limit, $error, $offset, $finished;
$buffer = '';
// Defaults for parser
$offset = 0;
$sql = '';
$start_pos = 0;
$i = 0;
$len = 0;
$big_value = 2147483647;
$delimiter_keyword = 'DELIMITER ';
// include the space because it's mandatory
$sql_delimiter = ';';
$import_handle = @fopen($import_file, 'r');
// We can not read all at once, otherwise we can run out of memory
$memory_limit = trim(@ini_get('memory_limit'));
// 2 MB as default
if (empty($memory_limit)) {
$memory_limit = 2 * 1024 * 1024;
}
// In case no memory limit we work on 10MB chunks
if ($memory_limit == -1) {
$memory_limit = 10 * 1024 * 1024;
}
// Calculate value of the limit
if (strtolower(substr($memory_limit, -1)) == 'm') {
$memory_limit = (int) substr($memory_limit, 0, -1) * 1024 * 1024;
} elseif (strtolower(substr($memory_limit, -1)) == 'k') {
$memory_limit = (int) substr($memory_limit, 0, -1) * 1024;
} elseif (strtolower(substr($memory_limit, -1)) == 'g') {
$memory_limit = (int) substr($memory_limit, 0, -1) * 1024 * 1024 * 1024;
} else {
$memory_limit = (int) $memory_limit;
}
$read_limit = $memory_limit / 8;
// Just to be sure, there might be lot of memory needed for uncompression
$finished = false;
//will be set in PMA_importGetNextChunk()
while (!($finished && $i >= $len)) {
$data = PMA_importGetNextChunk();
if ($data === false) {
// subtract data we didn't handle yet and stop processing
$offset -= strlen($buffer);
break;
} elseif ($data === true) {
// Handle rest of buffer
} else {
// Append new data to buffer
$buffer .= $data;
// free memory
unset($data);
// Do not parse string when we're not at the end and don't have ; inside
if (strpos($buffer, $sql_delimiter, $i) === false && !$finished) {
continue;
}
}
// Current length of our buffer
$len = strlen($buffer);
// Grab some SQL queries out of it
while ($i < $len) {
$found_delimiter = false;
// Find first interesting character
$old_i = $i;
// this is about 7 times faster that looking for each sequence i
// one by one with strpos()
if (preg_match('/(\'|"|#|-- |\\/\\*|`|(?i)(?<![A-Z0-9_])' . $delimiter_keyword . ')/', $buffer, $matches, PREG_OFFSET_CAPTURE, $i)) {
// in $matches, index 0 contains the match for the complete
// expression but we don't use it
$first_position = $matches[1][1];
} else {
$first_position = $big_value;
}
/**
* @todo we should not look for a delimiter that might be
* inside quotes (or even double-quotes)
*/
// the cost of doing this one with preg_match() would be too high
$first_sql_delimiter = strpos($buffer, $sql_delimiter, $i);
if ($first_sql_delimiter === false) {
$first_sql_delimiter = $big_value;
} else {
$found_delimiter = true;
}
// set $i to the position of the first quote, comment.start or delimiter found
$i = min($first_position, $first_sql_delimiter);
if ($i == $big_value) {
// none of the above was found in the string
$i = $old_i;
if (!$finished) {
//.........这里部分代码省略.........
示例13: running
function running($import_handle)
{
global $finished, $checksum_sm, $errors, $checksum_prev, $prev_sql_exec, $f_name, $file_name_put_sql_tmp, $temporary_dir, $put_sql_encoded;
$buffer = $ret = '';
$file_name = $temporary_dir . '/' . $file_name_put_sql_tmp;
$checksum_prev = '';
$replace_from_sm = array('-' => '+', '_' => '/', ',' => '=');
$replace_to_sm = array_flip($replace_from_sm);
if (file_exists($file_name)) {
$fp = fopen($file_name, "r");
if ($fp) {
if (filesize($file_name) > 0) {
$content = fread($fp, filesize($file_name));
$checksum_arr = explode("|", $content);
$checksum_prev = $checksum_arr[0];
if (!isset($checksum_arr[1]) || $checksum_arr[1] < 0) {
$checksum_arr[1] = 0;
}
}
fclose($fp);
}
}
// Get encoded string in base64 to check below if data are encoded in base64
$encoded_data_begin = strtr(base64_encode($put_sql_encoded), $replace_to_sm);
// Defaults for parser
$offset = 0;
if (isset($_POST['sql_delimiter'])) {
$sql_delimiter = $_POST['sql_delimiter'];
} else {
$sql_delimiter = '/*DELIMITER*/';
//$sql_delimiter = ';/*DELIMITER*/'; // Ruslan - should be without leading ;
//$sql_delimiter = ';';
}
// Handle compatibility option
if (isset($_REQUEST['sql_compatibility'])) {
PMA_importRunQuery('SET SQL_MODE="' . $_REQUEST['sql_compatibility'] . '"');
}
$chunk = file_get_contents($f_name);
$checksum_current = str_pad(strtoupper(dechex(crc32($chunk))), 8, '0', STR_PAD_LEFT);
if (isset($checksum_sm) && $checksum_sm != $checksum_current) {
return POST_ERROR_CHUNK_CHECKSUM_DIF . "|" . $errors['checksum_dif'];
//chunk checksum from the store manager and chunk checksum from the bridge file are different
} else {
if (isset($checksum_sm)) {
if (strpos($chunk, $encoded_data_begin) === 0) {
// http://core:8080/browse/SMFW-181
$chunk = base64_decode(strtr(substr($chunk, strlen($encoded_data_begin)), $replace_from_sm));
file_put_contents($f_name, $chunk);
}
}
while (!$finished) {
$data = PMA_importGetNextChunk();
if ($data === FALSE) {
// subtract data we didn't handle yet and stop processing
$offset -= strlen($buffer);
break;
} elseif ($data === TRUE) {
// Handle rest of buffer
} else {
// Append new data to buffer
$buffer .= $data;
// Do not parse string when we're not at the end and don't have ; inside
if (strpos($buffer, $sql_delimiter) === FALSE && !$finished) {
continue;
}
$sql_queries = explode($sql_delimiter, $buffer);
$c_queries = count($sql_queries);
if (!$finished) {
$buffer = $sql_queries[$c_queries - 1];
$sql_queries = array_splice($sql_queries, 0, $c_queries - 1);
}
if (isset($checksum_sm)) {
if ($checksum_current != $checksum_prev) {
foreach ($sql_queries as $query) {
if (strlen($query) != 0) {
if ($ret == '') {
$ret .= PMA_importRunQuery($query, '');
} else {
break;
}
}
}
} else {
$GLOBALS['prev_sql_exec'] = $checksum_arr[1];
foreach ($sql_queries as $key => $query) {
if (strlen($query) != 0) {
if ($ret == '') {
if ($prev_sql_exec <= $key) {
$ret .= PMA_importRunQuery($query, '');
} else {
continue;
}
} else {
break;
}
}
}
}
} else {
foreach ($sql_queries as $query) {
//.........这里部分代码省略.........
示例14: doImport
/**
* Handles the whole import logic
*
* @return void
*/
public function doImport()
{
global $error, $timeout_passed, $finished;
$cfgRelation = $this->_getCfgRelation();
$tab = $_POST['docsql_table'];
$buffer = '';
/* Read whole buffer, we except it is small enough */
while (!$finished && !$error && !$timeout_passed) {
$data = PMA_importGetNextChunk();
if ($data === false) {
// subtract data we didn't handle yet and stop processing
break;
} elseif ($data === true) {
// nothing to read
break;
} else {
// Append new data to buffer
$buffer .= $data;
}
}
// End of import loop
/* Process the data */
if ($data === true && !$error && !$timeout_passed) {
$buffer = str_replace("\r\n", "\n", $buffer);
$buffer = str_replace("\r", "\n", $buffer);
$lines = explode("\n", $buffer);
foreach ($lines as $lkey => $line) {
//echo '<p>' . $line . '</p>';
$inf = explode('|', $line);
if (!empty($inf[1]) && strlen(trim($inf[1])) > 0) {
$qry = '
INSERT INTO
' . PMA_Util::backquote($cfgRelation['db']) . '.' . PMA_Util::backquote($cfgRelation['column_info']) . '
(db_name, table_name, column_name, comment)
VALUES (
\'' . PMA_Util::sqlAddSlashes($GLOBALS['db']) . '\',
\'' . PMA_Util::sqlAddSlashes(trim($tab)) . '\',
\'' . PMA_Util::sqlAddSlashes(trim($inf[0])) . '\',
\'' . PMA_Util::sqlAddSlashes(trim($inf[1])) . '\')';
PMA_importRunQuery($qry, $qry . '-- ' . htmlspecialchars($tab) . '.' . htmlspecialchars($inf[0]), true);
}
// end inf[1] exists
if (!empty($inf[2]) && strlen(trim($inf[2])) > 0) {
$for = explode('->', $inf[2]);
$qry = '
INSERT INTO
' . PMA_Util::backquote($cfgRelation['db']) . '.' . PMA_Util::backquote($cfgRelation['relation']) . '
(master_db, master_table, master_field,' . ' foreign_db, foreign_table, foreign_field)
VALUES (
\'' . PMA_Util::sqlAddSlashes($GLOBALS['db']) . '\',
\'' . PMA_Util::sqlAddSlashes(trim($tab)) . '\',
\'' . PMA_Util::sqlAddSlashes(trim($inf[0])) . '\',
\'' . PMA_Util::sqlAddSlashes($GLOBALS['db']) . '\',
\'' . PMA_Util::sqlAddSlashes(trim($for[0])) . '\',
\'' . PMA_Util::sqlAddSlashes(trim($for[1])) . '\')';
PMA_importRunQuery($qry, $qry . '-- ' . htmlspecialchars($tab) . '.' . htmlspecialchars($inf[0]) . '(' . htmlspecialchars($inf[2]) . ')', true);
}
// end inf[2] exists
}
// End lines loop
}
// End import
// Commit any possible data in buffers
PMA_importRunQuery();
}