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

Last change on this file since 1037 was 1037, checked in by kake, 14 years ago

Added experimental support for local spam detection modules.

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