alternate --ALL-- fix
[openemr.git] / phpmyadmin / libraries / export / csv.php
blobb1b6b223674eae5cd1a38fb47f854b146bd012f8
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * @version $Id$
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' => 'strStrucCSV',
17 'extension' => 'csv',
18 'mime_type' => 'text/comma-separated-values',
19 'options' => array(
20 array('type' => 'text', 'name' => 'separator', 'text' => 'strFieldsTerminatedBy'),
21 array('type' => 'text', 'name' => 'enclosed', 'text' => 'strFieldsEnclosedBy'),
22 array('type' => 'text', 'name' => 'escaped', 'text' => 'strFieldsEscapedBy'),
23 array('type' => 'text', 'name' => 'terminated', 'text' => 'strLinesTerminatedBy'),
24 array('type' => 'text', 'name' => 'null', 'text' => 'strReplaceNULLBy'),
25 array('type' => 'bool', 'name' => 'columns', 'text' => 'strPutColNames'),
26 array('type' => 'hidden', 'name' => 'data'),
28 'options_text' => 'strOptions',
30 } else {
32 /**
33 * Outputs comment
35 * @param string Text of comment
37 * @return bool Whether it suceeded
39 function PMA_exportComment($text) {
40 return TRUE;
43 /**
44 * Outputs export footer
46 * @return bool Whether it suceeded
48 * @access public
50 function PMA_exportFooter() {
51 return TRUE;
54 /**
55 * Outputs export header
57 * @return bool Whether it suceeded
59 * @access public
61 function PMA_exportHeader() {
62 global $what;
63 global $csv_terminated;
64 global $csv_separator;
65 global $csv_enclosed;
66 global $csv_escaped;
68 // Here we just prepare some values for export
69 if ($what == 'excel') {
70 $csv_terminated = "\015\012";
71 $csv_separator = isset($GLOBALS['excel_edition']) && $GLOBALS['excel_edition'] == 'mac' ? ';' : ',';
72 $csv_enclosed = '"';
73 $csv_escaped = '"';
74 if (isset($GLOBALS['excel_columns'])) {
75 $GLOBALS['csv_columns'] = 'yes';
77 } else {
78 if (empty($csv_terminated) || strtolower($csv_terminated) == 'auto') {
79 $csv_terminated = $GLOBALS['crlf'];
80 } else {
81 $csv_terminated = str_replace('\\r', "\015", $csv_terminated);
82 $csv_terminated = str_replace('\\n', "\012", $csv_terminated);
83 $csv_terminated = str_replace('\\t', "\011", $csv_terminated);
84 } // end if
85 $csv_separator = str_replace('\\t', "\011", $csv_separator);
87 return TRUE;
90 /**
91 * Outputs database header
93 * @param string Database name
95 * @return bool Whether it suceeded
97 * @access public
99 function PMA_exportDBHeader($db) {
100 return TRUE;
104 * Outputs database footer
106 * @param string Database name
108 * @return bool Whether it suceeded
110 * @access public
112 function PMA_exportDBFooter($db) {
113 return TRUE;
117 * Outputs create database database
119 * @param string Database name
121 * @return bool Whether it suceeded
123 * @access public
125 function PMA_exportDBCreate($db) {
126 return TRUE;
130 * Outputs the content of a table in CSV format
132 * @param string the database name
133 * @param string the table name
134 * @param string the end of line sequence
135 * @param string the url to go back in case of error
136 * @param string SQL query for obtaining data
138 * @return bool Whether it suceeded
140 * @access public
142 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query) {
143 global $what;
144 global $csv_terminated;
145 global $csv_separator;
146 global $csv_enclosed;
147 global $csv_escaped;
149 // Gets the data from the database
150 $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED);
151 $fields_cnt = PMA_DBI_num_fields($result);
153 // If required, get fields name at the first line
154 if (isset($GLOBALS['csv_columns'])) {
155 $schema_insert = '';
156 for ($i = 0; $i < $fields_cnt; $i++) {
157 if ($csv_enclosed == '') {
158 $schema_insert .= stripslashes(PMA_DBI_field_name($result, $i));
159 } else {
160 $schema_insert .= $csv_enclosed
161 . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, stripslashes(PMA_DBI_field_name($result, $i)))
162 . $csv_enclosed;
164 $schema_insert .= $csv_separator;
165 } // end for
166 $schema_insert =trim(substr($schema_insert, 0, -1));
167 if (!PMA_exportOutputHandler($schema_insert . $csv_terminated)) {
168 return FALSE;
170 } // end if
172 // Format the data
173 while ($row = PMA_DBI_fetch_row($result)) {
174 $schema_insert = '';
175 for ($j = 0; $j < $fields_cnt; $j++) {
176 if (!isset($row[$j]) || is_null($row[$j])) {
177 $schema_insert .= $GLOBALS[$what . '_null'];
178 } elseif ($row[$j] == '0' || $row[$j] != '') {
179 // loic1 : always enclose fields
180 if ($what == 'excel') {
181 $row[$j] = ereg_replace("\015(\012)?", "\012", $row[$j]);
183 if ($csv_enclosed == '') {
184 $schema_insert .= $row[$j];
185 } else {
186 // also double the escape string if found in the data
187 if ('csv' == $what) {
188 $schema_insert .= $csv_enclosed
189 . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, str_replace($csv_escaped, $csv_escaped . $csv_escaped, $row[$j]))
190 . $csv_enclosed;
191 } else {
192 // for excel, avoid a problem when a field contains
193 // double quotes
194 $schema_insert .= $csv_enclosed
195 . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, $row[$j])
196 . $csv_enclosed;
199 } else {
200 $schema_insert .= '';
202 if ($j < $fields_cnt-1) {
203 $schema_insert .= $csv_separator;
205 } // end for
207 if (!PMA_exportOutputHandler($schema_insert . $csv_terminated)) {
208 return FALSE;
210 } // end while
211 PMA_DBI_free_result($result);
213 return TRUE;
214 } // end of the 'PMA_getTableCsv()' function