package stemmaweb::Controller::Relation;
+use JSON qw/ to_json from_json /;
use Moose;
use Module::Load;
use namespace::autoclean;
$c->stash->{'template'} = 'relate.tt';
}
-=head2 definitions
-
- GET relation/definitions
-
-Returns a data structure giving the valid types and scopes for a relationship.
-
-=cut
-
-sub definitions :Local :Args(0) {
- my( $self, $c ) = @_;
- my $valid_relationships = [ qw/ spelling orthographic grammatical lexical transposition / ];
- my $valid_scopes = [ qw/ local global / ];
- $c->stash->{'result'} = { 'types' => $valid_relationships, 'scopes' => $valid_scopes };
- $c->forward('View::JSON');
-}
-
=head2 text
GET relation/$textid/
my $ok = _check_permission( $c, $tradition );
return unless $ok;
+ $c->stash->{'textid'} = $textid;
+ $c->stash->{'tradition'} = $tradition;
+}
+
+sub main :Chained('text') :PathPart('') :Args(0) {
+ my( $self, $c ) = @_;
+ my $tradition = delete $c->stash->{'tradition'};
+ my $collation = $tradition->collation;
+
+ # Stash the relationship definitions
+ $c->stash->{'relationship_scopes'} = to_json( [ qw/ local global / ] );
+ my @reltypeinfo;
+ foreach my $type ( sort { _typesort( $a, $b ) } $collation->relations->types ) {
+ next if $type->is_weak;
+ my $struct = { name => $type->name, description => $type->description };
+ push( @reltypeinfo, $struct );
+ }
+ $c->stash->{'relationship_types'} = to_json( \@reltypeinfo );
+
# See how big the tradition is. Edges are more important than nodes
# when it comes to rendering difficulty.
- my $numnodes = scalar $tradition->collation->readings;
- my $numedges = scalar $tradition->collation->paths;
- my $length = $tradition->collation->end->rank;
+ my $numnodes = scalar $collation->readings;
+ my $numedges = scalar $collation->paths;
+ my $length = $collation->end->rank;
# We should display no more than roughly 500 nodes, or roughly 700
# edges, at a time.
my $segments = $numnodes / 500;
$r += $segsize;
}
$c->stash->{'textsegments'} = [];
- $c->stash->{'segsize'} = $segsize;
- $c->stash->{'margin'} = $margin;
foreach my $i ( 0..$#divs ) {
my $seg = { 'start' => $divs[$i] };
$seg->{'display'} = "Segment " . ($i+1);
push( @{$c->stash->{'textsegments'}}, $seg );
}
}
- $c->stash->{'textid'} = $textid;
- $c->stash->{'tradition'} = $tradition;
-}
-
-sub main :Chained('text') :PathPart('') :Args(0) {
- my( $self, $c ) = @_;
my $startseg = $c->req->param('start');
- my $tradition = delete $c->stash->{'tradition'};
- my $collation = $tradition->collation;
my $svgopts;
if( $startseg ) {
# Only render the subgraph from startseg to endseg or to END,
# whichever is less.
- my $endseg = $startseg + $c->stash->{'segsize'} + $c->stash->{'margin'};
+ my $endseg = $startseg + $segsize + $margin;
$svgopts = { 'from' => $startseg };
$svgopts->{'to'} = $endseg if $endseg < $collation->end->rank;
} elsif( exists $c->stash->{'textsegments'} ) {
# This is the unqualified load of a long tradition. We implicitly start
- # at zero, but go only as far as 550.
- my $endseg = $c->stash->{'segsize'} + $c->stash->{'margin'};
+ # at zero, but go only as far as our segment size.
+ my $endseg = $segsize + $margin;
$startseg = 0;
$svgopts = { 'to' => $endseg };
}
+ # Spit out the SVG
my $svg_str = $collation->as_svg( $svgopts );
$svg_str =~ s/\n//gs;
$c->stash->{'startseg'} = $startseg if defined $startseg;
$c->stash->{'template'} = 'relate.tt';
}
+sub _typesort {
+ my( $a, $b ) = @_;
+ my $blsort = $a->bindlevel <=> $b->bindlevel;
+ return $blsort if $blsort;
+ return $a->name cmp $b->name;
+}
+
=head2 help
GET relation/help/$language
if( $c->stash->{'permission'} ne 'full' ) {
$c->response->status( '403' );
$c->stash->{'result'} = {
- 'error' => 'You do not have permission to view this tradition.' };
+ 'error' => 'You do not have permission to modify this tradition.' };
+ $c->detach( 'View::JSON' );
} elsif( $c->request->method eq 'POST' ) {
- unless( $c->stash->{'permission'} eq 'full' ) {
- $c->response->status( '403' );
- $c->stash->{'result'} = {
- 'error' => 'You do not have permission to view this tradition.' };
- $c->detach( 'View::JSON' );
- }
my $node = $c->request->param('source_id');
my $target = $c->request->param('target_id');
my $relation = $c->request->param('rel_type');
} 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 ) {
my $t = $_[0]->type;
return $t eq 'spelling' || $t eq 'orthographic';
};
- my @variants;
- foreach my $sr ( $reading->related_readings( $sameword ) ) {
- push( @variants, $sr->text );
- }
- $struct->{'variants'} = \@variants;
+ # Now add the list data
+ $struct->{'variants'} = [ map { $_->text } $reading->related_readings( $sameword ) ];
+ $struct->{'witnesses'} = [ $reading->witnesses ];
return $struct;
}
if( $c->stash->{'permission'} ne 'full' ) {
$c->response->status( '403' );
$c->stash->{'result'} = {
- 'error' => 'You do not have permission to view this tradition.' };
+ 'error' => 'You do not have permission to modify this tradition.' };
$c->detach('View::JSON');
return;
}
}
+=head2 duplicate
+
+ POST relation/$textid/duplicate { data }
+
+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(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 = {};
+ # 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};
+ }
+ if( $numwits > 0 && $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;
+ foreach my $rank ( sort { $a <=> $b } keys %rdgranks ) {
+ my $newrdg;
+ my $reading_id = $rdgranks{$rank};
+ try {
+ $newrdg = $collation->duplicate_reading( $reading_id, @witlist );
+ } 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';
+ }
+ if( $newrdg ) {
+ my $data = _reading_struct( $newrdg );
+ $data->{'orig_rdg'} = $reading_id;
+ $response->{"$newrdg"} = $data;
+ }
+ }
+ if( $errmsg ) {
+ $c->stash->{'result'} = { 'error' => $errmsg };
+ } else {
+ $m->save( $collation );
+ $c->stash->{'result'} = $response;
+ }
+ }
+ $c->forward('View::JSON');
+}
+
+
+
sub _check_permission {
my( $c, $tradition ) = @_;
my $user = $c->user_exists ? $c->user->get_object : undef;