Translation update done using Pootle.
[phpmyadmin/dkf.git] / libraries / export / csv.php
blob6d6242ed7f72c5a49c5dcb1b479218e0e2602817
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * @package phpMyAdmin-Export-CSV
5 */
6 if (! defined('PHPMYADMIN')) {
7 exit;
10 /**
11 * Set of functions used to build CSV dumps of tables
14 if (isset($plugin_list)) {
15 $plugin_list['csv'] = array(
16 'text' => __('CSV'),
17 'extension' => 'csv',
18 'mime_type' => 'text/comma-separated-values',
19 'options' => array(
20 array('type' => 'begin_group', 'name' => 'general_opts'),
21 array('type' => 'text', 'name' => 'separator', 'text' => __('Columns separated with:')),
22 array('type' => 'text', 'name' => 'enclosed', 'text' => __('Columns enclosed with:')),
23 array('type' => 'text', 'name' => 'escaped', 'text' => __('Columns escaped with:')),
24 array('type' => 'text', 'name' => 'terminated', 'text' => __('Lines terminated with:')),
25 array('type' => 'text', 'name' => 'null', 'text' => __('Replace NULL with:')),
26 array('type' => 'bool', 'name' => 'removeCRLF', 'text' => __('Remove carriage return/line feed characters within columns')),
27 array('type' => 'bool', 'name' => 'columns', 'text' => __('Put columns names in the first row')),
28 array('type' => 'hidden', 'name' => 'structure_or_data'),
29 array('type' => 'end_group'),
31 'options_text' => __('Options'),
33 } else {
35 /**
36 * Outputs comment
38 * @param string Text of comment
40 * @return bool Whether it suceeded
42 function PMA_exportComment($text) {
43 return TRUE;
46 /**
47 * Outputs export footer
49 * @return bool Whether it suceeded
51 * @access public
53 function PMA_exportFooter() {
54 return TRUE;
57 /**
58 * Outputs export header
60 * @return bool Whether it suceeded
62 * @access public
64 function PMA_exportHeader() {
65 global $what;
66 global $csv_terminated;
67 global $csv_separator;
68 global $csv_enclosed;
69 global $csv_escaped;
71 // Here we just prepare some values for export
72 if ($what == 'excel') {
73 $csv_terminated = "\015\012";
74 switch($GLOBALS['excel_edition']) {
75 case 'win':
76 // as tested on Windows with Excel 2002 and Excel 2007
77 $csv_separator = ';';
78 break;
79 case 'mac_excel2003':
80 $csv_separator = ';';
81 break;
82 case 'mac_excel2008':
83 $csv_separator = ',';
84 break;
86 $csv_enclosed = '"';
87 $csv_escaped = '"';
88 if (isset($GLOBALS['excel_columns'])) {
89 $GLOBALS['csv_columns'] = 'yes';
91 } else {
92 if (empty($csv_terminated) || strtolower($csv_terminated) == 'auto') {
93 $csv_terminated = $GLOBALS['crlf'];
94 } else {
95 $csv_terminated = str_replace('\\r', "\015", $csv_terminated);
96 $csv_terminated = str_replace('\\n', "\012", $csv_terminated);
97 $csv_terminated = str_replace('\\t', "\011", $csv_terminated);
98 } // end if
99 $csv_separator = str_replace('\\t', "\011", $csv_separator);
101 return TRUE;
105 * Outputs database header
107 * @param string Database name
109 * @return bool Whether it suceeded
111 * @access public
113 function PMA_exportDBHeader($db) {
114 return TRUE;
118 * Outputs database footer
120 * @param string Database name
122 * @return bool Whether it suceeded
124 * @access public
126 function PMA_exportDBFooter($db) {
127 return TRUE;
131 * Outputs create database database
133 * @param string Database name
135 * @return bool Whether it suceeded
137 * @access public
139 function PMA_exportDBCreate($db) {
140 return TRUE;
144 * Outputs the content of a table in CSV format
146 * @param string the database name
147 * @param string the table name
148 * @param string the end of line sequence
149 * @param string the url to go back in case of error
150 * @param string SQL query for obtaining data
152 * @return bool Whether it suceeded
154 * @access public
156 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query) {
157 global $what;
158 global $csv_terminated;
159 global $csv_separator;
160 global $csv_enclosed;
161 global $csv_escaped;
163 // Gets the data from the database
164 $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED);
165 $fields_cnt = PMA_DBI_num_fields($result);
167 // If required, get fields name at the first line
168 if (isset($GLOBALS['csv_columns'])) {
169 $schema_insert = '';
170 for ($i = 0; $i < $fields_cnt; $i++) {
171 if ($csv_enclosed == '') {
172 $schema_insert .= stripslashes(PMA_DBI_field_name($result, $i));
173 } else {
174 $schema_insert .= $csv_enclosed
175 . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, stripslashes(PMA_DBI_field_name($result, $i)))
176 . $csv_enclosed;
178 $schema_insert .= $csv_separator;
179 } // end for
180 $schema_insert =trim(substr($schema_insert, 0, -1));
181 if (!PMA_exportOutputHandler($schema_insert . $csv_terminated)) {
182 return FALSE;
184 } // end if
186 // Format the data
187 while ($row = PMA_DBI_fetch_row($result)) {
188 $schema_insert = '';
189 for ($j = 0; $j < $fields_cnt; $j++) {
190 if (!isset($row[$j]) || is_null($row[$j])) {
191 $schema_insert .= $GLOBALS[$what . '_null'];
192 } elseif ($row[$j] == '0' || $row[$j] != '') {
193 // always enclose fields
194 if ($what == 'excel') {
195 $row[$j] = preg_replace("/\015(\012)?/", "\012", $row[$j]);
197 // remove CRLF characters within field
198 if (isset($GLOBALS[$what . '_removeCRLF']) && $GLOBALS[$what . '_removeCRLF']) {
199 $row[$j] = str_replace("\n", "", str_replace("\r", "", $row[$j]));
201 if ($csv_enclosed == '') {
202 $schema_insert .= $row[$j];
203 } else {
204 // also double the escape string if found in the data
205 if ('csv' == $what) {
206 $schema_insert .= $csv_enclosed
207 . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, str_replace($csv_escaped, $csv_escaped . $csv_escaped, $row[$j]))
208 . $csv_enclosed;
209 } else {
210 // for excel, avoid a problem when a field contains
211 // double quotes
212 $schema_insert .= $csv_enclosed
213 . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, $row[$j])
214 . $csv_enclosed;
217 } else {
218 $schema_insert .= '';
220 if ($j < $fields_cnt-1) {
221 $schema_insert .= $csv_separator;
223 } // end for
225 if (!PMA_exportOutputHandler($schema_insert . $csv_terminated)) {
226 return FALSE;
228 } // end while
229 PMA_DBI_free_result($result);
231 return TRUE;
232 } // end of the 'PMA_getTableCsv()' function