aboutsummaryrefslogtreecommitdiff
path: root/locopy/locopy.rb
diff options
context:
space:
mode:
authorEugen Wissner <belka@caraus.de>2025-11-17 16:54:55 +0100
committerEugen Wissner <belka@caraus.de>2025-11-17 16:54:55 +0100
commit5e4c38c28ecfd927a466aa128b9cb85a2e0e4ebc (patch)
tree59b4ba2f104a9c357b711bfc315e90ea41e135ec /locopy/locopy.rb
parenta3530666a60dd004fe2c28bb87fbabdf5e5bebab (diff)
downloadkazbek-5e4c38c28ecfd927a466aa128b9cb85a2e0e4ebc.tar.gz
locopy: Require all arguments
Diffstat (limited to 'locopy/locopy.rb')
-rwxr-xr-xlocopy/locopy.rb123
1 files changed, 123 insertions, 0 deletions
diff --git a/locopy/locopy.rb b/locopy/locopy.rb
new file mode 100755
index 0000000..0bd48a3
--- /dev/null
+++ b/locopy/locopy.rb
@@ -0,0 +1,123 @@
+#!/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/.
+
+# frozen_string_literal: true
+
+require 'pathname'
+require 'mysql2'
+require 'optparse'
+require 'json'
+require 'term/ansicolor'
+
+# Tool for easy updating a local copy of a website
+
+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
+
+def copy_db(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
+
+class CommandOptions
+ attr_reader :root, :dump
+
+ def []=(key, value)
+ case key
+ when :root
+ @root = Pathname.new value
+ when :dump
+ @dump = Pathname.new value
+ end
+ end
+
+ def valid?
+ !@root.nil? && !@dump.nil?
+ end
+end
+
+def wordpress
+ arguments = CommandOptions.new
+ option_parser = OptionParser.new do |options|
+ options.banner = "Usage: #{File.basename $0} [OPTIONS] (wordpress)"
+
+ options.on '--root=ROOT', 'Website configuration directory'
+ options.on '--dump=DUMP', 'Database file'
+ end
+
+ option_parser.parse!(into: arguments)
+ unless arguments.valid?
+ $stderr.puts option_parser.help
+ exit 2
+ end
+
+ wp_settings = Pathname.new('locopy/wp-settings.php').realpath
+ wp_config = nil
+
+ Dir.chdir arguments.root do
+ wp_config = JSON.parse `php #{wp_settings}`
+ end
+
+ copy_db wp_config
+end
+
+# Check for supported command.
+command = ARGV.shift
+case command
+when 'wordpress'
+ wordpress
+when nil
+ Kernel.abort Term::ANSIColor.red "No command given at the command line."
+else
+ Kernel.abort Term::ANSIColor.red %Q(Unsupported command "#{command}".)
+end