start reorganization of stemmaweb structure
[scpubgit/stemmatology.git] / TreeOfTexts / lib / TreeOfTexts / Controller / Root.pm
CommitLineData
dbcf12a6 1package TreeOfTexts::Controller::Root;
2use Moose;
3use namespace::autoclean;
3837c155 4use Text::Tradition::Analysis qw/ run_analysis /;
5
dbcf12a6 6
7BEGIN { extends 'Catalyst::Controller' }
8
9#
10# Sets the actions in this controller to be registered with no prefix
11# so they function identically to actions created in MyApp.pm
12#
13__PACKAGE__->config(namespace => '');
14
15=head1 NAME
16
17TreeOfTexts::Controller::Root - Root Controller for TreeOfTexts
18
19=head1 DESCRIPTION
20
6b70c348 21Serves up the main container pages.
dbcf12a6 22
6b70c348 23=head1 URLs
dbcf12a6 24
25=head2 index
26
6b70c348 27The root page (/). Serves the main container page, from which the various
28components will be loaded.
dbcf12a6 29
30=cut
31
32sub index :Path :Args(0) {
33 my ( $self, $c ) = @_;
34
6b70c348 35 $c->stash->{template} = 'index.tt';
36}
37
38=head1 Elements of index page
39
40=head2 directory
41
42 GET /directory
43
44Serves a snippet of HTML that lists the available texts. Eventually this will be available texts by user.
45
46=cut
47sub directory :Path :Args(0) {
48 my( $self, $c ) = @_;
3837c155 49 my $m = $c->model('Directory');
6b70c348 50 # TODO not used yet, will load user texts later
51 my $user = $c->request->param( 'user' ) || 'ALL';
52 my @textlist;
12720144 53 foreach my $id ( $m->tradition_ids ) {
3837c155 54 my $data = {
55 'id' => $id,
12720144 56 'name' => $m->name( $id ),
3837c155 57 };
58 push( @all_texts, $data );
59 }
60
6b70c348 61 $c->stash->{texts} = \@textlist;
62 $c->stash->{template} = 'directory.tt';
63}
64
65=head2 alignment
66
67 GET /alignment/$textid
68
69Returns an alignment table for the text specified at $textid.
70
71=cut
72
73sub alignment :Path :Args(1) {
74 my( $self, $c, $textid ) = @_;
75 my $m = $c->model('Directory');
76 my $collation = $m->tradition( $textid )->collation;
77 my $alignment = $collation->make_alignment_table;
78
79 # Turn the table, so that witnesses are by column and the rows
80 # are by rank.
81 my $wits = [ map { $_->{'witness'} } @{$alignment->{'alignment'}} ];
82 my $rows;
83 foreach my $i ( 0 .. $alignment->{'length'} - 1 ) {
84 my @rankrdgs = map { $_->{'tokens'}->[$i]->{'t'} }
85 @{$alignment->{'alignment'}};
86 push( @$rows, { 'rank' => $i+1, 'readings' => \@rankrdgs } );
87 }
88 $c->log->debug( Dumper( $rows ) );
89 $c->stash->{'witnesses'} = $wits;
90 $c->stash->{'table'} = $rows;
91 $c->stash->{'template'} = 'alignment.tt';
92}
93
94=head2 stemma
95
96 GET /stemma/$textid
97 POST /stemma/$textid, { 'dot' => $dot_string }
98
99Returns an SVG representation of the stemma hypothesis for the text. If
100the URL is called with POST and a new dot string, updates the stemma and
101returns the SVG as with GET.
102
103=cut
104
105sub stemma :Path :Args(1) {
106 my( $self, $c, $textid ) = @_;
107 my $m = $c->model('Directory');
108 my $tradition = $m->tradition( $textid );
109
110 if( $c->req->method eq 'POST' ) {
111 # Update the stemma
112 my $dot = $c->request->body_params->{'dot'};
113 $tradition->add_stemma( $dot );
114 $m->store( $tradition );
115 }
116
117 $c->stash->{'result'} = $tradition->stemma->as_svg;
118 $c->forward('View::SVG');
dbcf12a6 119}
120
6b70c348 121=head2 stemmadot
12720144 122
6b70c348 123 GET /stemmadot/$textid
124
125Returns the 'dot' format representation of the current stemma hypothesis.
126
127=cut
128
129sub stemma :Path :Args(1) {
130 my( $self, $c, $textid ) = @_;
131 my $m = $c->model('Directory');
132 my $tradition = $m->tradition( $textid );
133
134 $c->response->body( $tradition->stemma->editable );
135 $c->forward('View::Plain');
136}
12720144 137
138=head2 relationships
139
140The relationship editor tool.
141
142=cut
143
3837c155 144sub relationships :Local {
145 my( $self, $c ) = @_;
146 my $m = $c->model('Directory');
147 my $tradition = $m->tradition( $c->request->params->{'textid'} );
b90c84a0 148 my $table = $tradition->collation->make_alignment_table();
68454b71 149 my $witlist = map { $_->{'witness'} } @{$table->{'alignment'}};
150 $c->stash->{witnesses} = $witlist;
b90c84a0 151 $c->stash->{alignment} = $table;
152 $c->stash->{template} = 'relate.tt';
3837c155 153}
154
12720144 155=head2 stexaminer
156
157The stemma analysis tool with the pretty colored table.
158
159=cut
160
3837c155 161sub stexaminer :Local {
e367f5c0 162 my( $self, $c ) = @_;
3837c155 163 my $m = $c->model('Directory');
12720144 164 my $tradition = $m->tradition( $c->request->params->{'textid'} );
165 my $stemma = $tradition->stemma;
166 # TODO Think about caching the stemma in a session
3837c155 167 $c->stash->{svg} = $stemma->as_svg;
3837c155 168 $c->stash->{text_title} = $tradition->name;
12720144 169 $c->stash->{template} = 'index.tt';
170 # TODO Run the analysis as AJAX from the loaded page.
171 my $t = run_analysis( $tradition );
172 $c->stash->{variants} = $t->{'variants'};
e367f5c0 173 $c->stash->{total} = $t->{'variant_count'};
174 $c->stash->{genealogical} = $t->{'genealogical_count'};
175 $c->stash->{conflict} = $t->{'conflict_count'};
e367f5c0 176}
a275e7e6 177
6cfc5703 178=head1 MICROSERVICE CALLS
179
180=head2 renderSVG
181
182Parse the passed collation data and return an SVG of the collated text. Takes
183the following parameters:
184
185=over 4
186
187=item * data - The collation data itself.
188
189=item * input - The data format. Valid values include CollateX, Self, TEI (for parallel segmentation) eventually Tabular.
190
191=item * name - A name for the text. Not so important for this function.
192
193=cut
194
195# Utility function to render SVG from a graph input.
196sub renderSVG :Local {
197 my( $self, $c ) = @_;
198 my $format = $c->request->param('format') || 'string';
199 my $type = $c->request->body_params->{'type'};
200 my $name = $c->request->param('name') || 'Collation graph';
201 my $data = $c->request->body_params->{'data'};
202 $c->log->debug( $data );
203 my $tradition = Text::Tradition->new(
204 'name' => $name,
205 'input' => $type,
206 $format => $data,
207 );
208 $c->log->debug( "Got tradition with " . $tradition->collation->readings . " readings" );
209 $c->stash->{'result'} = $tradition->collation->as_svg;
210 $c->forward('View::SVG');
211}
212
213
12720144 214=head1 OPENSOCIAL URLs
215
216=head2 view_table
217
218Simple gadget to return the analysis table for the stexaminer
219
220=cut
221
a275e7e6 222sub view_table :Local {
223 my( $self, $c ) = @_;
3837c155 224 my $m = $c->model('Directory');
225 my $id = $c->request->params->{'textid'};
226 my $t = run_analysis( $m->tradition( $id ), $m->stemma( $id ) );
a275e7e6 227 $c->stash->{variants} = $t->{'variants'};
228 $c->stash->{template} = 'table_gadget.tt';
229}
230
12720144 231=head2 view_svg
232
233Simple gadget to return the SVG for a given stemma
234
235=cut
236
a275e7e6 237sub view_svg :Local {
238 my( $self, $c ) = @_;
3837c155 239 my $m = $c->model('Directory');
12720144 240 my $stemma = $m->tradition( $c->request->params->{'textid'} )->stemma;
3837c155 241 if( $stemma ) {
242 $c->stash->{svg} = $stemma->as_svg;
243 }
eb1ac99d 244 $c->stash->{template} = 'stemma_gadget.tt';
a275e7e6 245}
246
dbcf12a6 247=head2 default
248
249Standard 404 error page
250
251=cut
252
253sub default :Path {
254 my ( $self, $c ) = @_;
255 $c->response->body( 'Page not found' );
256 $c->response->status(404);
257}
258
259=head2 end
260
261Attempt to render a view, if needed.
262
263=cut
264
265sub end : ActionClass('RenderView') {}
266
267=head1 AUTHOR
268
269Tara L Andrews
270
271=head1 LICENSE
272
273This library is free software. You can redistribute it and/or modify
274it under the same terms as Perl itself.
275
276=cut
277
278__PACKAGE__->meta->make_immutable;
279
2801;