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

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

change default database type to sqlite (#241)

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