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

Last change on this file since 874 was 874, checked in by nick, 15 years ago

Add ping support

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