PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/2.0.10/wp-admin/import/livejournal.php

#
PHP | 171 lines | 145 code | 24 blank | 2 comment | 9 complexity | b89dbd579bc9c528008321c6e327461e MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.0, LGPL-2.1, GPL-2.0
  1. <?php
  2. class LJ_Import {
  3. var $file;
  4. function header() {
  5. echo '<div class="wrap">';
  6. echo '<h2>'.__('Import LiveJournal').'</h2>';
  7. }
  8. function footer() {
  9. echo '</div>';
  10. }
  11. function unhtmlentities($string) { // From php.net for < 4.3 compat
  12. $trans_tbl = get_html_translation_table(HTML_ENTITIES);
  13. $trans_tbl = array_flip($trans_tbl);
  14. return strtr($string, $trans_tbl);
  15. }
  16. function greet() {
  17. 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>';
  18. wp_import_upload_form("admin.php?import=livejournal&amp;step=1");
  19. }
  20. function import_posts() {
  21. global $wpdb, $current_user;
  22. set_magic_quotes_runtime(0);
  23. $importdata = file($this->file); // Read the file into an array
  24. $importdata = implode('', $importdata); // squish it
  25. $importdata = str_replace(array ("\r\n", "\r"), "\n", $importdata);
  26. preg_match_all('|<entry>(.*?)</entry>|is', $importdata, $posts);
  27. $posts = $posts[1];
  28. unset($importdata);
  29. echo '<ol>';
  30. foreach ($posts as $post) {
  31. flush();
  32. preg_match('|<subject>(.*?)</subject>|is', $post, $post_title);
  33. $post_title = $wpdb->escape(trim($post_title[1]));
  34. if ( empty($post_title) ) {
  35. preg_match('|<itemid>(.*?)</itemid>|is', $post, $post_title);
  36. $post_title = $wpdb->escape(trim($post_title[1]));
  37. }
  38. preg_match('|<eventtime>(.*?)</eventtime>|is', $post, $post_date);
  39. $post_date = strtotime($post_date[1]);
  40. $post_date = gmdate('Y-m-d H:i:s', $post_date);
  41. preg_match('|<event>(.*?)</event>|is', $post, $post_content);
  42. $post_content = str_replace(array ('<![CDATA[', ']]>'), '', trim($post_content[1]));
  43. $post_content = $this->unhtmlentities($post_content);
  44. // Clean up content
  45. $post_content = preg_replace('|<(/?[A-Z]+)|e', "'<' . strtolower('$1')", $post_content);
  46. $post_content = str_replace('<br>', '<br />', $post_content);
  47. $post_content = str_replace('<hr>', '<hr />', $post_content);
  48. $post_content = $wpdb->escape($post_content);
  49. $post_author = $current_user->ID;
  50. $post_status = 'publish';
  51. echo '<li>';
  52. if ($post_id = post_exists($post_title, $post_content, $post_date)) {
  53. printf(__('Post <i>%s</i> already exists.'), stripslashes($post_title));
  54. } else {
  55. printf(__('Importing post <i>%s</i>...'), stripslashes($post_title));
  56. $postdata = compact('post_author', 'post_date', 'post_content', 'post_title', 'post_status');
  57. $post_id = wp_insert_post($postdata);
  58. if (!$post_id) {
  59. _e("Couldn't get post ID");
  60. echo '</li>';
  61. break;
  62. }
  63. }
  64. preg_match_all('|<comment>(.*?)</comment>|is', $post, $comments);
  65. $comments = $comments[1];
  66. if ( $comments ) {
  67. $comment_post_ID = (int) $post_id;
  68. $num_comments = 0;
  69. foreach ($comments as $comment) {
  70. preg_match('|<event>(.*?)</event>|is', $comment, $comment_content);
  71. $comment_content = str_replace(array ('<![CDATA[', ']]>'), '', trim($comment_content[1]));
  72. $comment_content = $this->unhtmlentities($comment_content);
  73. // Clean up content
  74. $comment_content = preg_replace('|<(/?[A-Z]+)|e', "'<' . strtolower('$1')", $comment_content);
  75. $comment_content = str_replace('<br>', '<br />', $comment_content);
  76. $comment_content = str_replace('<hr>', '<hr />', $comment_content);
  77. $comment_content = $wpdb->escape($comment_content);
  78. preg_match('|<eventtime>(.*?)</eventtime>|is', $comment, $comment_date);
  79. $comment_date = trim($comment_date[1]);
  80. $comment_date = date('Y-m-d H:i:s', strtotime($comment_date));
  81. preg_match('|<name>(.*?)</name>|is', $comment, $comment_author);
  82. $comment_author = $wpdb->escape(trim($comment_author[1]));
  83. preg_match('|<email>(.*?)</email>|is', $comment, $comment_author_email);
  84. $comment_author_email = $wpdb->escape(trim($comment_author_email[1]));
  85. $comment_approved = 1;
  86. // Check if it's already there
  87. if (!comment_exists($comment_author, $comment_date)) {
  88. $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_email', 'comment_date', 'comment_content', 'comment_approved');
  89. $commentdata = wp_filter_comment($commentdata);
  90. wp_insert_comment($commentdata);
  91. $num_comments++;
  92. }
  93. }
  94. }
  95. if ( $num_comments ) {
  96. echo ' ';
  97. printf(__('(%s comments)'), $num_comments);
  98. }
  99. echo '</li>';
  100. flush();
  101. ob_flush();
  102. }
  103. echo '</ol>';
  104. }
  105. function import() {
  106. $file = wp_import_handle_upload();
  107. if ( isset($file['error']) ) {
  108. echo $file['error'];
  109. return;
  110. }
  111. $this->file = $file['file'];
  112. $this->import_posts();
  113. wp_import_cleanup($file['id']);
  114. echo '<h3>';
  115. printf(__('All done. <a href="%s">Have fun!</a>'), get_option('home'));
  116. echo '</h3>';
  117. }
  118. function dispatch() {
  119. if (empty ($_GET['step']))
  120. $step = 0;
  121. else
  122. $step = (int) $_GET['step'];
  123. $this->header();
  124. switch ($step) {
  125. case 0 :
  126. $this->greet();
  127. break;
  128. case 1 :
  129. $this->import();
  130. break;
  131. }
  132. $this->footer();
  133. }
  134. function LJ_Import() {
  135. // Nothing.
  136. }
  137. }
  138. $livejournal_import = new LJ_Import();
  139. register_importer('livejournal', __('LiveJournal'), __('Import posts from LiveJournal'), array ($livejournal_import, 'dispatch'));
  140. ?>