Merge branch 'QA_3_4'
[phpmyadmin-regexreplace.git] / libraries / export / texytext.php
blob8ed6a742acdd807dadeaaada2ce7d7030f30acb2
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Export to Texy! text.
6 * @package phpMyAdmin-Export-Texy
7 */
8 if (! defined('PHPMYADMIN')) {
9 exit;
12 /**
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'),
30 'options_text' => __('Options'),
32 } else {
34 /**
35 * Outputs comment
37 * @param string Text of comment
39 * @return bool Whether it suceeded
41 function PMA_exportComment($text) {
42 return TRUE;
45 /**
46 * Outputs export footer
48 * @return bool Whether it suceeded
50 * @access public
52 function PMA_exportFooter() {
53 return true;
56 /**
57 * Outputs export header
59 * @return bool Whether it suceeded
61 * @access public
63 function PMA_exportHeader() {
64 return true;
67 /**
68 * Outputs database header
70 * @param string Database name
72 * @return bool Whether it suceeded
74 * @access public
76 function PMA_exportDBHeader($db) {
77 return PMA_exportOutputHandler('===' . __('Database') . ' ' . $db . "\n\n");
80 /**
81 * Outputs database footer
83 * @param string Database name
85 * @return bool Whether it suceeded
87 * @access public
89 function PMA_exportDBFooter($db) {
90 return TRUE;
93 /**
94 * Outputs create database database
96 * @param string Database name
98 * @return bool Whether it suceeded
100 * @access public
102 function PMA_exportDBCreate($db) {
103 return TRUE;
107 * Outputs the content of a table in CSV format
109 * @param string the database name
110 * @param string the table name
111 * @param string the end of line sequence
112 * @param string the url to go back in case of error
113 * @param string SQL query for obtaining data
115 * @return bool Whether it suceeded
117 * @access public
119 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query)
121 global $what;
123 if (! PMA_exportOutputHandler('== ' . __('Dumping data for table') . ' ' . $table . "\n\n")) {
124 return FALSE;
127 // Gets the data from the database
128 $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED);
129 $fields_cnt = PMA_DBI_num_fields($result);
131 // If required, get fields name at the first line
132 if (isset($GLOBALS[$what . '_columns'])) {
133 $text_output = "|------\n";
134 for ($i = 0; $i < $fields_cnt; $i++) {
135 $text_output .= '|' . htmlspecialchars(stripslashes(PMA_DBI_field_name($result, $i)));
136 } // end for
137 $text_output .= "\n|------\n";
138 if (! PMA_exportOutputHandler($text_output)) {
139 return FALSE;
141 } // end if
143 // Format the data
144 while ($row = PMA_DBI_fetch_row($result)) {
145 $text_output = '';
146 for ($j = 0; $j < $fields_cnt; $j++) {
147 if (! isset($row[$j]) || is_null($row[$j])) {
148 $value = $GLOBALS[$what . '_null'];
149 } elseif ($row[$j] == '0' || $row[$j] != '') {
150 $value = $row[$j];
151 } else {
152 $value = ' ';
154 $text_output .= '|' . htmlspecialchars($value);
155 } // end for
156 $text_output .= "\n";
157 if (! PMA_exportOutputHandler($text_output)) {
158 return FALSE;
160 } // end while
161 PMA_DBI_free_result($result);
163 return TRUE;
166 function PMA_exportStructure($db, $table, $crlf, $error_url, $do_relation = false, $do_comments = false, $do_mime = false, $dates = false, $dummy)
168 global $cfgRelation;
170 if (! PMA_exportOutputHandler('== ' . __('Table structure for table') . ' ' .$table . "\n\n")) {
171 return FALSE;
175 * Get the unique keys in the table
177 $keys_query = 'SHOW KEYS FROM ' . PMA_backquote($table) . ' FROM '. PMA_backquote($db);
178 $keys_result = PMA_DBI_query($keys_query);
179 $unique_keys = array();
180 while ($key = PMA_DBI_fetch_assoc($keys_result)) {
181 if ($key['Non_unique'] == 0) {
182 $unique_keys[] = $key['Column_name'];
185 PMA_DBI_free_result($keys_result);
188 * Gets fields properties
190 PMA_DBI_select_db($db);
191 $local_query = 'SHOW FIELDS FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table);
192 $result = PMA_DBI_query($local_query);
193 $fields_cnt = PMA_DBI_num_rows($result);
195 // Check if we can use Relations (Mike Beck)
196 if ($do_relation && ! empty($cfgRelation['relation'])) {
197 // Find which tables are related with the current one and write it in
198 // an array
199 $res_rel = PMA_getForeigners($db, $table);
201 if ($res_rel && count($res_rel) > 0) {
202 $have_rel = TRUE;
203 } else {
204 $have_rel = FALSE;
206 } else {
207 $have_rel = FALSE;
208 } // end if
211 * Displays the table structure
214 $columns_cnt = 4;
215 if ($do_relation && $have_rel) {
216 $columns_cnt++;
218 if ($do_comments && $cfgRelation['commwork']) {
219 $columns_cnt++;
221 if ($do_mime && $cfgRelation['mimework']) {
222 $columns_cnt++;
225 $text_output = "|------\n";
226 $text_output .= '|' . htmlspecialchars(__('Column'));
227 $text_output .= '|' . htmlspecialchars(__('Type'));
228 $text_output .= '|' . htmlspecialchars(__('Null'));
229 $text_output .= '|' . htmlspecialchars(__('Default'));
230 if ($do_relation && $have_rel) {
231 $text_output .= '|' . htmlspecialchars(__('Links to'));
233 if ($do_comments) {
234 $text_output .= '|' . htmlspecialchars(__('Comments'));
235 $comments = PMA_getComments($db, $table);
237 if ($do_mime && $cfgRelation['mimework']) {
238 $text_output .= '|' . htmlspecialchars('MIME');
239 $mime_map = PMA_getMIME($db, $table, true);
241 $text_output .= "\n|------\n";
243 if (! PMA_exportOutputHandler($text_output)) {
244 return FALSE;
247 while ($row = PMA_DBI_fetch_assoc($result)) {
249 $text_output = '';
250 $type = $row['Type'];
251 // reformat mysql query output
252 // set or enum types: slashes single quotes inside options
253 if (preg_match('/^(set|enum)\((.+)\)$/i', $type, $tmp)) {
254 $tmp[2] = substr(preg_replace('/([^,])\'\'/', '\\1\\\'', ',' . $tmp[2]), 1);
255 $type = $tmp[1] . '(' . str_replace(',', ', ', $tmp[2]) . ')';
256 $type_nowrap = '';
258 $binary = 0;
259 $unsigned = 0;
260 $zerofill = 0;
261 } else {
262 $type_nowrap = ' nowrap="nowrap"';
263 $type = preg_replace('/BINARY/i', '', $type);
264 $type = preg_replace('/ZEROFILL/i', '', $type);
265 $type = preg_replace('/UNSIGNED/i', '', $type);
266 if (empty($type)) {
267 $type = '&nbsp;';
270 $binary = preg_match('/BINARY/i', $row['Type']);
271 $unsigned = preg_match('/UNSIGNED/i', $row['Type']);
272 $zerofill = preg_match('/ZEROFILL/i', $row['Type']);
274 $attribute = '&nbsp;';
275 if ($binary) {
276 $attribute = 'BINARY';
278 if ($unsigned) {
279 $attribute = 'UNSIGNED';
281 if ($zerofill) {
282 $attribute = 'UNSIGNED ZEROFILL';
284 if (! isset($row['Default'])) {
285 if ($row['Null'] != 'NO') {
286 $row['Default'] = 'NULL';
288 } else {
289 $row['Default'] = $row['Default'];
292 $fmt_pre = '';
293 $fmt_post = '';
294 if (in_array($row['Field'], $unique_keys)) {
295 $fmt_pre = '**' . $fmt_pre;
296 $fmt_post = $fmt_post . '**';
298 if ($row['Key']=='PRI') {
299 $fmt_pre = '//' . $fmt_pre;
300 $fmt_post = $fmt_post . '//';
302 $text_output .= '|' . $fmt_pre . htmlspecialchars($row['Field']) . $fmt_post;
303 $text_output .= '|' . htmlspecialchars($type);
304 $text_output .= '|' . htmlspecialchars(($row['Null'] == '' || $row['Null'] == 'NO') ? __('No') : __('Yes'));
305 $text_output .= '|' . htmlspecialchars(isset($row['Default']) ? $row['Default'] : '');
307 $field_name = $row['Field'];
309 if ($do_relation && $have_rel) {
310 $text_output .= '|' . (isset($res_rel[$field_name]) ? htmlspecialchars($res_rel[$field_name]['foreign_table'] . ' (' . $res_rel[$field_name]['foreign_field'] . ')') : '');
312 if ($do_comments && $cfgRelation['commwork']) {
313 $text_output .= '|' . (isset($comments[$field_name]) ? htmlspecialchars($comments[$field_name]) : '');
315 if ($do_mime && $cfgRelation['mimework']) {
316 $text_output .= '|' . (isset($mime_map[$field_name]) ? htmlspecialchars(str_replace('_', '/', $mime_map[$field_name]['mimetype'])) : '');
319 $text_output .= "\n";
321 if (! PMA_exportOutputHandler($text_output)) {
322 return FALSE;
324 } // end while
325 PMA_DBI_free_result($result);
327 return true;