1 package stemmaweb::Controller::Root;
3 use namespace::autoclean;
4 use Text::Tradition::Analysis qw/ run_analysis /;
8 BEGIN { extends 'Catalyst::Controller' }
11 # Sets the actions in this controller to be registered with no prefix
12 # so they function identically to actions created in MyApp.pm
14 __PACKAGE__->config(namespace => '');
18 stemmaweb::Controller::Root - Root Controller for stemmaweb
22 Serves up the main container pages.
28 The root page (/). Serves the main container page, from which the various
29 components will be loaded.
33 sub index :Path :Args(0) {
34 my ( $self, $c ) = @_;
36 # Are we being asked to load a text immediately? If so
37 if( $c->req->param('withtradition') ) {
38 $c->stash->{'withtradition'} = $c->req->param('withtradition');
40 $c->stash->{template} = 'index.tt';
45 A general overview/documentation page for the site.
49 sub about :Local :Args(0) {
51 $c->stash->{template} = 'about.tt';
54 =head1 Elements of index page
60 Serves a snippet of HTML that lists the available texts. This returns texts belonging to the logged-in user if any, otherwise it returns all public texts.
64 sub directory :Local :Args(0) {
66 my $m = $c->model('Directory');
67 # Is someone logged in?
69 if( $c->user_exists ) {
70 my $user = $c->user->get_object;
71 my @list = $m->traditionlist( $user );
72 map { $usertexts{$_->{id}} = 1 } @list;
73 $c->stash->{usertexts} = \@list;
74 $c->stash->{is_admin} = 1 if $user->is_admin;
76 # List public (i.e. readonly) texts separately from any user (i.e.
77 # full access) texts that exist. Admin users therefore have nothing
79 my @plist = grep { !$usertexts{$_->{id}} } $m->traditionlist('public');
80 $c->stash->{publictexts} = \@plist;
81 $c->stash->{template} = 'directory.tt';
84 =head1 AJAX methods for traditions and their properties
94 Creates a new tradition belonging to the logged-in user, with the given name
95 and the collation given in the uploaded file. The file type is indicated via
96 the filename extension (.csv, .txt, .xls, .xlsx, .xml). Returns the ID and
97 name of the new tradition.
101 sub newtradition :Local :Args(0) {
102 my( $self, $c ) = @_;
103 return _json_error( $c, 403, 'Cannot save a tradition without being logged in' )
104 unless $c->user_exists;
106 my $user = $c->user->get_object;
107 # Grab the file upload, check its name/extension, and call the
108 # appropriate parser(s).
109 my $upload = $c->request->upload('file');
110 my $name = $c->request->param('name') || 'Uploaded tradition';
111 my $lang = $c->request->param( 'language' ) || 'Default';
112 my $public = $c->request->param( 'public' ) ? 1 : undef;
113 my( $ext ) = $upload->filename =~ /\.(\w+)$/;
118 'file' => $upload->tempname
123 if( $ext eq 'xml' ) {
124 # Try the different XML parsing options to see if one works.
125 foreach my $type ( qw/ CollateX CTE TEI / ) {
127 $tradition = Text::Tradition->new( %newopts, 'input' => $type );
128 } catch ( Text::Tradition::Error $e ) {
129 $errmsg = $e->message;
131 $errmsg = "Unexpected parsing error";
138 } elsif( $ext =~ /^(txt|csv|xls(x)?)$/ ) {
139 # If it's Excel we need to pass excel => $ext;
140 # otherwise we need to pass sep_char => [record separator].
141 if( $ext =~ /xls/ ) {
142 $newopts{'excel'} = $ext;
144 $newopts{'sep_char'} = $ext eq 'txt' ? "\t" : ',';
147 $tradition = Text::Tradition->new(
149 'input' => 'Tabular',
151 } catch ( Text::Tradition::Error $e ) {
152 $errmsg = $e->message;
154 $errmsg = "Unexpected parsing error";
157 # Error unless we have a recognized filename extension
158 return _json_error( $c, 403, "Unrecognized file type extension $ext" );
161 # Save the tradition if we have it, and return its data or else the
162 # error that occurred trying to make it.
164 return _json_error( $c, 500, "Error parsing tradition .$ext file: $errmsg" );
165 } elsif( !$tradition ) {
166 return _json_error( $c, 500, "No error caught but tradition not created" );
169 my $m = $c->model('Directory');
170 $user->add_tradition( $tradition );
171 my $id = $c->model('Directory')->store( $tradition );
172 $c->model('Directory')->store( $user );
173 $c->stash->{'result'} = { 'id' => $id, 'name' => $tradition->name };
174 $c->forward('View::JSON');
179 GET /textinfo/$textid
180 POST /textinfo/$textid,
182 language: $new_language,
184 owner: $new_userid } # only admin users can update the owner
186 Returns information about a particular text.
190 sub textinfo :Local :Args(1) {
191 my( $self, $c, $textid ) = @_;
192 my $tradition = $c->model('Directory')->tradition( $textid );
193 ## Have to keep users in the same scope as tradition
196 unless( $tradition ) {
197 return _json_error( $c, 404, "No tradition with ID $textid" );
199 my $ok = _check_permission( $c, $tradition );
201 if( $c->req->method eq 'POST' ) {
202 return _json_error( $c, 403,
203 'You do not have permission to update this tradition' )
204 unless $ok eq 'full';
205 my $params = $c->request->parameters;
206 # Handle changes to owner-accessible parameters
207 my $m = $c->model('Directory');
209 # Handle name param - easy
210 if( exists $params->{name} ) {
211 my $newname = delete $params->{name};
212 unless( $tradition->name eq $newname ) {
214 $tradition->name( $newname );
217 return _json_error( $c, 500, "Error setting name to $newname" );
221 # Handle language param, making Default => null
222 my $langval = delete $params->{language} || 'Default';
223 unless( $tradition->language eq $langval ) {
225 $tradition->language( $langval );
228 return _json_error( $c, 500, "Error setting language to $langval" );
233 my $ispublic = $tradition->public;
234 if( delete $params->{'public'} ) { # if it's any true value...
235 $tradition->public( 1 );
236 $changed = 1 unless $ispublic;
237 } else { # the checkbox was unchecked, ergo it should not be public
238 $tradition->public( 0 );
239 $changed = 1 if $ispublic;
242 # Handle ownership change
243 if( exists $params->{'owner'} ) {
244 # Only admins can update user / owner
245 my $newownerid = delete $params->{'owner'};
246 unless( !$newownerid ||
247 ( $tradition->has_user && $tradition->user->email eq $newownerid ) ) {
248 unless( $c->user->get_object->is_admin ) {
249 return _json_error( $c, 403,
250 "Only admin users can change tradition ownership" );
252 $newuser = $m->find_user({ email => $newownerid });
254 return _json_error( $c, 500, "No such user " . $newownerid );
256 if( $tradition->has_user ) {
257 $olduser = $tradition->user;
258 $olduser->remove_tradition( $tradition );
260 $newuser->add_tradition( $tradition );
264 # TODO check for rogue parameters
265 if( scalar keys %$params ) {
266 my $rogueparams = join( ', ', keys %$params );
267 return _json_error( $c, 403, "Request parameters $rogueparams not recognized" );
269 # If we safely got to the end, then write to the database.
270 $m->save( $tradition ) if $changed;
271 $m->save( $newuser ) if $newuser;
274 # Now return the current textinfo, whether GET or successful POST.
277 name => $tradition->name,
278 language => $tradition->language,
279 public => $tradition->public || 0,
280 owner => $tradition->user ? $tradition->user->email : undef,
281 witnesses => [ map { $_->sigil } $tradition->witnesses ],
283 my @stemmasvg = map { $_->as_svg() } $tradition->stemmata;
284 map { $_ =~ s/\n/ /mg } @stemmasvg;
285 $textinfo->{stemmata} = \@stemmasvg;
286 $c->stash->{'result'} = $textinfo;
287 $c->forward('View::JSON');
292 GET /variantgraph/$textid
294 Returns the variant graph for the text specified at $textid, in SVG form.
298 sub variantgraph :Local :Args(1) {
299 my( $self, $c, $textid ) = @_;
300 my $tradition = $c->model('Directory')->tradition( $textid );
301 unless( $tradition ) {
302 return _json_error( $c, 404, "No tradition with ID $textid" );
304 my $ok = _check_permission( $c, $tradition );
307 my $collation = $tradition->collation;
308 $c->stash->{'result'} = $collation->as_svg;
309 $c->forward('View::SVG');
314 GET /stemma/$textid/$stemmaseq
315 POST /stemma/$textid/$stemmaseq, { 'dot' => $dot_string }
317 Returns an SVG representation of the given stemma hypothesis for the text.
318 If the URL is called with POST, the stemma at $stemmaseq will be altered
319 to reflect the definition in $dot_string. If $stemmaseq is 'n', a new
320 stemma will be added.
324 sub stemma :Local :Args(2) {
325 my( $self, $c, $textid, $stemmaid ) = @_;
326 my $m = $c->model('Directory');
327 my $tradition = $m->tradition( $textid );
328 unless( $tradition ) {
329 return _json_error( $c, 404, "No tradition with ID $textid" );
331 my $ok = _check_permission( $c, $tradition );
334 $c->stash->{'result'} = '';
336 if( $c->req->method eq 'POST' ) {
337 if( $ok eq 'full' ) {
338 my $dot = $c->request->body_params->{'dot'};
340 if( $stemmaid eq 'n' ) {
341 # We are adding a new stemma.
342 $stemmaid = $tradition->stemma_count;
343 $stemma = $tradition->add_stemma( 'dot' => $dot );
344 } elsif( $stemmaid !~ /^\d+$/ ) {
345 return _json_error( $c, 403, "Invalid stemma ID specification $stemmaid" );
346 } elsif( $stemmaid < $tradition->stemma_count ) {
347 # We are updating an existing stemma.
348 $stemma = $tradition->stemma( $stemmaid );
349 $stemma->alter_graph( $dot );
351 # Unrecognized stemma ID
352 return _json_error( $c, 404, "No stemma at index $stemmaid, cannot update" );
354 } catch ( Text::Tradition::Error $e ) {
355 return _json_error( $c, 500, $e->message );
357 $m->store( $tradition );
359 # No permissions to update the stemma
360 return _json_error( $c, 403,
361 'You do not have permission to update stemmata for this tradition' );
365 # For a GET or a successful POST request, return the SVG representation
366 # of the stemma in question, if any.
367 if( !$stemma && $tradition->stemma_count > $stemmaid ) {
368 $stemma = $tradition->stemma( $stemmaid );
370 my $stemma_xml = $stemma ? $stemma->as_svg() : '';
371 # What was requested, XML or JSON?
372 my $return_view = 'SVG';
373 if( my $accept_header = $c->req->header('Accept') ) {
374 $c->log->debug( "Received Accept header: $accept_header" );
375 foreach my $type ( split( /,\s*/, $accept_header ) ) {
376 # If we were first asked for XML, return SVG
377 last if $type =~ /^(application|text)\/xml$/;
378 # If we were first asked for JSON, return JSON
379 if( $type eq 'application/json' ) {
380 $return_view = 'JSON';
385 if( $return_view eq 'SVG' ) {
386 $c->stash->{'result'} = $stemma_xml;
387 $c->forward('View::SVG');
389 $stemma_xml =~ s/\n/ /mg;
390 $c->stash->{'result'} = { 'stemmaid' => $stemmaid, 'stemmasvg' => $stemma_xml };
391 $c->forward('View::JSON');
397 GET /stemmadot/$textid/$stemmaseq
399 Returns the 'dot' format representation of the current stemma hypothesis.
403 sub stemmadot :Local :Args(2) {
404 my( $self, $c, $textid, $stemmaid ) = @_;
405 my $m = $c->model('Directory');
406 my $tradition = $m->tradition( $textid );
407 unless( $tradition ) {
408 return _json_error( $c, 404, "No tradition with ID $textid" );
410 my $ok = _check_permission( $c, $tradition );
412 my $stemma = $tradition->stemma( $stemmaid );
414 return _json_error( $c, 404, "Tradition $textid has no stemma ID $stemmaid" );
416 # Get the dot and transmute its line breaks to literal '|n'
417 $c->stash->{'result'} = { 'dot' => $stemma->editable( { linesep => '|n' } ) };
418 $c->forward('View::JSON');
425 # Helper to check what permission, if any, the active user has for
426 # the given tradition
427 sub _check_permission {
428 my( $c, $tradition ) = @_;
429 my $user = $c->user_exists ? $c->user->get_object : undef;
431 return 'full' if ( $user->is_admin ||
432 ( $tradition->has_user && $tradition->user->id eq $user->id ) );
434 # Text doesn't belong to us, so maybe it's public?
435 return 'readonly' if $tradition->public;
437 # ...nope. Forbidden!
438 return _json_error( $c, 403, 'You do not have permission to view this tradition.' );
441 # Helper to throw a JSON exception
443 my( $c, $code, $errmsg ) = @_;
444 $c->response->status( $code );
445 $c->stash->{'result'} = { 'error' => $errmsg };
446 $c->forward('View::JSON');
452 Standard 404 error page
457 my ( $self, $c ) = @_;
458 $c->response->body( 'Page not found' );
459 $c->response->status(404);
464 Attempt to render a view, if needed.
468 sub end : ActionClass('RenderView') {}
476 This library is free software. You can redistribute it and/or modify
477 it under the same terms as Perl itself.
481 __PACKAGE__->meta->make_immutable;