├── .gitignore ├── README.md ├── LICENSE ├── Plugin.php ├── panel.php └── Action.php /.gitignore: -------------------------------------------------------------------------------- 1 | .idea -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | TypExport 2 | ========= 3 | 4 | Typecho导出WXR插件 5 | 6 | WXR全称为WordPress eXtended Rss,是wordpress导出数据的一种格式,它包含了您的全部文章、页面、评论、自定义字段、分类目录和标签。 7 | 8 | #### 简介 9 | 10 | 测试平台:typecho 1.0 & wordpress 4.0/4.1 11 | 12 | #### 如何使用 13 | 14 | 点击右侧的`Download ZIP`按钮,下载完成之后解压得到类似`TypExport-master`文件夹,将文件夹重命名为`TypExport`,上传到Typecho目录`usr/plugins`,然后在后台启用插件。 15 | 16 | 在后台界面,`控制台`菜单下会有一个`数据导出`菜单,点击进入导出界面,只有一个按钮,我相信你肯定会使用的。 17 | 18 | #### 更新纪录 19 | 20 | ##### 2015-01-06 21 | 22 | * 修正typecho 0.9 markdown类报错的问题 23 | * typecho 1.0 文章内容可能不是markdown格式的处理 24 | 25 | #### 问题反馈 26 | 27 | 在[这里](https://github.com/panxianhai/TypExport/issues)提出使用中的问题,我会在时间允许的第一时间进行处理。 28 | 29 | 另外,欢迎fork & star。 30 | 31 | #### LICENSE 32 | 33 | [MIT LICENSE](https://github.com/panxianhai/TypExport/blob/master/LICENSE) 34 | 35 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 panxianhai 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Plugin.php: -------------------------------------------------------------------------------- 1 | $current_version) { 7 | $message = "此插件有新版本({$version}),当前版本({$current_version}),请到这里下载更新"; 8 | } 9 | 10 | include_once 'common.php'; 11 | include 'header.php'; 12 | include 'menu.php'; 13 | ?> 14 | 15 |
16 |
17 |
18 |

19 |
20 |
21 |
22 |

在您点击下面的按钮后,Typecho会创建一个XML文件,供您保存到计算机中。

23 |

我们称这种格式为WordPress eXtended RSS或WXR,它包含了您的全部文章、页面、评论、分类目录和标签。

24 |

使用过程中如果有问题,请到 Github 或者 博客 提出。

26 |

27 |
28 |
    29 |
  • 30 | 31 |
  • 32 |
33 |
34 |
35 |
36 |
37 |
38 | 39 | 45 | -------------------------------------------------------------------------------- /Action.php: -------------------------------------------------------------------------------- 1 | wxr_authors_list(); 16 | $cats_list = $this->wxr_cats_list(); 17 | $tags_list = $this->wxr_tags_list(); 18 | $posts_list = $this->wxr_posts_list($options); 19 | 20 | // 备份文件名 21 | $fileName = 'wordpress.' . date('Y-m-d') . '.xml'; 22 | 23 | //header('Content-Description: File Transfer'); 24 | header('Content-Type: text/xml'); 25 | header('Content-Disposition: attachment; filename=' . $fileName); 26 | if (preg_match("/MSIE ([0-9].[0-9]{1,2})/", $_SERVER['HTTP_USER_AGENT'])) { 27 | header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 28 | header('Pragma: public'); 29 | } else { 30 | header('Pragma: no-cache'); 31 | header('Last-Modified: '. gmdate('D, d M Y H:i:s', 32 | Typecho_Date::gmtTime() + (Typecho_Date::$timezoneOffset - Typecho_Date::$serverTimezoneOffset)) . ' GMT'); 33 | } 34 | header('Expires: ' . gmdate('D, d M Y H:i:s', 35 | Typecho_Date::gmtTime() + (Typecho_Date::$timezoneOffset - Typecho_Date::$serverTimezoneOffset)) . ' GMT'); 36 | 37 | echo 'charset.'" ?>' . "\n"; 38 | echo <<< EOT 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 62 | 63 | $options->title 64 | $options->siteUrl 65 | $options->description 66 | Wed, 19 Nov 2014 06:15:10 +0000 67 | $options->lang 68 | 1.2 69 | $options->siteUrl 70 | $options->siteUrl 71 | $authors_list 72 | $cats_list 73 | $tags_list 74 | http://wordpress.org/?v=4.0 75 | $posts_list 76 | 77 | 78 | EOT; 79 | } 80 | 81 | /** 82 | * 绑定动作 83 | * 84 | * @access public 85 | * @return void 86 | */ 87 | public function action() 88 | { 89 | $this->widget('Widget_User')->pass('administrator'); 90 | $this->on($this->request->is('export'))->doExport(); 91 | } 92 | 93 | /** 94 | * Output list of authors 95 | */ 96 | public function wxr_authors_list() 97 | { 98 | $db = Typecho_Db::get(); 99 | $dbPrefix = $db->getPrefix(); 100 | 101 | $user_table = $dbPrefix . 'users'; 102 | $authors = $db->fetchAll($db->query("select uid,name,mail from {$user_table} where activated != 0")); 103 | $html = ''; 104 | foreach ( $authors as $author ) { 105 | $html .= "\t"; 106 | $html .= '' . $author['uid'] . ''; 107 | $html .= '' . $author['name'] . ''; 108 | $html .= '' . $author['mail'] . ''; 109 | $html .= '' . $this->wxr_cdata($author['name']) . ''; 110 | $html .= '' . $this->wxr_cdata('') . ''; 111 | $html .= '' . $this->wxr_cdata('') . ''; 112 | $html .= "\n"; 113 | } 114 | return $html; 115 | } 116 | 117 | public function wxr_cats_list() 118 | { 119 | $db = Typecho_Db::get(); 120 | $dbPrefix = $db->getPrefix(); 121 | $html = ''; 122 | $table_name = $dbPrefix . 'metas'; 123 | $sql = "select a.mid, a.name, a.slug, a.description, b.name parent 124 | from {$table_name} a 125 | left join {$table_name} b 126 | on a.mid = b.parent 127 | where a.type = 'category'"; 128 | $rows = $db->fetchAll($db->query($sql)); 129 | foreach ($rows as $row) { 130 | $html .= "{$row['mid']}{$row['slug']}{$row['parent']}\n"; 131 | } 132 | return $html; 133 | } 134 | 135 | public function wxr_tags_list() 136 | { 137 | $db = Typecho_Db::get(); 138 | $dbPrefix = $db->getPrefix(); 139 | $html = ''; 140 | $table_name = $dbPrefix . 'metas'; 141 | $sql = "select mid, name, slug, description 142 | from {$table_name} 143 | where type = 'tag'"; 144 | $rows = $db->fetchAll($db->query($sql)); 145 | foreach ($rows as $row) { 146 | $html .= "{$row['mid']}{$row['slug']}\n"; 147 | } 148 | return $html; 149 | } 150 | 151 | public function wxr_posts_list($options) 152 | { 153 | $db = Typecho_Db::get(); 154 | $dbPrefix = $db->getPrefix(); 155 | $html = ''; 156 | $post_table = $dbPrefix . 'contents'; 157 | $relation_table = $dbPrefix . 'relationships'; 158 | $meta_table = $dbPrefix . 'metas'; 159 | $user_table = $dbPrefix . 'users'; 160 | 161 | $sql = "select p.*, 162 | group_concat(concat(m.name, '@', m.slug, '@', m.type) separator '|') terms, 163 | u.name author 164 | from {$post_table} p 165 | left join {$relation_table} r 166 | on p.cid = r.cid 167 | left join {$meta_table} m 168 | on r.mid = m.mid 169 | left join {$user_table} u 170 | on p.authorId = u.uid 171 | group by p.cid"; 172 | $rows = $db->fetchAll($db->query($sql)); 173 | foreach ($rows as $row) { 174 | $pub_date = gmdate(DATE_RFC2822, $row['created']); 175 | $post_date = date('Y-m-d H:i:s', $row['created']); 176 | $tag_str = ''; 177 | $cat_str = ''; 178 | $post_meta = ''; 179 | $comment_str = $this->get_comments($row['cid']); 180 | if ($row['type'] == 'page') { 181 | $post_meta = " 182 | _wp_page_template 183 | 184 | "; 185 | } else { 186 | if ($row['terms']) { 187 | $terms_arr = explode('|', $row['terms']); 188 | foreach ($terms_arr as $term) { 189 | $temp = explode('@', $term); 190 | $t_name = $temp[0]; 191 | $t_slug = $temp[1]; 192 | $t_type = $temp[2]; 193 | if ($t_type == 'tag') { 194 | $tag_str .= "\n"; 195 | } else if ($t_type == 'category') { 196 | $cat_str .= "\n"; 197 | } else {} 198 | } 199 | } 200 | } 201 | // 处理markdown,判断是否是markdown 202 | $version_string = Typecho_Common::VERSION; 203 | $args = explode('/', $version_string); 204 | $version = floatval($args[0]); 205 | if (substr($row['text'], 0, 15) == '' && $version >= 1.0) { 206 | $content = Markdown::convert(strip_tags($row['text'])); 207 | } else { 208 | $content = $row['text']; 209 | } 210 | $html .= " 211 | 212 | {$row['title']} 213 | {$options->siteUrl}?p={$row['cid']} 214 | {$pub_date} 215 | 216 | {$options->siteUrl}?p={$row['cid']} 217 | 218 | 219 | 220 | ={$row['cid']} 221 | {$post_date} 222 | {$post_date} 223 | open 224 | open 225 | {$row['slug']} 226 | {$row['status']} 227 | {$row['parent']} 228 | 0 229 | {$row['type']} 230 | {$row['password']} 231 | 0 232 | {$post_meta} 233 | {$cat_str} 234 | {$tag_str} 235 | {$comment_str} 236 | "; 237 | } 238 | return $html; 239 | } 240 | 241 | public function get_comments($cid) 242 | { 243 | $html = ''; 244 | $db = Typecho_Db::get(); 245 | $dbPrefix = $db->getPrefix(); 246 | $comment_table = $dbPrefix . 'comments'; 247 | 248 | $sql = "select * from {$comment_table} where cid = {$cid}"; 249 | $rows = $db->fetchAll($db->query($sql)); 250 | if (count($rows) > 0) { 251 | foreach ($rows as $c) { 252 | 253 | $co_date = date('Y-m-d H:i:s', $c['created']); 254 | 255 | switch($c['status']) { 256 | case 'approved': 257 | $status = 1; 258 | break; 259 | case 'spam': 260 | $status = 'spam'; 261 | break; 262 | case 'waiting': 263 | $status = 0; 264 | break; 265 | case 'hidden': 266 | $status = 0; 267 | break; 268 | } 269 | $html .= " 270 | 271 | {$c['coid']} 272 | 273 | {$c['mail']} 274 | {$c['url']} 275 | {$c['ip']} 276 | {$co_date} 277 | {$co_date} 278 | 279 | {$status} 280 | {$c['type']} 281 | {$c['parent']} 282 | {$c['authorId']} 283 | "; 284 | } 285 | } 286 | return $html; 287 | } 288 | 289 | public function wxr_cdata( $str ) { 290 | $str = '', ']]]]>', $str ) . ']]>'; 291 | return $str; 292 | } 293 | } --------------------------------------------------------------------------------