PageRenderTime 63ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 0ms

/www/addons/pma/libraries/import/ods.php

https://bitbucket.org/onekit/mrhost
PHP | 307 lines | 192 code | 43 blank | 72 comment | 56 complexity | ba4346f3549af8410c2ae0b1ca7f8945 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * OpenDocument Spreadsheet import plugin for phpMyAdmin
  5. *
  6. * @todo Pretty much everything
  7. * @todo Importing of accented characters seems to fail
  8. * @version 0.5-beta
  9. * @package phpMyAdmin-Import
  10. */
  11. if (! defined('PHPMYADMIN')) {
  12. exit;
  13. }
  14. /**
  15. * We need way to disable external XML entities processing.
  16. */
  17. if (!function_exists('libxml_disable_entity_loader')) {
  18. return;
  19. }
  20. /**
  21. * The possible scopes for $plugin_param are: 'table', 'database', and 'server'
  22. */
  23. if (isset($plugin_list)) {
  24. $plugin_list['ods'] = array(
  25. 'text' => __('Open Document Spreadsheet'),
  26. 'extension' => 'ods',
  27. 'options' => array(
  28. array('type' => 'begin_group', 'name' => 'general_opts'),
  29. 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>')),
  30. array('type' => 'bool', 'name' => 'empty_rows', 'text' => __('Do not import empty rows')),
  31. array('type' => 'bool', 'name' => 'recognize_percentages', 'text' => __('Import percentages as proper decimals <i>(ex. 12.00% to .12)</i>')),
  32. array('type' => 'bool', 'name' => 'recognize_currency', 'text' => __('Import currencies <i>(ex. $5.00 to 5.00)</i>')),
  33. array('type' => 'end_group')
  34. ),
  35. 'options_text' => __('Options'),
  36. );
  37. /* We do not define function when plugin is just queried for information above */
  38. return;
  39. }
  40. $i = 0;
  41. $len = 0;
  42. $buffer = "";
  43. /**
  44. * Read in the file via PMA_importGetNextChunk so that
  45. * it can process compressed files
  46. */
  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);
  52. break;
  53. } elseif ($data === TRUE) {
  54. /* Handle rest of buffer */
  55. } else {
  56. /* Append new data to buffer */
  57. $buffer .= $data;
  58. unset($data);
  59. }
  60. }
  61. unset($data);
  62. /**
  63. * Disable loading of external XML entities.
  64. */
  65. libxml_disable_entity_loader();
  66. /**
  67. * Load the XML string
  68. *
  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.
  72. */
  73. $xml = simplexml_load_string($buffer, "SimpleXMLElement", LIBXML_COMPACT);
  74. unset($buffer);
  75. if ($xml === FALSE) {
  76. $sheets = array();
  77. /* TODO: this message should be improved later, used existing because of string freeze */
  78. $message = PMA_Message::error(__('Error in Processing Request'));
  79. $error = TRUE;
  80. } else {
  81. $sheets = $xml->children('office', true)->{'body'}->{'spreadsheet'}->children('table', true);
  82. }
  83. $tables = array();
  84. $max_cols = 0;
  85. $row_count = 0;
  86. $col_count = 0;
  87. $col_names = array();
  88. $tempRow = array();
  89. $tempRows = array();
  90. $rows = array();
  91. /* Iterate over tables */
  92. foreach ($sheets as $sheet) {
  93. $col_names_in_first_row = isset($_REQUEST['ods_col_names']);
  94. /* Iterate over rows */
  95. foreach ($sheet as $row) {
  96. $type = $row->getName();
  97. if (! strcmp('table-row', $type)) {
  98. /* Iterate over columns */
  99. foreach ($row as $cell) {
  100. $text = $cell->children('text', true);
  101. $cell_attrs = $cell->attributes('office', true);
  102. if (count($text) != 0) {
  103. $attr = $cell->attributes('table', true);
  104. $num_repeat = (int) $attr['number-columns-repeated'];
  105. $num_iterations = $num_repeat ? $num_repeat : 1;
  106. for ($k = 0; $k < $num_iterations; $k++) {
  107. if (! $col_names_in_first_row) {
  108. if ($_REQUEST['ods_recognize_percentages'] && !strcmp('percentage', $cell_attrs['value-type'])) {
  109. $tempRow[] = (double)$cell_attrs['value'];
  110. } elseif ($_REQUEST['ods_recognize_currency'] && !strcmp('currency', $cell_attrs['value-type'])) {
  111. $tempRow[] = (double)$cell_attrs['value'];
  112. } else {
  113. $tempRow[] = (string)$text;
  114. }
  115. } else {
  116. if ($_REQUEST['ods_recognize_percentages'] && !strcmp('percentage', $cell_attrs['value-type'])) {
  117. $col_names[] = (double)$cell_attrs['value'];
  118. } else if ($_REQUEST['ods_recognize_currency'] && !strcmp('currency', $cell_attrs['value-type'])) {
  119. $col_names[] = (double)$cell_attrs['value'];
  120. } else {
  121. $col_names[] = (string)$text;
  122. }
  123. }
  124. ++$col_count;
  125. }
  126. } else {
  127. /* Number of blank columns repeated */
  128. if ($col_count < count($row->children('table', true)) - 1) {
  129. $attr = $cell->attributes('table', true);
  130. $num_null = (int)$attr['number-columns-repeated'];
  131. if ($num_null) {
  132. if (! $col_names_in_first_row) {
  133. for ($i = 0; $i < $num_null; ++$i) {
  134. $tempRow[] = 'NULL';
  135. ++$col_count;
  136. }
  137. } else {
  138. for ($i = 0; $i < $num_null; ++$i) {
  139. $col_names[] = PMA_getColumnAlphaName($col_count + 1);
  140. ++$col_count;
  141. }
  142. }
  143. } else {
  144. if (! $col_names_in_first_row) {
  145. $tempRow[] = 'NULL';
  146. } else {
  147. $col_names[] = PMA_getColumnAlphaName($col_count + 1);
  148. }
  149. ++$col_count;
  150. }
  151. }
  152. }
  153. }
  154. /* Find the widest row */
  155. if ($col_count > $max_cols) {
  156. $max_cols = $col_count;
  157. }
  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;
  164. break;
  165. }
  166. }
  167. } else {
  168. $tempRows[] = $tempRow;
  169. }
  170. }
  171. $col_count = 0;
  172. $col_names_in_first_row = false;
  173. $tempRow = array();
  174. }
  175. }
  176. /* Skip over empty sheets */
  177. if (count($tempRows) == 0 || count($tempRows[0]) == 0) {
  178. $col_names = array();
  179. $tempRow = array();
  180. $tempRows = array();
  181. continue;
  182. }
  183. /**
  184. * Fill out each row as necessary to make
  185. * every one exactly as wide as the widest
  186. * row. This included column names.
  187. */
  188. /* Fill out column names */
  189. for ($i = count($col_names); $i < $max_cols; ++$i) {
  190. $col_names[] = PMA_getColumnAlphaName($i + 1);
  191. }
  192. /* Fill out all rows */
  193. $num_rows = count($tempRows);
  194. for ($i = 0; $i < $num_rows; ++$i) {
  195. for ($j = count($tempRows[$i]); $j < $max_cols; ++$j) {
  196. $tempRows[$i][] = 'NULL';
  197. }
  198. }
  199. /* Store the table name so we know where to place the row set */
  200. $tbl_attr = $sheet->attributes('table', true);
  201. $tables[] = array((string)$tbl_attr['name']);
  202. /* Store the current sheet in the accumulator */
  203. $rows[] = array((string)$tbl_attr['name'], $col_names, $tempRows);
  204. $tempRows = array();
  205. $col_names = array();
  206. $max_cols = 0;
  207. }
  208. unset($tempRow);
  209. unset($tempRows);
  210. unset($col_names);
  211. unset($sheets);
  212. unset($xml);
  213. /**
  214. * Bring accumulated rows into the corresponding table
  215. */
  216. $num_tbls = count($tables);
  217. for ($i = 0; $i < $num_tbls; ++$i) {
  218. for ($j = 0; $j < count($rows); ++$j) {
  219. if (! strcmp($tables[$i][TBL_NAME], $rows[$j][TBL_NAME])) {
  220. if (! isset($tables[$i][COL_NAMES])) {
  221. $tables[$i][] = $rows[$j][COL_NAMES];
  222. }
  223. $tables[$i][ROWS] = $rows[$j][ROWS];
  224. }
  225. }
  226. }
  227. /* No longer needed */
  228. unset($rows);
  229. /* Obtain the best-fit MySQL types for each column */
  230. $analyses = array();
  231. $len = count($tables);
  232. for ($i = 0; $i < $len; ++$i) {
  233. $analyses[] = PMA_analyzeTable($tables[$i]);
  234. }
  235. /**
  236. * string $db_name (no backquotes)
  237. *
  238. * array $table = array(table_name, array() column_names, array()() rows)
  239. * array $tables = array of "$table"s
  240. *
  241. * array $analysis = array(array() column_types, array() column_sizes)
  242. * array $analyses = array of "$analysis"s
  243. *
  244. * array $create = array of SQL strings
  245. *
  246. * array $options = an associative array of options
  247. */
  248. /* Set database name to the currently selected one, if applicable */
  249. if (strlen($db)) {
  250. $db_name = $db;
  251. $options = array('create_db' => false);
  252. } else {
  253. $db_name = 'ODS_DB';
  254. $options = NULL;
  255. }
  256. /* Non-applicable parameters */
  257. $create = NULL;
  258. /* Created and execute necessary SQL statements from data */
  259. PMA_buildSQL($db_name, $tables, $analyses, $create, $options);
  260. unset($tables);
  261. unset($analyses);
  262. /* Commit any possible data in buffers */
  263. PMA_importRunQuery();
  264. ?>