Difference between revisions of "Adding your own configuration variable"
(Created page with 'catagory:snippits ==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 dat…') |
m |
||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
− | |||
==Why== | ==Why== | ||
There are a number of reason why you would want to add your own configuration data to EPrints. | There are a number of reason why you would want to add your own configuration data to EPrints. | ||
Line 11: | Line 10: | ||
# {oarj} is the general configuration area | # {oarj} is the general configuration area | ||
$c->{oarj} = {} unless exists $c->{oarj}; # only create it if it doesn't exist | $c->{oarj} = {} unless exists $c->{oarj}; # only create it if it doesn't exist | ||
+ | |||
# {db_conf} is a set of variables within that area | # {db_conf} is a set of variables within that area | ||
$c->{oarj}->{db_conf} = { | $c->{oarj}->{db_conf} = { | ||
Line 23: | Line 23: | ||
my $ep = $self->{session}; | my $ep = $self->{session}; | ||
+ | |||
my $db_conf = $ep->get_repository->get_conf( "oarj","db_conf" ); | my $db_conf = $ep->get_repository->get_conf( "oarj","db_conf" ); | ||
+ | |||
my $dbh = DBI->connect( | my $dbh = DBI->connect( | ||
"dbi:Pg:dbname=".$db_conf->{db_name}.";host=".$db_conf->{db_hostname}, | "dbi:Pg:dbname=".$db_conf->{db_name}.";host=".$db_conf->{db_hostname}, | ||
Line 37: | Line 39: | ||
There you are... | There you are... | ||
+ | |||
+ | |||
+ | [[Category:Snippets]] |
Latest revision as of 09:48, 20 January 2011
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...