Make WordPress Core

Ticket #2665: xanga.php.txt

File xanga.php.txt, 7.1 KB (added by dinkumator, 20 years ago)

xanga importer

Line 
1<?php
2// new xanga archive importer by Jeremy Jay
3//  - updated 4-2-06 for wordpress 2.0 and new archive format
4//
5// borrows code heavily from livejournal importer
6
7
8class Xanga_Import {
9
10    var $file;
11
12    function header() {
13        echo '<div class="wrap">';
14        echo '<h2>'.__('Import Xanga').'</h2>';
15    }
16
17    function footer() {
18        echo '</div>';
19    }
20
21    function unhtmlentities($string) { // From php.net for < 4.3 compat
22        $trans_tbl = get_html_translation_table(HTML_ENTITIES);
23        $trans_tbl = array_flip($trans_tbl);
24        return strtr($string, $trans_tbl);
25    }
26   
27    function greet() {
28        echo '<p>'.__('Howdy! This importer allows you to extract posts and comments from Xanga Premium Archive files into your blog.  If you don't have Premium but have enough posts to be looking at this, just pay $4 for a month to get the archive and you'll at least be supporting Xanga for all the hosting they've done for you.  Pick an archive file to upload and click Import.').'</p>';
29        wp_import_upload_form("admin.php?import=xanga&amp;step=1");
30    }
31
32    function import_posts() {
33        global $wpdb, $current_user;
34       
35        set_magic_quotes_runtime(0);
36        $importdata = file($this->file); // Read the file into an array
37        $importdata = implode('', $importdata); // squish it
38        $importdata = str_replace(array ("\r\n", "\r"), "\n", $importdata);
39       
40        preg_match_all('|<div class="blogheader">(.*?)<hr size=1 noshade>(<div class="blogheader">)*?|is', $importdata, $posts);
41        $posts = $posts[1];
42        unset($importdata);
43        echo '<ol>';       
44       
45        foreach ($posts as $post) {
46            flush();
47            preg_match('|^(.*?)</div>|is', $post, $post_title);
48            $post_title = $wpdb->escape(trim($post_title[1]));
49           
50            preg_match('|<div class="smalltext">Posted (.*?)</div>|is', $post, $post_date);
51            $post_date = strtotime($post_date[1]);
52
53            $com = split(' ', $post_title);
54
55            if( $com[1]=='Comments' ) {
56                preg_match_all('|<div class="ctextfooterwrap"><div class="ctext">(.*?)</div></div>|is', $post, $comments);
57                $comments = $comments[1];
58
59                $comment_post_ID = $post_id;
60                $num_comments = 0;
61                foreach ($comments as $comment) {
62                    preg_match('|^(.*?)</div><div class="cfooter">|is', $comment, $comment_content);
63                    $comment_content = str_replace(array ('<![CDATA[', ']]>'), '', trim($comment_content[1]));
64                    $comment_content = $this->unhtmlentities($comment_content);
65
66                    // Clean up content
67                    $comment_content = preg_replace('|<(/?[A-Z]+)|e', "'<' . strtolower('$1')", $comment_content);
68                    $comment_content = str_replace('<br>', '<br />', $comment_content);
69                    $comment_content = str_replace('<hr>', '<hr />', $comment_content);
70                    $comment_content = $wpdb->escape($comment_content);
71
72                    preg_match('|<div class="cfooter">Posted (.*?) by |is', $comment, $comment_date);
73                    $comment_date = trim($comment_date[1]);
74                    $comment_date = date('Y-m-d H:i:s', strtotime($comment_date));
75
76                    preg_match('|<a href="http://www\.xanga\.com/home\.aspx\?user=(.*?)">(.*?)</a>|is', $comment, $comment_author);
77                    $comment_author = $wpdb->escape(trim($comment_author[1]));
78                    $comment_author_url = 'http://www.xanga.com/home.aspx?user='.$comment_author;
79
80                    $comment_approved = 1;
81                    // Check if it's already there
82                    if (!comment_exists($comment_author, $comment_date)) {
83                        $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_url', 'comment_date', 'comment_content', 'comment_approved');
84                        $commentdata = wp_filter_comment($commentdata);
85                        wp_insert_comment($commentdata);
86                        $num_comments++;
87                    }
88                }
89                if ( $num_comments ) {
90                    echo ' ';
91                    printf(__('(%s comments)'), $num_comments);
92                }
93            } else {
94                $post_title.=' at '.date('h:i a', $post_date);
95                $post_date = gmdate('Y-m-d H:i:s', $post_date);
96
97                preg_match('|<td style="padding-left:20; padding-bottom:20">(.*?)<div class="smalltext">Posted |is', $post, $post_content);
98                $post_content = str_replace(array ('<![CDATA[', ']]>'), '', trim($post_content[1]));
99                $post_content = $this->unhtmlentities($post_content);
100
101                // Clean up content
102                $post_content = preg_replace('|<(/?[A-Z]+)|e', "'<' . strtolower('$1')", $post_content);
103                $post_content = str_replace('<br>', '<br />', $post_content);
104                $post_content = str_replace('<hr>', '<hr />', $post_content);
105                $post_content = $wpdb->escape($post_content);
106
107                $post_author = $current_user->ID;
108                $post_status = 'publish';
109
110                echo '<li>';
111                if ($post_id = post_exists($post_title, $post_content, $post_date)) {
112                    printf(__('Post <i>%s</i> already exists.'), stripslashes($post_title));
113                } else {
114                    printf(__('Importing post <i>%s</i>...'), stripslashes($post_title));
115                    $postdata = compact('post_author', 'post_date', 'post_content', 'post_title', 'post_status');
116                    $post_id = wp_insert_post($postdata);
117                    if (!$post_id) {
118                        _e("Couldn't get post ID");
119                        echo '</li>';
120                        break;
121                    }
122                }
123            }
124
125            echo '</li>';
126            flush();
127            ob_flush();
128        }
129        echo '</ol>';
130    }
131
132    function import() {
133        $file = wp_import_handle_upload();
134        if ( isset($file['error']) ) {
135            echo $file['error'];
136            return;
137        }
138
139        $this->file = $file['file'];
140        $this->import_posts();
141        wp_import_cleanup($file['id']);
142       
143        echo '<h3>';
144        printf(__('All done. <a href="%s">Have fun!</a>'), get_option('home'));
145        echo '</h3>';
146    }
147
148    function dispatch() {
149        if (empty ($_GET['step']))
150            $step = 0;
151        else
152            $step = (int) $_GET['step'];
153
154        $this->header();
155       
156        switch ($step) {
157            case 0 :
158                $this->greet();
159                break;
160            case 1 :
161                $this->import();
162                break;
163        }
164       
165        $this->footer();
166    }
167
168    function Xanga_Import() {
169        // Nothing.   
170    }
171}
172
173$xanga_import = new Xanga_Import();
174
175register_importer('xanga', 'Xanga', __('Import posts from Xanga Archives'), array ($xanga_import, 'dispatch'));
176?>
177