diff --git a/wp-admin/import/livejournal.php b/wp-admin/import/livejournal.php index 441c85e9ac..0266e67c96 100644 --- a/wp-admin/import/livejournal.php +++ b/wp-admin/import/livejournal.php @@ -15,7 +15,7 @@ require_once( ABSPATH . WPINC . '/class-IXR.php' ); * * Imports your LiveJournal contents into WordPress using the LJ API * - * @since 2.7.1 + * @since 2.8 */ class LJ_API_Import { @@ -190,6 +190,7 @@ class LJ_API_Import {
+
@@ -236,7 +237,7 @@ class LJ_API_Import { lj_ixr( 'syncitems', array( 'ver' => 1, 'lastsync' => $lastsync ) ); - + if ( is_wp_error( $synclist ) ) + return $synclist; + // Keep track of if we've downloaded everything $total = $synclist['total']; $count = $synclist['count']; @@ -258,27 +261,31 @@ class LJ_API_Import { foreach ( $synclist['syncitems'] as $event ) { if ( substr( $event['item'], 0, 2 ) == 'L-' ) { $sync_item_times[ str_replace( 'L-', '', $event['item'] ) ] = $event['time']; - if ( $event['time'] > $lastsync ) + if ( $event['time'] > $lastsync ) { $lastsync = $event['time']; + update_option( 'ljapi_lastsync', $lastsync ); + } } } } while ( $total > $count ); // endwhile - all post meta is cached locally - + unset( $synclist ); update_option( 'ljapi_sync_item_times', $sync_item_times ); update_option( 'ljapi_total', $total ); update_option( 'ljapi_count', $count ); - update_option( 'ljapi_lastsync', $lastsync ); echo '' . __( 'Post metadata has been downloaded, proceeding with posts...' ) . '
'; - - echo '' . __( ' Comment metadata downloaded successfully, proceeding with posts...' ) . '
'; + echo '' . __( ' Comment metadata downloaded successfully, proceeding with comment bodies...' ) . '
'; return true; } @@ -472,7 +495,9 @@ class LJ_API_Import { // Inserts them all directly to the DB, with additional info stored in "spare" fields function download_comment_bodies() { global $wpdb; - $cookie = $this->getsession(); + $cookie = $this->get_session(); + if ( is_wp_error( $cookie ) ) + return $cookie; // Load previous state (if any) $this->usermap = (array) get_option( 'ljapi_usermap' ); @@ -603,8 +628,10 @@ class LJ_API_Import { // Gets the post_ID that a LJ post has been saved as within WP function get_wp_post_ID( $post ) { global $wpdb; + if ( empty( $this->postmap[$post] ) ) $this->postmap[$post] = (int) $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = 'lj_itemid' AND meta_value = %d", $post ) ); + return $this->postmap[$post]; } @@ -626,7 +653,7 @@ class LJ_API_Import { 'auth_challenge' => $challenge['challenge'], 'auth_response' => md5( $challenge['challenge'] . md5( $this->password ) ) ); } else { - return new WP_Error( 'IXR', __( 'LiveJournal does not appear to be responding right now. Please try again later.' ) ); + return new WP_Error( 'IXR', __( 'LiveJournal is not responding to authentication requests. Please wait a while and then try again.' ) ); } $args = func_get_args(); @@ -636,7 +663,7 @@ class LJ_API_Import { if ( $this->ixr->query( 'LJ.XMLRPC.' . $method, $params ) ) { return $this->ixr->getResponse(); } else { - return new WP_Error( 'IXR', __( 'XML-RPC Request Failed - ' ) . $this->ixr->getErrorCode() . ': ' . $this->ixr->getErrorMessage() ); + return new WP_Error( 'IXR', __( 'XML-RPC Request Failed -- ' ) . $this->ixr->getErrorCode() . ': ' . $this->ixr->getErrorMessage() ); } } @@ -656,22 +683,23 @@ class LJ_API_Import { $this->greet(); break; case 1 : - $this->ixr = new IXR_Client( $this->ixr_url ); - // Intentional no break case 2 : case 3 : check_admin_referer( 'lj-api-import' ); $result = $this->{ 'step' . $step }(); - if ( is_wp_error( $result ) ) - echo $result->get_error_message(); + if ( is_wp_error( $result ) ) { + $this->throw_error( $result, $step ); + } break; } $this->footer(); } - // Check form inputs and start importing posts - function step1() { + // Technically the first half of step 1, this is separated to allow for AJAX + // calls. Sets up some variables and options and confirms authentication. + function setup() { + global $verified; // Get details from form or from DB if ( !empty( $_POST['lj_username'] ) && !empty( $_POST['lj_password'] ) ) { // Store details for later @@ -698,21 +726,24 @@ class LJ_API_Import {and password so we can download your posts and comments.' ) ?>
lj_ixr( 'login' ); - if ( is_wp_error( $login ) ) { + $verified = $this->lj_ixr( 'login' ); + if ( is_wp_error( $verified ) ) { if ( 100 == $this->ixr->getErrorCode() || 101 == $this->ixr->getErrorCode() ) { delete_option( 'ljapi_username' ); delete_option( 'ljapi_password' ); + delete_option( 'ljapi_protected_password' ); ?> ixr ) $this->ixr = new IXR_Client( $this->ixr_url, false, 80, 30 ); + if ( empty( $_POST['login'] ) ) { + // We're looping -- load some details from DB + $this->username = get_option( 'ljapi_username' ); + $this->password = get_option( 'ljapi_password' ); + $this->protected_password = get_option( 'ljapi_protected_password' ); + } else { + // First run (non-AJAX) + $setup = $this->setup(); + if ( !$setup ) { + return false; + } else if ( is_wp_error( $setup ) ) { + $this->throw_error( $setup, 1 ); + return false; + } + } + + echo '' . __( "We're downloading and importing all your LiveJournal posts..." ) . '
'; + echo '' . __( "We're downloading and importing your LiveJournal posts..." ) . '
'; + if ( get_option( 'ljapi_post_batch' ) && count( get_option( 'ljapi_sync_item_times' ) ) ) { + $batch = count( get_option( 'ljapi_sync_item_times' ) ); + $batch = $count > 300 ? ceil( $batch / 300 ) : 1; + echo '' . sprintf( __( 'Imported post batch %d of approximately %d' ), ( get_option( 'ljapi_post_batch' ) + 1 ), $batch ) . '
'; + } ob_flush(); flush(); - // Now do the grunt work - set_time_limit( 0 ); - $result = $this->import_posts(); + if ( !get_option( 'ljapi_lastsync' ) || '1900-01-01 00:00:00' == get_option( 'ljapi_lastsync' ) ) { + // We haven't downloaded meta yet, so do that first + $result = $this->download_post_meta(); + if ( is_wp_error( $result ) ) { + $this->throw_error( $result, 1 ); + return false; + } + } + + // Download a batch of actual posts + $result = $this->download_post_bodies(); if ( is_wp_error( $result ) ) { if ( 406 == $this->ixr->getErrorCode() ) { ?>
next_step( 1, __( 'Try Again' ) ); + return false; } else { - return $result; + $this->throw_error( $result, 1 ); + return false; } } - - echo '
' . __( "Your posts have all been imported, but wait - there's more! Now we need to download & import your comments." ) . '
'; - echo $this->next_step( 2, __( 'Download my comments »' ) ); - $this->auto_submit(); + + if ( get_option( 'ljapi_last_sync_count' ) > 0 ) { + ?> + + auto_ajax( 'ljapi-auto-repost', 'auto-message', 0 ); ?> + ' . __( "Your posts have all been imported, but wait - there's more! Now we need to download & import your comments." ) . ''; + echo $this->next_step( 2, __( 'Download my comments »' ) ); + $this->auto_submit(); + } + echo '' . $error->get_error_message() . '
'; + echo $this->next_step( $step, __( 'Try Again' ) ); + } + // Returns the HTML for a link to the next page function next_step( $next_step, $label, $id = 'ljapi-next-form' ) { $str = '