-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathff2wp.sh
executable file
·120 lines (108 loc) · 3.84 KB
/
ff2wp.sh
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
#!/bin/bash
function usage {
echo -e "\n=========================\n"
echo "Usage: $0 [-w wordpress directory] [-p posts directory] [-d] [-r]"
echo ""
echo " -w: wordpress directory"
echo " -p: posts directory"
echo " -d: dry-run mode (no changes will be applied)"
echo " -r: reset (deletes all posts, tags and categories on wordpress host), ignored in dry-run mode"
echo -e "\n========================="
}
wordpress_dir=""
posts_dir=""
dryrun=false
reset=false
while getopts ":w:p:drh" opt; do
case $opt in
w)
wordpress_dir=$(realpath $OPTARG)
;;
p)
posts_dir=$(realpath $OPTARG)
;;
d)
dryrun=true
;;
r)
reset=true
;;
h)
usage
exit 1
;;
\?)
echo -e "\nInvalid option: -$OPTARG" >&2
usage
exit 1
;;
:)
echo -e "\nOption -$OPTARG requires an argument." >&2
usage
exit 1
;;
esac
done
if [ -z "$wordpress_dir" ] || [ -z "$posts_dir" ]; then
echo -e "\nMissed wordpress directory or posts directory or both."
usage
exit 1
fi
echo -e "\nabout to import ff to wp ..\n"
echo " wordpress dir : $wordpress_dir"
echo " posts dir : $posts_dir"
echo " mode dry-run : $dryrun"
echo " reset : $reset"
echo ""
read -n 1 -s -r -p "Press any key to continue (ctrl-c to abort) ..."
echo -e "\n\n"
cd $wordpress_dir
if [ "$reset" = "true" ]; then
if [ "$dryrun" = "true" ]; then
echo ">> mode dryrun.. requested reset is ignored"
else
echo "About to delete all posts, tags and categories"
wp post delete $(wp post list --format=ids) --force --defer-term-counting
wp post delete $(wp post list --post_type=page --format=ids) --force --defer-term-counting
wp post delete $(wp post list --post_status=trash --format=ids) --force --defer-term-counting
wp term list post_tag --field=term_id | xargs wp term delete post_tag
wp term list category --field=term_id | xargs wp term delete category
fi
fi
ls $posts_dir | while read postYear; do
ls $posts_dir/$postYear | while read postId; do
echo "=== import $postYear : $postId"
grep 'post_categories ' $posts_dir/$postYear/$postId/prop.properties | awk -F '= ' '{print $2}' | sed "s/[[:space:]]//g" | sed 's/,/\n/g' | while read post_categ; do
echo "try to create category: $post_categ"
wp term create category "$post_categ" 2>/dev/null
done
post_author=1
post_date="$(grep 'post_date ' $posts_dir/$postYear/$postId/prop.properties | awk -F '= ' '{print $2}' )"
post_title="$(grep 'post_title ' $posts_dir/$postYear/$postId/prop.properties | awk -F '= ' '{print $2}' )"
post_status="$(grep 'post_status ' $posts_dir/$postYear/$postId/prop.properties | awk -F '= ' '{print $2}' )"
post_type="$(grep 'post_type ' $posts_dir/$postYear/$postId/prop.properties | awk -F '= ' '{print $2}' )"
tags_input=$(grep 'post_tags ' $posts_dir/$postYear/$postId/prop.properties | awk -F '= ' '{print $2}' | sed "s/[[:space:]]//g")
post_category=$(grep 'post_categories ' $posts_dir/$postYear/$postId/prop.properties | awk -F '= ' '{print $2}' | sed "s/[[:space:]]//g" )
echo "about to create post: $post_title ($post_status)"
echo " --post_date=\"$post_date\""
echo " --post_title=\"$post_title\""
echo " --post_status=\"$post_status\""
echo " --post_type=\"$post_type\""
echo " --tags_input=$tags_input"
echo " --post_category=$post_category"
echo " --post_author=$post_author"
if [ "$dryrun" = "true" ]; then
echo ">> mode dryrun.. no updates will take effect"
else
wp post create \
--post_date="$post_date" \
--post_title="$post_title" \
--post_status="$post_status" \
--post_type="$post_type" \
--tags_input=$tags_input \
--post_category=$post_category \
--post_author=$post_author \
$posts_dir/$postYear/$postId/content.md
fi
done
done