Changeset 1332618
- Timestamp:
- 01/21/2016 07:11:25 AM (10 years ago)
- Location:
- tp2wp-importer/trunk
- Files:
-
- 5 edited
-
CHANGELOG.md (modified) (1 diff)
-
attachments/functions.php (modified) (2 diffs)
-
content/functions.php (modified) (3 diffs)
-
content/importer.php (modified) (5 diffs)
-
content/parsers.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
tp2wp-importer/trunk/CHANGELOG.md
r1202006 r1332618 1 1 TP2WP Wordpress plugin Changelog 2 2 === 3 4 1.0.12 5 --- 6 * When multiple TypePad posts have the same post name, we now generate 7 a new unique postname for the duplicately named post(s). This means that 8 all posts will be imported those, even those sharing the same post name / 9 slug (though some incoming links will be broken). 10 * Small optimization to avoid needing to download duplicate linked to files. 11 * Small formatting fixes / corrections 12 13 1.0.11 14 --- 15 * Avoid trying to perform redundant symlinks / copies to existing files 16 when creating the legacy (ie /.a./<hash>) style links. 3 17 4 18 1.0.10 5 19 --- 6 20 * Handle rewriting links to popup versions of images within the plugin 7 (previously was only handled in the [co version](https://convert.tp2wp.com/)21 (previously was only handled in the [conversion](https://convert.tp2wp.com/) 8 22 process. 9 23 * Remove some redundant checking on remote mimetypes, for some quicker -
tp2wp-importer/trunk/attachments/functions.php
r1202006 r1332618 169 169 // local filesystem / OS supports. 170 170 $second_version_path = $alt_upload_path . DIRECTORY_SEPARATOR . $imported_file_filename; 171 $alt_version_func( $relative_path_to_upload, $second_version_path ); 171 172 // But make sure we're not trying to symlink (or equiv) ontop 173 // of an existing file, since the same file could be linked 174 // to elsewhere already. 175 if ( ! is_file( $second_version_path ) && ! is_link( $second_version_path ) ) { 176 $alt_version_func( $relative_path_to_upload, $second_version_path ); 177 } 172 178 } 173 179 … … 484 490 485 491 $local_filename = tp2wp_importer_attachments_generate_local_filename( $url ); 492 $upload_info = wp_upload_dir( date( 'Y/m', $date ) ); 493 $possible_existing_file = $upload_info['path'] . '/' . $local_filename; 494 $possible_existing_url = $upload_info['url'] . '/' . $local_filename; 495 496 // If this file already exists on the disk, then we can save having to 497 // fetch it agian, and just use the existing version. 498 if ( is_file( $possible_existing_file ) && filesize( $possible_existing_file ) > 0 ) { 499 return array( true, array( $possible_existing_file, $possible_existing_url ) ); 500 } 486 501 487 502 $response = wp_remote_get( $url, array( 'timeout' => 30 ) ); -
tp2wp-importer/trunk/content/functions.php
r1201964 r1332618 3 3 /** 4 4 * Checks to see if a post already exits in the database that matches 5 * a given set of parameters. This is meant to be a replacement5 * a given posts name. This is meant to be a replacement 6 6 * for wordpress's `post_exists` function, which gives too many false 7 7 * positivies for our purposes. 8 8 * 9 9 * @param string $name 10 * The possible `post_name` of a post 10 * The possible `post_name` of a post 11 11 * 12 12 * @return int|NULL … … 14 14 * and otherwise NULL. 15 15 */ 16 function tp2wp_importer_content_post_ exists ($name) {16 function tp2wp_importer_content_post_name_exists ($name) { 17 17 18 18 global $wpdb; 19 19 20 $args = array( $name);20 $args = array( $name ); 21 21 $query = " 22 22 SELECT 23 23 ID 24 FROM 24 FROM 25 25 $wpdb->posts 26 26 WHERE … … 29 29 1"; 30 30 31 $post_id = (int) $wpdb->get_var( $wpdb->prepare( $query, $args) );32 return ( $post_id === 0 ) ? NULL: $post_id;31 $post_id = (int) $wpdb->get_var( $wpdb->prepare( $query, $args ) ); 32 return ( $post_id === 0 ) ? null : $post_id; 33 33 } 34 34 35 36 /** 37 * Checks to see if a post already exists in the database that matches 38 * the given post name, date and status. 39 * 40 * This is a more rigerious test than the above 41 * `tp2wp_importer_content_post_name_exists` function. This function is used to 42 * see if the same content has been imported, the above function is used 43 * to check if the post's name is unique. 44 * 45 * @param string $name 46 * The possible `post_name` of a post 47 * @param string $post_date 48 * The date a post was created, such as (ex 2010-06-28 08:45:15) 49 * @param string $status 50 * Either "publish" or "draft", describing the status of a post to test. 51 * 52 * @return boolean 53 * Returns "true" if a post matching the above parameters exists, and 54 * otherwise "false". 55 */ 56 function tp2wp_importer_content_post_exists ($name, $post_date, $status) { 57 58 global $wpdb; 59 60 $args = array( $name, $post_date, $status ); 61 $query = " 62 SELECT 63 ID 64 FROM 65 $wpdb->posts 66 WHERE 67 post_name = %s, 68 post_date = %s, 69 post_status = %s 70 LIMIT 71 1"; 72 73 $post_id = (int) $wpdb->get_var( $wpdb->prepare( $query, $args ) ); 74 return ( $post_id !== 0 ); 75 } 76 77 78 /** 79 * Generates a post name that is similar to the given name, but which 80 * is currently unique in the system. This is similar to the core 81 * `wp_unique_post_slug` function, but differs in that it doesn't require 82 * that the post already exist in the database (ie it doesn't require an 83 * existing database record). 84 * 85 * The funciton assumes that the existing post name does not exist, and 86 * attempts to find a new unique post name by appending -[1...1000] to the 87 * end of the name. 88 * 89 * @param string $name 90 * The possible `post_name` of a post 91 * 92 * @return string|null 93 * Identical to the output of `wp_unique_post_slug`, unless $name-[1...1000] 94 * are all already in the database, in which case null is returned. 95 */ 96 function tp2wp_importer_content_generate_unique_post_name ($name) { 97 98 $index = 0; 99 100 while ( ++$index <= 1000 ) { 101 $attempted_stub = $name . '-' . $index; 102 $existing_post_id = tp2wp_importer_content_post_name_exists( $attempted_stub ); 103 if ( $existing_post_id === null ) { 104 return $attempted_stub; 105 } 106 } 107 108 return null; 109 } -
tp2wp-importer/trunk/content/importer.php
r1192952 r1332618 525 525 $post_type_object = get_post_type_object( $post['post_type'] ); 526 526 527 $post_exists = tp2wp_importer_content_post_exists( $post['post_name'] ); 528 if ( $post_exists && get_post_type( $post_exists ) == $post['post_type'] ) { 529 printf( __('%s “%s” already exists.', 'tp2wp-importer'), $post_type_object->labels->singular_name, esc_html($post['post_title']) ); 530 echo '<br />'; 531 $comment_post_ID = $post_id = $post_exists; 532 } else { 527 $should_import_post = true; 528 $existing_post_id = tp2wp_importer_content_post_name_exists( $post['post_name'] ); 529 if ( $existing_post_id && get_post_type( $existing_post_id ) == $post['post_type'] ) { 530 531 // Here we need to disambiguate between two possible cases, where 532 // we're trying to reimport content that already exists in the database 533 // (in which case there will be an item in the database with the 534 // same post_date, post_type and status). If this is the case, 535 // then we don't want to process this item any further. We want 536 // to just re-use the existing post in the database. 537 // 538 // Otherwise, we need to rewrite the post name to be something unique 539 // for this post, so that it doesn't clobber the existing post. 540 // This will break incoming links, but at least the content will be 541 // imported. 542 $post_exists = tp2wp_importer_content_post_exists( $post['post_name'], $post['post_date'], $post['status'] ); 543 if ( $post_exists === true ) { 544 printf( 545 __('%s “%s” already exists.', 'tp2wp-importer'), 546 $post_type_object->labels->singular_name, 547 esc_html( $post['post_title'] ) 548 ); 549 echo '<br />'; 550 $comment_post_ID = $post_id = $existing_post_id; 551 $should_import_post = false; 552 553 } else { 554 555 $unique_stub = tp2wp_importer_content_generate_unique_post_name( $post['post_name'] ); 556 // If we weren't able to generate a unique stub of the post_name, then 557 // then we've done all we can do to try and import this post, 558 // so give up. 559 if ( $unique_stub === null ) { 560 printf( 561 __( 'Failed to import “%s”: Could not generate a unique post name for %s', 'tp2wp-importer' ), 562 esc_html( $post['post_title'] ), 563 esc_html( $post['post_name'] ) 564 ); 565 echo '<br />'; 566 continue; 567 } 568 569 $post['post_name'] = $unique_stub; 570 } 571 } 572 573 if ( $should_import_post === true ) { 533 574 $post_parent = (int) $post['post_parent']; 534 575 if ( $post_parent ) { … … 545 586 // map the post author 546 587 $author = sanitize_user( $post['post_author'], true ); 547 if ( isset( $this->author_mapping[$author] ) ) 588 if ( isset( $this->author_mapping[$author] ) ) { 548 589 $author = $this->author_mapping[$author]; 549 else590 } else { 550 591 $author = (int) get_current_user_id(); 592 } 551 593 552 594 $postdata = array( … … 560 602 ); 561 603 562 if ( 'attachment' == $postdata['post_type'] ) { 563 604 if ( 'attachment' === $postdata['post_type'] ) { 564 605 continue; 565 566 } else { 567 568 $comment_post_ID = $post_id = wp_insert_post( $postdata, true ); 569 570 } 606 } 607 608 $comment_post_ID = $post_id = wp_insert_post( $postdata, true ); 571 609 572 610 if ( is_wp_error( $post_id ) ) { … … 586 624 } 587 625 588 if ( $post['is_sticky'] == 1 ) 626 if ( $post['is_sticky'] == 1 ) { 589 627 stick_post( $post_id ); 628 } 590 629 } 591 630 … … 647 686 foreach ( $newcomments as $key => $comment ) { 648 687 // if this is a new post we can skip the comment_exists() check 649 if ( ! $ post_exists|| ! comment_exists( $comment['comment_author'], $comment['comment_date'] ) ) {688 if ( ! $existing_post_id || ! comment_exists( $comment['comment_author'], $comment['comment_date'] ) ) { 650 689 if ( isset( $inserted_comments[$comment['comment_parent']] ) ) 651 690 $comment['comment_parent'] = $inserted_comments[$comment['comment_parent']]; -
tp2wp-importer/trunk/content/parsers.php
r1192952 r1332618 111 111 112 112 $base_url = $xml->xpath('/rss/channel/wp:base_site_url'); 113 $base_url = ( string) trim( $base_url[0] );113 $base_url = (empty($base_url[0])) ? '' : (string) trim( $base_url[0] ); 114 114 115 115 $namespaces = $xml->getDocNamespaces();
Note: See TracChangeset
for help on using the changeset viewer.