/
bug_revision_api.php
354 lines (299 loc) · 9.87 KB
/
bug_revision_api.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
<?php
# Mantis - a php based bugtracking system
# Mantis is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# Mantis is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Mantis. If not, see <http://www.gnu.org/licenses/>.
/**
* This API allows bugs to keep a read-only history of changes to longtext boxes.
*
* @package CoreAPI
* @subpackage BugRevisionAPI
* @copyright Copyright (C) 2002 - 2012 Mantis Team - mantisbt-dev@lists.sourceforge.net
* @link http://www.mantisbt.org
*/
/**
* Add a new revision to a bug history.
* @param int $p_bug_id Bug ID
* @param int $p_user_id User ID
* @param int $p_type Revision Type
* @param string $p_value Value
* @param int $p_bugnote_id Bugnote ID
* @param int $p_timestamp Timestamp(int)
* @return int Revision ID
*/
function bug_revision_add( $p_bug_id, $p_user_id, $p_type, $p_value, $p_bugnote_id=0, $p_timestamp = null ) {
if ( $p_type <= REV_ANY ) {
return null;
}
$t_bug_rev_table = db_get_table( 'mantis_bug_revision_table' );
$t_last = bug_revision_last( $p_bug_id, $p_type );
# Don't save a revision twice if nothing has changed
if ( !is_null( $t_last ) &&
$p_value == $t_last['value'] ) {
return $t_last['id'];
}
if ( $p_timestamp === null ) {
$t_timestamp = db_now();
} else {
$t_timestamp = $p_timestamp;
}
$t_query = "INSERT INTO $t_bug_rev_table (
bug_id,
bugnote_id,
user_id,
timestamp,
type,
value
) VALUES ( " .
db_param() . ', ' .
db_param() . ', ' .
db_param() . ', ' .
db_param() . ', ' .
db_param() . ', ' .
db_param() .
' )';
db_query_bound( $t_query, array(
$p_bug_id,
$p_bugnote_id,
$p_user_id,
$t_timestamp,
$p_type,
$p_value
) );
return db_insert_id( $t_bug_rev_table );
}
/**
* Check if a bug revision exists
* @param int $p_revision_id Revision ID
* @return bool Whether or not the bug revision exists
*/
function bug_revision_exists( $p_revision_id ) {
$t_bug_rev_table = db_get_table( 'mantis_bug_revision_table' );
$t_query = "SELECT * FROM $t_bug_rev_table WHERE id=" . db_param();
$t_result = db_query_bound( $t_query, array( $p_revision_id ) );
if ( db_num_rows( $t_result ) < 1 ) {
return false;
}
return true;
}
/**
* Get a row of data for a given revision ID.
* @param int $p_revision_id Revision ID
* @return array Revision data row
*/
function bug_revision_get( $p_revision_id ) {
$t_bug_rev_table = db_get_table( 'mantis_bug_revision_table' );
$t_query = "SELECT * FROM $t_bug_rev_table WHERE id=" . db_param();
$t_result = db_query_bound( $t_query, array( $p_revision_id ) );
if ( db_num_rows( $t_result ) < 1 ) {
trigger_error( ERROR_BUG_REVISION_NOT_FOUND, ERROR );
}
return db_fetch_array( $t_result );
}
/**
* Get the name of the type of a bug revision.
* @param int $p_revision_id Revision type ID (see constant_inc.php for possible values)
* @return string Name of the type of the bug revision
*/
function bug_revision_get_type_name( $p_revision_type_id ) {
$t_type_name = '';
switch( $p_revision_type_id ) {
case REV_DESCRIPTION:
$t_type_name = lang_get( 'description' );
break;
case REV_STEPS_TO_REPRODUCE:
$t_type_name = lang_get( 'steps_to_reproduce' );
break;
case REV_ADDITIONAL_INFO:
$t_type_name = lang_get( 'additional_information' );
break;
case REV_BUGNOTE:
$t_type_name = lang_get( 'bugnote' );
break;
}
return $t_type_name;
}
/**
* Remove one or more bug revisions from the bug history.
* @param int $p_revision_id Revision ID, or array of revision IDs
* @return null
*/
function bug_revision_drop( $p_revision_id ) {
$t_bug_rev_table = db_get_table( 'mantis_bug_revision_table' );
if ( is_array( $p_revision_id ) ) {
$t_revisions = array();
$t_first = true;
$t_query = "DELETE FROM $t_bug_rev_table WHERE id IN ( ";
# TODO: Fetch bug revisions in one query (and cache them)
foreach( $p_revision_id as $t_rev_id ) {
$t_query .= ( $t_first ? db_param() : ', ' . db_param() );
$t_revisions[$t_rev_id] = bug_revision_get( $t_rev_id );
}
$t_query .= ' )';
db_query_bound( $t_query, $p_revision_id );
foreach( $p_revision_id as $t_rev_id ) {
if ( $t_revisions[$t_rev_id]['type'] == REV_BUGNOTE ) {
history_log_event_special( $t_revisions[$t_rev_id]['bug_id'], BUGNOTE_REVISION_DROPPED, bugnote_format_id( $t_rev_id ), $t_revisions[$t_rev_id]['bugnote_id'] );
} else {
history_log_event_special( $t_revisions[$t_rev_id]['bug_id'], BUG_REVISION_DROPPED, bugnote_format_id( $t_rev_id ), $t_revisions[$t_rev_id]['type'] );
}
}
} else {
$t_revision = bug_revision_get( $p_revision_id );
$t_query = "DELETE FROM $t_bug_rev_table WHERE id=" . db_param();
db_query_bound( $t_query, array( $p_revision_id ) );
if ( $t_revision['type'] == REV_BUGNOTE ) {
history_log_event_special( $t_revision['bug_id'], BUGNOTE_REVISION_DROPPED, bugnote_format_id( $p_revision_id ), $t_revision['bugnote_id'] );
} else {
history_log_event_special( $t_revision['bug_id'], BUG_REVISION_DROPPED, bugnote_format_id( $p_revision_id ), $t_revision['type'] );
}
}
}
/**
* Retrieve a count of revisions to the bug's information.
* @param int $p_bug_id Bug ID
* @param int $p_type Revision Type (optional)
* @param int $p_bugnote_id Bugnote ID (optional)
* @return array|null Array of Revision rows
*/
function bug_revision_count( $p_bug_id, $p_type=REV_ANY, $p_bugnote_id=0 ) {
$t_bug_rev_table = db_get_table( 'mantis_bug_revision_table' );
$t_params = array( $p_bug_id );
$t_query = "SELECT COUNT(id) FROM $t_bug_rev_table
WHERE bug_id=" . db_param();
if ( REV_ANY < $p_type ) {
$t_query .= ' AND type=' . db_param();
$t_params[] = $p_type;
}
if ( $p_bugnote_id > 0 ) {
$t_query .= ' AND bugnote_id=' . db_param();
$t_params[] = $p_bugnote_id;
} else {
$t_query .= ' AND bugnote_id=0';
}
$t_result = db_query_bound( $t_query, $t_params );
return db_result( $t_result );
}
/**
* Delete all revision history for a bug.
* @param int $p_bug_id Bug ID
* @param int $p_bugnote_id Bugnote ID (optional)
* @return null
*/
function bug_revision_delete( $p_bug_id, $p_bugnote_id=0 ) {
$t_bug_rev_table = db_get_table( 'mantis_bug_revision_table' );
if ( $p_bugnote_id < 1 ) {
$t_query = "DELETE FROM $t_bug_rev_table WHERE bug_id=" . db_param();
db_query_bound( $t_query, array( $p_bug_id ) );
} else {
$t_query = "DELETE FROM $t_bug_rev_table WHERE bugnote_id=" . db_param();
db_query_bound( $t_query, array( $p_bugnote_id ) );
}
}
/**
* Retrieve the last change to the bug's information.
* @param int $p_bug_id Bug ID
* @param int $p_type Revision Type (optional)
* @param int $p_bugnote_id Bugnote ID (optional)
* @return null|array Revision row
*/
function bug_revision_last( $p_bug_id, $p_type=REV_ANY, $p_bugnote_id=0 ) {
$t_bug_rev_table = db_get_table( 'mantis_bug_revision_table' );
$t_params = array( $p_bug_id );
$t_query = "SELECT * FROM $t_bug_rev_table
WHERE bug_id=" . db_param();
if ( REV_ANY < $p_type ) {
$t_query .= ' AND type=' . db_param();
$t_params[] = $p_type;
}
if ( $p_bugnote_id > 0 ) {
$t_query .= ' AND bugnote_id=' . db_param();
$t_params[] = $p_bugnote_id;
} else {
$t_query .= ' AND bugnote_id=0';
}
$t_query .= ' ORDER BY timestamp DESC';
$t_result = db_query_bound( $t_query, $t_params, 1 );
if ( db_num_rows( $t_result ) > 0 ) {
return db_fetch_array( $t_result );
} else {
return null;
}
}
/**
* Retrieve a full list of changes to the bug's information.
* @param int $p_bug_id Bug ID
* @param int $p_type Revision Type
* @param int $p_bugnote_id Bugnote ID
* @return array/null Array of Revision rows
*/
function bug_revision_list( $p_bug_id, $p_type=REV_ANY, $p_bugnote_id=0 ) {
$t_bug_rev_table = db_get_table( 'mantis_bug_revision_table' );
$t_params = array( $p_bug_id );
$t_query = "SELECT * FROM $t_bug_rev_table
WHERE bug_id=" . db_param();
if ( REV_ANY < $p_type ) {
$t_query .= ' AND type=' . db_param();
$t_params[] = $p_type;
}
if ( $p_bugnote_id > 0 ) {
$t_query .= ' AND bugnote_id=' . db_param();
$t_params[] = $p_bugnote_id;
} else {
$t_query .= ' AND bugnote_id=0';
}
$t_query .= ' ORDER BY timestamp ASC';
$t_result = db_query_bound( $t_query, $t_params );
$t_revisions = array();
while( $t_row = db_fetch_array( $t_result ) ) {
$t_revisions[$t_row['id']] = $t_row;
}
return $t_revisions;
}
/**
* Retrieve a list of changes to a bug of the same type as the
* given revision ID.
* @param int $p_rev_id Revision ID
* @return array|null Array of Revision rows
*/
function bug_revision_like( $p_rev_id ) {
$t_bug_rev_table = db_get_table( 'mantis_bug_revision_table' );
$t_query = "SELECT bug_id, bugnote_id, type FROM $t_bug_rev_table WHERE id=" . db_param();
$t_result = db_query_bound( $t_query, array( $p_rev_id ) );
if ( db_num_rows( $t_result ) < 1 ) {
trigger_error( ERROR_BUG_REVISION_NOT_FOUND, ERROR );
}
$t_row = db_fetch_array( $t_result );
$t_bug_id = $t_row['bug_id'];
$t_bugnote_id = $t_row['bugnote_id'];
$t_type = $t_row['type'];
$t_params = array( $t_bug_id );
$t_query = "SELECT * FROM $t_bug_rev_table
WHERE bug_id=" . db_param();
if ( REV_ANY < $t_type ) {
$t_query .= ' AND type=' . db_param();
$t_params[] = $t_type;
}
if ( $t_bugnote_id > 0 ) {
$t_query .= ' AND bugnote_id=' . db_param();
$t_params[] = $t_bugnote_id;
} else {
$t_query .= ' AND bugnote_id=0';
}
$t_query .= ' ORDER BY timestamp ASC';
$t_result = db_query_bound( $t_query, $t_params );
$t_revisions = array();
while( $t_row = db_fetch_array( $t_result ) ) {
$t_revisions[$t_row['id']] = $t_row;
}
return $t_revisions;
}