source: trunk/lib/OpenGuides/Config.pm @ 621

Last change on this file since 621 was 621, checked in by Dominic Hargreaves, 17 years ago

Use environment variable OPENGUIDES_CONFIG_FILE for config file location;
will be useful for mod_perl. Also add error checking to OpenGuides::Config
config file opening.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.6 KB
Line 
1package OpenGuides::Config;
2use strict;
3
4use Config::Tiny;
5
6use base qw( Class::Accessor );
7my @variables = qw(
8   dbtype dbname dbuser dbpass dbhost script_name install_directory script_url
9   custom_lib_path use_plucene indexing_directory enable_page_deletion
10   admin_pass stylesheet_url site_name navbar_on_home_page home_name
11   site_desc default_city default_country contact_email default_language
12   formatting_rules_node formatting_rules_link backlinks_in_title template_path custom_template_path
13   geo_handler ellipsoid
14);
15my @questions = map { $_ . "__qu" } @variables;
16OpenGuides::Config->mk_accessors( @variables );
17OpenGuides::Config->mk_accessors( @questions );
18
19=head1 NAME
20
21OpenGuides::Config - Handle OpenGuides configuration variables.
22
23=head1 DESCRIPTION
24
25Does config stuff for OpenGuides.  Distributed and installed as part of
26the OpenGuides project, not intended for independent installation.
27This documentation is probably only useful to OpenGuides developers.
28
29=head1 METHODS
30
31=over
32
33=item B<new>
34
35  my $config = OpenGuides::Config->new( file => "wiki.conf" );
36
37Initialises itself from the config file specified.  Variables which
38are not set in that file, and which have sensible defaults, will be
39initialised as described below in ACCESSORS; others will be given a
40value of C<undef>.
41
42  my $config = OpenGuides::Config->new( vars => { dbname => "foo" } );
43
44As above but gets variables from a supplied hashref instead.
45
46=cut
47
48sub new {
49    my $class = shift;
50    my $self = { };
51    bless $self, $class;
52    return $self->_init( @_ );
53}
54
55sub _init {
56    my ($self, %args) = @_;
57
58    # Here are the defaults for the variable values.
59    # Don't forget to add to INSTALL when changing these.
60    my %defaults = (
61                     dbtype => "postgres",
62                     script_name => "wiki.cgi",
63                     install_directory => "/usr/lib/cgi-bin/openguides/",
64                     use_plucene => 1,
65                     indexing_directory => "/usr/lib/cgi-bin/openguides/indexes/",
66                     enable_page_deletion => 0,
67                     admin_pass => "Change This!",
68                     site_name => "Unconfigured OpenGuides site",
69                     navbar_on_home_page => 1,
70                     home_name => "Home",
71                     site_desc => "A default configuration of OpenGuides",
72                     default_city => "",
73                     default_country => "",
74                     default_language => "en",
75                     formatting_rules_node => "Text Formatting Examples",
76                     formatting_rules_link => "http://openguides.org/page/text_formatting",
77                     backlinks_in_title => 0,
78                     geo_handler => 1,
79                     ellipsoid => "International"
80                   );
81
82    # See if we already have some config variables set.
83    my %stored;
84    if ( $args{file} ) {
85        my $read_config = Config::Tiny->read( $args{file} ) or
86            die "Cannot read config file $args{file}";
87        %stored = $read_config ? %{ $read_config->{_} } : ();
88    } elsif ( $args{vars} ) {
89        %stored = %{ $args{vars} };
90    }
91
92    # Set all defaults first, then set the stored values.  This allows us
93    # to make sure that the stored values override the defaults yet be sure
94    # to set any variables which have stored values but not defaults.
95    foreach my $var ( keys %defaults ) {
96        $self->$var( $defaults{$var} );
97    }
98    foreach my $var ( keys %stored ) {
99        if ( $self->can( $var ) ) { # handle any garbage in file gracefully
100            $self->$var( $stored{$var} );
101        } else {
102            warn "Don't know what to do with variable '$var'";
103        }
104    }
105
106    # And the questions.
107    # Don't forget to add to INSTALL when changing these.
108    my %questions = (
109        dbtype => "What type of database do you want the site to run on?  postgres/mysql/sqlite",
110        dbname => "What's the name of the database that this site runs on?",
111        dbuser => "...the database user that can access that database?",
112        dbpass => "...the password that they use to access the database?",
113        dbhost => "...the machine that the database is hosted on? (blank if local)",
114        script_name => "What do you want the script to be called?",
115        install_directory => "What directory should I install it in?",
116        template_path => "What directory should I install the templates in?",
117        custom_template_path => "Where should I look for custom templates?",
118        script_url => "What URL does the install directory map to?",
119        custom_lib_path => "Do you want me to munge a custom lib path into the scripts?  If so, enter it here.  Separate path entries with whitespace.",
120        use_plucene => "Do you want to use Plucene for searching? (recommended, but see Changes file before saying yes to this if you are upgrading)",
121        indexing_directory => "What directory can I use to store indexes in for searching? ***NOTE*** This directory must exist and be writeable by the user that your script will run as.  See README for more on this.",
122        enable_page_deletion => "Do you want to enable page deletion?",
123        admin_pass => "Please specify a password for the site admin.",
124        stylesheet_url => "What's the URL of the site's stylesheet?",
125        site_name => "What's the site called? (should be unique)",
126        navbar_on_home_page => "Do you want the navigation bar included on the home page?",
127        home_name => "What should the home page of the wiki be called?",
128        site_desc => "How would you describe the site?",
129        default_city => "What city is the site based in?",
130        default_country => "What country is the site based in?",
131        contact_email => "Contact email address for the site administrator?",
132        default_language => "What language will the site be in? (Please give an ISO language code.)",
133        formatting_rules_node => "What's the name of the node or page to use for the text formatting rules link (this is by default an external document, but if you make formatting_rules_link empty, it will be a wiki node instead",
134        formatting_rules_link => "What URL do you want to use for the text formatting rules (leave blank to use a wiki node instead)?",
135        backlinks_in_title => "Make node titles link to node backlinks (C2 style)?",
136        ellipsoid => "Which ellipsoid do you want to use? (eg 'Airy', 'WGS-84')",
137    );
138
139    foreach my $var ( keys %questions ) {
140        my $method = $var . "__qu";
141        $self->$method( $questions{$var} );
142    }
143
144    return $self;
145}
146
147=back
148
149=head1 ACCESSORS
150
151Each of the accessors described below is read-write.  Additionally,
152for each of them, there is also a read-write accessor called, for
153example, C<dbname__qu>.  This will contain an English-language
154question suitable for asking for a value for that variable.  You
155shouldn't write to them, but this is not enforced.
156
157The defaults mentioned below are those which are applied when
158C<< ->new >> is called, to variables which are not supplied in
159the config file.
160
161=over
162
163=item * dbname
164
165=item * dbuser
166
167=item * dbpass
168
169=item * dbhost
170
171=item * script_name (default: C<wiki.cgi>)
172
173=item * install_directory (default: C</usr/lib/cgi-bin/openguides/>)
174
175=item * script_url (this is constrained to always end in C</>)
176
177=cut
178
179sub script_url {
180    my $self = shift;
181    # See perldoc Class::Accessor - can't just use SUPER.
182    my $url = $self->_script_url_accessor( @_ );
183    $url .= "/" unless $url =~ /\/$/;
184    return $url;
185}
186
187=item * custom_lib_path
188
189=item * use_plucene (default: true)
190
191=item * indexing_directory (default: C</usr/lib/cgi-bin/openguides/indexes>)
192
193=item * enable_page_deletion (default: false)
194
195=item * admin_pass (default: C<Change This!>)
196
197=item * stylesheet_url
198
199=item * site_name (default: C<Unconfigured OpenGuides site>)
200
201=item * navbar_on_home_page (default: true)
202
203=item * home_name (default: C<Home>)
204
205=item * site_desc (default: C<A default configuration of OpenGuides>)
206
207=item * default_city (default: C<London>)
208
209=item * default_country (default: C<United Kingdom>)
210
211=item * default_language (default: C<en>)
212
213=item * contact_email
214
215=item * formatting_rules_node (default: C<Text Formatting Examples>)
216
217=item * formatting_rules_link (default: C<http://openguides.org/page/text_formatting>
218
219=item * backlinks_in_title (default: false)
220
221=item * geo_handler (default: C<1>)
222
223=item * ellipsoid (default: C<International>)
224
225=back
226
227=head1 AUTHOR
228
229The OpenGuides Project (openguides-dev@openguides.org)
230
231=head1 COPYRIGHT
232
233     Copyright (C) 2004 The OpenGuides Project.  All Rights Reserved.
234
235The OpenGuides distribution is free software; you can redistribute it
236and/or modify it under the same terms as Perl itself.
237
238=head1 SEE ALSO
239
240L<OpenGuides>
241
242=cut
243
2441;
Note: See TracBrowser for help on using the repository browser.