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::Collation;
9 use Text::Tradition::Stemma;
11 BEGIN { extends 'Catalyst::Controller' }
16 stemmaweb::Controller::Stexaminer - Simple controller for stemma display
20 The stemma analysis tool with the pretty colored table.
26 GET stexaminer/$textid/$stemmaid
28 Renders the application for the text identified by $textid, using the stemma
29 graph identified by $stemmaid.
33 sub index :Path :Args(2) {
34 my( $self, $c, $textid, $stemid ) = @_;
35 my $m = $c->model('Directory');
36 my $tradition = $m->tradition( $textid );
37 my $ok = _check_permission( $c, $tradition );
39 if( $tradition->stemma_count ) {
40 my $stemma = $tradition->stemma(0);
41 $c->stash->{svg} = $stemma->as_svg( { size => [ 600, 350 ] } );
42 $c->stash->{graphdot} = $stemma->editable({ linesep => ' ' });
43 $c->stash->{text_title} = $tradition->name;
44 $c->stash->{template} = 'stexaminer.tt';
46 # Get the analysis options
47 my( $use_type1, $ignore_sort ) = ( 0, 'none' );
48 $use_type1 = $c->req->param( 'show_type1' ) ? 1 : 0;
49 $ignore_sort = $c->req->param( 'ignore_variant' ) || '';
50 $c->stash->{'show_type1'} = $use_type1;
51 $c->stash->{'ignore_variant'} = $ignore_sort;
52 # TODO Run the analysis as AJAX from the loaded page.
53 my %analysis_options = (
55 exclude_type1 => !$use_type1 );
56 if( $ignore_sort eq 'spelling' ) {
57 $analysis_options{'merge_types'} = [ qw/ spelling orthographic / ];
58 } elsif( $ignore_sort eq 'orthographic' ) {
59 $analysis_options{'merge_types'} = 'orthographic';
63 my $t = run_analysis( $tradition, %analysis_options );
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;
71 _stringify_element( $rhash, 'independent_occurrence' );
72 _stringify_element( $rhash, 'reversions' );
73 unless( $rhash->{'text'} ) {
74 $rhash->{'text'} = $rhash->{'readingid'};
78 # Values for TT rendering
79 $c->stash->{variants} = $t->{'variants'};
80 $c->stash->{total} = $t->{'variant_count'};
81 $c->stash->{genealogical} = $t->{'genealogical_count'};
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'} );
86 $c->stash->{error} = 'Tradition ' . $tradition->name
87 . 'has no stemma for analysis.';
91 sub _stringify_element {
92 my( $hash, $key ) = @_;
93 return undef unless exists $hash->{$key};
94 if( ref( $hash->{$key} ) eq 'ARRAY' ) {
95 my $str = join( ', ', @{$hash->{$key}} );
100 sub _check_permission {
101 my( $c, $tradition ) = @_;
102 my $user = $c->user_exists ? $c->user->get_object : undef;
104 $c->stash->{'permission'} = 'full'
105 if( $user->is_admin || $tradition->user->id eq $user->id );
107 } elsif( $tradition->public ) {
108 $c->stash->{'permission'} = 'readonly';
112 $c->response->status( 403 );
113 $c->response->body( 'You do not have permission to view this tradition.' );
114 $c->detach( 'View::Plain' );
121 POST stexaminer/graphsvg
122 dot: <stemmagraph dot string>
123 layerwits: [ <a.c. witnesses ]
125 Returns an SVG string of the given graph, extended to include the given
130 sub graphsvg :Local {
131 my( $self, $c ) = @_;
132 my $dot = $c->request->param('dot');
133 my @layerwits = $c->request->param('layerwits[]');
134 open my $stemma_fh, '<', \$dot;
135 binmode( $stemma_fh, ':encoding(UTF-8)' );
136 my $emptycoll = Text::Tradition::Collation->new();
137 my $tempstemma = Text::Tradition::Stemma->new(
138 collation => $emptycoll, 'dot' => $stemma_fh );
139 my $svgopts = { size => [ 600, 350 ] };
141 $svgopts->{'layerwits'} = \@layerwits;
143 $c->stash->{'result'} = $tempstemma->as_svg( $svgopts );
144 $c->forward('View::SVG');
149 Attempt to render a view, if needed.
153 sub end : ActionClass('RenderView') {}
161 This library is free software. You can redistribute it and/or modify
162 it under the same terms as Perl itself.
166 __PACKAGE__->meta->make_immutable;