Make WordPress Core

Ticket #2665: xanga.2.php

File xanga.2.php, 7.2 KB (added by everglow, 19 years ago)

Fixed 5-10-2007

Line 
1<?php
2
3//Xanga archive importer by Jeremy Jay
4//Modified for current WP release by Daniel Kozlowski
5//
6//Borrows heavily from the LiveJournal import script.
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 compatability
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 do not have Premium but have enough posts to be looking at this, just pay $4 for a month to get the archive and you will at least be supporting Xanga for all the hosting they have 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           
51            // <div class="smalltext">Posted 7/31/2004 at 4:56 PM</div>
52            preg_match('|<div class="smalltext">Posted (\d{1,2}/\d{1,2}/\d{4}) at|i', $post, $post_date);
53            $post_date = strtotime($post_date[1]);
54
55            $com = split(' ', $post_title);
56
57            if( $com[1]=='Comments' ) {
58                preg_match_all('|<div class="ctextfooterwrap"><div class="ctext">(.*?)</div></div>|is', $post, $comments);
59                $comments = $comments[1];
60
61                $comment_post_ID = $post_id;
62                $num_comments = 0;
63                foreach ($comments as $comment) {
64                    preg_match('|^(.*?)</div><div class="cfooter">|is', $comment, $comment_content);
65                    $comment_content = str_replace(array ('<![CDATA[', ']]>'), '', trim($comment_content[1]));
66                    $comment_content = $this->unhtmlentities($comment_content);
67
68                    // Clean up content
69                    $comment_content = preg_replace('|<(/?[A-Z]+)|e', "'<' . strtolower('$1')", $comment_content);
70                    $comment_content = str_replace('<br>', '<br />', $comment_content);
71                    $comment_content = str_replace('<hr>', '<hr />', $comment_content);
72                    $comment_content = $wpdb->escape($comment_content);
73
74                    preg_match('|<div class="cfooter">Posted (\d{1,2}/\d{1,2}/\d{4}) at |i', $comment, $comment_date);
75                    $comment_date = trim($comment_date[1]);
76                    $comment_date = date('Y-m-d H:i:s', strtotime($comment_date));
77
78                    preg_match('|<a href="http://www\.xanga\.com/home\.aspx\?user=(.*?)">(.*?)</a>|i', $comment, $comment_author);
79                    $comment_author = $wpdb->escape(trim($comment_author[1]));
80                    $comment_author_url = 'http://www.xanga.com/home.aspx?user='.$comment_author;
81
82                    $comment_approved = 1;
83                    // Check if it's already there
84                    if (!comment_exists($comment_author, $comment_date)) {
85                        $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_url', 'comment_date', 'comment_content', 'comment_approved');
86                        $commentdata = wp_filter_comment($commentdata);
87                        wp_insert_comment($commentdata);
88                        $num_comments++;
89                    }
90                }
91                if ( $num_comments ) {
92                    echo ' ';
93                    printf(__('(%s comments)'), $num_comments);
94                }
95            } else {
96                $post_title.=' at '.date('h:i a', $post_date);
97                $post_date = gmdate('Y-m-d H:i:s', $post_date);
98
99                preg_match('|<td style="padding-left:20; padding-bottom:20">(.*?)<div class="smalltext">Posted (\d{1,2}/\d{1,2}/\d{4}) at |i', $post, $post_content);
100                $post_content = str_replace(array ('<![CDATA[', ']]>'), '', trim($post_content[1]));
101                $post_content = $this->unhtmlentities($post_content);
102
103                // Clean up content
104                $post_content = preg_replace('|<(/?[A-Z]+)|e', "'<' . strtolower('$1')", $post_content);
105                $post_content = str_replace('<br>', '<br />', $post_content);
106                $post_content = str_replace('<hr>', '<hr />', $post_content);
107                $post_content = $wpdb->escape($post_content);
108
109                $post_author = $current_user->ID;
110                $post_status = 'publish';
111
112                echo '<li>';
113                if ($post_id = post_exists($post_title, $post_content, $post_date)) {
114                    printf(__('Post <i>%s</i> already exists.'), stripslashes($post_title));
115                } else {
116                    printf(__('Importing post <i>%s</i>...'), stripslashes($post_title));
117                    $postdata = compact('post_author', 'post_date', 'post_content', 'post_title', 'post_status');
118                    $post_id = wp_insert_post($postdata);
119                    if (!$post_id) {
120                        _e("Couldn't get post ID");
121                        echo '</li>';
122                        break;
123                    }
124                }
125            }
126
127            echo '</li>';
128            flush();
129            ob_flush();
130        }
131        echo '</ol>';
132    }
133
134    function import() {
135        $file = wp_import_handle_upload();
136        if ( isset($file['error']) ) {
137            echo $file['error'];
138            return;
139        }
140
141        $this->file = $file['file'];
142        $this->import_posts();
143        wp_import_cleanup($file['id']);
144       
145        echo '<h3>';
146        printf(__('All done. <a href="%s">Have fun!</a>'), get_option('home'));
147        echo '</h3>';
148    }
149
150    function dispatch() {
151        if (empty ($_GET['step']))
152            $step = 0;
153        else
154            $step = (int) $_GET['step'];
155
156        $this->header();
157       
158        switch ($step) {
159            case 0 :
160                $this->greet();
161                break;
162            case 1 :
163                $this->import();
164                break;
165        }
166       
167        $this->footer();
168    }
169
170    function Xanga_Import() {
171        // Nothing.   
172    }
173}
174
175$xanga_import = new Xanga_Import();
176
177register_importer('xanga', 'Xanga', __('Import posts from Xanga Archives'), array ($xanga_import, 'dispatch'));
178?>
179