Fix format strings
[phpmyadmin/madhuracj.git] / libraries / import / ods.php
blob8af40f43f2688072a61e56c0cf33df0fb566aaab
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 * @package PhpMyAdmin-Import
9 * @subpackage ODS
12 if (! defined('PHPMYADMIN')) {
13 exit;
16 /**
17 * We need way to disable external XML entities processing.
19 if (!function_exists('libxml_disable_entity_loader')) {
20 return;
23 /**
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'),
30 'extension' => 'ods',
31 'options' => array(
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 */
42 return;
45 $i = 0;
46 $len = 0;
47 $buffer = "";
49 /**
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);
58 break;
59 } elseif ($data === true) {
60 /* Handle rest of buffer */
61 } else {
62 /* Append new data to buffer */
63 $buffer .= $data;
64 unset($data);
68 unset($data);
70 /**
71 * Disable loading of external XML entities.
73 libxml_disable_entity_loader();
75 /**
76 * Load the XML string
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);
84 unset($buffer);
86 if ($xml === false) {
87 $sheets = array();
88 $message = PMA_Message::error(__('The XML file specified was either malformed or incomplete. Please correct the issue and try again.'));
89 $error = true;
90 } else {
91 $sheets = $xml->children('office', true)->{'body'}->{'spreadsheet'}->children('table', true);
94 $tables = array();
96 $max_cols = 0;
98 $row_count = 0;
99 $col_count = 0;
100 $col_names = array();
102 $tempRow = array();
103 $tempRows = array();
104 $rows = 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'];
125 } else {
126 $tempRow[] = (string)$text;
128 } else {
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'];
133 } else {
134 $col_names[] = (string)$text;
138 ++$col_count;
139 } else {
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'];
145 if ($num_null) {
146 if (! $col_names_in_first_row) {
147 for ($i = 0; $i < $num_null; ++$i) {
148 $tempRow[] = 'NULL';
149 ++$col_count;
151 } else {
152 for ($i = 0; $i < $num_null; ++$i) {
153 $col_names[] = PMA_getColumnAlphaName($col_count + 1);
154 ++$col_count;
157 } else {
158 if (! $col_names_in_first_row) {
159 $tempRow[] = 'NULL';
160 } else {
161 $col_names[] = PMA_getColumnAlphaName($col_count + 1);
164 ++$col_count;
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;
181 break;
184 } else {
185 $tempRows[] = $tempRow;
189 $col_count = 0;
190 $col_names_in_first_row = false;
191 $tempRow = array();
195 /* Skip over empty sheets */
196 if (count($tempRows) == 0 || count($tempRows[0]) == 0) {
197 $col_names = array();
198 $tempRow = array();
199 $tempRows = array();
200 continue;
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);
228 $tempRows = array();
229 $col_names = array();
230 $max_cols = 0;
233 unset($tempRow);
234 unset($tempRows);
235 unset($col_names);
236 unset($sheets);
237 unset($xml);
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 */
256 unset($rows);
258 /* Obtain the best-fit MySQL types for each column */
259 $analyses = array();
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 */
281 if (strlen($db)) {
282 $db_name = $db;
283 $options = array('create_db' => false);
284 } else {
285 $db_name = 'ODS_DB';
286 $options = null;
289 /* Non-applicable parameters */
290 $create = null;
292 /* Created and execute necessary SQL statements from data */
293 PMA_buildSQL($db_name, $tables, $analyses, $create, $options);
295 unset($tables);
296 unset($analyses);
298 /* Commit any possible data in buffers */
299 PMA_importRunQuery();