-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwp_import.php
74 lines (52 loc) · 1.93 KB
/
wp_import.php
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
<?php
require('HTML_To_Markdown.php');
$config['db'] = 'robotys';
$config['user'] = 'root';
$config['pass'] = 'root';
$config['host'] = '127.0.0.1';
$config['prefix'] = 'wp';
$config['old_wp_url'] = array('http://robotys.net','http://localhost/robotys');
// $config['old_wp_content_dir'] = 'http://robotys.net/wp-content/uploads';
// connect to database
$conn = new mysqli($config['host'], $config['user'], $config['pass'], $config['db']);
// $conn = new mysqli('127.0.0.1', 'root', 'root', 'robotys');
// check connection
if ($conn->connect_error) {
dumper('Database connection failed: ' . $conn->connect_error, E_USER_ERROR);
}
// dumper($conn);
$sql='SELECT post_title, post_name, post_date_gmt, post_status, post_content FROM `'.$config['prefix'].'_posts` WHERE post_status = "publish" AND post_content != ""';
$rs=$conn->query($sql);
if($rs === false) {
trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $conn->error, E_USER_ERROR);
} else {
$rows_returned = $rs->num_rows;
// dumper($rs->fetch_assoc());
while($row = $rs->fetch_assoc()){
// process the posts
$date = date('Y-m-d', strtotime($row['post_date_gmt']));
$filename = $row['post_name'].'.md';
if($row['post_status'] == 'publish') $filename = $date.'-'.$filename;
// convert content to md
$temp = new HTML_To_Markdown($row['post_content']);
$md = $temp->output();
// change url and assets directory
foreach($config['old_wp_url'] as $arr){
// change upload
$md = str_replace($arr.'/wp-content/uploads', 'images', $md);
$md = str_replace($arr, '', $md);
}
// insert title in content;
$md = "#".$row['post_title']."\n\n".$md;
// create post in posts directory
file_put_contents('posts/'.$filename, $md);
}
}
echo 'Done! Go <a href="gen.php">generate the index now »</a>';
//// =========================
function dumper($multi){
echo '<pre>';
var_dump($multi);
echo '</pre>';
}
?>