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 $attr = $cell->attributes('table', true);
121 $num_repeat = (int) $attr['number-columns-repeated'];
122 $num_iterations = $num_repeat ?
$num_repeat : 1;
124 for ($k = 0; $k < $num_iterations; $k++
) {
125 if (! $col_names_in_first_row) {
126 if ($_REQUEST['ods_recognize_percentages'] && !strcmp('percentage', $cell_attrs['value-type'])) {
127 $tempRow[] = (double)$cell_attrs['value'];
128 } elseif ($_REQUEST['ods_recognize_currency'] && !strcmp('currency', $cell_attrs['value-type'])) {
129 $tempRow[] = (double)$cell_attrs['value'];
131 $tempRow[] = (string)$text;
134 if ($_REQUEST['ods_recognize_percentages'] && !strcmp('percentage', $cell_attrs['value-type'])) {
135 $col_names[] = (double)$cell_attrs['value'];
136 } else if ($_REQUEST['ods_recognize_currency'] && !strcmp('currency', $cell_attrs['value-type'])) {
137 $col_names[] = (double)$cell_attrs['value'];
139 $col_names[] = (string)$text;
146 /* Number of blank columns repeated */
147 if ($col_count < count($row->children('table', true)) - 1) {
148 $attr = $cell->attributes('table', true);
149 $num_null = (int)$attr['number-columns-repeated'];
152 if (! $col_names_in_first_row) {
153 for ($i = 0; $i < $num_null; ++
$i) {
158 for ($i = 0; $i < $num_null; ++
$i) {
159 $col_names[] = PMA_getColumnAlphaName($col_count +
1);
164 if (! $col_names_in_first_row) {
167 $col_names[] = PMA_getColumnAlphaName($col_count +
1);
176 /* Find the widest row */
177 if ($col_count > $max_cols) {
178 $max_cols = $col_count;
181 /* Don't include a row that is full of NULL values */
182 if (! $col_names_in_first_row) {
183 if ($_REQUEST['ods_empty_rows']) {
184 foreach ($tempRow as $cell) {
185 if (strcmp('NULL', $cell)) {
186 $tempRows[] = $tempRow;
191 $tempRows[] = $tempRow;
196 $col_names_in_first_row = false;
201 /* Skip over empty sheets */
202 if (count($tempRows) == 0 ||
count($tempRows[0]) == 0) {
203 $col_names = array();
210 * Fill out each row as necessary to make
211 * every one exactly as wide as the widest
212 * row. This included column names.
215 /* Fill out column names */
216 for ($i = count($col_names); $i < $max_cols; ++
$i) {
217 $col_names[] = PMA_getColumnAlphaName($i +
1);
220 /* Fill out all rows */
221 $num_rows = count($tempRows);
222 for ($i = 0; $i < $num_rows; ++
$i) {
223 for ($j = count($tempRows[$i]); $j < $max_cols; ++
$j) {
224 $tempRows[$i][] = 'NULL';
228 /* Store the table name so we know where to place the row set */
229 $tbl_attr = $sheet->attributes('table', true);
230 $tables[] = array((string)$tbl_attr['name']);
232 /* Store the current sheet in the accumulator */
233 $rows[] = array((string)$tbl_attr['name'], $col_names, $tempRows);
235 $col_names = array();
246 * Bring accumulated rows into the corresponding table
248 $num_tbls = count($tables);
249 for ($i = 0; $i < $num_tbls; ++
$i) {
250 for ($j = 0; $j < count($rows); ++
$j) {
251 if (! strcmp($tables[$i][TBL_NAME
], $rows[$j][TBL_NAME
])) {
252 if (! isset($tables[$i][COL_NAMES
])) {
253 $tables[$i][] = $rows[$j][COL_NAMES
];
256 $tables[$i][ROWS
] = $rows[$j][ROWS
];
261 /* No longer needed */
264 /* Obtain the best-fit MySQL types for each column */
267 $len = count($tables);
268 for ($i = 0; $i < $len; ++
$i) {
269 $analyses[] = PMA_analyzeTable($tables[$i]);
273 * string $db_name (no backquotes)
275 * array $table = array(table_name, array() column_names, array()() rows)
276 * array $tables = array of "$table"s
278 * array $analysis = array(array() column_types, array() column_sizes)
279 * array $analyses = array of "$analysis"s
281 * array $create = array of SQL strings
283 * array $options = an associative array of options
286 /* Set database name to the currently selected one, if applicable */
289 $options = array('create_db' => false);
295 /* Non-applicable parameters */
298 /* Created and execute necessary SQL statements from data */
299 PMA_buildSQL($db_name, $tables, $analyses, $create, $options);
304 /* Commit any possible data in buffers */
305 PMA_importRunQuery();