Translation update done using Pootle.
[phpmyadmin/ammaryasirr.git] / libraries / export / csv.php
blob8b04841ba1dfcb607f13e796b052d7dfe1daac87
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * CSV export code
6 * @package phpMyAdmin-Export
7 * @subpackage CSV
8 */
9 if (! defined('PHPMYADMIN')) {
10 exit;
13 /**
14 * Set of functions used to build CSV dumps of tables
17 if (isset($plugin_list)) {
18 $plugin_list['csv'] = array(
19 'text' => __('CSV'),
20 'extension' => 'csv',
21 'mime_type' => 'text/comma-separated-values',
22 'options' => array(
23 array('type' => 'begin_group', 'name' => 'general_opts'),
24 array('type' => 'text', 'name' => 'separator', 'text' => __('Columns separated with:')),
25 array('type' => 'text', 'name' => 'enclosed', 'text' => __('Columns enclosed with:')),
26 array('type' => 'text', 'name' => 'escaped', 'text' => __('Columns escaped with:')),
27 array('type' => 'text', 'name' => 'terminated', 'text' => __('Lines terminated with:')),
28 array('type' => 'text', 'name' => 'null', 'text' => __('Replace NULL with:')),
29 array('type' => 'bool', 'name' => 'removeCRLF', 'text' => __('Remove carriage return/line feed characters within columns')),
30 array('type' => 'bool', 'name' => 'columns', 'text' => __('Put columns names in the first row')),
31 array('type' => 'hidden', 'name' => 'structure_or_data'),
32 array('type' => 'end_group'),
34 'options_text' => __('Options'),
36 } else {
38 /**
39 * Outputs export footer
41 * @return bool Whether it suceeded
43 * @access public
45 function PMA_exportFooter() {
46 return true;
49 /**
50 * Outputs export header
52 * @return bool Whether it suceeded
54 * @access public
56 function PMA_exportHeader() {
57 global $what;
58 global $csv_terminated;
59 global $csv_separator;
60 global $csv_enclosed;
61 global $csv_escaped;
63 // Here we just prepare some values for export
64 if ($what == 'excel') {
65 $csv_terminated = "\015\012";
66 switch($GLOBALS['excel_edition']) {
67 case 'win':
68 // as tested on Windows with Excel 2002 and Excel 2007
69 $csv_separator = ';';
70 break;
71 case 'mac_excel2003':
72 $csv_separator = ';';
73 break;
74 case 'mac_excel2008':
75 $csv_separator = ',';
76 break;
78 $csv_enclosed = '"';
79 $csv_escaped = '"';
80 if (isset($GLOBALS['excel_columns'])) {
81 $GLOBALS['csv_columns'] = 'yes';
83 } else {
84 if (empty($csv_terminated) || strtolower($csv_terminated) == 'auto') {
85 $csv_terminated = $GLOBALS['crlf'];
86 } else {
87 $csv_terminated = str_replace('\\r', "\015", $csv_terminated);
88 $csv_terminated = str_replace('\\n', "\012", $csv_terminated);
89 $csv_terminated = str_replace('\\t', "\011", $csv_terminated);
90 } // end if
91 $csv_separator = str_replace('\\t', "\011", $csv_separator);
93 return true;
96 /**
97 * Outputs database header
99 * @param string $db Database name
100 * @return bool Whether it suceeded
102 * @access public
104 function PMA_exportDBHeader($db) {
105 return true;
109 * Outputs database footer
111 * @param string $db Database name
112 * @return bool Whether it suceeded
114 * @access public
116 function PMA_exportDBFooter($db) {
117 return true;
121 * Outputs CREATE DATABASE statement
123 * @param string $db Database name
124 * @return bool Whether it suceeded
126 * @access public
128 function PMA_exportDBCreate($db) {
129 return true;
133 * Outputs the content of a table in CSV format
135 * @param string $db database name
136 * @param string $table table name
137 * @param string $crlf the end of line sequence
138 * @param string $error_url the url to go back in case of error
139 * @param string $sql_query SQL query for obtaining data
140 * @return bool Whether it suceeded
142 * @access public
144 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query) {
145 global $what;
146 global $csv_terminated;
147 global $csv_separator;
148 global $csv_enclosed;
149 global $csv_escaped;
151 // Gets the data from the database
152 $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED);
153 $fields_cnt = PMA_DBI_num_fields($result);
155 // If required, get fields name at the first line
156 if (isset($GLOBALS['csv_columns'])) {
157 $schema_insert = '';
158 for ($i = 0; $i < $fields_cnt; $i++) {
159 if ($csv_enclosed == '') {
160 $schema_insert .= stripslashes(PMA_DBI_field_name($result, $i));
161 } else {
162 $schema_insert .= $csv_enclosed
163 . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, stripslashes(PMA_DBI_field_name($result, $i)))
164 . $csv_enclosed;
166 $schema_insert .= $csv_separator;
167 } // end for
168 $schema_insert =trim(substr($schema_insert, 0, -1));
169 if (!PMA_exportOutputHandler($schema_insert . $csv_terminated)) {
170 return false;
172 } // end if
174 // Format the data
175 while ($row = PMA_DBI_fetch_row($result)) {
176 $schema_insert = '';
177 for ($j = 0; $j < $fields_cnt; $j++) {
178 if (!isset($row[$j]) || is_null($row[$j])) {
179 $schema_insert .= $GLOBALS[$what . '_null'];
180 } elseif ($row[$j] == '0' || $row[$j] != '') {
181 // always enclose fields
182 if ($what == 'excel') {
183 $row[$j] = preg_replace("/\015(\012)?/", "\012", $row[$j]);
185 // remove CRLF characters within field
186 if (isset($GLOBALS[$what . '_removeCRLF']) && $GLOBALS[$what . '_removeCRLF']) {
187 $row[$j] = str_replace("\n", "", str_replace("\r", "", $row[$j]));
189 if ($csv_enclosed == '') {
190 $schema_insert .= $row[$j];
191 } else {
192 // also double the escape string if found in the data
193 if ('csv' == $what) {
194 $schema_insert .= $csv_enclosed
195 . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, str_replace($csv_escaped, $csv_escaped . $csv_escaped, $row[$j]))
196 . $csv_enclosed;
197 } else {
198 // for excel, avoid a problem when a field contains
199 // double quotes
200 $schema_insert .= $csv_enclosed
201 . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, $row[$j])
202 . $csv_enclosed;
205 } else {
206 $schema_insert .= '';
208 if ($j < $fields_cnt-1) {
209 $schema_insert .= $csv_separator;
211 } // end for
213 if (!PMA_exportOutputHandler($schema_insert . $csv_terminated)) {
214 return false;
216 } // end while
217 PMA_DBI_free_result($result);
219 return true;
220 } // end of the 'PMA_getTableCsv()' function