aboutsummaryrefslogtreecommitdiff
path: root/locopy
diff options
context:
space:
mode:
authorEugen Wissner <belka@caraus.de>2025-11-14 20:30:32 +0100
committerEugen Wissner <belka@caraus.de>2025-11-14 20:30:32 +0100
commit964d815a90a78c0ebbfc2fa57f8b847c1fab298d (patch)
treedf45816448f805414208f2e8bf4f4e160e104681 /locopy
parentcfa3d93290afa369d80f7a8921c32ad9570396b0 (diff)
downloadkazbek-964d815a90a78c0ebbfc2fa57f8b847c1fab298d.tar.gz
Add locopy
Diffstat (limited to 'locopy')
-rw-r--r--locopy/Gemfile8
-rw-r--r--locopy/Gemfile.lock18
-rwxr-xr-xlocopy/bin/locopy.rb102
-rw-r--r--locopy/wp-settings.php12
4 files changed, 140 insertions, 0 deletions
diff --git a/locopy/Gemfile b/locopy/Gemfile
new file mode 100644
index 0000000..3cfb1a0
--- /dev/null
+++ b/locopy/Gemfile
@@ -0,0 +1,8 @@
+# frozen_string_literal: true
+
+source 'https://rubygems.org'
+
+git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
+
+gem 'mysql2', '~> 0.5'
+gem 'optparse', '~> 0.8.0'
diff --git a/locopy/Gemfile.lock b/locopy/Gemfile.lock
new file mode 100644
index 0000000..0393bfd
--- /dev/null
+++ b/locopy/Gemfile.lock
@@ -0,0 +1,18 @@
+GEM
+ remote: https://rubygems.org/
+ specs:
+ bigdecimal (3.3.1)
+ mysql2 (0.5.7)
+ bigdecimal
+ optparse (0.8.0)
+
+PLATFORMS
+ arm64-darwin-24
+ x86_64-linux
+
+DEPENDENCIES
+ mysql2 (~> 0.5)
+ optparse (~> 0.8.0)
+
+BUNDLED WITH
+ 2.6.9
diff --git a/locopy/bin/locopy.rb b/locopy/bin/locopy.rb
new file mode 100755
index 0000000..933789b
--- /dev/null
+++ b/locopy/bin/locopy.rb
@@ -0,0 +1,102 @@
+#!/usr/bin/env ruby
+
+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
diff --git a/locopy/wp-settings.php b/locopy/wp-settings.php
new file mode 100644
index 0000000..42aec0e
--- /dev/null
+++ b/locopy/wp-settings.php
@@ -0,0 +1,12 @@
+<?php
+define('ABSPATH', __DIR__ . '/');
+
+require_once 'wp-config.php';
+
+echo json_encode([
+ 'DB_NAME' => DB_NAME,
+ 'DB_USER' => DB_USER,
+ 'DB_PASSWORD' => DB_PASSWORD,
+ 'DB_HOST' => DB_HOST,
+ 'table_prefix' => $table_prefix
+]);