Skip to content

Commit

Permalink
Additionally store recording merge information in edits.
Browse files Browse the repository at this point in the history
  • Loading branch information
ianmcorvidae committed Feb 21, 2014
1 parent b630292 commit 0e41a26
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 35 deletions.
26 changes: 25 additions & 1 deletion lib/MusicBrainz/Server/Controller/Release.pm
Expand Up @@ -23,6 +23,7 @@ use MusicBrainz::Server::Constants qw( :edit_type );
use MusicBrainz::Server::ControllerUtils::Delete qw( cancel_or_action );
use Scalar::Util qw( looks_like_number );
use MusicBrainz::Server::Data::Utils qw( partial_date_to_hash artist_credit_to_ref );
use MusicBrainz::Server::Edit::Utils qw( calculate_recording_merges );

use aliased 'MusicBrainz::Server::Entity::Work';

Expand Down Expand Up @@ -530,7 +531,30 @@ sub _merge_parameters {
mediums => $medium_changes{$_}
}, keys %medium_changes
]
)
);
} elsif ($form->field('merge_strategy')->value == $MusicBrainz::Server::Data::Release::MERGE_MERGE) {
my %release_map = map { $_->id => $_ } @$releases;

my $new_id = $form->field('target')->value;
my $new = $release_map{$new_id};
my $old = [map { $release_map{$_} } grep { $_ != $new_id } @{ $form->field('merging')->value }];

my $recording_merges = [map +{
medium => $_->{medium},
track => $_->{track},
destination => {
id => $_->{destination}->id,
name => $_->{destination}->name,
length => $_->{destination}->length
},
sources => [map +{
id => $_->id,
name => $_->name,
length => $_->length
}, @{ $_->{sources} }]
}, @{ calculate_recording_merges($new, $old) } ];

return (recording_merges => $recording_merges);
} else {
return ()
}
Expand Down
71 changes: 40 additions & 31 deletions lib/MusicBrainz/Server/Edit/Release/Merge.pm
Expand Up @@ -5,6 +5,7 @@ use List::AllUtils qw( any );
use MusicBrainz::Server::Constants qw( $EDIT_RELEASE_MERGE );
use MusicBrainz::Server::Edit::Exceptions;
use MusicBrainz::Server::Edit::Types qw( Nullable PartialDateHash ArtistCreditDefinition );
use MusicBrainz::Server::Edit::Utils qw( calculate_recording_merges );
use MusicBrainz::Server::Translation qw ( N_l );
use Try::Tiny;

Expand All @@ -29,6 +30,8 @@ use aliased 'MusicBrainz::Server::Entity::Label';

use aliased 'MusicBrainz::Server::Entity::ArtistCredit';

use aliased 'MusicBrainz::Server::Entity::Recording';

has '+data' => (
isa => Dict[
new_entity => Dict[
Expand Down Expand Up @@ -88,7 +91,21 @@ has '+data' => (
old_name => Nullable[Str],
new_name => Nullable[Str],
]]
]]]
]]],
recording_merges => Nullable[ArrayRef[Dict[
medium => Int,
track => Str,
sources => ArrayRef[Dict[
id => Int,
name => Str,
length => Nullable[Int]
]],
destination => Dict[
id => Int,
name => Str,
length => Nullable[Int]
]
]]]
]
);

Expand Down Expand Up @@ -126,7 +143,8 @@ sub foreign_keys
},
Area => [ map { $_->{country_id} } map { @{ $_->{events} // [] } } @{ $self->data->{old_entities} }, $self->data->{new_entity} ],
Label => [ map { $_->{label}{id} } map { @{ $_->{labels} // [] } } @{ $self->data->{old_entities} }, $self->data->{new_entity} ],
Artist => [ map { $_->{artist}{id} } map { @{ $_->{artist_credit}{names} // [] } } @{ $self->data->{old_entities} }, $self->data->{new_entity} ]
Artist => [ map { $_->{artist}{id} } map { @{ $_->{artist_credit}{names} // [] } } @{ $self->data->{old_entities} }, $self->data->{new_entity} ],
Recording => [ map { $_->{id} } map { $_->{destination}, @{ $_->{sources} } } @{ $self->data->{recording_merges} // [] } ]
};
}

Expand Down Expand Up @@ -199,36 +217,27 @@ override build_display_data => sub
}, @{ $self->data->{medium_changes} }
];
} elsif ($self->data->{merge_strategy} == $MusicBrainz::Server::Data::Release::MERGE_MERGE) {
$self->c->model('Track')->load_for_mediums(
map { $_->all_mediums }
values %{ $loaded->{Release} }
);

$self->c->model('Recording')->load(
map { $_->all_tracks }
map { $_->all_mediums }
values %{ $loaded->{Release} }
);

my $recording_merges = [];
for my $medium ($data->{new}->all_mediums) {
for my $track ($medium->all_tracks) {
try {
my @sources;
for my $source_medium (map { $_->all_mediums } @{ $data->{old} }) {
if ($source_medium->position == $medium->position) {
push @sources, map { $_->recording }
grep { $_->position == $track->position } $source_medium->all_tracks;
}
}
@sources = grep { $_->id != $track->recording->id } @sources;
push(@$recording_merges, {
medium => $medium->position,
track => $track->number,
sources => \@sources,
destination => $track->recording}) if scalar @sources;
};
}
if ($self->data->{recording_merges}) {
$recording_merges = [map +{medium => $_->{medium},
track => $_->{track},
destination => $loaded->{Recording}->{$_->{destination}{id}} // Recording->new(name => $_->{destination}{name}, length => $_->{destination}{length}),
sources => [map { $loaded->{Recording}->{$_->{id}} // Recording->new(name => $_->{name}, length => $_->{length}) } @{ $_->{sources} }]
}, @{ $self->data->{recording_merges} }];
} else {
$self->c->model('Track')->load_for_mediums(
map { $_->all_mediums }
values %{ $loaded->{Release} }
);

$self->c->model('Recording')->load(
map { $_->all_tracks }
map { $_->all_mediums }
values %{ $loaded->{Release} }
);

$recording_merges = calculate_recording_merges($data->{new}, $data->{old});
$data->{merges_are_calculated} = 1;
}
$data->{recording_merges} = $recording_merges;
}
Expand Down
27 changes: 27 additions & 0 deletions lib/MusicBrainz/Server/Edit/Utils.pm
Expand Up @@ -11,6 +11,7 @@ use MusicBrainz::Server::Entity::ArtistCredit;
use MusicBrainz::Server::Entity::ArtistCreditName;
use MusicBrainz::Server::Translation qw( N_l );
use Set::Scalar;
use Try::Tiny;

use aliased 'MusicBrainz::Server::Entity::Artist';
use aliased 'MusicBrainz::Server::Entity::PartialDate';
Expand All @@ -22,6 +23,7 @@ use base 'Exporter';
our @EXPORT_OK = qw(
artist_credit_from_loaded_definition
artist_credit_preview
calculate_recording_merges
changed_relations
changed_display_data
clean_submitted_artist_credits
Expand Down Expand Up @@ -423,6 +425,31 @@ sub merge_set {
return $result_set->members;
}

sub calculate_recording_merges {
my ($new, $old) = @_;
my $recording_merges = [];
for my $medium ($new->all_mediums) {
for my $track ($medium->all_tracks) {
try {
my @sources;
for my $source_medium (map { $_->all_mediums } @{ $old }) {
if ($source_medium->position == $medium->position) {
push @sources, map { $_->recording }
grep { $_->position == $track->position } $source_medium->all_tracks;
}
}
@sources = grep { $_->id != $track->recording->id } @sources;
push(@$recording_merges, {
medium => $medium->position,
track => $track->number,
sources => \@sources,
destination => $track->recording}) if scalar @sources;
};
}
}
return $recording_merges;
}

1;

=head1 COPYRIGHT
Expand Down
8 changes: 5 additions & 3 deletions root/edit/details/merge_releases.tt
Expand Up @@ -107,20 +107,22 @@
[%~ SET ev = loop.count % 2 ~%]
<tr[% IF ev == 0 %] class="ev"[% END %]>
<td[% IF rowspan > 1 %] rowspan=[% rowspan %][% END %]>[% merge.medium %].[% merge.track %]</td>
<td>[% link_recording(merge.sources.0) %]</td>
<td>[% link_entity(merge.sources.0) %]</td>
<td>[% merge.sources.0.length | format_length %]</td>
<td[% IF rowspan > 1 %] rowspan=[% rowspan %][% END %]>[% link_recording(merge.destination) %]</td>
<td[% IF rowspan > 1 %] rowspan=[% rowspan %][% END %]>[% link_entity(merge.destination) %]</td>
<td[% IF rowspan > 1 %] rowspan=[% rowspan %][% END %]>[% merge.destination.length | format_length %]</td>
</tr>
[% FOR source = merge.sources ~%]
[%~ UNLESS loop.first ~%]
<tr[% IF ev == 0 %] class="ev"[% END %]>
<td>[% link_recording(source) %]</td>
<td>[% link_entity(source) %]</td>
<td>[% source.length | format_length %]</td>
</tr>
[%~ END; END %]
[%~ END ~%]
</tbody></table>
[%~ ELSIF edit.display_data.merges_are_calculated && !edit.is_open ~%]
<p><strong>[% l('This edit does not store recording merge information and is closed, so no recording merge information can be shown.') %]</strong></p>
[%~ ELSE ~%]
<p><strong>[% l('All recordings for these releases are already merged.') %]</strong></p>
[%~ END ~%]
Expand Down

0 comments on commit 0e41a26

Please sign in to comment.