query dynamically for relationship types in rel mapper
[scpubgit/stemmaweb.git] / lib / stemmaweb / Controller / Relation.pm
1 package stemmaweb::Controller::Relation;
2 use Moose;
3 use Module::Load;
4 use namespace::autoclean;
5 use TryCatch;
6
7 BEGIN { extends 'Catalyst::Controller' }
8
9
10 =head1 NAME
11
12 stemmaweb::Controller::Relation - Controller for the relationship mapper
13
14 =head1 DESCRIPTION
15
16 The reading relationship mapper with draggable nodes.
17
18 =head1 METHODS
19
20 =head2 index
21
22  GET relation/$textid
23  
24 Renders the application for the text identified by $textid.
25
26 =cut
27
28 sub index :Path :Args(0) {
29         my( $self, $c ) = @_;
30         $c->stash->{'template'} = 'relate.tt';
31 }
32
33 =head2 text
34
35  GET relation/$textid/
36  
37  Runs the relationship mapper for the specified text ID.
38  
39 =cut
40
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');
48                 return;
49         }
50         
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 );
56     }
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 );
60     return unless $ok;
61
62         $c->stash->{'textid'} = $textid;
63         $c->stash->{'tradition'} = $tradition;
64 }
65
66 sub main :Chained('text') :PathPart('') :Args(0) {
67         my( $self, $c ) = @_;
68         my $tradition = delete $c->stash->{'tradition'};
69         my $collation = $tradition->collation;
70         
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
77         # edges, at a time.
78         my $segments = $numnodes / 500;
79         if( $numedges / 700 > $segments ) {
80                 $segments = $numedges / 700;
81         }
82         my $segsize = sprintf( "%.0f", $length / $segments );
83         my $margin = sprintf( "%.0f", $segsize / 10 );
84         if( $segments > 1 ) {
85                 # Segment the tradition in order not to overload the browser.
86                 my @divs;
87                 my $r = 0;
88                 while( $r + $margin < $length ) {
89                         push( @divs, $r );
90                         $r += $segsize;
91                 }
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 );
97                 }
98         }
99         my $startseg = $c->req->param('start');
100         my $svgopts;
101         if( $startseg ) {
102                 # Only render the subgraph from startseg to endseg or to END,
103                 # whichever is less.
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;
111                 $startseg = 0;
112                 $svgopts = { 'to' => $endseg };
113         }
114         # Spit out the SVG
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;
123         } else {
124                 $c->stash->{'text_lang'} = 'Default';
125         }
126         $c->stash->{'template'} = 'relate.tt';
127 }
128
129 =head2 definitions
130
131  GET relation/$textid/definitions
132  
133 Returns a data structure giving the valid types and scopes for a relationship in
134 this tradition.
135
136 =cut
137
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 
147         };
148         $c->forward('View::JSON');
149 }
150
151 =head2 help
152
153  GET relation/help/$language
154
155 Returns the help window HTML.
156
157 =cut
158
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;
164                 try {
165                         load( $mod );
166                 } catch {
167                         $c->log->debug("Warning: could not load $mod");
168                 }
169                 my $has_mod = $mod->can('morphology_tags');
170                 if( $has_mod ) {
171                         my $tagset = &$has_mod;
172                         $c->stash->{'tagset'} = $tagset;
173                 }
174         }
175         $c->stash->{'template'} = 'relatehelp.tt';
176 }
177
178 =head2 relationships
179
180  GET relation/$textid/relationships
181
182 Returns the list of relationships defined for this text.
183
184  POST relation/$textid/relationships { request }
185  
186 Attempts to define the requested relationship within the text. Returns 200 on
187 success or 403 on error.
188
189  DELETE relation/$textid/relationships { request }
190  
191
192 =cut
193
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 );
198         return unless $ok;
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
203                 my @all_relations;
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 );
212                 }
213                 $c->stash->{'result'} = \@all_relations;
214         } else {
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' );
226                         }       
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');
232                 
233                         my $opts = { 'type' => $relation, 'propagate' => 1 };
234                         $opts->{'scope'} = $scope if $scope;
235                         $opts->{'annotation'} = $note if $note;
236                         
237                         try {
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 };
244                         }
245                 } elsif( $c->request->method eq 'DELETE' ) {
246                         my $node = $c->request->param('source_id');
247                         my $target = $c->request->param('target_id');
248                 
249                         try {
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 };
256                         }       
257                 }
258         }
259         $c->forward('View::JSON');
260 }
261
262 =head2 readings
263
264  GET relation/$textid/readings
265
266 Returns the list of readings defined for this text along with their metadata.
267
268 =cut
269
270 my %read_write_keys = (
271         'id' => 0,
272         'text' => 0,
273         'is_meta' => 0,
274         'grammar_invalid' => 1,
275         'is_nonsense' => 1,
276         'normal_form' => 1,
277 );
278
279 sub _reading_struct {
280         my( $reading ) = @_;
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.
283         my $struct = {};
284         map { $struct->{$_} = $reading->$_ if $reading->can( $_ ) } keys( %read_write_keys );
285         # Special case
286         $struct->{'lexemes'} = $reading->can( 'lexemes' ) ? [ $reading->lexemes ] : [];
287         # Look up any words related via spelling or orthography
288         my $sameword = sub { 
289                 my $t = $_[0]->type;
290                 return $t eq 'spelling' || $t eq 'orthographic';
291         };
292         my @variants;
293         foreach my $sr ( $reading->related_readings( $sameword ) ) {
294                 push( @variants, $sr->text );
295         }
296         $struct->{'variants'} = \@variants;
297         return $struct;
298 }
299
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 );
304         return unless $ok;
305         my $collation = $tradition->collation;
306         my $m = $c->model('Directory');
307         if( $c->request->method eq 'GET' ) {
308                 my $rdginfo = {};
309                 foreach my $rdg ( $collation->readings ) {
310                         $rdginfo->{$rdg->id} = _reading_struct( $rdg );
311                 }
312                 $c->stash->{'result'} = $rdginfo;
313         }
314         $c->forward('View::JSON');
315 }
316
317 =head2 reading
318
319  GET relation/$textid/reading/$id
320
321 Returns the list of readings defined for this text along with their metadata.
322
323  POST relation/$textid/reading/$id { request }
324  
325 Alters the reading according to the values in request. Returns 403 Forbidden if
326 the alteration isn't allowed.
327
328 =cut
329
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');
345                         return;
346                 }
347                 my $errmsg;
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
356                                 $rdg->lemmatize();
357                         } else {
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;
365                                                 my $midx = $1;
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.
371                                                         try {
372                                                                 $idx = $lx->add_matching_form( $strrep ) - 1;
373                                                         } catch( Text::Tradition::Error $e ) {
374                                                                 $c->response->status( '403' );
375                                                                 $errmsg = $e->message;
376                                                         } catch {
377                                                                 # Something else went wrong, probably a Moose error
378                                                                 $c->response->status( '403' );
379                                                                 $errmsg = 'Something went wrong with the request';      
380                                                         }
381                                                 }
382                                                 $lx->disambiguate( $idx ) if defined $idx;
383                                         } elsif( $read_write_keys{$p} ) {
384                                                 my $val = _clean_booleans( $rdg, $p, $c->request->param( $p ) );
385                                                 $rdg->$p( $val );
386                                         }
387                                 }               
388                         }
389                         $m->save( $rdg );
390                 } else {
391                         $errmsg = "Reading does not exist or cannot be morphologized";
392                 }
393                 $c->stash->{'result'} = $errmsg ? { 'error' => $errmsg }
394                         : _reading_struct( $rdg );
395
396         }
397         $c->forward('View::JSON');
398
399 }
400
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?
405     if( $user ) {
406                 if( $user->is_admin || 
407                         ( $tradition->has_user && $tradition->user->id eq $user->id ) ) {
408                         $c->stash->{'permission'} = 'full';
409                         return 1;
410                 }
411     } 
412     # Is it public?
413     if( $tradition->public ) {
414         $c->stash->{'permission'} = 'readonly';
415         return 1;
416     } 
417         # Forbidden!
418         $c->response->status( 403 );
419         $c->response->body( 'You do not have permission to view this tradition.' );
420         $c->detach( 'View::Plain' );
421         return 0;
422 }
423
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';
429         } 
430         return $val;
431 }
432
433 =head2 end
434
435 Attempt to render a view, if needed.
436
437 =cut
438
439 sub end : ActionClass('RenderView') {}
440
441 =head1 AUTHOR
442
443 Tara L Andrews
444
445 =head1 LICENSE
446
447 This library is free software. You can redistribute it and/or modify
448 it under the same terms as Perl itself.
449
450 =cut
451
452 __PACKAGE__->meta->make_immutable;
453
454 1;