mirror of
https://github.com/atlanticbiomedical/portal-legacy.git
synced 2025-07-02 01:47:28 -04:00
initial commit
This commit is contained in:
41
html/phpmyad/libraries/import/README
Normal file
41
html/phpmyad/libraries/import/README
Normal file
@ -0,0 +1,41 @@
|
||||
This directory holds import plugins for phpMyAdmin. Plugin should
|
||||
basically look like following code. Official plugins need to have str*
|
||||
messages with their definition in language files, if you build some
|
||||
plugins for your use, you can use directly texts in plugin.
|
||||
|
||||
<?php
|
||||
// vim: expandtab sw=4 ts=4 sts=4 ft=php:
|
||||
|
||||
/* Demo import plugin for phpMyAdmin */
|
||||
|
||||
if (isset($plugin_list)) {
|
||||
$plugin_list['name'] = array( // set name of your plugin
|
||||
'text' => 'strName', // text to be displayed as choice
|
||||
'extension' => '', // extension this plugin can handle
|
||||
'options' => array( // array of options for your plugin (optional)
|
||||
array('type' => '', 'name' => '', 'text' => ''), // type: bool or text, name: form element name, text: description in GUI, size: size of text element (optional). len: maximal size of input (optional)
|
||||
),
|
||||
'options_text' => 'strNameImportOptions', // text to describe plugin options (must be set if options are used)
|
||||
);
|
||||
} else {
|
||||
/* We do not define function when plugin is just queried for information above */
|
||||
$buffer = '';
|
||||
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;
|
||||
}
|
||||
// PARSE $buffer here, post sql queries using:
|
||||
PMA_importRunQuery($sql, $verbose_sql_with_comments);
|
||||
} // End of import loop
|
||||
// Commit any possible data in buffers
|
||||
PMA_importRunQuery();
|
||||
}
|
||||
?>
|
455
html/phpmyad/libraries/import/csv.php
Normal file
455
html/phpmyad/libraries/import/csv.php
Normal file
@ -0,0 +1,455 @@
|
||||
<?php
|
||||
/* vim: set expandtab sw=4 ts=4 sts=4: */
|
||||
/**
|
||||
* CSV import plugin for phpMyAdmin
|
||||
*
|
||||
* @todo add an option for handling NULL values
|
||||
* @package PhpMyAdmin-Import
|
||||
* @subpackage CSV
|
||||
*/
|
||||
if (! defined('PHPMYADMIN')) {
|
||||
exit;
|
||||
}
|
||||
|
||||
$analyze = false;
|
||||
|
||||
if ($plugin_param !== 'table') {
|
||||
$analyze = true;
|
||||
}
|
||||
|
||||
if (isset($plugin_list)) {
|
||||
$plugin_list['csv'] = array(
|
||||
'text' => __('CSV'),
|
||||
'extension' => 'csv',
|
||||
'options' => array(
|
||||
array('type' => 'begin_group', 'name' => 'general_opts'),
|
||||
array('type' => 'bool', 'name' => 'replace', 'text' => __('Replace table data with file')),
|
||||
array('type' => 'bool', 'name' => 'ignore', 'text' => __('Do not abort on INSERT error')),
|
||||
array('type' => 'text', 'name' => 'terminated', 'text' => __('Columns separated with:'), 'size' => 2, 'len' => 2),
|
||||
array('type' => 'text', 'name' => 'enclosed', 'text' => __('Columns enclosed with:'), 'size' => 2, 'len' => 2),
|
||||
array('type' => 'text', 'name' => 'escaped', 'text' => __('Columns escaped with:'), 'size' => 2, 'len' => 2),
|
||||
array('type' => 'text', 'name' => 'new_line', 'text' => __('Lines terminated with:'), 'size' => 2),
|
||||
),
|
||||
'options_text' => __('Options'),
|
||||
);
|
||||
|
||||
if ($plugin_param !== 'table') {
|
||||
$plugin_list['csv']['options'][]
|
||||
= 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>'));
|
||||
} else {
|
||||
$hint = new PMA_Message(__('If the data in each row of the file is not in the same order as in the database, list the corresponding column names here. Column names must be separated by commas and not enclosed in quotations.'));
|
||||
$plugin_list['csv']['options'][]
|
||||
= array('type' => 'text', 'name' => 'columns', 'text' => __('Column names: ') . PMA_showHint($hint));
|
||||
}
|
||||
$plugin_list['csv']['options'][] = array('type' => 'end_group');
|
||||
|
||||
/* We do not define function when plugin is just queried for information above */
|
||||
return;
|
||||
}
|
||||
|
||||
$replacements = array(
|
||||
'\\n' => "\n",
|
||||
'\\t' => "\t",
|
||||
'\\r' => "\r",
|
||||
);
|
||||
$csv_terminated = strtr($csv_terminated, $replacements);
|
||||
$csv_enclosed = strtr($csv_enclosed, $replacements);
|
||||
$csv_escaped = strtr($csv_escaped, $replacements);
|
||||
$csv_new_line = strtr($csv_new_line, $replacements);
|
||||
|
||||
$param_error = false;
|
||||
if (strlen($csv_terminated) != 1) {
|
||||
$message = PMA_Message::error(__('Invalid parameter for CSV import: %s'));
|
||||
$message->addParam(__('Columns terminated by'), false);
|
||||
$error = true;
|
||||
$param_error = true;
|
||||
// The default dialog of MS Excel when generating a CSV produces a
|
||||
// semi-colon-separated file with no chance of specifying the
|
||||
// enclosing character. Thus, users who want to import this file
|
||||
// tend to remove the enclosing character on the Import dialog.
|
||||
// I could not find a test case where having no enclosing characters
|
||||
// confuses this script.
|
||||
// But the parser won't work correctly with strings so we allow just
|
||||
// one character.
|
||||
} elseif (strlen($csv_enclosed) > 1) {
|
||||
$message = PMA_Message::error(__('Invalid parameter for CSV import: %s'));
|
||||
$message->addParam(__('Columns enclosed by'), false);
|
||||
$error = true;
|
||||
$param_error = true;
|
||||
} elseif (strlen($csv_escaped) != 1) {
|
||||
$message = PMA_Message::error(__('Invalid parameter for CSV import: %s'));
|
||||
$message->addParam(__('Columns escaped by'), false);
|
||||
$error = true;
|
||||
$param_error = true;
|
||||
} elseif (strlen($csv_new_line) != 1 && $csv_new_line != 'auto') {
|
||||
$message = PMA_Message::error(__('Invalid parameter for CSV import: %s'));
|
||||
$message->addParam(__('Lines terminated by'), false);
|
||||
$error = true;
|
||||
$param_error = true;
|
||||
}
|
||||
|
||||
// If there is an error in the parameters entered, indicate that immediately.
|
||||
if ($param_error) {
|
||||
PMA_mysqlDie($message->getMessage(), '', '', $err_url);
|
||||
}
|
||||
|
||||
$buffer = '';
|
||||
$required_fields = 0;
|
||||
|
||||
if (!$analyze) {
|
||||
if (isset($csv_replace)) {
|
||||
$sql_template = 'REPLACE';
|
||||
} else {
|
||||
$sql_template = 'INSERT';
|
||||
if (isset($csv_ignore)) {
|
||||
$sql_template .= ' IGNORE';
|
||||
}
|
||||
}
|
||||
$sql_template .= ' INTO ' . PMA_backquote($table);
|
||||
|
||||
$tmp_fields = PMA_DBI_get_columns($db, $table);
|
||||
|
||||
if (empty($csv_columns)) {
|
||||
$fields = $tmp_fields;
|
||||
} else {
|
||||
$sql_template .= ' (';
|
||||
$fields = array();
|
||||
$tmp = preg_split('/,( ?)/', $csv_columns);
|
||||
foreach ($tmp as $key => $val) {
|
||||
if (count($fields) > 0) {
|
||||
$sql_template .= ', ';
|
||||
}
|
||||
/* Trim also `, if user already included backquoted fields */
|
||||
$val = trim($val, " \t\r\n\0\x0B`");
|
||||
$found = false;
|
||||
foreach ($tmp_fields as $id => $field) {
|
||||
if ($field['Field'] == $val) {
|
||||
$found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!$found) {
|
||||
$message = PMA_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_backquote($val);
|
||||
}
|
||||
$sql_template .= ') ';
|
||||
}
|
||||
|
||||
$required_fields = count($fields);
|
||||
|
||||
$sql_template .= ' VALUES (';
|
||||
}
|
||||
|
||||
// Defaults for parser
|
||||
$i = 0;
|
||||
$len = 0;
|
||||
$line = 1;
|
||||
$lasti = -1;
|
||||
$values = array();
|
||||
$csv_finish = false;
|
||||
|
||||
$tempRow = array();
|
||||
$rows = array();
|
||||
$col_names = array();
|
||||
$tables = array();
|
||||
|
||||
$col_count = 0;
|
||||
$max_cols = 0;
|
||||
|
||||
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);
|
||||
// Do not parse string when we're not at the end and don't have new line inside
|
||||
if (($csv_new_line == 'auto' && strpos($buffer, "\r") === false && strpos($buffer, "\n") === false)
|
||||
|| ($csv_new_line != 'auto' && strpos($buffer, $csv_new_line) === false)
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// Current length of our buffer
|
||||
$len = strlen($buffer);
|
||||
// Currently parsed char
|
||||
$ch = $buffer[$i];
|
||||
while ($i < $len) {
|
||||
// Deadlock protection
|
||||
if ($lasti == $i && $lastlen == $len) {
|
||||
$message = PMA_Message::error(__('Invalid format of CSV input on line %d.'));
|
||||
$message->addParam($line);
|
||||
$error = true;
|
||||
break;
|
||||
}
|
||||
$lasti = $i;
|
||||
$lastlen = $len;
|
||||
|
||||
// This can happen with auto EOL and \r at the end of buffer
|
||||
if (!$csv_finish) {
|
||||
// Grab empty field
|
||||
if ($ch == $csv_terminated) {
|
||||
if ($i == $len - 1) {
|
||||
break;
|
||||
}
|
||||
$values[] = '';
|
||||
$i++;
|
||||
$ch = $buffer[$i];
|
||||
continue;
|
||||
}
|
||||
|
||||
// Grab one field
|
||||
$fallbacki = $i;
|
||||
if ($ch == $csv_enclosed) {
|
||||
if ($i == $len - 1) {
|
||||
break;
|
||||
}
|
||||
$need_end = true;
|
||||
$i++;
|
||||
$ch = $buffer[$i];
|
||||
} else {
|
||||
$need_end = false;
|
||||
}
|
||||
$fail = false;
|
||||
$value = '';
|
||||
while (($need_end && ($ch != $csv_enclosed || $csv_enclosed == $csv_escaped))
|
||||
|| (!$need_end && !($ch == $csv_terminated
|
||||
|| $ch == $csv_new_line || ($csv_new_line == 'auto'
|
||||
&& ($ch == "\r" || $ch == "\n"))))) {
|
||||
if ($ch == $csv_escaped) {
|
||||
if ($i == $len - 1) {
|
||||
$fail = true;
|
||||
break;
|
||||
}
|
||||
$i++;
|
||||
$ch = $buffer[$i];
|
||||
if ($csv_enclosed == $csv_escaped && ($ch == $csv_terminated
|
||||
|| $ch == $csv_new_line || ($csv_new_line == 'auto'
|
||||
&& ($ch == "\r" || $ch == "\n")))) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
$value .= $ch;
|
||||
if ($i == $len - 1) {
|
||||
if (!$finished) {
|
||||
$fail = true;
|
||||
}
|
||||
break;
|
||||
}
|
||||
$i++;
|
||||
$ch = $buffer[$i];
|
||||
}
|
||||
|
||||
// unquoted NULL string
|
||||
if (false === $need_end && $value === 'NULL') {
|
||||
$value = null;
|
||||
}
|
||||
|
||||
if ($fail) {
|
||||
$i = $fallbacki;
|
||||
$ch = $buffer[$i];
|
||||
break;
|
||||
}
|
||||
// Need to strip trailing enclosing char?
|
||||
if ($need_end && $ch == $csv_enclosed) {
|
||||
if ($finished && $i == $len - 1) {
|
||||
$ch = null;
|
||||
} elseif ($i == $len - 1) {
|
||||
$i = $fallbacki;
|
||||
$ch = $buffer[$i];
|
||||
break;
|
||||
} else {
|
||||
$i++;
|
||||
$ch = $buffer[$i];
|
||||
}
|
||||
}
|
||||
// Are we at the end?
|
||||
if ($ch == $csv_new_line
|
||||
|| ($csv_new_line == 'auto' && ($ch == "\r" || $ch == "\n"))
|
||||
|| ($finished && $i == $len - 1)
|
||||
) {
|
||||
$csv_finish = true;
|
||||
}
|
||||
// Go to next char
|
||||
if ($ch == $csv_terminated) {
|
||||
if ($i == $len - 1) {
|
||||
$i = $fallbacki;
|
||||
$ch = $buffer[$i];
|
||||
break;
|
||||
}
|
||||
$i++;
|
||||
$ch = $buffer[$i];
|
||||
}
|
||||
// If everything went okay, store value
|
||||
$values[] = $value;
|
||||
}
|
||||
|
||||
// End of line
|
||||
if ($csv_finish
|
||||
|| $ch == $csv_new_line
|
||||
|| ($csv_new_line == 'auto' && ($ch == "\r" || $ch == "\n"))
|
||||
) {
|
||||
if ($csv_new_line == 'auto' && $ch == "\r") { // Handle "\r\n"
|
||||
if ($i >= ($len - 2) && !$finished) {
|
||||
break; // We need more data to decide new line
|
||||
}
|
||||
if ($buffer[$i + 1] == "\n") {
|
||||
$i++;
|
||||
}
|
||||
}
|
||||
// We didn't parse value till the end of line, so there was empty one
|
||||
if (!$csv_finish) {
|
||||
$values[] = '';
|
||||
}
|
||||
|
||||
if ($analyze) {
|
||||
foreach ($values as $ley => $val) {
|
||||
$tempRow[] = $val;
|
||||
++$col_count;
|
||||
}
|
||||
|
||||
if ($col_count > $max_cols) {
|
||||
$max_cols = $col_count;
|
||||
}
|
||||
$col_count = 0;
|
||||
|
||||
$rows[] = $tempRow;
|
||||
$tempRow = array();
|
||||
} else {
|
||||
// Do we have correct count of values?
|
||||
if (count($values) != $required_fields) {
|
||||
|
||||
// Hack for excel
|
||||
if ($values[count($values) - 1] == ';') {
|
||||
unset($values[count($values) - 1]);
|
||||
} else {
|
||||
$message = PMA_Message::error(__('Invalid column count in CSV input on line %d.'));
|
||||
$message->addParam($line);
|
||||
$error = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$first = true;
|
||||
$sql = $sql_template;
|
||||
foreach ($values as $key => $val) {
|
||||
if (!$first) {
|
||||
$sql .= ', ';
|
||||
}
|
||||
if ($val === null) {
|
||||
$sql .= 'NULL';
|
||||
} else {
|
||||
$sql .= '\'' . PMA_sqlAddSlashes($val) . '\'';
|
||||
}
|
||||
|
||||
$first = false;
|
||||
}
|
||||
$sql .= ')';
|
||||
|
||||
/**
|
||||
* @todo maybe we could add original line to verbose SQL in comment
|
||||
*/
|
||||
PMA_importRunQuery($sql, $sql);
|
||||
}
|
||||
|
||||
$line++;
|
||||
$csv_finish = false;
|
||||
$values = array();
|
||||
$buffer = substr($buffer, $i + 1);
|
||||
$len = strlen($buffer);
|
||||
$i = 0;
|
||||
$lasti = -1;
|
||||
$ch = $buffer[0];
|
||||
}
|
||||
} // End of parser loop
|
||||
} // End of import loop
|
||||
|
||||
if ($analyze) {
|
||||
/* Fill out all rows */
|
||||
$num_rows = count($rows);
|
||||
for ($i = 0; $i < $num_rows; ++$i) {
|
||||
for ($j = count($rows[$i]); $j < $max_cols; ++$j) {
|
||||
$rows[$i][] = 'NULL';
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($_REQUEST['csv_col_names'])) {
|
||||
$col_names = array_splice($rows, 0, 1);
|
||||
$col_names = $col_names[0];
|
||||
}
|
||||
|
||||
if ((isset($col_names) && count($col_names) != $max_cols)
|
||||
|| ! isset($col_names)
|
||||
) {
|
||||
// Fill out column names
|
||||
for ($i = 0; $i < $max_cols; ++$i) {
|
||||
$col_names[] = 'COL '.($i+1);
|
||||
}
|
||||
}
|
||||
|
||||
if (strlen($db)) {
|
||||
$result = PMA_DBI_fetch_result('SHOW TABLES');
|
||||
$tbl_name = 'TABLE '.(count($result) + 1);
|
||||
} else {
|
||||
$tbl_name = 'TBL_NAME';
|
||||
}
|
||||
|
||||
$tables[] = array($tbl_name, $col_names, $rows);
|
||||
|
||||
/* Obtain the best-fit MySQL types for each column */
|
||||
$analyses = array();
|
||||
$analyses[] = PMA_analyzeTable($tables[0]);
|
||||
|
||||
/**
|
||||
* string $db_name (no backquotes)
|
||||
*
|
||||
* array $table = array(table_name, array() column_names, array()() rows)
|
||||
* array $tables = array of "$table"s
|
||||
*
|
||||
* array $analysis = array(array() column_types, array() column_sizes)
|
||||
* array $analyses = array of "$analysis"s
|
||||
*
|
||||
* array $create = array of SQL strings
|
||||
*
|
||||
* array $options = an associative array of options
|
||||
*/
|
||||
|
||||
/* Set database name to the currently selected one, if applicable */
|
||||
if (strlen($db)) {
|
||||
$db_name = $db;
|
||||
$options = array('create_db' => false);
|
||||
} else {
|
||||
$db_name = 'CSV_DB';
|
||||
$options = null;
|
||||
}
|
||||
|
||||
/* Non-applicable parameters */
|
||||
$create = null;
|
||||
|
||||
/* Created and execute necessary SQL statements from data */
|
||||
PMA_buildSQL($db_name, $tables, $analyses, $create, $options);
|
||||
|
||||
unset($tables);
|
||||
unset($analyses);
|
||||
}
|
||||
|
||||
// Commit any possible data in buffers
|
||||
PMA_importRunQuery();
|
||||
|
||||
if (count($values) != 0 && !$error) {
|
||||
$message = PMA_Message::error(__('Invalid format of CSV input on line %d.'));
|
||||
$message->addParam($line);
|
||||
$error = true;
|
||||
}
|
||||
?>
|
96
html/phpmyad/libraries/import/docsql.php
Normal file
96
html/phpmyad/libraries/import/docsql.php
Normal file
@ -0,0 +1,96 @@
|
||||
<?php
|
||||
/* vim: set expandtab sw=4 ts=4 sts=4: */
|
||||
/**
|
||||
* DocSQL import plugin for phpMyAdmin
|
||||
*
|
||||
* @package PhpMyAdmin-Import
|
||||
* @subpackage DocSQL
|
||||
*/
|
||||
if (! defined('PHPMYADMIN')) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load relations.
|
||||
*/
|
||||
$cfgRelation = PMA_getRelationsParam();
|
||||
|
||||
/**
|
||||
* We need relations enabled and we work only on database
|
||||
*/
|
||||
if ($plugin_param !== 'database' || $GLOBALS['num_tables'] < 1
|
||||
|| ! $cfgRelation['relwork'] || ! $cfgRelation['commwork']) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (isset($plugin_list)) {
|
||||
$plugin_list['docsql'] = array( // set name of your plugin
|
||||
'text' => __('DocSQL'), // text to be displayed as choice
|
||||
'extension' => '', // extension this plugin can handle
|
||||
'options' => array( // array of options for your plugin (optional)
|
||||
array('type' => 'begin_group', 'name' => 'general_opts'),
|
||||
array('type' => 'text', 'name' => 'table', 'text' => __('Table name')),
|
||||
array('type' => 'end_group')
|
||||
),
|
||||
'options_text' => __('Options'), // text to describe plugin options (must be set if options are used)
|
||||
);
|
||||
/* We do not define function when plugin is just queried for information above */
|
||||
return;
|
||||
}
|
||||
|
||||
$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_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['column_info']) . '
|
||||
(db_name, table_name, column_name, comment)
|
||||
VALUES (
|
||||
\'' . PMA_sqlAddSlashes($GLOBALS['db']) . '\',
|
||||
\'' . PMA_sqlAddSlashes(trim($tab)) . '\',
|
||||
\'' . PMA_sqlAddSlashes(trim($inf[0])) . '\',
|
||||
\'' . PMA_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_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['relation']) . '
|
||||
(master_db, master_table, master_field, foreign_db, foreign_table, foreign_field)
|
||||
VALUES (
|
||||
\'' . PMA_sqlAddSlashes($GLOBALS['db']) . '\',
|
||||
\'' . PMA_sqlAddSlashes(trim($tab)) . '\',
|
||||
\'' . PMA_sqlAddSlashes(trim($inf[0])) . '\',
|
||||
\'' . PMA_sqlAddSlashes($GLOBALS['db']) . '\',
|
||||
\'' . PMA_sqlAddSlashes(trim($for[0])) . '\',
|
||||
\'' . PMA_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();
|
||||
?>
|
110
html/phpmyad/libraries/import/ldi.php
Normal file
110
html/phpmyad/libraries/import/ldi.php
Normal file
@ -0,0 +1,110 @@
|
||||
<?php
|
||||
/* vim: set expandtab sw=4 ts=4 sts=4: */
|
||||
/**
|
||||
* CSV import plugin for phpMyAdmin using LOAD DATA
|
||||
*
|
||||
* @package PhpMyAdmin-Import
|
||||
* @subpackage LDI
|
||||
*/
|
||||
if (! defined('PHPMYADMIN')) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
if ($plugin_param !== 'table') {
|
||||
return;
|
||||
}
|
||||
|
||||
if (isset($plugin_list)) {
|
||||
if ($GLOBALS['cfg']['Import']['ldi_local_option'] == 'auto') {
|
||||
$GLOBALS['cfg']['Import']['ldi_local_option'] = false;
|
||||
|
||||
$result = PMA_DBI_try_query('SHOW VARIABLES LIKE \'local\\_infile\';');
|
||||
if ($result != false && PMA_DBI_num_rows($result) > 0) {
|
||||
$tmp = PMA_DBI_fetch_row($result);
|
||||
if ($tmp[1] == 'ON') {
|
||||
$GLOBALS['cfg']['Import']['ldi_local_option'] = true;
|
||||
}
|
||||
}
|
||||
PMA_DBI_free_result($result);
|
||||
unset($result);
|
||||
}
|
||||
$plugin_list['ldi'] = array(
|
||||
'text' => __('CSV using LOAD DATA'),
|
||||
'extension' => 'ldi', // This is nonsense, however we want to default to our parser for csv
|
||||
'options' => array(
|
||||
array('type' => 'begin_group', 'name' => 'general_opts'),
|
||||
array('type' => 'bool', 'name' => 'replace', 'text' => __('Replace table data with file')),
|
||||
array('type' => 'bool', 'name' => 'ignore', 'text' => __('Do not abort on INSERT error')),
|
||||
array('type' => 'text', 'name' => 'terminated', 'text' => __('Columns terminated by'), 'size' => 2, 'len' => 2),
|
||||
array('type' => 'text', 'name' => 'enclosed', 'text' => __('Columns enclosed by'), 'size' => 2, 'len' => 2),
|
||||
array('type' => 'text', 'name' => 'escaped', 'text' => __('Columns escaped by'), 'size' => 2, 'len' => 2),
|
||||
array('type' => 'text', 'name' => 'new_line', 'text' => __('Lines terminated by'), 'size' => 2),
|
||||
array('type' => 'text', 'name' => 'columns', 'text' => __('Column names')),
|
||||
array('type' => 'bool', 'name' => 'local_option', 'text' => __('Use LOCAL keyword')),
|
||||
array('type' => 'end_group')
|
||||
),
|
||||
'options_text' => __('Options'),
|
||||
);
|
||||
/* We do not define function when plugin is just queried for information above */
|
||||
return;
|
||||
}
|
||||
|
||||
if ($import_file == 'none' || $compression != 'none' || $charset_conversion) {
|
||||
// We handle only some kind of data!
|
||||
$message = PMA_Message::error(__('This plugin does not support compressed imports!'));
|
||||
$error = true;
|
||||
return;
|
||||
}
|
||||
|
||||
$sql = 'LOAD DATA';
|
||||
if (isset($ldi_local_option)) {
|
||||
$sql .= ' LOCAL';
|
||||
}
|
||||
$sql .= ' INFILE \'' . PMA_sqlAddSlashes($import_file) . '\'';
|
||||
if (isset($ldi_replace)) {
|
||||
$sql .= ' REPLACE';
|
||||
} elseif (isset($ldi_ignore)) {
|
||||
$sql .= ' IGNORE';
|
||||
}
|
||||
$sql .= ' INTO TABLE ' . PMA_backquote($table);
|
||||
|
||||
if (strlen($ldi_terminated) > 0) {
|
||||
$sql .= ' FIELDS TERMINATED BY \'' . $ldi_terminated . '\'';
|
||||
}
|
||||
if (strlen($ldi_enclosed) > 0) {
|
||||
$sql .= ' ENCLOSED BY \'' . PMA_sqlAddSlashes($ldi_enclosed) . '\'';
|
||||
}
|
||||
if (strlen($ldi_escaped) > 0) {
|
||||
$sql .= ' ESCAPED BY \'' . PMA_sqlAddSlashes($ldi_escaped) . '\'';
|
||||
}
|
||||
if (strlen($ldi_new_line) > 0) {
|
||||
if ($ldi_new_line == 'auto') {
|
||||
$ldi_new_line = PMA_whichCrlf() == "\n" ? '\n' : '\r\n';
|
||||
}
|
||||
$sql .= ' LINES TERMINATED BY \'' . $ldi_new_line . '\'';
|
||||
}
|
||||
if ($skip_queries > 0) {
|
||||
$sql .= ' IGNORE ' . $skip_queries . ' LINES';
|
||||
$skip_queries = 0;
|
||||
}
|
||||
if (strlen($ldi_columns) > 0) {
|
||||
$sql .= ' (';
|
||||
$tmp = preg_split('/,( ?)/', $ldi_columns);
|
||||
$cnt_tmp = count($tmp);
|
||||
for ($i = 0; $i < $cnt_tmp; $i++) {
|
||||
if ($i > 0) {
|
||||
$sql .= ', ';
|
||||
}
|
||||
/* Trim also `, if user already included backquoted fields */
|
||||
$sql .= PMA_backquote(trim($tmp[$i], " \t\r\n\0\x0B`"));
|
||||
} // end for
|
||||
$sql .= ')';
|
||||
}
|
||||
|
||||
PMA_importRunQuery($sql, $sql);
|
||||
PMA_importRunQuery();
|
||||
$finished = true;
|
||||
?>
|
306
html/phpmyad/libraries/import/ods.php
Normal file
306
html/phpmyad/libraries/import/ods.php
Normal file
@ -0,0 +1,306 @@
|
||||
<?php
|
||||
/* vim: set expandtab sw=4 ts=4 sts=4: */
|
||||
/**
|
||||
* OpenDocument Spreadsheet import plugin for phpMyAdmin
|
||||
*
|
||||
* @todo Pretty much everything
|
||||
* @todo Importing of accented characters seems to fail
|
||||
* @package PhpMyAdmin-Import
|
||||
* @subpackage ODS
|
||||
*/
|
||||
|
||||
if (! defined('PHPMYADMIN')) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* We need way to disable external XML entities processing.
|
||||
*/
|
||||
if (!function_exists('libxml_disable_entity_loader')) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* The possible scopes for $plugin_param are: 'table', 'database', and 'server'
|
||||
*/
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
$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.
|
||||
*/
|
||||
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();
|
||||
$message = PMA_Message::error(__('The XML file specified was either malformed or incomplete. Please correct the issue and try again.'));
|
||||
$error = true;
|
||||
} else {
|
||||
$sheets = $xml->children('office', true)->{'body'}->{'spreadsheet'}->children('table', true);
|
||||
}
|
||||
|
||||
$tables = array();
|
||||
|
||||
$max_cols = 0;
|
||||
|
||||
$row_count = 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)) {
|
||||
/* 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++) {
|
||||
if (! $col_names_in_first_row) {
|
||||
if ($_REQUEST['ods_recognize_percentages'] && !strcmp('percentage', $cell_attrs['value-type'])) {
|
||||
$tempRow[] = (double)$cell_attrs['value'];
|
||||
} elseif ($_REQUEST['ods_recognize_currency'] && !strcmp('currency', $cell_attrs['value-type'])) {
|
||||
$tempRow[] = (double)$cell_attrs['value'];
|
||||
} else {
|
||||
$tempRow[] = (string)$text;
|
||||
}
|
||||
} else {
|
||||
if ($_REQUEST['ods_recognize_percentages'] && !strcmp('percentage', $cell_attrs['value-type'])) {
|
||||
$col_names[] = (double)$cell_attrs['value'];
|
||||
} else if ($_REQUEST['ods_recognize_currency'] && !strcmp('currency', $cell_attrs['value-type'])) {
|
||||
$col_names[] = (double)$cell_attrs['value'];
|
||||
} else {
|
||||
$col_names[] = (string)$text;
|
||||
}
|
||||
}
|
||||
|
||||
++$col_count;
|
||||
}
|
||||
} else {
|
||||
/* Number of blank columns repeated */
|
||||
if ($col_count < count($row->children('table', true)) - 1) {
|
||||
$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';
|
||||
++$col_count;
|
||||
}
|
||||
} else {
|
||||
for ($i = 0; $i < $num_null; ++$i) {
|
||||
$col_names[] = PMA_getColumnAlphaName($col_count + 1);
|
||||
++$col_count;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (! $col_names_in_first_row) {
|
||||
$tempRow[] = 'NULL';
|
||||
} else {
|
||||
$col_names[] = PMA_getColumnAlphaName($col_count + 1);
|
||||
}
|
||||
|
||||
++$col_count;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Find the widest row */
|
||||
if ($col_count > $max_cols) {
|
||||
$max_cols = $col_count;
|
||||
}
|
||||
|
||||
/* Don't include a row that is full of NULL values */
|
||||
if (! $col_names_in_first_row) {
|
||||
if ($_REQUEST['ods_empty_rows']) {
|
||||
foreach ($tempRow as $cell) {
|
||||
if (strcmp('NULL', $cell)) {
|
||||
$tempRows[] = $tempRow;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$tempRows[] = $tempRow;
|
||||
}
|
||||
}
|
||||
|
||||
$col_count = 0;
|
||||
$col_names_in_first_row = false;
|
||||
$tempRow = array();
|
||||
}
|
||||
}
|
||||
|
||||
/* Skip over empty sheets */
|
||||
if (count($tempRows) == 0 || count($tempRows[0]) == 0) {
|
||||
$col_names = array();
|
||||
$tempRow = array();
|
||||
$tempRows = array();
|
||||
continue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fill out each row as necessary to make
|
||||
* every one exactly as wide as the widest
|
||||
* row. This included column names.
|
||||
*/
|
||||
|
||||
/* Fill out column names */
|
||||
for ($i = count($col_names); $i < $max_cols; ++$i) {
|
||||
$col_names[] = PMA_getColumnAlphaName($i + 1);
|
||||
}
|
||||
|
||||
/* Fill out all rows */
|
||||
$num_rows = count($tempRows);
|
||||
for ($i = 0; $i < $num_rows; ++$i) {
|
||||
for ($j = count($tempRows[$i]); $j < $max_cols; ++$j) {
|
||||
$tempRows[$i][] = 'NULL';
|
||||
}
|
||||
}
|
||||
|
||||
/* Store the table name so we know where to place the row set */
|
||||
$tbl_attr = $sheet->attributes('table', true);
|
||||
$tables[] = array((string)$tbl_attr['name']);
|
||||
|
||||
/* Store the current sheet in the accumulator */
|
||||
$rows[] = array((string)$tbl_attr['name'], $col_names, $tempRows);
|
||||
$tempRows = array();
|
||||
$col_names = array();
|
||||
$max_cols = 0;
|
||||
}
|
||||
|
||||
unset($tempRow);
|
||||
unset($tempRows);
|
||||
unset($col_names);
|
||||
unset($sheets);
|
||||
unset($xml);
|
||||
|
||||
/**
|
||||
* Bring accumulated rows into the corresponding table
|
||||
*/
|
||||
$num_tbls = count($tables);
|
||||
for ($i = 0; $i < $num_tbls; ++$i) {
|
||||
for ($j = 0; $j < count($rows); ++$j) {
|
||||
if (! strcmp($tables[$i][TBL_NAME], $rows[$j][TBL_NAME])) {
|
||||
if (! isset($tables[$i][COL_NAMES])) {
|
||||
$tables[$i][] = $rows[$j][COL_NAMES];
|
||||
}
|
||||
|
||||
$tables[$i][ROWS] = $rows[$j][ROWS];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* No longer needed */
|
||||
unset($rows);
|
||||
|
||||
/* Obtain the best-fit MySQL types for each column */
|
||||
$analyses = array();
|
||||
|
||||
$len = count($tables);
|
||||
for ($i = 0; $i < $len; ++$i) {
|
||||
$analyses[] = PMA_analyzeTable($tables[$i]);
|
||||
}
|
||||
|
||||
/**
|
||||
* string $db_name (no backquotes)
|
||||
*
|
||||
* array $table = array(table_name, array() column_names, array()() rows)
|
||||
* array $tables = array of "$table"s
|
||||
*
|
||||
* array $analysis = array(array() column_types, array() column_sizes)
|
||||
* array $analyses = array of "$analysis"s
|
||||
*
|
||||
* array $create = array of SQL strings
|
||||
*
|
||||
* array $options = an associative array of options
|
||||
*/
|
||||
|
||||
/* Set database name to the currently selected one, if applicable */
|
||||
if (strlen($db)) {
|
||||
$db_name = $db;
|
||||
$options = array('create_db' => false);
|
||||
} else {
|
||||
$db_name = 'ODS_DB';
|
||||
$options = null;
|
||||
}
|
||||
|
||||
/* Non-applicable parameters */
|
||||
$create = null;
|
||||
|
||||
/* Created and execute necessary SQL statements from data */
|
||||
PMA_buildSQL($db_name, $tables, $analyses, $create, $options);
|
||||
|
||||
unset($tables);
|
||||
unset($analyses);
|
||||
|
||||
/* Commit any possible data in buffers */
|
||||
PMA_importRunQuery();
|
||||
?>
|
426
html/phpmyad/libraries/import/shp.php
Normal file
426
html/phpmyad/libraries/import/shp.php
Normal file
@ -0,0 +1,426 @@
|
||||
<?php
|
||||
/**
|
||||
* ESRI Shape file import plugin for phpMyAdmin
|
||||
*
|
||||
* @package PhpMyAdmin-Import
|
||||
* @subpackage ESRI_Shape
|
||||
*/
|
||||
if (! defined('PHPMYADMIN')) {
|
||||
exit;
|
||||
}
|
||||
|
||||
// Drizzle does not suppost GIS data types
|
||||
if (PMA_DRIZZLE) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (isset($plugin_list)) {
|
||||
$plugin_list['shp'] = array(
|
||||
'text' => __('ESRI Shape File'),
|
||||
'extension' => 'shp',
|
||||
'options' => array(),
|
||||
'options_text' => __('Options'),
|
||||
);
|
||||
} else {
|
||||
|
||||
if ((int) ini_get('memory_limit') < 512) {
|
||||
@ini_set('memory_limit', '512M');
|
||||
}
|
||||
@set_time_limit(300);
|
||||
|
||||
|
||||
// Append the bfShapeFiles directory to the include path variable
|
||||
set_include_path(get_include_path() . PATH_SEPARATOR . getcwd() . '/libraries/bfShapeFiles/');
|
||||
include_once './libraries/bfShapeFiles/ShapeFile.lib.php';
|
||||
|
||||
$GLOBALS['finished'] = false;
|
||||
$buffer = '';
|
||||
$eof = false;
|
||||
|
||||
// Returns specified number of bytes from the buffer.
|
||||
// Buffer automatically fetches next chunk of data when the buffer falls short.
|
||||
// Sets $eof when $GLOBALS['finished'] is set and the buffer falls short.
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* This class extends ShapeFile class to cater the following phpMyAdmin
|
||||
* specific requirements.
|
||||
* 1) To load data from .dbf file only when the dBase extension is available.
|
||||
* 2) To use PMA_importGetNextChunk() functionality to read data, rather than
|
||||
* reading directly from a file. Using readFromBuffer() in place of fread().
|
||||
* This makes it possible to use compressions.
|
||||
*/
|
||||
class PMA_ShapeFile extends ShapeFile
|
||||
{
|
||||
function _isDbaseLoaded()
|
||||
{
|
||||
return extension_loaded('dbase');
|
||||
}
|
||||
|
||||
function loadFromFile($FileName)
|
||||
{
|
||||
$this->_loadHeaders();
|
||||
$this->_loadRecords();
|
||||
if ($this->_isDbaseLoaded()) {
|
||||
$this->_closeDBFFile();
|
||||
}
|
||||
}
|
||||
|
||||
function _loadHeaders()
|
||||
{
|
||||
readFromBuffer(24);
|
||||
$this->fileLength = loadData("N", readFromBuffer(4));
|
||||
|
||||
readFromBuffer(4);
|
||||
$this->shapeType = loadData("V", readFromBuffer(4));
|
||||
|
||||
$this->boundingBox = array();
|
||||
$this->boundingBox["xmin"] = loadData("d", readFromBuffer(8));
|
||||
$this->boundingBox["ymin"] = loadData("d", readFromBuffer(8));
|
||||
$this->boundingBox["xmax"] = loadData("d", readFromBuffer(8));
|
||||
$this->boundingBox["ymax"] = loadData("d", readFromBuffer(8));
|
||||
|
||||
if ($this->_isDbaseLoaded() && $this->_openDBFFile()) {
|
||||
$this->DBFHeader = $this->_loadDBFHeader();
|
||||
}
|
||||
}
|
||||
|
||||
function _loadRecords()
|
||||
{
|
||||
global $eof;
|
||||
readFromBuffer(32);
|
||||
while (true) {
|
||||
$record = new PMA_ShapeRecord(-1);
|
||||
$record->loadFromFile($this->SHPFile, $this->DBFFile);
|
||||
if ($record->lastError != "") {
|
||||
return false;
|
||||
}
|
||||
if ($eof) {
|
||||
break;
|
||||
}
|
||||
|
||||
$this->records[] = $record;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This class extends ShapeRecord class to cater the following phpMyAdmin
|
||||
* specific requirements.
|
||||
* 1) To load data from .dbf file only when the dBase extension is available.
|
||||
* 2) To use PMA_importGetNextChunk() functionality to read data, rather than
|
||||
* reading directly from a file. Using readFromBuffer() in place of fread().
|
||||
* This makes it possible to use compressions.
|
||||
*/
|
||||
class PMA_ShapeRecord extends ShapeRecord
|
||||
{
|
||||
function loadFromFile(&$SHPFile, &$DBFFile)
|
||||
{
|
||||
$this->DBFFile = $DBFFile;
|
||||
$this->_loadHeaders();
|
||||
|
||||
switch ($this->shapeType) {
|
||||
case 0:
|
||||
$this->_loadNullRecord();
|
||||
break;
|
||||
case 1:
|
||||
$this->_loadPointRecord();
|
||||
break;
|
||||
case 3:
|
||||
$this->_loadPolyLineRecord();
|
||||
break;
|
||||
case 5:
|
||||
$this->_loadPolygonRecord();
|
||||
break;
|
||||
case 8:
|
||||
$this->_loadMultiPointRecord();
|
||||
break;
|
||||
default:
|
||||
$this->setError(sprintf("The Shape Type '%s' is not supported.", $this->shapeType));
|
||||
break;
|
||||
}
|
||||
if (extension_loaded('dbase') && isset($this->DBFFile)) {
|
||||
$this->_loadDBFData();
|
||||
}
|
||||
}
|
||||
|
||||
function _loadHeaders()
|
||||
{
|
||||
$this->recordNumber = loadData("N", readFromBuffer(4));
|
||||
//We read the length of the record
|
||||
$tmp = loadData("N", readFromBuffer(4));
|
||||
$this->shapeType = loadData("V", readFromBuffer(4));
|
||||
}
|
||||
|
||||
function _loadPoint()
|
||||
{
|
||||
$data = array();
|
||||
|
||||
$data["x"] = loadData("d", readFromBuffer(8));
|
||||
$data["y"] = loadData("d", readFromBuffer(8));
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
function _loadMultiPointRecord()
|
||||
{
|
||||
$this->SHPData = array();
|
||||
$this->SHPData["xmin"] = loadData("d", readFromBuffer(8));
|
||||
$this->SHPData["ymin"] = loadData("d", readFromBuffer(8));
|
||||
$this->SHPData["xmax"] = loadData("d", readFromBuffer(8));
|
||||
$this->SHPData["ymax"] = loadData("d", readFromBuffer(8));
|
||||
|
||||
$this->SHPData["numpoints"] = loadData("V", readFromBuffer(4));
|
||||
|
||||
for ($i = 0; $i <= $this->SHPData["numpoints"]; $i++) {
|
||||
$this->SHPData["points"][] = $this->_loadPoint();
|
||||
}
|
||||
}
|
||||
|
||||
function _loadPolyLineRecord()
|
||||
{
|
||||
$this->SHPData = array();
|
||||
$this->SHPData["xmin"] = loadData("d", readFromBuffer(8));
|
||||
$this->SHPData["ymin"] = loadData("d", readFromBuffer(8));
|
||||
$this->SHPData["xmax"] = loadData("d", readFromBuffer(8));
|
||||
$this->SHPData["ymax"] = loadData("d", readFromBuffer(8));
|
||||
|
||||
$this->SHPData["numparts"] = loadData("V", readFromBuffer(4));
|
||||
$this->SHPData["numpoints"] = loadData("V", readFromBuffer(4));
|
||||
|
||||
for ($i = 0; $i < $this->SHPData["numparts"]; $i++) {
|
||||
$this->SHPData["parts"][$i] = loadData("V", readFromBuffer(4));
|
||||
}
|
||||
|
||||
$readPoints = 0;
|
||||
reset($this->SHPData["parts"]);
|
||||
while (list($partIndex, $partData) = each($this->SHPData["parts"])) {
|
||||
if (! isset($this->SHPData["parts"][$partIndex]["points"])
|
||||
|| !is_array($this->SHPData["parts"][$partIndex]["points"])
|
||||
) {
|
||||
$this->SHPData["parts"][$partIndex] = array();
|
||||
$this->SHPData["parts"][$partIndex]["points"] = array();
|
||||
}
|
||||
while (! in_array($readPoints, $this->SHPData["parts"])
|
||||
&& ($readPoints < ($this->SHPData["numpoints"]))
|
||||
) {
|
||||
$this->SHPData["parts"][$partIndex]["points"][] = $this->_loadPoint();
|
||||
$readPoints++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$shp = new PMA_ShapeFile(1);
|
||||
// If the zip archive has more than one file,
|
||||
// get the correct content to the buffer from .shp file.
|
||||
if ($compression == 'application/zip' && PMA_getNoOfFilesInZip($import_file) > 1) {
|
||||
$zip_content = PMA_getZipContents($import_file, '/^.*\.shp$/i');
|
||||
$GLOBALS['import_text'] = $zip_content['data'];
|
||||
}
|
||||
|
||||
$temp_dbf_file = false;
|
||||
// We need dbase extension to handle .dbf file
|
||||
if (extension_loaded('dbase')) {
|
||||
// If we can extract the zip archive to 'TempDir'
|
||||
// and use the files in it for import
|
||||
if ($compression == 'application/zip'
|
||||
&& ! empty($cfg['TempDir'])
|
||||
&& is_writable($cfg['TempDir'])
|
||||
) {
|
||||
$dbf_file_name = PMA_findFileFromZipArchive('/^.*\.dbf$/i', $import_file);
|
||||
// If the corresponding .dbf file is in the zip archive
|
||||
if ($dbf_file_name) {
|
||||
// Extract the .dbf file and point to it.
|
||||
$extracted = PMA_zipExtract(
|
||||
$import_file,
|
||||
realpath($cfg['TempDir']),
|
||||
array($dbf_file_name)
|
||||
);
|
||||
if ($extracted) {
|
||||
$dbf_file_path = realpath($cfg['TempDir'])
|
||||
. (PMA_IS_WINDOWS ? '\\' : '/') . $dbf_file_name;
|
||||
$temp_dbf_file = true;
|
||||
// Replace the .dbf with .*, as required by the bsShapeFiles library.
|
||||
$file_name = substr($dbf_file_path, 0, strlen($dbf_file_path) - 4) . '.*';
|
||||
$shp->FileName = $file_name;
|
||||
}
|
||||
}
|
||||
}
|
||||
// If file is in UploadDir, use .dbf file in the same UploadDir
|
||||
// to load extra data.
|
||||
elseif (! empty($local_import_file)
|
||||
&& ! empty($cfg['UploadDir'])
|
||||
&& $compression == 'none'
|
||||
) {
|
||||
// Replace the .shp with .*,
|
||||
// so the bsShapeFiles library correctly locates .dbf file.
|
||||
$file_name = substr($import_file, 0, strlen($import_file) - 4) . '.*';
|
||||
$shp->FileName = $file_name;
|
||||
}
|
||||
}
|
||||
|
||||
// Load data
|
||||
$shp->loadFromFile('');
|
||||
if ($shp->lastError != "") {
|
||||
$error = true;
|
||||
$message = PMA_Message::error(__('There was an error importing the ESRI shape file: "%s".'));
|
||||
$message->addParam($shp->lastError);
|
||||
return;
|
||||
}
|
||||
|
||||
// Delete the .dbf file extracted to 'TempDir'
|
||||
if ($temp_dbf_file) {
|
||||
unlink($dbf_file_path);
|
||||
}
|
||||
|
||||
$esri_types = array(
|
||||
0 => 'Null Shape',
|
||||
1 => 'Point',
|
||||
3 => 'PolyLine',
|
||||
5 => 'Polygon',
|
||||
8 => 'MultiPoint',
|
||||
11 => 'PointZ',
|
||||
13 => 'PolyLineZ',
|
||||
15 => 'PolygonZ',
|
||||
18 => 'MultiPointZ',
|
||||
21 => 'PointM',
|
||||
23 => 'PolyLineM',
|
||||
25 => 'PolygonM',
|
||||
28 => 'MultiPointM',
|
||||
31 => 'MultiPatch',
|
||||
);
|
||||
|
||||
include_once './libraries/gis/pma_gis_geometry.php';
|
||||
switch ($shp->shapeType) {
|
||||
// ESRI Null Shape
|
||||
case 0:
|
||||
$gis_obj = null;
|
||||
break;
|
||||
// ESRI Point
|
||||
case 1:
|
||||
include_once './libraries/gis/pma_gis_point.php';
|
||||
$gis_obj = PMA_GIS_Point::singleton();
|
||||
break;
|
||||
// ESRI PolyLine
|
||||
case 3:
|
||||
include_once './libraries/gis/pma_gis_multilinestring.php';
|
||||
$gis_obj = PMA_GIS_Multilinestring::singleton();
|
||||
break;
|
||||
// ESRI Polygon
|
||||
case 5:
|
||||
include_once './libraries/gis/pma_gis_multipolygon.php';
|
||||
$gis_obj = PMA_GIS_Multipolygon::singleton();
|
||||
break;
|
||||
// ESRI MultiPoint
|
||||
case 8:
|
||||
include_once './libraries/gis/pma_gis_multipoint.php';
|
||||
$gis_obj = PMA_GIS_Multipoint::singleton();
|
||||
break;
|
||||
default:
|
||||
$error = true;
|
||||
if (! isset($esri_types[$shp->shapeType])) {
|
||||
$message = PMA_Message::error(__('You tried to import an invalid file or the imported file contains invalid data'));
|
||||
} else {
|
||||
$message = PMA_Message::error(__('MySQL Spatial Extension does not support ESRI type "%s".'));
|
||||
$message->addParam($param);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
$num_rows = count($shp->records);
|
||||
// If .dbf file is loaded, the number of extra data columns
|
||||
$num_data_cols = isset($shp->DBFHeader) ? count($shp->DBFHeader) : 0;
|
||||
|
||||
$rows = array();
|
||||
$col_names = array();
|
||||
if ($num_rows != 0) {
|
||||
foreach ($shp->records as $record) {
|
||||
$tempRow = array();
|
||||
if ($gis_obj == null) {
|
||||
$tempRow[] = null;
|
||||
} else {
|
||||
$tempRow[] = "GeomFromText('" . $gis_obj->getShape($record->SHPData) . "')";
|
||||
}
|
||||
|
||||
if (isset($shp->DBFHeader)) {
|
||||
foreach ($shp->DBFHeader as $c) {
|
||||
$cell = trim($record->DBFData[$c[0]]);
|
||||
|
||||
if (! strcmp($cell, '')) {
|
||||
$cell = 'NULL';
|
||||
}
|
||||
|
||||
$tempRow[] = $cell;
|
||||
}
|
||||
}
|
||||
$rows[] = $tempRow;
|
||||
}
|
||||
}
|
||||
|
||||
if (count($rows) == 0) {
|
||||
$error = true;
|
||||
$message = PMA_Message::error(__('The imported file does not contain any data'));
|
||||
return;
|
||||
}
|
||||
|
||||
// Column names for spatial column and the rest of the columns,
|
||||
// if they are available
|
||||
$col_names[] = 'SPATIAL';
|
||||
for ($n = 0; $n < $num_data_cols; $n++) {
|
||||
$col_names[] = $shp->DBFHeader[$n][0];
|
||||
}
|
||||
|
||||
// Set table name based on the number of tables
|
||||
if (strlen($db)) {
|
||||
$result = PMA_DBI_fetch_result('SHOW TABLES');
|
||||
$table_name = 'TABLE '.(count($result) + 1);
|
||||
} else {
|
||||
$table_name = 'TBL_NAME';
|
||||
}
|
||||
$tables = array(array($table_name, $col_names, $rows));
|
||||
|
||||
// Use data from shape file to chose best-fit MySQL types for each column
|
||||
$analyses = array();
|
||||
$analyses[] = PMA_analyzeTable($tables[0]);
|
||||
|
||||
$table_no = 0; $spatial_col = 0;
|
||||
$analyses[$table_no][TYPES][$spatial_col] = GEOMETRY;
|
||||
$analyses[$table_no][FORMATTEDSQL][$spatial_col] = true;
|
||||
|
||||
// Set database name to the currently selected one, if applicable
|
||||
if (strlen($db)) {
|
||||
$db_name = $db;
|
||||
$options = array('create_db' => false);
|
||||
} else {
|
||||
$db_name = 'SHP_DB';
|
||||
$options = null;
|
||||
}
|
||||
|
||||
// Created and execute necessary SQL statements from data
|
||||
$null_param = null;
|
||||
PMA_buildSQL($db_name, $tables, $analyses, $null_param, $options);
|
||||
|
||||
unset($tables);
|
||||
unset($analyses);
|
||||
|
||||
$finished = true;
|
||||
$error = false;
|
||||
|
||||
// Commit any possible data in buffers
|
||||
PMA_importRunQuery();
|
||||
}
|
||||
?>
|
321
html/phpmyad/libraries/import/sql.php
Normal file
321
html/phpmyad/libraries/import/sql.php
Normal file
@ -0,0 +1,321 @@
|
||||
<?php
|
||||
/* vim: set expandtab sw=4 ts=4 sts=4: */
|
||||
/**
|
||||
* SQL import plugin for phpMyAdmin
|
||||
*
|
||||
* @package PhpMyAdmin-Import
|
||||
* @subpackage SQL
|
||||
*/
|
||||
if (! defined('PHPMYADMIN')) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
if (isset($plugin_list)) {
|
||||
$plugin_list['sql'] = array(
|
||||
'text' => __('SQL'),
|
||||
'extension' => 'sql',
|
||||
'options_text' => __('Options'),
|
||||
);
|
||||
$compats = PMA_DBI_getCompatibilities();
|
||||
if (count($compats) > 0) {
|
||||
$values = array();
|
||||
foreach ($compats as $val) {
|
||||
$values[$val] = $val;
|
||||
}
|
||||
$plugin_list['sql']['options'] = array(
|
||||
array('type' => 'begin_group', 'name' => 'general_opts'),
|
||||
array(
|
||||
'type' => 'select',
|
||||
'name' => 'compatibility',
|
||||
'text' => __('SQL compatibility mode:'),
|
||||
'values' => $values,
|
||||
'doc' => array(
|
||||
'manual_MySQL_Database_Administration',
|
||||
'Server_SQL_mode',
|
||||
),
|
||||
),
|
||||
array(
|
||||
'type' => 'bool',
|
||||
'name' => 'no_auto_value_on_zero',
|
||||
'text' => __('Do not use <code>AUTO_INCREMENT</code> for zero values'),
|
||||
'doc' => array(
|
||||
'manual_MySQL_Database_Administration',
|
||||
'Server_SQL_mode',
|
||||
'sqlmode_no_auto_value_on_zero'
|
||||
),
|
||||
|
||||
),
|
||||
array('type' => 'end_group'),
|
||||
);
|
||||
}
|
||||
|
||||
/* We do not define function when plugin is just queried for information above */
|
||||
return;
|
||||
}
|
||||
|
||||
$buffer = '';
|
||||
// Defaults for parser
|
||||
$sql = '';
|
||||
$start_pos = 0;
|
||||
$i = 0;
|
||||
$len= 0;
|
||||
$big_value = 2147483647;
|
||||
$delimiter_keyword = 'DELIMITER '; // include the space because it's mandatory
|
||||
$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) {
|
||||
PMA_DBI_try_query('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
|
||||
$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;
|
||||
}
|
||||
}
|
||||
// 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 (!$GLOBALS['finished']) {
|
||||
break;
|
||||
}
|
||||
// at the end there might be some whitespace...
|
||||
if (trim($buffer) == '') {
|
||||
$buffer = '';
|
||||
$len = 0;
|
||||
break;
|
||||
}
|
||||
// We hit end of query, go there!
|
||||
$i = strlen($buffer) - 1;
|
||||
}
|
||||
|
||||
// Grab current character
|
||||
$ch = $buffer[$i];
|
||||
|
||||
// Quotes
|
||||
if (strpos('\'"`', $ch) !== false) {
|
||||
$quote = $ch;
|
||||
$endq = false;
|
||||
while (!$endq) {
|
||||
// Find next quote
|
||||
$pos = strpos($buffer, $quote, $i + 1);
|
||||
/*
|
||||
* Behave same as MySQL and accept end of query as end of backtick.
|
||||
* I know this is sick, but MySQL behaves like this:
|
||||
*
|
||||
* SELECT * FROM `table
|
||||
*
|
||||
* is treated like
|
||||
*
|
||||
* SELECT * FROM `table`
|
||||
*/
|
||||
if ($pos === false && $quote == '`' && $found_delimiter) {
|
||||
$pos = $first_sql_delimiter - 1;
|
||||
// No quote? Too short string
|
||||
} elseif ($pos === false) {
|
||||
// We hit end of string => unclosed quote, but we handle it as end of query
|
||||
if ($GLOBALS['finished']) {
|
||||
$endq = true;
|
||||
$i = $len - 1;
|
||||
}
|
||||
$found_delimiter = false;
|
||||
break;
|
||||
}
|
||||
// Was not the quote escaped?
|
||||
$j = $pos - 1;
|
||||
while ($buffer[$j] == '\\') $j--;
|
||||
// Even count means it was not escaped
|
||||
$endq = (((($pos - 1) - $j) % 2) == 0);
|
||||
// Skip the string
|
||||
$i = $pos;
|
||||
|
||||
if ($first_sql_delimiter < $pos) {
|
||||
$found_delimiter = false;
|
||||
}
|
||||
}
|
||||
if (!$endq) {
|
||||
break;
|
||||
}
|
||||
$i++;
|
||||
// Aren't we at the end?
|
||||
if ($GLOBALS['finished'] && $i == $len) {
|
||||
$i--;
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// Not enough data to decide
|
||||
if ((($i == ($len - 1) && ($ch == '-' || $ch == '/'))
|
||||
|| ($i == ($len - 2) && (($ch == '-' && $buffer[$i + 1] == '-')
|
||||
|| ($ch == '/' && $buffer[$i + 1] == '*')))) && !$GLOBALS['finished']) {
|
||||
break;
|
||||
}
|
||||
|
||||
// Comments
|
||||
if ($ch == '#'
|
||||
|| ($i < ($len - 1) && $ch == '-' && $buffer[$i + 1] == '-'
|
||||
&& (($i < ($len - 2) && $buffer[$i + 2] <= ' ')
|
||||
|| ($i == ($len - 1) && $GLOBALS['finished'])))
|
||||
|| ($i < ($len - 1) && $ch == '/' && $buffer[$i + 1] == '*')
|
||||
) {
|
||||
// Copy current string to SQL
|
||||
if ($start_pos != $i) {
|
||||
$sql .= substr($buffer, $start_pos, $i - $start_pos);
|
||||
}
|
||||
// Skip the rest
|
||||
$start_of_comment = $i;
|
||||
// do not use PHP_EOL here instead of "\n", because the export
|
||||
// file might have been produced on a different system
|
||||
$i = strpos($buffer, $ch == '/' ? '*/' : "\n", $i);
|
||||
// didn't we hit end of string?
|
||||
if ($i === false) {
|
||||
if ($GLOBALS['finished']) {
|
||||
$i = $len - 1;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
// Skip *
|
||||
if ($ch == '/') {
|
||||
$i++;
|
||||
}
|
||||
// Skip last char
|
||||
$i++;
|
||||
// We need to send the comment part in case we are defining
|
||||
// a procedure or function and comments in it are valuable
|
||||
$sql .= substr($buffer, $start_of_comment, $i - $start_of_comment);
|
||||
// Next query part will start here
|
||||
$start_pos = $i;
|
||||
// Aren't we at the end?
|
||||
if ($i == $len) {
|
||||
$i--;
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
// Change delimiter, if redefined, and skip it (don't send to server!)
|
||||
if (strtoupper(substr($buffer, $i, $length_of_delimiter_keyword)) == $delimiter_keyword
|
||||
&& ($i + $length_of_delimiter_keyword < $len)) {
|
||||
// look for EOL on the character immediately after 'DELIMITER '
|
||||
// (see previous comment about PHP_EOL)
|
||||
$new_line_pos = strpos($buffer, "\n", $i + $length_of_delimiter_keyword);
|
||||
// it might happen that there is no EOL
|
||||
if (false === $new_line_pos) {
|
||||
$new_line_pos = $len;
|
||||
}
|
||||
$sql_delimiter = substr($buffer, $i + $length_of_delimiter_keyword, $new_line_pos - $i - $length_of_delimiter_keyword);
|
||||
$i = $new_line_pos + 1;
|
||||
// Next query part will start here
|
||||
$start_pos = $i;
|
||||
continue;
|
||||
}
|
||||
|
||||
// End of SQL
|
||||
if ($found_delimiter || ($GLOBALS['finished'] && ($i == $len - 1))) {
|
||||
$tmp_sql = $sql;
|
||||
if ($start_pos < $len) {
|
||||
$length_to_grab = $i - $start_pos;
|
||||
|
||||
if (! $found_delimiter) {
|
||||
$length_to_grab++;
|
||||
}
|
||||
$tmp_sql .= substr($buffer, $start_pos, $length_to_grab);
|
||||
unset($length_to_grab);
|
||||
}
|
||||
// Do not try to execute empty SQL
|
||||
if (! preg_match('/^([\s]*;)*$/', trim($tmp_sql))) {
|
||||
$sql = $tmp_sql;
|
||||
PMA_importRunQuery($sql, substr($buffer, 0, $i + strlen($sql_delimiter)));
|
||||
$buffer = substr($buffer, $i + strlen($sql_delimiter));
|
||||
// Reset parser:
|
||||
$len = strlen($buffer);
|
||||
$sql = '';
|
||||
$i = 0;
|
||||
$start_pos = 0;
|
||||
// Any chance we will get a complete query?
|
||||
//if ((strpos($buffer, ';') === false) && !$GLOBALS['finished']) {
|
||||
if ((strpos($buffer, $sql_delimiter) === false) && !$GLOBALS['finished']) {
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
$i++;
|
||||
$start_pos = $i;
|
||||
}
|
||||
}
|
||||
} // End of parser loop
|
||||
} // End of import loop
|
||||
// Commit any possible data in buffers
|
||||
PMA_importRunQuery('', substr($buffer, 0, $len));
|
||||
PMA_importRunQuery();
|
||||
?>
|
66
html/phpmyad/libraries/import/upload/apc.php
Normal file
66
html/phpmyad/libraries/import/upload/apc.php
Normal file
@ -0,0 +1,66 @@
|
||||
<?php
|
||||
/* vim: set expandtab sw=4 ts=4 sts=4: */
|
||||
/**
|
||||
*
|
||||
* @package PhpMyAdmin
|
||||
*/
|
||||
|
||||
if (! defined('PHPMYADMIN')) {
|
||||
exit;
|
||||
}
|
||||
|
||||
$ID_KEY = 'APC_UPLOAD_PROGRESS';
|
||||
|
||||
/**
|
||||
* Returns upload status.
|
||||
*
|
||||
* This is implementation for APC extension.
|
||||
*
|
||||
* @param string $id
|
||||
* @return array|null
|
||||
*/
|
||||
function PMA_getUploadStatus($id)
|
||||
{
|
||||
global $SESSION_KEY;
|
||||
global $ID_KEY;
|
||||
|
||||
if (trim($id) == "") {
|
||||
return null;
|
||||
}
|
||||
if (! array_key_exists($id, $_SESSION[$SESSION_KEY])) {
|
||||
$_SESSION[$SESSION_KEY][$id] = array(
|
||||
'id' => $id,
|
||||
'finished' => false,
|
||||
'percent' => 0,
|
||||
'total' => 0,
|
||||
'complete' => 0,
|
||||
'plugin' => $ID_KEY
|
||||
);
|
||||
}
|
||||
$ret = $_SESSION[$SESSION_KEY][$id];
|
||||
|
||||
if (! PMA_import_apcCheck() || $ret['finished']) {
|
||||
return $ret;
|
||||
}
|
||||
$status = apc_fetch('upload_' . $id);
|
||||
|
||||
if ($status) {
|
||||
$ret['finished'] = (bool)$status['done'];
|
||||
$ret['total'] = $status['total'];
|
||||
$ret['complete'] = $status['current'];
|
||||
|
||||
if ($ret['total'] > 0) {
|
||||
$ret['percent'] = $ret['complete'] / $ret['total'] * 100;
|
||||
}
|
||||
|
||||
if ($ret['percent'] == 100) {
|
||||
$ret['finished'] = (bool)true;
|
||||
}
|
||||
|
||||
$_SESSION[$SESSION_KEY][$id] = $ret;
|
||||
}
|
||||
|
||||
return $ret;
|
||||
}
|
||||
|
||||
?>
|
44
html/phpmyad/libraries/import/upload/noplugin.php
Normal file
44
html/phpmyad/libraries/import/upload/noplugin.php
Normal file
@ -0,0 +1,44 @@
|
||||
<?php
|
||||
/* vim: set expandtab sw=4 ts=4 sts=4: */
|
||||
/**
|
||||
*
|
||||
* @package PhpMyAdmin
|
||||
*/
|
||||
|
||||
if (! defined('PHPMYADMIN')) {
|
||||
exit;
|
||||
}
|
||||
|
||||
$ID_KEY = 'noplugin';
|
||||
|
||||
/**
|
||||
* Returns upload status.
|
||||
*
|
||||
* This is implementation when no webserver support exists, so it returns just zeroes.
|
||||
*
|
||||
* @param string $id
|
||||
* @return array|null
|
||||
*/
|
||||
function PMA_getUploadStatus($id)
|
||||
{
|
||||
global $SESSION_KEY;
|
||||
global $ID_KEY;
|
||||
|
||||
if (trim($id) == "") {
|
||||
return null;
|
||||
}
|
||||
if (! array_key_exists($id, $_SESSION[$SESSION_KEY])) {
|
||||
$_SESSION[$SESSION_KEY][$id] = array(
|
||||
'id' => $id,
|
||||
'finished' => false,
|
||||
'percent' => 0,
|
||||
'total' => 0,
|
||||
'complete' => 0,
|
||||
'plugin' => $ID_KEY
|
||||
);
|
||||
}
|
||||
$ret = $_SESSION[$SESSION_KEY][$id];
|
||||
|
||||
return $ret;
|
||||
}
|
||||
?>
|
75
html/phpmyad/libraries/import/upload/uploadprogress.php
Normal file
75
html/phpmyad/libraries/import/upload/uploadprogress.php
Normal file
@ -0,0 +1,75 @@
|
||||
<?php
|
||||
/* vim: set expandtab sw=4 ts=4 sts=4: */
|
||||
/**
|
||||
*
|
||||
* @package PhpMyAdmin
|
||||
*/
|
||||
if (! defined('PHPMYADMIN')) {
|
||||
exit;
|
||||
}
|
||||
|
||||
$ID_KEY = "UPLOAD_IDENTIFIER";
|
||||
|
||||
/**
|
||||
* Returns upload status.
|
||||
*
|
||||
* This is implementation for uploadprogress extension.
|
||||
*
|
||||
* @param string $id
|
||||
* @return array|null
|
||||
*/
|
||||
function PMA_getUploadStatus($id)
|
||||
{
|
||||
global $SESSION_KEY;
|
||||
global $ID_KEY;
|
||||
|
||||
if (trim($id) == "") {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (! array_key_exists($id, $_SESSION[$SESSION_KEY])) {
|
||||
$_SESSION[$SESSION_KEY][$id] = array(
|
||||
'id' => $id,
|
||||
'finished' => false,
|
||||
'percent' => 0,
|
||||
'total' => 0,
|
||||
'complete' => 0,
|
||||
'plugin' => $ID_KEY
|
||||
);
|
||||
}
|
||||
$ret = $_SESSION[$SESSION_KEY][$id];
|
||||
|
||||
if (! PMA_import_uploadprogressCheck() || $ret['finished']) {
|
||||
return $ret;
|
||||
}
|
||||
|
||||
$status = uploadprogress_get_info($id);
|
||||
|
||||
if ($status) {
|
||||
if ($status['bytes_uploaded'] == $status['bytes_total']) {
|
||||
$ret['finished'] = true;
|
||||
} else {
|
||||
$ret['finished'] = false;
|
||||
}
|
||||
$ret['total'] = $status['bytes_total'];
|
||||
$ret['complete'] = $status['bytes_uploaded'];
|
||||
|
||||
if ($ret['total'] > 0) {
|
||||
$ret['percent'] = $ret['complete'] / $ret['total'] * 100;
|
||||
}
|
||||
} else {
|
||||
$ret = array(
|
||||
'id' => $id,
|
||||
'finished' => true,
|
||||
'percent' => 100,
|
||||
'total' => $ret['total'],
|
||||
'complete' => $ret['total'],
|
||||
'plugin' => $ID_KEY
|
||||
);
|
||||
}
|
||||
|
||||
$_SESSION[$SESSION_KEY][$id] = $ret;
|
||||
|
||||
return $ret;
|
||||
}
|
||||
?>
|
317
html/phpmyad/libraries/import/xml.php
Normal file
317
html/phpmyad/libraries/import/xml.php
Normal file
@ -0,0 +1,317 @@
|
||||
<?php
|
||||
/* vim: set expandtab sw=4 ts=4 sts=4: */
|
||||
/**
|
||||
* XML import plugin for phpMyAdmin
|
||||
*
|
||||
* @todo Improve efficiency
|
||||
* @package PhpMyAdmin-Import
|
||||
* @subpackage XML
|
||||
*/
|
||||
|
||||
if (! defined('PHPMYADMIN')) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* We need way to disable external XML entities processing.
|
||||
*/
|
||||
if (!function_exists('libxml_disable_entity_loader')) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* The possible scopes for $plugin_param are: 'table', 'database', and 'server'
|
||||
*/
|
||||
|
||||
if (isset($plugin_list)) {
|
||||
$plugin_list['xml'] = array(
|
||||
'text' => __('XML'),
|
||||
'extension' => 'xml',
|
||||
'options' => array(
|
||||
),
|
||||
'options_text' => __('Options'),
|
||||
);
|
||||
/* We do not define function when plugin is just queried for information above */
|
||||
return;
|
||||
}
|
||||
|
||||
$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.
|
||||
*/
|
||||
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_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_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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the structure information
|
||||
*/
|
||||
if (isset($namespaces['pma'])) {
|
||||
/**
|
||||
* Get structures for all tables
|
||||
*/
|
||||
$struct = $xml->children($namespaces['pma']);
|
||||
|
||||
$create = array();
|
||||
|
||||
foreach ($struct as $tier1 => $val1) {
|
||||
foreach ($val1 as $tier2 => $val2) {
|
||||
/* Need to select the correct database for the creation of tables, views, triggers, etc. */
|
||||
/**
|
||||
* @todo Generating a USE here blocks importing of a table
|
||||
* into another database.
|
||||
*/
|
||||
$attrs = $val2->attributes();
|
||||
$create[] = "USE " . PMA_backquote($attrs["name"]);
|
||||
|
||||
foreach ($val2 as $val3) {
|
||||
/**
|
||||
* Remove the extra cosmetic spacing
|
||||
*/
|
||||
$val3 = str_replace(" ", "", (string)$val3);
|
||||
$create[] = $val3;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$struct_present = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Move down the XML tree to the actual data
|
||||
*/
|
||||
$xml = $xml->children()->children();
|
||||
|
||||
$data_present = false;
|
||||
|
||||
/**
|
||||
* Only attempt to analyze/collect data if there is data present
|
||||
*/
|
||||
if ($xml && @$xml->count()) {
|
||||
$data_present = true;
|
||||
|
||||
/**
|
||||
* Process all database content
|
||||
*/
|
||||
foreach ($xml as $k1 => $v1) {
|
||||
$tbl_attr = $v1->attributes();
|
||||
|
||||
$isInTables = false;
|
||||
for ($i = 0; $i < count($tables); ++$i) {
|
||||
if (! strcmp($tables[$i][TBL_NAME], (string)$tbl_attr['name'])) {
|
||||
$isInTables = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ($isInTables == false) {
|
||||
$tables[] = array((string)$tbl_attr['name']);
|
||||
}
|
||||
|
||||
foreach ($v1 as $k2 => $v2) {
|
||||
$row_attr = $v2->attributes();
|
||||
if (! array_search((string)$row_attr['name'], $tempRow)) {
|
||||
$tempRow[] = (string)$row_attr['name'];
|
||||
}
|
||||
$tempCells[] = (string)$v2;
|
||||
}
|
||||
|
||||
$rows[] = array((string)$tbl_attr['name'], $tempRow, $tempCells);
|
||||
|
||||
$tempRow = array();
|
||||
$tempCells = array();
|
||||
}
|
||||
|
||||
unset($tempRow);
|
||||
unset($tempCells);
|
||||
unset($xml);
|
||||
|
||||
/**
|
||||
* Bring accumulated rows into the corresponding table
|
||||
*/
|
||||
$num_tbls = count($tables);
|
||||
for ($i = 0; $i < $num_tbls; ++$i) {
|
||||
for ($j = 0; $j < count($rows); ++$j) {
|
||||
if (! strcmp($tables[$i][TBL_NAME], $rows[$j][TBL_NAME])) {
|
||||
if (! isset($tables[$i][COL_NAMES])) {
|
||||
$tables[$i][] = $rows[$j][COL_NAMES];
|
||||
}
|
||||
|
||||
$tables[$i][ROWS][] = $rows[$j][ROWS];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
unset($rows);
|
||||
|
||||
if (! $struct_present) {
|
||||
$analyses = array();
|
||||
|
||||
$len = count($tables);
|
||||
for ($i = 0; $i < $len; ++$i) {
|
||||
$analyses[] = PMA_analyzeTable($tables[$i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
unset($xml);
|
||||
unset($tempRows);
|
||||
unset($tempCells);
|
||||
unset($rows);
|
||||
|
||||
/**
|
||||
* Only build SQL from data if there is data present
|
||||
*/
|
||||
if ($data_present) {
|
||||
/**
|
||||
* Set values to NULL if they were not present
|
||||
* to maintain PMA_buildSQL() call integrity
|
||||
*/
|
||||
if (! isset($analyses)) {
|
||||
$analyses = null;
|
||||
if (! $struct_present) {
|
||||
$create = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* string $db_name (no backquotes)
|
||||
*
|
||||
* array $table = array(table_name, array() column_names, array()() rows)
|
||||
* array $tables = array of "$table"s
|
||||
*
|
||||
* array $analysis = array(array() column_types, array() column_sizes)
|
||||
* array $analyses = array of "$analysis"s
|
||||
*
|
||||
* array $create = array of SQL strings
|
||||
*
|
||||
* array $options = an associative array of options
|
||||
*/
|
||||
|
||||
/* Set database name to the currently selected one, if applicable */
|
||||
if (strlen($db)) {
|
||||
/* Override the database name in the XML file, if one is selected */
|
||||
$db_name = $db;
|
||||
$options = array('create_db' => false);
|
||||
} else {
|
||||
if ($db_name === null) {
|
||||
$db_name = 'XML_DB';
|
||||
}
|
||||
|
||||
/* Set database collation/charset */
|
||||
$options = array(
|
||||
'db_collation' => $collation,
|
||||
'db_charset' => $charset,
|
||||
);
|
||||
}
|
||||
|
||||
/* Created and execute necessary SQL statements from data */
|
||||
PMA_buildSQL($db_name, $tables, $analyses, $create, $options);
|
||||
|
||||
unset($analyses);
|
||||
unset($tables);
|
||||
unset($create);
|
||||
|
||||
/* Commit any possible data in buffers */
|
||||
PMA_importRunQuery();
|
||||
?>
|
Reference in New Issue
Block a user