/
version_api.php
652 lines (546 loc) · 19.9 KB
/
version_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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
<?php
# MantisBT - a php based bugtracking system
# Copyright (C) 2000 - 2002 Kenzaburo Ito - kenito@300baud.org
# Copyright (C) 2002 - 2009 MantisBT Team - mantisbt-dev@lists.sourceforge.net
# MantisBT 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.
#
# MantisBT 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 MantisBT. If not, see <http://www.gnu.org/licenses/>.
/**
* @copyright Copyright (C) 2000 - 2002 Kenzaburo Ito - kenito@300baud.org
* @copyright Copyright (C) 2002 - 2009 MantisBT Team - mantisbt-dev@lists.sourceforge.net
* @link http://www.mantisbt.org
* @package CoreAPI
* @subpackage VersionAPI
*/
/**
* Version Data Structure Definition
* @package MantisBT
* @subpackage classes
*/
class VersionData {
protected $id = 0;
protected $project_id = 0;
protected $version = '';
protected $description = '';
protected $released = VERSION_FUTURE;
protected $date_order = 1;
protected $obsolete = 0;
/**
* @param string $name
* @param string $value
* @private
*/
public function __set($name, $value) {
switch ($name) {
case 'date_order':
if( !is_numeric($value) ) {
if( $value == '' ) {
$value = date_get_null();
} else {
$value = strtotime( $value );
if ( $value === false ) {
trigger_error( ERROR_INVALID_DATE_FORMAT, ERROR );
}
}
}
}
$this->$name = $value;
}
/**
* @param string $p_string
* @private
*/
public function __get( $p_name ) {
return $this->{$p_name};
}
}
$g_cache_versions = array();
/**
* Cache a version row if necessary and return the cached copy
* If the second parameter is true (default), trigger an error
* if the version can't be found. If the second parameter is
* false, return false if the version can't be found.
* @param int $p_version_id
* @param bool $p_trigger_errors
* @return array
*/
function version_cache_row( $p_version_id, $p_trigger_errors = true ) {
global $g_cache_versions;
$c_version_id = db_prepare_int( $p_version_id );
$t_project_version_table = db_get_table( 'project_version' );
if( isset( $g_cache_versions[$c_version_id] ) ) {
return $g_cache_versions[$c_version_id];
}
$query = "SELECT *
FROM $t_project_version_table
WHERE id=" . db_param();
$result = db_query_bound( $query, Array( $c_version_id ) );
if( 0 == db_num_rows( $result ) ) {
$g_cache_versions[$c_version_id] = false;
if( $p_trigger_errors ) {
error_parameters( $p_version_id );
trigger_error( ERROR_VERSION_NOT_FOUND, ERROR );
} else {
return false;
}
}
$row = db_fetch_array( $result );
$g_cache_versions[$c_version_id] = $row;
return $row;
}
/**
* Check whether the version exists
* $p_project_id : null will use the current project, otherwise the specified project
* Returns true if the version exists, false otherwise
* @param int $p_version_id
* @return bool
*/
function version_exists( $p_version_id ) {
return version_cache_row( $p_version_id, false ) !== false;
}
/**
* Check whether the version name is unique
* Returns true if the name is unique, false otherwise
* @param string $p_version
* @param int $p_project_id
* @return bool
*/
function version_is_unique( $p_version, $p_project_id = null ) {
return version_get_id( $p_version, $p_project_id ) === false;
}
/**
* Check whether the version exists
* Trigger an error if it does not
* @param int $p_version_id
*/
function version_ensure_exists( $p_version_id ) {
if( !version_exists( $p_version_id ) ) {
error_parameters( $p_version_id );
trigger_error( ERROR_VERSION_NOT_FOUND, ERROR );
}
}
/**
* Check whether the version is unique within a project
* Trigger an error if it is not
* @param string $p_version
* @param int $p_project_id
*/
function version_ensure_unique( $p_version, $p_project_id = null ) {
if( !version_is_unique( $p_version, $p_project_id ) ) {
trigger_error( ERROR_VERSION_DUPLICATE, ERROR );
}
}
/**
* Add a version to the project
* @param int $p_project_id
* @param string $p_version
* @param int $p_released
* @param string $p_description
* @param int $p_date_order
* @param bool $p_obsolete
* @return int
*/
function version_add( $p_project_id, $p_version, $p_released = VERSION_FUTURE, $p_description = '', $p_date_order = null, $p_obsolete = false ) {
$c_project_id = db_prepare_int( $p_project_id );
$c_released = db_prepare_int( $p_released );
$c_obsolete = db_prepare_bool( $p_obsolete );
if( null === $p_date_order ) {
$c_date_order = db_now();
} else {
$c_date_order = $p_date_order;
}
version_ensure_unique( $p_version, $p_project_id );
$t_project_version_table = db_get_table( 'project_version' );
$query = "INSERT INTO $t_project_version_table
( project_id, version, date_order, description, released, obsolete )
VALUES
(" . db_param() . ', ' . db_param() . ', ' . db_param() . ', ' . db_param() . ', ' . db_param() . ', ' . db_param() . ' )';
db_query_bound( $query, Array( $c_project_id, $p_version, $c_date_order, $p_description, $c_released, $c_obsolete ) );
# db_query errors on failure so:
return db_insert_id( $t_project_version_table );
}
/**
* Update the definition of a version
* @param VersionData @p_version_info
* @return true
*/
function version_update( $p_version_info ) {
version_ensure_exists( $p_version_info->id );
$t_old_version_name = version_get_field( $p_version_info->id, 'version' );
# check for duplicates
if(( utf8_strtolower( $t_old_version_name ) != utf8_strtolower( $p_version_info->version ) ) && !version_is_unique( $p_version_info->version, $p_version_info->project_id ) ) {
trigger_error( ERROR_VERSION_DUPLICATE, ERROR );
}
$c_version_id = db_prepare_int( $p_version_info->id );
$c_version_name = $p_version_info->version;
$c_old_version_name = $t_old_version_name;
$c_description = $p_version_info->description;
$c_released = db_prepare_int( $p_version_info->released );
$c_obsolete = db_prepare_bool( $p_version_info->obsolete );
$c_date_order = $p_version_info->date_order;
$c_project_id = db_prepare_int( $p_version_info->project_id );
$t_project_version_table = db_get_table( 'project_version' );
$t_bug_table = db_get_table( 'bug' );
$query = "UPDATE $t_project_version_table
SET version=" . db_param() . ",
description=" . db_param() . ",
released=" . db_param() . ",
date_order=" . db_param() . ",
obsolete=" . db_param() . "
WHERE id=" . db_param();
db_query_bound( $query, Array( $c_version_name, $c_description, $c_released, $c_date_order, $c_obsolete, $c_version_id ) );
if( $c_version_name != $c_old_version_name ) {
$query = 'UPDATE ' . $t_bug_table . ' SET version=' . db_param() .
' WHERE ( project_id=' . db_param() . ') AND ( version=' . db_param() . ')';
db_query_bound( $query, Array( $c_version_name, $c_project_id, $c_old_version_name ) );
$query = "UPDATE $t_bug_table
SET fixed_in_version=" . db_param() . '
WHERE ( project_id=' . db_param() . ') AND ( fixed_in_version=' . db_param() . ')';
db_query_bound( $query, Array( $c_version_name, $c_project_id, $c_old_version_name ) );
$query = "UPDATE $t_bug_table
SET target_version=" . db_param() . '
WHERE ( project_id=' . db_param() . ') AND ( target_version=' . db_param() . ')';
db_query_bound( $query, Array( $c_version_name, $c_project_id, $c_old_version_name ) );
/**
* @todo We should consider using ids instead of names for foreign keys. The main advantage of using the names are:
* - for history the version history entries will still be valid even if the version is deleted in the future. -- we can ban deleting referenced versions.
* - when an issue is copied or moved from one project to another, we can keep the last version with the issue even if it doesn't exist in the new project. Also previous history entries remain valid.
* @todo We should update the history for version, fixed_in_version, and target_version.
* @todo We probably need to update the saved filters too?
*/
}
// db_query errors on failure so:
return true;
}
/**
* Remove a version from the project
* @param int $p_version_id
* @param string $p_new_version
* @return true
*/
function version_remove( $p_version_id, $p_new_version = '' ) {
$c_version_id = db_prepare_int( $p_version_id );
version_ensure_exists( $p_version_id );
$t_old_version = version_get_field( $p_version_id, 'version' );
$t_project_id = version_get_field( $p_version_id, 'project_id' );
$c_project_id = db_prepare_int( $t_project_id );
$t_project_version_table = db_get_table( 'project_version' );
$t_bug_table = db_get_table( 'bug' );
$query = "DELETE FROM $t_project_version_table
WHERE id=" . db_param();
db_query_bound( $query, Array( $c_version_id ) );
$query = "UPDATE $t_bug_table
SET version=" . db_param() . "
WHERE project_id=" . db_param() . " AND version=" . db_param();
db_query_bound( $query, Array( $p_new_version, $c_project_id, $t_old_version ) );
$query = "UPDATE $t_bug_table
SET fixed_in_version=" . db_param() . '
WHERE ( project_id=' . db_param() . ' ) AND ( fixed_in_version=' . db_param() . ')';
db_query_bound( $query, Array( $p_new_version, $c_project_id, $t_old_version ) );
$query = "UPDATE $t_bug_table
SET target_version=" . db_param() . '
WHERE ( project_id=' . db_param() . ' ) AND ( target_version=' . db_param() . ')';
db_query_bound( $query, array( $p_new_version, $c_project_id, $t_old_version ) );
# db_query errors on failure so:
return true;
}
/**
* Remove all versions associated with a project
* @param int $p_project_id
* @return true
*/
function version_remove_all( $p_project_id ) {
$c_project_id = db_prepare_int( $p_project_id );
$t_project_version_table = db_get_table( 'project_version' );
$t_bug_table = db_get_table( 'bug' );
# remove all references to versions from verison, fixed in version and target version.
$query = "UPDATE $t_bug_table
SET version='', fixed_in_version='', target_version=''
WHERE project_id=" . db_param();
db_query_bound( $query, array( $c_project_id ) );
# remove the actual versions associated with the project.
$query = "DELETE FROM $t_project_version_table
WHERE project_id=" . db_param();
db_query_bound( $query, array( $c_project_id ) );
# db_query errors on failure so:
return true;
}
$g_cache_versions_project = null;
/**
* Cache version information for an array of project id's
* @param array $p_project_id_array
* @return null
*/
function version_cache_array_rows( $p_project_id_array ) {
global $g_cache_versions, $g_cache_versions_project;
$c_project_id_array = array();
foreach( $p_project_id_array as $t_project_id ) {
if( !isset( $g_cache_versions_project[(int) $t_project_id] ) ) {
$c_project_id_array[] = (int) $t_project_id;
$g_cache_versions_project[(int) $t_project_id] = array();
}
}
if( empty( $c_project_id_array ) ) {
return;
}
$t_project_version_table = db_get_table( 'project_version' );
$query = "SELECT *
FROM $t_project_version_table
WHERE project_id IN (" . implode( ',', $c_project_id_array ) . ')
ORDER BY date_order DESC';
$result = db_query_bound( $query );
$rows = array();
while( $row = db_fetch_array( $result ) ) {
$g_cache_versions[(int) $row['id']] = $row;
$rows[ (int)$row[ 'project_id' ] ][] = $row['id'];
}
foreach( $rows as $t_project_id => $t_row ) {
$g_cache_versions_project[ (int)$t_project_id ] = $t_row;
}
return;
}
/**
* Return all versions for the specified project
* @param int $p_project_id
* @param int $p_released
* @param bool $p_obsolete
* @return array Array of version rows (in array format)
*/
function version_get_all_rows( $p_project_id, $p_released = null, $p_obsolete = false, $p_inherit = null ) {
global $g_cache_versions, $g_cache_versions_project;
if( isset( $g_cache_versions_project[ (int)$p_project_id ] ) ) {
if( !empty( $g_cache_versions_project[ (int)$p_project_id ]) ) {
foreach( $g_cache_versions_project[ (int)$p_project_id ] as $t_id ) {
$t_versions[] = version_cache_row( $t_id );
}
return $t_versions;
} else {
return array();
}
}
$c_project_id = db_prepare_int( $p_project_id );
$t_project_version_table = db_get_table( 'project_version' );
$t_param_count = 0;
$t_project_where = version_get_project_where_clause( $p_project_id, $p_inherit );
$query = "SELECT *
FROM $t_project_version_table
WHERE $t_project_where";
$query_params = array();
if( $p_released !== null ) {
$c_released = db_prepare_int( $p_released );
$query .= " AND released = " . db_param( $t_param_count++ );
$query_params[] = $c_released;
}
if( $p_obsolete !== null ) {
$c_obsolete = db_prepare_bool( $p_obsolete );
$query .= " AND obsolete = " . db_param( $t_param_count++ );
$query_params[] = $c_obsolete;
}
$query .= " ORDER BY date_order DESC";
$result = db_query_bound( $query, $query_params );
$count = db_num_rows( $result );
$rows = array();
for( $i = 0;$i < $count;$i++ ) {
$row = db_fetch_array( $result );
$g_cache_versions[(int) $row['id']] = $row;
$rows[] = $row;
}
return $rows;
}
/**
* Return all versions for the specified project, including subprojects
* @param int $p_project_id
* @param int $p_released
* @param bool $p_obsolete
* @return array
*/
function version_get_all_rows_with_subs( $p_project_id, $p_released = null, $p_obsolete = false ) {
$t_project_where = helper_project_specific_where( $p_project_id );
$t_param_count = 0;
$t_query_params = array();
if( $p_released === null ) {
$t_released_where = '';
} else {
$c_released = db_prepare_int( $p_released );
$t_released_where = "AND ( released = " . db_param( $t_param_count++ ) . " )";
$t_query_params[] = $c_released;
}
if( $p_obsolete === null ) {
$t_obsolete_where = '';
} else {
$c_obsolete = db_prepare_bool( $p_obsolete );
$t_obsolete_where = "AND ( obsolete = " . db_param( $t_param_count++ ) . " )";
$t_query_params[] = $c_obsolete;
}
$t_project_version_table = db_get_table( 'project_version' );
$query = "SELECT *
FROM $t_project_version_table
WHERE $t_project_where $t_released_where $t_obsolete_where
ORDER BY date_order DESC";
$result = db_query_bound( $query, $t_query_params );
$count = db_num_rows( $result );
$rows = array();
for( $i = 0;$i < $count;$i++ ) {
$row = db_fetch_array( $result );
$rows[] = $row;
}
return $rows;
}
/**
* Get the version_id, given the project_id and $p_version_id
* returns false if not found, otherwise returns the id.
* @param string $p_version
* @param int $p_project_id
* @param mixed $p_inherit true to look for version in parent projects, false not to, null to use default configuration.
* @return int
*/
function version_get_id( $p_version, $p_project_id = null, $p_inherit = null ) {
global $g_cache_versions;
if( $p_project_id === null ) {
$c_project_id = helper_get_current_project();
} else {
$c_project_id = db_prepare_int( $p_project_id );
}
foreach( $g_cache_versions as $t_version ) {
if(( $t_version['version'] == $p_version ) && ( $t_version['project_id'] == $c_project_id ) ) {
return $t_version['id'];
}
}
$t_project_where = version_get_project_where_clause( $c_project_id, $p_inherit );
$t_project_version_table = db_get_table( 'project_version' );
$query = "SELECT id FROM $t_project_version_table
WHERE " . $t_project_where . " AND
version=" . db_param();
$result = db_query_bound( $query, Array( $p_version ) );
if( 0 == db_num_rows( $result ) ) {
return false;
} else {
return db_result( $result );
}
}
/**
* Get the specified field name for the specified version id.
* triggers an error if version not found, otherwise returns the field value.
* @param int $p_version_id
* @param string $p_field_name
* @return string
*/
function version_get_field( $p_version_id, $p_field_name ) {
$row = version_cache_row( $p_version_id );
if( isset( $row[$p_field_name] ) ) {
return $row[$p_field_name];
} else {
error_parameters( $p_field_name );
trigger_error( ERROR_DB_FIELD_NOT_FOUND, WARNING );
return '';
}
}
/**
* Gets the full name of a version. This may include the project name as a prefix (e.g. '[MantisBT] 1.2.0')
*
* @param int $p_version_id The version id.
* @param bool $p_show_project Whether to include the project or not, null means include the project if different from current.
* @param int $p_current_project_id The current project id or null to use the cookie.
* @return string The full name of the version.
*/
function version_full_name( $p_version_id, $p_show_project = null, $p_current_project_id = null ) {
if ( 0 == $p_version_id ) {
# No Version
return '';
} else {
$t_row = version_cache_row( $p_version_id );
$t_project_id = $t_row['project_id'];
$t_current_project_id = is_null( $p_current_project_id ) ? helper_get_current_project() : $p_current_project_id;
if ( $p_show_project === null ) {
$t_show_project = $t_project_id != $t_current_project_id;
} else {
$t_show_project = $p_show_project;
}
if ( $t_show_project && $t_project_id != $t_current_project_id ) {
return '[' . project_get_name( $t_project_id ) . '] ' . $t_row['version'];
}
return $t_row['version'];
}
}
/**
* get information about a version given its id
* @param int $p_version_id
* @return VersionData
*/
function version_get( $p_version_id ) {
static $t_vars;
$row = version_cache_row( $p_version_id );
if ($t_vars == null ) {
$t_reflection = new ReflectionClass('VersionData');
$t_vars = $t_reflection->getDefaultProperties();
}
$t_version_data = new VersionData;
$t_row_keys = array_keys( $row );
# Check each variable in the class
foreach( $t_vars as $var => $val ) {
# If we got a field from the DB with the same name
if( in_array( $var, $t_row_keys, true ) ) {
# Store that value in the object
$t_version_data->$var = $row[$var];
}
}
return $t_version_data;
}
/**
* Return a copy of the version structure with all the instvars prepared for db insertion
* @param VersionData $p_version_info
* @return VersionData
*/
function version_prepare_db( $p_version_info ) {
$p_version_info->id = db_prepare_int( $p_version_info->id );
$p_version_info->project_id = db_prepare_int( $p_version_info->project_id );
$p_version_info->released = db_prepare_int( $p_version_info->released );
return $p_version_info;
}
/**
* Checks whether the product version should be shown
* (i.e. report, update, view, print).
* @param integer $p_project_id The project id.
* @return bool true: show, false: otherwise.
*/
function version_should_show_product_version( $p_project_id ) {
return ( ON == config_get( 'show_product_version', /* default */ null, /* user_id */ null, $p_project_id ) )
|| ( ( AUTO == config_get( 'show_product_version', /* default */ null, /* user_id */ null, $p_project_id ) )
&& ( count( version_get_all_rows( $p_project_id ) ) > 0 ) );
}
/**
* Gets the where clause to use for retrieving versions.
*
* @param integer $p_project_id The project id to use.
* @param bool $p_inherit Include versions from parent projects? true: yes, false: no, null: use default configuration.
* @return string The where clause not including WHERE.
*/
function version_get_project_where_clause( $p_project_id, $p_inherit ) {
project_hierarchy_cache();
if ( $p_project_id == ALL_PROJECTS ) {
$t_inherit = false;
} else {
if ( $p_inherit === null ) {
$t_inherit = ( ON == config_get( 'subprojects_inherit_versions' ) );
} else {
$t_inherit = $p_inherit;
}
}
$c_project_id = db_prepare_int( $p_project_id );
if ( $t_inherit ) {
$t_project_ids = project_hierarchy_inheritance( $p_project_id );
$t_project_where = ' project_id IN ( ' . implode( ', ', $t_project_ids ) . ' ) ';
} else {
$t_project_where = ' project_id=' . $c_project_id . ' ';
}
return $t_project_where;
}