Upgraded phpmyadmin to 4.0.4 (All Languages) - No modifications yet
[openemr.git] / phpmyadmin / libraries / plugins / import / ImportOds.class.php
blob5a681e97afa98961dee8d1a995338c9bfbd44dc7
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
11 if (! defined('PHPMYADMIN')) {
12 exit;
15 /**
16 * We need way to disable external XML entities processing.
18 if (!function_exists('libxml_disable_entity_loader')) {
19 return;
22 /* Get the import interface */
23 require_once 'libraries/plugins/ImportPlugin.class.php';
25 /**
26 * Handles the import for the ODS format
28 * @package PhpMyAdmin-Import
29 * @subpackage ODS
31 class ImportOds extends ImportPlugin
33 /**
34 * Constructor
36 public function __construct()
38 $this->setProperties();
41 /**
42 * Sets the import plugin properties.
43 * Called in the constructor.
45 * @return void
47 protected function setProperties()
49 $props = 'libraries/properties/';
50 include_once "$props/plugins/ImportPluginProperties.class.php";
51 include_once "$props/options/groups/OptionsPropertyRootGroup.class.php";
52 include_once "$props/options/groups/OptionsPropertyMainGroup.class.php";
53 include_once "$props/options/items/BoolPropertyItem.class.php";
55 $importPluginProperties = new ImportPluginProperties();
56 $importPluginProperties->setText('OpenDocument Spreadsheet');
57 $importPluginProperties->setExtension('ods');
58 $importPluginProperties->setOptionsText(__('Options'));
60 // create the root group that will be the options field for
61 // $importPluginProperties
62 // this will be shown as "Format specific options"
63 $importSpecificOptions = new OptionsPropertyRootGroup();
64 $importSpecificOptions->setName("Format Specific Options");
66 // general options main group
67 $generalOptions = new OptionsPropertyMainGroup();
68 $generalOptions->setName("general_opts");
69 // create primary items and add them to the group
70 $leaf = new BoolPropertyItem();
71 $leaf->setName("col_names");
72 $leaf->setText(
73 __(
74 'The first line of the file contains the table column names'
75 . ' <i>(if this is unchecked, the first line will become part'
76 . ' of the data)</i>'
79 $generalOptions->addProperty($leaf);
80 $leaf = new BoolPropertyItem();
81 $leaf->setName("empty_rows");
82 $leaf->setText(__('Do not import empty rows'));
83 $generalOptions->addProperty($leaf);
84 $leaf = new BoolPropertyItem();
85 $leaf->setName("recognize_percentages");
86 $leaf->setText(
87 __(
88 'Import percentages as proper decimals <i>(ex. 12.00% to .12)</i>'
91 $generalOptions->addProperty($leaf);
92 $leaf = new BoolPropertyItem();
93 $leaf->setName("recognize_currency");
94 $leaf->setText(__('Import currencies <i>(ex. $5.00 to 5.00)</i>'));
95 $generalOptions->addProperty($leaf);
97 // add the main group to the root group
98 $importSpecificOptions->addProperty($generalOptions);
100 // set the options for the import plugin property item
101 $importPluginProperties->setOptions($importSpecificOptions);
102 $this->properties = $importPluginProperties;
106 * This method is called when any PluginManager to which the observer
107 * is attached calls PluginManager::notify()
109 * @param SplSubject $subject The PluginManager notifying the observer
110 * of an update.
112 * @return void
114 public function update (SplSubject $subject)
119 * Handles the whole import logic
121 * @return void
123 public function doImport()
125 global $db, $error, $timeout_passed, $finished;
127 $i = 0;
128 $len = 0;
129 $buffer = "";
132 * Read in the file via PMA_importGetNextChunk so that
133 * it can process compressed files
135 while (! ($finished && $i >= $len) && ! $error && ! $timeout_passed) {
136 $data = PMA_importGetNextChunk();
137 if ($data === false) {
138 /* subtract data we didn't handle yet and stop processing */
139 $offset -= strlen($buffer);
140 break;
141 } elseif ($data === true) {
142 /* Handle rest of buffer */
143 } else {
144 /* Append new data to buffer */
145 $buffer .= $data;
146 unset($data);
150 unset($data);
153 * Disable loading of external XML entities.
155 libxml_disable_entity_loader();
158 * Load the XML string
160 * The option LIBXML_COMPACT is specified because it can
161 * result in increased performance without the need to
162 * alter the code in any way. It's basically a freebee.
164 $xml = simplexml_load_string($buffer, "SimpleXMLElement", LIBXML_COMPACT);
166 unset($buffer);
168 if ($xml === false) {
169 $sheets = array();
170 $message = PMA_Message::error(
172 'The XML file specified was either malformed or incomplete.'
173 . ' Please correct the issue and try again.'
176 $error = true;
177 } else {
178 $root = $xml->children('office', true)->{'body'}->{'spreadsheet'};
179 if (empty($root)) {
180 $sheets = array();
181 $message = PMA_Message::error(
182 __('Could not parse OpenDocument Spreasheet!')
184 $error = true;
185 } else {
186 $sheets = $root->children('table', true);
190 $tables = array();
192 $max_cols = 0;
194 $row_count = 0;
195 $col_count = 0;
196 $col_names = array();
198 $tempRow = array();
199 $tempRows = array();
200 $rows = array();
202 /* Iterate over tables */
203 foreach ($sheets as $sheet) {
204 $col_names_in_first_row = isset($_REQUEST['ods_col_names']);
206 /* Iterate over rows */
207 foreach ($sheet as $row) {
208 $type = $row->getName();
209 if (! strcmp('table-row', $type)) {
210 /* Iterate over columns */
211 foreach ($row as $cell) {
212 $text = $cell->children('text', true);
213 $cell_attrs = $cell->attributes('office', true);
215 if (count($text) != 0) {
216 $attr = $cell->attributes('table', true);
217 $num_repeat = (int) $attr['number-columns-repeated'];
218 $num_iterations = $num_repeat ? $num_repeat : 1;
220 for ($k = 0; $k < $num_iterations; $k++) {
221 if ($_REQUEST['ods_recognize_percentages']
222 && ! strcmp(
223 'percentage',
224 $cell_attrs['value-type']
227 $value = (double)$cell_attrs['value'];
228 } elseif ($_REQUEST['ods_recognize_currency']
229 && !strcmp('currency', $cell_attrs['value-type'])
231 $value = (double)$cell_attrs['value'];
232 } else {
233 /* We need to concatenate all paragraphs */
234 $values = array();
235 foreach ($text as $paragraph) {
236 $values[] = (string)$paragraph;
238 $value = implode("\n", $values);
240 if (! $col_names_in_first_row) {
241 $tempRow[] = $value;
242 } else {
243 $col_names[] = $value;
246 ++$col_count;
248 } else {
249 /* Number of blank columns repeated */
250 if ($col_count < count($row->children('table', true)) - 1
252 $attr = $cell->attributes('table', true);
253 $num_null = (int)$attr['number-columns-repeated'];
255 if ($num_null) {
256 if (! $col_names_in_first_row) {
257 for ($i = 0; $i < $num_null; ++$i) {
258 $tempRow[] = 'NULL';
259 ++$col_count;
261 } else {
262 for ($i = 0; $i < $num_null; ++$i) {
263 $col_names[] = PMA_getColumnAlphaName(
264 $col_count + 1
266 ++$col_count;
269 } else {
270 if (! $col_names_in_first_row) {
271 $tempRow[] = 'NULL';
272 } else {
273 $col_names[] = PMA_getColumnAlphaName(
274 $col_count + 1
278 ++$col_count;
284 /* Find the widest row */
285 if ($col_count > $max_cols) {
286 $max_cols = $col_count;
289 /* Don't include a row that is full of NULL values */
290 if (! $col_names_in_first_row) {
291 if ($_REQUEST['ods_empty_rows']) {
292 foreach ($tempRow as $cell) {
293 if (strcmp('NULL', $cell)) {
294 $tempRows[] = $tempRow;
295 break;
298 } else {
299 $tempRows[] = $tempRow;
303 $col_count = 0;
304 $col_names_in_first_row = false;
305 $tempRow = array();
309 /* Skip over empty sheets */
310 if (count($tempRows) == 0 || count($tempRows[0]) == 0) {
311 $col_names = array();
312 $tempRow = array();
313 $tempRows = array();
314 continue;
318 * Fill out each row as necessary to make
319 * every one exactly as wide as the widest
320 * row. This included column names.
323 /* Fill out column names */
324 for ($i = count($col_names); $i < $max_cols; ++$i) {
325 $col_names[] = PMA_getColumnAlphaName($i + 1);
328 /* Fill out all rows */
329 $num_rows = count($tempRows);
330 for ($i = 0; $i < $num_rows; ++$i) {
331 for ($j = count($tempRows[$i]); $j < $max_cols; ++$j) {
332 $tempRows[$i][] = 'NULL';
336 /* Store the table name so we know where to place the row set */
337 $tbl_attr = $sheet->attributes('table', true);
338 $tables[] = array((string)$tbl_attr['name']);
340 /* Store the current sheet in the accumulator */
341 $rows[] = array((string)$tbl_attr['name'], $col_names, $tempRows);
342 $tempRows = array();
343 $col_names = array();
344 $max_cols = 0;
347 unset($tempRow);
348 unset($tempRows);
349 unset($col_names);
350 unset($sheets);
351 unset($xml);
354 * Bring accumulated rows into the corresponding table
356 $num_tbls = count($tables);
357 for ($i = 0; $i < $num_tbls; ++$i) {
358 for ($j = 0; $j < count($rows); ++$j) {
359 if (! strcmp($tables[$i][TBL_NAME], $rows[$j][TBL_NAME])) {
360 if (! isset($tables[$i][COL_NAMES])) {
361 $tables[$i][] = $rows[$j][COL_NAMES];
364 $tables[$i][ROWS] = $rows[$j][ROWS];
369 /* No longer needed */
370 unset($rows);
372 /* Obtain the best-fit MySQL types for each column */
373 $analyses = array();
375 $len = count($tables);
376 for ($i = 0; $i < $len; ++$i) {
377 $analyses[] = PMA_analyzeTable($tables[$i]);
381 * string $db_name (no backquotes)
383 * array $table = array(table_name, array() column_names, array()() rows)
384 * array $tables = array of "$table"s
386 * array $analysis = array(array() column_types, array() column_sizes)
387 * array $analyses = array of "$analysis"s
389 * array $create = array of SQL strings
391 * array $options = an associative array of options
394 /* Set database name to the currently selected one, if applicable */
395 if (strlen($db)) {
396 $db_name = $db;
397 $options = array('create_db' => false);
398 } else {
399 $db_name = 'ODS_DB';
400 $options = null;
403 /* Non-applicable parameters */
404 $create = null;
406 /* Created and execute necessary SQL statements from data */
407 PMA_buildSQL($db_name, $tables, $analyses, $create, $options);
409 unset($tables);
410 unset($analyses);
412 /* Commit any possible data in buffers */
413 PMA_importRunQuery();