} elsif( $c->request->method eq 'DELETE' ) {
my $node = $c->request->param('source_id');
my $target = $c->request->param('target_id');
-
+ my $scopewide = $c->request->param('scopewide')
+ && $c->request->param('scopewide') eq 'true';
try {
- my @vectors = $collation->del_relationship( $node, $target );
+ my @vectors = $collation->del_relationship( $node, $target, $scopewide );
$m->save( $tradition );
$c->stash->{'result'} = \@vectors;
} catch( Text::Tradition::Error $e ) {
}
+=head2 merge
+
+ POST relation/$textid/merge { data }
+
+Merges the requested readings, combining the witnesses of both readings into
+the target reading. All non-conflicting source relationships are inherited by
+the target relationship.
+
+=cut
+
+sub merge :Chained('text') :PathPart :Args(0) {
+ my( $self, $c ) = @_;
+ my $tradition = delete $c->stash->{'tradition'};
+ my $collation = $tradition->collation;
+ my $m = $c->model('Directory');
+ if( $c->request->method eq 'POST' ) {
+ if( $c->stash->{'permission'} ne 'full' ) {
+ $c->response->status( '403' );
+ $c->stash->{'result'} = {
+ 'error' => 'You do not have permission to modify this tradition.' };
+ $c->detach('View::JSON');
+ return;
+ }
+ my $errmsg;
+ my $response;
+
+ my $main = $c->request->param('target_id');
+ my $second = $c->request->param('source_id');
+ # Find the common successor of these, so that we can detect other
+ # potentially identical readings.
+ my $csucc = $collation->common_successor( $main, $second );
+
+ # Try the merge if these are parallel readings.
+ if( $csucc->id eq $main || $csucc->id eq $second ) {
+ $errmsg = "Cannot merge readings in the same path";
+ } else {
+ try {
+ $collation->merge_readings( $main, $second );
+ } catch( Text::Tradition::Error $e ) {
+ $c->response->status( '403' );
+ $errmsg = $e->message;
+ } catch {
+ # Something else went wrong, probably a Moose error
+ $c->response->status( '403' );
+ $errmsg = 'Something went wrong with the request';
+ }
+ }
+
+ # Look for readings that are now identical.
+ if( $errmsg ) {
+ $response = { status => 'error', error => $errmsg };
+ } else {
+ $response = { status => 'ok' };
+ unless( $c->request->param('single') ) {
+ my @identical = $collation->identical_readings(
+ start => $main, end => $csucc->id );
+ if( @identical ) {
+ $response->{'checkalign'} = [
+ map { [ $_->[0]->id, $_->[1]->id ] } @identical ];
+ }
+ }
+ $m->save( $collation );
+ }
+ $c->stash->{'result'} = $response;
+ $c->forward('View::JSON');
+ }
+}
+
=head2 duplicate
- POST relation/$textid/duplicate/$id/ { witnesses }
+ POST relation/$textid/duplicate { data }
-Duplicates the given reading, detaching the witnesses specified in the list to use
-the new reading instead of the old. The 'witnesses' param should be a JSON array.
+Duplicates the requested readings, detaching the witnesses specified in
+the list to use the new reading(s) instead of the old. The data to be
+passed should be a JSON structure:
+
+ { readings: rid1,rid2,rid3,...
+ witnesses: [ wit1, ... ] }
=cut
-sub duplicate :Chained('text') :PathPart :Args(1) {
- my( $self, $c, $reading_id ) = @_;
+sub duplicate :Chained('text') :PathPart :Args(0) {
+ my( $self, $c ) = @_;
my $tradition = delete $c->stash->{'tradition'};
my $collation = $tradition->collation;
- my $rdg = $collation->reading( $reading_id );
my $m = $c->model('Directory');
if( $c->request->method eq 'POST' ) {
if( $c->stash->{'permission'} ne 'full' ) {
}
my $errmsg;
my $response = {};
- if( $c->request->param('witnesses') ) {
- my $witlist = from_json( $c->request->param('witnesses') );
+ # Sort out which readings need to be duplicated from the set given, and
+ # ensure that all the given wits bear each relevant reading.
+
+ my %wits = ();
+ map { $wits{$_} = 1 } $c->request->param('witnesses[]');
+ my %rdgranks = ();
+ foreach my $rid ( $c->request->param('readings[]') ) {
+ my $numwits = 0;
+ my $rdg = $collation->reading( $rid );
+ foreach my $rwit ( $rdg->witnesses( $rid ) ) {
+ $numwits++ if exists $wits{$rwit};
+ }
+ next unless $numwits; # Disregard readings with none of our witnesses
+ if( $numwits < keys( %wits ) ) {
+ $errmsg = "Reading $rid contains some but not all of the specified witnesses.";
+ last;
+ } elsif( exists $rdgranks{ $rdg->rank } ) {
+ $errmsg = "More than one reading would be detached along with $rid at rank " . $rdg->rank;
+ last;
+ } else {
+ $rdgranks{ $rdg->rank } = $rid;
+ }
+ }
+
+ # Now check that the readings make a single sequence.
+ unless( $errmsg ) {
+ my $prior;
+ foreach my $rank ( sort { $a <=> $b } keys %rdgranks ) {
+ my $rid = $rdgranks{$rank};
+ if( $prior ) {
+ # Check that there is only one path between $prior and $rdg.
+ foreach my $wit ( keys %wits ) {
+ unless( $collation->prior_reading( $rid, $wit ) eq $prior ) {
+ $errmsg = "Diverging witness paths from $prior to $rid at $wit";
+ last;
+ }
+ }
+ }
+ $prior = $rid;
+ }
+ }
+
+ # Abort if we've run into a problem.
+ if( $errmsg ) {
+ $c->stash->{'result'} = { 'error' => $errmsg };
+ $c->response->status( '403' );
+ $c->forward('View::JSON');
+ return;
+ }
+
+ # Otherwise, do the dirty work.
+ my @witlist = keys %wits;
+ my @deleted_relations;
+ foreach my $rank ( sort { $a <=> $b } keys %rdgranks ) {
my $newrdg;
+ my $reading_id = $rdgranks{$rank};
+ my @delrels;
try {
- $newrdg = $collation->duplicate_reading( $reading_id, @$witlist );
+ ( $newrdg, @delrels ) =
+ $collation->duplicate_reading( $reading_id, @witlist );
} catch( Text::Tradition::Error $e ) {
$c->response->status( '403' );
$errmsg = $e->message;
$errmsg = 'Something went wrong with the request';
}
if( $newrdg ) {
- $response = { reading => $newrdg->id, witnesses => $witlist };
+ my $data = _reading_struct( $newrdg );
+ $data->{'orig_rdg'} = $reading_id;
+ $response->{"$newrdg"} = $data;
+ push( @deleted_relations, @delrels );
}
+ }
+ if( $errmsg ) {
+ $c->stash->{'result'} = { 'error' => $errmsg };
} else {
- $c->response->status( '403' );
- $errmsg = "At least one witness must be specified for a duplication";
+ $m->save( $collation );
+ $response->{'DELETED'} = \@deleted_relations;
+ $c->stash->{'result'} = $response;
}
- $c->stash->{'result'} = $errmsg ? { 'error' => $errmsg } : $response;
}
$c->forward('View::JSON');
}