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';
35 GET relation/definitions
37 Returns a data structure giving the valid types and scopes for a relationship.
41 sub definitions :Local :Args(0) {
43 my $valid_relationships = [ qw/ spelling orthographic grammatical lexical transposition / ];
44 my $valid_scopes = [ qw/ local global / ];
45 $c->stash->{'result'} = { 'types' => $valid_relationships, 'scopes' => $valid_scopes };
46 $c->forward('View::JSON');
53 Runs the relationship mapper for the specified text ID.
57 sub text :Chained('/') :PathPart('relation') :CaptureArgs(1) {
58 my( $self, $c, $textid ) = @_;
59 # If the tradition has more than 500 ranks or so, split it up.
60 my $tradition = $c->model('Directory')->tradition( $textid );
61 # Account for a bad interaction between FastCGI and KiokuDB
62 unless( $tradition->collation->tradition ) {
63 $c->log->warn( "Fixing broken tradition link" );
64 $tradition->collation->_set_tradition( $tradition );
65 $c->model('Directory')->save( $tradition );
67 # See how big the tradition is. Edges are more important than nodes
68 # when it comes to rendering difficulty.
69 my $numnodes = scalar $tradition->collation->readings;
70 my $numedges = scalar $tradition->collation->paths;
71 my $length = $tradition->collation->end->rank;
72 # We should display no more than roughly 500 nodes, or roughly 700
74 my $segments = $numnodes / 500;
75 if( $numedges / 700 > $segments ) {
76 $segments = $numedges / 700;
78 my $segsize = sprintf( "%.0f", $length / $segments );
79 my $margin = sprintf( "%.0f", $segsize / 10 );
81 # Segment the tradition in order not to overload the browser.
84 while( $r + $margin < $length ) {
88 $c->stash->{'textsegments'} = [];
89 $c->stash->{'segsize'} = $segsize;
90 $c->stash->{'margin'} = $margin;
91 foreach my $i ( 0..$#divs ) {
92 my $seg = { 'start' => $divs[$i] };
93 $seg->{'display'} = "Segment " . ($i+1);
94 push( @{$c->stash->{'textsegments'}}, $seg );
97 $c->stash->{'textid'} = $textid;
98 $c->stash->{'tradition'} = $tradition;
101 sub main :Chained('text') :PathPart('') :Args(0) {
102 my( $self, $c ) = @_;
103 my $startseg = $c->req->param('start');
104 my $tradition = delete $c->stash->{'tradition'};
105 my $collation = $tradition->collation;
108 # Only render the subgraph from startseg to endseg or to END,
110 my $endseg = $startseg + $c->stash->{'segsize'} + $c->stash->{'margin'};
111 $svgopts = { 'from' => $startseg };
112 $svgopts->{'to'} = $endseg if $endseg < $collation->end->rank;
113 } elsif( exists $c->stash->{'textsegments'} ) {
114 # This is the unqualified load of a long tradition. We implicitly start
115 # at zero, but go only as far as 550.
116 my $endseg = $c->stash->{'segsize'} + $c->stash->{'margin'};
118 $svgopts = { 'to' => $endseg };
120 my $svg_str = $collation->as_svg( $svgopts );
121 $svg_str =~ s/\n//gs;
122 $c->stash->{'startseg'} = $startseg if defined $startseg;
123 $c->stash->{'svg_string'} = $svg_str;
124 $c->stash->{'text_title'} = $tradition->name;
125 $c->stash->{'text_lang'} = $tradition->language;
126 $c->stash->{'template'} = 'relate.tt';
131 GET relation/help/$language
133 Returns the help window HTML.
137 sub help :Local :Args(1) {
138 my( $self, $c, $lang ) = @_;
139 # Display the morphological help for the language if it is defined.
140 if( $lang && $lang ne 'Default' ) {
141 my $mod = 'Text::Tradition::Language::' . $lang;
145 $c->log->debug("Warning: could not load $mod");
147 my $has_mod = $mod->can('morphology_tags');
150 my $tagset = &$has_mod;
151 $c->stash->{'tagset'} = $tagset;
154 $c->stash->{'template'} = 'relatehelp.tt';
159 GET relation/$textid/relationships
161 Returns the list of relationships defined for this text.
163 POST relation/$textid/relationships { request }
165 Attempts to define the requested relationship within the text. Returns 200 on
166 success or 403 on error.
168 DELETE relation/$textid/relationships { request }
173 sub relationships :Chained('text') :PathPart :Args(0) {
174 my( $self, $c ) = @_;
175 my $tradition = delete $c->stash->{'tradition'};
176 my $collation = $tradition->collation;
177 my $m = $c->model('Directory');
178 if( $c->request->method eq 'GET' ) {
179 my @pairs = $collation->relationships; # returns the edges
181 foreach my $p ( @pairs ) {
182 my $relobj = $collation->relations->get_relationship( @$p );
183 next if $relobj->type eq 'collated'; # Don't show these
184 next if $p->[0] eq $p->[1]; # HACK until bugfix
185 my $relhash = { source => $p->[0], target => $p->[1],
186 type => $relobj->type, scope => $relobj->scope };
187 $relhash->{'note'} = $relobj->annotation if $relobj->has_annotation;
188 push( @all_relations, $relhash );
190 $c->stash->{'result'} = \@all_relations;
191 } elsif( $c->request->method eq 'POST' ) {
192 my $node = $c->request->param('source_id');
193 my $target = $c->request->param('target_id');
194 my $relation = $c->request->param('rel_type');
195 my $note = $c->request->param('note');
196 my $scope = $c->request->param('scope');
198 my $opts = { 'type' => $relation,
200 $opts->{'annotation'} = $note if $note;
203 my @vectors = $collation->add_relationship( $node, $target, $opts );
204 $c->stash->{'result'} = \@vectors;
205 $m->save( $tradition );
206 } catch( Text::Tradition::Error $e ) {
207 $c->response->status( '403' );
208 $c->stash->{'result'} = { 'error' => $e->message };
210 } elsif( $c->request->method eq 'DELETE' ) {
211 my $node = $c->request->param('source_id');
212 my $target = $c->request->param('target_id');
215 my @vectors = $collation->del_relationship( $node, $target );
216 $m->save( $tradition );
217 $c->stash->{'result'} = \@vectors;
218 } catch( Text::Tradition::Error $e ) {
219 $c->response->status( '403' );
220 $c->stash->{'result'} = { 'error' => $e->message };
223 $c->forward('View::JSON');
228 GET relation/$textid/readings
230 Returns the list of readings defined for this text along with their metadata.
234 my %read_write_keys = (
238 'grammar_invalid' => 1,
243 sub _reading_struct {
245 # Return a JSONable struct of the useful keys. Keys meant to be writable
246 # have a true value; read-only keys have a false value.
248 map { $struct->{$_} = $reading->$_ } keys( %read_write_keys );
250 $struct->{'lexemes'} = [ $reading->lexemes ];
251 # Look up any words related via spelling or orthography
254 return $t eq 'spelling' || $t eq 'orthographic';
257 foreach my $sr ( $reading->related_readings( $sameword ) ) {
258 push( @variants, $sr->text );
260 $struct->{'variants'} = \@variants;
264 sub readings :Chained('text') :PathPart :Args(0) {
265 my( $self, $c ) = @_;
266 my $tradition = delete $c->stash->{'tradition'};
267 my $collation = $tradition->collation;
268 my $m = $c->model('Directory');
269 if( $c->request->method eq 'GET' ) {
271 foreach my $rdg ( $collation->readings ) {
272 $rdginfo->{$rdg->id} = _reading_struct( $rdg );
274 $c->stash->{'result'} = $rdginfo;
276 $c->forward('View::JSON');
281 GET relation/$textid/reading/$id
283 Returns the list of readings defined for this text along with their metadata.
285 POST relation/$textid/reading/$id { request }
287 Alters the reading according to the values in request. Returns 403 Forbidden if
288 the alteration isn't allowed.
292 sub reading :Chained('text') :PathPart :Args(1) {
293 my( $self, $c, $reading_id ) = @_;
294 my $tradition = delete $c->stash->{'tradition'};
295 my $collation = $tradition->collation;
296 my $rdg = $collation->reading( $reading_id );
297 my $m = $c->model('Directory');
298 if( $c->request->method eq 'GET' ) {
299 $c->stash->{'result'} = $rdg ? _reading_struct( $rdg )
300 : { 'error' => "No reading with ID $reading_id" };
301 } elsif ( $c->request->method eq 'POST' ) {
302 # Are we re-lemmatizing?
303 if( $c->request->param('relemmatize') ) {
304 my $nf = $c->request->param('normal_form');
305 # TODO throw error unless $nf
306 $rdg->normal_form( $nf );
307 # TODO throw error if lemmatization fails
310 # Set all the values that we have for the reading.
311 # TODO error handling
312 foreach my $p ( keys %{$c->request->params} ) {
313 if( $p =~ /^morphology_(\d+)$/ ) {
314 # Set the form on the correct lexeme
316 $c->log->debug( "Fetching lexeme $midx" );
317 my $lx = $rdg->lexeme( $midx );
318 my $strrep = $rdg->language . ' // '
319 . $c->request->param( $p );
320 my $idx = $lx->has_form( $strrep );
321 unless( defined $idx ) {
322 # Make the word form and add it to the lexeme.
323 $c->log->debug("Adding new form for $strrep");
324 $idx = $lx->add_matching_form( $strrep ) - 1;
326 $lx->disambiguate( $idx );
327 } elsif( $read_write_keys{$p} ) {
328 my $val = _clean_booleans( $rdg, $p, $c->request->param( $p ) );
333 $m->save( $tradition );
334 $c->stash->{'result'} = _reading_struct( $rdg );
337 $c->forward('View::JSON');
341 sub _clean_booleans {
342 my( $rdg, $param, $val ) = @_;
343 if( $rdg->meta->get_attribute( $param )->type_constraint->name eq 'Bool' ) {
344 $val = 1 if $val eq 'true';
345 $val = undef if $val eq 'false';
352 Attempt to render a view, if needed.
356 sub end : ActionClass('RenderView') {}
364 This library is free software. You can redistribute it and/or modify
365 it under the same terms as Perl itself.
369 __PACKAGE__->meta->make_immutable;