[ Index ]

PHP Cross Reference of MantisBT

title

Body

[close]

/plugins/XmlImportExport/ImportXml/ -> Issue.php (source)

   1  <?php
   2  # MantisBT - A PHP based bugtracking system
   3  # Copyright (C) 2002 - 2011  MantisBT Team - mantisbt-dev@lists.sourceforge.net
   4  # MantisBT is free software: you can redistribute it and/or modify
   5  # it under the terms of the GNU General Public License as published by
   6  # the Free Software Foundation, either version 2 of the License, or
   7  # (at your option) any later version.
   8  #
   9  # MantisBT is distributed in the hope that it will be useful,
  10  # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  # GNU General Public License for more details.
  13  #
  14  # You should have received a copy of the GNU General Public License
  15  # along with MantisBT.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  require_once( 'bug_api.php' );
  18  require_once( 'user_api.php' );
  19  require_once ( 'Interface.php' );
  20  
  21  class ImportXml_Issue implements ImportXml_Interface {
  22  
  23      private $old_id_;
  24      private $new_id_;
  25  
  26      private $newbug_;
  27  
  28      // import Issues options
  29      private $keepCategory_;
  30      private $defaultCategory_;
  31  
  32  	public function __construct( $keepCategory, $defaultCategory ) {
  33          $this->newbug_ = new BugData;
  34          $this->keepCategory_ = $keepCategory;
  35          $this->defaultCategory_ = $defaultCategory;
  36      }
  37  
  38      // Read stream until current item finishes, processing
  39      // the data found
  40  	public function process( XMLreader $reader ) {
  41  
  42          //print "\nImportIssue process()\n";
  43          $t_project_id = helper_get_current_project(); // TODO: category_get_id_by_name could work by default on current project
  44          $userId = auth_get_current_user_id( );
  45  
  46          $depth = $reader->depth;
  47          while( $reader->read() &&
  48                  ($reader->depth > $depth ||
  49                   $reader->nodeType != XMLReader::END_ELEMENT)) {
  50              if( $reader->nodeType == XMLReader::ELEMENT ) {
  51                  switch( $reader->localName ) {
  52                      case 'reporter':
  53                          $t_old_id = $reader->getAttribute( 'id' );
  54                          $reader->read( );
  55                          $this->newbug_->reporter_id = $this->get_user_id( $reader->value, $userId );
  56  
  57                          //echo "reporter: old id = $t_old_id - new id = {$this->newbug_->reporter_id}\n";
  58                          break;
  59  
  60                      case 'handler':
  61                          $t_old_id = $reader->getAttribute( 'id' );
  62                          $reader->read( );
  63                          $this->newbug_->handler_id = $this->get_user_id( $reader->value, $userId );
  64  
  65                          //echo "handler: old id = $t_old_id - new id = {$this->newbug_->handler_id}\n";
  66                          break;
  67  
  68                      case 'category':
  69                          $this->newbug_->category_id = $this->defaultCategory_;
  70  
  71                          // TODO: if we port the import/export code to 1.1.x, this needs to be
  72                          //       improved to cope with the different cases (1.1 => 1.2, 1.2 => 1.1 etc)
  73                          if( version_compare( MANTIS_VERSION, '1.2', '>' ) === true ) {
  74                              $reader->read( );
  75  
  76                              if( $this->keepCategory_ ) {
  77                                  $t_category_id = category_get_id_by_name( $reader->value, $t_project_id );
  78                                  if( $t_category_id !== false ) {
  79                                      $this->newbug_->category_id = $t_category_id;
  80                                  }
  81                              }
  82  
  83                              //    echo "new id = {$this->newbug_->category_id}\n";
  84                          }
  85                          break;
  86  
  87                      case 'eta':
  88                      case 'priority':
  89                      case 'projection':
  90                      case 'reproducibility':
  91                      case 'resolution':
  92                      case 'severity':
  93                      case 'status':
  94                      case 'view_state':
  95                          $t_field = $reader->localName;
  96                          $t_id = $reader->getAttribute( 'id' );
  97                          $reader->read( );
  98                          $t_value = $reader->value;
  99  
 100                          // Here we assume ids have the same meaning in both installations
 101                          // TODO add a check for customized values
 102                          $this->newbug_->$t_field = $t_id;
 103                          break;
 104  
 105                      case 'id':
 106                          $reader->read( );
 107                          $this->old_id_ = $reader->value;
 108                          break;
 109  
 110                      case 'project';
 111                          // ignore original value, use current project
 112                          $this->newbug_->project_id = $t_project_id;
 113                          break;
 114  
 115                      case 'custom_fields':
 116                          // store custom fields
 117                          $i = -1;
 118                          $depth_cf = $reader->depth;
 119                          while ( $reader->read() &&
 120                                  ( $reader->depth > $depth_cf ||
 121                                    $reader->nodeType != XMLReader::END_ELEMENT ) ) {
 122                              if ( $reader->nodeType == XMLReader::ELEMENT ) {
 123                                  if ($reader->localName == 'custom_field') {
 124                                      $i++;
 125                                  }
 126                                  switch ( $reader->localName ) {
 127                                      default:
 128                                          $field = $reader->localName;
 129                                          $reader->read( );
 130                                          $t_custom_fields[$i]->$field = $reader->value;
 131                                  }
 132                              }
 133                          }
 134                          break;
 135  
 136                      case 'bugnotes':
 137                          // store bug notes
 138                          $i = -1;
 139                          $depth_bn = $reader->depth;
 140                          while ( $reader->read() &&
 141                                  ( $reader->depth > $depth_bn ||
 142                                    $reader->nodeType != XMLReader::END_ELEMENT ) ) {
 143                              if ( $reader->nodeType == XMLReader::ELEMENT ) {
 144                                  if ($reader->localName == 'bugnote') {
 145                                      $i++;
 146                                  }
 147                                  switch ( $reader->localName ) {
 148                                      case 'reporter':
 149                                          $t_old_id = $reader->getAttribute( 'id' );
 150                                          $reader->read( );
 151                                          $t_bugnotes[$i]->reporter_id = $this->get_user_id( $reader->value, $userId );
 152                                          break;
 153  
 154                                      case 'view_state':
 155                                          $t_old_id = $reader->getAttribute( 'id' );
 156                                          $reader->read( );
 157                                          $t_bugnotes[$i]->private = $reader->value == VS_PRIVATE ? true : false;
 158                                          break;
 159  
 160                                      default:
 161                                          $field = $reader->localName;
 162                                          $reader->read( );
 163                                          $t_bugnotes[$i]->$field = $reader->value;
 164                                  }
 165                              }
 166                          }
 167                          break;
 168  
 169                      case 'attachments':
 170                          // store attachments
 171                          $i = -1;
 172                          $depth_att = $reader->depth;
 173                          while ( $reader->read() &&
 174                                  ( $reader->depth > $depth_att ||
 175                                    $reader->nodeType != XMLReader::END_ELEMENT ) ) {
 176                              if ( $reader->nodeType == XMLReader::ELEMENT ) {
 177                                  if ($reader->localName == 'attachment') {
 178                                      $i++;
 179                                  }
 180                                  switch ( $reader->localName ) {
 181                                      default:
 182                                          $field = $reader->localName;
 183                                          $reader->read( );
 184                                          $t_attachments[$i]->$field = $reader->value;
 185                                  }
 186                              }
 187                          }
 188                          break;
 189  
 190                      default:
 191                          $field = $reader->localName;
 192  
 193                          //echo "using default handler for field: $field\n";
 194                          $reader->read( );
 195                          $this->newbug_->$field = $reader->value;
 196                  }
 197              }
 198          }
 199  
 200          // now save the new bug
 201          $this->new_id_ = $this->newbug_->create();
 202  
 203          // add custom fields
 204          if ( $this->new_id_ > 0 && is_array( $t_custom_fields ) && count( $t_custom_fields ) > 0 ) {
 205              foreach ( $t_custom_fields as $t_custom_field) {
 206                  $t_custom_field_id = custom_field_get_id_from_name( $t_custom_field->name );
 207                  if ( custom_field_ensure_exists( $t_custom_field_id ) && custom_field_is_linked( $t_custom_field_id, $t_project_id ) ) {
 208                      custom_field_set_value( $t_custom_field->id, $this->new_id_, $t_custom_field->value );
 209                  }
 210                  else {
 211                      error_parameters( $t_custom_field->name, $t_custom_field_id );
 212                      trigger_error( ERROR_CUSTOM_FIELD_NOT_LINKED_TO_PROJECT, ERROR );
 213                  }
 214              }
 215          }
 216  
 217          // add bugnotes
 218          if ( $this->new_id_ > 0 && is_array( $t_bugnotes ) && count( $t_bugnotes ) > 0 ) {
 219              foreach ( $t_bugnotes as $t_bugnote) {
 220                  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 );
 221              }
 222          }
 223  
 224          // add attachments
 225          if ( $this->new_id_ > 0 && is_array( $t_attachments ) && count( $t_attachments ) > 0 ) {
 226              foreach ( $t_attachments as $t_attachment) {
 227                  // Create a temporary file in the temporary files directory using sys_get_temp_dir()
 228                  $temp_file_name = tempnam( sys_get_temp_dir(), 'MantisImport' );
 229                  file_put_contents( $temp_file_name, base64_decode( $t_attachment->content ) );
 230                  $file_data = array( 'name' => $t_attachment->filename,
 231                                      'type' => $t_attachment->file_type,
 232                                      'tmp_name' => $temp_file_name,
 233                                      'size' => filesize( $temp_file_name ) );
 234                  // unfortunately we have no clue who has added the attachment (this could only be fetched from history -> feel free to implement this)
 235                  // also I have no clue where description should come from...
 236                  file_add( $this->new_id_, $file_data, 'bug', $t_attachment->title, $p_desc = '', $p_user_id = null, $t_attachment->date_added, true );
 237                  unlink( $temp_file_name );
 238              }
 239          }
 240  
 241          //echo "\nnew bug: $this->new_id_\n";
 242      }
 243  
 244  	public function update_map( Mapper $mapper ) {
 245          $mapper->add( 'issue', $this->old_id_, $this->new_id_ );
 246      }
 247  
 248  
 249  	public function dumpbug( ) {
 250          var_dump( $this->newbug_ );
 251          var_dump( $this->issueMap );
 252      }
 253  
 254      /**
 255      * Return the user id in the destination tracker
 256      *
 257      * Current logic is: try to find the same user by username;
 258      * if it fails, use $squash_userid
 259      *
 260      * @param $field string bugdata filed to update
 261      * @param $username string username as imported
 262      * @param $squash_userid integer fallback userid
 263      */
 264  	private function get_user_id( $username, $squash_userid = 0 ) {
 265          $t_user_id = user_get_id_by_name( $username );
 266          if( $t_user_id === false ) {
 267              // user not found by username -> check real name
 268              // keep in mind that the setting config_get( 'show_realname' ) may differ between import and export system!
 269              $t_user_id = user_get_id_by_realname( $username );
 270              if ( $t_user_id === false ) {
 271                  //not found
 272                  $t_user_id = $squash_userid;
 273              }
 274          }
 275          return $t_user_id;
 276      }
 277  }


Generated: Thu Jul 28 15:48:31 2011 Cross-referenced by PHPXref 0.7