Make WordPress Core


Ignore:
Timestamp:
03/18/2009 02:43:45 AM (17 years ago)
Author:
ryan
Message:

Trim trailing whitespace

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/import/livejournal.php

    r10647 r10810  
    3131    var $commentmap;
    3232    var $pointers = array();
    33    
     33
    3434    // This list taken from LJ, they don't appear to have an API for it
    3535    var $moods = array( '1' => 'aggravated',
     
    194194            <p><?php _e( 'Howdy! This importer allows you to connect directly to LiveJournal and download all your entries and comments' ) ?></p>
    195195            <p><?php _e( 'Enter your LiveJournal username and password below so we can connect to your account:' ) ?></p>
    196        
     196
    197197            <table class="form-table">
    198198
     
    206206            <td><input type="password" name="lj_password" id="lj_password" class="regular-text" /></td>
    207207            </tr>
    208      
     208
    209209            </table>
    210      
     210
    211211            <p><?php _e( 'If you have any entries on LiveJournal which are marked as private, they will be password-protected when they are imported so that only people who know the password can see them.' ) ?></p>
    212212            <p><?php _e( "If you don't enter a password, ALL ENTRIES from your LiveJournal will be imported as public posts in WordPress." ) ?></p>
     
    222222
    223223            <p><?php _e( "<strong>WARNING:</strong> This can take a really long time if you have a lot of entries in your LiveJournal, or a lot of comments. Ideally, you should only start this process if you can leave your computer alone while it finishes the import." ) ?></p>
    224        
     224
    225225            <p class="submit">
    226226                <input type="submit" class="button-primary" value="<?php echo attribute_escape( __( 'Connect to LiveJournal and Import' ) ) ?>" />
    227227            </p>
    228        
     228
    229229            <p><?php _e( '<strong>NOTE:</strong> If the import process is interrupted for <em>any</em> reason, come back to this page and it will continue from where it stopped automatically.' ) ?></p>
    230230
     
    237237        <?php
    238238    }
    239    
     239
    240240    function download_post_meta() {
    241241        $total           = (int) get_option( 'ljapi_total' );
     
    248248        if ( !is_array( $sync_item_times ) )
    249249            $sync_item_times = array();
    250        
     250
    251251        do {
    252252            $lastsync = date( 'Y-m-d H:i:s', strtotime( get_option( 'ljapi_lastsync' ) ) );
     
    254254            if ( is_wp_error( $synclist ) )
    255255                return $synclist;
    256                
     256
    257257            // Keep track of if we've downloaded everything
    258258            $total = $synclist['total'];
    259259            $count = $synclist['count'];
    260        
     260
    261261            foreach ( $synclist['syncitems'] as $event ) {
    262262                if ( substr( $event['item'], 0, 2 ) == 'L-' ) {
     
    277277        echo '<p>' . __( 'Post metadata has been downloaded, proceeding with posts...' ) . '</p>';
    278278    }
    279    
     279
    280280    function download_post_bodies() {
    281281        $imported_count  = (int) get_option( 'ljapi_imported_count' );
     
    286286
    287287        $count = 0;
    288         echo '<ol>';       
     288        echo '<ol>';
    289289        do {
    290290            $lastsync = date( 'Y-m-d H:i:s', strtotime( get_option( 'ljapi_lastsync_posts' ) ) );
    291            
     291
    292292            // Get the batch of items that match up with the syncitems list
    293293            $itemlist = $this->lj_ixr( 'getevents', array( 'ver' => 1,
     
    297297            if ( is_wp_error( $itemlist ) )
    298298                return $itemlist;
    299            
     299
    300300            if ( $num = count( $itemlist['events'] ) ) {
    301301                for ( $e = 0; $e < count( $itemlist['events'] ); $e++ ) {
     
    315315            $count++;
    316316        } while ( $num > 0 && $count < 3 ); // Doing up to 3 requests at a time to avoid memory problems
    317        
     317
    318318        // Used so that step1 knows when to stop posting back on itself
    319319        update_option( 'ljapi_last_sync_count', $num );
    320        
     320
    321321        // Counter just used to show progress to user
    322322        update_option( 'ljapi_post_batch', ( (int) get_option( 'ljapi_post_batch' ) + 1 ) );
     
    324324        echo '</ol>';
    325325    }
    326    
     326
    327327    function import_post( $post ) {
    328328        global $wpdb;
    329        
     329
    330330        // Make sure we haven't already imported this one
    331331        if ( $this->get_wp_post_ID( $post['itemid'] ) )
    332332            return;
    333        
     333
    334334        $user = wp_get_current_user();
    335335        $post_author      = $user->ID;
     
    342342        if ( 18 == strlen( $post_date ) )
    343343            $post_date = substr( $post_date, 0, 10 ) . ' ' . substr( $post_date, 10 );
    344        
     344
    345345        // Cleaning up and linking the title
    346346        $post_title = isset( $post['subject'] ) ? trim( $post['subject'] ) : '';
     
    348348        $post_title = strip_tags( $post_title ); // Can't have tags in the title in WP
    349349        $post_title = $wpdb->escape( $post_title );
    350        
     350
    351351        // Clean up content
    352352        $post_content = $post['event'];
     
    364364        $post_content = force_balance_tags( $post_content );
    365365        $post_content = $wpdb->escape( $post_content );
    366        
     366
    367367        // Handle any tags associated with the post
    368368        $tags_input = !empty( $post['props']['taglist'] ) ? $post['props']['taglist'] : '';
    369        
     369
    370370        // Check if comments are closed on this post
    371371        $comment_status = !empty( $post['props']['opt_nocomments'] ) ? 'closed' : 'open';
     
    388388                return new WP_Error( 'insert_post_failed', __( 'Failed to create post.' ) );
    389389            }
    390            
     390
    391391            // Handle all the metadata for this post
    392392            $this->insert_postmeta( $post_id, $post );
     
    394394        echo '</li>';
    395395    }
    396    
     396
    397397    // Convert lj-user tags to links to that user
    398398    function translate_lj_user( $str ) {
    399399        return preg_replace( '|<lj\s+user\s*=\s*["\']([\w-]+)["\']>|', '<a href="http://$1.livejournal.com/" class="lj-user">$1</a>', $str );
    400400    }
    401    
     401
    402402    function insert_postmeta( $post_id, $post ) {
    403403        // Need the original LJ id for comments
    404404        add_post_meta( $post_id, 'lj_itemid', $post['itemid'] );
    405        
     405
    406406        // And save the permalink on LJ in case we want to link back or something
    407407        add_post_meta( $post_id, 'lj_permalink', $post['url'] );
    408        
     408
    409409        // Supports the following "props" from LJ, saved as lj_<prop_name> in wp_postmeta
    410410        //      Adult Content - adult_content
     
    425425        }
    426426    }
    427    
     427
    428428    // Set up a session (authenticate) with LJ
    429429    function get_session() {
     
    434434        return new WP_Http_Cookie( array( 'name' => 'ljsession', 'value' => $cookie['ljsession'] ) );
    435435    }
    436    
     436
    437437    // Loops through and gets comment meta from LJ in batches
    438438    function download_comment_meta() {
     
    440440        if ( is_wp_error( $cookie ) )
    441441            return $cookie;
    442        
     442
    443443        // Load previous state (if any)
    444444        $this->usermap = (array) get_option( 'ljapi_usermap' );
    445445        $maxid         = get_option( 'ljapi_maxid' ) ? get_option( 'ljapi_maxid' ) : 1;
    446446        $highest_id    = get_option( 'ljapi_highest_id' ) ? get_option( 'ljapi_highest_id' ) : 0;
    447        
     447
    448448        // We need to loop over the metadata request until we have it all
    449449        while ( $maxid > $highest_id ) {
     
    453453            if ( is_wp_error( $results ) )
    454454                return new WP_Error( 'comment_meta', __( 'Failed to retrieve comment meta information from LiveJournal. Please try again soon.' ) );
    455            
     455
    456456            $results = wp_remote_retrieve_body( $results );
    457            
     457
    458458            // Get the maxid so we know if we have them all yet
    459459            preg_match( '|<maxid>(\d+)</maxid>|', $results, $matches );
     
    466466            }
    467467            $maxid = !empty( $matches[1] ) ? $matches[1] : $maxid;
    468            
     468
    469469            // Parse comments and get highest id available
    470470            preg_match_all( '|<comment id=\'(\d+)\'|is', $results, $matches );
     
    478478            foreach ( $matches[1] as $count => $userid )
    479479                $this->usermap[$userid] = $matches[2][$count]; // need this in memory for translating ids => names
    480                
     480
    481481            wp_cache_flush();
    482482        }
     
    486486        update_option( 'ljapi_maxid',      $maxid );
    487487        update_option( 'ljapi_highest_id', $highest_id );
    488        
     488
    489489        echo '<p>' . __( ' Comment metadata downloaded successfully, proceeding with comment bodies...' ) . '</p>';
    490        
     490
    491491        return true;
    492492    }
    493    
     493
    494494    // Downloads actual comment bodies from LJ
    495495    // Inserts them all directly to the DB, with additional info stored in "spare" fields
     
    499499        if ( is_wp_error( $cookie ) )
    500500            return $cookie;
    501        
     501
    502502        // Load previous state (if any)
    503503        $this->usermap = (array) get_option( 'ljapi_usermap' );
     
    507507        while ( $maxid > $highest_id && $loop < 5 ) { // We do 5 loops per call to avoid memory limits
    508508            $loop++;
    509            
     509
    510510            // Get a batch of comments, using the highest_id we've already got as a starting point
    511511            $results = wp_remote_get( $this->comments_url . '?get=comment_body&startid=' . ( $highest_id + 1 ),
     
    513513            if ( is_wp_error( $results ) )
    514514                return new WP_Error( 'comment_bodies', __( 'Failed to retrieve comment bodies from LiveJournal. Please try again soon.' ) );
    515            
     515
    516516            $results = wp_remote_retrieve_body( $results );
    517            
     517
    518518            // Parse out each comment and insert directly
    519519            preg_match_all( '|<comment id=\'(\d+)\'.*</comment>|iUs', $results, $matches );
     
    524524                    update_option( 'ljapi_highest_comment_id', $highest_id );
    525525                }
    526                    
     526
    527527                $comment = $matches[0][$c];
    528528
     
    537537                clean_comment_cache( $id );
    538538            }
    539            
     539
    540540            // Clear cache to preseve memory
    541541            wp_cache_flush();
    542542        }
    543543        // endwhile - all comments downloaded and ready for bulk processing
    544        
     544
    545545        // Counter just used to show progress to user
    546546        update_option( 'ljapi_comment_batch', ( (int) get_option( 'ljapi_comment_batch' ) + 1 ) );
    547        
     547
    548548        return true;
    549549    }
    550    
     550
    551551    // Takes a block of XML and parses out all the elements of the comment
    552552    function parse_comment( $comment ) {
    553553        global $wpdb;
    554        
     554
    555555        // Get the top-level attributes
    556556        preg_match( '|<comment([^>]+)>|i', $comment, $attribs );
     
    565565        preg_match( '| state=\'([SDFA])\'|i', $attribs[1], $matches ); // optional
    566566        $lj_comment_state = isset( $matches[1] ) ? $matches[1] : 'A';
    567        
     567
    568568        // Clean up "subject" - this will become the first line of the comment in WP
    569569        preg_match( '|<subject>(.*)</subject>|is', $comment, $matches );
     
    573573                $comment_subject = '';
    574574        }
    575        
     575
    576576        // Get the body and HTMLize it
    577577        preg_match( '|<body>(.*)</body>|is', $comment, $matches );
     
    584584        $comment_content = preg_replace_callback( '|<(/?[A-Z]+)|', create_function( '$match', 'return "<" . strtolower( $match[1] );' ), $comment_content );
    585585        $comment_content = $wpdb->escape( trim( $comment_content ) );
    586        
     586
    587587        // Get and convert the date
    588588        preg_match( '|<date>(.*)</date>|i', $comment, $matches );
    589589        $comment_date = trim( str_replace( array( 'T', 'Z' ), ' ', $matches[1] ) );
    590        
     590
    591591        // Grab IP if available
    592592        preg_match( '|<property name=\'poster_ip\'>(.*)</property>|i', $comment, $matches ); // optional
    593593        $comment_author_IP = isset( $matches[1] ) ? $matches[1] : '';
    594        
     594
    595595        // Try to get something useful for the comment author, especially if it was "my" comment
    596596        $author = ( empty( $comment_author_ID ) || empty( $this->usermap[$comment_author_ID] ) || substr( $this->usermap[$comment_author_ID], 0, 4 ) == 'ext_' ) ? __( 'Anonymous' ) : $this->usermap[$comment_author_ID];
     
    602602        } else {
    603603            $user_id = 0;
    604             $url     = ( __( 'Anonymous' ) == $author ) ? '' : 'http://' . $author . '.livejournal.com/';           
    605         }
    606        
     604            $url     = ( __( 'Anonymous' ) == $author ) ? '' : 'http://' . $author . '.livejournal.com/';
     605        }
     606
    607607        // Send back the array of details
    608608        return array( 'lj_comment_ID' => $lj_comment_ID,
     
    624624                    );
    625625    }
    626    
    627    
     626
     627
    628628    // Gets the post_ID that a LJ post has been saved as within WP
    629629    function get_wp_post_ID( $post ) {
    630630        global $wpdb;
    631        
     631
    632632        if ( empty( $this->postmap[$post] ) )
    633633            $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 ) );
    634        
     634
    635635        return $this->postmap[$post];
    636636    }
    637    
     637
    638638    // Gets the comment_ID that a LJ comment has been saved as within WP
    639639    function get_wp_comment_ID( $comment ) {
     
    643643        return $this->commentmap[$comment];
    644644    }
    645            
     645
    646646    function lj_ixr() {
    647647        if ( $challenge = $this->ixr->query( 'LJ.XMLRPC.getchallenge' ) ) {
     
    656656            return new WP_Error( 'IXR', __( 'LiveJournal is not responding to authentication requests. Please wait a while and then try again.' ) );
    657657        }
    658        
     658
    659659        $args = func_get_args();
    660660        $method = array_shift( $args );
     
    667667        }
    668668    }
    669    
     669
    670670    function dispatch() {
    671671        if ( empty( $_REQUEST['step'] ) )
     
    675675
    676676        $this->header();
    677        
     677
    678678        switch ( $step ) {
    679679            case -1 :
     
    712712            $this->password = get_option( 'ljapi_password' );
    713713        }
    714    
     714
    715715        // This is the password to set on protected posts
    716716        if ( !empty( $_POST['protected_password'] ) ) {
     
    720720            $this->protected_password = get_option( 'ljapi_protected_password' );
    721721        }
    722        
     722
    723723        // Login to confirm the details are correct
    724724        if ( empty( $this->username ) || empty( $this->password ) ) {
     
    746746            update_option( 'ljapi_verified', 'yes' );
    747747        }
    748        
     748
    749749        // Set up some options to avoid them autoloading (these ones get big)
    750750        add_option( 'ljapi_sync_item_times',  '', '', 'no' );
    751751        add_option( 'ljapi_usermap',          '', '', 'no' );
    752752        update_option( 'ljapi_comment_batch', 0 );
    753        
     753
    754754        return true;
    755755    }
     
    776776            }
    777777        }
    778        
     778
    779779        echo '<div id="ljapi-status">';
    780780        echo '<h3>' . __( 'Importing Posts' ) . '</h3>';
     
    786786        }
    787787        ob_flush(); flush();
    788        
     788
    789789        if ( !get_option( 'ljapi_lastsync' ) || '1900-01-01 00:00:00' == get_option( 'ljapi_lastsync' ) ) {
    790790            // We haven't downloaded meta yet, so do that first
     
    828828        echo '</div>';
    829829    }
    830    
     830
    831831    // Download comments to local XML
    832832    function step2() {
     
    836836        $this->password = get_option( 'ljapi_password' );
    837837        $this->ixr = new IXR_Client( $this->ixr_url, false, 80, 30 );
    838        
     838
    839839        echo '<div id="ljapi-status">';
    840840        echo '<h3>' . __( 'Downloading Comments' ) . '</h3>';
    841841        echo '<p>' . __( 'Now we will download your comments so we can import them (this could take a <strong>long</strong> time if you have lots of comments)...' ) . '</p>';
    842842        ob_flush(); flush();
    843        
     843
    844844        if ( !get_option( 'ljapi_usermap' ) ) {
    845845            // We haven't downloaded meta yet, so do that first
     
    878878        echo '</div>';
    879879    }
    880    
     880
    881881    // Re-thread comments already in the DB
    882882    function step3() {
     
    884884        set_time_limit( 0 );
    885885        update_option( 'ljapi_step', 3 );
    886        
     886
    887887        echo '<div id="ljapi-status">';
    888888        echo '<h3>' . __( 'Threading Comments' ) . '</h3>';
    889889        echo '<p>' . __( 'We are now re-building the threading of your comments (this can also take a while if you have lots of comments)...' ) . '</p>';
    890890        ob_flush(); flush();
    891        
     891
    892892        // Only bother adding indexes if they have over 5000 comments (arbitrary number)
    893893        $imported_comments = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->comments} WHERE comment_type = 'livejournal'" );
     
    900900            add_clean_index( $wpdb->comments, 'comment_agent' );
    901901        }
    902        
     902
    903903        // Get LJ comments, which haven't been threaded yet, 5000 at a time and thread them
    904904        while ( $comments = $wpdb->get_results( "SELECT comment_ID, comment_agent FROM {$wpdb->comments} WHERE comment_type = 'livejournal' AND comment_agent != '0' LIMIT 5000", OBJECT ) ) {
    905905            foreach ( $comments as $comment ) {
    906                 $wpdb->update( $wpdb->comments, 
    907                                 array( 'comment_parent' => $this->get_wp_comment_ID( $comment->comment_agent ), 'comment_type' => 'livejournal-done' ), 
     906                $wpdb->update( $wpdb->comments,
     907                                array( 'comment_parent' => $this->get_wp_comment_ID( $comment->comment_agent ), 'comment_type' => 'livejournal-done' ),
    908908                                array( 'comment_ID' => $comment->comment_ID ) );
    909909            }
     
    911911            $wpdb->flush();
    912912        }
    913        
     913
    914914        // Revert the comments table back to normal and optimize it to reclaim space
    915915        if ( $added_indices ) {
     
    919919            $wpdb->query( "OPTIMIZE TABLE {$wpdb->comments}" );
    920920        }
    921        
     921
    922922        // Clean up database and we're out
    923923        $this->cleanup();
     
    930930        echo '</div>';
    931931    }
    932    
     932
    933933    // Output an error message with a button to try again.
    934934    function throw_error( $error, $step ) {
     
    936936        echo $this->next_step( $step, __( 'Try Again' ) );
    937937    }
    938    
     938
    939939    // Returns the HTML for a link to the next page
    940940    function next_step( $next_step, $label, $id = 'ljapi-next-form' ) {
     
    945945        $str .= '<p><input type="submit" class="button-primary" value="' . attribute_escape( $label ) . '" /> <span id="auto-message"></span></p>';
    946946        $str .= '</form>';
    947        
     947
    948948        return $str;
    949949    }
    950    
     950
    951951    // Automatically submit the specified form after $seconds
    952952    // Include a friendly countdown in the element with id=$msg
     
    957957                ljapi_msg();
    958958            });
    959            
     959
    960960            function ljapi_msg() {
    961961                str = '<?php _e( "Continuing in %d" ) ?>';
     
    975975        </script><?php
    976976    }
    977    
     977
    978978    // Automatically submit the form with #id to continue the process
    979979    // Hide any submit buttons to avoid people clicking them
     
    985985                ljapi_msg();
    986986            });
    987            
     987
    988988            function ljapi_msg() {
    989989                str = '<?php _e( "Continuing in %d" ) ?>';
     
    10121012    function cleanup() {
    10131013        global $wpdb;
    1014        
     1014
    10151015        delete_option( 'ljapi_username' );
    10161016        delete_option( 'ljapi_password' );
     
    10311031        delete_option( 'ljapi_comment_batch' );
    10321032        delete_option( 'ljapi_step' );
    1033        
    1034         $wpdb->update( $wpdb->comments, 
    1035                         array( 'comment_karma' => 0, 'comment_agent' => 'WP LJ Importer', 'comment_type' => '' ), 
     1033
     1034        $wpdb->update( $wpdb->comments,
     1035                        array( 'comment_karma' => 0, 'comment_agent' => 'WP LJ Importer', 'comment_type' => '' ),
    10361036                        array( 'comment_type' => 'livejournal-done' ) );
    1037         $wpdb->update( $wpdb->comments, 
    1038                         array( 'comment_karma' => 0, 'comment_agent' => 'WP LJ Importer', 'comment_type' => '' ), 
     1037        $wpdb->update( $wpdb->comments,
     1038                        array( 'comment_karma' => 0, 'comment_agent' => 'WP LJ Importer', 'comment_type' => '' ),
    10391039                        array( 'comment_type' => 'livejournal' ) );
    10401040    }
    1041    
     1041
    10421042    function LJ_API_Import() {
    10431043        $this->__construct();
    10441044    }
    1045    
     1045
    10461046    function __construct() {
    10471047        // Nothing
Note: See TracChangeset for help on using the changeset viewer.