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