2 /* vim: set expandtab sw=4 ts=4 sts=4: */
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 CSV format
19 * @package PhpMyAdmin-Export
22 class ExportCsv
extends ExportPlugin
25 * The string used to end lines
29 private $_csvTerminated;
32 * The string used to separate columns
36 private $_csvSeparator;
39 * The string used to enclose columns
43 private $_csvEnclosed;
46 * The string used to escape columns
55 public function __construct()
57 $this->setProperties();
61 * Sets the export CSV properties
65 protected function setProperties()
67 $props = 'libraries/properties/';
68 include_once "$props/plugins/ExportPluginProperties.class.php";
69 include_once "$props/options/groups/OptionsPropertyRootGroup.class.php";
70 include_once "$props/options/groups/OptionsPropertyMainGroup.class.php";
71 include_once "$props/options/items/TextPropertyItem.class.php";
72 include_once "$props/options/items/BoolPropertyItem.class.php";
73 include_once "$props/options/items/HiddenPropertyItem.class.php";
75 $exportPluginProperties = new ExportPluginProperties();
76 $exportPluginProperties->setText('CSV');
77 $exportPluginProperties->setExtension('csv');
78 $exportPluginProperties->setMimeType('text/comma-separated-values');
79 $exportPluginProperties->setOptionsText(__('Options'));
81 // create the root group that will be the options field for
82 // $exportPluginProperties
83 // this will be shown as "Format specific options"
84 $exportSpecificOptions = new OptionsPropertyRootGroup();
85 $exportSpecificOptions->setName("Format Specific Options");
87 // general options main group
88 $generalOptions = new OptionsPropertyMainGroup();
89 $generalOptions->setName("general_opts");
90 // create leaf items and add them to the group
91 $leaf = new TextPropertyItem();
92 $leaf->setName("separator");
93 $leaf->setText(__('Columns separated with:'));
94 $generalOptions->addProperty($leaf);
95 $leaf = new TextPropertyItem();
96 $leaf->setName("enclosed");
97 $leaf->setText(__('Columns enclosed with:'));
98 $generalOptions->addProperty($leaf);
99 $leaf = new TextPropertyItem();
100 $leaf->setName("escaped");
101 $leaf->setText(__('Columns escaped with:'));
102 $generalOptions->addProperty($leaf);
103 $leaf = new TextPropertyItem();
104 $leaf->setName("terminated");
105 $leaf->setText(__('Lines terminated with:'));
106 $generalOptions->addProperty($leaf);
107 $leaf = new TextPropertyItem();
108 $leaf->setName('null');
109 $leaf->setText(__('Replace NULL with:'));
110 $generalOptions->addProperty($leaf);
111 $leaf = new BoolPropertyItem();
112 $leaf->setName('removeCRLF');
114 __('Remove carriage return/line feed characters within columns')
116 $generalOptions->addProperty($leaf);
117 $leaf = new BoolPropertyItem();
118 $leaf->setName('columns');
119 $leaf->setText(__('Put columns names in the first row'));
120 $generalOptions->addProperty($leaf);
121 $leaf = new HiddenPropertyItem();
122 $leaf->setName('structure_or_data');
123 $generalOptions->addProperty($leaf);
124 // add the main group to the root group
125 $exportSpecificOptions->addProperty($generalOptions);
127 // set the options for the export plugin property item
128 $exportPluginProperties->setOptions($exportSpecificOptions);
129 $this->properties
= $exportPluginProperties;
133 * This method is called when any PluginManager to which the observer
134 * is attached calls PluginManager::notify()
136 * @param SplSubject $subject The PluginManager notifying the observer
141 public function update (SplSubject
$subject)
146 * Outputs export header
148 * @return bool Whether it succeeded
150 public function exportHeader ()
152 global $what, $csv_terminated, $csv_separator, $csv_enclosed, $csv_escaped;
154 // Here we just prepare some values for export
155 if ($what == 'excel') {
156 $csv_terminated = "\015\012";
157 switch($GLOBALS['excel_edition']) {
159 // as tested on Windows with Excel 2002 and Excel 2007
160 $csv_separator = ';';
162 case 'mac_excel2003':
163 $csv_separator = ';';
165 case 'mac_excel2008':
166 $csv_separator = ',';
171 if (isset($GLOBALS['excel_columns'])) {
172 $GLOBALS['csv_columns'] = 'yes';
175 if (empty($csv_terminated) ||
strtolower($csv_terminated) == 'auto') {
176 $csv_terminated = $GLOBALS['crlf'];
178 $csv_terminated = str_replace('\\r', "\015", $csv_terminated);
179 $csv_terminated = str_replace('\\n', "\012", $csv_terminated);
180 $csv_terminated = str_replace('\\t', "\011", $csv_terminated);
182 $csv_separator = str_replace('\\t', "\011", $csv_separator);
189 * Outputs export footer
191 * @return bool Whether it succeeded
193 public function exportFooter ()
199 * Outputs database header
201 * @param string $db Database name
203 * @return bool Whether it succeeded
205 public function exportDBHeader ($db)
211 * Outputs database footer
213 * @param string $db Database name
215 * @return bool Whether it succeeded
217 public function exportDBFooter ($db)
223 * Outputs CREATE DATABASE statement
225 * @param string $db Database name
227 * @return bool Whether it succeeded
229 public function exportDBCreate($db)
235 * Outputs the content of a table in CSV format
237 * @param string $db database name
238 * @param string $table table name
239 * @param string $crlf the end of line sequence
240 * @param string $error_url the url to go back in case of error
241 * @param string $sql_query SQL query for obtaining data
243 * @return bool Whether it succeeded
245 public function exportData($db, $table, $crlf, $error_url, $sql_query)
247 global $what, $csv_terminated, $csv_separator, $csv_enclosed, $csv_escaped;
249 // Gets the data from the database
250 $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED
);
251 $fields_cnt = PMA_DBI_num_fields($result);
253 // If required, get fields name at the first line
254 if (isset($GLOBALS['csv_columns'])) {
256 for ($i = 0; $i < $fields_cnt; $i++
) {
257 if ($csv_enclosed == '') {
258 $schema_insert .= stripslashes(PMA_DBI_field_name($result, $i));
260 $schema_insert .= $csv_enclosed
263 $csv_escaped . $csv_enclosed,
264 stripslashes(PMA_DBI_field_name($result, $i))
268 $schema_insert .= $csv_separator;
270 $schema_insert = trim(substr($schema_insert, 0, -1));
271 if (! PMA_exportOutputHandler($schema_insert . $csv_terminated)) {
277 while ($row = PMA_DBI_fetch_row($result)) {
279 for ($j = 0; $j < $fields_cnt; $j++
) {
280 if (! isset($row[$j]) ||
is_null($row[$j])) {
281 $schema_insert .= $GLOBALS[$what . '_null'];
282 } elseif ($row[$j] == '0' ||
$row[$j] != '') {
283 // always enclose fields
284 if ($what == 'excel') {
285 $row[$j] = preg_replace("/\015(\012)?/", "\012", $row[$j]);
287 // remove CRLF characters within field
288 if (isset($GLOBALS[$what . '_removeCRLF'])
289 && $GLOBALS[$what . '_removeCRLF']
291 $row[$j] = str_replace(
301 if ($csv_enclosed == '') {
302 $schema_insert .= $row[$j];
304 // also double the escape string if found in the data
305 if ($csv_escaped != $csv_enclosed) {
306 $schema_insert .= $csv_enclosed
309 $csv_escaped . $csv_enclosed,
312 $csv_escaped . $csv_escaped,
318 // avoid a problem when escape string equals enclose
319 $schema_insert .= $csv_enclosed
322 $csv_escaped . $csv_enclosed,
329 $schema_insert .= '';
331 if ($j < $fields_cnt-1) {
332 $schema_insert .= $csv_separator;
336 if (! PMA_exportOutputHandler($schema_insert . $csv_terminated)) {
340 PMA_DBI_free_result($result);