Skip to content

Instantly share code, notes, and snippets.

@choonkeat
Created November 26, 2009 02:05
Show Gist options
  • Save choonkeat/243182 to your computer and use it in GitHub Desktop.
Save choonkeat/243182 to your computer and use it in GitHub Desktop.
Because customizing config.php is insufficient (3rd party code get left out, config.php get overwritten on update, ...) http://boblet.tumblr.com/post/68095239/ee-localhost
#!/bin/env ruby
if ARGV.empty?
$stderr.puts <<-EOM
ExpressionEngine (1.x) MYSQL DUMP Localizer
1. Reads MYSQL DUMP of ExpressionEngine website from STDIN
2. Replaces ALL HOSTNAME & PATH data (config) with LOCAL SETTINGS
3. Output corrected MYSQL DYMP file.
Usage arguments: <hostname> <document_root> [url_basepath: default '/']
Examples:
ruby #{__FILE__} www.livewebsite.com /var/www / < mysqldump.foreign.sql > mysqldump.local.sql
ruby #{__FILE__} localhost:8888 /var/www /livewebsite/ < mysqldump.foreign.sql > mysqldump.local.sql
EOM
exit(1)
end
# 1. sanitize arguments
hostname, documentroot, urlbasepath = ARGV
hostname = hostname.to_s.gsub(/^\/+|\/+$/, '') # no trailing slash
documentroot = documentroot.to_s.gsub(/^\/+|\/+$/, '') # no slashes before and after
urlbasepath = "#{urlbasepath.to_s}/".gsub(/\/\/+/, '/') # with trailing slash
# 2. configure all possible configs based on your different environments
map = {
"/livewebsite/" => urlbasepath,
"localhost:8888" => hostname,
"localhost:8888/livewebsite" => hostname,
"var/www" => documentroot,
}
# 3. removing circular reference
map.delete hostname
map.delete documentroot
map.delete urlbasepath
# 4. these key-values are sorted so that the lengthier keys are matched first
map = map.sort {|(k1,v1),(k2,v2)| k2 <=> k1 }
map.each do |key,val|
$stderr.puts "Will replace #{key} -> #{val}"
end
# 5. wait for regrets
sleep 5
begin
while line = STDIN.readline
line.chomp!
map.each do |key,val|
# 6. First, we try to look for PHP#serialize'd strings and replace those values (with corrected string length header)
r = Regexp.new(/(s:(\d+):(\\"(((?!\;s\:).)*?#{Regexp.escape(key)}((?!\;s\:).)*?)\\";))/)
$stderr.puts r if line.match(r)
while line.match(r)
match = $1
newval = $4.gsub(key, val)
$stderr.puts "\t" + match
replace = ('s:' + eval('"' + newval + '"').length.to_s + ':\"' + newval + '\";')
line.gsub!(match, replace)
$stderr.puts "\t" + replace
$stderr.puts
end
# 7. Next, we replace any other occurrences which are not PHP#serialize'd
line.gsub!(key, val)
end
puts line
end
rescue EOFError
# file has ended. it is ok
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment