| 27 | | EOF |
| 28 | | |
| 29 | | my $continue = Module::Build->y_n("Continue with install?", "y"); |
| 30 | | exit 0 unless $continue; |
| 31 | | } |
| 32 | | |
| 33 | | my $existing_config_file = 'wiki.conf'; |
| 34 | | my $existing_config; |
| 35 | | |
| 36 | | if (-f $existing_config_file) { |
| 37 | | $existing_config = OpenGuides::Config->new(file => $existing_config_file); |
| 38 | | } else { |
| 39 | | print <<EOF; |
| 40 | | No existing configuration file found; assuming this is a new install. |
| 41 | | See the message above if this isn't correct. |
| 42 | | |
| 43 | | EOF |
| 44 | | $existing_config = OpenGuides::Config->new(); |
| 45 | | } |
| 46 | | |
| 47 | | my %yn_vars = map { $_ => 1 } |
| 48 | | qw(use_plucene enable_page_deletion navbar_on_home_page backlinks_in_title |
| 49 | | moderation_requires_password enable_node_image enable_common_categories |
| 50 | | enable_common_locales recent_changes_on_home_page |
| 51 | | random_page_omits_locales random_page_omits_categories |
| 52 | | content_above_navbar_in_html show_gmap_in_node_display force_wgs84 |
| 53 | | send_moderation_notifications); |
| 54 | | |
| 55 | | my $skip_config = $force ? 'y' : Module::Build->y_n("Skip OpenGuides configuration?", "n"); |
| 56 | | if ( $skip_config ) { |
| 57 | | print <<EOF; |
| 58 | | =========================================================================== |
| 59 | | Skipping OpenGuides configuration - any configuration options previously |
| 60 | | saved will be used instead. You may tweak your configuration now by |
| 61 | | editing the 'wiki.conf' file produced by this script. |
| 62 | | =========================================================================== |
| 63 | | EOF |
| 64 | | } |
| 65 | | |
| 66 | | my @answers; |
| 67 | | |
| 68 | | # It is an ancient Configurer, and he chooseth one of three. |
| 69 | | my $dbtype; |
| 70 | | my $dbtype_qu = $existing_config->dbtype__qu; |
| 71 | | if ( $skip_config ) { |
| 72 | | $dbtype = $existing_config->dbtype; |
| 73 | | } else { |
| 74 | | until ( $dbtype ) { |
| 75 | | my $def = $existing_config->dbtype; |
| 76 | | $dbtype = Module::Build->prompt("\n$dbtype_qu", $def); |
| 77 | | $dbtype = lc($dbtype); |
| 78 | | $dbtype =~ s/^\s*//; |
| 79 | | $dbtype =~ s/\s*$//; |
| 80 | | unless ( $dbtype eq "postgres" or $dbtype eq "mysql" |
| 81 | | or $dbtype eq "sqlite" ) { |
| 82 | | undef $dbtype; |
| 83 | | } |
| | 39 | return $ret; |
| 85 | | } |
| 86 | | |
| 87 | | # Check they have the relevant DBD driver installed. |
| 88 | | my %drivers = ( postgres => "DBD::Pg", |
| 89 | | mysql => "DBD::mysql", |
| 90 | | sqlite => "DBD::SQLite", |
| 91 | | ); |
| 92 | | eval "require $drivers{$dbtype}"; |
| 93 | | warn "$drivers{$dbtype} is needed to run a $dbtype database" if $@; |
| 94 | | |
| 95 | | push @answers, { question => $dbtype_qu, |
| 96 | | variable => "dbtype", |
| 97 | | value => $dbtype }; |
| 98 | | |
| 99 | | my $install_directory; # used to suggest template paths |
| 100 | | my $use_plucene = 1; # keep track of this so we know what to put in prereqs |
| 101 | | my $centre_lat = ''; # contains centre lat derived from Google Maps URL |
| 102 | | foreach my $var ( qw( |
| 103 | | dbname dbuser dbpass dbhost dbport script_name |
| 104 | | install_directory template_path custom_template_path script_url |
| 105 | | custom_lib_path use_plucene indexing_directory enable_page_deletion |
| 106 | | admin_pass stylesheet_url site_name navbar_on_home_page |
| 107 | | recent_changes_on_home_page random_page_omits_locales |
| 108 | | random_page_omits_categories content_above_navbar_in_html home_name |
| 109 | | site_desc default_city default_country contact_email default_language |
| 110 | | formatting_rules_node backlinks_in_title gmaps_api_key centre_long |
| 111 | | centre_lat show_gmap_in_node_display default_gmaps_zoom |
| 112 | | default_gmaps_search_zoom force_wgs84 google_analytics_key |
| 113 | | licence_name licence_url licence_info_url moderation_requires_password |
| 114 | | enable_node_image enable_common_categories enable_common_locales |
| 115 | | spam_detector_module host_checker_module static_path static_url |
| 116 | | send_moderation_notifications |
| 117 | | ) ) { |
| 118 | | my $q_method = $var . "__qu"; |
| 119 | | my $qu = $existing_config->$q_method; |
| 120 | | my $type = $yn_vars{$var} ? "y_n" : ""; |
| 121 | | my $def = $existing_config->$var; |
| 122 | | my $val = $def; |
| 123 | | |
| 124 | | # Override dbname question for SQLite only. |
| 125 | | if ( $dbtype eq "sqlite" and $var eq "dbname" ) { |
| 126 | | $qu = "what's the full filename of the SQLite database this site runs on?"; |
| 127 | | } |
| 128 | | |
| 129 | | if ( $dbtype eq "sqlite" and |
| 130 | | ( $var eq "dbuser" or $var eq "dbpass" or $var eq "dbhost" or |
| 131 | | $var eq "dbport") |
| 132 | | ) { |
| 133 | | print "$var not relevant for SQLite... skipping...\n" |
| 134 | | unless $skip_config; |
| 135 | | push @answers, { question => $qu, |
| 136 | | variable => $var, |
| 137 | | value => "not-used" }; |
| 138 | | next; |
| 139 | | } |
| 140 | | |
| 141 | | # We don't ask this for new installs as Search::InvertedIndex is |
| 142 | | # deprecated |
| 143 | | if ( $var eq "use_plucene" and $existing_config->$var == 1) { |
| 144 | | print "Skipping question about plucene\n" |
| 145 | | unless $skip_config; |
| 146 | | push @answers, { question => $qu, |
| 147 | | variable => $var, |
| 148 | | value => 1 }; |
| 149 | | next; |
| 150 | | } |
| 151 | | |
| 152 | | # Make sensible suggestions for template paths if we don't already |
| 153 | | # have them stored. Not really a default, but a useful hint/shortcut. |
| 154 | | if ( $var eq "template_path" && !defined $existing_config->$var ) { |
| 155 | | $def = $install_directory; |
| 156 | | $def .= "/" unless $def =~ m|/$|; |
| 157 | | $def .= "templates"; |
| 158 | | } |
| 159 | | if ( $var eq "custom_template_path" && !defined $existing_config->$var ) { |
| 160 | | $def = $install_directory; |
| 161 | | $def .= "/" unless $def =~ m|/$|; |
| 162 | | $def .= "custom-templates"; |
| 163 | | } |
| 164 | | |
| 165 | | # If a Google Maps URL was provided last time we know the centre_lat |
| 166 | | if ( $var eq 'centre_lat' && $centre_lat ) { |
| 167 | | $val = $centre_lat; |
| 168 | | next; |
| 169 | | } |
| 170 | | |
| 171 | | # Here is where we actually ask the questions. |
| 172 | | unless ( $skip_config ) { |
| 173 | | if ( $type eq "y_n" ) { |
| 174 | | # may be stored as true/false integer value |
| 175 | | if ( $def =~ /^\d+$/ ) { |
| 176 | | $def = $def ? "y" : "n"; |
| 177 | | } |
| 178 | | $val = Module::Build->y_n("\n$qu ", $def); |
| 179 | | } else { |
| 180 | | $val = Module::Build->prompt("\n$qu ", $def); |
| 181 | | } |
| 182 | | } |
| 183 | | |
| 184 | | # Allow user to use a Google Maps URL rather than enter lat/long by hand. |
| 185 | | # We assume centre_long is being asked for first; ensure so in big list above. |
| 186 | | if ( $var eq 'centre_long' ) { |
| 187 | | if ( $val =~ /ll=([-\d.]+),([-\d.]+)/ ) { |
| 188 | | print "Got a Google Maps URL with centre long,lat: [$1, $2]\n"; |
| 189 | | $val = $1; |
| 190 | | $centre_lat = $2; |
| 191 | | } |
| 192 | | } |
| 193 | | |
| 194 | | # Store install_directory so we can use it to suggest template paths. |
| 195 | | $install_directory = $val if $var eq "install_directory"; |
| 196 | | |
| 197 | | # Keep track of chosen search method so we know what to put in prereqs. |
| 198 | | # From Module::Build docs: ->y_n returns a Perl boolean true or false. |
| 199 | | $use_plucene = 1 if $var eq "use_plucene" and $val; |
| 200 | | |
| 201 | | # Make sure that script_url ends in a / |
| 202 | | if ( $var eq "script_url" and $val !~ /\/$/ ) { |
| 203 | | $val .= "/"; |
| 204 | | } |
| 205 | | |
| 206 | | push @answers, { question => $qu, |
| 207 | | variable => $var, |
| 208 | | value => $val }; |
| 209 | | } |
| 210 | | |
| 211 | | # Now deal with the geo stuff. |
| 212 | | my $geo_handler; |
| 213 | | my $geo_handler_qu = "Distance calculation methods available are:" |
| 214 | | . "\n 1) British National Grid" |
| 215 | | . "\n 2) Irish National Grid" |
| 216 | | . "\n 3) UTM ellipsoid" |
| 217 | | . "\nWhich would you like to use?"; |
| 218 | | |
| 219 | | if ( $skip_config ) { |
| 220 | | # We default to GB National Grid for historical reasons. |
| 221 | | $geo_handler = $existing_config->geo_handler; |
| 222 | | } else { |
| 223 | | my $choice; |
| 224 | | until ( $choice ) { |
| 225 | | my $def = $existing_config->geo_handler; |
| 226 | | $choice = Module::Build->prompt("\n".$geo_handler_qu, $def); |
| 227 | | $choice =~ s/^\s*//; |
| 228 | | $choice =~ s/\s*$//; |
| 229 | | unless ( $choice eq "1" or $choice eq "2" or $choice eq "3" ) { |
| 230 | | undef $choice; |
| 231 | | } |
| 232 | | } |
| 233 | | $geo_handler = $choice; |
| 234 | | } |
| 235 | | |
| 236 | | $geo_handler_qu =~ s/\n//gs; |
| 237 | | push @answers, { |
| 238 | | question => $geo_handler_qu, |
| 239 | | variable => "geo_handler", |
| 240 | | value => $geo_handler, |
| 241 | | }; |
| 242 | | |
| 243 | | if ( $geo_handler eq "3" ) { |
| 244 | | my $qu = $existing_config->ellipsoid__qu; |
| 245 | | my $ellipsoid; |
| 246 | | if ( $skip_config ) { |
| 247 | | $ellipsoid = $existing_config->ellipsoid; |
| 248 | | } else { |
| 249 | | my $def = $existing_config->ellipsoid; |
| 250 | | $ellipsoid = Module::Build->prompt("\n".$qu, $def); |
| 251 | | $ellipsoid =~ s/^\s*//; |
| 252 | | $ellipsoid =~ s/\s*$//; |
| 253 | | } |
| 254 | | push @answers, { |
| 255 | | question => $qu, |
| 256 | | variable => "ellipsoid", |
| 257 | | value => $ellipsoid, |
| 258 | | }; |
| 259 | | } |
| 260 | | |
| 261 | | # Create a user-friendly config file from answers to prompts. |
| 262 | | open FILE, ">wiki.conf" or die "Can't open wiki.conf for writing: $!"; |
| 263 | | foreach my $ans (@answers) { |
| 264 | | print FILE "# $ans->{question}\n"; |
| 265 | | print FILE "$ans->{variable} = $ans->{value}\n\n"; |
| 266 | | } |
| 267 | | close FILE or die "Can't close wiki.conf: $!"; |
| | 41 | }, |
| | 42 | ); |