PageRenderTime 57ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/sitemanager/phpmyadmin/libraries/export/texytext.php

https://bitbucket.org/itoxable/chiron-gaming
PHP | 304 lines | 174 code | 33 blank | 97 comment | 53 complexity | e704f7bb5db8e5e0db534d06b074390a MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Export to Texy! text.
  5. *
  6. * @package PhpMyAdmin-Export
  7. * @subpackage Texy
  8. */
  9. if (! defined('PHPMYADMIN')) {
  10. exit;
  11. }
  12. /**
  13. *
  14. */
  15. if (isset($plugin_list)) {
  16. $plugin_list['texytext'] = array(
  17. 'text' => __('Texy! text'),
  18. 'extension' => 'txt',
  19. 'mime_type' => 'text/plain',
  20. 'options' => array(
  21. /* what to dump (structure/data/both) */
  22. array('type' => 'begin_group', 'text' => __('Dump table'), 'name' => 'general_opts'),
  23. array('type' => 'radio', 'name' => 'structure_or_data', 'values' => array('structure' => __('structure'), 'data' => __('data'), 'structure_and_data' => __('structure and data'))),
  24. array('type' => 'end_group'),
  25. array('type' => 'begin_group', 'name' => 'data', 'text' => __('Data dump options'), 'force' => 'structure'),
  26. array('type' => 'text', 'name' => 'null', 'text' => __('Replace NULL by')),
  27. array('type' => 'bool', 'name' => 'columns', 'text' => __('Put columns names in the first row')),
  28. array('type' => 'end_group'),
  29. ),
  30. 'options_text' => __('Options'),
  31. );
  32. } else {
  33. /**
  34. * Outputs export footer
  35. *
  36. * @return bool Whether it succeeded
  37. *
  38. * @access public
  39. */
  40. function PMA_exportFooter() {
  41. return true;
  42. }
  43. /**
  44. * Outputs export header
  45. *
  46. * @return bool Whether it succeeded
  47. *
  48. * @access public
  49. */
  50. function PMA_exportHeader() {
  51. return true;
  52. }
  53. /**
  54. * Outputs database header
  55. *
  56. * @param string $db Database name
  57. * @return bool Whether it succeeded
  58. *
  59. * @access public
  60. */
  61. function PMA_exportDBHeader($db) {
  62. return PMA_exportOutputHandler('===' . __('Database') . ' ' . $db . "\n\n");
  63. }
  64. /**
  65. * Outputs database footer
  66. *
  67. * @param string $db Database name
  68. * @return bool Whether it succeeded
  69. *
  70. * @access public
  71. */
  72. function PMA_exportDBFooter($db) {
  73. return true;
  74. }
  75. /**
  76. * Outputs CREATE DATABASE statement
  77. *
  78. * @param string $db Database name
  79. * @return bool Whether it succeeded
  80. *
  81. * @access public
  82. */
  83. function PMA_exportDBCreate($db) {
  84. return true;
  85. }
  86. /**
  87. * Outputs the content of a table in Texy format
  88. *
  89. * @param string $db database name
  90. * @param string $table table name
  91. * @param string $crlf the end of line sequence
  92. * @param string $error_url the url to go back in case of error
  93. * @param string $sql_query SQL query for obtaining data
  94. * @return bool Whether it succeeded
  95. *
  96. * @access public
  97. */
  98. function PMA_exportData($db, $table, $crlf, $error_url, $sql_query)
  99. {
  100. global $what;
  101. if (! PMA_exportOutputHandler('== ' . __('Dumping data for table') . ' ' . $table . "\n\n")) {
  102. return false;
  103. }
  104. // Gets the data from the database
  105. $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED);
  106. $fields_cnt = PMA_DBI_num_fields($result);
  107. // If required, get fields name at the first line
  108. if (isset($GLOBALS[$what . '_columns'])) {
  109. $text_output = "|------\n";
  110. for ($i = 0; $i < $fields_cnt; $i++) {
  111. $text_output .= '|' . htmlspecialchars(stripslashes(PMA_DBI_field_name($result, $i)));
  112. } // end for
  113. $text_output .= "\n|------\n";
  114. if (! PMA_exportOutputHandler($text_output)) {
  115. return false;
  116. }
  117. } // end if
  118. // Format the data
  119. while ($row = PMA_DBI_fetch_row($result)) {
  120. $text_output = '';
  121. for ($j = 0; $j < $fields_cnt; $j++) {
  122. if (! isset($row[$j]) || is_null($row[$j])) {
  123. $value = $GLOBALS[$what . '_null'];
  124. } elseif ($row[$j] == '0' || $row[$j] != '') {
  125. $value = $row[$j];
  126. } else {
  127. $value = ' ';
  128. }
  129. $text_output .= '|' . htmlspecialchars($value);
  130. } // end for
  131. $text_output .= "\n";
  132. if (! PMA_exportOutputHandler($text_output)) {
  133. return false;
  134. }
  135. } // end while
  136. PMA_DBI_free_result($result);
  137. return true;
  138. }
  139. /**
  140. * Outputs table's structure
  141. *
  142. * @param string $db database name
  143. * @param string $table table name
  144. * @param string $crlf the end of line sequence
  145. * @param string $error_url the url to go back in case of error
  146. * @param bool $do_relation whether to include relation comments
  147. * @param bool $do_comments whether to include the pmadb-style column comments
  148. * as comments in the structure; this is deprecated
  149. * but the parameter is left here because export.php
  150. * calls PMA_exportStructure() also for other export
  151. * types which use this parameter
  152. * @param bool $do_mime whether to include mime comments
  153. * @param bool $dates whether to include creation/update/check dates
  154. * @param string $export_mode 'create_table', 'triggers', 'create_view', 'stand_in'
  155. * @param string $export_type 'server', 'database', 'table'
  156. * @return bool Whether it succeeded
  157. *
  158. * @access public
  159. */
  160. function PMA_exportStructure($db, $table, $crlf, $error_url, $do_relation = false, $do_comments = false, $do_mime = false, $dates = false, $export_mode, $export_type)
  161. {
  162. global $cfgRelation;
  163. if (! PMA_exportOutputHandler('== ' . __('Table structure for table') . ' ' .$table . "\n\n")) {
  164. return false;
  165. }
  166. /**
  167. * Get the unique keys in the table
  168. */
  169. $unique_keys = array();
  170. $keys = PMA_DBI_get_table_indexes($db, $table);
  171. foreach ($keys as $key) {
  172. if ($key['Non_unique'] == 0) {
  173. $unique_keys[] = $key['Column_name'];
  174. }
  175. }
  176. /**
  177. * Gets fields properties
  178. */
  179. PMA_DBI_select_db($db);
  180. // Check if we can use Relations
  181. if ($do_relation && ! empty($cfgRelation['relation'])) {
  182. // Find which tables are related with the current one and write it in
  183. // an array
  184. $res_rel = PMA_getForeigners($db, $table);
  185. if ($res_rel && count($res_rel) > 0) {
  186. $have_rel = true;
  187. } else {
  188. $have_rel = false;
  189. }
  190. } else {
  191. $have_rel = false;
  192. } // end if
  193. /**
  194. * Displays the table structure
  195. */
  196. $columns_cnt = 4;
  197. if ($do_relation && $have_rel) {
  198. $columns_cnt++;
  199. }
  200. if ($do_comments && $cfgRelation['commwork']) {
  201. $columns_cnt++;
  202. }
  203. if ($do_mime && $cfgRelation['mimework']) {
  204. $columns_cnt++;
  205. }
  206. $text_output = "|------\n";
  207. $text_output .= '|' . __('Column');
  208. $text_output .= '|' . __('Type');
  209. $text_output .= '|' . __('Null');
  210. $text_output .= '|' . __('Default');
  211. if ($do_relation && $have_rel) {
  212. $text_output .= '|' . __('Links to');
  213. }
  214. if ($do_comments) {
  215. $text_output .= '|' . __('Comments');
  216. $comments = PMA_getComments($db, $table);
  217. }
  218. if ($do_mime && $cfgRelation['mimework']) {
  219. $text_output .= '|' . htmlspecialchars('MIME');
  220. $mime_map = PMA_getMIME($db, $table, true);
  221. }
  222. $text_output .= "\n|------\n";
  223. if (! PMA_exportOutputHandler($text_output)) {
  224. return false;
  225. }
  226. $columns = PMA_DBI_get_columns($db, $table);
  227. foreach ($columns as $column) {
  228. $text_output = '';
  229. $extracted_fieldspec = PMA_extractFieldSpec($column['Type']);
  230. $type = $extracted_fieldspec['print_type'];
  231. if (empty($type)) {
  232. $type = '&nbsp;';
  233. }
  234. if (! isset($column['Default'])) {
  235. if ($column['Null'] != 'NO') {
  236. $column['Default'] = 'NULL';
  237. }
  238. }
  239. $fmt_pre = '';
  240. $fmt_post = '';
  241. if (in_array($column['Field'], $unique_keys)) {
  242. $fmt_pre = '**' . $fmt_pre;
  243. $fmt_post = $fmt_post . '**';
  244. }
  245. if ($column['Key']=='PRI') {
  246. $fmt_pre = '//' . $fmt_pre;
  247. $fmt_post = $fmt_post . '//';
  248. }
  249. $text_output .= '|' . $fmt_pre . htmlspecialchars($column['Field']) . $fmt_post;
  250. $text_output .= '|' . htmlspecialchars($type);
  251. $text_output .= '|' . (($column['Null'] == '' || $column['Null'] == 'NO') ? __('No') : __('Yes'));
  252. $text_output .= '|' . htmlspecialchars(isset($column['Default']) ? $column['Default'] : '');
  253. $field_name = $column['Field'];
  254. if ($do_relation && $have_rel) {
  255. $text_output .= '|' . (isset($res_rel[$field_name]) ? htmlspecialchars($res_rel[$field_name]['foreign_table'] . ' (' . $res_rel[$field_name]['foreign_field'] . ')') : '');
  256. }
  257. if ($do_comments && $cfgRelation['commwork']) {
  258. $text_output .= '|' . (isset($comments[$field_name]) ? htmlspecialchars($comments[$field_name]) : '');
  259. }
  260. if ($do_mime && $cfgRelation['mimework']) {
  261. $text_output .= '|' . (isset($mime_map[$field_name]) ? htmlspecialchars(str_replace('_', '/', $mime_map[$field_name]['mimetype'])) : '');
  262. }
  263. $text_output .= "\n";
  264. if (! PMA_exportOutputHandler($text_output)) {
  265. return false;
  266. }
  267. } // end while
  268. return true;
  269. }
  270. }
  271. ?>