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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
#!/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 <file:///srv>.
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
|