Plugin Directory

Changeset 1332618


Ignore:
Timestamp:
01/21/2016 07:11:25 AM (10 years ago)
Author:
ReadyMadeWeb
Message:

Commit of version 1.0.12

Location:
tp2wp-importer/trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • tp2wp-importer/trunk/CHANGELOG.md

    r1202006 r1332618  
    11TP2WP Wordpress plugin Changelog
    22===
     3
     41.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
     131.0.11
     14---
     15  * Avoid trying to perform redundant symlinks / copies to existing files
     16    when creating the legacy (ie /.a./<hash>) style links.
    317
    4181.0.10
    519---
    620  * Handle rewriting links to popup versions of images within the plugin
    7     (previously was only handled in the [coversion](https://convert.tp2wp.com/)
     21    (previously was only handled in the [conversion](https://convert.tp2wp.com/)
    822    process.
    923  * Remove some redundant checking on remote mimetypes, for some quicker
  • tp2wp-importer/trunk/attachments/functions.php

    r1202006 r1332618  
    169169            // local filesystem / OS supports.
    170170            $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            }
    172178        }
    173179
     
    484490
    485491    $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    }
    486501
    487502    $response = wp_remote_get( $url, array( 'timeout' => 30 ) );
  • tp2wp-importer/trunk/content/functions.php

    r1201964 r1332618  
    33/**
    44 * 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 replacement
     5 * a given posts name.  This is meant to be a replacement
    66 * for wordpress's `post_exists` function, which gives too many false
    77 * positivies for our purposes.
    88 *
    99 * @param string $name
    10  *   The possible `post_name` of a post 
     10 *   The possible `post_name` of a post
    1111 *
    1212 * @return int|NULL
     
    1414 *   and otherwise NULL.
    1515 */
    16 function tp2wp_importer_content_post_exists ($name) {
     16function tp2wp_importer_content_post_name_exists ($name) {
    1717
    1818  global $wpdb;
    1919
    20   $args = array($name);
     20  $args = array( $name );
    2121  $query = "
    2222    SELECT
    2323      ID
    24     FROM 
     24    FROM
    2525      $wpdb->posts
    2626    WHERE
     
    2929      1";
    3030
    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;
    3333}
    3434
     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 */
     56function 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 */
     96function 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  
    525525            $post_type_object = get_post_type_object( $post['post_type'] );
    526526
    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 &#8220;%s&#8221; 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 &#8220;%s&#8221; 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 &#8220;%s&#8221;: 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 ) {
    533574                $post_parent = (int) $post['post_parent'];
    534575                if ( $post_parent ) {
     
    545586                // map the post author
    546587                $author = sanitize_user( $post['post_author'], true );
    547                 if ( isset( $this->author_mapping[$author] ) )
     588                if ( isset( $this->author_mapping[$author] ) ) {
    548589                    $author = $this->author_mapping[$author];
    549                 else
     590                } else {
    550591                    $author = (int) get_current_user_id();
     592                }
    551593
    552594                $postdata = array(
     
    560602                );
    561603
    562                 if ( 'attachment' == $postdata['post_type'] ) {
    563 
     604                if ( 'attachment' === $postdata['post_type'] ) {
    564605                    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 );
    571609
    572610                if ( is_wp_error( $post_id ) ) {
     
    586624                }
    587625
    588                 if ( $post['is_sticky'] == 1 )
     626                if ( $post['is_sticky'] == 1 ) {
    589627                    stick_post( $post_id );
     628                }
    590629            }
    591630
     
    647686                foreach ( $newcomments as $key => $comment ) {
    648687                    // 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'] ) ) {
    650689                        if ( isset( $inserted_comments[$comment['comment_parent']] ) )
    651690                            $comment['comment_parent'] = $inserted_comments[$comment['comment_parent']];
  • tp2wp-importer/trunk/content/parsers.php

    r1192952 r1332618  
    111111
    112112        $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] );
    114114
    115115        $namespaces = $xml->getDocNamespaces();
Note: See TracChangeset for help on using the changeset viewer.