/
bug_api.php
1113 lines (930 loc) · 33.5 KB
/
bug_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
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
# MantisBT - A PHP based bugtracking system
# 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/>.
/**
* Bug API
*
* @package CoreAPI
* @subpackage BugAPI
* @copyright Copyright (C) 2000 - 2002 Kenzaburo Ito - kenito@300baud.org
* @copyright Copyright (C) 2002 - 2012 MantisBT Team - mantisbt-dev@lists.sourceforge.net
* @link http://www.mantisbt.org
*
* @uses access_api.php
* @uses authentication_api.php
* @uses bugnote_api.php
* @uses bug_revision_api.php
* @uses category_api.php
* @uses config_api.php
* @uses constant_inc.php
* @uses custom_field_api.php
* @uses database_api.php
* @uses date_api.php
* @uses email_api.php
* @uses event_api.php
* @uses file_api.php
* @uses helper_api.php
* @uses history_api.php
* @uses lang_api.php
* @uses relationship_api.php
* @uses sponsorship_api.php
* @uses tag_api.php
* @uses twitter_api.php
* @uses user_api.php
* @uses utility_api.php
*/
require_api( 'access_api.php' );
require_api( 'authentication_api.php' );
require_api( 'bugnote_api.php' );
require_api( 'bug_revision_api.php' );
require_api( 'category_api.php' );
require_api( 'config_api.php' );
require_api( 'constant_inc.php' );
require_api( 'custom_field_api.php' );
require_api( 'database_api.php' );
require_api( 'date_api.php' );
require_api( 'email_api.php' );
require_api( 'event_api.php' );
require_api( 'file_api.php' );
require_api( 'helper_api.php' );
require_api( 'history_api.php' );
require_api( 'lang_api.php' );
require_api( 'relationship_api.php' );
require_api( 'sponsorship_api.php' );
require_api( 'tag_api.php' );
require_api( 'twitter_api.php' );
require_api( 'user_api.php' );
require_api( 'utility_api.php' );
/**
* Cache a database result-set containing full contents of bug_table row.
* @param array p_bug_database_result database row containing all columns from mantis_bug_table
* @param array p_stats (optional) array representing bugnote stats
* @return array returns an array representing the bug row if bug exists
* @access public
*/
function bug_cache_database_result( $p_bug_database_result, $p_stats = null ) {
global $g_cache_bug;
if( !is_array( $p_bug_database_result ) || isset( $g_cache_bug[(int) $p_bug_database_result['id']] ) ) {
return $g_cache_bug[(int) $p_bug_database_result['id']];
}
return bug_add_to_cache( $p_bug_database_result, $p_stats );
}
/**
* Cache a set of bugs
* @param array p_bug_id_array integer array representing bug ids to cache
* @return null
* @access public
* @uses database_api.php
*/
function bug_cache_array_rows( $p_bug_id_array ) {
global $g_cache_bug;
$c_bug_id_array = array();
foreach( $p_bug_id_array as $t_bug_id ) {
if( !isset( $g_cache_bug[(int) $t_bug_id] ) ) {
$c_bug_id_array[] = (int) $t_bug_id;
}
}
if( empty( $c_bug_id_array ) ) {
return;
}
$t_query = 'SELECT * FROM {bug} WHERE id IN (' . implode( ',', $c_bug_id_array ) . ')';
$t_result = db_query( $t_query );
while( $t_row = db_fetch_array( $t_result ) ) {
bug_add_to_cache( $t_row );
}
return;
}
/**
* Inject a bug into the bug cache
* @param array p_bug_row bug row to cache
* @param array p_stats bugnote stats to cache
* @return null
* @access private
*/
function bug_add_to_cache( $p_bug_row, $p_stats = null ) {
global $g_cache_bug;
$g_cache_bug[(int) $p_bug_row['id']] = $p_bug_row;
if( !is_null( $p_stats ) ) {
$g_cache_bug[(int) $p_bug_row['id']]['_stats'] = $p_stats;
}
return $g_cache_bug[(int) $p_bug_row['id']];
}
/**
* Clear a bug from the cache or all bugs if no bug id specified.
* @param int bug id to clear (optional)
* @return null
* @access public
*/
function bug_clear_cache( $p_bug_id = null ) {
global $g_cache_bug;
if( null === $p_bug_id ) {
$g_cache_bug = array();
} else {
unset( $g_cache_bug[(int) $p_bug_id] );
}
return true;
}
/**
* Check if a bug exists
* @param int p_bug_id integer representing bug id
* @return bool true if bug exists, false otherwise
* @access public
*/
function bug_exists( $p_bug_id ) {
$t_bug_data = new MantisBug($p_bug_id);
if( false == $t_bug_data->exists() ) {
return false;
} else {
return true;
}
}
/**
* Check if a bug exists. If it doesn't then trigger an error
* @param int p_bug_id integer representing bug id
* @return null
* @access public
*/
function bug_ensure_exists( $p_bug_id ) {
if( !bug_exists( $p_bug_id ) ) {
throw new MantisBT\Exception\Bug_Not_Found( $p_bug_id );
}
}
/**
* check if the given user is the reporter of the bug
* @param int p_bug_id integer representing bug id
* @param int p_user_id integer reprenting a user id
* @return bool return true if the user is the reporter, false otherwise
* @access public
*/
function bug_is_user_reporter( $p_bug_id, $p_user_id ) {
if( bug_get_field( $p_bug_id, 'reporter_id' ) == $p_user_id ) {
return true;
} else {
return false;
}
}
/**
* check if the given user is the handler of the bug
* @param int p_bug_id integer representing bug id
* @param int p_user_id integer reprenting a user id
* @return bool return true if the user is the handler, false otherwise
* @access public
*/
function bug_is_user_handler( $p_bug_id, $p_user_id ) {
if( bug_get_field( $p_bug_id, 'handler_id' ) == $p_user_id ) {
return true;
} else {
return false;
}
}
/**
* Check if the bug is readonly and shouldn't be modified
* For a bug to be readonly the status has to be >= bug_readonly_status_threshold and
* current user access level < update_readonly_bug_threshold.
* @param int p_bug_id integer representing bug id
* @return bool
* @access public
* @uses access_api.php
* @uses config_api.php
*/
function bug_is_readonly( $p_bug_id ) {
$t_status = bug_get_field( $p_bug_id, 'status' );
if( $t_status < config_get( 'bug_readonly_status_threshold' ) ) {
return false;
}
if( access_has_bug_level( config_get( 'update_readonly_bug_threshold' ), $p_bug_id ) ) {
return false;
}
return true;
}
/**
* Check if a given bug is resolved
* @param int p_bug_id integer representing bug id
* @return bool true if bug is resolved, false otherwise
* @access public
* @uses config_api.php
*/
function bug_is_resolved( $p_bug_id ) {
$t_status = bug_get_field( $p_bug_id, 'status' );
return( $t_status >= config_get( 'bug_resolved_status_threshold' ) );
}
/**
* Check if a given bug is overdue
* @param int p_bug_id integer representing bug id
* @return bool true if bug is overdue, false otherwise
* @access public
* @uses database_api.php
*/
function bug_is_overdue( $p_bug_id ) {
$t_due_date = bug_get_field( $p_bug_id, 'due_date' );
if( !date_is_null( $t_due_date ) ) {
$t_now = db_now();
if( $t_now > $t_due_date ) {
if( !bug_is_resolved( $p_bug_id ) ) {
return true;
}
}
}
return false;
}
/**
* Validate workflow state to see if bug can be moved to requested state
* @param int p_bug_status current bug status
* @param int p_wanted_status new bug status
* @return bool
* @access public
* @uses config_api.php
* @uses utility_api.php
*/
function bug_check_workflow( $p_bug_status, $p_wanted_status ) {
$t_status_enum_workflow = config_get( 'status_enum_workflow' );
if( count( $t_status_enum_workflow ) < 1 ) {
# workflow not defined, use default enum
return true;
}
if ( $p_bug_status == $p_wanted_status ) {
# no change in state, allow the transition
return true;
}
# workflow defined - find allowed states
$t_allowed_states = $t_status_enum_workflow[$p_bug_status];
return MantisEnum::hasValue( $t_allowed_states, $p_wanted_status );
}
/**
* Copy a bug from one project to another. Also make copies of issue notes, attachments, history,
* email notifications etc.
* @todo Not managed FTP file upload
* @param array p_bug_id integer representing bug id
* @param int p_target_project_id
* @param bool p_copy_custom_fields
* @param bool p_copy_relationships
* @return int representing the new bugid
* @access public
*/
function bug_copy( $p_bug_id, $p_target_project_id = null, $p_copy_custom_fields = false, $p_copy_relationships = false, $p_copy_history = false, $p_copy_attachments = false, $p_copy_bugnotes = false, $p_copy_monitoring_users = false ) {
global $g_db;
$t_mantis_db = $g_db;
$t_bug_id = (int)$p_bug_id;
$t_target_project_id = (int)$p_target_project_id;
$t_bug_data = bug_get( $t_bug_id, true );
# retrieve the project id associated with the bug
if(( $p_target_project_id == null ) || is_blank( $p_target_project_id ) ) {
$t_target_project_id = $t_bug_data->project_id;
}
$t_bug_data->project_id = $t_target_project_id;
$t_new_bug_id = $t_bug_data->create();
# MASC ATTENTION: IF THE SOURCE BUG HAS TO HANDLER THE bug_create FUNCTION CAN TRY TO AUTO-ASSIGN THE BUG
# WE FORCE HERE TO DUPLICATE THE SAME HANDLER OF THE SOURCE BUG
# @todo VB: Shouldn't we check if the handler in the source project is also a handler in the destination project?
bug_set_field( $t_new_bug_id, 'handler_id', $t_bug_data->handler_id );
bug_set_field( $t_new_bug_id, 'duplicate_id', $t_bug_data->duplicate_id );
bug_set_field( $t_new_bug_id, 'status', $t_bug_data->status );
bug_set_field( $t_new_bug_id, 'resolution', $t_bug_data->resolution );
bug_set_field( $t_new_bug_id, 'projection', $t_bug_data->projection );
bug_set_field( $t_new_bug_id, 'date_submitted', $t_bug_data->date_submitted );
bug_set_field( $t_new_bug_id, 'last_updated', $t_bug_data->last_updated );
bug_set_field( $t_new_bug_id, 'eta', $t_bug_data->eta );
bug_set_field( $t_new_bug_id, 'fixed_in_version', $t_bug_data->fixed_in_version );
bug_set_field( $t_new_bug_id, 'target_version', $t_bug_data->target_version );
bug_set_field( $t_new_bug_id, 'sponsorship_total', 0 );
bug_set_field( $t_new_bug_id, 'sticky', 0 );
bug_set_field( $t_new_bug_id, 'due_date', $t_bug_data->due_date );
# COPY CUSTOM FIELDS
if( $p_copy_custom_fields ) {
$query = "SELECT field_id, bug_id, value
FROM {custom_field_string}
WHERE bug_id=%d";
$result = db_query( $query, array( $t_bug_id ) );
while( $t_bug_custom = db_fetch_array( $result ) ) {
$c_field_id = (int)$t_bug_custom['field_id'];
$c_new_bug_id = (int)$t_new_bug_id;
$c_value = $t_bug_custom['value'];
$query = "INSERT INTO {custom_field_string}
( field_id, bug_id, value )
VALUES (%d, %d, %s)";
db_query( $query, array( $c_field_id, $c_new_bug_id, $c_value ) );
}
}
# Copy Relationships
if( $p_copy_relationships ) {
relationship_copy_all( $t_bug_id, $t_new_bug_id );
}
# Copy bugnotes
if( $p_copy_bugnotes ) {
$query = 'SELECT * FROM {bugnote} WHERE bug_id=%d';
$result = db_query( $query, array( $t_bug_id ) );
while( $t_bug_note = db_fetch_array( $result ) ) {
$t_query2 = "INSERT INTO {bugnote}
( bug_id, reporter_id, view_state, date_submitted, last_modified, note )
VALUES ( %d, %d, %d, %d, %d, %s)";
db_query( $t_query2, array( $t_new_bug_id, $t_bug_note['reporter_id'], $t_bug_note['view_state'], $t_bug_note['date_submitted'], $t_bug_note['last_modified'], $t_bugnote_text['note'] ) );
}
}
# Copy attachments
if( $p_copy_attachments ) {
$t_query = 'SELECT * FROM {bug_file} WHERE bug_id=%d';
$t_result = db_query( $t_query, array( $t_bug_id ) );
$t_bug_file = array();
while( $t_bug_file = db_fetch_array( $t_result ) ) {
# prepare the new diskfile name and then copy the file
$t_file_path = dirname( $t_bug_file['folder'] );
$t_new_diskfile_name = $t_file_path . file_generate_unique_name( 'bug-' . $t_bug_file['filename'], $t_file_path );
$t_new_file_name = file_get_display_name( $t_bug_file['filename'] );
if(( config_get( 'file_upload_method' ) == DISK ) ) {
copy( $t_bug_file['diskfile'], $t_new_diskfile_name );
chmod( $t_new_diskfile_name, config_get( 'attachments_file_permissions' ) );
}
$t_query = "INSERT INTO {bug_file}
( bug_id, title, description, diskfile, filename, folder, filesize, file_type, date_added, content )
VALUES ( %d, %s, %s, %s, %s, %s, %d, %s, %d, %d)";
db_query( $t_query, array( $t_new_bug_id, $t_bug_file['title'], $t_bug_file['description'], $t_new_diskfile_name, $t_new_file_name, $t_bug_file['folder'], $t_bug_file['filesize'], $t_bug_file['file_type'], $t_bug_file['date_added'], $t_bug_file['content'] ) );
}
}
# Copy users monitoring bug
if( $p_copy_monitoring_users ) {
bug_monitor_copy( $t_bug_id, $t_new_bug_id );
}
# COPY HISTORY
history_delete( $t_new_bug_id ); # should history only be deleted inside the if statement below?
if( $p_copy_history ) {
$query = 'SELECT * FROM {bug_history} WHERE bug_id = %d';
$result = db_query( $query, array( $t_bug_id ) );
while( $t_bug_history = db_fetch_array( $result ) ) {
$query = "INSERT INTO {bug_history}
( user_id, bug_id, date_modified, field_name, old_value, new_value, type )
VALUES ( %d, %d, %d, %s, %s, %s, %d );";
db_query( $query, array( $t_bug_history['user_id'], $t_new_bug_id, $t_bug_history['date_modified'], $t_bug_history['field_name'], $t_bug_history['old_value'], $t_bug_history['new_value'], $t_bug_history['type'] ) );
}
}
return $t_new_bug_id;
}
/**
* Moves an issue from a project to another.
*
* @todo Validate with sub-project / category inheritance scenarios.
* @param int p_bug_id The bug to be moved.
* @param int p_target_project_id The target project to move the bug to.
* @access public
*/
function bug_move( $p_bug_id, $p_target_project_id ) {
// Attempt to move disk based attachments to new project file directory.
file_move_bug_attachments( $p_bug_id, $p_target_project_id );
// Move the issue to the new project.
bug_set_field( $p_bug_id, 'project_id', $p_target_project_id );
// Check if the category for the issue is global or not.
$t_category_id = bug_get_field( $p_bug_id, 'category_id' );
$t_category_project_id = category_get_field( $t_category_id, 'project_id' );
// If not global, then attempt mapping it to the new project.
if ( $t_category_project_id != ALL_PROJECTS ) {
// Map by name
$t_category_name = category_get_field( $t_category_id, 'name' );
$t_target_project_category_id = category_get_id_by_name( $t_category_name, $p_target_project_id, /* triggerErrors */ false );
if ( $t_target_project_category_id === false ) {
// Use default category after moves, since there is no match by name.
$t_target_project_category_id = config_get( 'default_category_for_moves' );
}
bug_set_field( $p_bug_id, 'category_id', $t_target_project_category_id );
}
}
/**
* allows bug deletion :
* delete the bug, bugtext, bugnote, and bugtexts selected
* @param array p_bug_id integer representing bug id
* @return bool (always true)
* @access public
*/
function bug_delete( $p_bug_id ) {
# call pre-deletion custom function
helper_call_custom_function( 'issue_delete_validate', array( $p_bug_id ) );
# log deletion of bug
history_log_event_special( $p_bug_id, BUG_DELETED, bug_format_id( $p_bug_id ) );
email_bug_deleted( $p_bug_id );
# call post-deletion custom function. We call this here to allow the custom function to access the details of the bug before
# they are deleted from the database given it's id. The other option would be to move this to the end of the function and
# provide it with bug data rather than an id, but this will break backward compatibility.
helper_call_custom_function( 'issue_delete_notify', array( $p_bug_id ) );
# Unmonitor bug for all users
bug_unmonitor( $p_bug_id, null );
# Delete custom fields
custom_field_delete_all_values( $p_bug_id );
# Delete bugnotes
bugnote_delete_all( $p_bug_id );
# Delete all sponsorships
sponsorship_delete( sponsorship_get_all_ids( $p_bug_id ) );
# delete any relationships
relationship_delete_all( $p_bug_id );
# Delete files
file_delete_attachments( $p_bug_id );
# Detach tags
tag_bug_detach_all( $p_bug_id, false );
# Delete the bug history
history_delete( $p_bug_id );
# Delete bug info revisions
bug_revision_delete( $p_bug_id );
# Delete the bug entry
$t_query = 'DELETE FROM {bug} WHERE id=%d';
db_query( $t_query, array( $p_bug_id ) );
bug_clear_cache( $p_bug_id );
# db_query errors on failure so:
return true;
}
/**
* Delete all bugs associated with a project
* @param array p_project_id integer representing a projectid
* @return bool always true
* @access public
* @uses database_api.php
*/
function bug_delete_all( $p_project_id ) {
$t_query = 'SELECT id FROM {bug} WHERE project_id=%d';
$t_result = db_query( $t_query, array( $p_project_id ) );
while( $t_row = db_fetch_array( $t_result ) ) {
bug_delete( $t_row['id'] );
}
# @todo should we check the return value of each bug_delete() and
# return false if any of them return false? Presumable bug_delete()
# will eventually trigger an error on failure so it won't matter...
return true;
}
/**
* Returns an object representing the specified bug
* @param int p_bug_id integer representing bug id
* @param bool p_get_extended included extended information (including bug_text)
* @return object MantisBug Object
* @access public
*/
function bug_get( $p_bug_id, $p_get_extended = false ) {
$t_bug_data = new MantisBug($p_bug_id, $p_get_extended);
return $t_bug_data;
}
function bug_row_to_object( $p_row ) {
$t_bug_data = new MantisBug;
$t_bug_data->loadrow( $p_row );
return $t_bug_data;
}
/**
* return the specified field of the given bug
* if the field does not exist, display a warning and return ''
* @param int p_bug_id integer representing bug id
* @param string p_fieldname field name
* @return string
* @access public
*/
function bug_get_field( $p_bug_id, $p_field_name ) {
$t_bug_data = new MantisBug($p_bug_id);
if( isset( $t_bug_data->{$p_field_name} ) ) {
return $t_bug_data->{$p_field_name};
} else {
throw new MantisBT\Exception\DB_Field_Not_Found( $p_field_name );
return '';
}
}
/**
* return the bug summary
* this is a wrapper for the custom function
* @param int p_bug_id integer representing bug id
* @param int p_context representing SUMMARY_CAPTION, SUMMARY_FIELD
* @return string
* @access public
* @uses helper_api.php
*/
function bug_format_summary( $p_bug_id, $p_context ) {
return helper_call_custom_function( 'format_issue_summary', array( $p_bug_id, $p_context ) );
}
/**
* return the timestamp for the most recent time at which a bugnote
* associated with the bug was modified
* @param int p_bug_id integer representing bug id
* @return bool|int false or timestamp in integer format representing newest bugnote timestamp
* @access public
* @uses database_api.php
*/
function bug_get_newest_bugnote_timestamp( $p_bug_id ) {
$c_bug_id = (int)$p_bug_id;
$query = 'SELECT last_modified FROM {bugnote} WHERE bug_id=%d ORDER BY last_modified DESC';
$result = db_query( $query, array( $c_bug_id ), 1 );
$row = db_result( $result );
if( false === $row ) {
return false;
} else {
return $row;
}
}
/**
* return the timestamp for the most recent time at which a bugnote
* associated with the bug was modified and the total bugnote
* count in one db query
* @param int p_bug_id integer representing bug id
* @return object consisting of bugnote stats
* @access public
* @uses database_api.php
*/
function bug_get_bugnote_stats( $p_bug_id ) {
global $g_cache_bug;
$c_bug_id = (int)$p_bug_id;
if ( !is_null( $g_cache_bug[$c_bug_id]['_stats'] ) ) {
if ( $g_cache_bug[$c_bug_id]['_stats'] === false )
return false;
else
$t_stats = $g_cache_bug[$c_bug_id]['_stats'];
return $t_stats;
}
$t_query = "SELECT last_modified FROM {bugnote} WHERE bug_id=%d ORDER BY last_modified DESC";
$t_result = db_query( $t_query, array( $c_bug_id ) );
$t_bugnote_count = 0;
while ( $t_row = db_fetch_array( $t_result ) ) {
$t_bugnote_count++;
}
if ( $t_bugnote_count === 0 ) {
return false;
}
$t_stats['last_modified'] = $t_row['last_modified'];
$t_stats['count'] = $t_bugnote_count;
return $t_stats;
}
/**
* Get array of attachments associated with the specified bug id. The array will be
* sorted in terms of date added (ASC). The array will include the following fields:
* id, title, diskfile, filename, filesize, file_type, date_added, user_id.
* @param int p_bug_id integer representing bug id
* @return array array of results or null
* @access public
* @uses database_api.php
* @uses file_api.php
*/
function bug_get_attachments( $p_bug_id ) {
$query = "SELECT id, title, diskfile, filename, filesize, file_type, date_added, user_id, downloaded
FROM {bug_file} WHERE bug_id=%d ORDER BY date_added";
$db_result = db_query( $query, array( $p_bug_id ) );
$t_result = array();
while( $t_row = db_fetch_array( $db_result ) ) {
$t_result[] = $t_row;
}
return $t_result;
}
/**
* Set the value of a bug field
* @param int p_bug_id integer representing bug id
* @param string p_field_name pre-defined field name
* @param any p_value value to set
* @return bool (always true)
* @access public
* @uses database_api.php
* @uses history_api.php
*/
function bug_set_field( $p_bug_id, $p_field_name, $p_value ) {
$c_bug_id = (int)$p_bug_id;
$c_value = null;
switch( $p_field_name ) {
# bool
case 'sticky':
$c_value = $p_value;
break;
# int
case 'project_id':
case 'reporter_id':
case 'handler_id':
case 'duplicate_id':
case 'priority':
case 'severity':
case 'reproducibility':
case 'status':
case 'resolution':
case 'projection':
case 'category_id':
case 'eta':
case 'view_state':
case 'profile_id':
case 'sponsorship_total':
$c_value = (int) $p_value;
break;
# string
case 'os':
case 'os_build':
case 'platform':
case 'version':
case 'fixed_in_version':
case 'target_version':
case 'build':
case 'summary':
$c_value = $p_value;
break;
# dates
case 'last_updated':
case 'date_submitted':
case 'due_date':
if ( !is_numeric( $p_value ) ) {
throw new MantisBT\Exception\Generic();
}
$c_value = $p_value;
break;
default:
throw new MantisBT\Exception\DB_Field_Not_Found( $p_field_name );
break;
}
$t_current_value = bug_get_field( $p_bug_id, $p_field_name );
# return if status is already set
if( $c_value == $t_current_value ) {
return true;
}
# Update fields
$query = 'UPDATE {bug} SET $p_field_name=%s WHERE id=%d';
db_query( $query, array( $c_value, $c_bug_id ) );
# updated the last_updated date
bug_update_date( $p_bug_id );
# log changes except for duplicate_id which is obsolete and should be removed in
# MantisBT 1.3.
switch( $p_field_name ) {
case 'duplicate_id':
break;
case 'category_id':
history_log_event_direct( $p_bug_id, 'category', category_full_name( $t_current_value, false ), category_full_name( $c_value, false ) );
break;
default:
history_log_event_direct( $p_bug_id, $p_field_name, $t_current_value, $c_value );
}
bug_clear_cache( $p_bug_id );
return true;
}
/**
* assign the bug to the given user
* @param array p_bug_id_array integer array representing bug ids to cache
* @return null
* @access public
* @uses database_api.php
*/
function bug_assign( $p_bug_id, $p_user_id, $p_bugnote_text = '', $p_bugnote_private = false ) {
if(( $p_user_id != NO_USER ) && !access_has_bug_level( config_get( 'handle_bug_threshold' ), $p_bug_id, $p_user_id ) ) {
throw new MantisBT\Exception\User_Does_Not_Have_Req_Access();
}
# extract current information into history variables
$h_status = bug_get_field( $p_bug_id, 'status' );
$h_handler_id = bug_get_field( $p_bug_id, 'handler_id' );
if(( ON == config_get( 'auto_set_status_to_assigned' ) ) && ( NO_USER != $p_user_id ) ) {
$t_ass_val = config_get( 'bug_assigned_status' );
} else {
$t_ass_val = $h_status;
}
if(( $t_ass_val != $h_status ) || ( $p_user_id != $h_handler_id ) ) {
# get user id
$query = 'UPDATE {bug} SET handler_id=%d, status=%d WHERE id=%d';
db_query( $query, array( $p_user_id, $t_ass_val, $p_bug_id ) );
# log changes
history_log_event_direct( $c_bug_id, 'status', $h_status, $t_ass_val );
history_log_event_direct( $c_bug_id, 'handler_id', $h_handler_id, $p_user_id );
# Add bugnote if supplied ignore false return
bugnote_add( $p_bug_id, $p_bugnote_text, 0, $p_bugnote_private, 0, '', NULL, FALSE );
# updated the last_updated date
bug_update_date( $p_bug_id );
bug_clear_cache( $p_bug_id );
# send assigned to email
email_assign( $p_bug_id );
}
return true;
}
/**
* close the given bug
* @param int p_bug_id
* @param string p_bugnote_text
* @param bool p_bugnote_private
* @param string p_time_tracking
* @return bool (always true)
* @access public
*/
function bug_close( $p_bug_id, $p_bugnote_text = '', $p_bugnote_private = false, $p_time_tracking = '0:00' ) {
$p_bugnote_text = trim( $p_bugnote_text );
# Add bugnote if supplied ignore a false return
# Moved bugnote_add before bug_set_field calls in case time_tracking_no_note is off.
# Error condition stopped execution but status had already been changed
bugnote_add( $p_bug_id, $p_bugnote_text, $p_time_tracking, $p_bugnote_private, 0, '', NULL, FALSE );
bug_set_field( $p_bug_id, 'status', config_get( 'bug_closed_status_threshold' ) );
email_close( $p_bug_id );
email_relationship_child_closed( $p_bug_id );
return true;
}
/**
* resolve the given bug
* @return bool (alawys true)
* @access public
*/
function bug_resolve( $p_bug_id, $p_resolution, $p_fixed_in_version = '', $p_bugnote_text = '', $p_duplicate_id = null, $p_handler_id = null, $p_bugnote_private = false, $p_time_tracking = '0:00' ) {
$c_resolution = (int) $p_resolution;
$p_bugnote_text = trim( $p_bugnote_text );
# Add bugnote if supplied
# Moved bugnote_add before bug_set_field calls in case time_tracking_no_note is off.
# Error condition stopped execution but status had already been changed
bugnote_add( $p_bug_id, $p_bugnote_text, $p_time_tracking, $p_bugnote_private, 0, '', NULL, FALSE );
$t_duplicate = !is_blank( $p_duplicate_id ) && ( $p_duplicate_id != 0 );
if( $t_duplicate ) {
if( $p_bug_id == $p_duplicate_id ) {
throw new MantisBT\Exception\Bug_Duplicate_Self();
# never returns
}
# the related bug exists...
bug_ensure_exists( $p_duplicate_id );
# check if there is other relationship between the bugs...
$t_id_relationship = relationship_same_type_exists( $p_bug_id, $p_duplicate_id, BUG_DUPLICATE );
if( $t_id_relationship > 0 ) {
# Update the relationship
relationship_update( $t_id_relationship, $p_bug_id, $p_duplicate_id, BUG_DUPLICATE );
# Add log line to the history (both bugs)
history_log_event_special( $p_bug_id, BUG_REPLACE_RELATIONSHIP, BUG_DUPLICATE, $p_duplicate_id );
history_log_event_special( $p_duplicate_id, BUG_REPLACE_RELATIONSHIP, BUG_HAS_DUPLICATE, $p_bug_id );
} else if ( $t_id_relationship != -1 ) {
# Add the new relationship
relationship_add( $p_bug_id, $p_duplicate_id, BUG_DUPLICATE );
# Add log line to the history (both bugs)
history_log_event_special( $p_bug_id, BUG_ADD_RELATIONSHIP, BUG_DUPLICATE, $p_duplicate_id );
history_log_event_special( $p_duplicate_id, BUG_ADD_RELATIONSHIP, BUG_HAS_DUPLICATE, $p_bug_id );
} # else relationship is -1 - same type exists, do nothing
# Copy list of users monitoring the duplicate bug to the original bug
$t_old_reporter_id = bug_get_field( $p_bug_id, 'reporter_id' );
$t_old_handler_id = bug_get_field( $p_bug_id, 'handler_id' );
if ( user_exists( $t_old_reporter_id ) ) {
bug_monitor( $p_duplicate_id, $t_old_reporter_id );
}
if ( user_exists ( $t_old_handler_id ) ) {
bug_monitor( $p_duplicate_id, $t_old_handler_id );
}
bug_monitor_copy( $p_bug_id, $p_duplicate_id );
bug_set_field( $p_bug_id, 'duplicate_id', (int) $p_duplicate_id );
}
bug_set_field( $p_bug_id, 'status', config_get( 'bug_resolved_status_threshold' ) );
bug_set_field( $p_bug_id, 'fixed_in_version', $p_fixed_in_version );
bug_set_field( $p_bug_id, 'resolution', $c_resolution );
# only set handler if specified explicitly or if bug was not assigned to a handler
if( null == $p_handler_id ) {
if( bug_get_field( $p_bug_id, 'handler_id' ) == 0 ) {
$p_handler_id = auth_get_current_user_id();
bug_set_field( $p_bug_id, 'handler_id', $p_handler_id );
}
} else {
bug_set_field( $p_bug_id, 'handler_id', $p_handler_id );
}
email_resolved( $p_bug_id );
email_relationship_child_resolved( $p_bug_id );
twitter_issue_resolved( $p_bug_id );
return true;
}
/**
* reopen the given bug
* @param int p_bug_id
* @param string p_bugnote_text
* @param string p_time_tracking
* @param bool p_bugnote_private
* @return bool (always true)
* @access public
* @uses database_api.php
* @uses email_api.php
* @uses bugnote_api.php
* @uses config_api.php
*/
function bug_reopen( $p_bug_id, $p_bugnote_text = '', $p_time_tracking = '0:00', $p_bugnote_private = false ) {
$p_bugnote_text = trim( $p_bugnote_text );
# Add bugnote if supplied
# Moved bugnote_add before bug_set_field calls in case time_tracking_no_note is off.
# Error condition stopped execution but status had already been changed
bugnote_add( $p_bug_id, $p_bugnote_text, $p_time_tracking, $p_bugnote_private, 0, '', NULL, FALSE );
bug_set_field( $p_bug_id, 'status', config_get( 'bug_reopen_status' ) );
bug_set_field( $p_bug_id, 'resolution', config_get( 'bug_reopen_resolution' ) );
email_reopen( $p_bug_id );
return true;
}
/**
* updates the last_updated field
* @param int p_bug_id integer representing bug ids
* @return bool (always true)
* @access public
* @uses database_api.php
*/
function bug_update_date( $p_bug_id ) {
$t_query = 'UPDATE {bug} SET last_updated= %d WHERE id=%d';
db_query( $t_query, array( db_now(), $p_bug_id ) );
bug_clear_cache( $p_bug_id );
return true;
}
/**
* enable monitoring of this bug for the user
* @param int p_bug_id integer representing bug ids
* @param int p_user_id integer representing user ids
* @return true if successful, false if unsuccessful
* @access public
* @uses database_api.php
* @uses history_api.php
* @uses user_api.php
*/
function bug_monitor( $p_bug_id, $p_user_id ) {
$c_bug_id = (int) $p_bug_id;
$c_user_id = (int) $p_user_id;
# Make sure we aren't already monitoring this bug
if( user_is_monitoring_bug( $c_user_id, $c_bug_id ) ) {
return true;
}
# Don't let the anonymous user monitor bugs
if ( user_is_anonymous( $c_user_id ) ) {
return false;
}