X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2Fstemmaweb%2FController%2FStexaminer.pm;h=86020a1530a25482db1e8a989d1f5cf535790b33;hb=080f8a02e4af4c545e8ecff2166931e31b687683;hp=1449ad209eed3528c4e3fafe98ea182144db3188;hpb=0737e7ddd147f7cc1c9b5ef087ca04dcce238b54;p=scpubgit%2Fstemmaweb.git diff --git a/lib/stemmaweb/Controller/Stexaminer.pm b/lib/stemmaweb/Controller/Stexaminer.pm index 1449ad2..86020a1 100644 --- a/lib/stemmaweb/Controller/Stexaminer.pm +++ b/lib/stemmaweb/Controller/Stexaminer.pm @@ -1,9 +1,12 @@ package stemmaweb::Controller::Stexaminer; use Moose; use namespace::autoclean; +use Encode qw/ decode_utf8 /; use File::Temp; use JSON; use Text::Tradition::Analysis qw/ run_analysis wit_stringify /; +use Text::Tradition::Collation; +use Text::Tradition::Stemma; BEGIN { extends 'Catalyst::Controller' } @@ -18,26 +21,46 @@ The stemma analysis tool with the pretty colored table. =head1 METHODS - GET stexaminer/$textid - -Renders the application for the text identified by $textid. - =head2 index + GET stexaminer/$textid/$stemmaid + +Renders the application for the text identified by $textid, using the stemma +graph identified by $stemmaid. + =cut -sub index :Path :Args(1) { - my( $self, $c, $textid ) = @_; +sub index :Path :Args(2) { + my( $self, $c, $textid, $stemid ) = @_; my $m = $c->model('Directory'); my $tradition = $m->tradition( $textid ); + my $ok = _check_permission( $c, $tradition ); + return unless $ok; if( $tradition->stemma_count ) { my $stemma = $tradition->stemma(0); - # TODO Think about caching the stemma in a session - $c->stash->{svg} = $stemma->as_svg; + $c->stash->{svg} = $stemma->as_svg( { size => [ 600, 350 ] } ); + $c->stash->{graphdot} = $stemma->editable({ linesep => ' ' }); $c->stash->{text_title} = $tradition->name; $c->stash->{template} = 'stexaminer.tt'; + + # Get the analysis options + my( $use_type1, $ignore_sort ) = ( 0, 'none' ); + $use_type1 = $c->req->param( 'show_type1' ) ? 1 : 0; + $ignore_sort = $c->req->param( 'ignore_variant' ) || ''; + $c->stash->{'show_type1'} = $use_type1; + $c->stash->{'ignore_variant'} = $ignore_sort; # TODO Run the analysis as AJAX from the loaded page. - my $t = run_analysis( $tradition ); + my %analysis_options = ( + stemma_id => $stemid, + exclude_type1 => !$use_type1 ); + if( $ignore_sort eq 'spelling' ) { + $analysis_options{'merge_types'} = [ qw/ spelling orthographic / ]; + } elsif( $ignore_sort eq 'orthographic' ) { + $analysis_options{'merge_types'} = 'orthographic'; + } + + # Do the deed + my $t = run_analysis( $tradition, %analysis_options ); # Stringify the reading groups foreach my $loc ( @{$t->{'variants'}} ) { my $mst = wit_stringify( $loc->{'missing'} ); @@ -45,18 +68,83 @@ sub index :Path :Args(1) { foreach my $rhash ( @{$loc->{'readings'}} ) { my $gst = wit_stringify( $rhash->{'group'} ); $rhash->{'group'} = $gst; + _stringify_element( $rhash, 'independent_occurrence' ); + _stringify_element( $rhash, 'reversions' ); + unless( $rhash->{'text'} ) { + $rhash->{'text'} = $rhash->{'readingid'}; + } } } + # Values for TT rendering $c->stash->{variants} = $t->{'variants'}; $c->stash->{total} = $t->{'variant_count'}; $c->stash->{genealogical} = $t->{'genealogical_count'}; - $c->stash->{conflict} = $t->{'conflict_count'}; + $c->stash->{conflict} = $t->{'conflict_count'}; + # Also make a JSON stash of the data for the statistics tables + $c->stash->{reading_statistics} = to_json( $t->{'variants'} ); } else { $c->stash->{error} = 'Tradition ' . $tradition->name . 'has no stemma for analysis.'; } } +sub _stringify_element { + my( $hash, $key ) = @_; + return undef unless exists $hash->{$key}; + if( ref( $hash->{$key} ) eq 'ARRAY' ) { + my $str = join( ', ', @{$hash->{$key}} ); + $hash->{$key} = $str; + } +} + +sub _check_permission { + my( $c, $tradition ) = @_; + my $user = $c->user_exists ? $c->user->get_object : undef; + if( $user ) { + $c->stash->{'permission'} = 'full' + if( $user->is_admin || $tradition->user->id eq $user->id ); + return 1; + } + # Is it public? + if( $tradition->public ) { + $c->stash->{'permission'} = 'readonly'; + return 1; + } + # Forbidden! + $c->response->status( 403 ); + $c->response->body( 'You do not have permission to view this tradition.' ); + $c->detach( 'View::Plain' ); + return 0; +} + +=head2 graphsvg + + POST stexaminer/graphsvg + dot: + layerwits: [ request->param('dot'); + my @layerwits = $c->request->param('layerwits[]'); + open my $stemma_fh, '<', \$dot; + binmode( $stemma_fh, ':encoding(UTF-8)' ); + my $emptycoll = Text::Tradition::Collation->new(); + my $tempstemma = Text::Tradition::Stemma->new( + collation => $emptycoll, 'dot' => $stemma_fh ); + my $svgopts = { size => [ 600, 350 ] }; + if( @layerwits ) { + $svgopts->{'layerwits'} = \@layerwits; + } + $c->stash->{'result'} = $tempstemma->as_svg( $svgopts ); + $c->forward('View::SVG'); +} + =head2 end Attempt to render a view, if needed.