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