Translation update done using Pootle.
[phpmyadmin/ammaryasirr.git] / libraries / export / json.php
blob860d640caa694626628baa075cb47b536958cd4b
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Set of functions used to build dumps of tables as JSON
6 * @package phpMyAdmin-Export
7 * @subpackage JSON
8 */
9 if (! defined('PHPMYADMIN')) {
10 exit;
13 /**
16 if (isset($plugin_list)) {
17 $plugin_list['json'] = array(
18 'text' => 'JSON',
19 'extension' => 'json',
20 'mime_type' => 'text/plain',
21 'options' => array(
22 array('type' => 'begin_group', 'name' => 'general_opts'),
23 array(
24 'type' => 'hidden',
25 'name' => 'structure_or_data',
27 array('type' => 'end_group')
29 'options_text' => __('Options'),
31 } else {
33 /**
34 * Set of functions used to build exports of tables
37 /**
38 * Outputs export footer
40 * @return bool Whether it suceeded
42 * @access public
44 function PMA_exportFooter()
46 return true;
49 /**
50 * Outputs export header
52 * @return bool Whether it suceeded
54 * @access public
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']
64 return true;
67 /**
68 * Outputs database header
70 * @param string $db Database name
71 * @return bool Whether it suceeded
73 * @access public
75 function PMA_exportDBHeader($db)
77 PMA_exportOutputHandler('// Database \'' . $db . '\'' . $GLOBALS['crlf'] );
78 return true;
81 /**
82 * Outputs database footer
84 * @param string $db Database name
85 * @return bool Whether it suceeded
87 * @access public
89 function PMA_exportDBFooter($db)
91 return true;
94 /**
95 * Outputs CREATE DATABASE statement
97 * @param string $db Database name
98 * @return bool Whether it suceeded
100 * @access public
102 function PMA_exportDBCreate($db)
104 return true;
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
117 * @access public
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));
127 unset($i);
129 $buffer = '';
130 $record_cnt = 0;
131 while ($record = PMA_DBI_fetch_row($result)) {
133 $record_cnt++;
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;
138 $buffer .= '[{';
139 } else {
140 $buffer .= ', {';
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 ? ',' : '');
153 } else {
154 $buffer .= '"' . addslashes($column) . '": "' . addslashes($record[$i]) . '"' . (! $isLastLine ? ',' : '');
158 $buffer .= '}';
161 if ($record_cnt) {
162 $buffer .= ']';
164 if (! PMA_exportOutputHandler($buffer)) {
165 return false;
168 PMA_DBI_free_result($result);
170 return true;