Automatically generated installer lang files
[moodle.git] / lib / adodb / toexport.inc.php
blob66bbf5424058906ac080984436114adde3b1fa08
1 <?php
2 /**
3 * Export recordsets in several formats.
5 * AS VARIABLE
6 * $s = rs2csv($rs); # comma-separated values
7 * $s = rs2tab($rs); # tab delimited
9 * TO A FILE
10 * $f = fopen($path,'w');
11 * rs2csvfile($rs,$f);
12 * fclose($f);
14 * TO STDOUT
15 * rs2csvout($rs);
17 * This file is part of ADOdb, a Database Abstraction Layer library for PHP.
19 * @package ADOdb
20 * @link https://adodb.org Project's web site and documentation
21 * @link https://github.com/ADOdb/ADOdb Source code and issue tracker
23 * The ADOdb Library is dual-licensed, released under both the BSD 3-Clause
24 * and the GNU Lesser General Public Licence (LGPL) v2.1 or, at your option,
25 * any later version. This means you can use it in proprietary products.
26 * See the LICENSE.md file distributed with this source code for details.
27 * @license BSD-3-Clause
28 * @license LGPL-2.1-or-later
30 * @copyright 2000-2013 John Lim
31 * @copyright 2014 Damien Regad, Mark Newnham and the ADOdb community
34 // returns a recordset as a csv string
35 function rs2csv(&$rs,$addtitles=true)
37 return _adodb_export($rs,',',',',false,$addtitles);
40 // writes recordset to csv file
41 function rs2csvfile(&$rs,$fp,$addtitles=true)
43 _adodb_export($rs,',',',',$fp,$addtitles);
46 // write recordset as csv string to stdout
47 function rs2csvout(&$rs,$addtitles=true)
49 $fp = fopen('php://stdout','wb');
50 _adodb_export($rs,',',',',true,$addtitles);
51 fclose($fp);
54 function rs2tab(&$rs,$addtitles=true)
56 return _adodb_export($rs,"\t",',',false,$addtitles);
59 // to file pointer
60 function rs2tabfile(&$rs,$fp,$addtitles=true)
62 _adodb_export($rs,"\t",',',$fp,$addtitles);
65 // to stdout
66 function rs2tabout(&$rs,$addtitles=true)
68 $fp = fopen('php://stdout','wb');
69 _adodb_export($rs,"\t",' ',true,$addtitles);
70 if ($fp) fclose($fp);
73 function _adodb_export(&$rs,$sep,$sepreplace,$fp=false,$addtitles=true,$quote = '"',$escquote = '"',$replaceNewLine = ' ')
75 if (!$rs) return '';
76 //----------
77 // CONSTANTS
78 $NEWLINE = "\r\n";
79 $BUFLINES = 100;
80 $escquotequote = $escquote.$quote;
81 $s = '';
83 if ($addtitles) {
84 $fieldTypes = $rs->FieldTypesArray();
85 reset($fieldTypes);
86 $i = 0;
87 $elements = array();
88 foreach ($fieldTypes as $o) {
90 $v = ($o) ? $o->name : 'Field'.($i++);
91 if ($escquote) $v = str_replace($quote,$escquotequote,$v);
92 $v = strip_tags(str_replace("\n", $replaceNewLine, str_replace("\r\n",$replaceNewLine,str_replace($sep,$sepreplace,$v))));
93 $elements[] = $v;
96 $s .= implode($sep, $elements).$NEWLINE;
98 $hasNumIndex = isset($rs->fields[0]);
100 $line = 0;
101 $max = $rs->FieldCount();
103 while (!$rs->EOF) {
104 $elements = array();
105 $i = 0;
107 if ($hasNumIndex) {
108 for ($j=0; $j < $max; $j++) {
109 $v = $rs->fields[$j];
110 if (!is_object($v)) $v = trim($v);
111 else $v = 'Object';
112 if ($escquote) $v = str_replace($quote,$escquotequote,$v);
113 $v = strip_tags(str_replace("\n", $replaceNewLine, str_replace("\r\n",$replaceNewLine,str_replace($sep,$sepreplace,$v))));
115 if (strpos($v,$sep) !== false || strpos($v,$quote) !== false) $elements[] = "$quote$v$quote";
116 else $elements[] = $v;
118 } else { // ASSOCIATIVE ARRAY
119 foreach($rs->fields as $v) {
120 if ($escquote) $v = str_replace($quote,$escquotequote,trim($v));
121 $v = strip_tags(str_replace("\n", $replaceNewLine, str_replace("\r\n",$replaceNewLine,str_replace($sep,$sepreplace,$v))));
123 if (strpos($v,$sep) !== false || strpos($v,$quote) !== false) $elements[] = "$quote$v$quote";
124 else $elements[] = $v;
127 $s .= implode($sep, $elements).$NEWLINE;
128 $rs->MoveNext();
129 $line += 1;
130 if ($fp && ($line % $BUFLINES) == 0) {
131 if ($fp === true) echo $s;
132 else fwrite($fp,$s);
133 $s = '';
137 if ($fp) {
138 if ($fp === true) echo $s;
139 else fwrite($fp,$s);
140 $s = '';
143 return $s;