从emlog转到WordPress
07 / 10 / 2010 admin

        昨天晚上赶到4点钟,终于把emlog转换到WordPress。之前也在网上搜索了从emlog到WordPress的转换方法,貌似都不怎么好,就自己手动写了段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
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
<?
$conn = mysql_connect('localhost', 'root', '123');
/**
 * 获取emlog文章列表,每次更新20条
 * @param unknown_type $conn
 * @return unknown
 */
function getEmlog($conn){
	mysql_query("SET NAMES 'utf8'");
	mysql_select_db('quou', $conn);
	$sql='select * from `em_blog` order by `gid` asc limit 0,20';
	//$sql='select * from `em_blog` where `gid`=199';
	$query = mysql_query($sql);
	$emlog = array();
	while( $row=mysql_fetch_assoc($query)){
		array_push($emlog, $row);
	}
	return $emlog;
}
 
/**
 * 博客文章转换函数
 * @param unknown_type $emlog
 * @param unknown_type $conn
 */
function insert($emlog, $conn){
	mysql_select_db('wordpress', $conn);
	foreach ($emlog as $v) {
		$id=$v['gid'];
		$date=date('Y-m-d H:i:s', $v['date']);
 
		$post_title=$v['title'];
		$content=str_replace(array('[break]', "'"), array('&lt;!--more--&gt;', '"'), $v['content']);
		$post_name = tran($v['title']);
		$excerpt=$v['excerpt'];
		$comment_status='open';
		$ping_status ='open';
		$comnum=$v['comnum'];
		$guid='http://www.quou.cn/?p='.$id;
 
		$defaults = array('ID'=>$id, 'post_author' => 1, 'post_date'=>$date, 'post_date_gmt'=>$date, 'post_content' => $content,
				'post_title' => $post_title, 'post_excerpt' => $excerpt, 'post_status' => 'publish', 'comment_status'=>'open', 
				'ping_status' => 'open', 'post_password' => '', 'post_name'=>$post_name, 'to_ping' =>  '', 'pinged' => '', 
				'post_modified'=>$date, 'post_modified_gmt'=>$date, 'post_content_filtered' => '', 'post_parent' => $v['sortid'], 
				'guid' => '', 'menu_order' => 0, 'post_type' => 'post', 'comment_count'=>$comnum
				);
		$fields=array_keys($defaults);
		$formatted_fields=array_values($defaults);
		$sql = "INSERT INTO `wp_posts` (`" . implode( '`,`', $fields ) . "`) VALUES ('" . implode( "','", $formatted_fields ) . "')";
		$flag=false;
		$flag=mysql_query($sql);
		if($flag){
			echo 'insert scuess for id='.$id.'<br />';
		}else{
			echo '<b style="color:#FF0000">insert fill for id='.$id.'</b><br />';
		}
	}
}
 
/**
 * 生成post_name函数,采用google翻译
 * @param unknown_type $a
 * @return unknown
 */
function tran($a){
	$url = "http://translate.google.com.hk/translate_a/t?client=t&text={$a}&sl=zh-CN&tl=en&pc=0";
    $cl = curl_init( $url);
    curl_setopt( $cl , CURLOPT_RETURNTRANSFER,true);
    curl_setopt( $cl, CURLOPT_USERAGENT , 'Mozilla/5.0');
    $res = curl_exec($cl);
    curl_close( $cl);
    $res=explode('"', $res);
    $res=isset($res[1]) ? $res[1] : $a;
    return str_replace(array('\\','"',"'"), array('','',''), $res);
}
 
/**
 * 更新文章分类代码
 * @param unknown_type $emlog
 * @param unknown_type $conn
 */
function updateSort($emlog, $conn){
	mysql_select_db('wordpress', $conn);
	foreach ($emlog as $v){
		$id=$v['gid'];
		$sid=$v['sortid'];
		$sid = $sid==2 ? 4 : $sid;
		$sql="insert into `wp_term_relationships` values ('".implode( "','",array($id, $sid, 0))."')";
		$flag=false;
		$flag=mysql_query($sql);
		if($flag){
			echo 'insert scuess for id='.$id.'<br />';
		}else{
			echo '<b style="color:#FF0000">insert fill for id='.$id.'</b><br />';
		}
	}
 
}
$emlog=getEmlog($conn);
insert($emlog, $conn);
//updateSort($emlog, $conn);
//建议不要同时更新
?>

        因为需要翻译标题缘故,为了不出现超时,建议每次20条更新一次,修改select的limite就可以了。如第一次limit 0,20,第二次就改为limit 20,20。不懂sql的limit用法我就解释一下,第一个参数是偏移量,第二个是条数。

        除此,WordPress默认会把两个</br>转换为一个<p>的解决方法是:打开并编辑 wp-includes/formatting.php 文件,找到以下代码注释掉即可:

1
2
$pee = preg_replace('|<br />\s*<br />|', "\n\n", $pee);
//$pee = preg_replace('|<br />\s*<br />|', "\n\n", $pee);

        最后说一下,为了方便以后插入html,php,python,css,js等代码,对编辑器稍作改变,添加相关按钮,这样不用每次手敲了,呵呵:

View Code JAVASCRIPT
1
2
3
4
5
edButtons[edButtons.length]=new edButton("ed_pre_html","html","<0pre lang='html' colla='+'>\n","\n</0pre>","p",-1);
edButtons[edButtons.length]=new edButton("ed_pre_php","php","<0pre lang='php' colla='+'>\n","\n</0pre>","pp",-1);
edButtons[edButtons.length]=new edButton("ed_pre_python","python","<0pre lang='python' colla='+'>\n","\n</0pre>","ppp",-1);
edButtons[edButtons.length]=new edButton("ed_pre_css","css","<0pre lang='css' colla='+'>\n","\n</0pre>","pppp",-1);
edButtons[edButtons.length]=new edButton("ed_pre_js","js","<0pre lang='JavaScript' colla='+'>\n","\n</0pre>","ppppp",-1);