fix logic bugs in stexaminer with explicit stemma specification
[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::Collation;
9use Text::Tradition::Stemma;
b8a92065 10
11BEGIN { extends 'Catalyst::Controller' }
12
13
14=head1 NAME
15
16stemmaweb::Controller::Stexaminer - Simple controller for stemma display
17
18=head1 DESCRIPTION
19
20The stemma analysis tool with the pretty colored table.
21
22=head1 METHODS
23
be8bf746 24=head2 index
25
98a45925 26 GET stexaminer/$textid/$stemmaid
b8a92065 27
98a45925 28Renders the application for the text identified by $textid, using the stemma
29graph identified by $stemmaid.
b8a92065 30
b8a92065 31=cut
32
98a45925 33sub index :Path :Args(2) {
34 my( $self, $c, $textid, $stemid ) = @_;
b8a92065 35 my $m = $c->model('Directory');
36 my $tradition = $m->tradition( $textid );
96eae440 37 my $ok = _check_permission( $c, $tradition );
38 return unless $ok;
9c2e7b80 39 if( $tradition->stemma_count ) {
847e1ac6 40 my $stemma = $tradition->stemma( $stemid );
be8bf746 41 $c->stash->{svg} = $stemma->as_svg( { size => [ 600, 350 ] } );
42 $c->stash->{graphdot} = $stemma->editable({ linesep => ' ' });
9c2e7b80 43 $c->stash->{text_title} = $tradition->name;
44 $c->stash->{template} = 'stexaminer.tt';
0c236c96 45
46 # Get the analysis options
47 my( $use_type1, $ignore_sort ) = ( 0, 'none' );
ccbe9315 48 $use_type1 = $c->req->param( 'show_type1' ) ? 1 : 0;
49 $ignore_sort = $c->req->param( 'ignore_variant' ) || '';
0c236c96 50 $c->stash->{'show_type1'} = $use_type1;
51 $c->stash->{'ignore_variant'} = $ignore_sort;
9c2e7b80 52 # TODO Run the analysis as AJAX from the loaded page.
98a45925 53 my %analysis_options = (
54 stemma_id => $stemid,
55 exclude_type1 => !$use_type1 );
0c236c96 56 if( $ignore_sort eq 'spelling' ) {
e816672a 57 $analysis_options{'merge_types'} = [ qw/ spelling orthographic / ];
0c236c96 58 } elsif( $ignore_sort eq 'orthographic' ) {
e816672a 59 $analysis_options{'merge_types'} = 'orthographic';
0c236c96 60 }
ccbe9315 61
62 # Do the deed
0c236c96 63 my $t = run_analysis( $tradition, %analysis_options );
0737e7dd 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;
f7371955 71 _stringify_element( $rhash, 'independent_occurrence' );
72 _stringify_element( $rhash, 'reversions' );
5089c3b7 73 unless( $rhash->{'text'} ) {
74 $rhash->{'text'} = $rhash->{'readingid'};
75 }
0737e7dd 76 }
77 }
d5514865 78 # Values for TT rendering
9c2e7b80 79 $c->stash->{variants} = $t->{'variants'};
80 $c->stash->{total} = $t->{'variant_count'};
81 $c->stash->{genealogical} = $t->{'genealogical_count'};
d5514865 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'} );
9c2e7b80 85 } else {
86 $c->stash->{error} = 'Tradition ' . $tradition->name
87 . 'has no stemma for analysis.';
88 }
b8a92065 89}
90
f7371955 91sub _stringify_element {
92 my( $hash, $key ) = @_;
8603127a 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 }
f7371955 98}
99
96eae440 100sub _check_permission {
101 my( $c, $tradition ) = @_;
102 my $user = $c->user_exists ? $c->user->get_object : undef;
103 if( $user ) {
847e1ac6 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!
080f8a02 111 $c->response->status( 403 );
847e1ac6 112 $c->response->body( 'You do not have permission to view this tradition' );
113 $c->forward('View::Plain');
080f8a02 114 return 0;
96eae440 115}
116
be8bf746 117=head2 graphsvg
118
119 POST stexaminer/graphsvg
120 dot: <stemmagraph dot string>
121 layerwits: [ <a.c. witnesses ]
122
123Returns an SVG string of the given graph, extended to include the given
124layered witnesses.
125
126=cut
127
128sub 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 $emptycoll = Text::Tradition::Collation->new();
135 my $tempstemma = Text::Tradition::Stemma->new(
136 collation => $emptycoll, 'dot' => $stemma_fh );
137 my $svgopts = { size => [ 600, 350 ] };
138 if( @layerwits ) {
139 $svgopts->{'layerwits'} = \@layerwits;
140 }
141 $c->stash->{'result'} = $tempstemma->as_svg( $svgopts );
142 $c->forward('View::SVG');
143}
144
b8a92065 145=head2 end
146
147Attempt to render a view, if needed.
148
149=cut
150
151sub end : ActionClass('RenderView') {}
152
153=head1 AUTHOR
154
155Tara L Andrews
156
157=head1 LICENSE
158
159This library is free software. You can redistribute it and/or modify
160it under the same terms as Perl itself.
161
162=cut
163
164__PACKAGE__->meta->make_immutable;
165
1661;