File Coverage

blib/lib/OpenGuides/Config.pm
Criterion Covered Total %
statement 47 50 94.0
branch 9 12 75.0
condition 5 6 83.3
subroutine 10 10 100.0
pod 3 3 100.0
total 74 81 91.3


line stmt bran cond sub pod time code
1             package OpenGuides::Config;
2 94     94   783657 use strict;
  94         161  
  94         3989  
3 94     94   477 use warnings;
  94         157  
  94         3662  
4              
5 94     94   487 use vars qw( $VERSION );
  94         142  
  94         5702  
6             $VERSION = '0.09';
7              
8 94     94   478 use Carp qw( croak );
  94         167  
  94         5704  
9 94     94   56441 use Config::Tiny;
  94         89940  
  94         3308  
10              
11 94     94   573 use base qw( Class::Accessor );
  94         164  
  94         77347  
12             my @variables = qw(
13             dbtype dbname dbuser dbpass dbport dbhost dbencoding
14             script_name install_directory script_url
15             custom_lib_path use_plucene use_lucy search_content_munger_module
16             indexing_directory enable_page_deletion
17             admin_pass stylesheet_url site_name navbar_on_home_page
18             recent_changes_on_home_page random_page_omits_locales
19             random_page_omits_categories content_above_navbar_in_html home_name
20             site_desc default_city default_country contact_email
21             default_language http_charset ping_services
22             formatting_rules_node formatting_rules_link backlinks_in_title template_path
23             custom_template_path geo_handler ellipsoid gmaps_api_key centre_long
24             show_gmap_in_node_display google_analytics_key use_leaflet
25             centre_lat default_gmaps_zoom default_gmaps_search_zoom force_wgs84
26             licence_name licence_url licence_info_url
27             moderation_requires_password moderate_whitelist
28             enable_node_image enable_common_categories enable_common_locales
29             spam_detector_module host_checker_module custom_macro_module
30             static_path static_url
31             send_moderation_notifications website_link_max_chars read_only
32             );
33             my @questions = map { $_ . "__qu" } @variables;
34             OpenGuides::Config->mk_accessors( @variables );
35             OpenGuides::Config->mk_accessors( @questions );
36              
37             =head1 NAME
38              
39             OpenGuides::Config - Handle OpenGuides configuration variables.
40              
41             =head1 DESCRIPTION
42              
43             Does config stuff for OpenGuides. Distributed and installed as part of
44             the OpenGuides project, not intended for independent installation.
45             This documentation is probably only useful to OpenGuides developers.
46              
47             =head1 METHODS
48              
49             =over
50              
51             =item B
52              
53             my $config = OpenGuides::Config->new( file => "wiki.conf" );
54              
55             Initialises itself from the config file specified. Variables which
56             are not set in that file, and which have sensible defaults, will be
57             initialised as described below in ACCESSORS; others will be given a
58             value of C.
59              
60             my $config = OpenGuides::Config->new( vars => { dbname => "foo" } );
61              
62             As above but gets variables from a supplied hashref instead.
63              
64             =cut
65              
66             sub new {
67 118     118 1 5368922 my $class = shift;
68 118         527 my $self = { };
69 118         546 bless $self, $class;
70 118         755 return $self->_init( @_ );
71             }
72              
73             sub _init {
74 118     118   611 my ($self, %args) = @_;
75              
76             # Here are the defaults for the variable values.
77             # Don't forget to add to INSTALL when changing these.
78 118         6285 my %defaults = (
79             dbtype => "sqlite",
80             script_name => "wiki.cgi",
81             install_directory => "/usr/lib/cgi-bin/openguides/",
82             use_plucene => 1,
83             use_lucy => 0,
84             search_content_munger_module => "",
85             indexing_directory => "/usr/lib/cgi-bin/openguides/indexes/",
86             enable_page_deletion => 0,
87             moderation_requires_password => 1,
88             moderate_whitelist => "",
89             admin_pass => "Change This!",
90             enable_node_image => 1,
91             enable_common_categories => 0,
92             enable_common_locales => 0,
93             ping_services => "",
94             site_name => "Unconfigured OpenGuides site",
95             navbar_on_home_page => 1,
96             recent_changes_on_home_page => 1,
97             random_page_omits_locales => 0,
98             random_page_omits_categories => 0,
99             content_above_navbar_in_html => 0,
100             home_name => "Home",
101             site_desc => "A default configuration of OpenGuides",
102             default_city => "",
103             default_country => "",
104             default_language => "en",
105             http_charset => "",
106             formatting_rules_node => "Text Formatting Examples",
107             formatting_rules_link => "http://openguides.org/text_formatting",
108             backlinks_in_title => 0,
109             geo_handler => 1,
110             ellipsoid => "WGS-84",
111             use_leaflet => 0,
112             show_gmap_in_node_display => 1,
113             centre_long => 0,
114             centre_lat => 0,
115             default_gmaps_zoom => 5,
116             default_gmaps_search_zoom => 3,
117             force_wgs84 => 0,
118             licence_name => "",
119             licence_url => "",
120             licence_info_url => "",
121             spam_detector_module => "",
122             host_checker_module => "",
123             custom_macro_module => "",
124             static_path => "/usr/local/share/openguides/static",
125             send_moderation_notifications => 1,
126             website_link_max_chars => 25,
127             read_only => 0,
128             );
129              
130             # See if we already have some config variables set.
131 118         306 my %stored;
132 118 100       1156 if ( $args{file} ) {
    50          
133 1 50       9 my $read_config = Config::Tiny->read( $args{file} ) or
134             croak "Cannot read config file '$args{file}': $Config::Tiny::errstr";
135 0         0 %stored = %{$read_config->{_}};
  0         0  
136             } elsif ( $args{vars} ) {
137 117         254 %stored = %{ $args{vars} };
  117         2621  
138             }
139              
140             # Set all defaults first, then set the stored values. This allows us
141             # to make sure that the stored values override the defaults yet be sure
142             # to set any variables which have stored values but not defaults.
143 117         1243 foreach my $var ( keys %defaults ) {
144 5733         60195 $self->$var( $defaults{$var} );
145             }
146 117         1895 foreach my $var ( keys %stored ) {
147 1340 50       11729 if ( $self->can( $var ) ) { # handle any garbage in file gracefully
148 1340         3210 $self->$var( $stored{$var} );
149             } else {
150 0         0 warn "Don't know what to do with variable '$var'";
151             }
152             }
153              
154             # And the questions.
155             # Don't forget to add to INSTALL when changing these.
156 117         6135 my %questions = (
157             dbtype => "What type of database do you want the site to run on? postgres/mysql/sqlite",
158             dbname => "What's the name of the database that this site runs on?",
159             dbuser => "...the database user that can access that database?",
160             dbpass => "...the password that they use to access the database?",
161             dbhost => "...the machine that the database is hosted on? (blank if local)",
162             dbport => "...the port the database is listening on? (blank if default)",
163             dbencoding => "...the encoding that your database uses? (blank if default)",
164             script_name => "What do you want the script to be called?",
165             install_directory => "What directory should I install it in?",
166             template_path => "What directory should I install the templates in?",
167             custom_template_path => "Where should I look for custom templates?",
168             script_url => "What URL does the install directory map to?",
169             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.",
170             use_plucene => "Do you want to use Plucene for searching? (recommended, but see Changes file before saying yes to this if you are upgrading)",
171             use_lucy => "Do you want to use Lucy for searching? (experimental)",
172             search_content_munger_module => "What module would you like to use to munge node content before indexing for the search? (optional, only works with Plucene and Lucy)",
173             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.",
174             enable_page_deletion => "Do you want to enable page deletion?",
175             moderation_requires_password => "Is the admin password required for moderating pages?",
176             admin_pass => "Please specify a password for the site admin.",
177             stylesheet_url => "What's the URL of the site's stylesheet? If you don't enter one here, the basic OpenGuides stylesheet will be used instead.",
178             enable_node_image => "Should nodes be allowed to have an externally hosted image?",
179             enable_common_categories => "Do you want a common list of categories shown on all node pages?",
180             enable_common_locales => "Do you want a common list of locales shown on all node pages?",
181             ping_services => "Which services do you wish to ping whenever you write a page? Can be pingerati, geourl, or both",
182             site_name => "What's the site called? (should be unique)",
183             navbar_on_home_page => "Do you want the navigation bar included on the home page?",
184             recent_changes_on_home_page => "Do you want the ten most recent changes included on the home page?",
185             random_page_omits_locales => "Do you want the \"Random Page\" link to avoid returning a locale page?",
186             random_page_omits_categories => "Do you want the \"Random Page\" link to avoid returning a category page?",
187             content_above_navbar_in_html => "Do you want the content to appear above the navbar in the HTML?",
188             home_name => "What should the home page of the wiki be called?",
189             site_desc => "How would you describe the site?",
190             default_city => "What city is the site based in?",
191             default_country => "What country is the site based in?",
192             contact_email => "Contact email address for the site administrator?",
193             default_language => "What language will the site be in? (Please give an ISO language code.)",
194             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",
195             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",
196             formatting_rules_link => "What URL do you want to use for the text formatting rules (leave blank to use a wiki node instead)?",
197             backlinks_in_title => "Make node titles link to node backlinks (C2 style)?",
198             ellipsoid => "Which ellipsoid do you want to use? (eg 'Airy', 'WGS-84')",
199             use_leaflet => "Do you want to use the Leaflet mapping library? (this is recommended)",
200             gmaps_api_key => "Do you have a Google Maps API key to use with this guide? If so, enter it here. (Note: our Google Maps support is deprecated, and we recommend you choose to use Leaflet instead.)",
201             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 - we recommend you use Leaflet instead, as our Leaflet code will figure this out for you.) You may paste in a Google Maps URL here (hint: copy URL from 'Link to this page')",
202             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 - we recommend you use Leaflet instead, as our Leaflet code will figure this out for you.)",
203             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)",
204             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)",
205             show_gmap_in_node_display => "Would you like to display a map on every node that has geodata?",
206             force_wgs84 => "Forcibly treat stored lat/long data as if they used the WGS84 ellipsoid?",
207             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.",
208             licence_name => "What licence will you use for the guide?",
209             licence_url => "What is the URL to your licence?",
210             licence_info_url => "What is the URL to your local page about your licensing policy?",
211             spam_detector_module => "What module would you like to use for spam detection? (optional)",
212             host_checker_module => "What module would you like to use to run an IP blacklist? (optional)",
213             custom_macro_module => "What module would you like to use to define custom macros? (optional)",
214             static_path => "What directory should we install static content (CSS, images, javascript) to?",
215             static_url => "What is the URL corresponding to the static content?",
216             send_moderation_notifications => "Should we send email notifications when a moderated node is edited?",
217             website_link_max_chars => "How many characters of the URL of node websites should be displayed?",
218             moderate_whitelist => "Enter a comma-separated list of IP addresses able to make changes to moderated nodes and have them show up immediately",
219             read_only => "Should the guide be read-only (no edits permitted)?",
220             );
221              
222 117         1161 foreach my $var ( keys %questions ) {
223 7371         59800 my $method = $var . "__qu";
224 7371         26563 $self->$method( $questions{$var} );
225             }
226              
227 117         3689 return $self;
228             }
229              
230             =back
231              
232             =head1 ACCESSORS
233              
234             Each of the accessors described below is read-write. Additionally,
235             for each of them, there is also a read-write accessor called, for
236             example, C. This will contain an English-language
237             question suitable for asking for a value for that variable. You
238             shouldn't write to them, but this is not enforced.
239              
240             The defaults mentioned below are those which are applied when
241             C<< ->new >> is called, to variables which are not supplied in
242             the config file.
243              
244             =over
245              
246             =item * dbname
247              
248             =item * dbuser
249              
250             =item * dbpass
251              
252             =item * dbhost
253              
254             =item * dbport
255              
256             =item * dbencoding
257              
258             =item * script_name (default: C)
259              
260             =item * install_directory (default: C)
261              
262             =item * script_url (this is constrained to always end in C)
263              
264             =cut
265              
266             sub script_url {
267 1121     1121 1 9044 my $self = shift;
268             # See perldoc Class::Accessor - can't just use SUPER.
269 1121         4944 my $url = $self->_script_url_accessor( @_ );
270 1121 100 66     21174 $url .= "/" unless (defined $url && $url =~ /\/$/);
271 1121         4605 return $url;
272             }
273              
274             =item * custom_lib_path
275              
276             =item * use_plucene (default: true)
277              
278             =item * use_lucy (default: false)
279              
280             =item * search_content_munger_module
281              
282             =item * indexing_directory (default: C)
283              
284             =item * enable_page_deletion (default: false)
285              
286             =item * admin_pass (default: C)
287              
288             =item * stylesheet_url
289              
290             =item * site_name (default: C)
291              
292             =item * navbar_on_home_page (default: true)
293              
294             =item * recent_changes_on_home_page (default: true)
295              
296             =item * random_page_omits_locales (default: false)
297              
298             =item * random_page_omits_categories (default: false)
299              
300             =item * content_above_navbar_in_html (default: false)
301              
302             =item * home_name (default: C)
303              
304             =item * site_desc (default: C)
305              
306             =item * default_city (default: C)
307              
308             =item * default_country (default: C)
309              
310             =item * default_language (default: C)
311              
312             =item * http_charset
313              
314             =item * contact_email
315              
316             =item * formatting_rules_node (default: C)
317              
318             =item * formatting_rules_link (default: C
319              
320             =item * backlinks_in_title (default: false)
321              
322             =item * geo_handler (default: C<1>)
323              
324             =item * ellipsoid (default: C)
325              
326             =item * use_leaflet
327              
328             =item * gmaps_api_key
329              
330             =item * centre_long
331              
332             =item * centre_lat
333              
334             =item * default_gmaps_zoom
335              
336             =item * default_gmaps_search_zoom
337              
338             =item * show_gmap_in_node_display
339              
340             =item * force_wgs84
341              
342             =item * google_analytics_key
343              
344             =item * licence_name
345              
346             =item * licence_url
347              
348             =item * licence_info_url
349              
350             =item * spam_detector_module
351              
352             =item * host_checker_module
353              
354             =item * custom_macro_module
355              
356             =item * static_path
357              
358             =item * static_url (this is constrained to always end in C)
359              
360             =cut
361              
362             sub static_url {
363 245     245 1 619 my $self = shift;
364             # See perldoc Class::Accessor - can't just use SUPER.
365 245         1409 my $url = $self->_static_url_accessor( @_ );
366 245 100 100     4217 $url .= "/" unless (defined $url && $url =~ /\/$/);
367 245         940 return $url;
368             }
369              
370             =item * send_moderation_notifications
371              
372             =item * moderate_whitelist
373              
374             =item * website_link_max_chars (default: C<25>)
375              
376             =item * read_only
377              
378             =back
379              
380             =head1 AUTHOR
381              
382             The OpenGuides Project (openguides-dev@lists.openguides.org)
383              
384             =head1 COPYRIGHT
385              
386             Copyright (C) 2004-2013 The OpenGuides Project. All Rights Reserved.
387              
388             The OpenGuides distribution is free software; you can redistribute it
389             and/or modify it under the same terms as Perl itself.
390              
391             =head1 SEE ALSO
392              
393             L
394              
395             =cut
396              
397             1;