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