Commit | Line | Data |
b8a92065 |
1 | package stemmaweb::Controller::Relation; |
2 | use Moose; |
3 | use namespace::autoclean; |
b28e606e |
4 | use TryCatch; |
b8a92065 |
5 | |
6 | BEGIN { extends 'Catalyst::Controller' } |
7 | |
8 | |
9 | =head1 NAME |
10 | |
11 | stemmaweb::Controller::Relation - Controller for the relationship mapper |
12 | |
13 | =head1 DESCRIPTION |
14 | |
b28e606e |
15 | The reading relationship mapper with draggable nodes. |
b8a92065 |
16 | |
17 | =head1 METHODS |
18 | |
b28e606e |
19 | =head2 index |
20 | |
b8a92065 |
21 | GET relation/$textid |
22 | |
23 | Renders the application for the text identified by $textid. |
24 | |
b8a92065 |
25 | =cut |
26 | |
9529f69c |
27 | sub index :Path :Args(0) { |
28 | my( $self, $c ) = @_; |
b28e606e |
29 | $c->stash->{'template'} = 'relate.tt'; |
30 | } |
31 | |
e847b186 |
32 | =head2 help |
33 | |
34 | GET relation/help |
35 | |
36 | Returns the help window HTML. |
37 | |
38 | =cut |
39 | |
40 | sub help :Local :Args(0) { |
41 | my( $self, $c ) = @_; |
42 | $c->stash->{'template'} = 'relatehelp.tt'; |
43 | } |
44 | |
9529f69c |
45 | =head2 definitions |
b28e606e |
46 | |
9c2e7b80 |
47 | GET relation/definitions |
b28e606e |
48 | |
49 | Returns a data structure giving the valid types and scopes for a relationship. |
50 | |
51 | =cut |
52 | |
9c2e7b80 |
53 | sub definitions :Local :Args(0) { |
b28e606e |
54 | my( $self, $c ) = @_; |
17b660e6 |
55 | my $valid_relationships = [ qw/ spelling orthographic grammatical lexical transposition / ]; |
b28e606e |
56 | my $valid_scopes = [ qw/ local global / ]; |
57 | $c->stash->{'result'} = { 'types' => $valid_relationships, 'scopes' => $valid_scopes }; |
58 | $c->forward('View::JSON'); |
b8a92065 |
59 | } |
60 | |
9529f69c |
61 | =head2 text |
b28e606e |
62 | |
9529f69c |
63 | GET relation/$textid/ |
64 | |
65 | Runs the relationship mapper for the specified text ID. |
66 | |
b28e606e |
67 | =cut |
68 | |
9529f69c |
69 | sub text :Chained('/') :PathPart('relation') :CaptureArgs(1) { |
70 | my( $self, $c, $textid ) = @_; |
13aa153c |
71 | # If the tradition has more than 500 ranks or so, split it up. |
72 | my $tradition = $c->model('Directory')->tradition( $textid ); |
7562a27b |
73 | # Account for a bad interaction between FastCGI and KiokuDB |
74 | unless( $tradition->collation->tradition ) { |
75 | $c->log->warn( "Fixing broken tradition link" ); |
76 | $tradition->collation->_set_tradition( $tradition ); |
77 | $c->model('Directory')->save( $tradition ); |
78 | } |
d58766c0 |
79 | # See how big the tradition is. Edges are more important than nodes |
80 | # when it comes to rendering difficulty. |
81 | my $numnodes = scalar $tradition->collation->readings; |
82 | my $numedges = scalar $tradition->collation->paths; |
13aa153c |
83 | my $length = $tradition->collation->end->rank; |
d58766c0 |
84 | # We should display no more than roughly 500 nodes, or roughly 700 |
85 | # edges, at a time. |
86 | my $segments = $numnodes / 500; |
87 | if( $numedges / 700 > $segments ) { |
88 | $segments = $numedges / 700; |
89 | } |
90 | my $segsize = sprintf( "%.0f", $length / $segments ); |
91 | my $margin = sprintf( "%.0f", $segsize / 10 ); |
92 | if( $segments > 1 ) { |
13aa153c |
93 | # Segment the tradition in order not to overload the browser. |
13aa153c |
94 | my @divs; |
95 | my $r = 0; |
d58766c0 |
96 | while( $r + $margin < $length ) { |
13aa153c |
97 | push( @divs, $r ); |
d58766c0 |
98 | $r += $segsize; |
13aa153c |
99 | } |
100 | $c->stash->{'textsegments'} = []; |
d58766c0 |
101 | $c->stash->{'segsize'} = $segsize; |
102 | $c->stash->{'margin'} = $margin; |
ea8e8b3c |
103 | foreach my $i ( 0..$#divs ) { |
104 | my $seg = { 'start' => $divs[$i] }; |
105 | $seg->{'display'} = "Segment " . ($i+1); |
13aa153c |
106 | push( @{$c->stash->{'textsegments'}}, $seg ); |
107 | } |
108 | } |
109 | $c->stash->{'textid'} = $textid; |
110 | $c->stash->{'tradition'} = $tradition; |
9529f69c |
111 | } |
112 | |
113 | sub main :Chained('text') :PathPart('') :Args(0) { |
b28e606e |
114 | my( $self, $c ) = @_; |
13aa153c |
115 | my $startseg = $c->req->param('start'); |
9c2e7b80 |
116 | my $tradition = delete $c->stash->{'tradition'}; |
117 | my $collation = $tradition->collation; |
13aa153c |
118 | my $svgopts; |
119 | if( $startseg ) { |
d58766c0 |
120 | # Only render the subgraph from startseg to endseg or to END, |
13aa153c |
121 | # whichever is less. |
d58766c0 |
122 | my $endseg = $startseg + $c->stash->{'segsize'} + $c->stash->{'margin'}; |
13aa153c |
123 | $svgopts = { 'from' => $startseg }; |
d58766c0 |
124 | $svgopts->{'to'} = $endseg if $endseg < $collation->end->rank; |
13aa153c |
125 | } elsif( exists $c->stash->{'textsegments'} ) { |
126 | # This is the unqualified load of a long tradition. We implicitly start |
127 | # at zero, but go only as far as 550. |
d58766c0 |
128 | my $endseg = $c->stash->{'segsize'} + $c->stash->{'margin'}; |
ea8e8b3c |
129 | $startseg = 0; |
d58766c0 |
130 | $svgopts = { 'to' => $endseg }; |
13aa153c |
131 | } |
132 | my $svg_str = $collation->as_svg( $svgopts ); |
9529f69c |
133 | $svg_str =~ s/\n//gs; |
ea8e8b3c |
134 | $c->stash->{'startseg'} = $startseg if defined $startseg; |
9529f69c |
135 | $c->stash->{'svg_string'} = $svg_str; |
136 | $c->stash->{'text_title'} = $tradition->name; |
137 | $c->stash->{'template'} = 'relate.tt'; |
b28e606e |
138 | } |
139 | |
140 | =head2 relationships |
141 | |
13aa153c |
142 | GET relation/$textid/relationships |
9529f69c |
143 | |
144 | Returns the list of relationships defined for this text. |
b28e606e |
145 | |
13aa153c |
146 | POST relation/$textid/relationships { request } |
9529f69c |
147 | |
148 | Attempts to define the requested relationship within the text. Returns 200 on |
149 | success or 403 on error. |
b28e606e |
150 | |
13aa153c |
151 | DELETE relation/$textid/relationships { request } |
9529f69c |
152 | |
b28e606e |
153 | |
154 | =cut |
155 | |
9529f69c |
156 | sub relationships :Chained('text') :PathPart :Args(0) { |
b28e606e |
157 | my( $self, $c ) = @_; |
6d124a83 |
158 | my $tradition = delete $c->stash->{'tradition'}; |
159 | my $collation = $tradition->collation; |
cdd592f3 |
160 | my $m = $c->model('Directory'); |
9529f69c |
161 | if( $c->request->method eq 'GET' ) { |
162 | my @pairs = $collation->relationships; # returns the edges |
163 | my @all_relations; |
164 | foreach my $p ( @pairs ) { |
165 | my $relobj = $collation->relations->get_relationship( @$p ); |
545163a2 |
166 | next if $relobj->type eq 'collated'; # Don't show these |
7562a27b |
167 | next if $p->[0] eq $p->[1]; # HACK until bugfix |
69a19c91 |
168 | my $relhash = { source => $p->[0], target => $p->[1], |
169 | type => $relobj->type, scope => $relobj->scope }; |
170 | $relhash->{'note'} = $relobj->annotation if $relobj->has_annotation; |
171 | push( @all_relations, $relhash ); |
9529f69c |
172 | } |
173 | $c->stash->{'result'} = \@all_relations; |
174 | } elsif( $c->request->method eq 'POST' ) { |
175 | my $node = $c->request->param('source_id'); |
176 | my $target = $c->request->param('target_id'); |
177 | my $relation = $c->request->param('rel_type'); |
178 | my $note = $c->request->param('note'); |
179 | my $scope = $c->request->param('scope'); |
180 | |
181 | my $opts = { 'type' => $relation, |
69a19c91 |
182 | 'scope' => $scope }; |
183 | $opts->{'annotation'} = $note if $note; |
9529f69c |
184 | |
185 | try { |
186 | my @vectors = $collation->add_relationship( $node, $target, $opts ); |
187 | $c->stash->{'result'} = \@vectors; |
cdd592f3 |
188 | $m->save( $tradition ); |
9529f69c |
189 | } catch( Text::Tradition::Error $e ) { |
190 | $c->response->status( '403' ); |
191 | $c->stash->{'result'} = { 'error' => $e->message }; |
192 | } |
193 | } elsif( $c->request->method eq 'DELETE' ) { |
194 | my $node = $c->request->param('source_id'); |
195 | my $target = $c->request->param('target_id'); |
196 | |
197 | try { |
198 | my @vectors = $collation->del_relationship( $node, $target ); |
cdd592f3 |
199 | $m->save( $tradition ); |
9529f69c |
200 | $c->stash->{'result'} = \@vectors; |
201 | } catch( Text::Tradition::Error $e ) { |
202 | $c->response->status( '403' ); |
203 | $c->stash->{'result'} = { 'error' => $e->message }; |
204 | } |
b28e606e |
205 | } |
b28e606e |
206 | $c->forward('View::JSON'); |
207 | } |
9529f69c |
208 | |
b28e606e |
209 | |
b8a92065 |
210 | =head2 end |
211 | |
212 | Attempt to render a view, if needed. |
213 | |
214 | =cut |
215 | |
216 | sub end : ActionClass('RenderView') {} |
217 | |
218 | =head1 AUTHOR |
219 | |
220 | Tara L Andrews |
221 | |
222 | =head1 LICENSE |
223 | |
224 | This library is free software. You can redistribute it and/or modify |
225 | it under the same terms as Perl itself. |
226 | |
227 | =cut |
228 | |
229 | __PACKAGE__->meta->make_immutable; |
230 | |
231 | 1; |