remove todo, after upgrade PHPExcel
[phpmyadmin/arisferyanto.git] / libraries / export / csv.php
blobe59e3bd7f0e00ec8725a75918c2ded46022042df
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * @package phpMyAdmin-Export-CSV
5 * @version $Id$
6 */
7 if (! defined('PHPMYADMIN')) {
8 exit;
11 /**
12 * Set of functions used to build CSV dumps of tables
15 if (isset($plugin_list)) {
16 $plugin_list['csv'] = array(
17 'text' => 'strStrucCSV',
18 'extension' => 'csv',
19 'mime_type' => 'text/comma-separated-values',
20 'options' => array(
21 array('type' => 'text', 'name' => 'separator', 'text' => 'strFieldsTerminatedBy'),
22 array('type' => 'text', 'name' => 'enclosed', 'text' => 'strFieldsEnclosedBy'),
23 array('type' => 'text', 'name' => 'escaped', 'text' => 'strFieldsEscapedBy'),
24 array('type' => 'text', 'name' => 'terminated', 'text' => 'strLinesTerminatedBy'),
25 array('type' => 'text', 'name' => 'null', 'text' => 'strReplaceNULLBy'),
26 array('type' => 'bool', 'name' => 'removeCRLF', 'text' => 'strRemoveCRLF'),
27 array('type' => 'bool', 'name' => 'columns', 'text' => 'strPutColNames'),
28 array('type' => 'hidden', 'name' => 'data'),
30 'options_text' => 'strOptions',
32 } else {
34 /**
35 * Outputs comment
37 * @param string Text of comment
39 * @return bool Whether it suceeded
41 function PMA_exportComment($text) {
42 return TRUE;
45 /**
46 * Outputs export footer
48 * @return bool Whether it suceeded
50 * @access public
52 function PMA_exportFooter() {
53 return TRUE;
56 /**
57 * Outputs export header
59 * @return bool Whether it suceeded
61 * @access public
63 function PMA_exportHeader() {
64 global $what;
65 global $csv_terminated;
66 global $csv_separator;
67 global $csv_enclosed;
68 global $csv_escaped;
70 // Here we just prepare some values for export
71 if ($what == 'excel') {
72 $csv_terminated = "\015\012";
73 switch($GLOBALS['excel_edition']) {
74 case 'win':
75 // as tested on Windows with Excel 2002 and Excel 2007
76 $csv_separator = ';';
77 break;
78 case 'mac_excel2003':
79 $csv_separator = ';';
80 break;
81 case 'mac_excel2008':
82 $csv_separator = ',';
83 break;
85 $csv_enclosed = '"';
86 $csv_escaped = '"';
87 if (isset($GLOBALS['excel_columns'])) {
88 $GLOBALS['csv_columns'] = 'yes';
90 } else {
91 if (empty($csv_terminated) || strtolower($csv_terminated) == 'auto') {
92 $csv_terminated = $GLOBALS['crlf'];
93 } else {
94 $csv_terminated = str_replace('\\r', "\015", $csv_terminated);
95 $csv_terminated = str_replace('\\n', "\012", $csv_terminated);
96 $csv_terminated = str_replace('\\t', "\011", $csv_terminated);
97 } // end if
98 $csv_separator = str_replace('\\t', "\011", $csv_separator);
100 return TRUE;
104 * Outputs database header
106 * @param string Database name
108 * @return bool Whether it suceeded
110 * @access public
112 function PMA_exportDBHeader($db) {
113 return TRUE;
117 * Outputs database footer
119 * @param string Database name
121 * @return bool Whether it suceeded
123 * @access public
125 function PMA_exportDBFooter($db) {
126 return TRUE;
130 * Outputs create database database
132 * @param string Database name
134 * @return bool Whether it suceeded
136 * @access public
138 function PMA_exportDBCreate($db) {
139 return TRUE;
143 * Outputs the content of a table in CSV format
145 * @param string the database name
146 * @param string the table name
147 * @param string the end of line sequence
148 * @param string the url to go back in case of error
149 * @param string SQL query for obtaining data
151 * @return bool Whether it suceeded
153 * @access public
155 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query) {
156 global $what;
157 global $csv_terminated;
158 global $csv_separator;
159 global $csv_enclosed;
160 global $csv_escaped;
162 // Gets the data from the database
163 $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED);
164 $fields_cnt = PMA_DBI_num_fields($result);
166 // If required, get fields name at the first line
167 if (isset($GLOBALS['csv_columns'])) {
168 $schema_insert = '';
169 for ($i = 0; $i < $fields_cnt; $i++) {
170 if ($csv_enclosed == '') {
171 $schema_insert .= stripslashes(PMA_DBI_field_name($result, $i));
172 } else {
173 $schema_insert .= $csv_enclosed
174 . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, stripslashes(PMA_DBI_field_name($result, $i)))
175 . $csv_enclosed;
177 $schema_insert .= $csv_separator;
178 } // end for
179 $schema_insert =trim(substr($schema_insert, 0, -1));
180 if (!PMA_exportOutputHandler($schema_insert . $csv_terminated)) {
181 return FALSE;
183 } // end if
185 // Format the data
186 while ($row = PMA_DBI_fetch_row($result)) {
187 $schema_insert = '';
188 for ($j = 0; $j < $fields_cnt; $j++) {
189 if (!isset($row[$j]) || is_null($row[$j])) {
190 $schema_insert .= $GLOBALS[$what . '_null'];
191 } elseif ($row[$j] == '0' || $row[$j] != '') {
192 // loic1 : always enclose fields
193 if ($what == 'excel') {
194 $row[$j] = preg_replace("/\015(\012)?/", "\012", $row[$j]);
196 // remove CRLF characters within field
197 if (isset($GLOBALS[$what . '_removeCRLF']) && $GLOBALS[$what . '_removeCRLF']) {
198 $row[$j] = str_replace("\n", "", str_replace("\r", "", $row[$j]));
200 if ($csv_enclosed == '') {
201 $schema_insert .= $row[$j];
202 } else {
203 // also double the escape string if found in the data
204 if ('csv' == $what) {
205 $schema_insert .= $csv_enclosed
206 . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, str_replace($csv_escaped, $csv_escaped . $csv_escaped, $row[$j]))
207 . $csv_enclosed;
208 } else {
209 // for excel, avoid a problem when a field contains
210 // double quotes
211 $schema_insert .= $csv_enclosed
212 . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, $row[$j])
213 . $csv_enclosed;
216 } else {
217 $schema_insert .= '';
219 if ($j < $fields_cnt-1) {
220 $schema_insert .= $csv_separator;
222 } // end for
224 if (!PMA_exportOutputHandler($schema_insert . $csv_terminated)) {
225 return FALSE;
227 } // end while
228 PMA_DBI_free_result($result);
230 return TRUE;
231 } // end of the 'PMA_getTableCsv()' function