root/trunk/wp-admin/import/livejournal.php

Revision 8645, 5.7 kB (checked in by westi, 2 weeks ago)

phpdoc for wp-admin. See #7496 props santosj.

  • Property svn:eol-style set to native
Line 
1 <?php
2 /**
3  * LiveJournal Importer
4  *
5  * @package WordPress
6  * @subpackage Importer
7  */
8
9 /**
10  * LiveJournal Importer class
11  *
12  * Imports your LiveJournal XML exported file into WordPress.
13  *
14  * @since unknown
15  */
16 class LJ_Import {
17
18     var $file;
19
20     function header() {
21         echo '<div class="wrap">';
22         echo '<h2>'.__('Import LiveJournal').'</h2>';
23     }
24
25     function footer() {
26         echo '</div>';
27     }
28
29     function unhtmlentities($string) { // From php.net for < 4.3 compat
30         $trans_tbl = get_html_translation_table(HTML_ENTITIES);
31         $trans_tbl = array_flip($trans_tbl);
32         return strtr($string, $trans_tbl);
33     }
34
35     function greet() {
36         echo '<div class="narrow">';
37         echo '<p>'.__('Howdy! Upload your LiveJournal XML export file and we&#8217;ll import the posts into this blog.').'</p>';
38         echo '<p>'.__('Choose a LiveJournal XML file to upload, then click Upload file and import.').'</p>';
39         wp_import_upload_form("admin.php?import=livejournal&amp;step=1");
40         echo '</div>';
41     }
42
43     function import_posts() {
44         global $wpdb, $current_user;
45
46         set_magic_quotes_runtime(0);
47         $importdata = file($this->file); // Read the file into an array
48         $importdata = implode('', $importdata); // squish it
49         $importdata = str_replace(array ("\r\n", "\r"), "\n", $importdata);
50
51         preg_match_all('|<entry>(.*?)</entry>|is', $importdata, $posts);
52         $posts = $posts[1];
53         unset($importdata);
54         echo '<ol>';
55         foreach ($posts as $post) {
56             preg_match('|<subject>(.*?)</subject>|is', $post, $post_title);
57             $post_title = $wpdb->escape(trim($post_title[1]));
58             if ( empty($post_title) ) {
59                 preg_match('|<itemid>(.*?)</itemid>|is', $post, $post_title);
60                 $post_title = $wpdb->escape(trim($post_title[1]));
61             }
62
63             preg_match('|<eventtime>(.*?)</eventtime>|is', $post, $post_date);
64             $post_date = strtotime($post_date[1]);
65             $post_date = date('Y-m-d H:i:s', $post_date);
66
67             preg_match('|<event>(.*?)</event>|is', $post, $post_content);
68             $post_content = str_replace(array ('<![CDATA[', ']]>'), '', trim($post_content[1]));
69             $post_content = $this->unhtmlentities($post_content);
70
71             // Clean up content
72             $post_content = preg_replace('|<(/?[A-Z]+)|e', "'<' . strtolower('$1')", $post_content);
73             $post_content = str_replace('<br>', '<br />', $post_content);
74             $post_content = str_replace('<hr>', '<hr />', $post_content);
75             $post_content = $wpdb->escape($post_content);
76
77             $post_author = $current_user->ID;
78             $post_status = 'publish';
79
80             echo '<li>';
81             if ($post_id = post_exists($post_title, $post_content, $post_date)) {
82                 printf(__('Post <em>%s</em> already exists.'), stripslashes($post_title));
83             } else {
84                 printf(__('Importing post <em>%s</em>...'), stripslashes($post_title));
85                 $postdata = compact('post_author', 'post_date', 'post_content', 'post_title', 'post_status');
86                 $post_id = wp_insert_post($postdata);
87                 if ( is_wp_error( $post_id ) )
88                     return $post_id;
89                 if (!$post_id) {
90                     _e("Couldn't get post ID");
91                     echo '</li>';
92                     break;
93                 }
94             }
95
96             preg_match_all('|<comment>(.*?)</comment>|is', $post, $comments);
97             $comments = $comments[1];
98
99             if ( $comments ) {
100                 $comment_post_ID = (int) $post_id;
101                 $num_comments = 0;
102                 foreach ($comments as $comment) {
103                     preg_match('|<event>(.*?)</event>|is', $comment, $comment_content);
104                     $comment_content = str_replace(array ('<![CDATA[', ']]>'), '', trim($comment_content[1]));
105                     $comment_content = $this->unhtmlentities($comment_content);
106
107                     // Clean up content
108                     $comment_content = preg_replace('|<(/?[A-Z]+)|e', "'<' . strtolower('$1')", $comment_content);
109                     $comment_content = str_replace('<br>', '<br />', $comment_content);
110                     $comment_content = str_replace('<hr>', '<hr />', $comment_content);
111                     $comment_content = $wpdb->escape($comment_content);
112
113                     preg_match('|<eventtime>(.*?)</eventtime>|is', $comment, $comment_date);
114                     $comment_date = trim($comment_date[1]);
115                     $comment_date = date('Y-m-d H:i:s', strtotime($comment_date));
116
117                     preg_match('|<name>(.*?)</name>|is', $comment, $comment_author);
118                     $comment_author = $wpdb->escape(trim($comment_author[1]));
119
120                     preg_match('|<email>(.*?)</email>|is', $comment, $comment_author_email);
121                     $comment_author_email = $wpdb->escape(trim($comment_author_email[1]));
122
123                     $comment_approved = 1;
124                     // Check if it's already there
125                     if (!comment_exists($comment_author, $comment_date)) {
126                         $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_email', 'comment_date', 'comment_content', 'comment_approved');
127                         $commentdata = wp_filter_comment($commentdata);
128                         wp_insert_comment($commentdata);
129                         $num_comments++;
130                     }
131                 }
132             }
133             if ( $num_comments ) {
134                 echo ' ';
135                 printf(__ngettext('(%s comment)', '(%s comments)', $num_comments), $num_comments);
136             }
137             echo '</li>';
138         }
139         echo '</ol>';
140     }
141
142     function import() {
143         $file = wp_import_handle_upload();
144         if ( isset($file['error']) ) {
145             echo $file['error'];
146             return;
147         }
148
149         $this->file = $file['file'];
150         $result = $this->import_posts();
151         if ( is_wp_error( $result ) )
152             return $result;
153         wp_import_cleanup($file['id']);
154         do_action('import_done', 'livejournal');
155
156         echo '<h3>';
157         printf(__('All done. <a href="%s">Have fun!</a>'), get_option('home'));
158         echo '</h3>';
159     }
160
161     function dispatch() {
162         if (empty ($_GET['step']))
163             $step = 0;
164         else
165             $step = (int) $_GET['step'];
166
167         $this->header();
168
169         switch ($step) {
170             case 0 :
171                 $this->greet();
172                 break;
173             case 1 :
174                 check_admin_referer('import-upload');
175                 $result = $this->import();
176                 if ( is_wp_error( $result ) )
177                     echo $result->get_error_message();
178                 break;
179         }
180
181         $this->footer();
182     }
183
184     function LJ_Import() {
185         // Nothing.
186     }
187 }
188
189 $livejournal_import = new LJ_Import();
190
191 register_importer('livejournal', __('LiveJournal'), __('Import posts from a LiveJournal XML export file.'), array ($livejournal_import, 'dispatch'));
192 ?>
193
Note: See TracBrowser for help on using the browser.