2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Export to Texy! text.
6 * @package phpMyAdmin-Export-Texy
8 if (! defined('PHPMYADMIN')) {
15 if (isset($plugin_list)) {
16 $plugin_list['texytext'] = array(
17 'text' => 'strTexyText',
19 'mime_type' => 'text/plain',
21 array('type' => 'bool',
22 'name' => 'structure',
23 'text' => 'strStructure',
25 array('type' => 'bgroup',
28 'force' => 'structure'),
29 array('type' => 'text',
31 'text' => 'strReplaceNULLBy'),
32 array('type' => 'bool',
34 'text' => 'strPutColNames'),
35 array('type' => 'egroup'),
37 'options_text' => 'strOptions',
44 * @param string Text of comment
46 * @return bool Whether it suceeded
48 function PMA_exportComment($text) {
53 * Outputs export footer
55 * @return bool Whether it suceeded
59 function PMA_exportFooter() {
64 * Outputs export header
66 * @return bool Whether it suceeded
70 function PMA_exportHeader() {
75 * Outputs database header
77 * @param string Database name
79 * @return bool Whether it suceeded
83 function PMA_exportDBHeader($db) {
84 return PMA_exportOutputHandler('===' . $GLOBALS['strDatabase'] . ' ' . $db . "\n\n");
88 * Outputs database footer
90 * @param string Database name
92 * @return bool Whether it suceeded
96 function PMA_exportDBFooter($db) {
101 * Outputs create database database
103 * @param string Database name
105 * @return bool Whether it suceeded
109 function PMA_exportDBCreate($db) {
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
126 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query)
130 if (! PMA_exportOutputHandler('== ' . $GLOBALS['strDumpingData'] . ' ' . $table . "\n\n")) {
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)));
144 $text_output .= "\n|------\n";
145 if (! PMA_exportOutputHandler($text_output)) {
151 while ($row = PMA_DBI_fetch_row($result)) {
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] != '') {
161 $text_output .= '|' . htmlspecialchars($value);
163 $text_output .= "\n";
164 if (! PMA_exportOutputHandler($text_output)) {
168 PMA_DBI_free_result($result);
173 function PMA_exportStructure($db, $table, $crlf, $error_url, $do_relation = false, $do_comments = false, $do_mime = false, $dates = false, $dummy)
177 if (! PMA_exportOutputHandler('== ' . $GLOBALS['strTableStructure'] . ' ' .$table . "\n\n")) {
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
206 $res_rel = PMA_getForeigners($db, $table);
208 if ($res_rel && count($res_rel) > 0) {
218 * Displays the table structure
222 if ($do_relation && $have_rel) {
225 if ($do_comments && $cfgRelation['commwork']) {
228 if ($do_mime && $cfgRelation['mimework']) {
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']);
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)) {
254 while ($row = PMA_DBI_fetch_assoc($result)) {
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]) . ')';
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);
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 = ' ';
283 $strAttribute = 'BINARY';
286 $strAttribute = 'UNSIGNED';
289 $strAttribute = 'UNSIGNED ZEROFILL';
291 if (! isset($row['Default'])) {
292 if ($row['Null'] != 'NO') {
293 $row['Default'] = 'NULL';
296 $row['Default'] = $row['Default'];
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)) {
332 PMA_DBI_free_result($result);