1 package stemmaweb::Controller::Relation;
4 use namespace::autoclean;
7 BEGIN { extends 'Catalyst::Controller' }
12 stemmaweb::Controller::Relation - Controller for the relationship mapper
16 The reading relationship mapper with draggable nodes.
24 Renders the application for the text identified by $textid.
28 sub index :Path :Args(0) {
30 $c->stash->{'template'} = 'relate.tt';
37 Runs the relationship mapper for the specified text ID.
41 sub text :Chained('/') :PathPart('relation') :CaptureArgs(1) {
42 my( $self, $c, $textid ) = @_;
43 my $tradition = $c->model('Directory')->tradition( $textid );
44 unless( $tradition ) {
45 $c->response->status('404');
46 $c->response->body("No such tradition with ID $textid");
47 $c->detach('View::Plain');
51 # Account for a bad interaction between FastCGI and KiokuDB
52 unless( $tradition->collation->tradition ) {
53 $c->log->warn( "Fixing broken tradition link" );
54 $tradition->collation->_set_tradition( $tradition );
55 $c->model('Directory')->save( $tradition );
57 # Check permissions. Will return 403 if denied, otherwise will
58 # put the appropriate value in the stash.
59 my $ok = _check_permission( $c, $tradition );
62 $c->stash->{'textid'} = $textid;
63 $c->stash->{'tradition'} = $tradition;
66 sub main :Chained('text') :PathPart('') :Args(0) {
68 my $tradition = delete $c->stash->{'tradition'};
69 my $collation = $tradition->collation;
71 # See how big the tradition is. Edges are more important than nodes
72 # when it comes to rendering difficulty.
73 my $numnodes = scalar $collation->readings;
74 my $numedges = scalar $collation->paths;
75 my $length = $collation->end->rank;
76 # We should display no more than roughly 500 nodes, or roughly 700
78 my $segments = $numnodes / 500;
79 if( $numedges / 700 > $segments ) {
80 $segments = $numedges / 700;
82 my $segsize = sprintf( "%.0f", $length / $segments );
83 my $margin = sprintf( "%.0f", $segsize / 10 );
85 # Segment the tradition in order not to overload the browser.
88 while( $r + $margin < $length ) {
92 $c->stash->{'textsegments'} = [];
93 foreach my $i ( 0..$#divs ) {
94 my $seg = { 'start' => $divs[$i] };
95 $seg->{'display'} = "Segment " . ($i+1);
96 push( @{$c->stash->{'textsegments'}}, $seg );
99 my $startseg = $c->req->param('start');
102 # Only render the subgraph from startseg to endseg or to END,
104 my $endseg = $startseg + $segsize + $margin;
105 $svgopts = { 'from' => $startseg };
106 $svgopts->{'to'} = $endseg if $endseg < $collation->end->rank;
107 } elsif( exists $c->stash->{'textsegments'} ) {
108 # This is the unqualified load of a long tradition. We implicitly start
109 # at zero, but go only as far as our segment size.
110 my $endseg = $segsize + $margin;
112 $svgopts = { 'to' => $endseg };
115 my $svg_str = $collation->as_svg( $svgopts );
116 $svg_str =~ s/\n//gs;
117 $c->stash->{'startseg'} = $startseg if defined $startseg;
118 $c->stash->{'svg_string'} = $svg_str;
119 $c->stash->{'text_title'} = $tradition->name;
120 if( $tradition->can('language') ) {
121 $c->stash->{'text_lang'} = $tradition->language;
122 $c->stash->{'can_morphologize'} = 1;
124 $c->stash->{'text_lang'} = 'Default';
126 $c->stash->{'template'} = 'relate.tt';
131 GET relation/$textid/definitions
133 Returns a data structure giving the valid types and scopes for a relationship in
138 sub definitions :Chained('text') :PathPart :Args(0) {
139 my( $self, $c ) = @_;
140 my $tradition = delete $c->stash->{'tradition'};
141 my @valid_relationships = map { $_->name } grep { !$_->is_weak }
142 $tradition->collation->relations->types;
143 my $valid_scopes = [ qw/ local global / ];
144 $c->stash->{'result'} = {
145 'types' => \@valid_relationships,
146 'scopes' => $valid_scopes
148 $c->forward('View::JSON');
153 GET relation/help/$language
155 Returns the help window HTML.
159 sub help :Local :Args(1) {
160 my( $self, $c, $lang ) = @_;
161 # Display the morphological help for the language if it is defined.
162 if( $lang && $lang ne 'Default' ) {
163 my $mod = 'Text::Tradition::Language::' . $lang;
167 $c->log->debug("Warning: could not load $mod");
169 my $has_mod = $mod->can('morphology_tags');
171 my $tagset = &$has_mod;
172 $c->stash->{'tagset'} = $tagset;
175 $c->stash->{'template'} = 'relatehelp.tt';
180 GET relation/$textid/relationships
182 Returns the list of relationships defined for this text.
184 POST relation/$textid/relationships { request }
186 Attempts to define the requested relationship within the text. Returns 200 on
187 success or 403 on error.
189 DELETE relation/$textid/relationships { request }
194 sub relationships :Chained('text') :PathPart :Args(0) {
195 my( $self, $c ) = @_;
196 my $tradition = delete $c->stash->{'tradition'};
197 my $ok = _check_permission( $c, $tradition );
199 my $collation = $tradition->collation;
200 my $m = $c->model('Directory');
201 if( $c->request->method eq 'GET' ) {
202 my @pairs = $collation->relationships; # returns the edges
204 foreach my $p ( @pairs ) {
205 my $relobj = $collation->relations->get_relationship( @$p );
206 next if $relobj->type eq 'collated'; # Don't show these
207 next if $p->[0] eq $p->[1]; # HACK until bugfix
208 my $relhash = { source => $p->[0], target => $p->[1],
209 type => $relobj->type, scope => $relobj->scope };
210 $relhash->{'note'} = $relobj->annotation if $relobj->has_annotation;
211 push( @all_relations, $relhash );
213 $c->stash->{'result'} = \@all_relations;
215 # Check write permissions first of all
216 if( $c->stash->{'permission'} ne 'full' ) {
217 $c->response->status( '403' );
218 $c->stash->{'result'} = {
219 'error' => 'You do not have permission to view this tradition.' };
220 } elsif( $c->request->method eq 'POST' ) {
221 unless( $c->stash->{'permission'} eq 'full' ) {
222 $c->response->status( '403' );
223 $c->stash->{'result'} = {
224 'error' => 'You do not have permission to view this tradition.' };
225 $c->detach( 'View::JSON' );
227 my $node = $c->request->param('source_id');
228 my $target = $c->request->param('target_id');
229 my $relation = $c->request->param('rel_type');
230 my $note = $c->request->param('note');
231 my $scope = $c->request->param('scope');
233 my $opts = { 'type' => $relation, 'propagate' => 1 };
234 $opts->{'scope'} = $scope if $scope;
235 $opts->{'annotation'} = $note if $note;
238 my @vectors = $collation->add_relationship( $node, $target, $opts );
239 $c->stash->{'result'} = \@vectors;
240 $m->save( $tradition );
241 } catch( Text::Tradition::Error $e ) {
242 $c->response->status( '403' );
243 $c->stash->{'result'} = { 'error' => $e->message };
245 } elsif( $c->request->method eq 'DELETE' ) {
246 my $node = $c->request->param('source_id');
247 my $target = $c->request->param('target_id');
250 my @vectors = $collation->del_relationship( $node, $target );
251 $m->save( $tradition );
252 $c->stash->{'result'} = \@vectors;
253 } catch( Text::Tradition::Error $e ) {
254 $c->response->status( '403' );
255 $c->stash->{'result'} = { 'error' => $e->message };
259 $c->forward('View::JSON');
264 GET relation/$textid/readings
266 Returns the list of readings defined for this text along with their metadata.
270 my %read_write_keys = (
274 'grammar_invalid' => 1,
279 sub _reading_struct {
281 # Return a JSONable struct of the useful keys. Keys meant to be writable
282 # have a true value; read-only keys have a false value.
284 map { $struct->{$_} = $reading->$_ if $reading->can( $_ ) } keys( %read_write_keys );
286 $struct->{'lexemes'} = $reading->can( 'lexemes' ) ? [ $reading->lexemes ] : [];
287 # Look up any words related via spelling or orthography
290 return $t eq 'spelling' || $t eq 'orthographic';
293 foreach my $sr ( $reading->related_readings( $sameword ) ) {
294 push( @variants, $sr->text );
296 $struct->{'variants'} = \@variants;
300 sub readings :Chained('text') :PathPart :Args(0) {
301 my( $self, $c ) = @_;
302 my $tradition = delete $c->stash->{'tradition'};
303 my $ok = _check_permission( $c, $tradition );
305 my $collation = $tradition->collation;
306 my $m = $c->model('Directory');
307 if( $c->request->method eq 'GET' ) {
309 foreach my $rdg ( $collation->readings ) {
310 $rdginfo->{$rdg->id} = _reading_struct( $rdg );
312 $c->stash->{'result'} = $rdginfo;
314 $c->forward('View::JSON');
319 GET relation/$textid/reading/$id
321 Returns the list of readings defined for this text along with their metadata.
323 POST relation/$textid/reading/$id { request }
325 Alters the reading according to the values in request. Returns 403 Forbidden if
326 the alteration isn't allowed.
330 sub reading :Chained('text') :PathPart :Args(1) {
331 my( $self, $c, $reading_id ) = @_;
332 my $tradition = delete $c->stash->{'tradition'};
333 my $collation = $tradition->collation;
334 my $rdg = $collation->reading( $reading_id );
335 my $m = $c->model('Directory');
336 if( $c->request->method eq 'GET' ) {
337 $c->stash->{'result'} = $rdg ? _reading_struct( $rdg )
338 : { 'error' => "No reading with ID $reading_id" };
339 } elsif ( $c->request->method eq 'POST' ) {
340 if( $c->stash->{'permission'} ne 'full' ) {
341 $c->response->status( '403' );
342 $c->stash->{'result'} = {
343 'error' => 'You do not have permission to view this tradition.' };
344 $c->detach('View::JSON');
348 if( $rdg && $rdg->does('Text::Tradition::Morphology') ) {
349 # Are we re-lemmatizing?
350 if( $c->request->param('relemmatize') ) {
351 my $nf = $c->request->param('normal_form');
352 # TODO throw error unless $nf
353 $rdg->normal_form( $nf );
354 # TODO throw error if lemmatization fails
355 # TODO skip this if normal form hasn't changed
358 # Set all the values that we have for the reading.
359 # TODO error handling
360 foreach my $p ( keys %{$c->request->params} ) {
361 if( $p =~ /^morphology_(\d+)$/ ) {
362 # Set the form on the correct lexeme
363 my $morphval = $c->request->param( $p );
364 next unless $morphval;
366 my $lx = $rdg->lexeme( $midx );
367 my $strrep = $rdg->language . ' // ' . $morphval;
368 my $idx = $lx->has_form( $strrep );
369 unless( defined $idx ) {
370 # Make the word form and add it to the lexeme.
372 $idx = $lx->add_matching_form( $strrep ) - 1;
373 } catch( Text::Tradition::Error $e ) {
374 $c->response->status( '403' );
375 $errmsg = $e->message;
377 # Something else went wrong, probably a Moose error
378 $c->response->status( '403' );
379 $errmsg = 'Something went wrong with the request';
382 $lx->disambiguate( $idx ) if defined $idx;
383 } elsif( $read_write_keys{$p} ) {
384 my $val = _clean_booleans( $rdg, $p, $c->request->param( $p ) );
391 $errmsg = "Reading does not exist or cannot be morphologized";
393 $c->stash->{'result'} = $errmsg ? { 'error' => $errmsg }
394 : _reading_struct( $rdg );
397 $c->forward('View::JSON');
401 sub _check_permission {
402 my( $c, $tradition ) = @_;
403 my $user = $c->user_exists ? $c->user->get_object : undef;
404 # Does this user have access?
406 if( $user->is_admin ||
407 ( $tradition->has_user && $tradition->user->id eq $user->id ) ) {
408 $c->stash->{'permission'} = 'full';
413 if( $tradition->public ) {
414 $c->stash->{'permission'} = 'readonly';
418 $c->response->status( 403 );
419 $c->response->body( 'You do not have permission to view this tradition.' );
420 $c->detach( 'View::Plain' );
424 sub _clean_booleans {
425 my( $rdg, $param, $val ) = @_;
426 if( $rdg->meta->get_attribute( $param )->type_constraint->name eq 'Bool' ) {
427 $val = 1 if $val eq 'true';
428 $val = undef if $val eq 'false';
435 Attempt to render a view, if needed.
439 sub end : ActionClass('RenderView') {}
447 This library is free software. You can redistribute it and/or modify
448 it under the same terms as Perl itself.
452 __PACKAGE__->meta->make_immutable;