Translation update done using Pootle.
[phpmyadmin/dkf.git] / libraries / import / ods.php
blobc0db7afbfcba64bc4d5f695091693eaffe37e2d0
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * OpenDocument Spreadsheet import plugin for phpMyAdmin
6 * @todo Pretty much everything
7 * @todo Importing of accented characters seems to fail
8 * @version 0.5-beta
9 * @package phpMyAdmin-Import
12 if (! defined('PHPMYADMIN')) {
13 exit;
16 /**
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'),
23 'extension' => 'ods',
24 'options' => array(
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 */
35 return;
38 ini_set('memory_limit', '128M');
39 set_time_limit(120);
41 $i = 0;
42 $len = 0;
43 $buffer = "";
45 /**
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);
54 break;
55 } elseif ($data === TRUE) {
56 /* Handle rest of buffer */
57 } else {
58 /* Append new data to buffer */
59 $buffer .= $data;
60 unset($data);
64 unset($data);
66 /**
67 * Load the XML string
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);
75 unset($buffer);
77 $sheets = $xml->children('office', true)->{'body'}->{'spreadsheet'}->children('table', true);
79 $tables = array();
81 $max_cols = 0;
83 $row_count = 0;
84 $col_count = 0;
85 $col_names = array();
87 $tempRow = array();
88 $tempRows = array();
89 $rows = array();
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'];
110 } else {
111 $tempRow[] = (string)$text;
113 } else {
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'];
118 } else {
119 $col_names[] = (string)$text;
123 ++$col_count;
124 } else {
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'];
130 if ($num_null) {
131 if (! $col_names_in_first_row) {
132 for ($i = 0; $i < $num_null; ++$i) {
133 $tempRow[] = 'NULL';
134 ++$col_count;
136 } else {
137 for ($i = 0; $i < $num_null; ++$i) {
138 $col_names[] = PMA_getColumnAlphaName($col_count + 1);
139 ++$col_count;
142 } else {
143 if (! $col_names_in_first_row) {
144 $tempRow[] = 'NULL';
145 } else {
146 $col_names[] = PMA_getColumnAlphaName($col_count + 1);
149 ++$col_count;
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;
166 break;
169 } else {
170 $tempRows[] = $tempRow;
174 $col_count = 0;
175 $col_names_in_first_row = false;
176 $tempRow = array();
180 /* Skip over empty sheets */
181 if (count($tempRows) == 0 || count($tempRows[0]) == 0) {
182 $col_names = array();
183 $tempRow = array();
184 $tempRows = array();
185 continue;
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);
213 $tempRows = array();
214 $col_names = array();
215 $max_cols = 0;
218 unset($tempRow);
219 unset($tempRows);
220 unset($col_names);
221 unset($sheets);
222 unset($xml);
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 */
241 unset($rows);
243 /* Obtain the best-fit MySQL types for each column */
244 $analyses = array();
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 */
266 if (strlen($db)) {
267 $db_name = $db;
268 $options = array('create_db' => false);
269 } else {
270 $db_name = 'ODS_DB';
271 $options = NULL;
274 /* Non-applicable parameters */
275 $create = NULL;
277 /* Created and execute necessary SQL statements from data */
278 PMA_buildSQL($db_name, $tables, $analyses, $create, $options);
280 unset($tables);
281 unset($analyses);
283 /* Commit any possible data in buffers */
284 PMA_importRunQuery();