Commit | Line | Data |
b8a92065 |
1 | package stemmaweb::Controller::Relation; |
5539cba3 |
2 | use JSON qw/ to_json from_json /; |
b8a92065 |
3 | use Moose; |
e4bdf660 |
4 | use Moose::Util::TypeConstraints qw/ find_type_constraint /; |
cc86fa11 |
5 | use Module::Load; |
b8a92065 |
6 | use namespace::autoclean; |
e4bdf660 |
7 | use Text::Tradition::Datatypes; |
b28e606e |
8 | use TryCatch; |
b8a92065 |
9 | |
10 | BEGIN { extends 'Catalyst::Controller' } |
11 | |
12 | |
13 | =head1 NAME |
14 | |
15 | stemmaweb::Controller::Relation - Controller for the relationship mapper |
16 | |
17 | =head1 DESCRIPTION |
18 | |
b28e606e |
19 | The reading relationship mapper with draggable nodes. |
b8a92065 |
20 | |
21 | =head1 METHODS |
22 | |
b28e606e |
23 | =head2 index |
24 | |
b8a92065 |
25 | GET relation/$textid |
26 | |
27 | Renders the application for the text identified by $textid. |
28 | |
b8a92065 |
29 | =cut |
30 | |
9529f69c |
31 | sub 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 |
44 | sub 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 | |
69 | sub 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 |
148 | sub _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 | |
159 | Returns the help window HTML. |
160 | |
161 | =cut |
162 | |
163 | sub 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 | |
186 | Returns the list of relationships defined for this text. |
b28e606e |
187 | |
13aa153c |
188 | POST relation/$textid/relationships { request } |
9529f69c |
189 | |
190 | Attempts to define the requested relationship within the text. Returns 200 on |
191 | success or 403 on error. |
b28e606e |
192 | |
13aa153c |
193 | DELETE relation/$textid/relationships { request } |
9529f69c |
194 | |
b28e606e |
195 | |
196 | =cut |
197 | |
9529f69c |
198 | sub 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 | |
298 | Returns the list of readings defined for this text along with their metadata. |
299 | |
300 | =cut |
301 | |
0dcdd5ec |
302 | my %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 |
311 | sub _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 | |
330 | sub 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 | |
351 | Returns the list of readings defined for this text along with their metadata. |
352 | |
353 | POST relation/$textid/reading/$id { request } |
354 | |
355 | Alters the reading according to the values in request. Returns 403 Forbidden if |
356 | the alteration isn't allowed. |
357 | |
358 | =cut |
359 | |
360 | sub 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 | |
b001c73d |
431 | =head2 merge |
432 | |
433 | POST relation/$textid/merge { data } |
434 | |
435 | Merges the requested readings, combining the witnesses of both readings into |
436 | the target reading. All non-conflicting source relationships are inherited by |
437 | the target relationship. |
438 | |
439 | =cut |
440 | |
441 | sub merge :Chained('text') :PathPart :Args(0) { |
442 | my( $self, $c ) = @_; |
443 | my $tradition = delete $c->stash->{'tradition'}; |
444 | my $collation = $tradition->collation; |
445 | my $m = $c->model('Directory'); |
446 | if( $c->request->method eq 'POST' ) { |
447 | if( $c->stash->{'permission'} ne 'full' ) { |
448 | $c->response->status( '403' ); |
449 | $c->stash->{'result'} = { |
450 | 'error' => 'You do not have permission to modify this tradition.' }; |
451 | $c->detach('View::JSON'); |
452 | return; |
453 | } |
454 | my $errmsg; |
455 | my $response; |
456 | |
457 | my $main = $c->request->param('target_id'); |
458 | my $second = $c->request->param('source_id'); |
459 | # Find the common successor of these, so that we can detect other |
460 | # potentially identical readings. |
461 | my $csucc = $collation->common_successor( $main, $second ); |
462 | |
463 | # Try the merge if these are parallel readings. |
464 | if( $csucc->id eq $main || $csucc->id eq $second ) { |
465 | $errmsg = "Cannot merge readings in the same path"; |
466 | } else { |
467 | try { |
468 | $collation->merge_readings( $main, $second ); |
469 | } catch( Text::Tradition::Error $e ) { |
470 | $c->response->status( '403' ); |
471 | $errmsg = $e->message; |
472 | } catch { |
473 | # Something else went wrong, probably a Moose error |
474 | $c->response->status( '403' ); |
475 | $errmsg = 'Something went wrong with the request'; |
476 | } |
477 | } |
478 | |
479 | # Look for readings that are now identical. |
480 | if( $errmsg ) { |
481 | $response = { status => 'error', error => $errmsg }; |
482 | } else { |
483 | $response = { status => 'ok' }; |
8880c19d |
484 | unless( $c->request->param('single') ) { |
485 | my @identical = $collation->identical_readings( |
486 | start => $main, end => $csucc->id ); |
487 | if( @identical ) { |
488 | $response->{'checkalign'} = [ |
489 | map { [ $_->[0]->id, $_->[1]->id ] } @identical ]; |
490 | } |
b001c73d |
491 | } |
492 | $m->save( $collation ); |
493 | } |
494 | $c->stash->{'result'} = $response; |
495 | $c->forward('View::JSON'); |
496 | } |
497 | } |
498 | |
5539cba3 |
499 | =head2 duplicate |
500 | |
fdb37581 |
501 | POST relation/$textid/duplicate { data } |
5539cba3 |
502 | |
fdb37581 |
503 | Duplicates the requested readings, detaching the witnesses specified in |
504 | the list to use the new reading(s) instead of the old. The data to be |
505 | passed should be a JSON structure: |
506 | |
507 | { readings: rid1,rid2,rid3,... |
508 | witnesses: [ wit1, ... ] } |
5539cba3 |
509 | |
510 | =cut |
511 | |
fdb37581 |
512 | sub duplicate :Chained('text') :PathPart :Args(0) { |
513 | my( $self, $c ) = @_; |
5539cba3 |
514 | my $tradition = delete $c->stash->{'tradition'}; |
515 | my $collation = $tradition->collation; |
5539cba3 |
516 | my $m = $c->model('Directory'); |
517 | if( $c->request->method eq 'POST' ) { |
518 | if( $c->stash->{'permission'} ne 'full' ) { |
519 | $c->response->status( '403' ); |
520 | $c->stash->{'result'} = { |
521 | 'error' => 'You do not have permission to modify this tradition.' }; |
522 | $c->detach('View::JSON'); |
523 | return; |
524 | } |
525 | my $errmsg; |
526 | my $response = {}; |
fdb37581 |
527 | # Sort out which readings need to be duplicated from the set given, and |
528 | # ensure that all the given wits bear each relevant reading. |
529 | |
530 | my %wits = (); |
531 | map { $wits{$_} = 1 } $c->request->param('witnesses[]'); |
532 | my %rdgranks = (); |
533 | foreach my $rid ( $c->request->param('readings[]') ) { |
534 | my $numwits = 0; |
535 | my $rdg = $collation->reading( $rid ); |
536 | foreach my $rwit ( $rdg->witnesses( $rid ) ) { |
537 | $numwits++ if exists $wits{$rwit}; |
538 | } |
769401c3 |
539 | next unless $numwits; # Disregard readings with none of our witnesses |
540 | if( $numwits < keys( %wits ) ) { |
fdb37581 |
541 | $errmsg = "Reading $rid contains some but not all of the specified witnesses."; |
542 | last; |
543 | } elsif( exists $rdgranks{ $rdg->rank } ) { |
544 | $errmsg = "More than one reading would be detached along with $rid at rank " . $rdg->rank; |
545 | last; |
546 | } else { |
547 | $rdgranks{ $rdg->rank } = $rid; |
548 | } |
549 | } |
550 | |
551 | # Now check that the readings make a single sequence. |
552 | unless( $errmsg ) { |
553 | my $prior; |
554 | foreach my $rank ( sort { $a <=> $b } keys %rdgranks ) { |
555 | my $rid = $rdgranks{$rank}; |
556 | if( $prior ) { |
557 | # Check that there is only one path between $prior and $rdg. |
558 | foreach my $wit ( keys %wits ) { |
559 | unless( $collation->prior_reading( $rid, $wit ) eq $prior ) { |
560 | $errmsg = "Diverging witness paths from $prior to $rid at $wit"; |
561 | last; |
562 | } |
563 | } |
564 | } |
565 | $prior = $rid; |
566 | } |
567 | } |
568 | |
569 | # Abort if we've run into a problem. |
570 | if( $errmsg ) { |
571 | $c->stash->{'result'} = { 'error' => $errmsg }; |
572 | $c->response->status( '403' ); |
573 | $c->forward('View::JSON'); |
574 | return; |
575 | } |
576 | |
577 | # Otherwise, do the dirty work. |
578 | my @witlist = keys %wits; |
217f5e64 |
579 | my @deleted_relations; |
fdb37581 |
580 | foreach my $rank ( sort { $a <=> $b } keys %rdgranks ) { |
5539cba3 |
581 | my $newrdg; |
fdb37581 |
582 | my $reading_id = $rdgranks{$rank}; |
217f5e64 |
583 | my @delrels; |
5539cba3 |
584 | try { |
217f5e64 |
585 | ( $newrdg, @delrels ) = |
586 | $collation->duplicate_reading( $reading_id, @witlist ); |
5539cba3 |
587 | } catch( Text::Tradition::Error $e ) { |
588 | $c->response->status( '403' ); |
589 | $errmsg = $e->message; |
590 | } catch { |
591 | # Something else went wrong, probably a Moose error |
995efe76 |
592 | $c->response->status( '500' ); |
5539cba3 |
593 | $errmsg = 'Something went wrong with the request'; |
594 | } |
595 | if( $newrdg ) { |
ea77ecb8 |
596 | my $data = _reading_struct( $newrdg ); |
597 | $data->{'orig_rdg'} = $reading_id; |
598 | $response->{"$newrdg"} = $data; |
217f5e64 |
599 | push( @deleted_relations, @delrels ); |
5539cba3 |
600 | } |
fdb37581 |
601 | } |
602 | if( $errmsg ) { |
603 | $c->stash->{'result'} = { 'error' => $errmsg }; |
5539cba3 |
604 | } else { |
fdb37581 |
605 | $m->save( $collation ); |
217f5e64 |
606 | $response->{'DELETED'} = \@deleted_relations; |
fdb37581 |
607 | $c->stash->{'result'} = $response; |
5539cba3 |
608 | } |
5539cba3 |
609 | } |
610 | $c->forward('View::JSON'); |
611 | } |
612 | |
613 | |
614 | |
20198e59 |
615 | sub _check_permission { |
616 | my( $c, $tradition ) = @_; |
617 | my $user = $c->user_exists ? $c->user->get_object : undef; |
b0524272 |
618 | # Does this user have access? |
20198e59 |
619 | if( $user ) { |
b0524272 |
620 | if( $user->is_admin || |
621 | ( $tradition->has_user && $tradition->user->id eq $user->id ) ) { |
622 | $c->stash->{'permission'} = 'full'; |
623 | return 1; |
624 | } |
080f8a02 |
625 | } |
626 | # Is it public? |
627 | if( $tradition->public ) { |
20198e59 |
628 | $c->stash->{'permission'} = 'readonly'; |
629 | return 1; |
080f8a02 |
630 | } |
631 | # Forbidden! |
632 | $c->response->status( 403 ); |
633 | $c->response->body( 'You do not have permission to view this tradition.' ); |
634 | $c->detach( 'View::Plain' ); |
635 | return 0; |
20198e59 |
636 | } |
637 | |
997ebe92 |
638 | sub _clean_booleans { |
eefe56ac |
639 | my( $obj, $param, $val ) = @_; |
640 | if( $obj->meta->get_attribute( $param )->type_constraint->name eq 'Bool' ) { |
997ebe92 |
641 | $val = 1 if $val eq 'true'; |
642 | $val = undef if $val eq 'false'; |
643 | } |
644 | return $val; |
645 | } |
646 | |
b8a92065 |
647 | =head2 end |
648 | |
649 | Attempt to render a view, if needed. |
650 | |
651 | =cut |
652 | |
653 | sub end : ActionClass('RenderView') {} |
654 | |
655 | =head1 AUTHOR |
656 | |
657 | Tara L Andrews |
658 | |
659 | =head1 LICENSE |
660 | |
661 | This library is free software. You can redistribute it and/or modify |
662 | it under the same terms as Perl itself. |
663 | |
664 | =cut |
665 | |
666 | __PACKAGE__->meta->make_immutable; |
667 | |
668 | 1; |