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' => __('Texy! text'),
19 'mime_type' => 'text/plain',
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'),
37 * @param string Text of comment
39 * @return bool Whether it suceeded
41 function PMA_exportComment($text) {
46 * Outputs export footer
48 * @return bool Whether it suceeded
52 function PMA_exportFooter() {
57 * Outputs export header
59 * @return bool Whether it suceeded
63 function PMA_exportHeader() {
68 * Outputs database header
70 * @param string Database name
72 * @return bool Whether it suceeded
76 function PMA_exportDBHeader($db) {
77 return PMA_exportOutputHandler('===' . __('Database') . ' ' . $db . "\n\n");
81 * Outputs database footer
83 * @param string Database name
85 * @return bool Whether it suceeded
89 function PMA_exportDBFooter($db) {
94 * Outputs create database database
96 * @param string Database name
98 * @return bool Whether it suceeded
102 function PMA_exportDBCreate($db) {
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
119 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query)
123 if (! PMA_exportOutputHandler('== ' . __('Dumping data for table') . ' ' . $table . "\n\n")) {
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)));
137 $text_output .= "\n|------\n";
138 if (! PMA_exportOutputHandler($text_output)) {
144 while ($row = PMA_DBI_fetch_row($result)) {
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] != '') {
154 $text_output .= '|' . htmlspecialchars($value);
156 $text_output .= "\n";
157 if (! PMA_exportOutputHandler($text_output)) {
161 PMA_DBI_free_result($result);
166 function PMA_exportStructure($db, $table, $crlf, $error_url, $do_relation = false, $do_comments = false, $do_mime = false, $dates = false, $dummy)
170 if (! PMA_exportOutputHandler('== ' . __('Table structure for table') . ' ' .$table . "\n\n")) {
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
199 $res_rel = PMA_getForeigners($db, $table);
201 if ($res_rel && count($res_rel) > 0) {
211 * Displays the table structure
215 if ($do_relation && $have_rel) {
218 if ($do_comments && $cfgRelation['commwork']) {
221 if ($do_mime && $cfgRelation['mimework']) {
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'));
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)) {
247 while ($row = PMA_DBI_fetch_assoc($result)) {
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]) . ')';
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);
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 = ' ';
276 $attribute = 'BINARY';
279 $attribute = 'UNSIGNED';
282 $attribute = 'UNSIGNED ZEROFILL';
284 if (! isset($row['Default'])) {
285 if ($row['Null'] != 'NO') {
286 $row['Default'] = 'NULL';
289 $row['Default'] = $row['Default'];
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)) {
325 PMA_DBI_free_result($result);