Merge branch 'QA_3_4'
[phpmyadmin-regexreplace.git] / libraries / export / json.php
blob40f4a4db55600332f191400f643d7354825bb15e
1 <?php
2 /**
3 * Set of functions used to build dumps of tables as JSON
5 */
6 if (! defined('PHPMYADMIN')) {
7 exit;
10 /**
13 if (isset($plugin_list)) {
14 $plugin_list['json'] = array(
15 'text' => 'JSON',
16 'extension' => 'json',
17 'mime_type' => 'text/plain',
18 'options' => array(
19 array('type' => 'begin_group', 'name' => 'general_opts'),
20 array(
21 'type' => 'hidden',
22 'name' => 'structure_or_data',
24 array('type' => 'end_group')
26 'options_text' => __('Options'),
28 } else {
30 /**
31 * Set of functions used to build exports of tables
34 /**
35 * Outputs comment
37 * @param string Text of comment
39 * @return bool Whether it suceeded
41 function PMA_exportComment($text)
43 PMA_exportOutputHandler('/* ' . $text . ' */' . $GLOBALS['crlf']);
44 return true;
47 /**
48 * Outputs export footer
50 * @return bool Whether it suceeded
52 * @access public
54 function PMA_exportFooter()
56 return true;
59 /**
60 * Outputs export header
62 * @return bool Whether it suceeded
64 * @access public
66 function PMA_exportHeader()
68 PMA_exportOutputHandler(
69 '/**' . $GLOBALS['crlf']
70 . ' Export to JSON plugin for PHPMyAdmin' . $GLOBALS['crlf']
71 . ' @version 0.1' . $GLOBALS['crlf']
72 . ' */' . $GLOBALS['crlf'] . $GLOBALS['crlf']
74 return true;
77 /**
78 * Outputs database header
80 * @param string Database name
82 * @return bool Whether it suceeded
84 * @access public
86 function PMA_exportDBHeader($db)
88 PMA_exportOutputHandler('/* Database \'' . $db . '\' */ ' . $GLOBALS['crlf'] );
89 return true;
92 /**
93 * Outputs database footer
95 * @param string Database name
97 * @return bool Whether it suceeded
99 * @access public
101 function PMA_exportDBFooter($db)
103 return true;
107 * Outputs create database database
109 * @param string Database name
111 * @return bool Whether it suceeded
113 * @access public
115 function PMA_exportDBCreate($db)
117 return true;
121 * Outputs the content of a table in YAML format
123 * @param string the database name
124 * @param string the table name
125 * @param string the end of line sequence
126 * @param string the url to go back in case of error
127 * @param string SQL query for obtaining data
129 * @return bool Whether it suceeded
131 * @access public
133 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query)
135 $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED);
137 $columns_cnt = PMA_DBI_num_fields($result);
138 for ($i = 0; $i < $columns_cnt; $i++) {
139 $columns[$i] = stripslashes(PMA_DBI_field_name($result, $i));
141 unset($i);
143 $buffer = '';
144 $record_cnt = 0;
145 while ($record = PMA_DBI_fetch_row($result)) {
147 $record_cnt++;
149 // Output table name as comment if this is the first record of the table
150 if ($record_cnt == 1) {
151 $buffer .= '/* ' . $db . '.' . $table . ' */' . $crlf . $crlf;
152 $buffer .= '[{';
153 } else {
154 $buffer .= ', {';
157 for ($i = 0; $i < $columns_cnt; $i++) {
159 $isLastLine = ($i + 1 >= $columns_cnt);
161 $column = $columns[$i];
163 if (is_null($record[$i])) {
164 $buffer .= '"' . $column . '": null' . (! $isLastLine ? ',' : '');
165 } elseif (is_numeric($record[$i])) {
166 $buffer .= '"' . $column . '": ' . $record[$i] . (! $isLastLine ? ',' : '');
167 } else {
168 $buffer .= '"' . $column . '": "' . addslashes($record[$i]) . '"' . (! $isLastLine ? ',' : '');
172 $buffer .= '}';
175 $buffer .= ']';
176 if (! PMA_exportOutputHandler($buffer)) {
177 return FALSE;
180 PMA_DBI_free_result($result);
182 return true;