ff4f69b7c4efc3175da9a57909ed6c0355490e43
[scpubgit/stemmaweb.git] / lib / stemmaweb / Controller / Stexaminer.pm
1 package stemmaweb::Controller::Stexaminer;
2 use Moose;
3 use namespace::autoclean;
4 use Encode qw/ decode_utf8 /;
5 use File::Temp;
6 use JSON;
7 use Text::Tradition::Analysis qw/ run_analysis wit_stringify /;
8 use Text::Tradition::Stemma;
9
10 BEGIN { extends 'Catalyst::Controller' }
11
12 has idp_solver_url => (
13         is => 'ro',
14         isa => 'Str',
15         predicate => 'has_idp_solver_url',
16         );
17
18 has idp_calcdsn => (
19         is => 'ro',
20         isa => 'Str',
21         predicate => 'has_idp_calcdsn',
22         );
23
24 =head1 NAME
25
26 stemmaweb::Controller::Stexaminer - Simple controller for stemma display
27
28 =head1 DESCRIPTION
29
30 The stemma analysis tool with the pretty colored table.
31
32 =head1 METHODS
33
34 =head2 index
35
36  GET stexaminer/$textid/$stemmaid
37  
38 Renders the application for the text identified by $textid, using the stemma
39 graph identified by $stemmaid.
40
41 =cut
42
43 sub index :Path :Args(2) {
44     my( $self, $c, $textid, $stemid ) = @_;
45     my $m = $c->model('Directory');
46         $c->stash->{template} = 'stexaminer.tt'; 
47         
48         # Make sure the tradition exists and is viewable
49         my $tradition = $m->tradition( $textid );
50         unless( $tradition ) {
51                 $c->response->status( 404 );
52                 $c->stash->{'error'} = "No tradition with ID $textid";
53                 return;
54         }       
55         my $ok = _check_permission( $c, $tradition );
56         return unless $ok;
57         
58         if( $stemid eq 'help' ) {
59                 # Just show the 'Help/About' popup.
60                 $c->stash->{template} = 'stexaminer_help.tt';
61                 $c->stash->{text_id} = $textid;
62         } elsif( $tradition->stemma_count ) {
63                 my $stemma = $tradition->stemma( $stemid );
64                 my $svgstr = $stemma->as_svg();
65                 $svgstr =~ s/\n/ /g;
66                 $c->stash->{svg} = $svgstr;
67                 $c->stash->{graphdot} = $stemma->editable({ linesep => ' ' });
68                 $c->stash->{text_id} = $textid;
69                 $c->stash->{text_title} = $tradition->name;
70                 
71                 # Get the analysis options
72                 my( $use_type1, $ignore_sort ) = ( 0, 'none' );
73                 $use_type1 = $c->req->param( 'show_type1' ) ? 1 : 0;
74                 $ignore_sort = $c->req->param( 'ignore_variant' ) || '';
75                 $c->stash->{'show_type1'} = $use_type1;
76                 $c->stash->{'ignore_variant'} = $ignore_sort;
77                 # TODO Run the analysis as AJAX from the loaded page.
78                 my %analysis_options = ( 
79                         stemma_id => $stemid,
80                         exclude_type1 => !$use_type1 );
81                 if( $ignore_sort eq 'spelling' ) {
82                         $analysis_options{'merge_types'} = [ qw/ spelling orthographic / ];
83                 } elsif( $ignore_sort eq 'orthographic' ) {
84                         $analysis_options{'merge_types'} = 'orthographic';
85                 }
86                 if( $self->has_idp_solver_url ) {
87                         $analysis_options{'solver_url'} = $self->idp_solver_url;
88                 } elsif( $self->has_idp_calcdsn ) {
89                         $analysis_options{'calcdsn'} = $self->idp_calcdsn;
90                 }
91
92                 my $t = run_analysis( $tradition, %analysis_options );
93                 # Stringify the reading groups
94                 foreach my $loc ( @{$t->{'variants'}} ) {
95                         my $mst = wit_stringify( $loc->{'missing'} );
96                         $loc->{'missing'} = $mst;
97                         foreach my $rhash ( @{$loc->{'readings'}} ) {
98                                 my $gst = wit_stringify( $rhash->{'group'} );
99                                 $rhash->{'group'} = $gst;
100                                 _stringify_element( $rhash, 'independent_occurrence' );
101                                 _stringify_element( $rhash, 'reversions' );
102                                 unless( $rhash->{'text'} ) {
103                                         $rhash->{'text'} = $rhash->{'readingid'};
104                                 }
105                         }
106                 }
107                 # Values for TT rendering
108                 $c->stash->{variants} = $t->{'variants'};
109                 $c->stash->{total} = $t->{'variant_count'};
110                 $c->stash->{genealogical} = $t->{'genealogical_count'};
111                 $c->stash->{conflict} = $t->{'conflict_count'};         
112                 # Also make a JSON stash of the data for the statistics tables
113                 $c->stash->{reading_statistics} = to_json( $t->{'variants'} );
114         } else {
115                 $c->stash->{error} = 'Tradition ' . $tradition->name 
116                         . 'has no stemma for analysis.';
117         }
118 }
119
120 sub _stringify_element {
121         my( $hash, $key ) = @_;
122         return undef unless exists $hash->{$key};
123         if( ref( $hash->{$key} ) eq 'ARRAY' ) {
124                 my $str = join( ', ', @{$hash->{$key}} );
125                 $hash->{$key} = $str;
126         }
127 }
128
129 sub _check_permission {
130         my( $c, $tradition ) = @_;
131     my $user = $c->user_exists ? $c->user->get_object : undef;
132     if( $user ) {
133         return 'full' if ( $user->is_admin || 
134                 ( $tradition->has_user && $tradition->user->id eq $user->id ) );
135     }
136         # Text doesn't belong to us, so maybe it's public?
137         return 'readonly' if $tradition->public;
138
139         # ...nope. Forbidden!
140         $c->response->status( 403 );
141         $c->stash->{'error'} = 'You do not have permission to view this tradition';
142         return 0;
143 }
144
145 =head2 graphsvg
146
147   POST stexaminer/graphsvg
148         dot: <stemmagraph dot string> 
149         layerwits: [ <a.c. witnesses ] 
150   
151 Returns an SVG string of the given graph, extended to include the given 
152 layered witnesses.
153
154 =cut
155
156 sub graphsvg :Local {
157         my( $self, $c ) = @_;
158         my $dot = $c->request->param('dot');
159         my @layerwits = $c->request->param('layerwits[]');
160         open my $stemma_fh, '<', \$dot;
161         binmode( $stemma_fh, ':encoding(UTF-8)' );
162         my $tempstemma = Text::Tradition::Stemma->new( 'dot' => $stemma_fh );
163         my $svgopts = {};
164         if( @layerwits ) {
165                 $svgopts->{'layerwits'} = \@layerwits;
166         }
167         $c->stash->{'result'} = $tempstemma->as_svg( $svgopts );
168         $c->forward('View::SVG');
169 }
170
171 =head2 end
172
173 Attempt to render a view, if needed.
174
175 =cut
176
177 sub end : ActionClass('RenderView') {}
178
179 =head1 AUTHOR
180
181 Tara L Andrews
182
183 =head1 LICENSE
184
185 This library is free software. You can redistribute it and/or modify
186 it under the same terms as Perl itself.
187
188 =cut
189
190 __PACKAGE__->meta->make_immutable;
191
192 1;