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