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

Last change on this file since 868 was 868, checked in by nick, 15 years ago

Support the config flag http_charset, which sets a http charset header on content type. (Closes #141)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.3 KB
RevLine 
[587]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
[868]11   site_desc default_city default_country contact_email
12   default_language http_charset
[730]13   formatting_rules_node formatting_rules_link backlinks_in_title template_path
14   custom_template_path geo_handler ellipsoid gmaps_api_key centre_long
15   centre_lat default_gmaps_zoom default_gmaps_search_zoom force_wgs84
[836]16   licence_name licence_url licence_info_url moderation_requires_password
[841]17   enable_node_image enable_common_categories enable_common_locales
[587]18);
19my @questions = map { $_ . "__qu" } @variables;
20OpenGuides::Config->mk_accessors( @variables );
21OpenGuides::Config->mk_accessors( @questions );
22
23=head1 NAME
24
25OpenGuides::Config - Handle OpenGuides configuration variables.
26
27=head1 DESCRIPTION
28
29Does config stuff for OpenGuides.  Distributed and installed as part of
30the OpenGuides project, not intended for independent installation.
31This documentation is probably only useful to OpenGuides developers.
32
33=head1 METHODS
34
35=over
36
37=item B<new>
38
39  my $config = OpenGuides::Config->new( file => "wiki.conf" );
40
41Initialises itself from the config file specified.  Variables which
42are not set in that file, and which have sensible defaults, will be
43initialised as described below in ACCESSORS; others will be given a
44value of C<undef>.
45
46  my $config = OpenGuides::Config->new( vars => { dbname => "foo" } );
47
48As above but gets variables from a supplied hashref instead.
49
50=cut
51
52sub new {
53    my $class = shift;
54    my $self = { };
55    bless $self, $class;
56    return $self->_init( @_ );
57}
58
59sub _init {
60    my ($self, %args) = @_;
61
62    # Here are the defaults for the variable values.
[611]63    # Don't forget to add to INSTALL when changing these.
[587]64    my %defaults = (
65                     dbtype => "postgres",
66                     script_name => "wiki.cgi",
67                     install_directory => "/usr/lib/cgi-bin/openguides/",
68                     use_plucene => 1,
69                     indexing_directory => "/usr/lib/cgi-bin/openguides/indexes/",
70                     enable_page_deletion => 0,
[840]71                     moderation_requires_password => 1,
[587]72                     admin_pass => "Change This!",
[841]73                     enable_node_image => 1,
[857]74                     enable_common_categories => 0,
75                     enable_common_locales => 0,
[587]76                     site_name => "Unconfigured OpenGuides site",
77                     navbar_on_home_page => 1,
78                     home_name => "Home",
79                     site_desc => "A default configuration of OpenGuides",
[609]80                     default_city => "",
81                     default_country => "",
[587]82                     default_language => "en",
[868]83                     http_charset => "",
[587]84                     formatting_rules_node => "Text Formatting Examples",
[607]85                     formatting_rules_link => "http://openguides.org/page/text_formatting",
[587]86                     backlinks_in_title => 0,
87                     geo_handler => 1,
[736]88                     ellipsoid => "WGS-84",
[730]89                     centre_long => 0,
90                     centre_lat => 0,
91                     default_gmaps_zoom => 5,
92                     default_gmaps_search_zoom => 3,
[776]93                     force_wgs84 => 0,
94                     licence_name => "",
95                     licence_url => "",
96                     licence_info_url => ""
[587]97                   );
98
99    # See if we already have some config variables set.
100    my %stored;
101    if ( $args{file} ) {
[621]102        my $read_config = Config::Tiny->read( $args{file} ) or
[662]103            warn "Cannot read config file $args{file}";
[587]104        %stored = $read_config ? %{ $read_config->{_} } : ();
105    } elsif ( $args{vars} ) {
106        %stored = %{ $args{vars} };
107    }
108
109    # Set all defaults first, then set the stored values.  This allows us
110    # to make sure that the stored values override the defaults yet be sure
111    # to set any variables which have stored values but not defaults.
112    foreach my $var ( keys %defaults ) {
113        $self->$var( $defaults{$var} );
114    }
115    foreach my $var ( keys %stored ) {
116        if ( $self->can( $var ) ) { # handle any garbage in file gracefully
117            $self->$var( $stored{$var} );
118        } else {
119            warn "Don't know what to do with variable '$var'";
120        }
121    }
122
123    # And the questions.
[611]124    # Don't forget to add to INSTALL when changing these.
[587]125    my %questions = (
126        dbtype => "What type of database do you want the site to run on?  postgres/mysql/sqlite",
127        dbname => "What's the name of the database that this site runs on?",
128        dbuser => "...the database user that can access that database?",
129        dbpass => "...the password that they use to access the database?",
130        dbhost => "...the machine that the database is hosted on? (blank if local)",
131        script_name => "What do you want the script to be called?",
132        install_directory => "What directory should I install it in?",
133        template_path => "What directory should I install the templates in?",
134        custom_template_path => "Where should I look for custom templates?",
135        script_url => "What URL does the install directory map to?",
136        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.",
137        use_plucene => "Do you want to use Plucene for searching? (recommended, but see Changes file before saying yes to this if you are upgrading)",
138        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.",
139        enable_page_deletion => "Do you want to enable page deletion?",
[840]140        moderation_requires_password => "Is the admin password required for moderating pages?",
[587]141        admin_pass => "Please specify a password for the site admin.",
142        stylesheet_url => "What's the URL of the site's stylesheet?",
[841]143        enable_node_image => "Should nodes be allowed to have an externally hosted image?",
[840]144        enable_common_categories => "Do you want a common list of categories shown on all node pages?",
145        enable_common_locales => "Do you want a common list of locales shown on all node pages?",
[587]146        site_name => "What's the site called? (should be unique)",
147        navbar_on_home_page => "Do you want the navigation bar included on the home page?",
148        home_name => "What should the home page of the wiki be called?",
149        site_desc => "How would you describe the site?",
150        default_city => "What city is the site based in?",
151        default_country => "What country is the site based in?",
152        contact_email => "Contact email address for the site administrator?",
153        default_language => "What language will the site be in? (Please give an ISO language code.)",
[868]154        http_charset => "What character set should we put in the http headers? (This won't change the character set internally, just what it's reported as). Leave blank for none to be sent",
[607]155        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",
156        formatting_rules_link => "What URL do you want to use for the text formatting rules (leave blank to use a wiki node instead)?",
[587]157        backlinks_in_title => "Make node titles link to node backlinks (C2 style)?",
158        ellipsoid => "Which ellipsoid do you want to use? (eg 'Airy', 'WGS-84')",
[730]159        gmaps_api_key => "Do you have a Google Maps API key to use with this guide? If you enter it here the Google Maps functionality will be automatically enabled.",
160        centre_long => "What is the longitude of the centre point of a map to draw for your guide? (This question can be ignored if you aren't using Google Maps)",
161        centre_lat => "What is the latitude of the centre point of a map to draw for your guide? (This question can be ignored if you aren't using Google Maps)",
162        default_gmaps_zoom => "What default zoom level shall we use for Google Maps? (This question can be ignored if you aren't using Google Maps)",
163        default_gmaps_search_zoom => "What default zoom level shall we use for Google Maps in the search results? (This question can be ignored if you aren't using Google Maps)",
[776]164        force_wgs84 => "Forcibly treat stored lat/long data as if they used the WGS84 ellipsoid?",
165        licence_name => "What licence will you use for the guide?",
166        licence_url => "What is the URL to your licence?",
167        licence_info_url => "What is the URL to your local page about your licensing policy?"
[587]168    );
169
170    foreach my $var ( keys %questions ) {
171        my $method = $var . "__qu";
172        $self->$method( $questions{$var} );
173    }
174
175    return $self;
176}
177
178=back
179
180=head1 ACCESSORS
181
182Each of the accessors described below is read-write.  Additionally,
183for each of them, there is also a read-write accessor called, for
184example, C<dbname__qu>.  This will contain an English-language
185question suitable for asking for a value for that variable.  You
186shouldn't write to them, but this is not enforced.
187
188The defaults mentioned below are those which are applied when
189C<< ->new >> is called, to variables which are not supplied in
190the config file.
191
192=over
193
194=item * dbname
195
196=item * dbuser
197
198=item * dbpass
199
200=item * dbhost
201
202=item * script_name (default: C<wiki.cgi>)
203
204=item * install_directory (default: C</usr/lib/cgi-bin/openguides/>)
205
206=item * script_url (this is constrained to always end in C</>)
207
208=cut
209
210sub script_url {
211    my $self = shift;
212    # See perldoc Class::Accessor - can't just use SUPER.
213    my $url = $self->_script_url_accessor( @_ );
214    $url .= "/" unless $url =~ /\/$/;
215    return $url;
216}
217
218=item * custom_lib_path
219
220=item * use_plucene (default: true)
221
222=item * indexing_directory (default: C</usr/lib/cgi-bin/openguides/indexes>)
223
224=item * enable_page_deletion (default: false)
225
226=item * admin_pass (default: C<Change This!>)
227
228=item * stylesheet_url
229
230=item * site_name (default: C<Unconfigured OpenGuides site>)
231
232=item * navbar_on_home_page (default: true)
233
234=item * home_name (default: C<Home>)
235
236=item * site_desc (default: C<A default configuration of OpenGuides>)
237
238=item * default_city (default: C<London>)
239
240=item * default_country (default: C<United Kingdom>)
241
242=item * default_language (default: C<en>)
243
[868]244=item * http_charset
245
[587]246=item * contact_email
247
248=item * formatting_rules_node (default: C<Text Formatting Examples>)
249
[607]250=item * formatting_rules_link (default: C<http://openguides.org/page/text_formatting>
251
[587]252=item * backlinks_in_title (default: false)
253
254=item * geo_handler (default: C<1>)
255
[736]256=item * ellipsoid (default: C<WGS-84>)
[587]257
[730]258=item * gmaps_api_key
259
260=item * centre_long
261
262=item * centre_lat
263
264=item * default_gmaps_zoom
265
266=item * default_gmaps_search_zoom
267
268=item * force_wgs84
269
[776]270=item * licence_name
271
272=item * licence_url
273
274=item * licence_info_url
275
[587]276=back
277
278=head1 AUTHOR
279
280The OpenGuides Project (openguides-dev@openguides.org)
281
282=head1 COPYRIGHT
283
[736]284     Copyright (C) 2004-2006 The OpenGuides Project.  All Rights Reserved.
[587]285
286The OpenGuides distribution is free software; you can redistribute it
287and/or modify it under the same terms as Perl itself.
288
289=head1 SEE ALSO
290
291L<OpenGuides>
292
293=cut
294
2951;
Note: See TracBrowser for help on using the repository browser.