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

Last change on this file since 857 was 857, checked in by Dominic Hargreaves, 15 years ago

Minor fixups

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