aboutsummaryrefslogtreecommitdiff
path: root/locopy/bin/locopy.rb
blob: f7262104957e6ee5a51a3d21e1b91d6f7153f557 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env ruby
# This Source Code Form is subject to the terms of the Mozilla Public License,
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
# obtain one at https://mozilla.org/MPL/2.0/.

require 'pathname'
require 'mysql2'
require 'optparse'
require 'json'

# Tool for easy updating a local copy of a website

arguments = {}
option_parser = OptionParser.new do |options|
  options.banner = "Usage: #{File.basename $0} [OPTIONS] (wordpress)"

  options.on '--root=ROOT', 'Website configuration directory' do |option|
    arguments[:root] = Pathname.new option
  end
  options.on '--dump=DUMP', 'Database file' do |option|
    arguments[:root] = Pathname.new option
  end
end

option_parser.parse!

# Read arguments
while argument = ARGV.shift do
  case argument
  when 'wordpress'
    ENV['CMD'] = argument
  end
end

class DatabaseAccess
  attr_accessor :name, :user, :password
  attr_reader :host, :socket, :port

  def initialize(wp_config)
    @name = wp_config['DB_NAME']
    @user = wp_config['DB_USER']
    @password = wp_config['DB_PASSWORD']

    self.host = wp_config['DB_HOST']
  end

  def host=(host)
    host_part, socket_part = host.split(':')
    port = socket_part.to_i

    @host = host_part
    if port == 0
      @socket = socket_part
    else
      @port = port
    end
  end
end

wp_settings = Pathname.new('wp-settings.php').realpath
wp_config = nil

Dir.chdir arguments[:root] do
  wp_config = JSON.parse `php #{wp_settings}`
end

def copy_db(root, wp_config)
  table_prefix = wp_config['table_prefix']
  php_constants = DatabaseAccess.new wp_config
  keep_option_names = ['siteurl', 'home']

  client = Mysql2::Client.new host: php_constants.host, username: php_constants.user,
    password: php_constants.password, database: php_constants.name, socket: php_constants.socket
  statement = client.prepare <<~SQL
    SELECT option_name, option_value
    FROM #{table_prefix}options
    WHERE option_name LIKE ?
  SQL
  keep_option_values = keep_option_names.each_with_object({}) do |keep_option_name, accumulator|
    accumulator[keep_option_name] = statement.execute(keep_option_name).first['option_value']
    accumulator
  end
  keep_option_values = {
    "siteurl"=>"http://localhost:8083",
    "home"=>"http://localhost:8083"
  }

  statement = client.prepare <<~SQL
    UPDATE #{table_prefix}options
    SET option_value = ?
    WHERE option_name LIKE ?
  SQL
  keep_option_values.each_pair do |name, value|
    statement.execute(value, name)
  end
end

case ENV['CMD']
when 'wordpress'
  # Set permissions
  copy_db(arguments[:root], wp_config)
else
  puts option_parser.help
  exit 1
end