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

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

Send email notifications when moderated nodes are edited (fixes #138)

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