Upgraded phpmyadmin to 4.0.4 (All Languages) - No modifications yet
[openemr.git] / phpmyadmin / libraries / plugins / export / ExportYaml.class.php
blob044daeee47ddcf4408295b0179a4e633b8cccd6e
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 * @package PhpMyAdmin-Export
7 * @subpackage YAML
8 */
9 if (! defined('PHPMYADMIN')) {
10 exit;
13 /* Get the export interface */
14 require_once 'libraries/plugins/ExportPlugin.class.php';
16 /**
17 * Handles the export for the YAML format
19 * @package PhpMyAdmin-Export
20 * @subpackage YAML
22 class ExportYaml extends ExportPlugin
24 /**
25 * Constructor
27 public function __construct()
29 $this->setProperties();
32 /**
33 * Sets the export YAML properties
35 * @return void
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('YAML');
47 $exportPluginProperties->setExtension('yml');
48 $exportPluginProperties->setMimeType('text/yaml');
49 $exportPluginProperties->setForceFile(true);
50 $exportPluginProperties->setOptionsText(__('Options'));
52 // create the root group that will be the options field for
53 // $exportPluginProperties
54 // this will be shown as "Format specific options"
55 $exportSpecificOptions = new OptionsPropertyRootGroup();
56 $exportSpecificOptions->setName("Format Specific Options");
58 // general options main group
59 $generalOptions = new OptionsPropertyMainGroup();
60 $generalOptions->setName("general_opts");
61 // create primary items and add them to the group
62 $leaf = new HiddenPropertyItem();
63 $leaf->setName("structure_or_data");
64 $generalOptions->addProperty($leaf);
65 // add the main group to the root group
66 $exportSpecificOptions->addProperty($generalOptions);
68 // set the options for the export plugin property item
69 $exportPluginProperties->setOptions($exportSpecificOptions);
70 $this->properties = $exportPluginProperties;
73 /**
74 * This method is called when any PluginManager to which the observer
75 * is attached calls PluginManager::notify()
77 * @param SplSubject $subject The PluginManager notifying the observer
78 * of an update.
80 * @return void
82 public function update (SplSubject $subject)
86 /**
87 * Outputs export header
89 * @return bool Whether it succeeded
91 public function exportHeader ()
93 PMA_exportOutputHandler(
94 '%YAML 1.1' . $GLOBALS['crlf'] . '---' . $GLOBALS['crlf']
96 return true;
99 /**
100 * Outputs export footer
102 * @return bool Whether it succeeded
104 public function exportFooter ()
106 PMA_exportOutputHandler('...' . $GLOBALS['crlf']);
107 return true;
111 * Outputs database header
113 * @param string $db Database name
115 * @return bool Whether it succeeded
117 public function exportDBHeader ($db)
119 return true;
123 * Outputs database footer
125 * @param string $db Database name
127 * @return bool Whether it succeeded
129 public function exportDBFooter ($db)
131 return true;
135 * Outputs CREATE DATABASE statement
137 * @param string $db Database name
139 * @return bool Whether it succeeded
141 public function exportDBCreate($db)
143 return true;
147 * Outputs the content of a table in JSON format
149 * @param string $db database name
150 * @param string $table table name
151 * @param string $crlf the end of line sequence
152 * @param string $error_url the url to go back in case of error
153 * @param string $sql_query SQL query for obtaining data
155 * @return bool Whether it succeeded
157 public function exportData($db, $table, $crlf, $error_url, $sql_query)
159 $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED);
161 $columns_cnt = PMA_DBI_num_fields($result);
162 for ($i = 0; $i < $columns_cnt; $i++) {
163 $columns[$i] = stripslashes(PMA_DBI_field_name($result, $i));
165 unset($i);
167 $buffer = '';
168 $record_cnt = 0;
169 while ($record = PMA_DBI_fetch_row($result)) {
170 $record_cnt++;
172 // Output table name as comment if this is the first record of the table
173 if ($record_cnt == 1) {
174 $buffer = '# ' . $db . '.' . $table . $crlf;
175 $buffer .= '-' . $crlf;
176 } else {
177 $buffer = '-' . $crlf;
180 for ($i = 0; $i < $columns_cnt; $i++) {
181 if (! isset($record[$i])) {
182 continue;
185 $column = $columns[$i];
187 if (is_null($record[$i])) {
188 $buffer .= ' ' . $column . ': null' . $crlf;
189 continue;
192 if (is_numeric($record[$i])) {
193 $buffer .= ' ' . $column . ': ' . $record[$i] . $crlf;
194 continue;
197 $record[$i] = str_replace(
198 array('\\', '"', "\n", "\r"),
199 array('\\\\', '\"', '\n', '\r'),
200 $record[$i]
202 $buffer .= ' ' . $column . ': "' . $record[$i] . '"' . $crlf;
205 if (! PMA_exportOutputHandler($buffer)) {
206 return false;
209 PMA_DBI_free_result($result);
211 return true;
212 } // end getTableYAML