/
Issue.php
161 lines (132 loc) · 4.76 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
<?php
# MantisBT - a php based bugtracking system
# 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/>.
$t_core_dir = config_get( 'core_path' );
require_once( $t_core_dir . DIRECTORY_SEPARATOR . 'bug_api.php' );
require_once( $t_core_dir . DIRECTORY_SEPARATOR . 'user_api.php' );
require_once( 'Interface.php' );
class ImportXml_Issue implements ImportXml_Interface {
private $old_id_;
private $new_id_;
private $newbug_;
// import Issues options
private $keepCategory_;
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_;
// TODO: if we port the import/export code to 1.1.x, this needs to be
// improved to cope with the different cases (1.1 => 1.2, 1.2 => 1.1 etc)
if( version_compare( MANTIS_VERSION, '1.2', '>' ) === true ) {
$reader->read( );
if( $this->keepCategory_ ) {
$t_category_id = category_get_id_by_name( $reader->value, $t_project_id );
if( $t_category_id !== false ) {
$this->newbug_->category_id = $t_category_id;
}
}
// 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;
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_ = bug_create( $this->newbug_ );
//echo "\nnew bug: $this->new_id_\n";
}
public function update_map( Mapper$mapper ) {
$mapper->add( 'issue', $this->old_id_, $this->new_id_ );
}
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 $squash_userid
*
* @param $field string bugdata filed to update
* @param $username string username as imported
* @param $squash_userid integer fallback userid
*/
private function get_user_id( $username, $squash_userid = 0 ) {
$t_user_id = user_get_id_by_name( $username );
if( $t_user_id === false ) {
//not found
$t_user_id = $squash_userid;
}
return $t_user_id;
}
}