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

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

Add config options to let admins omit category and/or locale pages from the list of pages that can be returned by the Random Page link (first half of ticket #200).

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