Implementation of merge back-end & UI
[scpubgit/stemmaweb.git] / lib / stemmaweb / Controller / Relation.pm
CommitLineData
b8a92065 1package stemmaweb::Controller::Relation;
5539cba3 2use JSON qw/ to_json from_json /;
b8a92065 3use Moose;
e4bdf660 4use Moose::Util::TypeConstraints qw/ find_type_constraint /;
cc86fa11 5use Module::Load;
b8a92065 6use namespace::autoclean;
e4bdf660 7use Text::Tradition::Datatypes;
b28e606e 8use TryCatch;
b8a92065 9
10BEGIN { extends 'Catalyst::Controller' }
11
12
13=head1 NAME
14
15stemmaweb::Controller::Relation - Controller for the relationship mapper
16
17=head1 DESCRIPTION
18
b28e606e 19The reading relationship mapper with draggable nodes.
b8a92065 20
21=head1 METHODS
22
b28e606e 23=head2 index
24
b8a92065 25 GET relation/$textid
26
27Renders the application for the text identified by $textid.
28
b8a92065 29=cut
30
9529f69c 31sub index :Path :Args(0) {
32 my( $self, $c ) = @_;
b28e606e 33 $c->stash->{'template'} = 'relate.tt';
34}
35
9529f69c 36=head2 text
b28e606e 37
9529f69c 38 GET relation/$textid/
39
40 Runs the relationship mapper for the specified text ID.
41
b28e606e 42=cut
43
9529f69c 44sub text :Chained('/') :PathPart('relation') :CaptureArgs(1) {
45 my( $self, $c, $textid ) = @_;
13aa153c 46 my $tradition = $c->model('Directory')->tradition( $textid );
cd3f7f55 47 unless( $tradition ) {
48 $c->response->status('404');
49 $c->response->body("No such tradition with ID $textid");
50 $c->detach('View::Plain');
51 return;
52 }
53
7562a27b 54 # Account for a bad interaction between FastCGI and KiokuDB
55 unless( $tradition->collation->tradition ) {
56 $c->log->warn( "Fixing broken tradition link" );
57 $tradition->collation->_set_tradition( $tradition );
58 $c->model('Directory')->save( $tradition );
59 }
20198e59 60 # Check permissions. Will return 403 if denied, otherwise will
61 # put the appropriate value in the stash.
62 my $ok = _check_permission( $c, $tradition );
63 return unless $ok;
64
8843c8b9 65 $c->stash->{'textid'} = $textid;
66 $c->stash->{'tradition'} = $tradition;
67}
68
69sub main :Chained('text') :PathPart('') :Args(0) {
70 my( $self, $c ) = @_;
71 my $tradition = delete $c->stash->{'tradition'};
72 my $collation = $tradition->collation;
89aae3ee 73
74 # Stash text direction to use in JS.
75 $c->stash->{'direction'} = $collation->direction;
76
56e3972e 77 # Stash the relationship definitions
e4bdf660 78 $c->stash->{'relationship_scopes'} =
79 to_json( find_type_constraint( 'RelationshipScope' )->values );
80 $c->stash->{'ternary_values'} =
81 to_json( find_type_constraint( 'Ternary' )->values );
56e3972e 82 my @reltypeinfo;
83 foreach my $type ( sort { _typesort( $a, $b ) } $collation->relations->types ) {
84 next if $type->is_weak;
85 my $struct = { name => $type->name, description => $type->description };
86 push( @reltypeinfo, $struct );
87 }
88 $c->stash->{'relationship_types'} = to_json( \@reltypeinfo );
89
d58766c0 90 # See how big the tradition is. Edges are more important than nodes
91 # when it comes to rendering difficulty.
8843c8b9 92 my $numnodes = scalar $collation->readings;
93 my $numedges = scalar $collation->paths;
94 my $length = $collation->end->rank;
d58766c0 95 # We should display no more than roughly 500 nodes, or roughly 700
96 # edges, at a time.
97 my $segments = $numnodes / 500;
98 if( $numedges / 700 > $segments ) {
99 $segments = $numedges / 700;
100 }
101 my $segsize = sprintf( "%.0f", $length / $segments );
102 my $margin = sprintf( "%.0f", $segsize / 10 );
103 if( $segments > 1 ) {
13aa153c 104 # Segment the tradition in order not to overload the browser.
13aa153c 105 my @divs;
106 my $r = 0;
d58766c0 107 while( $r + $margin < $length ) {
13aa153c 108 push( @divs, $r );
d58766c0 109 $r += $segsize;
13aa153c 110 }
111 $c->stash->{'textsegments'} = [];
ea8e8b3c 112 foreach my $i ( 0..$#divs ) {
113 my $seg = { 'start' => $divs[$i] };
114 $seg->{'display'} = "Segment " . ($i+1);
13aa153c 115 push( @{$c->stash->{'textsegments'}}, $seg );
116 }
117 }
13aa153c 118 my $startseg = $c->req->param('start');
13aa153c 119 my $svgopts;
120 if( $startseg ) {
d58766c0 121 # Only render the subgraph from startseg to endseg or to END,
13aa153c 122 # whichever is less.
8843c8b9 123 my $endseg = $startseg + $segsize + $margin;
13aa153c 124 $svgopts = { 'from' => $startseg };
d58766c0 125 $svgopts->{'to'} = $endseg if $endseg < $collation->end->rank;
13aa153c 126 } elsif( exists $c->stash->{'textsegments'} ) {
127 # This is the unqualified load of a long tradition. We implicitly start
8843c8b9 128 # at zero, but go only as far as our segment size.
129 my $endseg = $segsize + $margin;
ea8e8b3c 130 $startseg = 0;
d58766c0 131 $svgopts = { 'to' => $endseg };
13aa153c 132 }
8843c8b9 133 # Spit out the SVG
13aa153c 134 my $svg_str = $collation->as_svg( $svgopts );
9529f69c 135 $svg_str =~ s/\n//gs;
ea8e8b3c 136 $c->stash->{'startseg'} = $startseg if defined $startseg;
9529f69c 137 $c->stash->{'svg_string'} = $svg_str;
138 $c->stash->{'text_title'} = $tradition->name;
d935aef8 139 if( $tradition->can('language') && $tradition->language ) {
487674b9 140 $c->stash->{'text_lang'} = $tradition->language;
141 $c->stash->{'can_morphologize'} = 1;
142 } else {
143 $c->stash->{'text_lang'} = 'Default';
144 }
9529f69c 145 $c->stash->{'template'} = 'relate.tt';
b28e606e 146}
147
56e3972e 148sub _typesort {
149 my( $a, $b ) = @_;
150 my $blsort = $a->bindlevel <=> $b->bindlevel;
151 return $blsort if $blsort;
152 return $a->name cmp $b->name;
8843c8b9 153}
154
cc86fa11 155=head2 help
156
157 GET relation/help/$language
158
159Returns the help window HTML.
160
161=cut
162
163sub help :Local :Args(1) {
164 my( $self, $c, $lang ) = @_;
165 # Display the morphological help for the language if it is defined.
166 if( $lang && $lang ne 'Default' ) {
167 my $mod = 'Text::Tradition::Language::' . $lang;
168 try {
169 load( $mod );
170 } catch {
171 $c->log->debug("Warning: could not load $mod");
172 }
173 my $has_mod = $mod->can('morphology_tags');
cc86fa11 174 if( $has_mod ) {
175 my $tagset = &$has_mod;
176 $c->stash->{'tagset'} = $tagset;
177 }
178 }
179 $c->stash->{'template'} = 'relatehelp.tt';
180}
181
b28e606e 182=head2 relationships
183
13aa153c 184 GET relation/$textid/relationships
9529f69c 185
186Returns the list of relationships defined for this text.
b28e606e 187
13aa153c 188 POST relation/$textid/relationships { request }
9529f69c 189
190Attempts to define the requested relationship within the text. Returns 200 on
191success or 403 on error.
b28e606e 192
13aa153c 193 DELETE relation/$textid/relationships { request }
9529f69c 194
b28e606e 195
196=cut
197
9529f69c 198sub relationships :Chained('text') :PathPart :Args(0) {
b28e606e 199 my( $self, $c ) = @_;
6d124a83 200 my $tradition = delete $c->stash->{'tradition'};
20198e59 201 my $ok = _check_permission( $c, $tradition );
202 return unless $ok;
6d124a83 203 my $collation = $tradition->collation;
cdd592f3 204 my $m = $c->model('Directory');
9529f69c 205 if( $c->request->method eq 'GET' ) {
206 my @pairs = $collation->relationships; # returns the edges
207 my @all_relations;
208 foreach my $p ( @pairs ) {
209 my $relobj = $collation->relations->get_relationship( @$p );
545163a2 210 next if $relobj->type eq 'collated'; # Don't show these
7562a27b 211 next if $p->[0] eq $p->[1]; # HACK until bugfix
eefe56ac 212 my $relhash = { source_id => $p->[0], target_id => $p->[1],
213 source_text => $collation->reading( $p->[0] )->text,
214 target_text => $collation->reading( $p->[1] )->text,
215 type => $relobj->type, scope => $relobj->scope,
216 a_derivable_from_b => $relobj->a_derivable_from_b,
217 b_derivable_from_a => $relobj->b_derivable_from_a,
218 non_independent => $relobj->non_independent,
e4bdf660 219 is_significant => $relobj->is_significant
eefe56ac 220 };
69a19c91 221 $relhash->{'note'} = $relobj->annotation if $relobj->has_annotation;
222 push( @all_relations, $relhash );
9529f69c 223 }
224 $c->stash->{'result'} = \@all_relations;
20198e59 225 } else {
226 # Check write permissions first of all
227 if( $c->stash->{'permission'} ne 'full' ) {
9529f69c 228 $c->response->status( '403' );
20198e59 229 $c->stash->{'result'} = {
56e3972e 230 'error' => 'You do not have permission to modify this tradition.' };
231 $c->detach( 'View::JSON' );
20198e59 232 } elsif( $c->request->method eq 'POST' ) {
eefe56ac 233 my $opts = $c->request->params;
234
235 # Retrieve the source / target from the options
236 my $node = delete $opts->{source_id};
237 my $target = delete $opts->{target_id};
238
239 # Make sure we didn't send a blank or invalid relationship type
240 my $relation = $opts->{type};
241 unless( $collation->get_relationship_type( $relation ) ) {
242 my $errmsg = $relation ? "No such relationship type $relation" :
243 "You must specify a relationship type";
244 $c->stash->{'result'} = { error => $errmsg };
245 $c->response->status( '400' );
246 $c->detach( 'View::JSON' );
247 }
248
249 # Keep the data clean
250 my @booleans = qw/ a_derivable_from_b b_derivable_from_a non_independent /;
251 foreach my $k ( keys %$opts ) {
252 if( $opts->{$k} && grep { $_ eq $k } @booleans ) {
253 $opts->{$k} = 1;
254 }
255 }
20198e59 256
eefe56ac 257 delete $opts->{scope} unless $opts->{scope};
258 delete $opts->{annotation} unless $opts->{annotation};
995efe76 259 delete $opts->{is_significant} unless $opts->{is_significant};
eefe56ac 260 $opts->{propagate} = 1;
20198e59 261
262 try {
263 my @vectors = $collation->add_relationship( $node, $target, $opts );
264 $c->stash->{'result'} = \@vectors;
265 $m->save( $tradition );
266 } catch( Text::Tradition::Error $e ) {
267 $c->response->status( '403' );
995efe76 268 $c->stash->{'result'} = { error => $e->message };
269 } catch {
270 $c->response->status( '500' );
271 $c->stash->{'result'} = { error => "Something went wrong with the request" };
20198e59 272 }
273 } elsif( $c->request->method eq 'DELETE' ) {
274 my $node = $c->request->param('source_id');
275 my $target = $c->request->param('target_id');
088a14af 276 my $scopewide = $c->request->param('scopewide')
277 && $c->request->param('scopewide') eq 'true';
20198e59 278 try {
088a14af 279 my @vectors = $collation->del_relationship( $node, $target, $scopewide );
20198e59 280 $m->save( $tradition );
281 $c->stash->{'result'} = \@vectors;
282 } catch( Text::Tradition::Error $e ) {
283 $c->response->status( '403' );
284 $c->stash->{'result'} = { 'error' => $e->message };
995efe76 285 } catch {
286 $c->response->status( '500' );
287 $c->stash->{'result'} = { error => "Something went wrong with the request" };
288 }
9529f69c 289 }
b28e606e 290 }
b28e606e 291 $c->forward('View::JSON');
5f15640c 292}
293
294=head2 readings
295
296 GET relation/$textid/readings
297
298Returns the list of readings defined for this text along with their metadata.
299
300=cut
301
0dcdd5ec 302my %read_write_keys = (
303 'id' => 0,
304 'text' => 0,
305 'is_meta' => 0,
306 'grammar_invalid' => 1,
307 'is_nonsense' => 1,
308 'normal_form' => 1,
309);
310
5f15640c 311sub _reading_struct {
312 my( $reading ) = @_;
313 # Return a JSONable struct of the useful keys. Keys meant to be writable
314 # have a true value; read-only keys have a false value.
5f15640c 315 my $struct = {};
2be76d3f 316 map { $struct->{$_} = $reading->$_ if $reading->can( $_ ) } keys( %read_write_keys );
5f15640c 317 # Special case
2be76d3f 318 $struct->{'lexemes'} = $reading->can( 'lexemes' ) ? [ $reading->lexemes ] : [];
0dcdd5ec 319 # Look up any words related via spelling or orthography
320 my $sameword = sub {
321 my $t = $_[0]->type;
322 return $t eq 'spelling' || $t eq 'orthographic';
323 };
5539cba3 324 # Now add the list data
325 $struct->{'variants'} = [ map { $_->text } $reading->related_readings( $sameword ) ];
326 $struct->{'witnesses'} = [ $reading->witnesses ];
5f15640c 327 return $struct;
328}
329
330sub readings :Chained('text') :PathPart :Args(0) {
331 my( $self, $c ) = @_;
332 my $tradition = delete $c->stash->{'tradition'};
20198e59 333 my $ok = _check_permission( $c, $tradition );
334 return unless $ok;
5f15640c 335 my $collation = $tradition->collation;
336 my $m = $c->model('Directory');
337 if( $c->request->method eq 'GET' ) {
338 my $rdginfo = {};
339 foreach my $rdg ( $collation->readings ) {
340 $rdginfo->{$rdg->id} = _reading_struct( $rdg );
341 }
342 $c->stash->{'result'} = $rdginfo;
343 }
344 $c->forward('View::JSON');
345}
346
347=head2 reading
348
349 GET relation/$textid/reading/$id
350
351Returns the list of readings defined for this text along with their metadata.
352
353 POST relation/$textid/reading/$id { request }
354
355Alters the reading according to the values in request. Returns 403 Forbidden if
356the alteration isn't allowed.
357
358=cut
359
360sub reading :Chained('text') :PathPart :Args(1) {
361 my( $self, $c, $reading_id ) = @_;
362 my $tradition = delete $c->stash->{'tradition'};
363 my $collation = $tradition->collation;
0dcdd5ec 364 my $rdg = $collation->reading( $reading_id );
5f15640c 365 my $m = $c->model('Directory');
366 if( $c->request->method eq 'GET' ) {
5f15640c 367 $c->stash->{'result'} = $rdg ? _reading_struct( $rdg )
368 : { 'error' => "No reading with ID $reading_id" };
369 } elsif ( $c->request->method eq 'POST' ) {
20198e59 370 if( $c->stash->{'permission'} ne 'full' ) {
371 $c->response->status( '403' );
372 $c->stash->{'result'} = {
5539cba3 373 'error' => 'You do not have permission to modify this tradition.' };
20198e59 374 $c->detach('View::JSON');
487674b9 375 return;
20198e59 376 }
6666d111 377 my $errmsg;
487674b9 378 if( $rdg && $rdg->does('Text::Tradition::Morphology') ) {
379 # Are we re-lemmatizing?
380 if( $c->request->param('relemmatize') ) {
381 my $nf = $c->request->param('normal_form');
382 # TODO throw error unless $nf
383 $rdg->normal_form( $nf );
384 # TODO throw error if lemmatization fails
385 # TODO skip this if normal form hasn't changed
386 $rdg->lemmatize();
387 } else {
388 # Set all the values that we have for the reading.
389 # TODO error handling
390 foreach my $p ( keys %{$c->request->params} ) {
391 if( $p =~ /^morphology_(\d+)$/ ) {
392 # Set the form on the correct lexeme
393 my $morphval = $c->request->param( $p );
394 next unless $morphval;
395 my $midx = $1;
396 my $lx = $rdg->lexeme( $midx );
397 my $strrep = $rdg->language . ' // ' . $morphval;
398 my $idx = $lx->has_form( $strrep );
399 unless( defined $idx ) {
400 # Make the word form and add it to the lexeme.
401 try {
402 $idx = $lx->add_matching_form( $strrep ) - 1;
403 } catch( Text::Tradition::Error $e ) {
404 $c->response->status( '403' );
405 $errmsg = $e->message;
406 } catch {
407 # Something else went wrong, probably a Moose error
995efe76 408 $c->response->status( '500' );
487674b9 409 $errmsg = 'Something went wrong with the request';
410 }
6666d111 411 }
487674b9 412 $lx->disambiguate( $idx ) if defined $idx;
413 } elsif( $read_write_keys{$p} ) {
414 my $val = _clean_booleans( $rdg, $p, $c->request->param( $p ) );
415 $rdg->$p( $val );
0dcdd5ec 416 }
487674b9 417 }
418 }
419 $m->save( $rdg );
420 } else {
421 $errmsg = "Reading does not exist or cannot be morphologized";
0dcdd5ec 422 }
6666d111 423 $c->stash->{'result'} = $errmsg ? { 'error' => $errmsg }
424 : _reading_struct( $rdg );
0dcdd5ec 425
5f15640c 426 }
427 $c->forward('View::JSON');
428
429}
b28e606e 430
a51e34c5 431sub compress :Chained('text') :PathPart :Args(0) {
432 my( $self, $c ) = @_;
433 my $tradition = delete $c->stash->{'tradition'};
434 my $collation = $tradition->collation;
435 my $m = $c->model('Directory');
436
437 my @rids = $c->request->param('readings[]');
438 my @readings;
439
440 foreach my $rid (@rids) {
441 my $rdg = $collation->reading( $rid );
442
443 push @readings, $rdg;
444 }
445
446 my $len = scalar @readings;
447
448 if( $c->request->method eq 'POST' ) {
449 if( $c->stash->{'permission'} ne 'full' ) {
450 $c->response->status( '403' );
451 $c->stash->{'result'} = {
452 'error' => 'You do not have permission to modify this tradition.' };
453 $c->detach('View::JSON');
454 return;
455 }
456
457 # Sanity check: first save the original text of each witness.
458 my %origtext;
459 foreach my $wit ( $tradition->witnesses ) {
460 $origtext{$wit->sigil} = $collation->path_text( $wit->sigil );
461 if( $wit->is_layered ) {
462 my $acsig = $wit->sigil . $collation->ac_label;
463 $origtext{$acsig} = $collation->path_text( $acsig );
464 }
465 }
466
467 my $first = 0;
468
469 for (my $i = 0; $i < $len; $i++) {
470 my $rdg = $readings[$i];
471
472 if ($rdg->is_combinable) {
473 $first = $i;
474 last;
475 }
476 }
477
478 for (my $i = $first+1; $i < $len; $i++) {
479 my $rdg = $readings[$first];
480 my $next = $readings[$i];
481
482 last unless $next->is_combinable;
483
484 warn "Joining readings $rdg and $next\n";
485
486 $collation->merge_readings( "$rdg", "$next", 1 );
487 }
488
489 # Finally, make sure we haven't screwed anything up.
490 foreach my $wit ( $tradition->witnesses ) {
491 my $pathtext = $collation->path_text( $wit->sigil );
492 throw( "Text differs for witness " . $wit->sigil )
493 unless $pathtext eq $origtext{$wit->sigil};
494 if( $wit->is_layered ) {
495 my $acsig = $wit->sigil . $collation->ac_label;
496 $pathtext = $collation->path_text( $acsig );
497 throw( "Layered text differs for witness " . $wit->sigil )
498 unless $pathtext eq $origtext{$acsig};
499 }
500 }
501
502 $collation->relations->rebuild_equivalence();
503 $collation->calculate_ranks();
504
505 $m->save($collation);
506
507 $c->stash->{'result'} = {};
508 $c->forward('View::JSON');
509 }
510}
511
b001c73d 512=head2 merge
513
514 POST relation/$textid/merge { data }
515
516Merges the requested readings, combining the witnesses of both readings into
517the target reading. All non-conflicting source relationships are inherited by
518the target relationship.
519
520=cut
521
522sub merge :Chained('text') :PathPart :Args(0) {
523 my( $self, $c ) = @_;
524 my $tradition = delete $c->stash->{'tradition'};
525 my $collation = $tradition->collation;
526 my $m = $c->model('Directory');
527 if( $c->request->method eq 'POST' ) {
528 if( $c->stash->{'permission'} ne 'full' ) {
529 $c->response->status( '403' );
530 $c->stash->{'result'} = {
531 'error' => 'You do not have permission to modify this tradition.' };
532 $c->detach('View::JSON');
533 return;
534 }
535 my $errmsg;
536 my $response;
537
538 my $main = $c->request->param('target_id');
539 my $second = $c->request->param('source_id');
540 # Find the common successor of these, so that we can detect other
541 # potentially identical readings.
542 my $csucc = $collation->common_successor( $main, $second );
543
544 # Try the merge if these are parallel readings.
545 if( $csucc->id eq $main || $csucc->id eq $second ) {
546 $errmsg = "Cannot merge readings in the same path";
547 } else {
548 try {
549 $collation->merge_readings( $main, $second );
550 } catch( Text::Tradition::Error $e ) {
551 $c->response->status( '403' );
552 $errmsg = $e->message;
553 } catch {
554 # Something else went wrong, probably a Moose error
555 $c->response->status( '403' );
556 $errmsg = 'Something went wrong with the request';
557 }
558 }
559
560 # Look for readings that are now identical.
561 if( $errmsg ) {
562 $response = { status => 'error', error => $errmsg };
563 } else {
564 $response = { status => 'ok' };
8880c19d 565 unless( $c->request->param('single') ) {
566 my @identical = $collation->identical_readings(
567 start => $main, end => $csucc->id );
568 if( @identical ) {
569 $response->{'checkalign'} = [
570 map { [ $_->[0]->id, $_->[1]->id ] } @identical ];
571 }
b001c73d 572 }
573 $m->save( $collation );
574 }
575 $c->stash->{'result'} = $response;
576 $c->forward('View::JSON');
577 }
578}
579
5539cba3 580=head2 duplicate
581
fdb37581 582 POST relation/$textid/duplicate { data }
5539cba3 583
fdb37581 584Duplicates the requested readings, detaching the witnesses specified in
585the list to use the new reading(s) instead of the old. The data to be
586passed should be a JSON structure:
587
588 { readings: rid1,rid2,rid3,...
589 witnesses: [ wit1, ... ] }
5539cba3 590
591=cut
592
fdb37581 593sub duplicate :Chained('text') :PathPart :Args(0) {
594 my( $self, $c ) = @_;
5539cba3 595 my $tradition = delete $c->stash->{'tradition'};
596 my $collation = $tradition->collation;
5539cba3 597 my $m = $c->model('Directory');
598 if( $c->request->method eq 'POST' ) {
599 if( $c->stash->{'permission'} ne 'full' ) {
600 $c->response->status( '403' );
601 $c->stash->{'result'} = {
602 'error' => 'You do not have permission to modify this tradition.' };
603 $c->detach('View::JSON');
604 return;
605 }
606 my $errmsg;
607 my $response = {};
fdb37581 608 # Sort out which readings need to be duplicated from the set given, and
609 # ensure that all the given wits bear each relevant reading.
610
611 my %wits = ();
612 map { $wits{$_} = 1 } $c->request->param('witnesses[]');
613 my %rdgranks = ();
614 foreach my $rid ( $c->request->param('readings[]') ) {
615 my $numwits = 0;
616 my $rdg = $collation->reading( $rid );
617 foreach my $rwit ( $rdg->witnesses( $rid ) ) {
618 $numwits++ if exists $wits{$rwit};
619 }
769401c3 620 next unless $numwits; # Disregard readings with none of our witnesses
621 if( $numwits < keys( %wits ) ) {
fdb37581 622 $errmsg = "Reading $rid contains some but not all of the specified witnesses.";
623 last;
624 } elsif( exists $rdgranks{ $rdg->rank } ) {
625 $errmsg = "More than one reading would be detached along with $rid at rank " . $rdg->rank;
626 last;
627 } else {
628 $rdgranks{ $rdg->rank } = $rid;
629 }
630 }
631
632 # Now check that the readings make a single sequence.
633 unless( $errmsg ) {
634 my $prior;
635 foreach my $rank ( sort { $a <=> $b } keys %rdgranks ) {
636 my $rid = $rdgranks{$rank};
637 if( $prior ) {
638 # Check that there is only one path between $prior and $rdg.
639 foreach my $wit ( keys %wits ) {
640 unless( $collation->prior_reading( $rid, $wit ) eq $prior ) {
641 $errmsg = "Diverging witness paths from $prior to $rid at $wit";
642 last;
643 }
644 }
645 }
646 $prior = $rid;
647 }
648 }
649
650 # Abort if we've run into a problem.
651 if( $errmsg ) {
652 $c->stash->{'result'} = { 'error' => $errmsg };
653 $c->response->status( '403' );
654 $c->forward('View::JSON');
655 return;
656 }
657
658 # Otherwise, do the dirty work.
659 my @witlist = keys %wits;
217f5e64 660 my @deleted_relations;
fdb37581 661 foreach my $rank ( sort { $a <=> $b } keys %rdgranks ) {
5539cba3 662 my $newrdg;
fdb37581 663 my $reading_id = $rdgranks{$rank};
217f5e64 664 my @delrels;
5539cba3 665 try {
217f5e64 666 ( $newrdg, @delrels ) =
667 $collation->duplicate_reading( $reading_id, @witlist );
5539cba3 668 } catch( Text::Tradition::Error $e ) {
669 $c->response->status( '403' );
670 $errmsg = $e->message;
671 } catch {
672 # Something else went wrong, probably a Moose error
995efe76 673 $c->response->status( '500' );
5539cba3 674 $errmsg = 'Something went wrong with the request';
675 }
676 if( $newrdg ) {
ea77ecb8 677 my $data = _reading_struct( $newrdg );
678 $data->{'orig_rdg'} = $reading_id;
679 $response->{"$newrdg"} = $data;
217f5e64 680 push( @deleted_relations, @delrels );
5539cba3 681 }
fdb37581 682 }
683 if( $errmsg ) {
684 $c->stash->{'result'} = { 'error' => $errmsg };
5539cba3 685 } else {
fdb37581 686 $m->save( $collation );
217f5e64 687 $response->{'DELETED'} = \@deleted_relations;
fdb37581 688 $c->stash->{'result'} = $response;
5539cba3 689 }
5539cba3 690 }
691 $c->forward('View::JSON');
692}
693
694
695
20198e59 696sub _check_permission {
697 my( $c, $tradition ) = @_;
698 my $user = $c->user_exists ? $c->user->get_object : undef;
b0524272 699 # Does this user have access?
20198e59 700 if( $user ) {
b0524272 701 if( $user->is_admin ||
702 ( $tradition->has_user && $tradition->user->id eq $user->id ) ) {
703 $c->stash->{'permission'} = 'full';
704 return 1;
705 }
080f8a02 706 }
707 # Is it public?
708 if( $tradition->public ) {
20198e59 709 $c->stash->{'permission'} = 'readonly';
710 return 1;
080f8a02 711 }
712 # Forbidden!
713 $c->response->status( 403 );
714 $c->response->body( 'You do not have permission to view this tradition.' );
715 $c->detach( 'View::Plain' );
716 return 0;
20198e59 717}
718
997ebe92 719sub _clean_booleans {
eefe56ac 720 my( $obj, $param, $val ) = @_;
721 if( $obj->meta->get_attribute( $param )->type_constraint->name eq 'Bool' ) {
997ebe92 722 $val = 1 if $val eq 'true';
723 $val = undef if $val eq 'false';
724 }
725 return $val;
726}
727
b8a92065 728=head2 end
729
730Attempt to render a view, if needed.
731
732=cut
733
734sub end : ActionClass('RenderView') {}
735
736=head1 AUTHOR
737
738Tara L Andrews
739
740=head1 LICENSE
741
742This library is free software. You can redistribute it and/or modify
743it under the same terms as Perl itself.
744
745=cut
746
747__PACKAGE__->meta->make_immutable;
748
7491;