2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * OpenDocument Spreadsheet import plugin for phpMyAdmin
6 * @todo Pretty much everything
7 * @todo Importing of accented characters seems to fail
8 * @package PhpMyAdmin-Import
12 if (! defined('PHPMYADMIN')) {
17 * We need way to disable external XML entities processing.
19 if (!function_exists('libxml_disable_entity_loader')) {
24 * The possible scopes for $plugin_param are: 'table', 'database', and 'server'
27 if (isset($plugin_list)) {
28 $plugin_list['ods'] = array(
29 'text' => __('Open Document Spreadsheet'),
32 array('type' => 'begin_group', 'name' => 'general_opts'),
33 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>')),
34 array('type' => 'bool', 'name' => 'empty_rows', 'text' => __('Do not import empty rows')),
35 array('type' => 'bool', 'name' => 'recognize_percentages', 'text' => __('Import percentages as proper decimals <i>(ex. 12.00% to .12)</i>')),
36 array('type' => 'bool', 'name' => 'recognize_currency', 'text' => __('Import currencies <i>(ex. $5.00 to 5.00)</i>')),
37 array('type' => 'end_group')
39 'options_text' => __('Options'),
41 /* We do not define function when plugin is just queried for information above */
50 * Read in the file via PMA_importGetNextChunk so that
51 * it can process compressed files
53 while (! ($finished && $i >= $len) && ! $error && ! $timeout_passed) {
54 $data = PMA_importGetNextChunk();
55 if ($data === false) {
56 /* subtract data we didn't handle yet and stop processing */
57 $offset -= strlen($buffer);
59 } elseif ($data === true) {
60 /* Handle rest of buffer */
62 /* Append new data to buffer */
71 * Disable loading of external XML entities.
73 libxml_disable_entity_loader();
78 * The option LIBXML_COMPACT is specified because it can
79 * result in increased performance without the need to
80 * alter the code in any way. It's basically a freebee.
82 $xml = simplexml_load_string($buffer, "SimpleXMLElement", LIBXML_COMPACT
);
88 $message = PMA_Message
::error(__('The XML file specified was either malformed or incomplete. Please correct the issue and try again.'));
91 $sheets = $xml->children('office', true)->{'body'}->{'spreadsheet'}->children('table', true);
100 $col_names = array();
106 /* Iterate over tables */
107 foreach ($sheets as $sheet) {
108 $col_names_in_first_row = isset($_REQUEST['ods_col_names']);
110 /* Iterate over rows */
111 foreach ($sheet as $row) {
112 $type = $row->getName();
113 if (! strcmp('table-row', $type)) {
114 /* Iterate over columns */
115 foreach ($row as $cell) {
116 $text = $cell->children('text', true);
117 $cell_attrs = $cell->attributes('office', true);
119 if (count($text) != 0) {
120 if (! $col_names_in_first_row) {
121 if ($_REQUEST['ods_recognize_percentages'] && !strcmp('percentage', $cell_attrs['value-type'])) {
122 $tempRow[] = (double)$cell_attrs['value'];
123 } elseif ($_REQUEST['ods_recognize_currency'] && !strcmp('currency', $cell_attrs['value-type'])) {
124 $tempRow[] = (double)$cell_attrs['value'];
126 $tempRow[] = (string)$text;
129 if ($_REQUEST['ods_recognize_percentages'] && !strcmp('percentage', $cell_attrs['value-type'])) {
130 $col_names[] = (double)$cell_attrs['value'];
131 } else if ($_REQUEST['ods_recognize_currency'] && !strcmp('currency', $cell_attrs['value-type'])) {
132 $col_names[] = (double)$cell_attrs['value'];
134 $col_names[] = (string)$text;
140 /* Number of blank columns repeated */
141 if ($col_count < count($row->children('table', true)) - 1) {
142 $attr = $cell->attributes('table', true);
143 $num_null = (int)$attr['number-columns-repeated'];
146 if (! $col_names_in_first_row) {
147 for ($i = 0; $i < $num_null; ++
$i) {
152 for ($i = 0; $i < $num_null; ++
$i) {
153 $col_names[] = PMA_getColumnAlphaName($col_count +
1);
158 if (! $col_names_in_first_row) {
161 $col_names[] = PMA_getColumnAlphaName($col_count +
1);
170 /* Find the widest row */
171 if ($col_count > $max_cols) {
172 $max_cols = $col_count;
175 /* Don't include a row that is full of NULL values */
176 if (! $col_names_in_first_row) {
177 if ($_REQUEST['ods_empty_rows']) {
178 foreach ($tempRow as $cell) {
179 if (strcmp('NULL', $cell)) {
180 $tempRows[] = $tempRow;
185 $tempRows[] = $tempRow;
190 $col_names_in_first_row = false;
195 /* Skip over empty sheets */
196 if (count($tempRows) == 0 ||
count($tempRows[0]) == 0) {
197 $col_names = array();
204 * Fill out each row as necessary to make
205 * every one exactly as wide as the widest
206 * row. This included column names.
209 /* Fill out column names */
210 for ($i = count($col_names); $i < $max_cols; ++
$i) {
211 $col_names[] = PMA_getColumnAlphaName($i +
1);
214 /* Fill out all rows */
215 $num_rows = count($tempRows);
216 for ($i = 0; $i < $num_rows; ++
$i) {
217 for ($j = count($tempRows[$i]); $j < $max_cols; ++
$j) {
218 $tempRows[$i][] = 'NULL';
222 /* Store the table name so we know where to place the row set */
223 $tbl_attr = $sheet->attributes('table', true);
224 $tables[] = array((string)$tbl_attr['name']);
226 /* Store the current sheet in the accumulator */
227 $rows[] = array((string)$tbl_attr['name'], $col_names, $tempRows);
229 $col_names = array();
240 * Bring accumulated rows into the corresponding table
242 $num_tbls = count($tables);
243 for ($i = 0; $i < $num_tbls; ++
$i) {
244 for ($j = 0; $j < count($rows); ++
$j) {
245 if (! strcmp($tables[$i][TBL_NAME
], $rows[$j][TBL_NAME
])) {
246 if (! isset($tables[$i][COL_NAMES
])) {
247 $tables[$i][] = $rows[$j][COL_NAMES
];
250 $tables[$i][ROWS
] = $rows[$j][ROWS
];
255 /* No longer needed */
258 /* Obtain the best-fit MySQL types for each column */
261 $len = count($tables);
262 for ($i = 0; $i < $len; ++
$i) {
263 $analyses[] = PMA_analyzeTable($tables[$i]);
267 * string $db_name (no backquotes)
269 * array $table = array(table_name, array() column_names, array()() rows)
270 * array $tables = array of "$table"s
272 * array $analysis = array(array() column_types, array() column_sizes)
273 * array $analyses = array of "$analysis"s
275 * array $create = array of SQL strings
277 * array $options = an associative array of options
280 /* Set database name to the currently selected one, if applicable */
283 $options = array('create_db' => false);
289 /* Non-applicable parameters */
292 /* Created and execute necessary SQL statements from data */
293 PMA_buildSQL($db_name, $tables, $analyses, $create, $options);
298 /* Commit any possible data in buffers */
299 PMA_importRunQuery();