Skip to content

Commit

Permalink
XML Import: use new method to replace bug links
Browse files Browse the repository at this point in the history
A new private method replaceLinks() was added to avoid code duplication.
Now replacement of buglinks for 'description', 'steps_to_reproduce' and
'additional_information' is performed via this method.
  • Loading branch information
dregad committed Nov 7, 2014
1 parent 4350b4d commit 8e111ab
Showing 1 changed file with 41 additions and 27 deletions.
68 changes: 41 additions & 27 deletions plugins/XmlImportExport/ImportXml.php
Expand Up @@ -171,33 +171,14 @@ public function import() {
printf( 'Processing cross-references for %s issues...', count( $t_imported_issues ) );
foreach( $t_imported_issues as $t_old_id => $t_new_id ) {
$t_bug = bug_get( $t_new_id, true );
$t_content_replaced = false;
$t_bug_link_regexp = '/(^|[^\w])(' . preg_quote( $this->source_->issuelink, '/' ) . ')(\d+)\b/';

# replace links in description
preg_match_all( $t_bug_link_regexp, $t_bug->description, $t_matches );
if( is_array( $t_matches[3] ) && count( $t_matches[3] ) > 0 ) {
$t_content_replaced = true;
foreach ( $t_matches[3] as $t_old_id2 ) {
$t_bug->description = str_replace( $this->source_->issuelink . $t_old_id2, $this->getReplacementString( $this->source_->issuelink, $t_old_id2 ), $t_bug->description );
}
}
# replace links in steps to reproduce
preg_match_all( $t_bug_link_regexp, $t_bug->steps_to_reproduce, $t_matches );
if( is_array( $t_matches[3] ) && count( $t_matches[3] ) > 0 ) {
$t_content_replaced = true;
foreach ( $t_matches[3] as $t_old_id2 ) {
$t_bug->steps_to_reproduce = str_replace( $this->source_->issuelink . $t_old_id2, $this->getReplacementString( $this->source_->issuelink, $t_old_id2 ), $t_bug->steps_to_reproduce );
}
}
# replace links in additional information
preg_match_all( $t_bug_link_regexp, $t_bug->additional_information, $t_matches );
if( is_array( $t_matches[3] ) && count( $t_matches[3] ) > 0 ) {
$t_content_replaced = true;
foreach ( $t_matches[3] as $t_old_id3 ) {
$t_bug->additional_information = str_replace( $this->source_->issuelink . $t_old_id3, $this->getReplacementString( $this->source_->issuelink, $t_old_id3 ), $t_bug->additional_information );
}
}

# Using bitwise 'or' here to ensure the all replacements are made
# regardless of outcome of the previous one(s)
$t_content_replaced =
$this->replaceLinks( $t_bug, 'description' )
| $this->replaceLinks( $t_bug, 'steps_to_reproduce' )
| $this->replaceLinks( $t_bug, 'additional_information' );

if( $t_content_replaced ) {
# only update bug if necessary (otherwise last update date would be unnecessarily overwritten)
$t_bug->update( true );
Expand All @@ -208,6 +189,39 @@ public function import() {
echo " Done\n";
}

/**
* Replace links in the given bug for the specified field
* @param object $p_bug
* @param string $p_field Field to process (one of 'description',
* 'steps_to_reproduce' or 'additional_information')
* @return boolean true if replacements have been made
*/
private function replaceLinks( $p_bug, $p_field ) {
static $s_bug_link_regexp;
$t_content_replaced = false;

if( is_null( $s_bug_link_regexp ) ) {
$s_bug_link_regexp = '/(?:^|[^\w])'
. preg_quote( $this->source_->issuelink, '/' )
. '(\d+)\b/';
}

preg_match_all( $s_bug_link_regexp, $p_bug->$p_field, $t_matches );

if( is_array( $t_matches[1] ) && count( $t_matches[1] ) > 0 ) {
$t_content_replaced = true;
foreach ( $t_matches[1] as $t_old_id ) {
$p_bug->$p_field = str_replace(
$this->source_->issuelink . $t_old_id,
$this->getReplacementString( $this->source_->issuelink, $t_old_id ),
$p_bug->$p_field
);
}
}

return $t_content_replaced;
}

/**
* Compute and return the new link
*
Expand Down

0 comments on commit 8e111ab

Please sign in to comment.