ImportOds.class.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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. * @package PhpMyAdmin-Import
  9. * @subpackage ODS
  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. $GLOBALS['skip_import'] = true;
  19. return;
  20. }
  21. /* Get the import interface */
  22. require_once 'libraries/plugins/ImportPlugin.class.php';
  23. /**
  24. * Handles the import for the ODS format
  25. *
  26. * @package PhpMyAdmin-Import
  27. * @subpackage ODS
  28. */
  29. class ImportOds extends ImportPlugin
  30. {
  31. /**
  32. * Constructor
  33. */
  34. public function __construct()
  35. {
  36. $this->setProperties();
  37. }
  38. /**
  39. * Sets the import plugin properties.
  40. * Called in the constructor.
  41. *
  42. * @return void
  43. */
  44. protected function setProperties()
  45. {
  46. $props = 'libraries/properties/';
  47. include_once "$props/plugins/ImportPluginProperties.class.php";
  48. include_once "$props/options/groups/OptionsPropertyRootGroup.class.php";
  49. include_once "$props/options/groups/OptionsPropertyMainGroup.class.php";
  50. include_once "$props/options/items/BoolPropertyItem.class.php";
  51. $importPluginProperties = new ImportPluginProperties();
  52. $importPluginProperties->setText('OpenDocument Spreadsheet');
  53. $importPluginProperties->setExtension('ods');
  54. $importPluginProperties->setOptionsText(__('Options'));
  55. // create the root group that will be the options field for
  56. // $importPluginProperties
  57. // this will be shown as "Format specific options"
  58. $importSpecificOptions = new OptionsPropertyRootGroup();
  59. $importSpecificOptions->setName("Format Specific Options");
  60. // general options main group
  61. $generalOptions = new OptionsPropertyMainGroup();
  62. $generalOptions->setName("general_opts");
  63. // create primary items and add them to the group
  64. $leaf = new BoolPropertyItem();
  65. $leaf->setName("col_names");
  66. $leaf->setText(
  67. __(
  68. 'The first line of the file contains the table column names'
  69. . ' <i>(if this is unchecked, the first line will become part'
  70. . ' of the data)</i>'
  71. )
  72. );
  73. $generalOptions->addProperty($leaf);
  74. $leaf = new BoolPropertyItem();
  75. $leaf->setName("empty_rows");
  76. $leaf->setText(__('Do not import empty rows'));
  77. $generalOptions->addProperty($leaf);
  78. $leaf = new BoolPropertyItem();
  79. $leaf->setName("recognize_percentages");
  80. $leaf->setText(
  81. __(
  82. 'Import percentages as proper decimals <i>(ex. 12.00% to .12)</i>'
  83. )
  84. );
  85. $generalOptions->addProperty($leaf);
  86. $leaf = new BoolPropertyItem();
  87. $leaf->setName("recognize_currency");
  88. $leaf->setText(__('Import currencies <i>(ex. $5.00 to 5.00)</i>'));
  89. $generalOptions->addProperty($leaf);
  90. // add the main group to the root group
  91. $importSpecificOptions->addProperty($generalOptions);
  92. // set the options for the import plugin property item
  93. $importPluginProperties->setOptions($importSpecificOptions);
  94. $this->properties = $importPluginProperties;
  95. }
  96. /**
  97. * This method is called when any PluginManager to which the observer
  98. * is attached calls PluginManager::notify()
  99. *
  100. * @param SplSubject $subject The PluginManager notifying the observer
  101. * of an update.
  102. *
  103. * @return void
  104. */
  105. public function update (SplSubject $subject)
  106. {
  107. }
  108. /**
  109. * Handles the whole import logic
  110. *
  111. * @return void
  112. */
  113. public function doImport()
  114. {
  115. global $db, $error, $timeout_passed, $finished;
  116. $i = 0;
  117. $len = 0;
  118. $buffer = "";
  119. /**
  120. * Read in the file via PMA_importGetNextChunk so that
  121. * it can process compressed files
  122. */
  123. while (! ($finished && $i >= $len) && ! $error && ! $timeout_passed) {
  124. $data = PMA_importGetNextChunk();
  125. if ($data === false) {
  126. /* subtract data we didn't handle yet and stop processing */
  127. $offset -= strlen($buffer);
  128. break;
  129. } elseif ($data === true) {
  130. /* Handle rest of buffer */
  131. } else {
  132. /* Append new data to buffer */
  133. $buffer .= $data;
  134. unset($data);
  135. }
  136. }
  137. unset($data);
  138. /**
  139. * Disable loading of external XML entities.
  140. */
  141. libxml_disable_entity_loader();
  142. /**
  143. * Load the XML string
  144. *
  145. * The option LIBXML_COMPACT is specified because it can
  146. * result in increased performance without the need to
  147. * alter the code in any way. It's basically a freebee.
  148. */
  149. $xml = simplexml_load_string($buffer, "SimpleXMLElement", LIBXML_COMPACT);
  150. unset($buffer);
  151. if ($xml === false) {
  152. $sheets = array();
  153. $message = PMA_Message::error(
  154. __(
  155. 'The XML file specified was either malformed or incomplete.'
  156. . ' Please correct the issue and try again.'
  157. )
  158. );
  159. $error = true;
  160. } else {
  161. $root = $xml->children('office', true)->{'body'}->{'spreadsheet'};
  162. if (empty($root)) {
  163. $sheets = array();
  164. $message = PMA_Message::error(
  165. __('Could not parse OpenDocument Spreadsheet!')
  166. );
  167. $error = true;
  168. } else {
  169. $sheets = $root->children('table', true);
  170. }
  171. }
  172. $tables = array();
  173. $max_cols = 0;
  174. $row_count = 0;
  175. $col_count = 0;
  176. $col_names = array();
  177. $tempRow = array();
  178. $tempRows = array();
  179. $rows = array();
  180. /* Iterate over tables */
  181. foreach ($sheets as $sheet) {
  182. $col_names_in_first_row = isset($_REQUEST['ods_col_names']);
  183. /* Iterate over rows */
  184. foreach ($sheet as $row) {
  185. $type = $row->getName();
  186. if (! strcmp('table-row', $type)) {
  187. /* Iterate over columns */
  188. foreach ($row as $cell) {
  189. $text = $cell->children('text', true);
  190. $cell_attrs = $cell->attributes('office', true);
  191. if (count($text) != 0) {
  192. $attr = $cell->attributes('table', true);
  193. $num_repeat = (int) $attr['number-columns-repeated'];
  194. $num_iterations = $num_repeat ? $num_repeat : 1;
  195. for ($k = 0; $k < $num_iterations; $k++) {
  196. if ($_REQUEST['ods_recognize_percentages']
  197. && ! strcmp(
  198. 'percentage',
  199. $cell_attrs['value-type']
  200. )
  201. ) {
  202. $value = (double)$cell_attrs['value'];
  203. } elseif ($_REQUEST['ods_recognize_currency']
  204. && !strcmp('currency', $cell_attrs['value-type'])
  205. ) {
  206. $value = (double)$cell_attrs['value'];
  207. } else {
  208. /* We need to concatenate all paragraphs */
  209. $values = array();
  210. foreach ($text as $paragraph) {
  211. $values[] = (string)$paragraph;
  212. }
  213. $value = implode("\n", $values);
  214. }
  215. if (! $col_names_in_first_row) {
  216. $tempRow[] = $value;
  217. } else {
  218. $col_names[] = $value;
  219. }
  220. ++$col_count;
  221. }
  222. } else {
  223. /* Number of blank columns repeated */
  224. if ($col_count < count($row->children('table', true)) - 1
  225. ) {
  226. $attr = $cell->attributes('table', true);
  227. $num_null = (int)$attr['number-columns-repeated'];
  228. if ($num_null) {
  229. if (! $col_names_in_first_row) {
  230. for ($i = 0; $i < $num_null; ++$i) {
  231. $tempRow[] = 'NULL';
  232. ++$col_count;
  233. }
  234. } else {
  235. for ($i = 0; $i < $num_null; ++$i) {
  236. $col_names[] = PMA_getColumnAlphaName(
  237. $col_count + 1
  238. );
  239. ++$col_count;
  240. }
  241. }
  242. } else {
  243. if (! $col_names_in_first_row) {
  244. $tempRow[] = 'NULL';
  245. } else {
  246. $col_names[] = PMA_getColumnAlphaName(
  247. $col_count + 1
  248. );
  249. }
  250. ++$col_count;
  251. }
  252. }
  253. }
  254. }
  255. /* Find the widest row */
  256. if ($col_count > $max_cols) {
  257. $max_cols = $col_count;
  258. }
  259. /* Don't include a row that is full of NULL values */
  260. if (! $col_names_in_first_row) {
  261. if ($_REQUEST['ods_empty_rows']) {
  262. foreach ($tempRow as $cell) {
  263. if (strcmp('NULL', $cell)) {
  264. $tempRows[] = $tempRow;
  265. break;
  266. }
  267. }
  268. } else {
  269. $tempRows[] = $tempRow;
  270. }
  271. }
  272. $col_count = 0;
  273. $col_names_in_first_row = false;
  274. $tempRow = array();
  275. }
  276. }
  277. /* Skip over empty sheets */
  278. if (count($tempRows) == 0 || count($tempRows[0]) == 0) {
  279. $col_names = array();
  280. $tempRow = array();
  281. $tempRows = array();
  282. continue;
  283. }
  284. /**
  285. * Fill out each row as necessary to make
  286. * every one exactly as wide as the widest
  287. * row. This included column names.
  288. */
  289. /* Fill out column names */
  290. for ($i = count($col_names); $i < $max_cols; ++$i) {
  291. $col_names[] = PMA_getColumnAlphaName($i + 1);
  292. }
  293. /* Fill out all rows */
  294. $num_rows = count($tempRows);
  295. for ($i = 0; $i < $num_rows; ++$i) {
  296. for ($j = count($tempRows[$i]); $j < $max_cols; ++$j) {
  297. $tempRows[$i][] = 'NULL';
  298. }
  299. }
  300. /* Store the table name so we know where to place the row set */
  301. $tbl_attr = $sheet->attributes('table', true);
  302. $tables[] = array((string)$tbl_attr['name']);
  303. /* Store the current sheet in the accumulator */
  304. $rows[] = array((string)$tbl_attr['name'], $col_names, $tempRows);
  305. $tempRows = array();
  306. $col_names = array();
  307. $max_cols = 0;
  308. }
  309. unset($tempRow);
  310. unset($tempRows);
  311. unset($col_names);
  312. unset($sheets);
  313. unset($xml);
  314. /**
  315. * Bring accumulated rows into the corresponding table
  316. */
  317. $num_tbls = count($tables);
  318. for ($i = 0; $i < $num_tbls; ++$i) {
  319. for ($j = 0; $j < count($rows); ++$j) {
  320. if (! strcmp($tables[$i][TBL_NAME], $rows[$j][TBL_NAME])) {
  321. if (! isset($tables[$i][COL_NAMES])) {
  322. $tables[$i][] = $rows[$j][COL_NAMES];
  323. }
  324. $tables[$i][ROWS] = $rows[$j][ROWS];
  325. }
  326. }
  327. }
  328. /* No longer needed */
  329. unset($rows);
  330. /* Obtain the best-fit MySQL types for each column */
  331. $analyses = array();
  332. $len = count($tables);
  333. for ($i = 0; $i < $len; ++$i) {
  334. $analyses[] = PMA_analyzeTable($tables[$i]);
  335. }
  336. /**
  337. * string $db_name (no backquotes)
  338. *
  339. * array $table = array(table_name, array() column_names, array()() rows)
  340. * array $tables = array of "$table"s
  341. *
  342. * array $analysis = array(array() column_types, array() column_sizes)
  343. * array $analyses = array of "$analysis"s
  344. *
  345. * array $create = array of SQL strings
  346. *
  347. * array $options = an associative array of options
  348. */
  349. /* Set database name to the currently selected one, if applicable */
  350. if (strlen($db)) {
  351. $db_name = $db;
  352. $options = array('create_db' => false);
  353. } else {
  354. $db_name = 'ODS_DB';
  355. $options = null;
  356. }
  357. /* Non-applicable parameters */
  358. $create = null;
  359. /* Created and execute necessary SQL statements from data */
  360. PMA_buildSQL($db_name, $tables, $analyses, $create, $options);
  361. unset($tables);
  362. unset($analyses);
  363. /* Commit any possible data in buffers */
  364. PMA_importRunQuery();
  365. }
  366. }