1 package stemmaweb::Controller::Stexaminer;
3 use namespace::autoclean;
4 use Encode qw/ decode_utf8 /;
7 use Text::Tradition::Analysis qw/ run_analysis wit_stringify /;
8 use Text::Tradition::Stemma;
10 BEGIN { extends 'Catalyst::Controller' }
12 has idp_solver_url => (
15 predicate => 'has_idp_solver_url',
21 predicate => 'has_idp_calcdsn',
26 stemmaweb::Controller::Stexaminer - Simple controller for stemma display
30 The stemma analysis tool with the pretty colored table.
36 GET stexaminer/$textid/$stemmaid
38 Renders the application for the text identified by $textid, using the stemma
39 graph identified by $stemmaid.
43 sub index :Path :Args(2) {
44 my( $self, $c, $textid, $stemid ) = @_;
45 my $m = $c->model('Directory');
46 $c->stash->{template} = 'stexaminer.tt';
48 # Make sure the tradition exists and is viewable
49 my $tradition = $m->tradition( $textid );
50 unless( $tradition ) {
51 $c->response->status( 404 );
52 $c->stash->{'error'} = "No tradition with ID $textid";
55 my $ok = _check_permission( $c, $tradition );
58 if( $stemid eq 'help' ) {
59 # Just show the 'Help/About' popup.
60 $c->stash->{template} = 'stexaminer_help.tt';
61 $c->stash->{text_id} = $textid;
62 } elsif( $tradition->stemma_count ) {
63 my $stemma = $tradition->stemma( $stemid );
64 my $svgstr = $stemma->as_svg();
66 $c->stash->{svg} = $svgstr;
67 $c->stash->{graphdot} = $stemma->editable({ linesep => ' ' });
68 $c->stash->{text_id} = $textid;
69 $c->stash->{text_title} = $tradition->name;
71 # Get the analysis options
72 my( $use_type1, $ignore_sort ) = ( 0, 'none' );
73 $use_type1 = $c->req->param( 'show_type1' ) ? 1 : 0;
74 $ignore_sort = $c->req->param( 'ignore_variant' ) || '';
75 $c->stash->{'show_type1'} = $use_type1;
76 $c->stash->{'ignore_variant'} = $ignore_sort;
77 # TODO Run the analysis as AJAX from the loaded page.
78 my %analysis_options = (
80 exclude_type1 => !$use_type1 );
81 if( $ignore_sort eq 'spelling' ) {
82 $analysis_options{'merge_types'} = [ qw/ spelling orthographic / ];
83 } elsif( $ignore_sort eq 'orthographic' ) {
84 $analysis_options{'merge_types'} = 'orthographic';
86 if( $self->has_idp_solver_url ) {
87 $analysis_options{'solver_url'} = $self->idp_solver_url;
88 } elsif( $self->has_idp_calcdsn ) {
89 $analysis_options{'calcdsn'} = $self->idp_calcdsn;
92 my $t = run_analysis( $tradition, %analysis_options );
93 # Stringify the reading groups
94 foreach my $loc ( @{$t->{'variants'}} ) {
95 my $mst = wit_stringify( $loc->{'missing'} );
96 $loc->{'missing'} = $mst;
97 foreach my $rhash ( @{$loc->{'readings'}} ) {
98 my $gst = wit_stringify( $rhash->{'group'} );
99 $rhash->{'group'} = $gst;
100 _stringify_element( $rhash, 'independent_occurrence' );
101 _stringify_element( $rhash, 'reversions' );
102 unless( $rhash->{'text'} ) {
103 $rhash->{'text'} = $rhash->{'readingid'};
107 # Values for TT rendering
108 $c->stash->{variants} = $t->{'variants'};
109 $c->stash->{total} = $t->{'variant_count'};
110 $c->stash->{genealogical} = $t->{'genealogical_count'};
111 $c->stash->{conflict} = $t->{'conflict_count'};
112 # Also make a JSON stash of the data for the statistics tables
113 $c->stash->{reading_statistics} = to_json( $t->{'variants'} );
115 $c->stash->{error} = 'Tradition ' . $tradition->name
116 . 'has no stemma for analysis.';
120 sub _stringify_element {
121 my( $hash, $key ) = @_;
122 return undef unless exists $hash->{$key};
123 if( ref( $hash->{$key} ) eq 'ARRAY' ) {
124 my $str = join( ', ', @{$hash->{$key}} );
125 $hash->{$key} = $str;
129 sub _check_permission {
130 my( $c, $tradition ) = @_;
131 my $user = $c->user_exists ? $c->user->get_object : undef;
133 return 'full' if ( $user->is_admin ||
134 ( $tradition->has_user && $tradition->user->id eq $user->id ) );
136 # Text doesn't belong to us, so maybe it's public?
137 return 'readonly' if $tradition->public;
139 # ...nope. Forbidden!
140 $c->response->status( 403 );
141 $c->stash->{'error'} = 'You do not have permission to view this tradition';
147 POST stexaminer/graphsvg
148 dot: <stemmagraph dot string>
149 layerwits: [ <a.c. witnesses ]
151 Returns an SVG string of the given graph, extended to include the given
156 sub graphsvg :Local {
157 my( $self, $c ) = @_;
158 my $dot = $c->request->param('dot');
159 my @layerwits = $c->request->param('layerwits[]');
160 open my $stemma_fh, '<', \$dot;
161 binmode( $stemma_fh, ':encoding(UTF-8)' );
162 my $tempstemma = Text::Tradition::Stemma->new( 'dot' => $stemma_fh );
165 $svgopts->{'layerwits'} = \@layerwits;
167 $c->stash->{'result'} = $tempstemma->as_svg( $svgopts );
168 $c->forward('View::SVG');
173 Attempt to render a view, if needed.
177 sub end : ActionClass('RenderView') {}
185 This library is free software. You can redistribute it and/or modify
186 it under the same terms as Perl itself.
190 __PACKAGE__->meta->make_immutable;