Pretty URLs with EPrints

Recently I have been working on a project which was focused on adding Homepages and Profiles for users to EPrints repositories called MicroViews. Currently when a user logs into an EPrints repository all you are presented with is your Manage Deposits page. With MicroViews the user will be presented with a Homepage which provides them with various pieces of information about the repository which is relevant to them, for example what are their most popular publications and if there are any issues with their publications that have been raised at the last audit.

The extension also adds a profile page to EPrints for each user. The idea of profile pages is to provide a static external facing page which the rest of the world can see. This page would include basic information about the user and the publications they have added to the repository. This effectively provides a biography of the user with regards to the repository.

One thing that we wanted to do is create pretty URLs for the user profiles of the form http://examplerepository.com/profile/. Initially we did this by making a modification to perl_lib/EPrints/Apache/Rewrite.pm but this was not the ideal situation for a plugin since this is a modification to the core code of EPrints. The problem with this approach is that if the repository administrator went on to update their core EPrints install then they would end up losing the hand made modification which is required for the plugin and they would have to add it again.

Instead we investigated creating a custom URL handler and adding it to the request handling chain using archives/ARCHIVEID/cfg/apachevhosts.conf. By doing this we won’t change any core EPrints code and all of the changes will be safe from any auto generated changes. The first thing we did was create a custom rewrite handler which took the following shape;

 1 package EPrints::Plugin::MePrints::Apache::Rewrite;
 2 
 3 use strict;
 4 use warnings;
 5 our @ISA = qw/ EPrints::Plugin /;
 6 use EPrints::Apache::AnApache;
 7 
 8 sub handler
 9 {
10   my( $r ) = @_;
11   my $uri = $r->uri;
12   my $repository_id = $r->dir_config( 'EPrints_ArchiveID' );
13   if( !defined $repository_id )
14   {
15     return DECLINED;
16   }
17   my $repository = EPrints::Repository->new( $repository_id );
18   if( $uri =~ m! ^\/profile\/([0-9a-zA-Z]+)$ !x )
19   {
20     # Find the user and if found set the appropriate filename for the response object.
21     # If the user is not found then don't do anything or return anything and this will
22     # cause EPrints to respond with a 404 error.
23   }
24   else
25   {
26     return DECLINED;
27   }
28 }

Next we needed to add only one line to the archves/ARCHIVEID/cfg/apachevhost.conf file;

1 PerlTransHandler EPrints::Plugin::MePrints::Apache::Rewrite

After a server reset we had the pretty URLs for the profile pages. This works thanks to the PerlTransHandler statement in the apachevhost.conf file. What this does is add a handler to the chain of handlers to be run. By default in EPrints there is only one handler and that is the default one. What we have done with the change above is add a handler into the chain before that. The handler method in EPrints::Plugin::MePrints::Apache::Rewrite gets called and if it gets a match for profile then it goes ahead and handles it. The “return DECLINED” tells Apache that it should move onto the next URI translation handler in the chain.

Written on September 21, 2009