-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBugzillaQuery.class.php
205 lines (156 loc) · 6.25 KB
/
BugzillaQuery.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
<?php
require_once dirname(__FILE__) . '/BugzillaJob.class.php';
require_once 'HTTP/Request2.php';
// Factory class
class BugzillaQuery {
public function create($type, $options, $title) {
global $wgBugzillaMethod;
if( strtolower($wgBugzillaMethod) == 'xml-rpc' ) {
return new BugzillaXMLRPCQuery($type, $options, $title);
}elseif( strtolower($wgBugzillaMethod) == 'json-rpc' ) {
return new BugzillaJSONRPCQuery($type, $options, $title);
}else {
return new BugzillaRESTQuery($type, $options, $title);
}
}
}
// Base class
abstract class BugzillaBaseQuery {
public function __construct($type, $options, $title) {
$this->type = $type;
$this->title = $title;
$this->url = FALSE;
$this->id = FALSE;
$this->fetched_at = FALSE;
$this->error = FALSE;
$this->data = array();
$this->_set_options($options);
}
public function id() {
// If we have already generated an id, return it
if( $this->id ) { return $this->id; }
return $this->_generate_id();
}
protected function _generate_id() {
// No need to generate if there are errors
if( !empty($this->error) ) { return; }
// FIXME: Should we strtolower() the keys?
// Sort it so the keys are always in the same order
ksort($this->options);
// Get a string representation of the array
$id_string = serialize($this->options);
// Hash it
$this->id = sha1($id_string);
return $this->id;
}
// Connect and fetch the data
public function fetch() {
global $wgBugzillaCacheMins;
// We need *some* options to do anything
if( !isset($this->options) || empty($this->options) ) { return; }
// Don't do anything if we already had an error
if( $this->error ) { return; }
// Connect to the database. Because we are reading and we can deal with
// a bit of stale data we can just look at the slave
$dbr = wfGetDB( DB_SLAVE );
// See if we have a cache entry
// TODO: Make this configuratble between count(*) and what is here now
$res = $dbr->select(
'bugzilla_cache',
array('id', 'fetched_at','data'),
'id = "' . $this->id() . '"',
__METHOD__,
array( 'ORDER BY' => 'fetched_at DESC',
'LIMIT' => 1)
);
// If the cache entry is older than this we need to invalidate it
$expiry = strtotime("-$wgBugzillaCacheMins minutes");
// Check if there was an entry in the cache
if( $res->numRows() ) {
$row = $res->fetchRow();
}else{
$row = FALSE;
}
if( !$row ) {
// No cache entry
$this->cached = FALSE;
$params = array( 'query_obj' => serialize($this) );
// Does the Bugzilla query in the background and updates the cache
$job = new BugzillaInsertJob( $this->title, $params );
$job->insert();
}elseif( $expiry > wfTimestamp(TS_UNIX, $row['fetched_at']) ) {
// Cache entry is too old
$this->cached = FALSE;
$params = array( 'query_obj' => serialize($this) );
// Does the Bugzilla query in the background and updates the cache
$job = new BugzillaUpdateJob( $this->title, $params );
$job->insert();
}else {
// Cache is good, use it
$this->id = $row['id'];
$this->fetched_at = wfTimestamp(TS_DB, $row['fetched_at']);
$this->data = unserialize($row['data']);
$this->cached = TRUE;
}
}
protected function _set_options($query_options_raw) {
// Make sure query options are valid JSON
$this->options = json_decode($query_options_raw);
if( !$query_options_raw || !$this->options ) {
$this->error = 'Query options must be valid json';
}
// Object is kinda userless, make it an array
$this->options = get_object_vars($this->options);
}
}
class BugzillaRESTQuery extends BugzillaBaseQuery {
function __construct($type, $options, $title='') {
global $wgBugzillaRESTURL;
parent::__construct($type, $options, $title);
// See what sort of REST query we are going to
switch( $type ) {
// Whitelist
case 'bug':
case 'count':
$this->url = $wgBugzillaRESTURL . '/' . urlencode($type);
break;
// Default to a bug query
default:
$this->url = $wgBugzillaRESTURL . '/bug';
}
$this->fetch();
}
// Load data from the Bugzilla REST API
public function _fetch_by_options() {
// Set up our HTTP request
$request = new HTTP_Request2($this->url,
HTTP_Request2::METHOD_GET,
array('follow_redirects' => TRUE,
// TODO: Not sure if I should do this
'ssl_verify_peer' => FALSE));
// The REST API requires these
$request->setHeader('Accept', 'application/json');
$request->setHeader('Content-Type', 'application/json');
// Add in the requested query options
$url = $request->getUrl();
$url->setQueryVariables($this->options);
// This is basically straight from the HTTP/Request2 docs
try {
$response = $request->send();
if (200 == $response->getStatus()) {
$this->data = json_decode($response->getBody());
} else {
$this->error = 'Server returned unexpected HTTP status: ' .
$response->getStatus() . ' ' .
$response->getReasonPhrase();
return;
}
} catch (HTTP_Request2_Exception $e) {
$this->error = $e->getMessage();
return;
}
// Now that we have the data, process it
// $this->_process_data();
}
}
?>