web/wp-admin/import/wordpress.php
branchwordpress
changeset 132 4d4862461b8d
parent 109 03b0d1493584
equal deleted inserted replaced
131:a4642baaf829 132:4d4862461b8d
    25 	var $allauthornames = array ();
    25 	var $allauthornames = array ();
    26 
    26 
    27 	var $author_ids = array ();
    27 	var $author_ids = array ();
    28 	var $tags = array ();
    28 	var $tags = array ();
    29 	var $categories = array ();
    29 	var $categories = array ();
       
    30 	var $terms = array ();
    30 
    31 
    31 	var $j = -1;
    32 	var $j = -1;
    32 	var $fetch_attachments = false;
    33 	var $fetch_attachments = false;
    33 	var $url_remap = array ();
    34 	var $url_remap = array ();
    34 
    35 
   118 					continue;
   119 					continue;
   119 				}
   120 				}
   120 				if ( false !== strpos($importline, '<wp:tag>') ) {
   121 				if ( false !== strpos($importline, '<wp:tag>') ) {
   121 					preg_match('|<wp:tag>(.*?)</wp:tag>|is', $importline, $tag);
   122 					preg_match('|<wp:tag>(.*?)</wp:tag>|is', $importline, $tag);
   122 					$this->tags[] = $tag[1];
   123 					$this->tags[] = $tag[1];
       
   124 					continue;
       
   125 				}
       
   126 				if ( false !== strpos($importline, '<wp:term>') ) {
       
   127 					preg_match('|<wp:term>(.*?)</wp:term>|is', $importline, $term);
       
   128 					$this->terms[] = $term[1];
   123 					continue;
   129 					continue;
   124 				}
   130 				}
   125 				if ( false !== strpos($importline, '<item>') ) {
   131 				if ( false !== strpos($importline, '<item>') ) {
   126 					$this->post = '';
   132 					$this->post = '';
   127 					$doing_entry = true;
   133 					$doing_entry = true;
   196 
   202 
   197 	}
   203 	}
   198 
   204 
   199 	function wp_authors_form() {
   205 	function wp_authors_form() {
   200 ?>
   206 ?>
   201 <?php screen_icon(); ?>
       
   202 <h2><?php _e('Assign Authors'); ?></h2>
   207 <h2><?php _e('Assign Authors'); ?></h2>
   203 <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>
   208 <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>
   204 <?php
   209 <?php
   205 	if ( $this->allow_create_users() ) {
   210 	if ( $this->allow_create_users() ) {
   206 		echo '<p>'.__('If a new user is created by WordPress, a password will be randomly generated. Manually change the user&#8217;s details if necessary.')."</p>\n";
   211 		echo '<p>'.__('If a new user is created by WordPress, a password will be randomly generated. Manually change the user&#8217;s details if necessary.')."</p>\n";
   208 
   213 
   209 
   214 
   210 		$authors = $this->get_wp_authors();
   215 		$authors = $this->get_wp_authors();
   211 		echo '<form action="?import=wordpress&amp;step=2&amp;id=' . $this->id . '" method="post">';
   216 		echo '<form action="?import=wordpress&amp;step=2&amp;id=' . $this->id . '" method="post">';
   212 		wp_nonce_field('import-wordpress');
   217 		wp_nonce_field('import-wordpress');
   213 		echo '<ol id="authors">';
   218 ?>
       
   219 <ol id="authors">
       
   220 <?php
   214 		$j = -1;
   221 		$j = -1;
   215 		foreach ($authors as $author) {
   222 		foreach ($authors as $author) {
   216 			++ $j;
   223 			++ $j;
   217 			echo '<li>'.__('Import author:').' <strong>'.$author.'</strong><br />';
   224 			echo '<li>'.__('Import author:').' <strong>'.$author.'</strong><br />';
   218 			$this->users_form($j, $author);
   225 			$this->users_form($j, $author);
   220 		}
   227 		}
   221 
   228 
   222 		if ( $this->allow_fetch_attachments() ) {
   229 		if ( $this->allow_fetch_attachments() ) {
   223 ?>
   230 ?>
   224 </ol>
   231 </ol>
   225 <?php screen_icon(); ?>
       
   226 <h2><?php _e('Import Attachments'); ?></h2>
   232 <h2><?php _e('Import Attachments'); ?></h2>
   227 <p>
   233 <p>
   228 	<input type="checkbox" value="1" name="attachments" id="import-attachments" />
   234 	<input type="checkbox" value="1" name="attachments" id="import-attachments" />
   229 	<label for="import-attachments"><?php _e('Download and import file attachments') ?></label>
   235 	<label for="import-attachments"><?php _e('Download and import file attachments') ?></label>
   230 </p>
   236 </p>
   335 			$tagarr = compact('slug', 'description');
   341 			$tagarr = compact('slug', 'description');
   336 
   342 
   337 			$tag_ID = wp_insert_term($tag_name, 'post_tag', $tagarr);
   343 			$tag_ID = wp_insert_term($tag_name, 'post_tag', $tagarr);
   338 		}
   344 		}
   339 	}
   345 	}
       
   346 	
       
   347 	function process_terms() {
       
   348 		global $wpdb, $wp_taxonomies;
       
   349 		
       
   350 		$custom_taxonomies = $wp_taxonomies;
       
   351 		// get rid of the standard taxonomies
       
   352 		unset( $custom_taxonomies['category'] );
       
   353 		unset( $custom_taxonomies['post_tag'] );
       
   354 		unset( $custom_taxonomies['link_category'] );
       
   355 		
       
   356 		$custom_taxonomies = array_keys( $custom_taxonomies );
       
   357 		$current_terms = (array) get_terms( $custom_taxonomies, 'get=all' );
       
   358 		$taxonomies = array();
       
   359 		foreach ( $current_terms as $term ) {
       
   360 			if ( isset( $_terms[$term->taxonomy] ) ) {
       
   361 				$taxonomies[$term->taxonomy] = array_merge( $taxonomies[$term->taxonomy], array($term->name) );
       
   362 			} else {
       
   363 				$taxonomies[$term->taxonomy] = array($term->name);
       
   364 			}
       
   365 		}
       
   366 
       
   367 		while ( $c = array_shift($this->terms) ) {
       
   368 			$term_name = trim($this->get_tag( $c, 'wp:term_name' ));
       
   369 			$term_taxonomy = trim($this->get_tag( $c, 'wp:term_taxonomy' ));
       
   370 
       
   371 			// If the term exists in the taxonomy we leave it alone
       
   372 			if ( isset($taxonomies[$term_taxonomy] ) && in_array( $term_name, $taxonomies[$term_taxonomy] ) )
       
   373 				continue;
       
   374 
       
   375 			$slug = $this->get_tag( $c, 'wp:term_slug' );
       
   376 			$description = $this->get_tag( $c, 'wp:term_description' );
       
   377 
       
   378 			$termarr = compact('slug', 'description');
       
   379 
       
   380 			$term_ID = wp_insert_term($term_name, $this->get_tag( $c, 'wp:term_taxonomy' ), $termarr);
       
   381 		}
       
   382 	}
   340 
   383 
   341 	function process_author($post) {
   384 	function process_author($post) {
   342 		$author = $this->get_tag( $post, 'dc:creator' );
   385 		$author = $this->get_tag( $post, 'dc:creator' );
   343 		if ($author)
   386 		if ($author)
   344 			$this->allauthornames[] = $author;
   387 			$this->allauthornames[] = $author;
   353 
   396 
   354 		wp_import_cleanup($this->id);
   397 		wp_import_cleanup($this->id);
   355 		do_action('import_done', 'wordpress');
   398 		do_action('import_done', 'wordpress');
   356 
   399 
   357 		echo '<h3>'.sprintf(__('All done.').' <a href="%s">'.__('Have fun!').'</a>', get_option('home')).'</h3>';
   400 		echo '<h3>'.sprintf(__('All done.').' <a href="%s">'.__('Have fun!').'</a>', get_option('home')).'</h3>';
       
   401 	}
       
   402 
       
   403 	function _normalize_tag( $matches ) {
       
   404 		return '<' . strtolower( $matches[1] );
   358 	}
   405 	}
   359 
   406 
   360 	function process_post($post) {
   407 	function process_post($post) {
   361 		global $wpdb;
   408 		global $wpdb;
   362 
   409 
   376 		$post_name      = $this->get_tag( $post, 'wp:post_name' );
   423 		$post_name      = $this->get_tag( $post, 'wp:post_name' );
   377 		$post_parent    = $this->get_tag( $post, 'wp:post_parent' );
   424 		$post_parent    = $this->get_tag( $post, 'wp:post_parent' );
   378 		$menu_order     = $this->get_tag( $post, 'wp:menu_order' );
   425 		$menu_order     = $this->get_tag( $post, 'wp:menu_order' );
   379 		$post_type      = $this->get_tag( $post, 'wp:post_type' );
   426 		$post_type      = $this->get_tag( $post, 'wp:post_type' );
   380 		$post_password  = $this->get_tag( $post, 'wp:post_password' );
   427 		$post_password  = $this->get_tag( $post, 'wp:post_password' );
       
   428 		$is_sticky		= $this->get_tag( $post, 'wp:is_sticky' );
   381 		$guid           = $this->get_tag( $post, 'guid' );
   429 		$guid           = $this->get_tag( $post, 'guid' );
   382 		$post_author    = $this->get_tag( $post, 'dc:creator' );
   430 		$post_author    = $this->get_tag( $post, 'dc:creator' );
   383 
   431 
   384 		$post_excerpt = $this->get_tag( $post, 'excerpt:encoded' );
   432 		$post_excerpt = $this->get_tag( $post, 'excerpt:encoded' );
   385 		$post_excerpt = preg_replace_callback('|<(/?[A-Z]+)|', create_function('$match', 'return "<" . strtolower($match[1]);'), $post_excerpt);
   433 		$post_excerpt = preg_replace_callback('|<(/?[A-Z]+)|', array( &$this, '_normalize_tag' ), $post_excerpt);
   386 		$post_excerpt = str_replace('<br>', '<br />', $post_excerpt);
   434 		$post_excerpt = str_replace('<br>', '<br />', $post_excerpt);
   387 		$post_excerpt = str_replace('<hr>', '<hr />', $post_excerpt);
   435 		$post_excerpt = str_replace('<hr>', '<hr />', $post_excerpt);
   388 
   436 
   389 		$post_content = $this->get_tag( $post, 'content:encoded' );
   437 		$post_content = $this->get_tag( $post, 'content:encoded' );
   390 		$post_content = preg_replace_callback('|<(/?[A-Z]+)|', create_function('$match', 'return "<" . strtolower($match[1]);'), $post_content);
   438 		$post_content = preg_replace_callback('|<(/?[A-Z]+)|', array( &$this, '_normalize_tag' ), $post_content);
   391 		$post_content = str_replace('<br>', '<br />', $post_content);
   439 		$post_content = str_replace('<br>', '<br />', $post_content);
   392 		$post_content = str_replace('<hr>', '<hr />', $post_content);
   440 		$post_content = str_replace('<hr>', '<hr />', $post_content);
   393 
   441 
   394 		preg_match_all('|<category domain="tag">(.*?)</category>|is', $post, $tags);
   442 		preg_match_all('|<category domain="tag">(.*?)</category>|is', $post, $tags);
   395 		$tags = $tags[1];
   443 		$tags = $tags[1];
   446 					return $post_id;
   494 					return $post_id;
   447 			}
   495 			}
   448 			else {
   496 			else {
   449 				printf(__('Importing post <em>%s</em>...'), stripslashes($post_title));
   497 				printf(__('Importing post <em>%s</em>...'), stripslashes($post_title));
   450 				$comment_post_ID = $post_id = wp_insert_post($postdata);
   498 				$comment_post_ID = $post_id = wp_insert_post($postdata);
       
   499 				if ( $post_id && $is_sticky == 1 )
       
   500 					stick_post( $post_id );
       
   501 
   451 			}
   502 			}
   452 
   503 
   453 			if ( is_wp_error( $post_id ) )
   504 			if ( is_wp_error( $post_id ) )
   454 				return $post_id;
   505 				return $post_id;
   455 
   506 
   506 
   557 
   507 		// Now for comments
   558 		// Now for comments
   508 		preg_match_all('|<wp:comment>(.*?)</wp:comment>|is', $post, $comments);
   559 		preg_match_all('|<wp:comment>(.*?)</wp:comment>|is', $post, $comments);
   509 		$comments = $comments[1];
   560 		$comments = $comments[1];
   510 		$num_comments = 0;
   561 		$num_comments = 0;
   511 		if ( $comments) { foreach ($comments as $comment) {
   562 		$inserted_comments = array();
   512 			$comment_author       = $this->get_tag( $comment, 'wp:comment_author');
   563 		if ( $comments) { 
   513 			$comment_author_email = $this->get_tag( $comment, 'wp:comment_author_email');
   564 			foreach ($comments as $comment) {
   514 			$comment_author_IP    = $this->get_tag( $comment, 'wp:comment_author_IP');
   565 				$comment_id	= $this->get_tag( $comment, 'wp:comment_id');
   515 			$comment_author_url   = $this->get_tag( $comment, 'wp:comment_author_url');
   566 				$newcomments[$comment_id]['comment_post_ID']      = $comment_post_ID;
   516 			$comment_date         = $this->get_tag( $comment, 'wp:comment_date');
   567 				$newcomments[$comment_id]['comment_author']       = $this->get_tag( $comment, 'wp:comment_author');
   517 			$comment_date_gmt     = $this->get_tag( $comment, 'wp:comment_date_gmt');
   568 				$newcomments[$comment_id]['comment_author_email'] = $this->get_tag( $comment, 'wp:comment_author_email');
   518 			$comment_content      = $this->get_tag( $comment, 'wp:comment_content');
   569 				$newcomments[$comment_id]['comment_author_IP']    = $this->get_tag( $comment, 'wp:comment_author_IP');
   519 			$comment_approved     = $this->get_tag( $comment, 'wp:comment_approved');
   570 				$newcomments[$comment_id]['comment_author_url']   = $this->get_tag( $comment, 'wp:comment_author_url');
   520 			$comment_type         = $this->get_tag( $comment, 'wp:comment_type');
   571 				$newcomments[$comment_id]['comment_date']         = $this->get_tag( $comment, 'wp:comment_date');
   521 			$comment_parent       = $this->get_tag( $comment, 'wp:comment_parent');
   572 				$newcomments[$comment_id]['comment_date_gmt']     = $this->get_tag( $comment, 'wp:comment_date_gmt');
   522 
   573 				$newcomments[$comment_id]['comment_content']      = $this->get_tag( $comment, 'wp:comment_content');
   523 			// if this is a new post we can skip the comment_exists() check
   574 				$newcomments[$comment_id]['comment_approved']     = $this->get_tag( $comment, 'wp:comment_approved');
   524 			if ( !$post_exists || !comment_exists($comment_author, $comment_date) ) {
   575 				$newcomments[$comment_id]['comment_type']         = $this->get_tag( $comment, 'wp:comment_type');
   525 				$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');
   576 				$newcomments[$comment_id]['comment_parent'] 	  = $this->get_tag( $comment, 'wp:comment_parent');
   526 				wp_insert_comment($commentdata);
   577 			}
   527 				$num_comments++;
   578 			// Sort by comment ID, to make sure comment parents exist (if there at all)
   528 			}
   579 			ksort($newcomments);
   529 		} }
   580 			foreach ($newcomments as $key => $comment) {
       
   581 				// if this is a new post we can skip the comment_exists() check
       
   582 				if ( !$post_exists || !comment_exists($comment['comment_author'], $comment['comment_date']) ) {
       
   583 					if (isset($inserted_comments[$comment['comment_parent']]))
       
   584 						$comment['comment_parent'] = $inserted_comments[$comment['comment_parent']];
       
   585 					$comment = wp_filter_comment($comment);
       
   586 					$inserted_comments[$key] = wp_insert_comment($comment);
       
   587 					$num_comments++;
       
   588 				}
       
   589 			}
       
   590 		}
   530 
   591 
   531 		if ( $num_comments )
   592 		if ( $num_comments )
   532 			printf(' '._n('(%s comment)', '(%s comments)', $num_comments), $num_comments);
   593 			printf(' '._n('(%s comment)', '(%s comments)', $num_comments), $num_comments);
   533 
   594 
   534 		// Now for post meta
   595 		// Now for post meta
   738 		$this->get_authors_from_post();
   799 		$this->get_authors_from_post();
   739 		wp_suspend_cache_invalidation(true);
   800 		wp_suspend_cache_invalidation(true);
   740 		$this->get_entries();
   801 		$this->get_entries();
   741 		$this->process_categories();
   802 		$this->process_categories();
   742 		$this->process_tags();
   803 		$this->process_tags();
       
   804 		$this->process_terms();
   743 		$result = $this->process_posts();
   805 		$result = $this->process_posts();
   744 		wp_suspend_cache_invalidation(false);
   806 		wp_suspend_cache_invalidation(false);
   745 		$this->backfill_parents();
   807 		$this->backfill_parents();
   746 		$this->backfill_attachment_urls();
   808 		$this->backfill_attachment_urls();
   747 		$this->import_end();
   809 		$this->import_end();