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
9 * @package phpMyAdmin-Import
12 if (! defined('PHPMYADMIN')) {
17 * The possible scopes for $plugin_param are: 'table', 'database', and 'server'
20 if (isset($plugin_list)) {
21 $plugin_list['ods'] = array(
22 'text' => __('Open Document Spreadsheet'),
25 array('type' => 'begin_group', 'name' => 'general_opts'),
26 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>')),
27 array('type' => 'bool', 'name' => 'empty_rows', 'text' => __('Do not import empty rows')),
28 array('type' => 'bool', 'name' => 'recognize_percentages', 'text' => __('Import percentages as proper decimals <i>(ex. 12.00% to .12)</i>')),
29 array('type' => 'bool', 'name' => 'recognize_currency', 'text' => __('Import currencies <i>(ex. $5.00 to 5.00)</i>')),
30 array('type' => 'end_group')
32 'options_text' => __('Options'),
34 /* We do not define function when plugin is just queried for information above */
38 ini_set('memory_limit', '128M');
46 * Read in the file via PMA_importGetNextChunk so that
47 * it can process compressed files
49 while (! ($finished && $i >= $len) && ! $error && ! $timeout_passed) {
50 $data = PMA_importGetNextChunk();
51 if ($data === FALSE) {
52 /* subtract data we didn't handle yet and stop processing */
53 $offset -= strlen($buffer);
55 } elseif ($data === TRUE) {
56 /* Handle rest of buffer */
58 /* Append new data to buffer */
69 * The option LIBXML_COMPACT is specified because it can
70 * result in increased performance without the need to
71 * alter the code in any way. It's basically a freebee.
73 $xml = simplexml_load_string($buffer, "SimpleXMLElement", LIBXML_COMPACT
);
77 $sheets = $xml->children('office', true)->{'body'}->{'spreadsheet'}->children('table', true);
91 /* Iterate over tables */
92 foreach ($sheets as $sheet) {
93 $col_names_in_first_row = isset($_REQUEST['ods_col_names']);
95 /* Iterate over rows */
96 foreach ($sheet as $row) {
97 $type = $row->getName();
98 if (! strcmp('table-row', $type)) {
99 /* Iterate over columns */
100 foreach ($row as $cell) {
101 $text = $cell->children('text', true);
102 $cell_attrs = $cell->attributes('office', true);
104 if (count($text) != 0) {
105 if (! $col_names_in_first_row) {
106 if ($_REQUEST['ods_recognize_percentages'] && !strcmp('percentage', $cell_attrs['value-type'])) {
107 $tempRow[] = (double)$cell_attrs['value'];
108 } elseif ($_REQUEST['ods_recognize_currency'] && !strcmp('currency', $cell_attrs['value-type'])) {
109 $tempRow[] = (double)$cell_attrs['value'];
111 $tempRow[] = (string)$text;
114 if ($_REQUEST['ods_recognize_percentages'] && !strcmp('percentage', $cell_attrs['value-type'])) {
115 $col_names[] = (double)$cell_attrs['value'];
116 } else if ($_REQUEST['ods_recognize_currency'] && !strcmp('currency', $cell_attrs['value-type'])) {
117 $col_names[] = (double)$cell_attrs['value'];
119 $col_names[] = (string)$text;
125 /* Number of blank columns repeated */
126 if ($col_count < count($row->children('table', true)) - 1) {
127 $attr = $cell->attributes('table', true);
128 $num_null = (int)$attr['number-columns-repeated'];
131 if (! $col_names_in_first_row) {
132 for ($i = 0; $i < $num_null; ++
$i) {
137 for ($i = 0; $i < $num_null; ++
$i) {
138 $col_names[] = PMA_getColumnAlphaName($col_count +
1);
143 if (! $col_names_in_first_row) {
146 $col_names[] = PMA_getColumnAlphaName($col_count +
1);
155 /* Find the widest row */
156 if ($col_count > $max_cols) {
157 $max_cols = $col_count;
160 /* Don't include a row that is full of NULL values */
161 if (! $col_names_in_first_row) {
162 if ($_REQUEST['ods_empty_rows']) {
163 foreach ($tempRow as $cell) {
164 if (strcmp('NULL', $cell)) {
165 $tempRows[] = $tempRow;
170 $tempRows[] = $tempRow;
175 $col_names_in_first_row = false;
180 /* Skip over empty sheets */
181 if (count($tempRows) == 0 ||
count($tempRows[0]) == 0) {
182 $col_names = array();
189 * Fill out each row as necessary to make
190 * every one exactly as wide as the widest
191 * row. This included column names.
194 /* Fill out column names */
195 for ($i = count($col_names); $i < $max_cols; ++
$i) {
196 $col_names[] = PMA_getColumnAlphaName($i +
1);
199 /* Fill out all rows */
200 $num_rows = count($tempRows);
201 for ($i = 0; $i < $num_rows; ++
$i) {
202 for ($j = count($tempRows[$i]); $j < $max_cols; ++
$j) {
203 $tempRows[$i][] = 'NULL';
207 /* Store the table name so we know where to place the row set */
208 $tbl_attr = $sheet->attributes('table', true);
209 $tables[] = array((string)$tbl_attr['name']);
211 /* Store the current sheet in the accumulator */
212 $rows[] = array((string)$tbl_attr['name'], $col_names, $tempRows);
214 $col_names = array();
225 * Bring accumulated rows into the corresponding table
227 $num_tbls = count($tables);
228 for ($i = 0; $i < $num_tbls; ++
$i) {
229 for ($j = 0; $j < count($rows); ++
$j) {
230 if (! strcmp($tables[$i][TBL_NAME
], $rows[$j][TBL_NAME
])) {
231 if (! isset($tables[$i][COL_NAMES
])) {
232 $tables[$i][] = $rows[$j][COL_NAMES
];
235 $tables[$i][ROWS
] = $rows[$j][ROWS
];
240 /* No longer needed */
243 /* Obtain the best-fit MySQL types for each column */
246 $len = count($tables);
247 for ($i = 0; $i < $len; ++
$i) {
248 $analyses[] = PMA_analyzeTable($tables[$i]);
252 * string $db_name (no backquotes)
254 * array $table = array(table_name, array() column_names, array()() rows)
255 * array $tables = array of "$table"s
257 * array $analysis = array(array() column_types, array() column_sizes)
258 * array $analyses = array of "$analysis"s
260 * array $create = array of SQL strings
262 * array $options = an associative array of options
265 /* Set database name to the currently selected one, if applicable */
268 $options = array('create_db' => false);
274 /* Non-applicable parameters */
277 /* Created and execute necessary SQL statements from data */
278 PMA_buildSQL($db_name, $tables, $analyses, $create, $options);
283 /* Commit any possible data in buffers */
284 PMA_importRunQuery();