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

Last change on this file since 965 was 965, checked in by kake, 15 years ago

Add Google Analytics support.

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