summaryrefslogtreecommitdiff
path: root/wordpress-export
blob: 9aadff6361517e04bedc40577cb13c34c7f70289 (plain)
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
#!/bin/bash
# convert wordpress posts to markdown files (without comments)

db="wordpress"
echo "MySQL password:"
stty -echo
read dbpwd
stty echo

posts(){
    mysql -p$dbpwd -D$db -s -e "SELECT ID from wp_posts WHERE post_status = 'publish' AND post_type = 'post';"
}
selpost() {
    mysql -p$dbpwd -D$db -s -e "SELECT $1 FROM wp_posts WHERE ID = $2;"
}
lastrev() {
    mysql -p$dbpwd -D$db -s -e "SELECT MAX(ID) FROM wp_posts WHERE post_parent = $1;"
}

if [ -d archives ]
then
    rm -rf archives
fi
mkdir archives
cd ./archives

posts > ../posts
while read line
do
    date=`selpost post_date $line`
    year=`echo $date | awk -F'-' '{print $1}'`
    if [ ! -d $year ]
    then
        mkdir $year
    fi
    month=`echo $date | awk -F'-' '{print $2}'`
    if [ ! -d $year/$month ]
    then
        mkdir $year/$month
    fi
    day=`echo $date | awk -F'-' '{print $3}' | awk '{print $1}'`
    if [ ! -d $year/$month/$day ]
    then
        mkdir $year/$month/$day
    fi
    current=`lastrev $line`
    post_title=`selpost post_title $current`
    echo $post_title > ../post
    echo -e '=========================================\n' >> ../post

    # pass the latest revision to html2text: https://github.com/aaronsw/html2text.git
    echo -e `selpost post_content $current | html2text` >> ../post

    count=`ls -1 $year/$month/$day | wc -l`
    mkdir $year/$month/$day/$count
    cat ../post > $year/$month/$day/$count/index.md
    echo $year/$month/$day/$count/index.md
    rm ../post
done < ../posts
rm ../posts