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' => 'strImportODS',
25 array('type' => 'bool', 'name' => 'col_names', 'text' => 'strImportColNames'),
26 array('type' => 'bool', 'name' => 'empty_rows', 'text' => 'strImportEmptyRows'),
27 array('type' => 'bool', 'name' => 'recognize_percentages', 'text' => 'strImportODSPercents'),
28 array('type' => 'bool', 'name' => 'recognize_currency', 'text' => 'strImportODSCurrency'),
30 'options_text' => 'strOptions',
32 /* We do not define function when plugin is just queried for information above */
36 ini_set('memory_limit', '128M');
44 * Read in the file via PMA_importGetNextChunk so that
45 * it can process compressed files
47 while (! ($finished && $i >= $len) && ! $error && ! $timeout_passed) {
48 $data = PMA_importGetNextChunk();
49 if ($data === FALSE) {
50 /* subtract data we didn't handle yet and stop processing */
51 $offset -= strlen($buffer);
53 } elseif ($data === TRUE) {
54 /* Handle rest of buffer */
56 /* Append new data to buffer */
67 * The option LIBXML_COMPACT is specified because it can
68 * result in increased performance without the need to
69 * alter the code in any way. It's basically a freebee.
71 $xml = simplexml_load_string($buffer, "SimpleXMLElement", LIBXML_COMPACT
);
75 $sheets = $xml->children('office', true)->{'body'}->{'spreadsheet'}->children('table', true);
89 /* Iterate over tables */
90 foreach ($sheets as $sheet) {
91 $col_names_in_first_row = $_REQUEST['ods_col_names'];
93 /* Iterate over rows */
94 foreach ($sheet as $row) {
95 $type = $row->getName();
96 if (! strcmp('table-row', $type)) {
97 /* Iterate over columns */
98 foreach ($row as $cell) {
99 $text = $cell->children('text', true);
100 $cell_attrs = $cell->attributes('office', true);
102 if (count($text) != 0) {
103 if (! $col_names_in_first_row) {
104 if ($_REQUEST['ods_recognize_percentages'] && !strcmp('percentage', $cell_attrs['value-type'])) {
105 $tempRow[] = (double)$cell_attrs['value'];
106 } elseif ($_REQUEST['ods_recognize_currency'] && !strcmp('currency', $cell_attrs['value-type'])) {
107 $tempRow[] = (double)$cell_attrs['value'];
109 $tempRow[] = (string)$text;
112 if ($_REQUEST['ods_recognize_percentages'] && !strcmp('percentage', $cell_attrs['value-type'])) {
113 $col_names[] = (double)$cell_attrs['value'];
114 } else if ($_REQUEST['ods_recognize_currency'] && !strcmp('currency', $cell_attrs['value-type'])) {
115 $col_names[] = (double)$cell_attrs['value'];
117 $col_names[] = (string)$text;
123 /* Number of blank columns repeated */
124 if ($col_count < count($row->children('table', true)) - 1) {
125 $attr = $cell->attributes('table', true);
126 $num_null = (int)$attr['number-columns-repeated'];
129 if (! $col_names_in_first_row) {
130 for ($i = 0; $i < $num_null; ++
$i) {
135 for ($i = 0; $i < $num_null; ++
$i) {
136 $col_names[] = PMA_getColumnAlphaName($col_count +
1);
141 if (! $col_names_in_first_row) {
144 $col_names[] = PMA_getColumnAlphaName($col_count +
1);
153 /* Find the widest row */
154 if ($col_count > $max_cols) {
155 $max_cols = $col_count;
158 /* Don't include a row that is full of NULL values */
159 if (! $col_names_in_first_row) {
160 if ($_REQUEST['ods_empty_rows']) {
161 foreach ($tempRow as $cell) {
162 if (strcmp('NULL', $cell)) {
163 $tempRows[] = $tempRow;
168 $tempRows[] = $tempRow;
173 $col_names_in_first_row = false;
178 /* Skip over empty sheets */
179 if (count($tempRows) == 0 ||
count($tempRows[0]) == 0) {
180 $col_names = array();
187 * Fill out each row as necessary to make
188 * every one exactly as wide as the widest
189 * row. This included column names.
192 /* Fill out column names */
193 for ($i = count($col_names); $i < $max_cols; ++
$i) {
194 $col_names[] = PMA_getColumnAlphaName($i +
1);
197 /* Fill out all rows */
198 $num_rows = count($tempRows);
199 for ($i = 0; $i < $num_rows; ++
$i) {
200 for ($j = count($tempRows[$i]); $j < $max_cols; ++
$j) {
201 $tempRows[$i][] = 'NULL';
205 /* Store the table name so we know where to place the row set */
206 $tbl_attr = $sheet->attributes('table', true);
207 $tables[] = array((string)$tbl_attr['name']);
209 /* Store the current sheet in the accumulator */
210 $rows[] = array((string)$tbl_attr['name'], $col_names, $tempRows);
212 $col_names = array();
223 * Bring accumulated rows into the corresponding table
225 $num_tbls = count($tables);
226 for ($i = 0; $i < $num_tbls; ++
$i) {
227 for ($j = 0; $j < count($rows); ++
$j) {
228 if (! strcmp($tables[$i][TBL_NAME
], $rows[$j][TBL_NAME
])) {
229 if (! isset($tables[$i][COL_NAMES
])) {
230 $tables[$i][] = $rows[$j][COL_NAMES
];
233 $tables[$i][ROWS
] = $rows[$j][ROWS
];
238 /* No longer needed */
241 /* Obtain the best-fit MySQL types for each column */
244 $len = count($tables);
245 for ($i = 0; $i < $len; ++
$i) {
246 $analyses[] = PMA_analyzeTable($tables[$i]);
250 * string $db_name (no backquotes)
252 * array $table = array(table_name, array() column_names, array()() rows)
253 * array $tables = array of "$table"s
255 * array $analysis = array(array() column_types, array() column_sizes)
256 * array $analyses = array of "$analysis"s
258 * array $create = array of SQL strings
260 * array $options = an associative array of options
263 /* Set database name to the currently selected one, if applicable */
266 $options = array('create_db' => false);
272 /* Non-applicable parameters */
275 /* Created and execute necessary SQL statements from data */
276 PMA_buildSQL($db_name, $tables, $analyses, $create, $options);
281 /* Commit any possible data in buffers */
282 PMA_importRunQuery();