-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathAction.php
executable file
·293 lines (276 loc) · 11.9 KB
/
Action.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
<?php
class TypExport_Action extends Typecho_Widget implements Widget_Interface_Do
{
/**
* 导出xml
*
* @access public
* @return void
*/
public function doExport()
{
$options = Helper::options();
// execution
$authors_list = $this->wxr_authors_list();
$cats_list = $this->wxr_cats_list();
$tags_list = $this->wxr_tags_list();
$posts_list = $this->wxr_posts_list($options);
// 备份文件名
$fileName = 'wordpress.' . date('Y-m-d') . '.xml';
//header('Content-Description: File Transfer');
header('Content-Type: text/xml');
header('Content-Disposition: attachment; filename=' . $fileName);
if (preg_match("/MSIE ([0-9].[0-9]{1,2})/", $_SERVER['HTTP_USER_AGENT'])) {
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
} else {
header('Pragma: no-cache');
header('Last-Modified: '. gmdate('D, d M Y H:i:s',
Typecho_Date::gmtTime() + (Typecho_Date::$timezoneOffset - Typecho_Date::$serverTimezoneOffset)) . ' GMT');
}
header('Expires: ' . gmdate('D, d M Y H:i:s',
Typecho_Date::gmtTime() + (Typecho_Date::$timezoneOffset - Typecho_Date::$serverTimezoneOffset)) . ' GMT');
echo '<?xml version="1.0" encoding="'.$options->charset.'" ?>' . "\n";
echo <<< EOT
<!-- This is a WordPress eXtended RSS file generated by WordPress as an export of your site. -->
<!-- It contains information about your site's posts, pages, comments, categories, and other content. -->
<!-- You may use this file to transfer that content from one site to another. -->
<!-- This file is not intended to serve as a complete backup of your site. -->
<!-- To import this information into a WordPress site follow these steps: -->
<!-- 1. Log in to that site as an administrator. -->
<!-- 2. Go to Tools: Import in the WordPress admin panel. -->
<!-- 3. Install the "WordPress" importer from the list. -->
<!-- 4. Activate & Run Importer. -->
<!-- 5. Upload this file using the form provided on that page. -->
<!-- 6. You will first be asked to map the authors in this export file to users -->
<!-- on the site. For each author, you may choose to map to an -->
<!-- existing user on the site or to create a new user. -->
<!-- 7. WordPress will then import each of the posts, pages, comments, categories, etc. -->
<!-- contained in this file into your site. -->
<rss version="2.0"
xmlns:excerpt="http://wordpress.org/export/1.2/excerpt/"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:wp="http://wordpress.org/export/1.2/"
>
<channel>
<title>$options->title</title>
<link>$options->siteUrl</link>
<description>$options->description</description>
<pubDate>Wed, 19 Nov 2014 06:15:10 +0000</pubDate>
<language>$options->lang</language>
<wp:wxr_version>1.2</wp:wxr_version>
<wp:base_site_url>$options->siteUrl</wp:base_site_url>
<wp:base_blog_url>$options->siteUrl</wp:base_blog_url>
$authors_list
$cats_list
$tags_list
<generator>http://wordpress.org/?v=4.0</generator>
$posts_list
</channel>
</rss>
EOT;
}
/**
* 绑定动作
*
* @access public
* @return void
*/
public function action()
{
$this->widget('Widget_User')->pass('administrator');
$this->on($this->request->is('export'))->doExport();
}
/**
* Output list of authors
*/
public function wxr_authors_list()
{
$db = Typecho_Db::get();
$dbPrefix = $db->getPrefix();
$user_table = $dbPrefix . 'users';
$authors = $db->fetchAll($db->query("select uid,name,mail from {$user_table} where activated != 0"));
$html = '';
foreach ( $authors as $author ) {
$html .= "\t<wp:author>";
$html .= '<wp:author_id>' . $author['uid'] . '</wp:author_id>';
$html .= '<wp:author_login>' . $author['name'] . '</wp:author_login>';
$html .= '<wp:author_email>' . $author['mail'] . '</wp:author_email>';
$html .= '<wp:author_display_name>' . $this->wxr_cdata($author['name']) . '</wp:author_display_name>';
$html .= '<wp:author_first_name>' . $this->wxr_cdata('') . '</wp:author_first_name>';
$html .= '<wp:author_last_name>' . $this->wxr_cdata('') . '</wp:author_last_name>';
$html .= "</wp:author>\n";
}
return $html;
}
public function wxr_cats_list()
{
$db = Typecho_Db::get();
$dbPrefix = $db->getPrefix();
$html = '';
$table_name = $dbPrefix . 'metas';
$sql = "select a.mid, a.name, a.slug, a.description, b.name parent
from {$table_name} a
left join {$table_name} b
on a.mid = b.parent
where a.type = 'category'";
$rows = $db->fetchAll($db->query($sql));
foreach ($rows as $row) {
$html .= "<wp:category><wp:term_id>{$row['mid']}</wp:term_id><wp:category_nicename>{$row['slug']}</wp:category_nicename><wp:category_parent>{$row['parent']}</wp:category_parent><wp:cat_name><![CDATA[{$row['name']}]]></wp:cat_name></wp:category>\n";
}
return $html;
}
public function wxr_tags_list()
{
$db = Typecho_Db::get();
$dbPrefix = $db->getPrefix();
$html = '';
$table_name = $dbPrefix . 'metas';
$sql = "select mid, name, slug, description
from {$table_name}
where type = 'tag'";
$rows = $db->fetchAll($db->query($sql));
foreach ($rows as $row) {
$html .= "<wp:tag><wp:term_id>{$row['mid']}</wp:term_id><wp:tag_slug>{$row['slug']}</wp:tag_slug><wp:tag_name><![CDATA[{$row['name']}]]></wp:tag_name></wp:tag>\n";
}
return $html;
}
public function wxr_posts_list($options)
{
$db = Typecho_Db::get();
$dbPrefix = $db->getPrefix();
$html = '';
$post_table = $dbPrefix . 'contents';
$relation_table = $dbPrefix . 'relationships';
$meta_table = $dbPrefix . 'metas';
$user_table = $dbPrefix . 'users';
$sql = "select p.*,
group_concat(concat(m.name, '@', m.slug, '@', m.type) separator '|') terms,
u.name author
from {$post_table} p
left join {$relation_table} r
on p.cid = r.cid
left join {$meta_table} m
on r.mid = m.mid
left join {$user_table} u
on p.authorId = u.uid
group by p.cid";
$rows = $db->fetchAll($db->query($sql));
foreach ($rows as $row) {
$pub_date = gmdate(DATE_RFC2822, $row['created']);
$post_date = date('Y-m-d H:i:s', $row['created']);
$tag_str = '';
$cat_str = '';
$post_meta = '';
$comment_str = $this->get_comments($row['cid']);
if ($row['type'] == 'page') {
$post_meta = "<wp:postmeta>
<wp:meta_key>_wp_page_template</wp:meta_key>
<wp:meta_value><![CDATA[default]]></wp:meta_value>
</wp:postmeta>";
} else {
if ($row['terms']) {
$terms_arr = explode('|', $row['terms']);
foreach ($terms_arr as $term) {
$temp = explode('@', $term);
$t_name = $temp[0];
$t_slug = $temp[1];
$t_type = $temp[2];
if ($t_type == 'tag') {
$tag_str .= "<category domain=\"post_tag\" nicename=\"{$t_slug}\"><![CDATA[{$t_name}]]></category>\n";
} else if ($t_type == 'category') {
$cat_str .= "<category domain=\"category\" nicename=\"{$t_slug}\"><![CDATA[{$t_name}]]></category>\n";
} else {}
}
}
}
// 处理markdown,判断是否是markdown
$version_string = Typecho_Common::VERSION;
$args = explode('/', $version_string);
$version = floatval($args[0]);
if (substr($row['text'], 0, 15) == '<!--markdown-->' && $version >= 1.0) {
$content = Markdown::convert(strip_tags($row['text']));
} else {
$content = $row['text'];
}
$html .= "
<item>
<title>{$row['title']}</title>
<link>{$options->siteUrl}?p={$row['cid']}</link>
<pubDate>{$pub_date}</pubDate>
<dc:creator><![CDATA[{$row['author']}]]></dc:creator>
<guid isPermaLink=\"false\">{$options->siteUrl}?p={$row['cid']}</guid>
<description></description>
<content:encoded><![CDATA[{$content}]]></content:encoded>
<excerpt:encoded><![CDATA[]]></excerpt:encoded>
<wp:post_id>={$row['cid']}</wp:post_id>
<wp:post_date>{$post_date}</wp:post_date>
<wp:post_date_gmt>{$post_date}</wp:post_date_gmt>
<wp:comment_status>open</wp:comment_status>
<wp:ping_status>open</wp:ping_status>
<wp:post_name>{$row['slug']}</wp:post_name>
<wp:status>{$row['status']}</wp:status>
<wp:post_parent>{$row['parent']}</wp:post_parent>
<wp:menu_order>0</wp:menu_order>
<wp:post_type>{$row['type']}</wp:post_type>
<wp:post_password>{$row['password']}</wp:post_password>
<wp:is_sticky>0</wp:is_sticky>
{$post_meta}
{$cat_str}
{$tag_str}
{$comment_str}
</item>";
}
return $html;
}
public function get_comments($cid)
{
$html = '';
$db = Typecho_Db::get();
$dbPrefix = $db->getPrefix();
$comment_table = $dbPrefix . 'comments';
$sql = "select * from {$comment_table} where cid = {$cid}";
$rows = $db->fetchAll($db->query($sql));
if (count($rows) > 0) {
foreach ($rows as $c) {
$co_date = date('Y-m-d H:i:s', $c['created']);
switch($c['status']) {
case 'approved':
$status = 1;
break;
case 'spam':
$status = 'spam';
break;
case 'waiting':
$status = 0;
break;
case 'hidden':
$status = 0;
break;
}
$html .= "
<wp:comment>
<wp:comment_id>{$c['coid']}</wp:comment_id>
<wp:comment_author><![CDATA[{$c['author']}]]></wp:comment_author>
<wp:comment_author_email>{$c['mail']}</wp:comment_author_email>
<wp:comment_author_url>{$c['url']}</wp:comment_author_url>
<wp:comment_author_IP>{$c['ip']}</wp:comment_author_IP>
<wp:comment_date>{$co_date}</wp:comment_date>
<wp:comment_date_gmt>{$co_date}</wp:comment_date_gmt>
<wp:comment_content><![CDATA[{$c['text']}]]></wp:comment_content>
<wp:comment_approved>{$status}</wp:comment_approved>
<wp:comment_type>{$c['type']}</wp:comment_type>
<wp:comment_parent>{$c['parent']}</wp:comment_parent>
<wp:comment_user_id>{$c['authorId']}</wp:comment_user_id>
</wp:comment>";
}
}
return $html;
}
public function wxr_cdata( $str ) {
$str = '<![CDATA[' . str_replace( ']]>', ']]]]><![CDATA[>', $str ) . ']]>';
return $str;
}
}