2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Set of functions used to build dumps of tables as JSON
6 * @package phpMyAdmin-Export
9 if (! defined('PHPMYADMIN')) {
16 if (isset($plugin_list)) {
17 $plugin_list['json'] = array(
19 'extension' => 'json',
20 'mime_type' => 'text/plain',
22 array('type' => 'begin_group', 'name' => 'general_opts'),
25 'name' => 'structure_or_data',
27 array('type' => 'end_group')
29 'options_text' => __('Options'),
34 * Set of functions used to build exports of tables
38 * Outputs export footer
40 * @return bool Whether it suceeded
44 function PMA_exportFooter()
50 * Outputs export header
52 * @return bool Whether it suceeded
56 function PMA_exportHeader()
58 PMA_exportOutputHandler(
59 '/**' . $GLOBALS['crlf']
60 . ' Export to JSON plugin for PHPMyAdmin' . $GLOBALS['crlf']
61 . ' @version 0.1' . $GLOBALS['crlf']
62 . ' */' . $GLOBALS['crlf'] . $GLOBALS['crlf']
68 * Outputs database header
70 * @param string $db Database name
71 * @return bool Whether it suceeded
75 function PMA_exportDBHeader($db)
77 PMA_exportOutputHandler('// Database \'' . $db . '\'' . $GLOBALS['crlf'] );
82 * Outputs database footer
84 * @param string $db Database name
85 * @return bool Whether it suceeded
89 function PMA_exportDBFooter($db)
95 * Outputs CREATE DATABASE statement
97 * @param string $db Database name
98 * @return bool Whether it suceeded
102 function PMA_exportDBCreate($db)
108 * Outputs the content of a table in JSON format
110 * @param string $db database name
111 * @param string $table table name
112 * @param string $crlf the end of line sequence
113 * @param string $error_url the url to go back in case of error
114 * @param string $sql_query SQL query for obtaining data
115 * @return bool Whether it suceeded
119 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query)
121 $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED
);
123 $columns_cnt = PMA_DBI_num_fields($result);
124 for ($i = 0; $i < $columns_cnt; $i++
) {
125 $columns[$i] = stripslashes(PMA_DBI_field_name($result, $i));
131 while ($record = PMA_DBI_fetch_row($result)) {
135 // Output table name as comment if this is the first record of the table
136 if ($record_cnt == 1) {
137 $buffer .= '// ' . $db . '.' . $table . $crlf . $crlf;
143 for ($i = 0; $i < $columns_cnt; $i++
) {
145 $isLastLine = ($i +
1 >= $columns_cnt);
147 $column = $columns[$i];
149 if (is_null($record[$i])) {
150 $buffer .= '"' . addslashes($column) . '": null' . (! $isLastLine ?
',' : '');
151 } elseif (is_numeric($record[$i])) {
152 $buffer .= '"' . addslashes($column) . '": ' . $record[$i] . (! $isLastLine ?
',' : '');
154 $buffer .= '"' . addslashes($column) . '": "' . addslashes($record[$i]) . '"' . (! $isLastLine ?
',' : '');
164 if (! PMA_exportOutputHandler($buffer)) {
168 PMA_DBI_free_result($result);