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

Last change on this file since 1240 was 1240, checked in by Dominic Hargreaves, 13 years ago

Add the ability to whitelist hosts who can change moderated nodes
without explicit moderation - thanks Oliver (fixes #203)

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