3.3.2-rc1
[phpmyadmin/madhuracj.git] / libraries / export / texytext.php
blobddb7668329c794cf39441350e8bb92f223a04e55
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' => 'strTexyText',
18 'extension' => 'txt',
19 'mime_type' => 'text/plain',
20 'options' => array(
21 array('type' => 'bool',
22 'name' => 'structure',
23 'text' => 'strStructure',
24 'force' => 'data'),
25 array('type' => 'bgroup',
26 'name' => 'data',
27 'text' => 'strData',
28 'force' => 'structure'),
29 array('type' => 'text',
30 'name' => 'null',
31 'text' => 'strReplaceNULLBy'),
32 array('type' => 'bool',
33 'name' => 'columns',
34 'text' => 'strPutColNames'),
35 array('type' => 'egroup'),
37 'options_text' => 'strOptions',
39 } else {
41 /**
42 * Outputs comment
44 * @param string Text of comment
46 * @return bool Whether it suceeded
48 function PMA_exportComment($text) {
49 return TRUE;
52 /**
53 * Outputs export footer
55 * @return bool Whether it suceeded
57 * @access public
59 function PMA_exportFooter() {
60 return true;
63 /**
64 * Outputs export header
66 * @return bool Whether it suceeded
68 * @access public
70 function PMA_exportHeader() {
71 return true;
74 /**
75 * Outputs database header
77 * @param string Database name
79 * @return bool Whether it suceeded
81 * @access public
83 function PMA_exportDBHeader($db) {
84 return PMA_exportOutputHandler('===' . $GLOBALS['strDatabase'] . ' ' . $db . "\n\n");
87 /**
88 * Outputs database footer
90 * @param string Database name
92 * @return bool Whether it suceeded
94 * @access public
96 function PMA_exportDBFooter($db) {
97 return TRUE;
101 * Outputs create database database
103 * @param string Database name
105 * @return bool Whether it suceeded
107 * @access public
109 function PMA_exportDBCreate($db) {
110 return TRUE;
114 * Outputs the content of a table in CSV format
116 * @param string the database name
117 * @param string the table name
118 * @param string the end of line sequence
119 * @param string the url to go back in case of error
120 * @param string SQL query for obtaining data
122 * @return bool Whether it suceeded
124 * @access public
126 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query)
128 global $what;
130 if (! PMA_exportOutputHandler('== ' . $GLOBALS['strDumpingData'] . ' ' . $table . "\n\n")) {
131 return FALSE;
134 // Gets the data from the database
135 $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED);
136 $fields_cnt = PMA_DBI_num_fields($result);
138 // If required, get fields name at the first line
139 if (isset($GLOBALS[$what . '_columns'])) {
140 $text_output = "|------\n";
141 for ($i = 0; $i < $fields_cnt; $i++) {
142 $text_output .= '|' . htmlspecialchars(stripslashes(PMA_DBI_field_name($result, $i)));
143 } // end for
144 $text_output .= "\n|------\n";
145 if (! PMA_exportOutputHandler($text_output)) {
146 return FALSE;
148 } // end if
150 // Format the data
151 while ($row = PMA_DBI_fetch_row($result)) {
152 $text_output = '';
153 for ($j = 0; $j < $fields_cnt; $j++) {
154 if (! isset($row[$j]) || is_null($row[$j])) {
155 $value = $GLOBALS[$what . '_null'];
156 } elseif ($row[$j] == '0' || $row[$j] != '') {
157 $value = $row[$j];
158 } else {
159 $value = ' ';
161 $text_output .= '|' . htmlspecialchars($value);
162 } // end for
163 $text_output .= "\n";
164 if (! PMA_exportOutputHandler($text_output)) {
165 return FALSE;
167 } // end while
168 PMA_DBI_free_result($result);
170 return TRUE;
173 function PMA_exportStructure($db, $table, $crlf, $error_url, $do_relation = false, $do_comments = false, $do_mime = false, $dates = false, $dummy)
175 global $cfgRelation;
177 if (! PMA_exportOutputHandler('== ' . $GLOBALS['strTableStructure'] . ' ' .$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);
198 $local_query = 'SHOW FIELDS FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table);
199 $result = PMA_DBI_query($local_query);
200 $fields_cnt = PMA_DBI_num_rows($result);
202 // Check if we can use Relations (Mike Beck)
203 if ($do_relation && ! empty($cfgRelation['relation'])) {
204 // Find which tables are related with the current one and write it in
205 // an array
206 $res_rel = PMA_getForeigners($db, $table);
208 if ($res_rel && count($res_rel) > 0) {
209 $have_rel = TRUE;
210 } else {
211 $have_rel = FALSE;
213 } else {
214 $have_rel = FALSE;
215 } // end if
218 * Displays the table structure
221 $columns_cnt = 4;
222 if ($do_relation && $have_rel) {
223 $columns_cnt++;
225 if ($do_comments && $cfgRelation['commwork']) {
226 $columns_cnt++;
228 if ($do_mime && $cfgRelation['mimework']) {
229 $columns_cnt++;
232 $text_output = "|------\n";
233 $text_output .= '|' . htmlspecialchars($GLOBALS['strField']);
234 $text_output .= '|' . htmlspecialchars($GLOBALS['strType']);
235 $text_output .= '|' . htmlspecialchars($GLOBALS['strNull']);
236 $text_output .= '|' . htmlspecialchars($GLOBALS['strDefault']);
237 if ($do_relation && $have_rel) {
238 $text_output .= '|' . htmlspecialchars($GLOBALS['strLinksTo']);
240 if ($do_comments) {
241 $text_output .= '|' . htmlspecialchars($GLOBALS['strComments']);
242 $comments = PMA_getComments($db, $table);
244 if ($do_mime && $cfgRelation['mimework']) {
245 $text_output .= '|' . htmlspecialchars('MIME');
246 $mime_map = PMA_getMIME($db, $table, true);
248 $text_output .= "\n|------\n";
250 if (! PMA_exportOutputHandler($text_output)) {
251 return FALSE;
254 while ($row = PMA_DBI_fetch_assoc($result)) {
256 $text_output = '';
257 $type = $row['Type'];
258 // reformat mysql query output - staybyte - 9. June 2001
259 // loic1: set or enum types: slashes single quotes inside options
260 if (preg_match('/^(set|enum)\((.+)\)$/i', $type, $tmp)) {
261 $tmp[2] = substr(preg_replace('/([^,])\'\'/', '\\1\\\'', ',' . $tmp[2]), 1);
262 $type = $tmp[1] . '(' . str_replace(',', ', ', $tmp[2]) . ')';
263 $type_nowrap = '';
265 $binary = 0;
266 $unsigned = 0;
267 $zerofill = 0;
268 } else {
269 $type_nowrap = ' nowrap="nowrap"';
270 $type = preg_replace('/BINARY/i', '', $type);
271 $type = preg_replace('/ZEROFILL/i', '', $type);
272 $type = preg_replace('/UNSIGNED/i', '', $type);
273 if (empty($type)) {
274 $type = '&nbsp;';
277 $binary = preg_match('/BINARY/i', $row['Type']);
278 $unsigned = preg_match('/UNSIGNED/i', $row['Type']);
279 $zerofill = preg_match('/ZEROFILL/i', $row['Type']);
281 $strAttribute = '&nbsp;';
282 if ($binary) {
283 $strAttribute = 'BINARY';
285 if ($unsigned) {
286 $strAttribute = 'UNSIGNED';
288 if ($zerofill) {
289 $strAttribute = 'UNSIGNED ZEROFILL';
291 if (! isset($row['Default'])) {
292 if ($row['Null'] != 'NO') {
293 $row['Default'] = 'NULL';
295 } else {
296 $row['Default'] = $row['Default'];
299 $fmt_pre = '';
300 $fmt_post = '';
301 if (in_array($row['Field'], $unique_keys)) {
302 $fmt_pre = '**' . $fmt_pre;
303 $fmt_post = $fmt_post . '**';
305 if ($row['Key']=='PRI') {
306 $fmt_pre = '//' . $fmt_pre;
307 $fmt_post = $fmt_post . '//';
309 $text_output .= '|' . $fmt_pre . htmlspecialchars($row['Field']) . $fmt_post;
310 $text_output .= '|' . htmlspecialchars($type);
311 $text_output .= '|' . htmlspecialchars(($row['Null'] == '' || $row['Null'] == 'NO') ? $GLOBALS['strNo'] : $GLOBALS['strYes']);
312 $text_output .= '|' . htmlspecialchars(isset($row['Default']) ? $row['Default'] : '');
314 $field_name = $row['Field'];
316 if ($do_relation && $have_rel) {
317 $text_output .= '|' . (isset($res_rel[$field_name]) ? htmlspecialchars($res_rel[$field_name]['foreign_table'] . ' (' . $res_rel[$field_name]['foreign_field'] . ')') : '');
319 if ($do_comments && $cfgRelation['commwork']) {
320 $text_output .= '|' . (isset($comments[$field_name]) ? htmlspecialchars($comments[$field_name]) : '');
322 if ($do_mime && $cfgRelation['mimework']) {
323 $text_output .= '|' . (isset($mime_map[$field_name]) ? htmlspecialchars(str_replace('_', '/', $mime_map[$field_name]['mimetype'])) : '');
326 $text_output .= "\n";
328 if (! PMA_exportOutputHandler($text_output)) {
329 return FALSE;
331 } // end while
332 PMA_DBI_free_result($result);
334 return true;