forked from mozilla/mediawiki-bugzilla
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBugzillaOutput.class.php
254 lines (192 loc) · 7.64 KB
/
BugzillaOutput.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
<?php
abstract class BugzillaOutput {
public $response;
public function __construct($config, $options, $title='') {
$this->title = $title;
$this->config = $config;
$this->error = false;
$this->response = new stdClass();
$this->query = BugzillaQuery::create($config['type'], $options, $title);
}
protected function _render_error($error) {
$this->template = dirname(__FILE__) . '/templates/error.tpl';
ob_start(); // Start output buffering.
require($this->template);
return ob_get_clean();
}
public function fetch() {
$this->query->fetch();
}
public function render() {
if( $this->query->error ) {
return $this->_render_error($this->query->error);
}
// Get our template path
$this->template = dirname(__FILE__) . '/templates/' .
$this->config['type'] . '/' .
$this->config['display'] . '.tpl';
// Make sure a template is there
if( !file_exists($this->template) ) {
$this->error = 'Invalid type ' .
'(' . htmlspecialchars($this->config['type']) . ')' .
' and display ' .
'(' . htmlspecialchars($this->config['display']) . ')' .
' combination';
}
if( $this->error ) {
return $this->_render_error($this->error);
}
$this->setup_template_data();
$response = $this->response;
ob_start(); // Start output buffering.
require($this->template);
$results = ob_get_clean();
return $results;
}
abstract protected function setup_template_data();
}
class BugzillaNumber extends BugzillaOutput {
function setup_template_data() {
}
function _render_error($error) {
return '<span style="color: red;">Bugzilla: '.htmlspecialchars($error).'</span>';
}
function render() {
return '<span>'.count($this->query->data['bugs']).'</span>';
}
}
class BugzillaBugListing extends BugzillaOutput {
protected function setup_template_data() {
global $wgBugzillaDefaultFields;
$this->response->bugs = array();
$this->response->fields = array();
$this->response->full_query_url = $this->query->full_query_url();
// Set the bug data for the templates
if(isset($this->query->data['bugs']) && count($this->query->data['bugs']) > 0) {
$this->response->bugs = $this->query->data['bugs'];
}
$this->response->fields = $this->query->options['include_fields'];
}
}
class BugzillaList extends BugzillaBugListing {
}
class BugzillaTable extends BugzillaBugListing {
}
/* Graphing */
abstract class BugzillaGraph extends BugzillaOutput {
public function __construct($config, $options, $title = '') {
parent::__construct($config, $options, $title);
if (!extension_loaded('gd') && !extension_loaded('gd2')) {
$this->error = 'GD extension must be loaded.';
}
}
protected function _get_size() {
if ( isset( $this->config['size'] ) ) {
switch($this->config['size']) {
// whitelist
case 'small':
case 'medium':
case 'large':
return $this->config['size'];
}
}
return 'large';
}
public function setup_template_data() {
include_once 'pchart/class/pDraw.class.php';
include_once 'pchart/class/pImage.class.php';
include_once 'pchart/class/pData.class.php';
global $wgBugzillaChartUrl;
global $wgBugzillaCacheTimeOut;
global $wgMainCacheType;
$fileName = sha1(serialize([$this->query->id, $this->_get_size(), get_class($this)]));
$key = implode(':', ['mediawiki', 'bugzilla', 'chart', $fileName]);
$cache = wfGetCache($wgMainCacheType);
// We use the cache only to invalidate/recompute the charts:
// the key is its own value. Only the TTL is useful here.
if ($cache->get($key) === false) {
if ($this->generate_chart($fileName)) {
$cache->set($key, $fileName);
}
}
$this->response->image = $wgBugzillaChartUrl.'/'.$fileName.'.png';
}
}
class BugzillaPieGraph extends BugzillaGraph {
public function generate_chart($chart_name)
{
include_once "pchart/class/pPie.class.php";
global $wgBugzillaChartStorage;
global $wgBugzillaFontStorage;
// TODO: Make all this size stuff trivial for other
// graph types to plug into
switch($this->_get_size()) {
case 'small':
$imgX = 200;
$imgY = 65;
$radius = 30;
$font = 6;
break;
case 'medium':
$imgX = 400;
$imgY = 125;
$radius = 60;
$font = 7;
break;
case 'large':
default:
$imgX = 500;
$imgY = 245;
$radius = 120;
$font = 9;
}
$padding = 5;
$startX = ( isset($startX) ) ? $startX : $radius;
$startY = ( isset($startY) ) ? $startY : $radius;
$pData = new pData();
$pData->addPoints($this->query->data['data'], 'Counts');
$pData->setAxisName(0, 'Bugs');
$pData->addPoints($this->query->data['x_labels'], "Bugs");
$pData->setSerieDescription("Bugs", "Bugs");
$pData->setAbscissa("Bugs");
$pImage = new pImage($imgX, $imgY, $pData);
$pImage->setFontProperties(array('FontName' => $wgBugzillaFontStorage . '/verdana.ttf', 'FontSize' => $font));
$pPieChart = new pPie($pImage, $pData);
$pPieChart->draw2DPie($startX,
$startY,
array(
"Radius" => $radius,
"ValuePosition" => PIE_VALUE_INSIDE,
"WriteValues"=>PIE_VALUE_NATURAL,
"DrawLabels"=>FALSE,
"LabelStacked"=>TRUE,
"ValueR" => 0,
"ValueG" => 0,
"ValueB" => 0,
"Border"=>TRUE));
// Legend
$pImage->setShadow(FALSE);
$pPieChart->drawPieLegend(2*$radius + 2*$padding, $padding, array("Alpha"=>20));
$pImage->render($wgBugzillaChartStorage . '/' . $chart_name . '.png');
return $chart_name;
}
}
class BugzillaBarGraph extends BugzillaGraph {
public function generate_chart($chart_name)
{
global $wgBugzillaChartStorage, $wgBugzillaFontStorage;
$pData = new pData();
$pData->addPoints($this->query->data['data'], 'Counts');
$pData->setAxisName(0, 'Bugs');
$pData->addPoints($this->query->data['x_labels'], "Bugs");
$pData->setSerieDescription("Bugs", "Bugs");
$pData->setAbscissa("Bugs");
$pImage = new pImage(600,300, $pData);
$pImage->setFontProperties(array('FontName' => $wgBugzillaFontStorage . '/verdana.ttf', 'FontSize' => 6));
$pImage->setGraphArea(75, 30, 580, 280);
$pImage->drawScale(array("CycleBackground"=>TRUE,'Factors'=>array(1),"DrawSubTicks"=>FALSE,"GridR"=>0,"GridG"=>0,"GridB"=>0,"GridAlpha"=>10, "Pos"=>SCALE_POS_TOPBOTTOM));
$pImage->drawBarChart();
$pImage->render($wgBugzillaChartStorage . '/' . $chart_name . '.png');
return $chart_name;
}
}