3.3.2-rc1
[phpmyadmin/madhuracj.git] / libraries / export / yaml.php
blobd1ba38e8102e0c366be30b9c989eb601192e7ad9
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Set of functions used to build YAML dumps of tables
6 * @version $Id$
7 * @package phpMyAdmin-Export-YAML
8 */
9 if (! defined('PHPMYADMIN')) {
10 exit;
13 /**
16 if (isset($plugin_list)) {
17 $plugin_list['yaml'] = array(
18 'text' => 'YAML',
19 'extension' => 'yml',
20 'mime_type' => 'text/yaml',
21 'force_file' => true,
22 'options' => array(
23 array(
24 'type' => 'hidden',
25 'name' => 'data',
28 'options_text' => 'strOptions',
30 } else {
32 /**
33 * Set of functions used to build exports of tables
36 /**
37 * Outputs comment
39 * @param string Text of comment
41 * @return bool Whether it suceeded
43 function PMA_exportComment($text)
45 PMA_exportOutputHandler('# ' . $text . $GLOBALS['crlf']);
46 return true;
49 /**
50 * Outputs export footer
52 * @return bool Whether it suceeded
54 * @access public
56 function PMA_exportFooter()
58 PMA_exportOutputHandler('...' . $GLOBALS['crlf']);
59 return true;
62 /**
63 * Outputs export header
65 * @return bool Whether it suceeded
67 * @access public
69 function PMA_exportHeader()
71 PMA_exportOutputHandler('%YAML 1.1' . $GLOBALS['crlf'] . '---' . $GLOBALS['crlf']);
72 return true;
75 /**
76 * Outputs database header
78 * @param string Database name
80 * @return bool Whether it suceeded
82 * @access public
84 function PMA_exportDBHeader($db)
86 return true;
89 /**
90 * Outputs database footer
92 * @param string Database name
94 * @return bool Whether it suceeded
96 * @access public
98 function PMA_exportDBFooter($db)
100 return true;
104 * Outputs create database database
106 * @param string Database name
108 * @return bool Whether it suceeded
110 * @access public
112 function PMA_exportDBCreate($db)
114 return true;
118 * Outputs the content of a table in YAML format
120 * @param string the database name
121 * @param string the table name
122 * @param string the end of line sequence
123 * @param string the url to go back in case of error
124 * @param string SQL query for obtaining data
126 * @return bool Whether it suceeded
128 * @access public
130 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query)
132 $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED);
134 $columns_cnt = PMA_DBI_num_fields($result);
135 for ($i = 0; $i < $columns_cnt; $i++) {
136 $columns[$i] = stripslashes(PMA_DBI_field_name($result, $i));
138 unset($i);
140 $buffer = '';
141 $record_cnt = 0;
142 while ($record = PMA_DBI_fetch_row($result)) {
143 $record_cnt++;
145 // Output table name as comment if this is the first record of the table
146 if ($record_cnt == 1) {
147 $buffer = '# ' . $db . '.' . $table . $crlf;
148 $buffer .= '-' . $crlf;
149 } else {
150 $buffer = '-' . $crlf;
153 for ($i = 0; $i < $columns_cnt; $i++) {
154 if (! isset($record[$i])) {
155 continue;
158 $column = $columns[$i];
160 if (is_null($record[$i])) {
161 $buffer .= ' ' . $column . ': null' . $crlf;
162 continue;
165 if (is_numeric($record[$i])) {
166 $buffer .= ' ' . $column . ': ' . $record[$i] . $crlf;
167 continue;
170 $record[$i] = preg_replace('/\r\n|\r|\n/', $crlf.' ', $record[$i]);
171 if (strstr($record[$i], $crlf))
172 $record[$i] = '|-' . $crlf . ' '.$record[$i];
174 $buffer .= ' ' . $column . ': ' . $record[$i] . $crlf;
177 if (! PMA_exportOutputHandler($buffer)) {
178 return FALSE;
181 PMA_DBI_free_result($result);
183 return true;