Fixed: Not selecting a datalabel used to issue a notice(undefined offset)
[phpmyadmin/ammaryasirr.git] / libraries / export / texytext.php
blob73353e1ad92df5c137fd345feb0a24cd8fb1ec66
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 $keys_query = 'SHOW KEYS FROM ' . PMA_backquote($table) . ' FROM '. PMA_backquote($db);
185 $keys_result = PMA_DBI_query($keys_query);
186 $unique_keys = array();
187 while ($key = PMA_DBI_fetch_assoc($keys_result)) {
188 if ($key['Non_unique'] == 0) {
189 $unique_keys[] = $key['Column_name'];
192 PMA_DBI_free_result($keys_result);
195 * Gets fields properties
197 PMA_DBI_select_db($db);
199 // Check if we can use Relations
200 if ($do_relation && ! empty($cfgRelation['relation'])) {
201 // Find which tables are related with the current one and write it in
202 // an array
203 $res_rel = PMA_getForeigners($db, $table);
205 if ($res_rel && count($res_rel) > 0) {
206 $have_rel = true;
207 } else {
208 $have_rel = false;
210 } else {
211 $have_rel = false;
212 } // end if
215 * Displays the table structure
218 $columns_cnt = 4;
219 if ($do_relation && $have_rel) {
220 $columns_cnt++;
222 if ($do_comments && $cfgRelation['commwork']) {
223 $columns_cnt++;
225 if ($do_mime && $cfgRelation['mimework']) {
226 $columns_cnt++;
229 $text_output = "|------\n";
230 $text_output .= '|' . __('Column');
231 $text_output .= '|' . __('Type');
232 $text_output .= '|' . __('Null');
233 $text_output .= '|' . __('Default');
234 if ($do_relation && $have_rel) {
235 $text_output .= '|' . __('Links to');
237 if ($do_comments) {
238 $text_output .= '|' . __('Comments');
239 $comments = PMA_getComments($db, $table);
241 if ($do_mime && $cfgRelation['mimework']) {
242 $text_output .= '|' . htmlspecialchars('MIME');
243 $mime_map = PMA_getMIME($db, $table, true);
245 $text_output .= "\n|------\n";
247 if (! PMA_exportOutputHandler($text_output)) {
248 return false;
251 $columns = PMA_DBI_get_columns($db, $table);
252 foreach ($columns as $column) {
254 $text_output = '';
256 $extracted_fieldspec = PMA_extractFieldSpec($column['Type']);
257 $type = $extracted_fieldspec['print_type'];
258 if (empty($type)) {
259 $type = '&nbsp;';
262 if (! isset($column['Default'])) {
263 if ($column['Null'] != 'NO') {
264 $column['Default'] = 'NULL';
268 $fmt_pre = '';
269 $fmt_post = '';
270 if (in_array($column['Field'], $unique_keys)) {
271 $fmt_pre = '**' . $fmt_pre;
272 $fmt_post = $fmt_post . '**';
274 if ($column['Key']=='PRI') {
275 $fmt_pre = '//' . $fmt_pre;
276 $fmt_post = $fmt_post . '//';
278 $text_output .= '|' . $fmt_pre . htmlspecialchars($column['Field']) . $fmt_post;
279 $text_output .= '|' . htmlspecialchars($type);
280 $text_output .= '|' . (($column['Null'] == '' || $column['Null'] == 'NO') ? __('No') : __('Yes'));
281 $text_output .= '|' . htmlspecialchars(isset($column['Default']) ? $column['Default'] : '');
283 $field_name = $column['Field'];
285 if ($do_relation && $have_rel) {
286 $text_output .= '|' . (isset($res_rel[$field_name]) ? htmlspecialchars($res_rel[$field_name]['foreign_table'] . ' (' . $res_rel[$field_name]['foreign_field'] . ')') : '');
288 if ($do_comments && $cfgRelation['commwork']) {
289 $text_output .= '|' . (isset($comments[$field_name]) ? htmlspecialchars($comments[$field_name]) : '');
291 if ($do_mime && $cfgRelation['mimework']) {
292 $text_output .= '|' . (isset($mime_map[$field_name]) ? htmlspecialchars(str_replace('_', '/', $mime_map[$field_name]['mimetype'])) : '');
295 $text_output .= "\n";
297 if (! PMA_exportOutputHandler($text_output)) {
298 return false;
300 } // end while
302 return true;