[ucsim-s51] Fox of [bugs:#3620] ucsim s51 push sp saves wrong value
[sdcc.git] / sdcc-web / download_regtests_db.php
blob5352baaaeb8bee2ccdad129c1177178632333b7d
1 <?php
2 /*-------------------------------------------------------------------------
3 download_regtest_db.php - download regression tests database
4 in CSV format
6 Copyright (C) 2011, Borut Razem <borut.razem AT gmail.com>
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2.1, or (at your option) any
11 later version.
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this library; see the file COPYING. If not, write to the
20 Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
21 MA 02110-1301, USA.
22 -------------------------------------------------------------------------*/
24 class Out {
25 protected $fileExt;
26 protected $view;
27 protected $encoding;
29 function gen_http_header()
33 function prolog()
37 function epilog()
41 function gen_head($row)
45 function gen_row($row)
49 function close()
54 class Csv extends Out {
55 protected $outstream;
57 function __construct($view, $encoding = 'windows-1250')
59 $this->view = $view;
60 $this->encoding = $encoding;
61 $this->fileExt = '.csv';
62 $this->outstream = fopen('php://output', 'w');
65 function gen_http_header()
67 header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
68 header('Expires: Sun, 22 May 2011 00:00:00 GMT');
69 header('Content-Type: application/ms-excel; charset=' . $this->encoding);
70 header('Content-Disposition: attachment; filename="' . $this->view . $this->fileExt . '"');
73 function gen_head($row)
75 $this->gen_row($row);
78 protected function recode_elem($elem)
80 if ($this->encoding != 'utf-8') {
81 return iconv('utf-8', $this->encoding . '//IGNORE', $elem);
83 else {
84 return $elem;
88 function gen_row($row)
90 $row1 = array_map(array($this, 'recode_elem'), $row);
91 return fputcsv($this->outstream, $row1, ';');
94 function close()
96 fclose($this->outstream);
100 class Bz2Csv extends Csv {
101 protected $bz2stream;
102 protected $tempfname;
104 function __construct($view, $encoding = 'windows-1250')
106 $this->view = $view;
107 $this->encoding = $encoding;
108 $this->fileExt = '.csv.bz2';
109 $this->tempfname = tempnam(sys_get_temp_dir(), 'sdcc');
110 $this->bz2stream = bzopen($this->tempfname, 'w');
111 $this->outstream = fopen('php://memory', 'rw');
114 function gen_http_header()
116 header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
117 header('Expires: Sun, 22 May 2011 00:00:00 GMT');
118 header('Content-Type: application/octet-stream');
119 header('Content-Disposition: attachment; filename="' . $this->view . $this->fileExt . '"');
120 header('Content-Transfer-Encoding: binary');
121 ob_flush();
124 function gen_row ($row)
126 rewind($this->outstream); // rewind the memory stream
127 $len = parent::gen_row($row);
128 rewind($this->outstream); // rewind the memory stream
129 $csvstr = stream_get_contents($this->outstream); // read csv from the memory stream
130 $ret = bzwrite($this->bz2stream, $csvstr, $len); // write csv from the bzip2 stream
133 function close()
135 fclose($this->outstream);
136 bzclose($this->bz2stream);
137 readfile($this->tempfname);
138 unlink($this->tempfname);
142 $where = htmlspecialchars(trim($_REQUEST['where']));
143 $compress = htmlspecialchars(trim($_REQUEST['compress']));
145 $ini = parse_ini_file('sdcc.ini');
146 $regTestDb = new mysqli($ini['host'], $ini['username'], $ini['passwd'], $ini['dbname']);
148 $query = 'SELECT * FROM regtest_results';
149 if ($where) {
150 $query .= ' WHERE ' . $where;
153 $result = $regTestDb->query($query);
154 if ($result) {
155 if ($compress == 'bz2') {
156 $out = new Bz2Csv('regression_test_results');
159 else {
160 $out = new Csv('regression_test_results');
163 $out->gen_http_header();
165 $out->prolog($view);
167 $out->gen_head(array('platform', 'target', 'build_number', 'date', 'regtest_name', 'failures', 'tests', 'cases', 'bytes', 'ticks', 'time_stamp'));
169 while ($row = $result->fetch_assoc()) {
170 $out->gen_row($row);
173 $out->epilog();
174 $out->close();
175 $result->free();
177 else {
178 echo('DB Error: could not execute the database quey = ' . $query . "\n");
179 echo('MySQL Error: ' . $regTestDb->error . "\n");
182 $regTestDb->close();