2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Set of methods used to build dumps of tables as JSON
6 * @package PhpMyAdmin-Export
9 if (! defined('PHPMYADMIN')) {
13 /* Get the export interface */
14 require_once 'libraries/plugins/ExportPlugin.class.php';
17 * Handles the export for the JSON format
19 * @package PhpMyAdmin-Export
22 class ExportJson
extends ExportPlugin
27 public function __construct()
29 $this->setProperties();
33 * Sets the export JSON properties
37 protected function setProperties()
39 $props = 'libraries/properties/';
40 include_once "$props/plugins/ExportPluginProperties.class.php";
41 include_once "$props/options/groups/OptionsPropertyRootGroup.class.php";
42 include_once "$props/options/groups/OptionsPropertyMainGroup.class.php";
43 include_once "$props/options/items/HiddenPropertyItem.class.php";
45 $exportPluginProperties = new ExportPluginProperties();
46 $exportPluginProperties->setText('JSON');
47 $exportPluginProperties->setExtension('json');
48 $exportPluginProperties->setMimeType('text/plain');
49 $exportPluginProperties->setOptionsText(__('Options'));
51 // create the root group that will be the options field for
52 // $exportPluginProperties
53 // this will be shown as "Format specific options"
54 $exportSpecificOptions = new OptionsPropertyRootGroup();
55 $exportSpecificOptions->setName("Format Specific Options");
57 // general options main group
58 $generalOptions = new OptionsPropertyMainGroup();
59 $generalOptions->setName("general_opts");
60 // create primary items and add them to the group
61 $leaf = new HiddenPropertyItem();
62 $leaf->setName("structure_or_data");
63 $generalOptions->addProperty($leaf);
64 // add the main group to the root group
65 $exportSpecificOptions->addProperty($generalOptions);
67 // set the options for the export plugin property item
68 $exportPluginProperties->setOptions($exportSpecificOptions);
69 $this->properties
= $exportPluginProperties;
73 * This method is called when any PluginManager to which the observer
74 * is attached calls PluginManager::notify()
76 * @param SplSubject $subject The PluginManager notifying the observer
81 public function update (SplSubject
$subject)
86 * Outputs export header
88 * @return bool Whether it succeeded
90 public function exportHeader ()
92 PMA_exportOutputHandler(
93 '/**' . $GLOBALS['crlf']
94 . ' Export to JSON plugin for PHPMyAdmin' . $GLOBALS['crlf']
95 . ' @version 0.1' . $GLOBALS['crlf']
96 . ' */' . $GLOBALS['crlf'] . $GLOBALS['crlf']
102 * Outputs export footer
104 * @return bool Whether it succeeded
106 public function exportFooter ()
112 * Outputs database header
114 * @param string $db Database name
116 * @return bool Whether it succeeded
118 public function exportDBHeader ($db)
120 PMA_exportOutputHandler('// Database \'' . $db . '\'' . $GLOBALS['crlf']);
125 * Outputs database footer
127 * @param string $db Database name
129 * @return bool Whether it succeeded
131 public function exportDBFooter ($db)
137 * Outputs CREATE DATABASE statement
139 * @param string $db Database name
141 * @return bool Whether it succeeded
143 public function exportDBCreate($db)
149 * Outputs the content of a table in JSON format
151 * @param string $db database name
152 * @param string $table table name
153 * @param string $crlf the end of line sequence
154 * @param string $error_url the url to go back in case of error
155 * @param string $sql_query SQL query for obtaining data
157 * @return bool Whether it succeeded
159 public function exportData($db, $table, $crlf, $error_url, $sql_query)
161 $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED
);
162 $columns_cnt = PMA_DBI_num_fields($result);
164 // Get field information
165 $fields_meta = PMA_DBI_get_fields_meta($result);
167 for ($i = 0; $i < $columns_cnt; $i++
) {
168 $columns[$i] = stripslashes(PMA_DBI_field_name($result, $i));
174 while ($record = PMA_DBI_fetch_row($result)) {
178 // Output table name as comment if this is the first record of the table
179 if ($record_cnt == 1) {
180 $buffer .= '// ' . $db . '.' . $table . $crlf . $crlf;
186 for ($i = 0; $i < $columns_cnt; $i++
) {
187 $isLastLine = ($i +
1 >= $columns_cnt);
188 $column = $columns[$i];
189 if (is_null($record[$i])) {
190 $buffer .= '"' . addslashes($column)
192 . (! $isLastLine ?
',' : '');
193 } elseif ($fields_meta[$i]->numeric) {
194 $buffer .= '"' . addslashes($column)
197 . (! $isLastLine ?
',' : '');
199 $buffer .= '"' . addslashes($column)
201 . addslashes($record[$i])
203 . (! $isLastLine ?
',' : '');
213 if (! PMA_exportOutputHandler($buffer)) {
217 PMA_DBI_free_result($result);