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

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

Add support for installing static files (fixes #213)

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