PageRenderTime 56ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/pma/libraries/export/odt.php

https://bitbucket.org/StasPiv/playzone
PHP | 412 lines | 273 code | 29 blank | 110 comment | 63 complexity | 946ca9bb79d0fe38071af8e8315c1491 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, GPL-2.0, LGPL-2.1
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Set of functions used to build OpenDocument Text dumps of tables
  5. *
  6. * @package phpMyAdmin-Export-ODT
  7. * @version $Id: odt.php 12349 2009-04-14 13:34:20Z helmo $
  8. */
  9. if (! defined('PHPMYADMIN')) {
  10. exit;
  11. }
  12. /**
  13. *
  14. */
  15. if (isset($plugin_list)) {
  16. $hide_structure = false;
  17. if ($plugin_param['export_type'] == 'table' && !$plugin_param['single_table']) {
  18. $hide_structure = true;
  19. }
  20. $plugin_list['odt'] = array(
  21. 'text' => 'strOpenDocumentText',
  22. 'extension' => 'odt',
  23. 'mime_type' => 'application/vnd.oasis.opendocument.text',
  24. 'force_file' => true,
  25. 'options' => array(), /* Filled later */
  26. 'options_text' => 'strOptions',
  27. );
  28. /* Structure options */
  29. if (!$hide_structure) {
  30. $plugin_list['odt']['options'][] =
  31. array('type' => 'bgroup', 'name' => 'structure', 'text' => 'strStructure', 'force' => 'data');
  32. if (!empty($GLOBALS['cfgRelation']['relation'])) {
  33. $plugin_list['odt']['options'][] =
  34. array('type' => 'bool', 'name' => 'relation', 'text' => 'strRelations');
  35. }
  36. $plugin_list['odt']['options'][] =
  37. array('type' => 'bool', 'name' => 'comments', 'text' => 'strComments');
  38. if (!empty($GLOBALS['cfgRelation']['mimework'])) {
  39. $plugin_list['odt']['options'][] =
  40. array('type' => 'bool', 'name' => 'mime', 'text' => 'strMIME_MIMEtype');
  41. }
  42. $plugin_list['odt']['options'][] =
  43. array('type' => 'egroup');
  44. }
  45. /* Data */
  46. $plugin_list['odt']['options'][] =
  47. array('type' => 'bgroup', 'name' => 'data', 'text' => 'strData', 'force' => 'structure');
  48. $plugin_list['odt']['options'][] =
  49. array('type' => 'bool', 'name' => 'columns', 'text' => 'strPutColNames');
  50. $plugin_list['odt']['options'][] =
  51. array('type' => 'text', 'name' => 'null', 'text' => 'strReplaceNULLBy');
  52. $plugin_list['odt']['options'][] =
  53. array('type' => 'egroup');
  54. } else {
  55. $GLOBALS['odt_buffer'] = '';
  56. require_once './libraries/opendocument.lib.php';
  57. /**
  58. * Outputs comment
  59. *
  60. * @param string Text of comment
  61. *
  62. * @return bool Whether it suceeded
  63. */
  64. function PMA_exportComment($text) {
  65. return TRUE;
  66. }
  67. /**
  68. * Outputs export footer
  69. *
  70. * @return bool Whether it suceeded
  71. *
  72. * @access public
  73. */
  74. function PMA_exportFooter() {
  75. $GLOBALS['odt_buffer'] .= '</office:text>'
  76. . '</office:body>'
  77. . '</office:document-content>';
  78. if (!PMA_exportOutputHandler(PMA_createOpenDocument('application/vnd.oasis.opendocument.text', $GLOBALS['odt_buffer']))) {
  79. return FALSE;
  80. }
  81. return TRUE;
  82. }
  83. /**
  84. * Outputs export header
  85. *
  86. * @return bool Whether it suceeded
  87. *
  88. * @access public
  89. */
  90. function PMA_exportHeader() {
  91. $GLOBALS['odt_buffer'] .= '<?xml version="1.0" encoding="' . $GLOBALS['charset'] . '"?' . '>'
  92. . '<office:document-content '. $GLOBALS['OpenDocumentNS'] . 'office:version="1.0">'
  93. . '<office:body>'
  94. . '<office:text>';
  95. return TRUE;
  96. }
  97. /**
  98. * Outputs database header
  99. *
  100. * @param string Database name
  101. *
  102. * @return bool Whether it suceeded
  103. *
  104. * @access public
  105. */
  106. function PMA_exportDBHeader($db) {
  107. $GLOBALS['odt_buffer'] .= '<text:h text:outline-level="1" text:style-name="Heading_1" text:is-list-header="true">' . htmlspecialchars($GLOBALS['strDatabase'] . ' ' . $db) . '</text:h>';
  108. return TRUE;
  109. }
  110. /**
  111. * Outputs database footer
  112. *
  113. * @param string Database name
  114. *
  115. * @return bool Whether it suceeded
  116. *
  117. * @access public
  118. */
  119. function PMA_exportDBFooter($db) {
  120. return TRUE;
  121. }
  122. /**
  123. * Outputs create database database
  124. *
  125. * @param string Database name
  126. *
  127. * @return bool Whether it suceeded
  128. *
  129. * @access public
  130. */
  131. function PMA_exportDBCreate($db) {
  132. return TRUE;
  133. }
  134. /**
  135. * Outputs the content of a table in CSV format
  136. *
  137. * @param string the database name
  138. * @param string the table name
  139. * @param string the end of line sequence
  140. * @param string the url to go back in case of error
  141. * @param string SQL query for obtaining data
  142. *
  143. * @return bool Whether it suceeded
  144. *
  145. * @access public
  146. */
  147. function PMA_exportData($db, $table, $crlf, $error_url, $sql_query) {
  148. global $what;
  149. // Gets the data from the database
  150. $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED);
  151. $fields_cnt = PMA_DBI_num_fields($result);
  152. $fields_meta = PMA_DBI_get_fields_meta($result);
  153. $field_flags = array();
  154. for ($j = 0; $j < $fields_cnt; $j++) {
  155. $field_flags[$j] = PMA_DBI_field_flags($result, $j);
  156. }
  157. $GLOBALS['odt_buffer'] .= '<text:h text:outline-level="2" text:style-name="Heading_2" text:is-list-header="true">' . htmlspecialchars($GLOBALS['strDumpingData'] . ' ' . $table) . '</text:h>';
  158. $GLOBALS['odt_buffer'] .= '<table:table table:name="' . htmlspecialchars($table) . '_structure">';
  159. $GLOBALS['odt_buffer'] .= '<table:table-column table:number-columns-repeated="' . $fields_cnt . '"/>';
  160. // If required, get fields name at the first line
  161. if (isset($GLOBALS[$what . '_columns'])) {
  162. $GLOBALS['odt_buffer'] .= '<table:table-row>';
  163. for ($i = 0; $i < $fields_cnt; $i++) {
  164. $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
  165. . '<text:p>' . htmlspecialchars(stripslashes(PMA_DBI_field_name($result, $i))) . '</text:p>'
  166. . '</table:table-cell>';
  167. } // end for
  168. $GLOBALS['odt_buffer'] .= '</table:table-row>';
  169. } // end if
  170. // Format the data
  171. while ($row = PMA_DBI_fetch_row($result)) {
  172. $GLOBALS['odt_buffer'] .= '<table:table-row>';
  173. for ($j = 0; $j < $fields_cnt; $j++) {
  174. if (!isset($row[$j]) || is_null($row[$j])) {
  175. $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
  176. . '<text:p>' . htmlspecialchars($GLOBALS[$what . '_null']) . '</text:p>'
  177. . '</table:table-cell>';
  178. // ignore BLOB
  179. } elseif (stristr($field_flags[$j], 'BINARY')
  180. && $fields_meta[$j]->blob) {
  181. $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
  182. . '<text:p></text:p>'
  183. . '</table:table-cell>';
  184. } elseif ($fields_meta[$j]->numeric && $fields_meta[$j]->type != 'timestamp' && ! $fields_meta[$j]->blob) {
  185. $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="float" office:value="' . $row[$j] . '" >'
  186. . '<text:p>' . htmlspecialchars($row[$j]) . '</text:p>'
  187. . '</table:table-cell>';
  188. } else {
  189. $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
  190. . '<text:p>' . htmlspecialchars($row[$j]) . '</text:p>'
  191. . '</table:table-cell>';
  192. }
  193. } // end for
  194. $GLOBALS['odt_buffer'] .= '</table:table-row>';
  195. } // end while
  196. PMA_DBI_free_result($result);
  197. $GLOBALS['odt_buffer'] .= '</table:table>';
  198. return TRUE;
  199. }
  200. /**
  201. * Returns $table's structure as Open Document Text
  202. *
  203. * @param string the database name
  204. * @param string the table name
  205. * @param string the end of line sequence
  206. * @param string the url to go back in case of error
  207. * @param boolean whether to include relation comments
  208. * @param boolean whether to include column comments
  209. * @param boolean whether to include mime comments
  210. * @param string future feature: support view dependencies
  211. *
  212. * @return bool Whether it suceeded
  213. *
  214. * @access public
  215. */
  216. // @@@ $strTableStructure
  217. function PMA_exportStructure($db, $table, $crlf, $error_url, $do_relation = false, $do_comments = false, $do_mime = false, $dates = false, $dummy)
  218. {
  219. global $cfgRelation;
  220. /* Heading */
  221. $GLOBALS['odt_buffer'] .= '<text:h text:outline-level="2" text:style-name="Heading_2" text:is-list-header="true">' . htmlspecialchars($GLOBALS['strTableStructure'] . ' ' . $table) . '</text:h>';
  222. /**
  223. * Get the unique keys in the table
  224. */
  225. $keys_query = 'SHOW KEYS FROM ' . PMA_backquote($table) . ' FROM '. PMA_backquote($db);
  226. $keys_result = PMA_DBI_query($keys_query);
  227. $unique_keys = array();
  228. while ($key = PMA_DBI_fetch_assoc($keys_result)) {
  229. if ($key['Non_unique'] == 0) {
  230. $unique_keys[] = $key['Column_name'];
  231. }
  232. }
  233. PMA_DBI_free_result($keys_result);
  234. /**
  235. * Gets fields properties
  236. */
  237. PMA_DBI_select_db($db);
  238. $local_query = 'SHOW FIELDS FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table);
  239. $result = PMA_DBI_query($local_query);
  240. $fields_cnt = PMA_DBI_num_rows($result);
  241. // Check if we can use Relations (Mike Beck)
  242. if ($do_relation && !empty($cfgRelation['relation'])) {
  243. // Find which tables are related with the current one and write it in
  244. // an array
  245. $res_rel = PMA_getForeigners($db, $table);
  246. if ($res_rel && count($res_rel) > 0) {
  247. $have_rel = TRUE;
  248. } else {
  249. $have_rel = FALSE;
  250. }
  251. } else {
  252. $have_rel = FALSE;
  253. } // end if
  254. /**
  255. * Displays the table structure
  256. */
  257. $GLOBALS['odt_buffer'] .= '<table:table table:name="' . htmlspecialchars($table) . '_data">';
  258. $columns_cnt = 4;
  259. if ($do_relation && $have_rel) {
  260. $columns_cnt++;
  261. }
  262. if ($do_comments) {
  263. $columns_cnt++;
  264. }
  265. if ($do_mime && $cfgRelation['mimework']) {
  266. $columns_cnt++;
  267. }
  268. $GLOBALS['odt_buffer'] .= '<table:table-column table:number-columns-repeated="' . $columns_cnt . '"/>';
  269. /* Header */
  270. $GLOBALS['odt_buffer'] .= '<table:table-row>';
  271. $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
  272. . '<text:p>' . htmlspecialchars($GLOBALS['strField']) . '</text:p>'
  273. . '</table:table-cell>';
  274. $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
  275. . '<text:p>' . htmlspecialchars($GLOBALS['strType']) . '</text:p>'
  276. . '</table:table-cell>';
  277. $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
  278. . '<text:p>' . htmlspecialchars($GLOBALS['strNull']) . '</text:p>'
  279. . '</table:table-cell>';
  280. $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
  281. . '<text:p>' . htmlspecialchars($GLOBALS['strDefault']) . '</text:p>'
  282. . '</table:table-cell>';
  283. if ($do_relation && $have_rel) {
  284. $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
  285. . '<text:p>' . htmlspecialchars($GLOBALS['strLinksTo']) . '</text:p>'
  286. . '</table:table-cell>';
  287. }
  288. if ($do_comments) {
  289. $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
  290. . '<text:p>' . htmlspecialchars($GLOBALS['strComments']) . '</text:p>'
  291. . '</table:table-cell>';
  292. $comments = PMA_getComments($db, $table);
  293. }
  294. if ($do_mime && $cfgRelation['mimework']) {
  295. $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
  296. . '<text:p>' . htmlspecialchars($GLOBALS['strMIME_MIMEtype']) . '</text:p>'
  297. . '</table:table-cell>';
  298. $mime_map = PMA_getMIME($db, $table, true);
  299. }
  300. $GLOBALS['odt_buffer'] .= '</table:table-row>';
  301. while ($row = PMA_DBI_fetch_assoc($result)) {
  302. $GLOBALS['odt_buffer'] .= '<table:table-row>';
  303. $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
  304. . '<text:p>' . htmlspecialchars($row['Field']) . '</text:p>'
  305. . '</table:table-cell>';
  306. // reformat mysql query output - staybyte - 9. June 2001
  307. // loic1: set or enum types: slashes single quotes inside options
  308. $field_name = $row['Field'];
  309. $type = $row['Type'];
  310. if (preg_match('/^(set|enum)\((.+)\)$/i', $type, $tmp)) {
  311. $tmp[2] = substr(preg_replace('/([^,])\'\'/', '\\1\\\'', ',' . $tmp[2]), 1);
  312. $type = $tmp[1] . '(' . str_replace(',', ', ', $tmp[2]) . ')';
  313. $type_nowrap = '';
  314. $binary = 0;
  315. $unsigned = 0;
  316. $zerofill = 0;
  317. } else {
  318. $type_nowrap = ' nowrap="nowrap"';
  319. $type = preg_replace('/BINARY/i', '', $type);
  320. $type = preg_replace('/ZEROFILL/i', '', $type);
  321. $type = preg_replace('/UNSIGNED/i', '', $type);
  322. if (empty($type)) {
  323. $type = '&nbsp;';
  324. }
  325. $binary = preg_match('/BINARY/i', $row['Type']);
  326. $unsigned = preg_match('/UNSIGNED/i', $row['Type']);
  327. $zerofill = preg_match('/ZEROFILL/i', $row['Type']);
  328. }
  329. $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
  330. . '<text:p>' . htmlspecialchars($type) . '</text:p>'
  331. . '</table:table-cell>';
  332. if (!isset($row['Default'])) {
  333. if ($row['Null'] != 'NO') {
  334. $row['Default'] = 'NULL';
  335. } else {
  336. $row['Default'] = '';
  337. }
  338. } else {
  339. $row['Default'] = $row['Default'];
  340. }
  341. $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
  342. . '<text:p>' . htmlspecialchars(($row['Null'] == '' || $row['Null'] == 'NO') ? $GLOBALS['strNo'] : $GLOBALS['strYes']) . '</text:p>'
  343. . '</table:table-cell>';
  344. $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
  345. . '<text:p>' . htmlspecialchars($row['Default']) . '</text:p>'
  346. . '</table:table-cell>';
  347. if ($do_relation && $have_rel) {
  348. if (isset($res_rel[$field_name])) {
  349. $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
  350. . '<text:p>' . htmlspecialchars($res_rel[$field_name]['foreign_table'] . ' (' . $res_rel[$field_name]['foreign_field'] . ')') . '</text:p>'
  351. . '</table:table-cell>';
  352. }
  353. }
  354. if ($do_comments) {
  355. if (isset($comments[$field_name])) {
  356. $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
  357. . '<text:p>' . htmlspecialchars($comments[$field_name]) . '</text:p>'
  358. . '</table:table-cell>';
  359. } else {
  360. $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
  361. . '<text:p></text:p>'
  362. . '</table:table-cell>';
  363. }
  364. }
  365. if ($do_mime && $cfgRelation['mimework']) {
  366. if (isset($mime_map[$field_name])) {
  367. $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
  368. . '<text:p>' . htmlspecialchars(str_replace('_', '/', $mime_map[$field_name]['mimetype'])) . '</text:p>'
  369. . '</table:table-cell>';
  370. } else {
  371. $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
  372. . '<text:p></text:p>'
  373. . '</table:table-cell>';
  374. }
  375. }
  376. $GLOBALS['odt_buffer'] .= '</table:table-row>';
  377. } // end while
  378. PMA_DBI_free_result($result);
  379. $GLOBALS['odt_buffer'] .= '</table:table>';
  380. return TRUE;
  381. } // end of the 'PMA_exportStructure' function
  382. } // end else
  383. ?>