Merge pull request #3095 from Elberet/master
FIX: handle timezone detection errors
This commit is contained in:
commit
f49b11aa34
|
@ -7,6 +7,7 @@ require 'tsort'
|
||||||
require 'set'
|
require 'set'
|
||||||
require 'optparse'
|
require 'optparse'
|
||||||
require 'etc'
|
require 'etc'
|
||||||
|
require 'open3'
|
||||||
|
|
||||||
class ImportScripts::Smf2 < ImportScripts::Base
|
class ImportScripts::Smf2 < ImportScripts::Base
|
||||||
|
|
||||||
|
@ -28,19 +29,21 @@ class ImportScripts::Smf2 < ImportScripts::Base
|
||||||
attr_reader :options
|
attr_reader :options
|
||||||
|
|
||||||
def initialize(options)
|
def initialize(options)
|
||||||
|
if options.timezone.nil?
|
||||||
|
$stderr.puts "No source timezone given and autodetection from PHP failed."
|
||||||
|
$stderr.puts "Use -t option to specify correct source timezone:"
|
||||||
|
$stderr.puts options.usage
|
||||||
|
exit 1
|
||||||
|
end
|
||||||
|
|
||||||
super()
|
super()
|
||||||
@options = options
|
@options = options
|
||||||
|
|
||||||
begin
|
begin
|
||||||
timezone = `php -i`.lines.each do |line|
|
Time.zone = options.timezone
|
||||||
key, *vals = line.split(' => ').map(&:strip)
|
|
||||||
break vals[0] if key == 'Default timezone'
|
|
||||||
end
|
|
||||||
Time.zone = timezone
|
|
||||||
rescue Errno::ENOENT
|
|
||||||
$stderr.puts "Cannot autodetect PHP timezone setting, php not found in $PATH"
|
|
||||||
rescue ArgumentError
|
rescue ArgumentError
|
||||||
$stderr.puts "Cannot set timezone '#{timezone}' (from PHP)"
|
$stderr.puts "Timezone name '#{options.timezone}' is invalid."
|
||||||
|
exit 1
|
||||||
end
|
end
|
||||||
|
|
||||||
if options.database.blank?
|
if options.database.blank?
|
||||||
|
@ -515,6 +518,7 @@ class ImportScripts::Smf2 < ImportScripts::Base
|
||||||
self.host ||= 'localhost'
|
self.host ||= 'localhost'
|
||||||
self.username ||= Etc.getlogin
|
self.username ||= Etc.getlogin
|
||||||
self.prefix ||= 'smf_'
|
self.prefix ||= 'smf_'
|
||||||
|
self.timezone ||= get_php_timezone
|
||||||
end
|
end
|
||||||
|
|
||||||
def usage
|
def usage
|
||||||
|
@ -527,9 +531,20 @@ class ImportScripts::Smf2 < ImportScripts::Base
|
||||||
attr_accessor :database
|
attr_accessor :database
|
||||||
attr_accessor :prefix
|
attr_accessor :prefix
|
||||||
attr_accessor :smfroot
|
attr_accessor :smfroot
|
||||||
|
attr_accessor :timezone
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
def get_php_timezone
|
||||||
|
phpinfo, status = Open3.capture2('phpnope', '-i')
|
||||||
|
phpinfo.lines.each do |line|
|
||||||
|
key, *vals = line.split(' => ').map(&:strip)
|
||||||
|
break vals[0] if key == 'Default timezone'
|
||||||
|
end
|
||||||
|
rescue Errno::ENOENT
|
||||||
|
$stderr.puts "Error: PHP CLI executable not found"
|
||||||
|
end
|
||||||
|
|
||||||
def read_smf_settings
|
def read_smf_settings
|
||||||
settings = File.join(self.smfroot, 'Settings.php')
|
settings = File.join(self.smfroot, 'Settings.php')
|
||||||
IO.readlines(settings).each do |line|
|
IO.readlines(settings).each do |line|
|
||||||
|
@ -555,6 +570,7 @@ class ImportScripts::Smf2 < ImportScripts::Base
|
||||||
o.on('-p [PASS]', :OPTIONAL, 'MySQL password. Without argument, reads password from STDIN.') {|s| self.password = s || :ask }
|
o.on('-p [PASS]', :OPTIONAL, 'MySQL password. Without argument, reads password from STDIN.') {|s| self.password = s || :ask }
|
||||||
o.on('-d DBNAME', :REQUIRED, 'Name of SMF database') {|s| self.database = s }
|
o.on('-d DBNAME', :REQUIRED, 'Name of SMF database') {|s| self.database = s }
|
||||||
o.on('-f PREFIX', :REQUIRED, "Table names prefix [\"#{self.prefix}\"]") {|s| self.prefix = s }
|
o.on('-f PREFIX', :REQUIRED, "Table names prefix [\"#{self.prefix}\"]") {|s| self.prefix = s }
|
||||||
|
o.on('-t TIMEZONE', :REQUIRED, 'Timezone used by SMF2 [auto-detected from PHP]') {|s| self.timezone = s }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue