| | 1 | <?php |
|---|
| | 2 | |
|---|
| | 3 | class LJ_Import { |
|---|
| | 4 | |
|---|
| | 5 | var $posts = array (); |
|---|
| | 6 | var $file; |
|---|
| | 7 | |
|---|
| | 8 | function header() { |
|---|
| | 9 | echo '<div class="wrap">'; |
|---|
| | 10 | echo '<h2>'.__('Import LiveJournal').'</h2>'; |
|---|
| | 11 | } |
|---|
| | 12 | |
|---|
| | 13 | function footer() { |
|---|
| | 14 | echo '</div>'; |
|---|
| | 15 | } |
|---|
| | 16 | |
|---|
| | 17 | function unhtmlentities($string) { // From php.net for < 4.3 compat |
|---|
| | 18 | $trans_tbl = get_html_translation_table(HTML_ENTITIES); |
|---|
| | 19 | $trans_tbl = array_flip($trans_tbl); |
|---|
| | 20 | return strtr($string, $trans_tbl); |
|---|
| | 21 | } |
|---|
| | 22 | |
|---|
| | 23 | function greet() { |
|---|
| | 24 | echo '<p>'.__('Howdy! This importer allows you to extract posts from LiveJournal XML export file into your blog. Pick a LiveJournal file to upload and click Import.').'</p>'; |
|---|
| | 25 | wp_import_upload_form("admin.php?import=livejournal&step=1"); |
|---|
| | 26 | } |
|---|
| | 27 | |
|---|
| | 28 | function get_posts() { |
|---|
| | 29 | global $wpdb, $current_user; |
|---|
| | 30 | |
|---|
| | 31 | set_magic_quotes_runtime(0); |
|---|
| | 32 | $datalines = file($this->file); // Read the file into an array |
|---|
| | 33 | $importdata = implode('', $datalines); // squish it |
|---|
| | 34 | $importdata = str_replace(array ("\r\n", "\r"), "\n", $importdata); |
|---|
| | 35 | |
|---|
| | 36 | preg_match_all('|<entry>(.*?)</entry>|is', $importdata, $this->posts); |
|---|
| | 37 | $this->posts = $this->posts[1]; |
|---|
| | 38 | $index = 0; |
|---|
| | 39 | foreach ($this->posts as $post) { |
|---|
| | 40 | preg_match('|<subject>(.*?)</subject>|is', $post, $post_title); |
|---|
| | 41 | $post_title = $wpdb->escape(trim($post_title[1])); |
|---|
| | 42 | if ( empty($post_title) ) { |
|---|
| | 43 | preg_match('|<eventid>(.*?)</eventid>|is', $post, $post_title); |
|---|
| | 44 | $post_title = $wpdb->escape(trim($post_title[1])); |
|---|
| | 45 | } |
|---|
| | 46 | |
|---|
| | 47 | preg_match('|<eventtime>(.*?)</eventtime>|is', $post, $post_date); |
|---|
| | 48 | |
|---|
| | 49 | $post_date = strtotime($post_date[1]); |
|---|
| | 50 | $post_date = gmdate('Y-m-d H:i:s', $post_date); |
|---|
| | 51 | |
|---|
| | 52 | preg_match('|<event>(.*?)</event>|is', $post, $post_content); |
|---|
| | 53 | $post_content = str_replace(array ('<![CDATA[', ']]>'), '', $wpdb->escape(trim($post_content[1]))); |
|---|
| | 54 | |
|---|
| | 55 | if (!$post_content) { |
|---|
| | 56 | // This is for feeds that put content in description |
|---|
| | 57 | preg_match('|<description>(.*?)</description>|is', $post, $post_content); |
|---|
| | 58 | $post_content = $wpdb->escape($this->unhtmlentities(trim($post_content[1]))); |
|---|
| | 59 | } |
|---|
| | 60 | |
|---|
| | 61 | // Clean up content |
|---|
| | 62 | $post_content = preg_replace('|<(/?[A-Z]+)|e', "'<' . strtolower('$1')", $post_content); |
|---|
| | 63 | $post_content = str_replace('<br>', '<br />', $post_content); |
|---|
| | 64 | $post_content = str_replace('<hr>', '<hr />', $post_content); |
|---|
| | 65 | |
|---|
| | 66 | $post_author = $current_user->ID; |
|---|
| | 67 | $post_status = 'publish'; |
|---|
| | 68 | $this->posts[$index] = compact('post_author', 'post_date', 'post_content', 'post_title', 'post_status'); |
|---|
| | 69 | $index++; |
|---|
| | 70 | } |
|---|
| | 71 | } |
|---|
| | 72 | |
|---|
| | 73 | function import_posts() { |
|---|
| | 74 | echo '<ol>'; |
|---|
| | 75 | |
|---|
| | 76 | foreach ($this->posts as $post) { |
|---|
| | 77 | echo "<li>".__('Importing post...'); |
|---|
| | 78 | |
|---|
| | 79 | extract($post); |
|---|
| | 80 | |
|---|
| | 81 | if ($post_id = post_exists($post_title, $post_content, $post_date)) { |
|---|
| | 82 | _e('Post already imported'); |
|---|
| | 83 | } else { |
|---|
| | 84 | $post_id = wp_insert_post($post); |
|---|
| | 85 | if (!$post_id) { |
|---|
| | 86 | _e("Couldn't get post ID"); |
|---|
| | 87 | return; |
|---|
| | 88 | } |
|---|
| | 89 | |
|---|
| | 90 | _e('Done !'); |
|---|
| | 91 | } |
|---|
| | 92 | echo '</li>'; |
|---|
| | 93 | } |
|---|
| | 94 | |
|---|
| | 95 | echo '</ol>'; |
|---|
| | 96 | |
|---|
| | 97 | } |
|---|
| | 98 | |
|---|
| | 99 | function import() { |
|---|
| | 100 | $file = wp_import_handle_upload(); |
|---|
| | 101 | if ( isset($file['error']) ) { |
|---|
| | 102 | echo $file['error']; |
|---|
| | 103 | return; |
|---|
| | 104 | } |
|---|
| | 105 | |
|---|
| | 106 | $this->file = $file['file']; |
|---|
| | 107 | $this->get_posts(); |
|---|
| | 108 | $this->import_posts(); |
|---|
| | 109 | wp_import_cleanup($file['id']); |
|---|
| | 110 | |
|---|
| | 111 | echo '<h3>'; |
|---|
| | 112 | printf(__('All done. <a href="%s">Have fun!</a>'), get_option('home')); |
|---|
| | 113 | echo '</h3>'; |
|---|
| | 114 | } |
|---|
| | 115 | |
|---|
| | 116 | function dispatch() { |
|---|
| | 117 | if (empty ($_GET['step'])) |
|---|
| | 118 | $step = 0; |
|---|
| | 119 | else |
|---|
| | 120 | $step = (int) $_GET['step']; |
|---|
| | 121 | |
|---|
| | 122 | $this->header(); |
|---|
| | 123 | |
|---|
| | 124 | switch ($step) { |
|---|
| | 125 | case 0 : |
|---|
| | 126 | $this->greet(); |
|---|
| | 127 | break; |
|---|
| | 128 | case 1 : |
|---|
| | 129 | $this->import(); |
|---|
| | 130 | break; |
|---|
| | 131 | } |
|---|
| | 132 | |
|---|
| | 133 | $this->footer(); |
|---|
| | 134 | } |
|---|
| | 135 | |
|---|
| | 136 | function LJ_Import() { |
|---|
| | 137 | // Nothing. |
|---|
| | 138 | } |
|---|
| | 139 | } |
|---|
| | 140 | |
|---|
| | 141 | $livejournal_import = new LJ_Import(); |
|---|
| | 142 | |
|---|
| | 143 | //register_importer('livejournal', 'LiveJournal', __('Import posts from LiveJournal'), array ($livejournal_import, 'dispatch')); |
|---|
| | 144 | ?> |
|---|