1 package stemmaweb::Controller::Relation;
2 use JSON qw/ to_json /;
5 use namespace::autoclean;
8 BEGIN { extends 'Catalyst::Controller' }
13 stemmaweb::Controller::Relation - Controller for the relationship mapper
17 The reading relationship mapper with draggable nodes.
25 Renders the application for the text identified by $textid.
29 sub index :Path :Args(0) {
31 $c->stash->{'template'} = 'relate.tt';
38 Runs the relationship mapper for the specified text ID.
42 sub text :Chained('/') :PathPart('relation') :CaptureArgs(1) {
43 my( $self, $c, $textid ) = @_;
44 my $tradition = $c->model('Directory')->tradition( $textid );
45 unless( $tradition ) {
46 $c->response->status('404');
47 $c->response->body("No such tradition with ID $textid");
48 $c->detach('View::Plain');
52 # Account for a bad interaction between FastCGI and KiokuDB
53 unless( $tradition->collation->tradition ) {
54 $c->log->warn( "Fixing broken tradition link" );
55 $tradition->collation->_set_tradition( $tradition );
56 $c->model('Directory')->save( $tradition );
58 # Check permissions. Will return 403 if denied, otherwise will
59 # put the appropriate value in the stash.
60 my $ok = _check_permission( $c, $tradition );
63 $c->stash->{'textid'} = $textid;
64 $c->stash->{'tradition'} = $tradition;
67 sub main :Chained('text') :PathPart('') :Args(0) {
69 my $tradition = delete $c->stash->{'tradition'};
70 my $collation = $tradition->collation;
72 # Stash the relationship definitions
73 $c->stash->{'relationship_scopes'} = to_json( [ qw/ local global / ] );
75 foreach my $type ( sort { _typesort( $a, $b ) } $collation->relations->types ) {
76 next if $type->is_weak;
77 my $struct = { name => $type->name, description => $type->description };
78 push( @reltypeinfo, $struct );
80 $c->stash->{'relationship_types'} = to_json( \@reltypeinfo );
82 # See how big the tradition is. Edges are more important than nodes
83 # when it comes to rendering difficulty.
84 my $numnodes = scalar $collation->readings;
85 my $numedges = scalar $collation->paths;
86 my $length = $collation->end->rank;
87 # We should display no more than roughly 500 nodes, or roughly 700
89 my $segments = $numnodes / 500;
90 if( $numedges / 700 > $segments ) {
91 $segments = $numedges / 700;
93 my $segsize = sprintf( "%.0f", $length / $segments );
94 my $margin = sprintf( "%.0f", $segsize / 10 );
96 # Segment the tradition in order not to overload the browser.
99 while( $r + $margin < $length ) {
103 $c->stash->{'textsegments'} = [];
104 foreach my $i ( 0..$#divs ) {
105 my $seg = { 'start' => $divs[$i] };
106 $seg->{'display'} = "Segment " . ($i+1);
107 push( @{$c->stash->{'textsegments'}}, $seg );
110 my $startseg = $c->req->param('start');
113 # Only render the subgraph from startseg to endseg or to END,
115 my $endseg = $startseg + $segsize + $margin;
116 $svgopts = { 'from' => $startseg };
117 $svgopts->{'to'} = $endseg if $endseg < $collation->end->rank;
118 } elsif( exists $c->stash->{'textsegments'} ) {
119 # This is the unqualified load of a long tradition. We implicitly start
120 # at zero, but go only as far as our segment size.
121 my $endseg = $segsize + $margin;
123 $svgopts = { 'to' => $endseg };
126 my $svg_str = $collation->as_svg( $svgopts );
127 $svg_str =~ s/\n//gs;
128 $c->stash->{'startseg'} = $startseg if defined $startseg;
129 $c->stash->{'svg_string'} = $svg_str;
130 $c->stash->{'text_title'} = $tradition->name;
131 if( $tradition->can('language') ) {
132 $c->stash->{'text_lang'} = $tradition->language;
133 $c->stash->{'can_morphologize'} = 1;
135 $c->stash->{'text_lang'} = 'Default';
137 $c->stash->{'template'} = 'relate.tt';
142 my $blsort = $a->bindlevel <=> $b->bindlevel;
143 return $blsort if $blsort;
144 return $a->name cmp $b->name;
149 GET relation/help/$language
151 Returns the help window HTML.
155 sub help :Local :Args(1) {
156 my( $self, $c, $lang ) = @_;
157 # Display the morphological help for the language if it is defined.
158 if( $lang && $lang ne 'Default' ) {
159 my $mod = 'Text::Tradition::Language::' . $lang;
163 $c->log->debug("Warning: could not load $mod");
165 my $has_mod = $mod->can('morphology_tags');
167 my $tagset = &$has_mod;
168 $c->stash->{'tagset'} = $tagset;
171 $c->stash->{'template'} = 'relatehelp.tt';
176 GET relation/$textid/relationships
178 Returns the list of relationships defined for this text.
180 POST relation/$textid/relationships { request }
182 Attempts to define the requested relationship within the text. Returns 200 on
183 success or 403 on error.
185 DELETE relation/$textid/relationships { request }
190 sub relationships :Chained('text') :PathPart :Args(0) {
191 my( $self, $c ) = @_;
192 my $tradition = delete $c->stash->{'tradition'};
193 my $ok = _check_permission( $c, $tradition );
195 my $collation = $tradition->collation;
196 my $m = $c->model('Directory');
197 if( $c->request->method eq 'GET' ) {
198 my @pairs = $collation->relationships; # returns the edges
200 foreach my $p ( @pairs ) {
201 my $relobj = $collation->relations->get_relationship( @$p );
202 next if $relobj->type eq 'collated'; # Don't show these
203 next if $p->[0] eq $p->[1]; # HACK until bugfix
204 my $relhash = { source => $p->[0], target => $p->[1],
205 type => $relobj->type, scope => $relobj->scope };
206 $relhash->{'note'} = $relobj->annotation if $relobj->has_annotation;
207 push( @all_relations, $relhash );
209 $c->stash->{'result'} = \@all_relations;
211 # Check write permissions first of all
212 if( $c->stash->{'permission'} ne 'full' ) {
213 $c->response->status( '403' );
214 $c->stash->{'result'} = {
215 'error' => 'You do not have permission to modify this tradition.' };
216 $c->detach( 'View::JSON' );
217 } elsif( $c->request->method eq 'POST' ) {
218 my $node = $c->request->param('source_id');
219 my $target = $c->request->param('target_id');
220 my $relation = $c->request->param('rel_type');
221 my $note = $c->request->param('note');
222 my $scope = $c->request->param('scope');
224 my $opts = { 'type' => $relation, 'propagate' => 1 };
225 $opts->{'scope'} = $scope if $scope;
226 $opts->{'annotation'} = $note if $note;
229 my @vectors = $collation->add_relationship( $node, $target, $opts );
230 $c->stash->{'result'} = \@vectors;
231 $m->save( $tradition );
232 } catch( Text::Tradition::Error $e ) {
233 $c->response->status( '403' );
234 $c->stash->{'result'} = { 'error' => $e->message };
236 } elsif( $c->request->method eq 'DELETE' ) {
237 my $node = $c->request->param('source_id');
238 my $target = $c->request->param('target_id');
241 my @vectors = $collation->del_relationship( $node, $target );
242 $m->save( $tradition );
243 $c->stash->{'result'} = \@vectors;
244 } catch( Text::Tradition::Error $e ) {
245 $c->response->status( '403' );
246 $c->stash->{'result'} = { 'error' => $e->message };
250 $c->forward('View::JSON');
255 GET relation/$textid/readings
257 Returns the list of readings defined for this text along with their metadata.
261 my %read_write_keys = (
265 'grammar_invalid' => 1,
270 sub _reading_struct {
272 # Return a JSONable struct of the useful keys. Keys meant to be writable
273 # have a true value; read-only keys have a false value.
275 map { $struct->{$_} = $reading->$_ if $reading->can( $_ ) } keys( %read_write_keys );
277 $struct->{'lexemes'} = $reading->can( 'lexemes' ) ? [ $reading->lexemes ] : [];
278 # Look up any words related via spelling or orthography
281 return $t eq 'spelling' || $t eq 'orthographic';
284 foreach my $sr ( $reading->related_readings( $sameword ) ) {
285 push( @variants, $sr->text );
287 $struct->{'variants'} = \@variants;
291 sub readings :Chained('text') :PathPart :Args(0) {
292 my( $self, $c ) = @_;
293 my $tradition = delete $c->stash->{'tradition'};
294 my $ok = _check_permission( $c, $tradition );
296 my $collation = $tradition->collation;
297 my $m = $c->model('Directory');
298 if( $c->request->method eq 'GET' ) {
300 foreach my $rdg ( $collation->readings ) {
301 $rdginfo->{$rdg->id} = _reading_struct( $rdg );
303 $c->stash->{'result'} = $rdginfo;
305 $c->forward('View::JSON');
310 GET relation/$textid/reading/$id
312 Returns the list of readings defined for this text along with their metadata.
314 POST relation/$textid/reading/$id { request }
316 Alters the reading according to the values in request. Returns 403 Forbidden if
317 the alteration isn't allowed.
321 sub reading :Chained('text') :PathPart :Args(1) {
322 my( $self, $c, $reading_id ) = @_;
323 my $tradition = delete $c->stash->{'tradition'};
324 my $collation = $tradition->collation;
325 my $rdg = $collation->reading( $reading_id );
326 my $m = $c->model('Directory');
327 if( $c->request->method eq 'GET' ) {
328 $c->stash->{'result'} = $rdg ? _reading_struct( $rdg )
329 : { 'error' => "No reading with ID $reading_id" };
330 } elsif ( $c->request->method eq 'POST' ) {
331 if( $c->stash->{'permission'} ne 'full' ) {
332 $c->response->status( '403' );
333 $c->stash->{'result'} = {
334 'error' => 'You do not have permission to view this tradition.' };
335 $c->detach('View::JSON');
339 if( $rdg && $rdg->does('Text::Tradition::Morphology') ) {
340 # Are we re-lemmatizing?
341 if( $c->request->param('relemmatize') ) {
342 my $nf = $c->request->param('normal_form');
343 # TODO throw error unless $nf
344 $rdg->normal_form( $nf );
345 # TODO throw error if lemmatization fails
346 # TODO skip this if normal form hasn't changed
349 # Set all the values that we have for the reading.
350 # TODO error handling
351 foreach my $p ( keys %{$c->request->params} ) {
352 if( $p =~ /^morphology_(\d+)$/ ) {
353 # Set the form on the correct lexeme
354 my $morphval = $c->request->param( $p );
355 next unless $morphval;
357 my $lx = $rdg->lexeme( $midx );
358 my $strrep = $rdg->language . ' // ' . $morphval;
359 my $idx = $lx->has_form( $strrep );
360 unless( defined $idx ) {
361 # Make the word form and add it to the lexeme.
363 $idx = $lx->add_matching_form( $strrep ) - 1;
364 } catch( Text::Tradition::Error $e ) {
365 $c->response->status( '403' );
366 $errmsg = $e->message;
368 # Something else went wrong, probably a Moose error
369 $c->response->status( '403' );
370 $errmsg = 'Something went wrong with the request';
373 $lx->disambiguate( $idx ) if defined $idx;
374 } elsif( $read_write_keys{$p} ) {
375 my $val = _clean_booleans( $rdg, $p, $c->request->param( $p ) );
382 $errmsg = "Reading does not exist or cannot be morphologized";
384 $c->stash->{'result'} = $errmsg ? { 'error' => $errmsg }
385 : _reading_struct( $rdg );
388 $c->forward('View::JSON');
392 sub _check_permission {
393 my( $c, $tradition ) = @_;
394 my $user = $c->user_exists ? $c->user->get_object : undef;
395 # Does this user have access?
397 if( $user->is_admin ||
398 ( $tradition->has_user && $tradition->user->id eq $user->id ) ) {
399 $c->stash->{'permission'} = 'full';
404 if( $tradition->public ) {
405 $c->stash->{'permission'} = 'readonly';
409 $c->response->status( 403 );
410 $c->response->body( 'You do not have permission to view this tradition.' );
411 $c->detach( 'View::Plain' );
415 sub _clean_booleans {
416 my( $rdg, $param, $val ) = @_;
417 if( $rdg->meta->get_attribute( $param )->type_constraint->name eq 'Bool' ) {
418 $val = 1 if $val eq 'true';
419 $val = undef if $val eq 'false';
426 Attempt to render a view, if needed.
430 sub end : ActionClass('RenderView') {}
438 This library is free software. You can redistribute it and/or modify
439 it under the same terms as Perl itself.
443 __PACKAGE__->meta->make_immutable;