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

Last change on this file since 1116 was 1116, checked in by Dominic Hargreaves, 14 years ago

Ensure that all modules we ship are versioned. (references #232)

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