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