Use PMA_DBI_get_table_indexes_sql() only when really needed, in all other cases use...
[phpmyadmin.git] / libraries / export / texytext.php
blob351ce718e56fd114ae3de076a544207fb2e7541d
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Export to Texy! text.
6 * @package phpMyAdmin-Export
7 * @subpackage Texy
8 */
9 if (! defined('PHPMYADMIN')) {
10 exit;
13 /**
16 if (isset($plugin_list)) {
17 $plugin_list['texytext'] = array(
18 'text' => __('Texy! text'),
19 'extension' => 'txt',
20 'mime_type' => 'text/plain',
21 'options' => array(
22 /* what to dump (structure/data/both) */
23 array('type' => 'begin_group', 'text' => __('Dump table'), 'name' => 'general_opts'),
24 array('type' => 'radio', 'name' => 'structure_or_data', 'values' => array('structure' => __('structure'), 'data' => __('data'), 'structure_and_data' => __('structure and data'))),
25 array('type' => 'end_group'),
26 array('type' => 'begin_group', 'name' => 'data', 'text' => __('Data dump options'), 'force' => 'structure'),
27 array('type' => 'text', 'name' => 'null', 'text' => __('Replace NULL by')),
28 array('type' => 'bool', 'name' => 'columns', 'text' => __('Put columns names in the first row')),
29 array('type' => 'end_group'),
31 'options_text' => __('Options'),
33 } else {
35 /**
36 * Outputs export footer
38 * @return bool Whether it suceeded
40 * @access public
42 function PMA_exportFooter() {
43 return true;
46 /**
47 * Outputs export header
49 * @return bool Whether it suceeded
51 * @access public
53 function PMA_exportHeader() {
54 return true;
57 /**
58 * Outputs database header
60 * @param string $db Database name
61 * @return bool Whether it suceeded
63 * @access public
65 function PMA_exportDBHeader($db) {
66 return PMA_exportOutputHandler('===' . __('Database') . ' ' . $db . "\n\n");
69 /**
70 * Outputs database footer
72 * @param string $db Database name
73 * @return bool Whether it suceeded
75 * @access public
77 function PMA_exportDBFooter($db) {
78 return true;
81 /**
82 * Outputs CREATE DATABASE statement
84 * @param string $db Database name
85 * @return bool Whether it suceeded
87 * @access public
89 function PMA_exportDBCreate($db) {
90 return true;
93 /**
94 * Outputs the content of a table in Texy format
96 * @param string $db database name
97 * @param string $table table name
98 * @param string $crlf the end of line sequence
99 * @param string $error_url the url to go back in case of error
100 * @param string $sql_query SQL query for obtaining data
101 * @return bool Whether it suceeded
103 * @access public
105 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query)
107 global $what;
109 if (! PMA_exportOutputHandler('== ' . __('Dumping data for table') . ' ' . $table . "\n\n")) {
110 return false;
113 // Gets the data from the database
114 $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED);
115 $fields_cnt = PMA_DBI_num_fields($result);
117 // If required, get fields name at the first line
118 if (isset($GLOBALS[$what . '_columns'])) {
119 $text_output = "|------\n";
120 for ($i = 0; $i < $fields_cnt; $i++) {
121 $text_output .= '|' . htmlspecialchars(stripslashes(PMA_DBI_field_name($result, $i)));
122 } // end for
123 $text_output .= "\n|------\n";
124 if (! PMA_exportOutputHandler($text_output)) {
125 return false;
127 } // end if
129 // Format the data
130 while ($row = PMA_DBI_fetch_row($result)) {
131 $text_output = '';
132 for ($j = 0; $j < $fields_cnt; $j++) {
133 if (! isset($row[$j]) || is_null($row[$j])) {
134 $value = $GLOBALS[$what . '_null'];
135 } elseif ($row[$j] == '0' || $row[$j] != '') {
136 $value = $row[$j];
137 } else {
138 $value = ' ';
140 $text_output .= '|' . htmlspecialchars($value);
141 } // end for
142 $text_output .= "\n";
143 if (! PMA_exportOutputHandler($text_output)) {
144 return false;
146 } // end while
147 PMA_DBI_free_result($result);
149 return true;
153 * Outputs table's structure
155 * @param string $db database name
156 * @param string $table table name
157 * @param string $crlf the end of line sequence
158 * @param string $error_url the url to go back in case of error
159 * @param bool $do_relation whether to include relation comments
160 * @param bool $do_comments whether to include the pmadb-style column comments
161 * as comments in the structure; this is deprecated
162 * but the parameter is left here because export.php
163 * calls PMA_exportStructure() also for other export
164 * types which use this parameter
165 * @param bool $do_mime whether to include mime comments
166 * @param bool $dates whether to include creation/update/check dates
167 * @param string $export_mode 'create_table', 'triggers', 'create_view', 'stand_in'
168 * @param string $export_type 'server', 'database', 'table'
169 * @return bool Whether it suceeded
171 * @access public
173 function PMA_exportStructure($db, $table, $crlf, $error_url, $do_relation = false, $do_comments = false, $do_mime = false, $dates = false, $export_mode, $export_type)
175 global $cfgRelation;
177 if (! PMA_exportOutputHandler('== ' . __('Table structure for table') . ' ' .$table . "\n\n")) {
178 return false;
182 * Get the unique keys in the table
184 $unique_keys = array();
185 $keys = PMA_DBI_get_table_indexes($db, $table);
186 foreach ($keys as $key) {
187 if ($key['Non_unique'] == 0) {
188 $unique_keys[] = $key['Column_name'];
193 * Gets fields properties
195 PMA_DBI_select_db($db);
197 // Check if we can use Relations
198 if ($do_relation && ! empty($cfgRelation['relation'])) {
199 // Find which tables are related with the current one and write it in
200 // an array
201 $res_rel = PMA_getForeigners($db, $table);
203 if ($res_rel && count($res_rel) > 0) {
204 $have_rel = true;
205 } else {
206 $have_rel = false;
208 } else {
209 $have_rel = false;
210 } // end if
213 * Displays the table structure
216 $columns_cnt = 4;
217 if ($do_relation && $have_rel) {
218 $columns_cnt++;
220 if ($do_comments && $cfgRelation['commwork']) {
221 $columns_cnt++;
223 if ($do_mime && $cfgRelation['mimework']) {
224 $columns_cnt++;
227 $text_output = "|------\n";
228 $text_output .= '|' . __('Column');
229 $text_output .= '|' . __('Type');
230 $text_output .= '|' . __('Null');
231 $text_output .= '|' . __('Default');
232 if ($do_relation && $have_rel) {
233 $text_output .= '|' . __('Links to');
235 if ($do_comments) {
236 $text_output .= '|' . __('Comments');
237 $comments = PMA_getComments($db, $table);
239 if ($do_mime && $cfgRelation['mimework']) {
240 $text_output .= '|' . htmlspecialchars('MIME');
241 $mime_map = PMA_getMIME($db, $table, true);
243 $text_output .= "\n|------\n";
245 if (! PMA_exportOutputHandler($text_output)) {
246 return false;
249 $columns = PMA_DBI_get_columns($db, $table);
250 foreach ($columns as $column) {
252 $text_output = '';
254 $extracted_fieldspec = PMA_extractFieldSpec($column['Type']);
255 $type = $extracted_fieldspec['print_type'];
256 if (empty($type)) {
257 $type = '&nbsp;';
260 if (! isset($column['Default'])) {
261 if ($column['Null'] != 'NO') {
262 $column['Default'] = 'NULL';
266 $fmt_pre = '';
267 $fmt_post = '';
268 if (in_array($column['Field'], $unique_keys)) {
269 $fmt_pre = '**' . $fmt_pre;
270 $fmt_post = $fmt_post . '**';
272 if ($column['Key']=='PRI') {
273 $fmt_pre = '//' . $fmt_pre;
274 $fmt_post = $fmt_post . '//';
276 $text_output .= '|' . $fmt_pre . htmlspecialchars($column['Field']) . $fmt_post;
277 $text_output .= '|' . htmlspecialchars($type);
278 $text_output .= '|' . (($column['Null'] == '' || $column['Null'] == 'NO') ? __('No') : __('Yes'));
279 $text_output .= '|' . htmlspecialchars(isset($column['Default']) ? $column['Default'] : '');
281 $field_name = $column['Field'];
283 if ($do_relation && $have_rel) {
284 $text_output .= '|' . (isset($res_rel[$field_name]) ? htmlspecialchars($res_rel[$field_name]['foreign_table'] . ' (' . $res_rel[$field_name]['foreign_field'] . ')') : '');
286 if ($do_comments && $cfgRelation['commwork']) {
287 $text_output .= '|' . (isset($comments[$field_name]) ? htmlspecialchars($comments[$field_name]) : '');
289 if ($do_mime && $cfgRelation['mimework']) {
290 $text_output .= '|' . (isset($mime_map[$field_name]) ? htmlspecialchars(str_replace('_', '/', $mime_map[$field_name]['mimetype'])) : '');
293 $text_output .= "\n";
295 if (! PMA_exportOutputHandler($text_output)) {
296 return false;
298 } // end while
300 return true;