Adding your own configuration variable

From EPrints Documentation
Jump to: navigation, search

Why

There are a number of reason why you would want to add your own configuration data to EPrints.

For me, I wanted to centralise the details for a database connection I needed for some secondary data (I had reasons for not including it in the main EPrints database)

Setup

To get the configuration variables into the system, the best way is to create a new configurations script:

 <my_config.pl>
 # {oarj} is the general configuration area
 $c->{oarj} = {} unless exists $c->{oarj}; # only create it if it doesn't exist
 
 # {db_conf} is a set of variables within that area
 $c->{oarj}->{db_conf} = {
    db_name     => 'myDB',
    db_hostname => 'some.host.name',
    db_username => 'user',
    db_password => 'password',
 }

Reading the configuration

Once we have the configuration, we need to be able to read it back:

 my $ep = $self->{session};
 
 my $db_conf = $ep->get_repository->get_conf( "oarj","db_conf" );
 
 my $dbh     = DBI->connect(
   "dbi:Pg:dbname=".$db_conf->{db_name}.";host=".$db_conf->{db_hostname},
   $db_conf->{db_username},
   $db_conf->{db_password},
   { AutoCommit        => 0,
     PrintError        => 1,
     showErrorStatment => 1
   }
     )
     or $self->{processor}->add_message( "error",
   $session->make_text("cannot connect to ".$db_conf->{db_name}.": $DBI::errstr") );

There you are...