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 95     95   487185 use strict;
  95         148  
  95         2249  
3 95     95   302 use warnings;
  95         104  
  95         2234  
4              
5 95     95   285 use vars qw( $VERSION );
  95         108  
  95         3644  
6             $VERSION = '0.10';
7              
8 95     95   306 use Carp qw( croak );
  95         105  
  95         3563  
9 95     95   36672 use Config::Tiny;
  95         67534  
  95         2369  
10              
11 95     95   399 use base qw( Class::Accessor );
  95         105  
  95         54913  
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 responsive
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 119     119 1 5102749 my $class = shift;
68 119         418 my $self = { };
69 119         331 bless $self, $class;
70 119         608 return $self->_init( @_ );
71             }
72              
73             sub _init {
74 119     119   468 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 119         4343 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             responsive => 0,
129             );
130              
131             # See if we already have some config variables set.
132 119         211 my %stored;
133 119 100       916 if ( $args{file} ) {
    50          
134 1 50       5 my $read_config = Config::Tiny->read( $args{file} ) or
135             croak "Cannot read config file '$args{file}': $Config::Tiny::errstr";
136 0         0 %stored = %{$read_config->{_}};
  0         0  
137             } elsif ( $args{vars} ) {
138 118         203 %stored = %{ $args{vars} };
  118         1010  
139             }
140              
141             # Set all defaults first, then set the stored values. This allows us
142             # to make sure that the stored values override the defaults yet be sure
143             # to set any variables which have stored values but not defaults.
144 118         947 foreach my $var ( keys %defaults ) {
145 5900         41258 $self->$var( $defaults{$var} );
146             }
147 118         1218 foreach my $var ( keys %stored ) {
148 1353 50       7896 if ( $self->can( $var ) ) { # handle any garbage in file gracefully
149 1353         2498 $self->$var( $stored{$var} );
150             } else {
151 0         0 warn "Don't know what to do with variable '$var'";
152             }
153             }
154              
155             # And the questions.
156             # Don't forget to add to INSTALL when changing these.
157 118         4202 my %questions = (
158             dbtype => "What type of database do you want the site to run on? postgres/mysql/sqlite",
159             dbname => "What's the name of the database that this site runs on?",
160             dbuser => "...the database user that can access that database?",
161             dbpass => "...the password that they use to access the database?",
162             dbhost => "...the machine that the database is hosted on? (blank if local)",
163             dbport => "...the port the database is listening on? (blank if default)",
164             dbencoding => "...the encoding that your database uses? (blank if default)",
165             script_name => "What do you want the script to be called?",
166             install_directory => "What directory should I install it in?",
167             template_path => "What directory should I install the templates in?",
168             custom_template_path => "Where should I look for custom templates?",
169             script_url => "What URL does the install directory map to?",
170             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.",
171             use_plucene => "Do you want to use Plucene for searching? (recommended, but see Changes file before saying yes to this if you are upgrading)",
172             use_lucy => "Do you want to use Lucy for searching? (experimental)",
173             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)",
174             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.",
175             enable_page_deletion => "Do you want to enable page deletion?",
176             moderation_requires_password => "Is the admin password required for moderating pages?",
177             admin_pass => "Please specify a password for the site admin.",
178             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.",
179             enable_node_image => "Should nodes be allowed to have an externally hosted image?",
180             enable_common_categories => "Do you want a common list of categories shown on all node pages?",
181             enable_common_locales => "Do you want a common list of locales shown on all node pages?",
182             ping_services => "Which services do you wish to ping whenever you write a page? Can be pingerati, geourl, or both",
183             site_name => "What's the site called? (should be unique)",
184             navbar_on_home_page => "Do you want the navigation bar included on the home page?",
185             recent_changes_on_home_page => "Do you want the ten most recent changes included on the home page?",
186             random_page_omits_locales => "Do you want the \"Random Page\" link to avoid returning a locale page?",
187             random_page_omits_categories => "Do you want the \"Random Page\" link to avoid returning a category page?",
188             content_above_navbar_in_html => "Do you want the content to appear above the navbar in the HTML?",
189             home_name => "What should the home page of the wiki be called?",
190             site_desc => "How would you describe the site?",
191             default_city => "What city is the site based in?",
192             default_country => "What country is the site based in?",
193             contact_email => "Contact email address for the site administrator?",
194             default_language => "What language will the site be in? (Please give an ISO language code.)",
195             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",
196             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",
197             formatting_rules_link => "What URL do you want to use for the text formatting rules (leave blank to use a wiki node instead)?",
198             backlinks_in_title => "Make node titles link to node backlinks (C2 style)?",
199             ellipsoid => "Which ellipsoid do you want to use? (eg 'Airy', 'WGS-84')",
200             use_leaflet => "Do you want to use the Leaflet mapping library? (this is recommended)",
201             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.)",
202             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')",
203             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.)",
204             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)",
205             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)",
206             show_gmap_in_node_display => "Would you like to display a map on every node that has geodata?",
207             force_wgs84 => "Forcibly treat stored lat/long data as if they used the WGS84 ellipsoid?",
208             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.",
209             licence_name => "What licence will you use for the guide?",
210             licence_url => "What is the URL to your licence?",
211             licence_info_url => "What is the URL to your local page about your licensing policy?",
212             spam_detector_module => "What module would you like to use for spam detection? (optional)",
213             host_checker_module => "What module would you like to use to run an IP blacklist? (optional)",
214             custom_macro_module => "What module would you like to use to define custom macros? (optional)",
215             static_path => "What directory should we install static content (CSS, images, javascript) to?",
216             static_url => "What is the URL corresponding to the static content?",
217             send_moderation_notifications => "Should we send email notifications when a moderated node is edited?",
218             website_link_max_chars => "How many characters of the URL of node websites should be displayed?",
219             moderate_whitelist => "Enter a comma-separated list of IP addresses able to make changes to moderated nodes and have them show up immediately",
220             read_only => "Should the guide be read-only (no edits permitted)?",
221             responsive => "Should the site be mobile-friendly (responsive)?",
222             );
223              
224 118         981 foreach my $var ( keys %questions ) {
225 7552         38968 my $method = $var . "__qu";
226 7552         18336 $self->$method( $questions{$var} );
227             }
228              
229 118         2069 return $self;
230             }
231              
232             =back
233              
234             =head1 ACCESSORS
235              
236             Each of the accessors described below is read-write. Additionally,
237             for each of them, there is also a read-write accessor called, for
238             example, C. This will contain an English-language
239             question suitable for asking for a value for that variable. You
240             shouldn't write to them, but this is not enforced.
241              
242             The defaults mentioned below are those which are applied when
243             C<< ->new >> is called, to variables which are not supplied in
244             the config file.
245              
246             =over
247              
248             =item * dbname
249              
250             =item * dbuser
251              
252             =item * dbpass
253              
254             =item * dbhost
255              
256             =item * dbport
257              
258             =item * dbencoding
259              
260             =item * script_name (default: C)
261              
262             =item * install_directory (default: C)
263              
264             =item * script_url (this is constrained to always end in C)
265              
266             =cut
267              
268             sub script_url {
269 1130     1130 1 4607 my $self = shift;
270             # See perldoc Class::Accessor - can't just use SUPER.
271 1130         3772 my $url = $self->_script_url_accessor( @_ );
272 1130 100 66     15996 $url .= "/" unless (defined $url && $url =~ /\/$/);
273 1130         3273 return $url;
274             }
275              
276             =item * custom_lib_path
277              
278             =item * use_plucene (default: true)
279              
280             =item * use_lucy (default: false)
281              
282             =item * search_content_munger_module
283              
284             =item * indexing_directory (default: C)
285              
286             =item * enable_page_deletion (default: false)
287              
288             =item * admin_pass (default: C)
289              
290             =item * stylesheet_url
291              
292             =item * site_name (default: C)
293              
294             =item * navbar_on_home_page (default: true)
295              
296             =item * recent_changes_on_home_page (default: true)
297              
298             =item * random_page_omits_locales (default: false)
299              
300             =item * random_page_omits_categories (default: false)
301              
302             =item * content_above_navbar_in_html (default: false)
303              
304             =item * home_name (default: C)
305              
306             =item * site_desc (default: C)
307              
308             =item * default_city (default: C)
309              
310             =item * default_country (default: C)
311              
312             =item * default_language (default: C)
313              
314             =item * http_charset
315              
316             =item * contact_email
317              
318             =item * formatting_rules_node (default: C)
319              
320             =item * formatting_rules_link (default: C
321              
322             =item * backlinks_in_title (default: false)
323              
324             =item * geo_handler (default: C<1>)
325              
326             =item * ellipsoid (default: C)
327              
328             =item * use_leaflet
329              
330             =item * gmaps_api_key
331              
332             =item * centre_long
333              
334             =item * centre_lat
335              
336             =item * default_gmaps_zoom
337              
338             =item * default_gmaps_search_zoom
339              
340             =item * show_gmap_in_node_display
341              
342             =item * force_wgs84
343              
344             =item * google_analytics_key
345              
346             =item * licence_name
347              
348             =item * licence_url
349              
350             =item * licence_info_url
351              
352             =item * spam_detector_module
353              
354             =item * host_checker_module
355              
356             =item * custom_macro_module
357              
358             =item * static_path
359              
360             =item * static_url (this is constrained to always end in C)
361              
362             =cut
363              
364             sub static_url {
365 250     250 1 518 my $self = shift;
366             # See perldoc Class::Accessor - can't just use SUPER.
367 250         1337 my $url = $self->_static_url_accessor( @_ );
368 250 100 100     3443 $url .= "/" unless (defined $url && $url =~ /\/$/);
369 250         731 return $url;
370             }
371              
372             =item * send_moderation_notifications
373              
374             =item * moderate_whitelist
375              
376             =item * website_link_max_chars (default: C<25>)
377              
378             =item * read_only
379              
380             =item * responsive
381              
382             =back
383              
384             =head1 AUTHOR
385              
386             The OpenGuides Project (openguides-dev@lists.openguides.org)
387              
388             =head1 COPYRIGHT
389              
390             Copyright (C) 2004-2013 The OpenGuides Project. All Rights Reserved.
391              
392             The OpenGuides distribution is free software; you can redistribute it
393             and/or modify it under the same terms as Perl itself.
394              
395             =head1 SEE ALSO
396              
397             L
398              
399             =cut
400              
401             1;