PageRenderTime 58ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/manage/phpmyadminlite/libraries/export/texytext.php

https://gitlab.com/albert925/lading-ach
PHP | 338 lines | 217 code | 34 blank | 87 comment | 61 complexity | f3b07913d5d313fe6562caf5b5a228ca MD5 | raw file
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Export to Texy! text.
  5. *
  6. * @package phpMyAdmin-Export-Texy
  7. */
  8. if (! defined('PHPMYADMIN')) {
  9. exit;
  10. }
  11. /**
  12. *
  13. */
  14. if (isset($plugin_list)) {
  15. $plugin_list['texytext'] = array(
  16. 'text' => 'strTexyText',
  17. 'extension' => 'txt',
  18. 'mime_type' => 'text/plain',
  19. 'options' => array(
  20. array('type' => 'bool',
  21. 'name' => 'structure',
  22. 'text' => 'strStructure',
  23. 'force' => 'data'),
  24. array('type' => 'bgroup',
  25. 'name' => 'data',
  26. 'text' => 'strData',
  27. 'force' => 'structure'),
  28. array('type' => 'text',
  29. 'name' => 'null',
  30. 'text' => 'strReplaceNULLBy'),
  31. array('type' => 'bool',
  32. 'name' => 'columns',
  33. 'text' => 'strPutColNames'),
  34. array('type' => 'egroup'),
  35. ),
  36. 'options_text' => 'strOptions',
  37. );
  38. } else {
  39. /**
  40. * Outputs comment
  41. *
  42. * @param string Text of comment
  43. *
  44. * @return bool Whether it suceeded
  45. */
  46. function PMA_exportComment($text) {
  47. return TRUE;
  48. }
  49. /**
  50. * Outputs export footer
  51. *
  52. * @return bool Whether it suceeded
  53. *
  54. * @access public
  55. */
  56. function PMA_exportFooter() {
  57. return true;
  58. }
  59. /**
  60. * Outputs export header
  61. *
  62. * @return bool Whether it suceeded
  63. *
  64. * @access public
  65. */
  66. function PMA_exportHeader() {
  67. return true;
  68. }
  69. /**
  70. * Outputs database header
  71. *
  72. * @param string Database name
  73. *
  74. * @return bool Whether it suceeded
  75. *
  76. * @access public
  77. */
  78. function PMA_exportDBHeader($db) {
  79. return PMA_exportOutputHandler('===' . $GLOBALS['strDatabase'] . ' ' . $db . "\n\n");
  80. }
  81. /**
  82. * Outputs database footer
  83. *
  84. * @param string Database name
  85. *
  86. * @return bool Whether it suceeded
  87. *
  88. * @access public
  89. */
  90. function PMA_exportDBFooter($db) {
  91. return TRUE;
  92. }
  93. /**
  94. * Outputs create database database
  95. *
  96. * @param string Database name
  97. *
  98. * @return bool Whether it suceeded
  99. *
  100. * @access public
  101. */
  102. function PMA_exportDBCreate($db) {
  103. return TRUE;
  104. }
  105. /**
  106. * Outputs the content of a table in CSV format
  107. *
  108. * @param string the database name
  109. * @param string the table name
  110. * @param string the end of line sequence
  111. * @param string the url to go back in case of error
  112. * @param string SQL query for obtaining data
  113. *
  114. * @return bool Whether it suceeded
  115. *
  116. * @access public
  117. */
  118. function PMA_exportData($db, $table, $crlf, $error_url, $sql_query)
  119. {
  120. global $what;
  121. if (! PMA_exportOutputHandler('== ' . $GLOBALS['strDumpingData'] . ' ' . $table . "\n\n")) {
  122. return FALSE;
  123. }
  124. // Gets the data from the database
  125. $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED);
  126. $fields_cnt = PMA_DBI_num_fields($result);
  127. // If required, get fields name at the first line
  128. if (isset($GLOBALS[$what . '_columns'])) {
  129. $text_output = "|------\n";
  130. for ($i = 0; $i < $fields_cnt; $i++) {
  131. $text_output .= '|' . htmlspecialchars(stripslashes(PMA_DBI_field_name($result, $i)));
  132. } // end for
  133. $text_output .= "\n|------\n";
  134. if (! PMA_exportOutputHandler($text_output)) {
  135. return FALSE;
  136. }
  137. } // end if
  138. // Format the data
  139. while ($row = PMA_DBI_fetch_row($result)) {
  140. $text_output = '';
  141. for ($j = 0; $j < $fields_cnt; $j++) {
  142. if (! isset($row[$j]) || is_null($row[$j])) {
  143. $value = $GLOBALS[$what . '_null'];
  144. } elseif ($row[$j] == '0' || $row[$j] != '') {
  145. $value = $row[$j];
  146. } else {
  147. $value = ' ';
  148. }
  149. $text_output .= '|' . htmlspecialchars($value);
  150. } // end for
  151. $text_output .= "\n";
  152. if (! PMA_exportOutputHandler($text_output)) {
  153. return FALSE;
  154. }
  155. } // end while
  156. PMA_DBI_free_result($result);
  157. return TRUE;
  158. }
  159. function PMA_exportStructure($db, $table, $crlf, $error_url, $do_relation = false, $do_comments = false, $do_mime = false, $dates = false, $dummy)
  160. {
  161. global $cfgRelation;
  162. if (! PMA_exportOutputHandler('== ' . $GLOBALS['strTableStructure'] . ' ' .$table . "\n\n")) {
  163. return FALSE;
  164. }
  165. /**
  166. * Get the unique keys in the table
  167. */
  168. $keys_query = 'SHOW KEYS FROM ' . PMA_backquote($table) . ' FROM '. PMA_backquote($db);
  169. $keys_result = PMA_DBI_query($keys_query);
  170. $unique_keys = array();
  171. while ($key = PMA_DBI_fetch_assoc($keys_result)) {
  172. if ($key['Non_unique'] == 0) {
  173. $unique_keys[] = $key['Column_name'];
  174. }
  175. }
  176. PMA_DBI_free_result($keys_result);
  177. /**
  178. * Gets fields properties
  179. */
  180. PMA_DBI_select_db($db);
  181. $local_query = 'SHOW FIELDS FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table);
  182. $result = PMA_DBI_query($local_query);
  183. $fields_cnt = PMA_DBI_num_rows($result);
  184. // Check if we can use Relations (Mike Beck)
  185. if ($do_relation && ! empty($cfgRelation['relation'])) {
  186. // Find which tables are related with the current one and write it in
  187. // an array
  188. $res_rel = PMA_getForeigners($db, $table);
  189. if ($res_rel && count($res_rel) > 0) {
  190. $have_rel = TRUE;
  191. } else {
  192. $have_rel = FALSE;
  193. }
  194. } else {
  195. $have_rel = FALSE;
  196. } // end if
  197. /**
  198. * Displays the table structure
  199. */
  200. $columns_cnt = 4;
  201. if ($do_relation && $have_rel) {
  202. $columns_cnt++;
  203. }
  204. if ($do_comments && $cfgRelation['commwork']) {
  205. $columns_cnt++;
  206. }
  207. if ($do_mime && $cfgRelation['mimework']) {
  208. $columns_cnt++;
  209. }
  210. $text_output = "|------\n";
  211. $text_output .= '|' . htmlspecialchars($GLOBALS['strField']);
  212. $text_output .= '|' . htmlspecialchars($GLOBALS['strType']);
  213. $text_output .= '|' . htmlspecialchars($GLOBALS['strNull']);
  214. $text_output .= '|' . htmlspecialchars($GLOBALS['strDefault']);
  215. if ($do_relation && $have_rel) {
  216. $text_output .= '|' . htmlspecialchars($GLOBALS['strLinksTo']);
  217. }
  218. if ($do_comments) {
  219. $text_output .= '|' . htmlspecialchars($GLOBALS['strComments']);
  220. $comments = PMA_getComments($db, $table);
  221. }
  222. if ($do_mime && $cfgRelation['mimework']) {
  223. $text_output .= '|' . htmlspecialchars('MIME');
  224. $mime_map = PMA_getMIME($db, $table, true);
  225. }
  226. $text_output .= "\n|------\n";
  227. if (! PMA_exportOutputHandler($text_output)) {
  228. return FALSE;
  229. }
  230. while ($row = PMA_DBI_fetch_assoc($result)) {
  231. $text_output = '';
  232. $type = $row['Type'];
  233. // reformat mysql query output - staybyte - 9. June 2001
  234. // loic1: set or enum types: slashes single quotes inside options
  235. if (preg_match('/^(set|enum)\((.+)\)$/i', $type, $tmp)) {
  236. $tmp[2] = substr(preg_replace('/([^,])\'\'/', '\\1\\\'', ',' . $tmp[2]), 1);
  237. $type = $tmp[1] . '(' . str_replace(',', ', ', $tmp[2]) . ')';
  238. $type_nowrap = '';
  239. $binary = 0;
  240. $unsigned = 0;
  241. $zerofill = 0;
  242. } else {
  243. $type_nowrap = ' nowrap="nowrap"';
  244. $type = preg_replace('/BINARY/i', '', $type);
  245. $type = preg_replace('/ZEROFILL/i', '', $type);
  246. $type = preg_replace('/UNSIGNED/i', '', $type);
  247. if (empty($type)) {
  248. $type = '&nbsp;';
  249. }
  250. $binary = preg_match('/BINARY/i', $row['Type']);
  251. $unsigned = preg_match('/UNSIGNED/i', $row['Type']);
  252. $zerofill = preg_match('/ZEROFILL/i', $row['Type']);
  253. }
  254. $strAttribute = '&nbsp;';
  255. if ($binary) {
  256. $strAttribute = 'BINARY';
  257. }
  258. if ($unsigned) {
  259. $strAttribute = 'UNSIGNED';
  260. }
  261. if ($zerofill) {
  262. $strAttribute = 'UNSIGNED ZEROFILL';
  263. }
  264. if (! isset($row['Default'])) {
  265. if ($row['Null'] != 'NO') {
  266. $row['Default'] = 'NULL';
  267. }
  268. } else {
  269. $row['Default'] = $row['Default'];
  270. }
  271. $fmt_pre = '';
  272. $fmt_post = '';
  273. if (in_array($row['Field'], $unique_keys)) {
  274. $fmt_pre = '**' . $fmt_pre;
  275. $fmt_post = $fmt_post . '**';
  276. }
  277. if ($row['Key']=='PRI') {
  278. $fmt_pre = '//' . $fmt_pre;
  279. $fmt_post = $fmt_post . '//';
  280. }
  281. $text_output .= '|' . $fmt_pre . htmlspecialchars($row['Field']) . $fmt_post;
  282. $text_output .= '|' . htmlspecialchars($type);
  283. $text_output .= '|' . htmlspecialchars(($row['Null'] == '' || $row['Null'] == 'NO') ? $GLOBALS['strNo'] : $GLOBALS['strYes']);
  284. $text_output .= '|' . htmlspecialchars(isset($row['Default']) ? $row['Default'] : '');
  285. $field_name = $row['Field'];
  286. if ($do_relation && $have_rel) {
  287. $text_output .= '|' . (isset($res_rel[$field_name]) ? htmlspecialchars($res_rel[$field_name]['foreign_table'] . ' (' . $res_rel[$field_name]['foreign_field'] . ')') : '');
  288. }
  289. if ($do_comments && $cfgRelation['commwork']) {
  290. $text_output .= '|' . (isset($comments[$field_name]) ? htmlspecialchars($comments[$field_name]) : '');
  291. }
  292. if ($do_mime && $cfgRelation['mimework']) {
  293. $text_output .= '|' . (isset($mime_map[$field_name]) ? htmlspecialchars(str_replace('_', '/', $mime_map[$field_name]['mimetype'])) : '');
  294. }
  295. $text_output .= "\n";
  296. if (! PMA_exportOutputHandler($text_output)) {
  297. return FALSE;
  298. }
  299. } // end while
  300. PMA_DBI_free_result($result);
  301. return true;
  302. }
  303. }
  304. ?>