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