ImportOds.class.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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. $GLOBALS['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. $GLOBALS['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. $GLOBALS['error'] = true;
  160. } else {
  161. $root = $xml->children('office', true)->{'body'}->{'spreadsheet'};
  162. if (empty($root)) {
  163. $sheets = array();
  164. $GLOBALS['message'] = PMA_Message::error(
  165. __('Could not parse OpenDocument Spreadsheet!')
  166. );
  167. $GLOBALS['error'] = true;
  168. } else {
  169. $sheets = $root->children('table', true);
  170. }
  171. }
  172. $tables = array();
  173. $max_cols = 0;
  174. $col_count = 0;
  175. $col_names = array();
  176. $tempRow = array();
  177. $tempRows = array();
  178. $rows = array();
  179. /* Iterate over tables */
  180. foreach ($sheets as $sheet) {
  181. $col_names_in_first_row = isset($_REQUEST['ods_col_names']);
  182. /* Iterate over rows */
  183. foreach ($sheet as $row) {
  184. $type = $row->getName();
  185. if (strcmp('table-row', $type)) {
  186. continue;
  187. }
  188. /* Iterate over columns */
  189. foreach ($row as $cell) {
  190. $text = $cell->children('text', true);
  191. $cell_attrs = $cell->attributes('office', true);
  192. if (count($text) != 0) {
  193. $attr = $cell->attributes('table', true);
  194. $num_repeat = (int) $attr['number-columns-repeated'];
  195. $num_iterations = $num_repeat ? $num_repeat : 1;
  196. for ($k = 0; $k < $num_iterations; $k++) {
  197. $value = $this->getValue($cell_attrs, $text);
  198. if (! $col_names_in_first_row) {
  199. $tempRow[] = $value;
  200. } else {
  201. $col_names[] = $value;
  202. }
  203. ++$col_count;
  204. }
  205. continue;
  206. }
  207. /* Number of blank columns repeated */
  208. if ($col_count >= count($row->children('table', true)) - 1) {
  209. continue;
  210. }
  211. $attr = $cell->attributes('table', true);
  212. $num_null = (int)$attr['number-columns-repeated'];
  213. if ($num_null) {
  214. if (! $col_names_in_first_row) {
  215. for ($i = 0; $i < $num_null; ++$i) {
  216. $tempRow[] = 'NULL';
  217. ++$col_count;
  218. }
  219. } else {
  220. for ($i = 0; $i < $num_null; ++$i) {
  221. $col_names[] = PMA_getColumnAlphaName(
  222. $col_count + 1
  223. );
  224. ++$col_count;
  225. }
  226. }
  227. } else {
  228. if (! $col_names_in_first_row) {
  229. $tempRow[] = 'NULL';
  230. } else {
  231. $col_names[] = PMA_getColumnAlphaName(
  232. $col_count + 1
  233. );
  234. }
  235. ++$col_count;
  236. }
  237. } //Endforeach
  238. /* Find the widest row */
  239. if ($col_count > $max_cols) {
  240. $max_cols = $col_count;
  241. }
  242. /* Don't include a row that is full of NULL values */
  243. if (! $col_names_in_first_row) {
  244. if ($_REQUEST['ods_empty_rows']) {
  245. foreach ($tempRow as $cell) {
  246. if (strcmp('NULL', $cell)) {
  247. $tempRows[] = $tempRow;
  248. break;
  249. }
  250. }
  251. } else {
  252. $tempRows[] = $tempRow;
  253. }
  254. }
  255. $col_count = 0;
  256. $col_names_in_first_row = false;
  257. $tempRow = array();
  258. }
  259. /* Skip over empty sheets */
  260. if (count($tempRows) == 0 || count($tempRows[0]) == 0) {
  261. $col_names = array();
  262. $tempRow = array();
  263. $tempRows = array();
  264. continue;
  265. }
  266. /**
  267. * Fill out each row as necessary to make
  268. * every one exactly as wide as the widest
  269. * row. This included column names.
  270. */
  271. /* Fill out column names */
  272. for ($i = count($col_names); $i < $max_cols; ++$i) {
  273. $col_names[] = PMA_getColumnAlphaName($i + 1);
  274. }
  275. /* Fill out all rows */
  276. $num_rows = count($tempRows);
  277. for ($i = 0; $i < $num_rows; ++$i) {
  278. for ($j = count($tempRows[$i]); $j < $max_cols; ++$j) {
  279. $tempRows[$i][] = 'NULL';
  280. }
  281. }
  282. /* Store the table name so we know where to place the row set */
  283. $tbl_attr = $sheet->attributes('table', true);
  284. $tables[] = array((string)$tbl_attr['name']);
  285. /* Store the current sheet in the accumulator */
  286. $rows[] = array((string)$tbl_attr['name'], $col_names, $tempRows);
  287. $tempRows = array();
  288. $col_names = array();
  289. $max_cols = 0;
  290. }
  291. unset($tempRow);
  292. unset($tempRows);
  293. unset($col_names);
  294. unset($sheets);
  295. unset($xml);
  296. /**
  297. * Bring accumulated rows into the corresponding table
  298. */
  299. $num_tbls = count($tables);
  300. for ($i = 0; $i < $num_tbls; ++$i) {
  301. for ($j = 0; $j < count($rows); ++$j) {
  302. if (strcmp($tables[$i][TBL_NAME], $rows[$j][TBL_NAME])) {
  303. continue;
  304. }
  305. if (! isset($tables[$i][COL_NAMES])) {
  306. $tables[$i][] = $rows[$j][COL_NAMES];
  307. }
  308. $tables[$i][ROWS] = $rows[$j][ROWS];
  309. }
  310. }
  311. /* No longer needed */
  312. unset($rows);
  313. /* Obtain the best-fit MySQL types for each column */
  314. $analyses = array();
  315. $len = count($tables);
  316. for ($i = 0; $i < $len; ++$i) {
  317. $analyses[] = PMA_analyzeTable($tables[$i]);
  318. }
  319. /**
  320. * string $db_name (no backquotes)
  321. *
  322. * array $table = array(table_name, array() column_names, array()() rows)
  323. * array $tables = array of "$table"s
  324. *
  325. * array $analysis = array(array() column_types, array() column_sizes)
  326. * array $analyses = array of "$analysis"s
  327. *
  328. * array $create = array of SQL strings
  329. *
  330. * array $options = an associative array of options
  331. */
  332. /* Set database name to the currently selected one, if applicable */
  333. if (strlen($db)) {
  334. $db_name = $db;
  335. $options = array('create_db' => false);
  336. } else {
  337. $db_name = 'ODS_DB';
  338. $options = null;
  339. }
  340. /* Non-applicable parameters */
  341. $create = null;
  342. /* Created and execute necessary SQL statements from data */
  343. PMA_buildSQL($db_name, $tables, $analyses, $create, $options);
  344. unset($tables);
  345. unset($analyses);
  346. /* Commit any possible data in buffers */
  347. PMA_importRunQuery();
  348. }
  349. /**
  350. * Get value
  351. *
  352. * @param array $cell_attrs Cell attributes
  353. * @param array $text Texts
  354. *
  355. * @return float|string
  356. */
  357. protected function getValue($cell_attrs, $text)
  358. {
  359. if ($_REQUEST['ods_recognize_percentages']
  360. && !strcmp(
  361. 'percentage',
  362. $cell_attrs['value-type']
  363. )
  364. ) {
  365. $value = (double)$cell_attrs['value'];
  366. return $value;
  367. } elseif ($_REQUEST['ods_recognize_currency']
  368. && !strcmp('currency', $cell_attrs['value-type'])
  369. ) {
  370. $value = (double)$cell_attrs['value'];
  371. return $value;
  372. } else {
  373. /* We need to concatenate all paragraphs */
  374. $values = array();
  375. foreach ($text as $paragraph) {
  376. $values[] = (string)$paragraph;
  377. }
  378. $value = implode("\n", $values);
  379. return $value;
  380. }
  381. }
  382. }