Make WordPress Core

Ticket #7612: tumblr_export.php

File tumblr_export.php, 15.0 KB (added by hailin, 15 years ago)

export code using XML parser

Line 
1<?php
2
3$username = isset($_REQUEST['username']) && !empty($_REQUEST['username']) ? $_REQUEST['username'] : '';
4
5if(empty($username))
6{
7?>
8<html>
9        <head>
10                <title>Tumblr2WordPress: Export Your Tumblr To WordPress</title>
11                <style>
12                        body
13                        {
14                                font-family: Verdana, Tahoma, Arial, Helvetica, sans-serif;
15                        }
16                        .sep
17                        {
18                                color: #CCCCCC;
19                        }
20                </style>
21        </head>
22        <body>
23                <h2>Tumblr2WordPress: Export Your Tumblr To WordPress</h2>
24                <small>
25                        Version: 0.1 &nbsp;<span class="sep">|</span>&nbsp;
26                        Author: <a href="http://haochen.me/">Hao Chen</a> &nbsp;<span class="sep">|</span>&nbsp;
27                        Email: detect [ta] hotmail [tod] com
28                </small><br/><br/>
29                <p>
30                        This tool will create a WordPress/WordPress.com compatible XML file from your Tumblr blog, which you can then save and import as a WordPress type.
31                </p>
32                <br/>
33                Tumblr username:<br/><br/>
34                <form method="POST" action="">
35                <input type="text" id="username" name="username" size="40"/>
36                <input type="submit" value="       Export       "/>
37                <br/><br/>
38                <input type="radio" name="type" value="wordpress.com" checked="checked">WordPress.com
39                <input type="radio" name="type" value="wordpress">WordPress (self hosted)
40                </form>
41                <br/><br/>
42        </body>
43</html>
44<?php
45die();
46}
47$type = $_REQUEST["type"];
48$i = 0;
49
50$posts = array();
51$feed = '';
52$allTags = array();
53
54do
55{
56        $url = 'http://'.$username.'.tumblr.com/api/read?start='. $i . '&num=50';
57        $file = file_get_contents($url);
58        $feed = new SimpleXMLElement($file);
59        $posts = array_merge($posts, $feed->xpath('posts//post'));
60        $i = (int)$feed->posts->attributes()->start + 50;
61}while($i <= (int)$feed->posts["total"]);
62
63function formatForWP($str)
64{
65        global $type;
66        switch($type)
67        {
68                case "wordpress.com":
69                        $str = formatVideoForWP(formatImageForWP($str));
70        }
71        return $str;
72}
73function formatImageForWP($str)
74{
75        if(preg_match_all('/(<p>)?\s*(<img[^>]*\/?>)\s*(<\/p>)?/', $str, $matches))
76        {
77                for($i=0;$i<sizeof($matches[0]);$i++)
78                {
79                        $str = str_replace($matches[0][$i], str_replace('/>',' alt=""/>', $matches[2][$i]), $str);
80                }
81        }
82        return $str;
83}
84
85function formatVideoForWP($str)
86{
87        if(preg_match_all('/<object[\s\S]*src="([\S\s]*?)&amp;[\s\S]*"[\s\S]*<\/object>/', $str, $matches))
88        {
89                for($i=0;$i<sizeof($matches);$i++)
90                {
91                        if((strpos($matches[1][$i], 'youtube.com') !== false))
92                        {
93                                $str = str_replace($matches[0][$i], '[youtube='.$matches[1][$i].']', $str);
94                        }
95                }
96        }
97        return $str;
98}
99
100function removeWeirdChars($str)
101{
102        return trim(preg_replace('{(-)\1+}','$1',preg_replace('/[^a-zA-Z0-9-]/', '', str_replace(' ','-',strtolower(strip_tags($str))))),'-');
103}
104
105function getTags($post)
106{
107        echo "<category><![CDATA[Uncategorized]]></category>\n";
108        echo "\t\t<category domain=\"category\" nicename=\"uncategorized\"><![CDATA[Uncategorized]]></category>\n";
109        if($post->tag) 
110        { 
111                foreach($post->tag as $tag)
112                {
113                        echo "\t\t<category domain=\"tag\"><![CDATA[$tag]]></category>\n";
114                        echo "\t\t<category domain=\"tag\" nicename=\"" . removeWeirdChars($tag) . "\"><![CDATA[$tag]]></category>\n";
115                        addTag((String)$tag);
116                }
117        }
118}
119
120function addTag($tag)
121{
122        global $allTags;
123        if(!in_array($tag, $allTags))
124                $allTags[] = $tag;
125}
126
127function getAllTags()
128{
129        global $allTags;
130        foreach($allTags as $tag)
131        {
132                echo "\t<wp:tag><wp:tag_slug>". removeWeirdChars($tag) . "</wp:tag_slug><wp:tag_name><![CDATA[$tag]]></wp:tag_name></wp:tag>\n";
133        }
134}
135
136header('content-type: text/xml');
137header("content-disposition: attachment; filename=tumblr_$username.xml");
138?>
139<?php echo "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"; ?>
140<!-- This is a WordPress eXtended RSS file generated by WordPress as an export of your blog. -->
141<!-- It contains information about your blog's posts, comments, and categories. -->
142<!-- You may use this file to transfer that content from one site to another. -->
143<!-- This file is not intended to serve as a complete backup of your blog. -->
144
145<!-- To import this information into a WordPress blog follow these steps. -->
146<!-- 1. Log into that blog as an administrator. -->
147<!-- 2. Go to Manage: Import in the blog's admin panels. -->
148<!-- 3. Choose "WordPress" from the list. -->
149<!-- 4. Upload this file using the form provided on that page. -->
150<!-- 5. You will first be asked to map the authors in this export file to users -->
151<!--    on the blog.  For each author, you may choose to map to an -->
152<!--    existing user on the blog or to create a new user -->
153<!-- 6. WordPress will then import each of the posts, comments, and categories -->
154<!--    contained in this file into your blog -->
155
156<!-- generator="Tumblr2WordPress/0.1" created="<?php echo date("Y-m-d H:i") ?>"-->
157<rss version="2.0"
158        xmlns:content="http://purl.org/rss/1.0/modules/content/"
159        xmlns:wfw="http://wellformedweb.org/CommentAPI/"
160        xmlns:dc="http://purl.org/dc/elements/1.1/"
161        xmlns:wp="http://wordpress.org/export/1.0/"
162>
163
164<channel>
165        <title><?php echo $feed->tumblelog->attributes()->title ?></title>
166        <link>http://<?php echo $feed->tumblelog->attributes()->name ?>.tumblr.com/</link>
167        <description><?php echo $feed->tumblelog ?></description>
168        <pubDate><?php echo date("r") ?></pubDate>
169        <generator>http://haochen.me/tumblr/?v=0.1</generator>
170        <language>en</language>
171        <wp:wxr_version>1.0</wp:wxr_version>
172        <wp:base_site_url>http://<?php echo $feed->tumblelog->attributes()->name ?>.tumblr.com/</wp:base_site_url>
173        <wp:base_blog_url>http://<?php echo $feed->tumblelog->attributes()->name ?>.tumblr.com/</wp:base_blog_url>
174        <wp:category>
175                <wp:category_nicename>uncategorized</wp:category_nicename>
176                <wp:category_parent></wp:category_parent>
177                <wp:cat_name><![CDATA[Uncategorized]]></wp:cat_name>
178        </wp:category>
179<?php 
180ob_start();
181        foreach($posts as $post) 
182        { 
183?>
184        <item>
185<?php
186                switch($post->attributes()->type) 
187                {
188                        case "regular":
189?>
190                <title><?php echo htmlspecialchars($post->{'regular-title'}) ?></title>
191                <link><?php echo $post->attributes()->url ?></link>
192                <pubDate><?php echo $post->attributes()->date ?> +0000</pubDate>
193                <dc:creator><![CDATA[admin]]></dc:creator>
194                <?php getTags($post) ?>
195                <guid isPermaLink="false"><?php echo $post->attributes()->url ?></guid>
196                <description></description>
197                <content:encoded><![CDATA[<?php echo formatForWP($post->{'regular-body'}) ?>]]></content:encoded>
198                <wp:post_id><?php echo $post->attributes()->id ?></wp:post_id>
199                <wp:post_date><?php echo date('Y-m-d G:i:s', (double)$post->attributes()->{'unix-timestamp'}) ?></wp:post_date>
200                <wp:post_date_gmt><?php echo str_replace(" GMT", "", $post->attributes()->{'date-gmt'}) ?></wp:post_date_gmt>
201                <wp:comment_status>open</wp:comment_status>
202                <wp:ping_status>open</wp:ping_status>
203                <wp:post_name><?php echo removeWeirdChars($post->{'regular-title'}) ?></wp:post_name>
204                <wp:status>publish</wp:status>
205                <wp:post_parent>0</wp:post_parent>
206                <wp:menu_order>0</wp:menu_order>
207                <wp:post_type>post</wp:post_type>
208                <wp:post_password></wp:post_password>
209<?php
210                                break;
211                        case "photo":
212?>
213                <title><?php echo htmlspecialchars(strip_tags($post->{'photo-caption'})) ?></title>
214                <link><?php echo $post->attributes()->url ?></link>
215                <pubDate><?php echo $post->attributes()->date ?> +0000</pubDate>
216                <dc:creator><![CDATA[admin]]></dc:creator>
217                <?php getTags($post) ?>
218                <guid isPermaLink="false"><?php echo $post->attributes()->url ?></guid>
219                <description></description>
220                <content:encoded><![CDATA[<img src="<?php echo $post->{'photo-url'} ?>" alt=""/><br/><br/><?php echo formatForWP($post->{'photo-caption'}) ?>]]></content:encoded>
221                <wp:post_id><?php echo $post->attributes()->id ?></wp:post_id>
222                <wp:post_date><?php echo date('Y-m-d G:i:s', (double)$post->attributes()->{'unix-timestamp'}) ?></wp:post_date>
223                <wp:post_date_gmt><?php echo str_replace(" GMT", "", $post->attributes()->{'date-gmt'}) ?></wp:post_date_gmt>
224                <wp:comment_status>open</wp:comment_status>
225                <wp:ping_status>open</wp:ping_status>
226                <wp:post_name><?php echo removeWeirdChars($post->{'photo-caption'}) ?></wp:post_name>
227                <wp:status>publish</wp:status>
228                <wp:post_parent>0</wp:post_parent>
229                <wp:menu_order>0</wp:menu_order>
230                <wp:post_type>post</wp:post_type>
231                <wp:post_password></wp:post_password>
232<?php
233                                break;
234                        case "quote":
235?>
236                <title><?php echo htmlspecialchars(strip_tags($post->{'quote-text'})) ?></title>
237                <link><?php echo $post->attributes()->url ?></link>
238                <pubDate><?php echo $post->attributes()->date ?> +0000</pubDate>
239                <dc:creator><![CDATA[admin]]></dc:creator>
240                <?php getTags($post) ?>
241                <guid isPermaLink="false"><?php echo $post->attributes()->url ?></guid>
242                <description></description>
243                <content:encoded><![CDATA[<?php echo $post->{'quote-text'} ?><br/><?php echo formatForWP($post->{'quote-source'}) ?>]]></content:encoded>
244                <wp:post_id><?php echo $post->attributes()->id ?></wp:post_id>
245                <wp:post_date><?php echo date('Y-m-d G:i:s', (double)$post->attributes()->{'unix-timestamp'}) ?></wp:post_date>
246                <wp:post_date_gmt><?php echo str_replace(" GMT", "", $post->attributes()->{'date-gmt'}) ?></wp:post_date_gmt>
247                <wp:comment_status>open</wp:comment_status>
248                <wp:ping_status>open</wp:ping_status>
249                <wp:post_name><?php echo removeWeirdChars(str_replace('&#8220;','',str_replace('&#8221;','',$post->{'quote-text'}))) ?></wp:post_name>
250                <wp:status>publish</wp:status>
251                <wp:post_parent>0</wp:post_parent>
252                <wp:menu_order>0</wp:menu_order>
253                <wp:post_type>post</wp:post_type>
254                <wp:post_password></wp:post_password>
255<?php
256                                break;
257                        case "link":
258?>
259                <title><?php echo htmlspecialchars(strip_tags($post->{'link-text'})) ?></title>
260                <link><?php echo $post->attributes()->url ?></link>
261                <pubDate><?php echo $post->attributes()->date ?> +0000</pubDate>
262                <dc:creator><![CDATA[admin]]></dc:creator>
263                <?php getTags($post) ?>
264                <guid isPermaLink="false"><?php echo $post->attributes()->url ?></guid>
265                <description><?php echo htmlspecialchars(strip_tags($post->{'link-description'})) ?></description>
266                <content:encoded><![CDATA[<a href="<?php echo $post->{'link-url'} ?>"><?php echo $post->{'link-text'} ?></a><br/><?php echo formatForWP($post->{'link-description'}) ?>]]></content:encoded>
267                <wp:post_id><?php echo $post->attributes()->id ?></wp:post_id>
268                <wp:post_date><?php echo date('Y-m-d G:i:s', (double)$post->attributes()->{'unix-timestamp'}) ?></wp:post_date>
269                <wp:post_date_gmt><?php echo str_replace(" GMT", "", $post->attributes()->{'date-gmt'}) ?></wp:post_date_gmt>
270                <wp:comment_status>open</wp:comment_status>
271                <wp:ping_status>open</wp:ping_status>
272                <wp:post_name><?php echo removeWeirdChars($post->{'link-text'}) ?></wp:post_name>
273                <wp:status>publish</wp:status>
274                <wp:post_parent>0</wp:post_parent>
275                <wp:menu_order>0</wp:menu_order>
276                <wp:post_type>post</wp:post_type>
277                <wp:post_password></wp:post_password>
278<?php
279                                break;
280                        case "conversation":
281?>
282                <title><?php echo htmlspecialchars(strip_tags($post->{'conversation-title'})) ?></title>
283                <link><?php echo $post->attributes()->url ?></link>
284                <pubDate><?php echo $post->attributes()->date ?> +0000</pubDate>
285                <dc:creator><![CDATA[admin]]></dc:creator>
286                <?php getTags($post) ?>
287                <guid isPermaLink="false"><?php echo $post->attributes()->url ?></guid>
288                <description></description>
289                <content:encoded><![CDATA[<?php foreach($post->{'conversation-line'} as $line) { ?><strong><?php echo $line->attributes()->label ?></strong> <?php echo $line ?><br/><?php } ?>]]></content:encoded>
290                <wp:post_id><?php echo $post->attributes()->id ?></wp:post_id>
291                <wp:post_date><?php echo date('Y-m-d G:i:s', (double)$post->attributes()->{'unix-timestamp'}) ?></wp:post_date>
292                <wp:post_date_gmt><?php echo str_replace(" GMT", "", $post->attributes()->{'date-gmt'}) ?></wp:post_date_gmt>
293                <wp:comment_status>open</wp:comment_status>
294                <wp:ping_status>open</wp:ping_status>
295                <wp:post_name><?php echo removeWeirdChars($post->{'conservation-title'}) ?></wp:post_name>
296                <wp:status>publish</wp:status>
297                <wp:post_parent>0</wp:post_parent>
298                <wp:menu_order>0</wp:menu_order>
299                <wp:post_type>post</wp:post_type>
300                <wp:post_password></wp:post_password>
301<?php
302                                break;
303                        case "video":
304?>
305                <title><?php echo htmlspecialchars(strip_tags($post->{'video-caption'})) ?></title>
306                <link><?php echo $post->attributes()->url ?></link>
307                <pubDate><?php echo $post->attributes()->date ?> +0000</pubDate>
308                <dc:creator><![CDATA[admin]]></dc:creator>
309                <?php getTags($post) ?>
310                <guid isPermaLink="false"><?php echo $post->attributes()->url ?></guid>
311                <description></description>
312<?php if($type == 'wordpress.com' && strpos($post->{'video-source'}, 'youtube.com') !== false) { ?>
313                <content:encoded><![CDATA[[youtube=<?php echo $post->{'video-source'} ?>]]]></content:encoded>
314<?php }elseif($type == 'wordpress.com' && strpos($post->{'video-source'}, 'video.google.com') !== false) { ?>   
315                <content:encoded><![CDATA[[googlevideo=<?php preg_match('/src="([\S\s]*?)"/', $post->{'video-player'}, $matches); echo $matches[1]; ?>]]]></content:encoded>
316<?php }else{ ?> 
317                <content:encoded><![CDATA[<?php echo $post->{'video-player'} ?>]]></content:encoded>
318<?php } ?>
319                <wp:post_id><?php echo $post->attributes()->id ?></wp:post_id>
320                <wp:post_date><?php echo date('Y-m-d G:i:s', (double)$post->attributes()->{'unix-timestamp'}) ?></wp:post_date>
321                <wp:post_date_gmt><?php echo str_replace(" GMT", "", $post->attributes()->{'date-gmt'}) ?></wp:post_date_gmt>
322                <wp:comment_status>open</wp:comment_status>
323                <wp:ping_status>open</wp:ping_status>
324                <wp:post_name><?php echo removeWeirdChars($post->{'video-caption'}) ?></wp:post_name>
325                <wp:status>publish</wp:status>
326                <wp:post_parent>0</wp:post_parent>
327                <wp:menu_order>0</wp:menu_order>
328                <wp:post_type>post</wp:post_type>
329                <wp:post_password></wp:post_password>
330<?php
331                                break;
332                        case "audio":
333?>
334                <title><?php echo htmlspecialchars(strip_tags($post->{'audio-caption'})) ?></title>
335                <link><?php echo $post->attributes()->url ?></link>
336                <pubDate><?php echo $post->attributes()->date ?> +0000</pubDate>
337                <dc:creator><![CDATA[admin]]></dc:creator>
338                <?php getTags($post) ?>
339                <guid isPermaLink="false"><?php echo $post->attributes()->url ?></guid>
340                <description></description>
341                <content:encoded><![CDATA[<a href="<?php preg_match('/audio_file=([\S\s]*?)(&|")/', $post->{'audio-player'}, $matches); echo $matches[1]; ?>"><?php echo $post->{'audio-caption'} ?></a>]]></content:encoded>
342                <wp:post_id><?php echo $post->attributes()->id ?></wp:post_id>
343                <wp:post_date><?php echo date('Y-m-d G:i:s', (double)$post->attributes()->{'unix-timestamp'}) ?></wp:post_date>
344                <wp:post_date_gmt><?php echo str_replace(" GMT", "", $post->attributes()->{'date-gmt'}) ?></wp:post_date_gmt>
345                <wp:comment_status>open</wp:comment_status>
346                <wp:ping_status>open</wp:ping_status>
347                <wp:post_name><?php echo removeWeirdChars($post->{'audio-caption'}) ?></wp:post_name>
348                <wp:status>publish</wp:status>
349                <wp:post_parent>0</wp:post_parent>
350                <wp:menu_order>0</wp:menu_order>
351                <wp:post_type>post</wp:post_type>
352                <wp:post_password></wp:post_password>
353<?php
354                                break;
355                }
356?>
357        </item>
358<?php 
359        }
360        $out = ob_get_contents();
361        ob_end_clean();
362        getAllTags();
363        echo $out;
364?>
365</channel>
366</rss>