Changeset 2716

Show
Ignore:
Timestamp:
07/04/08 09:52:12 (20 months ago)
Author:
auno
Message:

Added tag support for importing WXR file. BugzID:80487

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • branches/release-41/plugins/WXRImporter/lib/WXRImporter/WXRHandler.pm

    r2515 r2716  
    6969    $self->{in_wp_comment_content} = 1 if ('wp' eq $prefix) && ('comment_content' eq $name); 
    7070 
    71     push @{$self->{'bucket'}}, \%values if scalar(%values); 
    72     push @{$self->{'bucket'}}, $prefix . '_' . $name; 
     71    if ( scalar(%values) ) { 
     72        push @{ $self->{'bucket'} }, 
     73          { $prefix . '_' . $name => undef, _a => \%values }; 
     74    } 
     75    else { 
     76        push @{ $self->{'bucket'} }, $prefix . '_' . $name; 
     77    } 
    7378    1; 
    7479} 
     
    8691    my $chars = MT::I18N::utf8_off($data->{Data}); 
    8792    if ('HASH' eq ref($element)) { 
    88         my @hash_array = %$element; 
     93        my @hash_array = grep { $_ ne '_a' } keys %$element; 
    8994        return unless $hash_array[0]; 
    9095        my $val = $element->{$hash_array[0]}; 
     
    119124    } 
    120125    push @{$self->{'bucket'}}, $element; 
    121  
    122126    my $name_element = $prefix . '_' . $name; 
    123127    if ('_item' eq $name_element) { 
     
    127131    } elsif ('wp_category' eq $name_element) { 
    128132        $self->_create_category($data); 
     133    } elsif ('wp_tag' eq $name_element) { 
     134        $self->_create_tag($data); 
    129135    } elsif ('wp_comment' eq $name_element) { 
    130136        $self->_create_feedback($data); 
     
    253259            return die MT->translate( 
    254260                 "Saving category failed: [_1]", $cat->errstr); 
     261        } 
     262    } 
     263} 
     264 
     265sub _create_tag { 
     266    my $self = shift; 
     267    my $data = shift; 
     268 
     269    my $cb   = $self->{callback}; 
     270    my $blog = $self->{blog}; 
     271 
     272    require MT::Tag; 
     273    my $tag = MT::Tag->new; 
     274    my $set_name; 
     275    while ( my $hash = pop @{ $self->{'bucket'} } ) { 
     276        last if 'wp_tag' eq $hash; 
     277        next if 'HASH' ne ref $hash; 
     278        my @hash_array = %$hash; 
     279        my $key        = $hash_array[0]; 
     280        my $value      = $hash_array[1]; 
     281        if ( 'wp_tag_name' eq $key ) { 
     282            return if ( MT::Tag->load( { name => $value } ) ); 
     283            $tag->name($value); 
     284            $set_name = 1; 
     285        } 
     286    } 
     287    if ($set_name) { 
     288        $cb->( MT->translate( "Creating new tag ('[_1]')...", $tag->name ) ); 
     289        if ( $tag->save ) { 
     290            $cb->( MT->translate("ok") . "\n" ); 
     291        } 
     292        else { 
     293            $cb->( MT->translate("failed") . "\n" ); 
     294            return die MT->translate( "Saving tag failed: [_1]", $tag->errstr ); 
    255295        } 
    256296    } 
     
    451491    }; 
    452492    my %meta_hash; 
     493    my @tags; 
    453494 
    454495    my $class = MT->model($class_type); 
     
    462503    $post->status($blog->status_default); 
    463504    for my $hash (@$hashes) { 
    464         my @hash_array = %$hash; 
     505        my @hash_array = grep { $_ ne '_a' } keys %$hash; 
    465506        my $key = $hash_array[0]; 
    466         my $value = $hash_array[1]; 
     507        my $value = $hash->{ $hash_array[0] }; 
    467508        if ('_title' eq $key) { 
    468509            $post->title($value); 
     
    482523                $cat_ids{$cat->id} = 1; 
    483524                $primary_cat_id = $cat->id unless $primary_cat_id; 
     525            } 
     526            if ( $hash->{_a} ) { 
     527                if ( $hash->{_a}->{domain} eq 'tag' ) { 
     528                    push @tags, $value; 
     529                } 
    484530            } 
    485531        } elsif ('_guid' eq $key) { 
     
    589635    } 
    590636 
     637    # Associate tags to the entry. 
     638    if (@tags) { 
     639        $post->set_tags(@tags); 
     640    } 
     641 
    591642    # Now save the entry/page. 
    592643    if ('entry' eq $class_type) {