PageRenderTime 35ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/branches/2.2/wp-admin/import/wordpress.php

#
PHP | 408 lines | 330 code | 66 blank | 12 comment | 44 complexity | 2ba9c60472817c044cd3b151c218a007 MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.0, LGPL-2.1, GPL-2.0
  1. <?php
  2. class WP_Import {
  3. var $posts = array ();
  4. var $posts_processed = array ();
  5. // Array of arrays. [[0] => XML fragment, [1] => New post ID]
  6. var $file;
  7. var $id;
  8. var $mtnames = array ();
  9. var $newauthornames = array ();
  10. var $j = -1;
  11. function header() {
  12. echo '<div class="wrap">';
  13. echo '<h2>'.__('Import WordPress').'</h2>';
  14. }
  15. function footer() {
  16. echo '</div>';
  17. }
  18. function unhtmlentities($string) { // From php.net for < 4.3 compat
  19. $trans_tbl = get_html_translation_table(HTML_ENTITIES);
  20. $trans_tbl = array_flip($trans_tbl);
  21. return strtr($string, $trans_tbl);
  22. }
  23. function greet() {
  24. echo '<div class="narrow">';
  25. echo '<p>'.__('Howdy! Upload your WordPress eXtended RSS (WXR) file and we&#8217;ll import the posts, comments, custom fields, and categories into this blog.').'</p>';
  26. echo '<p>'.__('Choose a WordPress WXR file to upload, then click Upload file and import.').'</p>';
  27. wp_import_upload_form("admin.php?import=wordpress&amp;step=1");
  28. echo '</div>';
  29. }
  30. function get_tag( $string, $tag ) {
  31. global $wpdb;
  32. preg_match("|<$tag.*?>(.*?)</$tag>|is", $string, $return);
  33. $return = preg_replace('|^<!\[CDATA\[(.*)\]\]>$|s', '$1', $return[1]);
  34. $return = $wpdb->escape( trim( $return ) );
  35. return $return;
  36. }
  37. function users_form($n) {
  38. global $wpdb, $testing;
  39. $users = $wpdb->get_results("SELECT * FROM $wpdb->users ORDER BY ID");
  40. ?><select name="userselect[<?php echo $n; ?>]">
  41. <option value="#NONE#">- Select -</option>
  42. <?php
  43. foreach ($users as $user) {
  44. echo '<option value="'.$user->user_login.'">'.$user->user_login.'</option>';
  45. }
  46. ?>
  47. </select>
  48. <?php
  49. }
  50. //function to check the authorname and do the mapping
  51. function checkauthor($author) {
  52. global $wpdb;
  53. //mtnames is an array with the names in the mt import file
  54. $pass = 'changeme';
  55. if (!(in_array($author, $this->mtnames))) { //a new mt author name is found
  56. ++ $this->j;
  57. $this->mtnames[$this->j] = $author; //add that new mt author name to an array
  58. $user_id = username_exists($this->newauthornames[$this->j]); //check if the new author name defined by the user is a pre-existing wp user
  59. if (!$user_id) { //banging my head against the desk now.
  60. if ($this->newauthornames[$this->j] == 'left_blank') { //check if the user does not want to change the authorname
  61. $user_id = wp_create_user($author, $pass);
  62. $this->newauthornames[$this->j] = $author; //now we have a name, in the place of left_blank.
  63. } else {
  64. $user_id = wp_create_user($this->newauthornames[$this->j], $pass);
  65. }
  66. } else {
  67. return $user_id; // return pre-existing wp username if it exists
  68. }
  69. } else {
  70. $key = array_search($author, $this->mtnames); //find the array key for $author in the $mtnames array
  71. $user_id = username_exists($this->newauthornames[$key]); //use that key to get the value of the author's name from $newauthornames
  72. }
  73. return $user_id;
  74. }
  75. function get_entries() {
  76. set_magic_quotes_runtime(0);
  77. $importdata = array_map('rtrim', file($this->file)); // Read the file into an array
  78. $this->posts = array();
  79. $this->categories = array();
  80. $num = 0;
  81. $doing_entry = false;
  82. foreach ($importdata as $importline) {
  83. if ( false !== strpos($importline, '<wp:category>') ) {
  84. preg_match('|<wp:category>(.*?)</wp:category>|is', $importline, $category);
  85. $this->categories[] = $category[1];
  86. continue;
  87. }
  88. if ( false !== strpos($importline, '<item>') ) {
  89. $this->posts[$num] = '';
  90. $doing_entry = true;
  91. continue;
  92. }
  93. if ( false !== strpos($importline, '</item>') ) {
  94. $num++;
  95. $doing_entry = false;
  96. continue;
  97. }
  98. if ( $doing_entry ) {
  99. $this->posts[$num] .= $importline . "\n";
  100. }
  101. }
  102. foreach ($this->posts as $post) {
  103. $post_ID = (int) $this->get_tag( $post, 'wp:post_id' );
  104. if ($post_ID) {
  105. $this->posts_processed[$post_ID][0] = &$post;
  106. $this->posts_processed[$post_ID][1] = 0;
  107. }
  108. }
  109. }
  110. function get_wp_authors() {
  111. $temp = array ();
  112. $i = -1;
  113. foreach ($this->posts as $post) {
  114. if ('' != trim($post)) {
  115. ++ $i;
  116. $author = $this->get_tag( $post, 'dc:creator' );
  117. array_push($temp, "$author"); //store the extracted author names in a temporary array
  118. }
  119. }
  120. // We need to find unique values of author names, while preserving the order, so this function emulates the unique_value(); php function, without the sorting.
  121. $authors[0] = array_shift($temp);
  122. $y = count($temp) + 1;
  123. for ($x = 1; $x < $y; $x ++) {
  124. $next = array_shift($temp);
  125. if (!(in_array($next, $authors)))
  126. array_push($authors, "$next");
  127. }
  128. return $authors;
  129. }
  130. function get_authors_from_post() {
  131. $formnames = array ();
  132. $selectnames = array ();
  133. foreach ($_POST['user'] as $key => $line) {
  134. $newname = trim(stripslashes($line));
  135. if ($newname == '')
  136. $newname = 'left_blank'; //passing author names from step 1 to step 2 is accomplished by using POST. left_blank denotes an empty entry in the form.
  137. array_push($formnames, "$newname");
  138. } // $formnames is the array with the form entered names
  139. foreach ($_POST['userselect'] as $user => $key) {
  140. $selected = trim(stripslashes($key));
  141. array_push($selectnames, "$selected");
  142. }
  143. $count = count($formnames);
  144. for ($i = 0; $i < $count; $i ++) {
  145. if ($selectnames[$i] != '#NONE#') { //if no name was selected from the select menu, use the name entered in the form
  146. array_push($this->newauthornames, "$selectnames[$i]");
  147. } else {
  148. array_push($this->newauthornames, "$formnames[$i]");
  149. }
  150. }
  151. }
  152. function wp_authors_form() {
  153. ?>
  154. <h2><?php _e('Assign Authors'); ?></h2>
  155. <p><?php _e('To make it easier for you to edit and save the imported posts and drafts, you may want to change the name of the author of the posts. For example, you may want to import all the entries as <code>admin</code>s entries.'); ?></p>
  156. <p><?php _e('If a new user is created by WordPress, the password will be set, by default, to "changeme". Quite suggestive, eh? ;)'); ?></p>
  157. <?php
  158. $authors = $this->get_wp_authors();
  159. echo '<ol id="authors">';
  160. echo '<form action="?import=wordpress&amp;step=2&amp;id=' . $this->id . '" method="post">';
  161. wp_nonce_field('import-wordpress');
  162. $j = -1;
  163. foreach ($authors as $author) {
  164. ++ $j;
  165. echo '<li>'.__('Current author:').' <strong>'.$author.'</strong><br />'.sprintf(__('Create user %1$s or map to existing'), ' <input type="text" value="'.$author.'" name="'.'user[]'.'" maxlength="30"> <br />');
  166. $this->users_form($j);
  167. echo '</li>';
  168. }
  169. echo '<input type="submit" value="Submit">'.'<br/>';
  170. echo '</form>';
  171. echo '</ol>';
  172. }
  173. function select_authors() {
  174. $file = wp_import_handle_upload();
  175. if ( isset($file['error']) ) {
  176. echo '<p>'.__('Sorry, there has been an error.').'</p>';
  177. echo '<p><strong>' . $file['error'] . '</strong></p>';
  178. return;
  179. }
  180. $this->file = $file['file'];
  181. $this->id = (int) $file['id'];
  182. $this->get_entries();
  183. $this->wp_authors_form();
  184. }
  185. function process_categories() {
  186. global $wpdb;
  187. $cat_names = (array) $wpdb->get_col("SELECT cat_name FROM $wpdb->categories");
  188. while ( $c = array_shift($this->categories) ) {
  189. $cat_name = trim($this->get_tag( $c, 'wp:cat_name' ));
  190. // If the category exists we leave it alone
  191. if ( in_array($cat_name, $cat_names) )
  192. continue;
  193. $category_nicename = $this->get_tag( $c, 'wp:category_nicename' );
  194. $posts_private = (int) $this->get_tag( $c, 'wp:posts_private' );
  195. $links_private = (int) $this->get_tag( $c, 'wp:links_private' );
  196. $parent = $this->get_tag( $c, 'wp:category_parent' );
  197. if ( empty($parent) )
  198. $category_parent = '0';
  199. else
  200. $category_parent = category_exists($parent);
  201. $catarr = compact('category_nicename', 'category_parent', 'posts_private', 'links_private', 'posts_private', 'cat_name');
  202. $cat_ID = wp_insert_category($catarr);
  203. }
  204. }
  205. function process_posts() {
  206. $i = -1;
  207. echo '<ol>';
  208. foreach ($this->posts as $post)
  209. $this->process_post($post);
  210. echo '</ol>';
  211. wp_import_cleanup($this->id);
  212. echo '<h3>'.sprintf(__('All done.').' <a href="%s">'.__('Have fun!').'</a>', get_option('home')).'</h3>';
  213. }
  214. function process_post($post) {
  215. global $wpdb;
  216. $post_ID = (int) $this->get_tag( $post, 'wp:post_id' );
  217. if ( $post_ID && !empty($this->posts_processed[$post_ID][1]) ) // Processed already
  218. return 0;
  219. // There are only ever one of these
  220. $post_title = $this->get_tag( $post, 'title' );
  221. $post_date = $this->get_tag( $post, 'wp:post_date' );
  222. $post_date_gmt = $this->get_tag( $post, 'wp:post_date_gmt' );
  223. $comment_status = $this->get_tag( $post, 'wp:comment_status' );
  224. $ping_status = $this->get_tag( $post, 'wp:ping_status' );
  225. $post_status = $this->get_tag( $post, 'wp:status' );
  226. $post_name = $this->get_tag( $post, 'wp:post_name' );
  227. $post_parent = $this->get_tag( $post, 'wp:post_parent' );
  228. $menu_order = $this->get_tag( $post, 'wp:menu_order' );
  229. $post_type = $this->get_tag( $post, 'wp:post_type' );
  230. $guid = $this->get_tag( $post, 'guid' );
  231. $post_author = $this->get_tag( $post, 'dc:creator' );
  232. $post_content = $this->get_tag( $post, 'content:encoded' );
  233. $post_content = preg_replace('|<(/?[A-Z]+)|e', "'<' . strtolower('$1')", $post_content);
  234. $post_content = str_replace('<br>', '<br />', $post_content);
  235. $post_content = str_replace('<hr>', '<hr />', $post_content);
  236. preg_match_all('|<category>(.*?)</category>|is', $post, $categories);
  237. $categories = $categories[1];
  238. $cat_index = 0;
  239. foreach ($categories as $category) {
  240. $categories[$cat_index] = $wpdb->escape($this->unhtmlentities(str_replace(array ('<![CDATA[', ']]>'), '', $category)));
  241. $cat_index++;
  242. }
  243. if ($post_id = post_exists($post_title, '', $post_date)) {
  244. echo '<li>';
  245. printf(__('Post <i>%s</i> already exists.'), stripslashes($post_title));
  246. } else {
  247. // If it has parent, process parent first.
  248. $post_parent = (int) $post_parent;
  249. if ($parent = $this->posts_processed[$post_parent]) {
  250. if (!$parent[1]) $this->process_post($parent[0]); // If not yet, process the parent first.
  251. $post_parent = $parent[1]; // New ID of the parent;
  252. }
  253. echo '<li>';
  254. printf(__('Importing post <i>%s</i>...'), stripslashes($post_title));
  255. $post_author = $this->checkauthor($post_author); //just so that if a post already exists, new users are not created by checkauthor
  256. $postdata = compact('post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_excerpt', 'post_status', 'post_name', 'comment_status', 'ping_status', 'post_modified', 'post_modified_gmt', 'guid', 'post_parent', 'menu_order', 'post_type');
  257. $comment_post_ID = $post_id = wp_insert_post($postdata);
  258. // Memorize old and new ID.
  259. if ( $post_id && $post_ID && $this->posts_processed[$post_ID] )
  260. $this->posts_processed[$post_ID][1] = $post_id; // New ID.
  261. // Add categories.
  262. if (count($categories) > 0) {
  263. $post_cats = array();
  264. foreach ($categories as $category) {
  265. $cat_ID = (int) $wpdb->get_var("SELECT cat_ID FROM $wpdb->categories WHERE cat_name = '$category'");
  266. if ($cat_ID == 0) {
  267. $cat_ID = wp_insert_category(array('cat_name' => $category));
  268. }
  269. $post_cats[] = $cat_ID;
  270. }
  271. wp_set_post_categories($post_id, $post_cats);
  272. }
  273. }
  274. // Now for comments
  275. preg_match_all('|<wp:comment>(.*?)</wp:comment>|is', $post, $comments);
  276. $comments = $comments[1];
  277. $num_comments = 0;
  278. if ( $comments) { foreach ($comments as $comment) {
  279. $comment_author = $this->get_tag( $comment, 'wp:comment_author');
  280. $comment_author_email = $this->get_tag( $comment, 'wp:comment_author_email');
  281. $comment_author_IP = $this->get_tag( $comment, 'wp:comment_author_IP');
  282. $comment_author_url = $this->get_tag( $comment, 'wp:comment_author_url');
  283. $comment_date = $this->get_tag( $comment, 'wp:comment_date');
  284. $comment_date_gmt = $this->get_tag( $comment, 'wp:comment_date_gmt');
  285. $comment_content = $this->get_tag( $comment, 'wp:comment_content');
  286. $comment_approved = $this->get_tag( $comment, 'wp:comment_approved');
  287. $comment_type = $this->get_tag( $comment, 'wp:comment_type');
  288. $comment_parent = $this->get_tag( $comment, 'wp:comment_parent');
  289. if ( !comment_exists($comment_author, $comment_date) ) {
  290. $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_url', 'comment_author_email', 'comment_author_IP', 'comment_date', 'comment_date_gmt', 'comment_content', 'comment_approved', 'comment_type', 'comment_parent');
  291. wp_insert_comment($commentdata);
  292. $num_comments++;
  293. }
  294. } }
  295. if ( $num_comments )
  296. printf(' '.__('(%s comments)'), $num_comments);
  297. // Now for post meta
  298. preg_match_all('|<wp:postmeta>(.*?)</wp:postmeta>|is', $post, $postmeta);
  299. $postmeta = $postmeta[1];
  300. if ( $postmeta) { foreach ($postmeta as $p) {
  301. $key = $this->get_tag( $p, 'wp:meta_key' );
  302. $value = $this->get_tag( $p, 'wp:meta_value' );
  303. $value = stripslashes($value); // add_post_meta() will escape.
  304. add_post_meta( $post_id, $key, $value );
  305. } }
  306. }
  307. function import() {
  308. $this->id = (int) $_GET['id'];
  309. $this->file = get_attached_file($this->id);
  310. $this->get_authors_from_post();
  311. $this->get_entries();
  312. $this->process_categories();
  313. $this->process_posts();
  314. }
  315. function dispatch() {
  316. if (empty ($_GET['step']))
  317. $step = 0;
  318. else
  319. $step = (int) $_GET['step'];
  320. $this->header();
  321. switch ($step) {
  322. case 0 :
  323. $this->greet();
  324. break;
  325. case 1 :
  326. check_admin_referer('import-upload');
  327. $this->select_authors();
  328. break;
  329. case 2:
  330. check_admin_referer('import-wordpress');
  331. $this->import();
  332. break;
  333. }
  334. $this->footer();
  335. }
  336. function WP_Import() {
  337. // Nothing.
  338. }
  339. }
  340. $wp_import = new WP_Import();
  341. register_importer('wordpress', 'WordPress', __('Import <strong>posts, comments, custom fields, pages, and categories</strong> from a WordPress export file'), array ($wp_import, 'dispatch'));
  342. ?>