/
Issue.php
315 lines (279 loc) · 9.57 KB
/
Issue.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
<?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/>.
*
* @copyright Copyright 2002 MantisBT Team - mantisbt-dev@lists.sourceforge.net
*/
/**
* Import XML issue class
*/
require_api( 'bug_api.php' );
require_api( 'user_api.php' );
require_once( 'Interface.php' );
/**
* Import XML issue class
*/
class ImportXml_Issue implements ImportXml_Interface {
/**
* old issue id
*/
private $old_id_;
/**
* new issue id
*/
private $new_id_;
/**
* new bug object
*/
private $newbug_;
// import Issues options
/**
* keep existing category
* @var bool
*/
private $keepCategory_;
/**
* default category
* @var int
*/
private $defaultCategory_;
public function __construct( $keepCategory, $defaultCategory ) {
$this->newbug_ = new BugData;
$this->keepCategory_ = $keepCategory;
$this->defaultCategory_ = $defaultCategory;
}
// Read stream until current item finishes, processing
// the data found
public function process( XMLreader $reader ) {
//print "\nImportIssue process()\n";
$t_project_id = helper_get_current_project(); // TODO: category_get_id_by_name could work by default on current project
$userId = auth_get_current_user_id( );
$depth = $reader->depth;
while( $reader->read() &&
($reader->depth > $depth ||
$reader->nodeType != XMLReader::END_ELEMENT)) {
if( $reader->nodeType == XMLReader::ELEMENT ) {
switch( $reader->localName ) {
case 'reporter':
$t_old_id = $reader->getAttribute( 'id' );
$reader->read( );
$this->newbug_->reporter_id = $this->get_user_id( $reader->value, $userId );
//echo "reporter: old id = $t_old_id - new id = {$this->newbug_->reporter_id}\n";
break;
case 'handler':
$t_old_id = $reader->getAttribute( 'id' );
$reader->read( );
$this->newbug_->handler_id = $this->get_user_id( $reader->value, $userId );
//echo "handler: old id = $t_old_id - new id = {$this->newbug_->handler_id}\n";
break;
case 'category':
$this->newbug_->category_id = $this->defaultCategory_;
if( version_compare( MANTIS_VERSION, '1.2', '>' ) === true ) {
$reader->read( );
if( $this->keepCategory_ ) {
# Check for the category's existence in the current project
# well as its parents (if any)
$t_projects_hierarchy = project_hierarchy_inheritance( $t_project_id );
foreach( $t_projects_hierarchy as $t_project ) {
$t_category_id = category_get_id_by_name( $reader->value, $t_project, false );
if( $t_category_id !== false ) {
$this->newbug_->category_id = $t_category_id;
break;
}
}
}
// echo "new id = {$this->newbug_->category_id}\n";
}
break;
case 'eta':
case 'priority':
case 'projection':
case 'reproducibility':
case 'resolution':
case 'severity':
case 'status':
case 'view_state':
$t_field = $reader->localName;
$t_id = $reader->getAttribute( 'id' );
$reader->read( );
$t_value = $reader->value;
// Here we assume ids have the same meaning in both installations
// TODO add a check for customized values
$this->newbug_->$t_field = $t_id;
break;
case 'id':
$reader->read( );
$this->old_id_ = $reader->value;
break;
case 'project';
// ignore original value, use current project
$this->newbug_->project_id = $t_project_id;
break;
case 'custom_fields':
// store custom fields
$i = -1;
$depth_cf = $reader->depth;
while ( $reader->read() &&
( $reader->depth > $depth_cf ||
$reader->nodeType != XMLReader::END_ELEMENT ) ) {
if ( $reader->nodeType == XMLReader::ELEMENT ) {
if ($reader->localName == 'custom_field') {
$i++;
}
switch ( $reader->localName ) {
default:
$field = $reader->localName;
$reader->read( );
$t_custom_fields[$i]->$field = $reader->value;
}
}
}
break;
case 'bugnotes':
// store bug notes
$i = -1;
$depth_bn = $reader->depth;
while ( $reader->read() &&
( $reader->depth > $depth_bn ||
$reader->nodeType != XMLReader::END_ELEMENT ) ) {
if ( $reader->nodeType == XMLReader::ELEMENT ) {
if ($reader->localName == 'bugnote') {
$i++;
}
switch ( $reader->localName ) {
case 'reporter':
$t_old_id = $reader->getAttribute( 'id' );
$reader->read( );
$t_bugnotes[$i]->reporter_id = $this->get_user_id( $reader->value, $userId );
break;
case 'view_state':
$t_old_id = $reader->getAttribute( 'id' );
$reader->read( );
$t_bugnotes[$i]->private = $reader->value == VS_PRIVATE ? true : false;
break;
default:
$field = $reader->localName;
$reader->read( );
$t_bugnotes[$i]->$field = $reader->value;
}
}
}
break;
case 'attachments':
// store attachments
$i = -1;
$depth_att = $reader->depth;
while ( $reader->read() &&
( $reader->depth > $depth_att ||
$reader->nodeType != XMLReader::END_ELEMENT ) ) {
if ( $reader->nodeType == XMLReader::ELEMENT ) {
if ($reader->localName == 'attachment') {
$i++;
}
switch ( $reader->localName ) {
default:
$field = $reader->localName;
$reader->read( );
$t_attachments[$i]->$field = $reader->value;
}
}
}
break;
default:
$field = $reader->localName;
//echo "using default handler for field: $field\n";
$reader->read( );
$this->newbug_->$field = $reader->value;
}
}
}
// now save the new bug
$this->new_id_ = $this->newbug_->create();
// add custom fields
if ( $this->new_id_ > 0 && is_array( $t_custom_fields ) && count( $t_custom_fields ) > 0 ) {
foreach ( $t_custom_fields as $t_custom_field) {
$t_custom_field_id = custom_field_get_id_from_name( $t_custom_field->name );
if ( custom_field_ensure_exists( $t_custom_field_id ) && custom_field_is_linked( $t_custom_field_id, $t_project_id ) ) {
custom_field_set_value( $t_custom_field->id, $this->new_id_, $t_custom_field->value );
}
else {
error_parameters( $t_custom_field->name, $t_custom_field_id );
trigger_error( ERROR_CUSTOM_FIELD_NOT_LINKED_TO_PROJECT, ERROR );
}
}
}
// add bugnotes
if ( $this->new_id_ > 0 && is_array( $t_bugnotes ) && count( $t_bugnotes ) > 0 ) {
foreach ( $t_bugnotes as $t_bugnote) {
bugnote_add( $this->new_id_, $t_bugnote->note, $t_bugnote->time_tracking, $t_bugnote->private, $t_bugnote->note_type, $t_bugnote_>note_attr, $t_bugnote->reporter_id, false, $t_bugnote->date_submitted, $t_bugnote->last_modified, true );
}
}
// add attachments
if ( $this->new_id_ > 0 && is_array( $t_attachments ) && count( $t_attachments ) > 0 ) {
foreach ( $t_attachments as $t_attachment) {
// Create a temporary file in the temporary files directory using sys_get_temp_dir()
$temp_file_name = tempnam( sys_get_temp_dir(), 'MantisImport' );
file_put_contents( $temp_file_name, base64_decode( $t_attachment->content ) );
$file_data = array( 'name' => $t_attachment->filename,
'type' => $t_attachment->file_type,
'tmp_name' => $temp_file_name,
'size' => filesize( $temp_file_name ) );
// unfortunately we have no clue who has added the attachment (this could only be fetched from history -> feel free to implement this)
// also I have no clue where description should come from...
file_add( $this->new_id_, $file_data, 'bug', $t_attachment->title, $p_desc = '', $p_user_id = null, $t_attachment->date_added, true );
unlink( $temp_file_name );
}
}
//echo "\nnew bug: $this->new_id_\n";
}
/**
* update mapper
* @param Mapper mapper
*/
public function update_map( ImportXml_Mapper $mapper ) {
$mapper->add( 'issue', $this->old_id_, $this->new_id_ );
}
/**
* Dump Diagnostic information
*/
public function dumpbug( ) {
var_dump( $this->newbug_ );
var_dump( $this->issueMap );
}
/**
* Return the user id in the destination tracker
*
* Current logic is: try to find the same user by username;
* if it fails, use $p_squash_userid
*
* @param string $p_username username as imported
* @param int $p_squash_userid fallback userid
* @return int
*/
private function get_user_id( $p_username, $p_squash_userid = 0 ) {
$t_user_id = user_get_id_by_name( $p_username );
if( $t_user_id === false ) {
// user not found by username -> check real name
// keep in mind that the setting config_get( 'show_user_realname_threshold' ) may differ between import and export system!
$t_user_id = user_get_id_by_realname( $p_username );
if ( $t_user_id === false ) {
//not found
$t_user_id = $p_squash_userid;
}
}
return $t_user_id;
}
}