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

Last change on this file since 606 was 587, checked in by kake, 17 years ago

Encapsulate config data in OpenGuides::Config.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.0 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 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    my %defaults = (
60                     dbtype => "postgres",
61                     script_name => "wiki.cgi",
62                     install_directory => "/usr/lib/cgi-bin/openguides/",
63                     use_plucene => 1,
64                     indexing_directory => "/usr/lib/cgi-bin/openguides/indexes/",
65                     enable_page_deletion => 0,
66                     admin_pass => "Change This!",
67                     site_name => "Unconfigured OpenGuides site",
68                     navbar_on_home_page => 1,
69                     home_name => "Home",
70                     site_desc => "A default configuration of OpenGuides",
71                     default_city => "London",
72                     default_country => "United Kingdom",
73                     default_language => "en",
74                     formatting_rules_node => "Text Formatting Examples",
75                     backlinks_in_title => 0,
76                     geo_handler => 1,
77                     ellipsoid => "International"
78                   );
79
80    # See if we already have some config variables set.
81    my %stored;
82    if ( $args{file} ) {
83        my $read_config = Config::Tiny->read( $args{file} );
84        %stored = $read_config ? %{ $read_config->{_} } : ();
85    } elsif ( $args{vars} ) {
86        %stored = %{ $args{vars} };
87    }
88
89    # Set all defaults first, then set the stored values.  This allows us
90    # to make sure that the stored values override the defaults yet be sure
91    # to set any variables which have stored values but not defaults.
92    foreach my $var ( keys %defaults ) {
93        $self->$var( $defaults{$var} );
94    }
95    foreach my $var ( keys %stored ) {
96        if ( $self->can( $var ) ) { # handle any garbage in file gracefully
97            $self->$var( $stored{$var} );
98        } else {
99            warn "Don't know what to do with variable '$var'";
100        }
101    }
102
103    # And the questions.
104    my %questions = (
105        dbtype => "What type of database do you want the site to run on?  postgres/mysql/sqlite",
106        dbname => "What's the name of the database that this site runs on?",
107        dbuser => "...the database user that can access that database?",
108        dbpass => "...the password that they use to access the database?",
109        dbhost => "...the machine that the database is hosted on? (blank if local)",
110        script_name => "What do you want the script to be called?",
111        install_directory => "What directory should I install it in?",
112        template_path => "What directory should I install the templates in?",
113        custom_template_path => "Where should I look for custom templates?",
114        script_url => "What URL does the install directory map to?",
115        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.",
116        use_plucene => "Do you want to use Plucene for searching? (recommended, but see Changes file before saying yes to this if you are upgrading)",
117        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.",
118        enable_page_deletion => "Do you want to enable page deletion?",
119        admin_pass => "Please specify a password for the site admin.",
120        stylesheet_url => "What's the URL of the site's stylesheet?",
121        site_name => "What's the site called? (should be unique)",
122        navbar_on_home_page => "Do you want the navigation bar included on the home page?",
123        home_name => "What should the home page of the wiki be called?",
124        site_desc => "How would you describe the site?",
125        default_city => "What city is the site based in?",
126        default_country => "What country is the site based in?",
127        contact_email => "Contact email address for the site administrator?",
128        default_language => "What language will the site be in? (Please give an ISO language code.)",
129        formatting_rules_node => "What's the name of the node to use for the text formatting rules link?",
130        backlinks_in_title => "Make node titles link to node backlinks (C2 style)?",
131        ellipsoid => "Which ellipsoid do you want to use? (eg 'Airy', 'WGS-84')",
132    );
133
134    foreach my $var ( keys %questions ) {
135        my $method = $var . "__qu";
136        $self->$method( $questions{$var} );
137    }
138
139    return $self;
140}
141
142=back
143
144=head1 ACCESSORS
145
146Each of the accessors described below is read-write.  Additionally,
147for each of them, there is also a read-write accessor called, for
148example, C<dbname__qu>.  This will contain an English-language
149question suitable for asking for a value for that variable.  You
150shouldn't write to them, but this is not enforced.
151
152The defaults mentioned below are those which are applied when
153C<< ->new >> is called, to variables which are not supplied in
154the config file.
155
156=over
157
158=item * dbname
159
160=item * dbuser
161
162=item * dbpass
163
164=item * dbhost
165
166=item * script_name (default: C<wiki.cgi>)
167
168=item * install_directory (default: C</usr/lib/cgi-bin/openguides/>)
169
170=item * script_url (this is constrained to always end in C</>)
171
172=cut
173
174sub script_url {
175    my $self = shift;
176    # See perldoc Class::Accessor - can't just use SUPER.
177    my $url = $self->_script_url_accessor( @_ );
178    $url .= "/" unless $url =~ /\/$/;
179    return $url;
180}
181
182=item * custom_lib_path
183
184=item * use_plucene (default: true)
185
186=item * indexing_directory (default: C</usr/lib/cgi-bin/openguides/indexes>)
187
188=item * enable_page_deletion (default: false)
189
190=item * admin_pass (default: C<Change This!>)
191
192=item * stylesheet_url
193
194=item * site_name (default: C<Unconfigured OpenGuides site>)
195
196=item * navbar_on_home_page (default: true)
197
198=item * home_name (default: C<Home>)
199
200=item * site_desc (default: C<A default configuration of OpenGuides>)
201
202=item * default_city (default: C<London>)
203
204=item * default_country (default: C<United Kingdom>)
205
206=item * default_language (default: C<en>)
207
208=item * contact_email
209
210=item * formatting_rules_node (default: C<Text Formatting Examples>)
211
212=item * backlinks_in_title (default: false)
213
214=item * geo_handler (default: C<1>)
215
216=item * ellipsoid (default: C<International>)
217
218=back
219
220=head1 AUTHOR
221
222The OpenGuides Project (openguides-dev@openguides.org)
223
224=head1 COPYRIGHT
225
226     Copyright (C) 2004 The OpenGuides Project.  All Rights Reserved.
227
228The OpenGuides distribution is free software; you can redistribute it
229and/or modify it under the same terms as Perl itself.
230
231=head1 SEE ALSO
232
233L<OpenGuides>
234
235=cut
236
2371;
Note: See TracBrowser for help on using the repository browser.