#!/bin/bash set -e set -o pipefail env_to_json() { while read kv do set -- "${kv%%=*}" "${kv#*=}" jq -cn '{ ($k): $v }' --arg k "$1" --arg v "$2" done } wp_config_export_assigns() { set -- "$wp_config" wp_config_get_assigns | env_to_json | jq -s '{filename: $fn, assigns: .}' --arg fn "$1" } wp_config_export_source() { set -- "$wp_config" jq -n -s '{filename: $fn, data: $fdata}' --arg fn "$1" --rawfile fdata "$1" } wp_config_export_defines() { set -- "$wp_config" wp_config_get_defines | env_to_json | jq -s '{filename: $fn, defines: .}' --arg fn "$1" } wp_config_export_all() { ( wp_config_export_defines "$@" && wp_config_export_assigns "$@" && wp_config_export_source "$@" ) | jq -s add } wp_config_get_string_defines() { sed -n -e 's/\r//g' \ -e "s/^define *( *'\([^']*\)' *, *'\([^']*\)' *) *;.*$/\1=\2/p" \ < "$wp_config" } wp_config_get_raw_defines() { sed -n -e 's/\r//g' \ -e "s/^define *( *'\([^']*\)' *, *\([A-Z][A-Z_ |]*\) *) *;.*$/\1=\2/p" \ < "$wp_config" } wp_config_get_defines() { sed -n -e 's/\r//g' \ -e "s/^define *( *'\([^']*\)' *, *'\([^']*\)' *) *;.*$/\1=\2/p" \ -e "s/^define *( *'\([^']*\)' *, *\([A-Z][A-Z_ |]*\) *) *;.*$/~raw~\1=\2/p" \ < "$wp_config" } wp_config_get_assigns() { sed -E -e 's/\r//g' -ne \ "s/^[$]([a-zA-Z_][a-zA-Z_0-9]*) *= *'([^']*)' *; *$/\\1=\\2/p" \ < "$wp_config" } find_wp_config() { # If input contains a slash then it is an absolute path. # Otherwise it is a subdirectory or subvolume of . case "$1" in */*) ;; *) set -- /srv/"$1" ;; esac for wp_config in \ "$1" \ "$1"/public_html/wp-config.php \ "$1"/public_html/wordpress/wp-config.php do if [ -f "$wp_config" ] then return fi done false } new_interface_to_old_interface() { jq ' { defines: .defines | add , assigns: .assigns | add } | { db_host: .defines.DB_HOST , db_user: .defines.DB_USER , db_password: .defines.DB_PASSWORD , db_name: .defines.DB_NAME , table_prefix: .assigns.table_prefix }' } find_wp_conf() { local htdir="$1" conf for conf in \ "$htdir"/wp-config.php \ "$htdir"/wordpress/wp-config.php do if [ -f "$conf" ] then printf '%s\n' "$conf" return fi done false } find_all_wordpress_sites() { local htdir for htdir in /srv/*/public_html do find_wp_conf "$htdir" || continue done } if [ $# = 0 ] then find_all_wordpress_sites exit elif [ "$*" = '--all' ] then "$0" | while read wp_file do "$0" "$wp_file" || exit 7 done elif [ $# != 1 ] then exit -1 fi if ! find_wp_config "$1" then echo "Error: no wp config found. Argument: ${1@Q}" >&2 exit 4 fi if [ "$NEW_INTERFACE" ] then wp_config_export_all else wp_config_export_all | new_interface_to_old_interface fi