--- Utils.pm.orig 2003-09-19 17:15:54.000000000 +0100 +++ Utils.pm 2003-09-20 10:23:59.000000000 +0100 @@ -5,10 +5,12 @@ $VERSION = '0.05'; use Carp qw( croak ); +use Config::Tiny; use CGI::Wiki; use CGI::Wiki::Formatter::UseMod; use CGI::Wiki::Search::SII; use Search::InvertedIndex::DB::DB_File_SplitHash; +use Template; use URI::Escape; =head1 NAME @@ -111,6 +113,58 @@ return $wiki; } +=item B + + my $config = OpenGuides::Utils->make_config_object(); + +Returns a C object containing the configuration for +the current OpenGuides site. Assumes that the conf folder is +located one level up from the current working directory. + +Finds the site-specific config file by evaluating a TT2 template +stored in the master config file (named C) as item C. +This template has access to the arrays @domain and @path. + +If C is missing or empty, or if it contains or evaluates to +C then the contents of the master config file are returned. + +Croaks if an error occurs. + +=cut + +sub make_config_object { + croak "Can't find the openguides master config file" + unless -r '../conf/openguides'; + + my $common_config = Config::Tiny->read('../conf/openguides'); + + return $common_config + if !exists $common_config->{_}->{config} or + !defined $common_config->{_}->{config} or + $common_config->{_}->{config} eq 'openguides'; + + my @domain = $ENV{SERVER_NAME} =~ m{([^.]+).?}g; + my @path = $ENV{SCRIPT_NAME} =~ m{/([^/]+)}g; + + my $tt = Template->new(); + my $site_config_template = $common_config->{_}->{config}; + my $site_config = ''; + $tt->process(\$site_config_template, + { domain => \@domain, path => \@path }, + \$site_config); + + croak "Invalid site-specific OpenGuides config file supplied" + unless defined $site_config; + + return $common_config + if $site_config eq 'openguides'; + + croak "Can't find the site-specific OpenGuides config file ($site_config)" + unless -r "../conf/$site_config"; + + my $config = Config::Tiny->read("../conf/$site_config"); + return $config; +} =back