/
version_api.php
674 lines (567 loc) · 19.8 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
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
<?php
# MantisBT - A PHP based bugtracking system
# Copyright (C) 2000 - 2002 Kenzaburo Ito - kenito@300baud.org
# Copyright 2002 - 2013 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/>.
/**
* Version API
*
* @package CoreAPI
* @subpackage VersionAPI
* @copyright Copyright 2000 - 2002 Kenzaburo Ito - kenito@300baud.org
* @copyright Copyright 2002 MantisBT Team - mantisbt-dev@lists.sourceforge.net
* @link http://www.mantisbt.org
*
* @uses config_api.php
* @uses constant_inc.php
* @uses database_api.php
* @uses date_api.php
* @uses helper_api.php
* @uses project_api.php
* @uses project_hierarchy_api.php
*/
require_api( 'config_api.php' );
require_api( 'constant_inc.php' );
require_api( 'database_api.php' );
require_api( 'date_api.php' );
require_api( 'helper_api.php' );
require_api( 'project_api.php' );
require_api( 'project_hierarchy_api.php' );
/**
* Version Data Structure Definition
* @package MantisBT
* @subpackage classes
*/
class VersionData {
/**
* Version id
*/
protected $id = 0;
/**
* Project ID
*/
protected $project_id = 0;
/**
* Version name
*/
protected $version = '';
/**
* Version Description
*/
protected $description = '';
/**
* Version Release Status e.g. VERSION_FUTURE
*/
protected $released = VERSION_FUTURE;
/**
* Date Order
*/
protected $date_order = 1;
/**
* Obsolete
*/
protected $obsolete = 0;
/**
* Overloaded function
* @param string $p_name property name
* @param string $p_value value
* @private
* @throws MantisBT\Exception\Validation\DateFormatInvalid
*/
public function __set($p_name, $p_value) {
switch ($p_name) {
case 'date_order':
if( !is_numeric($p_value) ) {
if( $p_value == '' ) {
$p_value = date_get_null();
} else {
$p_value = strtotime( $p_value );
if ( $p_value === false ) {
throw new MantisBT\Exception\Validation\DateFormatInvalid();
}
}
}
}
$this->$p_name = $p_value;
}
/**
* Overloaded function
* @param string $p_name property name
* @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
* @return array
* @throws MantisBT\Exception\Issue\Version\VersionNotFound
*/
function version_cache_row( $p_version_id ) {
global $g_cache_versions;
$c_version_id = (int)$p_version_id;
if( isset( $g_cache_versions[$c_version_id] ) ) {
return $g_cache_versions[$c_version_id];
}
$query = 'SELECT * FROM {project_version} WHERE id=%d';
$result = db_query( $query, array( $c_version_id ) );
$row = db_fetch_array( $result );
if( !$row ) {
throw new MantisBT\Exception\Issue\Version\VersionNotFound( $p_version_id );
}
$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 ) {
try {
version_cache_row( $p_version_id );
} catch ( MantisBT\Exception\Issue\Version\VersionNotFound $e ) {
return false;
}
return true;
}
/**
* 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
* @throws MantisBT\Exception\Issue\Version\VersionNotFound
*/
function version_ensure_exists( $p_version_id ) {
if( !version_exists( $p_version_id ) ) {
throw new MantisBT\Exception\Issue\Version\VersionNotFound( $p_version_id );
}
}
/**
* 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
* @throws MantisBT\Exception\Issue\Version\VersionDuplicate
*/
function version_ensure_unique( $p_version, $p_project_id = null ) {
if( !version_is_unique( $p_version, $p_project_id ) ) {
throw new MantisBT\Exception\Issue\Version\VersionDuplicate();
}
}
/**
* 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 = (int)$p_project_id;
$c_released = (int)$p_released;
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 );
$query = "INSERT INTO {project_version}
( project_id, version, date_order, description, released, obsolete )
VALUES
(%d, %s, %d, %s, %d, %d )";
db_query( $query, array( $c_project_id, $p_version, $c_date_order, $p_description, $c_released, $p_obsolete ) );
$t_version_id = db_insert_id( '{project_version}' );
event_signal( 'EVENT_MANAGE_VERSION_CREATE', array( $t_version_id ) );
# db_query errors on failure so:
return $t_version_id;
}
/**
* Update the definition of a version
* @param VersionData $p_version_info
* @throws MantisBT\Exception\Issue\Version\VersionDuplicate
*/
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(( mb_strtolower( $t_old_version_name ) != mb_strtolower( $p_version_info->version ) ) && !version_is_unique( $p_version_info->version, $p_version_info->project_id ) ) {
throw new MantisBT\Exception\Issue\Version\VersionDuplicate();
}
$c_version_id = $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 = $p_version_info->released;
$c_obsolete = $p_version_info->obsolete;
$c_date_order = $p_version_info->date_order;
$c_project_id = $p_version_info->project_id;
$query = "UPDATE {project_version}
SET version=%s,
description=%s,
released=%d,
date_order=%d,
obsolete=%d
WHERE id=%d";
db_query( $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 ) {
$t_project_list = array( $c_project_id );
if ( config_get( 'subprojects_inherit_versions' ) ) {
$t_project_list = array_merge( $t_project_list, project_hierarchy_get_all_subprojects( $c_project_id, true ) );
}
$t_project_list = implode( ',', $t_project_list );
$query = 'UPDATE {bug} SET version=%s WHERE ( project_id IN ( $t_project_list ) ) AND ( version=%s)';
db_query( $query, array( $c_version_name, $c_old_version_name ) );
$query = "UPDATE {bug} SET fixed_in_version=%s
WHERE ( project_id IN ( $t_project_list ) ) AND ( fixed_in_version=%s)";
db_query( $query, array( $c_version_name, $c_old_version_name ) );
$query = "UPDATE {bug}
SET target_version=%s
WHERE ( project_id IN ( $t_project_list ) ) AND ( target_version=%s)";
db_query( $query, array( $c_version_name, $c_old_version_name ) );
$query = "UPDATE {history}
SET old_value=%s
WHERE field_name IN ('version','fixed_in_version','target_version')
AND old_value=%s
AND bug_id IN (SELECT id FROM {bug} WHERE project_id IN ( $t_project_list ))";
db_query( $query, array( $c_version_name, $c_old_version_name ) );
$query = "UPDATE {history}
SET new_value=%s
WHERE field_name IN ('version','fixed_in_version','target_version')
AND new_value=%s
AND bug_id IN (SELECT id FROM {bug} WHERE project_id IN ( $t_project_list ))";
db_query( $query, array( $c_version_name, $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 probably need to update the saved filters too?
*/
}
}
/**
* Remove a version from the project
* @param int $p_version_id
* @param string $p_new_version
*/
function version_remove( $p_version_id, $p_new_version = '' ) {
$c_version_id = (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 = (int)$t_project_id;
$query = 'DELETE FROM {project_version} WHERE id=%d';
db_query( $query, array( $c_version_id ) );
$t_project_list = array( $c_project_id );
if ( config_get( 'subprojects_inherit_versions' ) ) {
$t_project_list = array_merge( $t_project_list, project_hierarchy_get_all_subprojects( $c_project_id, true ) );
}
$t_project_list = implode( ',', $t_project_list );
$query = "UPDATE {bug} SET version=%s
WHERE project_id IN ( $t_project_list ) AND version=%s";
db_query( $query, array( $p_new_version, $t_old_version ) );
$query = "UPDATE {bug} SET fixed_in_version=%s
WHERE ( project_id IN ( $t_project_list ) ) AND ( fixed_in_version=%s )";
db_query( $query, array( $p_new_version, $t_old_version ) );
$query = "UPDATE {bug} SET target_version=%s
WHERE ( project_id IN ( $t_project_list ) ) AND ( target_version=%s)";
db_query( $query, array( $p_new_version, $t_old_version ) );
}
/**
* Remove all versions associated with a project
* @param int $p_project_id
* @return bool
*/
function version_remove_all( $p_project_id ) {
$c_project_id = (int)$p_project_id;
# remove all references to versions from verison, fixed in version and target version.
$query = "UPDATE {bug} SET version='', fixed_in_version='', target_version=''
WHERE project_id=%d";
db_query( $query, array( $c_project_id ) );
# remove the actual versions associated with the project.
$query = 'DELETE FROM {project_version} WHERE project_id=%d';
db_query( $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;
}
$query = "SELECT * FROM {project_version}
WHERE project_id IN (" . implode( ',', $c_project_id_array ) . ')
ORDER BY date_order DESC';
$result = db_query( $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 project id
* @param int $p_released include released
* @param bool $p_obsolete include obsolete
* @param bool $p_inherit inherit versions
* @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();
}
}
$t_project_where = version_get_project_where_clause( $p_project_id, $p_inherit );
$query = "SELECT * FROM {project_version} WHERE $t_project_where";
$query_params = array();
if( $p_released !== null ) {
$c_released = (int)$p_released;
$query .= ' AND released = %d';
$query_params[] = $c_released;
}
if( $p_obsolete !== null ) {
$query .= ' AND obsolete = %d';
$query_params[] = $p_obsolete;
}
$query .= " ORDER BY date_order DESC";
$t_result = db_query( $query, $query_params );
$t_rows = array();
while( $t_row = db_fetch_array( $t_result ) ) {
$g_cache_versions[(int) $t_row['id']] = $t_row;
$t_rows[] = $t_row;
}
return $t_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_query_params = array();
if( $p_released === null ) {
$t_released_where = '';
} else {
$c_released = (int)$p_released;
$t_released_where = "AND ( released = %d )";
$t_query_params[] = $c_released;
}
if( $p_obsolete === null ) {
$t_obsolete_where = '';
} else {
$t_obsolete_where = "AND ( obsolete = %d )";
$t_query_params[] = $p_obsolete;
}
$query = "SELECT * FROM {project_version}
WHERE $t_project_where $t_released_where $t_obsolete_where
ORDER BY date_order DESC";
$result = db_query( $query, $t_query_params );
$rows = array();
while( $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 = (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 );
$query = "SELECT id FROM {project_version}
WHERE " . $t_project_where . " AND version=%s";
$result = db_query( $query, array( $p_version ) );
if( $t_row = db_result( $result ) ) {
return $t_row;
} else {
return false;
}
}
/**
* 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
* @throws MantisBT\Exception\Database\FieldNotFound
*/
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 {
throw new MantisBT\Exception\Database\FieldNotFound( array( $p_field_name ) );
}
}
/**
* Gets the full name of a version. This may include the project name as a prefix (e.g. '[MantisBT] 2.0.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 = (int)($p_version_info->id);
$p_version_info->project_id = (int)($p_version_info->project_id);
$p_version_info->released = (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 ) {
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 = (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;
}