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' }
15 stemmaweb::Controller::Stexaminer - Simple controller for stemma display
19 The stemma analysis tool with the pretty colored table.
25 GET stexaminer/$textid/$stemmaid
27 Renders the application for the text identified by $textid, using the stemma
28 graph identified by $stemmaid.
32 sub index :Path :Args(2) {
33 my( $self, $c, $textid, $stemid ) = @_;
34 my $m = $c->model('Directory');
35 $c->stash->{template} = 'stexaminer.tt';
37 # Make sure the tradition exists and is viewable
38 my $tradition = $m->tradition( $textid );
39 unless( $tradition ) {
40 $c->response->status( 404 );
41 $c->stash->{'error'} = "No tradition with ID $textid";
44 my $ok = _check_permission( $c, $tradition );
47 if( $stemid eq 'help' ) {
48 # Just show the 'Help/About' popup.
49 $c->stash->{template} = 'stexaminer_help.tt';
50 $c->stash->{text_id} = $textid;
51 elsif( $tradition->stemma_count ) {
52 my $stemma = $tradition->stemma( $stemid );
53 my $svgstr = $stemma->as_svg();
55 $c->stash->{svg} = $svgstr;
56 $c->stash->{graphdot} = $stemma->editable({ linesep => ' ' });
57 $c->stash->{text_id} = $textid;
58 $c->stash->{text_title} = $tradition->name;
60 # Get the analysis options
61 my( $use_type1, $ignore_sort ) = ( 0, 'none' );
62 $use_type1 = $c->req->param( 'show_type1' ) ? 1 : 0;
63 $ignore_sort = $c->req->param( 'ignore_variant' ) || '';
64 $c->stash->{'show_type1'} = $use_type1;
65 $c->stash->{'ignore_variant'} = $ignore_sort;
66 # TODO Run the analysis as AJAX from the loaded page.
67 my %analysis_options = (
69 exclude_type1 => !$use_type1 );
70 if( $ignore_sort eq 'spelling' ) {
71 $analysis_options{'merge_types'} = [ qw/ spelling orthographic / ];
72 } elsif( $ignore_sort eq 'orthographic' ) {
73 $analysis_options{'merge_types'} = 'orthographic';
77 my $t = run_analysis( $tradition, %analysis_options );
78 # Stringify the reading groups
79 foreach my $loc ( @{$t->{'variants'}} ) {
80 my $mst = wit_stringify( $loc->{'missing'} );
81 $loc->{'missing'} = $mst;
82 foreach my $rhash ( @{$loc->{'readings'}} ) {
83 my $gst = wit_stringify( $rhash->{'group'} );
84 $rhash->{'group'} = $gst;
85 _stringify_element( $rhash, 'independent_occurrence' );
86 _stringify_element( $rhash, 'reversions' );
87 unless( $rhash->{'text'} ) {
88 $rhash->{'text'} = $rhash->{'readingid'};
92 # Values for TT rendering
93 $c->stash->{variants} = $t->{'variants'};
94 $c->stash->{total} = $t->{'variant_count'};
95 $c->stash->{genealogical} = $t->{'genealogical_count'};
96 $c->stash->{conflict} = $t->{'conflict_count'};
97 # Also make a JSON stash of the data for the statistics tables
98 $c->stash->{reading_statistics} = to_json( $t->{'variants'} );
100 $c->stash->{error} = 'Tradition ' . $tradition->name
101 . 'has no stemma for analysis.';
105 sub _stringify_element {
106 my( $hash, $key ) = @_;
107 return undef unless exists $hash->{$key};
108 if( ref( $hash->{$key} ) eq 'ARRAY' ) {
109 my $str = join( ', ', @{$hash->{$key}} );
110 $hash->{$key} = $str;
114 sub _check_permission {
115 my( $c, $tradition ) = @_;
116 my $user = $c->user_exists ? $c->user->get_object : undef;
118 return 'full' if ( $user->is_admin ||
119 ( $tradition->has_user && $tradition->user->id eq $user->id ) );
121 # Text doesn't belong to us, so maybe it's public?
122 return 'readonly' if $tradition->public;
124 # ...nope. Forbidden!
125 $c->response->status( 403 );
126 $c->stash->{'error'} = 'You do not have permission to view this tradition';
132 POST stexaminer/graphsvg
133 dot: <stemmagraph dot string>
134 layerwits: [ <a.c. witnesses ]
136 Returns an SVG string of the given graph, extended to include the given
141 sub graphsvg :Local {
142 my( $self, $c ) = @_;
143 my $dot = $c->request->param('dot');
144 my @layerwits = $c->request->param('layerwits[]');
145 open my $stemma_fh, '<', \$dot;
146 binmode( $stemma_fh, ':encoding(UTF-8)' );
147 my $tempstemma = Text::Tradition::Stemma->new( 'dot' => $stemma_fh );
150 $svgopts->{'layerwits'} = \@layerwits;
152 $c->stash->{'result'} = $tempstemma->as_svg( $svgopts );
153 $c->forward('View::SVG');
158 Attempt to render a view, if needed.
162 sub end : ActionClass('RenderView') {}
170 This library is free software. You can redistribute it and/or modify
171 it under the same terms as Perl itself.
175 __PACKAGE__->meta->make_immutable;