-
Notifications
You must be signed in to change notification settings - Fork 9
/
textpattern-to-wordpress.php
286 lines (215 loc) · 8.65 KB
/
textpattern-to-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
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
<?php
// Include your TXP config file:
include('textpattern/config.php');
// Do you want to export HTML (for WordPress, default true) or raw Textile formatting (false)?
$export_html = true;
/* ==============================================================================================================
SUPER-CRUDE TEXTPATTERN TO WORDPRESS RSS EXPORTER
Connects your TXP database tables and creates a WP-style extended RSS file. This can then be imported into
WordPress, or something else that supports WordPress format files.
Put this file in your Textpattern site, at the same level as the textpattern folder (usually root), then
load it up in your web browser.
This isn't pretty.
================================================================================================================= */
$dsn = 'mysql:dbname='.$txpcfg['db'].';host='.(isset($txpcfg['host']) ? $txpcfg['host'] : '127.0.0.1');
$opts = array();
if (isset($txpcfg['dbcharset'])) $opts = array(1002 => "SET NAMES '".$txpcfg['dbcharset']."'");
try {
$Connection = new PDO($dsn, $txpcfg['user'], $txpcfg['pass'], $opts);
} catch (PDOException $e) {
die('Connection failed: ' . $e->getMessage());
}
define('EOL', "\n");
header("Content-Type: text/xml; charset=UTF-8", true);
header("Content-Disposition: attachment; filename=\"textpattern-export.xml\"", true);
echo '<'.'?xml version="1.0" encoding="utf-8" ?'.'>
<rss version="2.0"
xmlns:excerpt="http://wordpress.org/export/1.1/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.1/"
>
<channel>';
/* ====== FRONT MATTER ======= */
$result = $Connection->query('SELECT name, val FROM '.$txpcfg['table_prefix'].'txp_prefs');
if ($result) {
$prefs = $result->fetchAll(PDO::FETCH_ASSOC);
if (is_array($prefs) && count($prefs)) {
foreach($prefs as $pref) {
switch($pref['name']) {
case 'sitename':
echo '<title>'.txpspecialchars($pref['val']).'</title>'.EOL;
break;
case 'site_slogan':
echo '<description>'.txpspecialchars($pref['val']).'</description>'.EOL;
break;
case 'siteurl':
echo '<link>http://'.$pref['val'].'</link>'.EOL;
echo '<wp:base_site_url>http://'.$pref['val'].'</wp:base_site_url>'.EOL;
echo '<wp:base_blog_url>http://'.$pref['val'].'</wp:base_blog_url>'.EOL;
$siteurl = 'http://'.$pref['val'].'/';
break;
case 'language':
echo '<language>'.$pref['val'].'</language>'.EOL;
break;
case 'lastmod':
echo '<pubDate>'.date('r', strtotime($pref['val'])).'</pubDate>'.EOL;
break;
case 'permlink_mode':
$permlink_mode = $pref['val'];
break;
}
}
}
}
echo '<wp:wxr_version>1.1</wp:wxr_version>'.EOL;
echo '<generator>https://github.com/drewm/textpattern-to-wordpress</generator>'.EOL;
/* ======= AUTHORS ======== */
$result = $Connection->query('SELECT * FROM '.$txpcfg['table_prefix'].'txp_users');
if ($result) {
$users = $result->fetchAll(PDO::FETCH_ASSOC);
if (is_array($users) && count($users)) {
foreach($users as $user) {
echo '<wp:author>'.EOL;
echo '<wp:author_id>'.$user['user_id'].'</wp:author_id>'.EOL;
echo '<wp:author_login>'.txpspecialchars($user['name']).'</wp:author_login>'.EOL;
echo '<wp:author_email>'.$user['email'].'</wp:author_email>'.EOL;
echo '<wp:author_display_name><![CDATA['.$user['RealName'].']]></wp:author_display_name>'.EOL;
$parts = explode(' ', $user['RealName'], 2);
if (isset($parts[0])) echo '<wp:author_first_name><![CDATA['.$parts[0].']]></wp:author_first_name>'.EOL;
if (isset($parts[1])) echo '<wp:author_last_name><![CDATA['.$parts[1].']]></wp:author_last_name>'.EOL;
echo '</wp:author>'.EOL;
}
}
}
/* ======= CATEGORIES ======== */
$cat_titles = array();
$result = $Connection->query('SELECT name, title FROM '.$txpcfg['table_prefix'].'txp_category');
if ($result) {
$cats = $result->fetchAll(PDO::FETCH_ASSOC);
if (is_array($cats) && count($cats)) {
foreach($cats as $cat) {
$cat_titles[$cat['name']] = $cat['title'];
}
}
}
/* ======= POSTS ======== */
$sql = 'SELECT * FROM '.$txpcfg['table_prefix'].'textpattern';
try {
$Statement = $Connection->query($sql);
$result = $Statement->setFetchMode(PDO::FETCH_ASSOC);
while ($row = $Statement->fetch()) {
$article_time = strtotime($row['Posted']);
echo '<item>'.EOL;
echo '<title>'.escape_title($row['Title']).'</title>'.EOL;
switch ($permlink_mode) {
case 'section_id_title':
$url = $siteurl.$row['Section'].'/'.$row['ID'].'/'.$row['url_title'];
break;
case 'year_month_day_title':
$url = $siteurl.date('Y/m/d', $article_time).'/'.$row['url_title'];
break;
case 'section_title':
$url = $siteurl.$row['Section'].'/'.$row['url_title'];
break;
case 'title_only':
$url = $siteurl.$row['url_title'];
break;
case 'id_title':
$url = $siteurl.$row['ID'].'/'.$row['url_title'];
break;
}
echo '<link>'.$url.'</link>'.EOL;
echo '<pubDate>'.date('r', $article_time).'</pubDate>'.EOL;
echo '<dc:creator>'.$row['AuthorID'].'</dc:creator>'.EOL;
echo '<guid isPermaLink="false">'.$url.'</guid>'.EOL;
echo '<description></description>'.EOL;
if ($export_html) {
echo '<content:encoded><![CDATA['.$row['Body_html'].']]></content:encoded>'.EOL;
echo '<excerpt:encoded><![CDATA['.$row['Excerpt_html'].']]></excerpt:encoded>'.EOL;
}else{
echo '<content:encoded><![CDATA['.$row['Body'].']]></content:encoded>'.EOL;
echo '<excerpt:encoded><![CDATA['.$row['Excerpt'].']]></excerpt:encoded>'.EOL;
}
echo '<wp:post_id>'.$row['ID'].'</wp:post_id>'.EOL;
echo '<wp:post_date>'.$row['Posted'].'</wp:post_date>'.EOL;
echo '<wp:post_date_gmt>'.$row['Posted'].'</wp:post_date_gmt>'.EOL;
echo '<wp:comment_status>'.($row['Annotate']=='1' ? 'open' : 'closed').'</wp:comment_status>'.EOL;
echo '<wp:ping_status></wp:ping_status>'.EOL;
echo '<wp:post_name>'.$row['url_title'].'</wp:post_name>'.EOL;
echo '<wp:status>'.((int)$row['Status'] < 4 ? 'draft' : 'publish').'</wp:status>'.EOL;
echo '<wp:post_parent>0</wp:post_parent>'.EOL;
echo '<wp:menu_order>0</wp:menu_order>'.EOL;
echo '<wp:post_type>post</wp:post_type>'.EOL;
echo '<wp:post_password></wp:post_password>'.EOL;
echo '<wp:is_sticky>'.(int)($row['Status'] == 5).'</wp:is_sticky>'.EOL;
if ($row['Category1']!='') {
echo '<category domain="category" nicename="'.$row['Category1'].'"><![CDATA['.$cat_titles[$row['Category1']].']]></category>'.EOL;
}
if ($row['Category2']!='') {
echo '<category domain="category" nicename="'.$row['Category2'].'"><![CDATA['.$cat_titles[$row['Category2']].']]></category>'.EOL;
}
/* ============ COMMENTS ============== */
$sql = 'SELECT * FROM '.$txpcfg['table_prefix'].'txp_discuss WHERE parentid='.(int)$row['ID'].' AND visible=1';
$Statement2 = $Connection->query($sql);
$result = $Statement2->setFetchMode(PDO::FETCH_ASSOC);
while ($comment = $Statement2->fetch()) {
$comment_web = txpspecialchars($comment['web']);
if (substr($comment_web, 0, 4) != 'http') {
$comment_web = 'http://'.$comment_web;
}
echo '<wp:comment>'.EOL;
echo '<wp:comment_id>'.$comment['discussid'].'</wp:comment_id>'.EOL;
echo '<wp:comment_author><![CDATA['.$comment['name'].']]></wp:comment_author>'.EOL;
echo '<wp:comment_author_email>'.$comment['email'].'</wp:comment_author_email>'.EOL;
echo '<wp:comment_author_url>'.$comment_web.'</wp:comment_author_url>'.EOL;
echo '<wp:comment_author_IP>'.$comment['ip'].'</wp:comment_author_IP>'.EOL;
echo '<wp:comment_date>'.$comment['posted'].'</wp:comment_date>'.EOL;
echo '<wp:comment_date_gmt>'.$comment['posted'].'</wp:comment_date_gmt>'.EOL;
echo '<wp:comment_content><![CDATA['.fix_encoded_mess($comment['message']).']]></wp:comment_content>'.EOL;
echo '<wp:comment_approved>'.$comment['visible'].'</wp:comment_approved>'.EOL;
echo '<wp:comment_type></wp:comment_type>'.EOL;
echo '<wp:comment_parent>0</wp:comment_parent>'.EOL;
echo '<wp:comment_user_id>0</wp:comment_user_id>'.EOL;
echo '</wp:comment>'.EOL;
}
echo '</item>'.EOL;
flush();
}
} catch (PDOException $e) {
print $e->getMessage();
}
echo '</channel>
</rss>';
function fix_encoded_mess($str)
{
$find = array(
'’',
'‘',
'“',
'â€',
'£',
);
$replace = array(
"’",
"‘",
'“',
'”',
'£',
);
return str_replace($find, $replace, $str);
}
function txpspecialchars($string, $flags = ENT_QUOTES, $encoding = 'UTF-8', $double_encode = true)
{
return htmlspecialchars($string, $flags, $encoding, $double_encode);
}
function escape_title($title)
{
return strtr($title, array(
'<' => '<',
'>' => '>',
"'" => ''',
'"' => '"',
));
}