WordPress.org

Plugin Directory

Changeset 525735


Ignore:
Timestamp:
04/01/12 02:02:59 (2 years ago)
Author:
dllh
Message:

Update draft, page, and link counts when reloading the import page after doing all the fun work.

This requires overriding get_imported_posts() from the core importer class to allow the passing of additional arguments to pass to the query for fetching postmeta for subsets of the posts so that we can separate posts, drafts, and pages.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • posterous-importer/branches/v2api/posterous.php

    r525655 r525735  
    1111 
    1212// TODO: Actually honor the retry-after headers instead of using an arbitrary wait time. 
    13 // TODO: Add page and link counts to bottom of import screen. 
    1413// TODO: Figure out Posterous's weird shortcode/hashing thing for inserting images into pages and body_html (e.g. [[posterous-content:jhsmtHnEjaoooaxfmmbm]]) 
    1514// Do something with private posts. 
     
    4443    var $bid = ''; 
    4544    var $permalinks = array(); 
     45    var $drafts = array(); 
     46    var $pages = array(); 
    4647    var $comments = array(); 
    4748    var $attachments = array(); 
     49    var $links = 0; 
    4850    var $url_remap = array(); 
    4951    var $have_posts = true; 
     
    107109        // Sleep after fetching sites because Posterous doesn't like if you do more than one request in a second. 
    108110        do_action( 'posterous_api_sleep' ); 
    109         $this->permalinks = $this->get_imported_posts( 'posterous', $this->bid ); 
     111        $this->permalinks = $this->get_imported_posts( 'posterous', $this->bid, array( 'post_type' => 'post', 'post_status' => 'publish' ) ); 
     112        $this->drafts = $this->get_imported_posts( 'posterous', $this->bid, array( 'post_type' => 'post', 'post_status' => 'draft' ) ); 
     113        $this->pages = $this->get_imported_posts( 'posterous', $this->bid, array( 'post_type' => 'page' ) ); 
    110114        $this->comments = $this->get_imported_comments(); 
    111115        $this->attachments = $this->get_imported_attachments( 'posterous', $this->bid ); 
     
    113117        $this->do_posts( 'pages' ); 
    114118        $this->do_posts( 'posts/drafts' ); 
    115         $this->process_attachments(); 
     119        $this->process_attachments( 'posts' ); 
     120        $this->process_attachments( 'drafts' ); 
     121        $this->process_attachments( 'pages' ); 
    116122        $this->process_links(); 
    117123        $this->cleanup(); 
    118124    } 
    119125 
     126    /** 
     127     * Returns array with imported permalinks from WordPress database 
     128     * 
     129     * Overrides core function by adding a $where array that lets us select postmeta 
     130     * for subsets of the posts. The array should specify column name as the key 
     131     * and column value to search for as the value (e.g. post_type => 'page'). 
     132     * 
     133     * @param string $bid 
     134     * @param string $where 
     135     * @return array 
     136     */ 
     137    function get_imported_posts( $importer_name, $bid, $where = array() ) { 
     138            global $wpdb; 
     139 
     140            $hashtable = array(); 
     141 
     142            $limit = 100; 
     143            $offset = 0; 
     144            $_where_array = array(); 
     145            $_where_string = ''; 
     146 
     147            // Grab all posts in chunks 
     148            do { 
     149                    $meta_key = $importer_name . '_' . $bid . '_permalink'; 
     150                    if ( 0 < count( $where ) ) { 
     151                        foreach ( $where as $col => $val ) { 
     152                            $_where_array[] = $col . '=' . "'$val'"; 
     153                        } 
     154                        $_where_string = implode( ' AND ', $_where_array ) . ' AND '; 
     155                    } 
     156                    $sql = $wpdb->prepare( "SELECT m.post_id, m.meta_value FROM $wpdb->postmeta m, $wpdb->posts p WHERE " . $_where_string . " m.meta_key = '%s' AND p.ID = m.post_id LIMIT %d,%d",  $meta_key, $offset, $limit ); 
     157                    $results = $wpdb->get_results( $sql ); 
     158 
     159                    // Increment offset 
     160                    $offset = ( $limit + $offset ); 
     161 
     162                    if ( !empty( $results ) ) { 
     163                            foreach ( $results as $r ) { 
     164                                    // Set permalinks into array 
     165                                    $hashtable[$r->meta_value] = intval( $r->post_id ); 
     166                            } 
     167                    } 
     168            } while ( count( $results ) == $limit ); 
     169 
     170            // unset to save memory 
     171            unset( $results, $r ); 
     172 
     173            return $hashtable; 
     174    } 
     175         
    120176    function get_sites() { 
    121177        $url = 'http://posterous.com/api/2/sites/'; 
     
    218274            $permalink = (string) $entry->full_url; 
    219275            $commentscount = (int) $entry->number_of_comments; 
    220             if ( isset( $this->permalinks[$permalink] ) ) { 
     276             
     277            switch( $type ) { 
     278                case 'pages': 
     279                    $_permalinks = $this->pages; 
     280                    break; 
     281                case 'posts/drafts': 
     282                    $_permalinks = $this->drafts; 
     283                    break; 
     284                default: 
     285                    $_permalinks = $this->permalinks; 
     286            } 
     287             
     288            if ( isset( $_permalinks[$permalink] ) ) { 
    221289                printf( "<em>%s</em><br />\n", __( 'Skipping' ) . ' ' . $entry->title ); 
    222290                // process_comments here to check for any new comments or ones that we missed 
    223291                printf( "\t<em>%s</em><br />\n", __( 'Found Comments:' ) . ' ' . $commentscount ); 
    224292                if ( $commentscount > 0 ) 
    225                     $this->process_comments( $this->permalinks[$permalink], $entry ); 
     293                    $this->process_comments( $_permalinks[$permalink], $entry ); 
    226294                continue; 
    227295            }                    
     
    287355 
    288356            printf( "<em>%s</em><br />\n", __( 'Importing' ) . ' ' . $entry->title ); 
    289             $this->permalinks[$permalink] = $post_id; 
     357             
     358            $_permalinks[$permalink] = $post_id; 
     359 
     360            switch ( $type ) { 
     361                case 'pages': 
     362                    $this->pages = $_permalinks; 
     363                    break; 
     364                case 'posts/drafts': 
     365                    $this->drafts = $_permalinks; 
     366                    break; 
     367                default: 
     368                    $this->permalinks = $_permalinks; 
     369            } 
    290370 
    291371            $tags = $this->get_tags( $entry ); 
     
    389469     * @return void 
    390470     */ 
    391     function process_attachments() { 
    392         if ( empty( $this->permalinks ) ) 
     471    function process_attachments( $type ) { 
     472        switch( $type ) { 
     473            case 'pages': 
     474                $_permalinks = $this->pages; 
     475                break; 
     476            case 'drafts': 
     477                $_permalinks = $this->drafts; 
     478                break; 
     479            default: 
     480                $_permalinks = $this->permalinks; 
     481        } 
     482         
     483        if ( empty( $_permalinks ) ) 
    393484            return; 
    394485 
    395486        // Loop over each post ID 
    396         foreach ( $this->permalinks as $permalink => $post_id ) { 
     487        foreach ( $_permalinks as $permalink => $post_id ) { 
    397488            // Get post data 
    398489            $post = get_post( $post_id ); 
     
    9731064            <input type="hidden" id="hostname" name="hostname" value="<?php echo $hostname; ?>" /> 
    9741065            <strong><?php _e( 'Posts:' ); ?></strong> <span id="posts_count"><?php echo $status['posts']; ?></span><br /> 
     1066            <strong><?php _e( 'Drafts:' ); ?></strong> <span id="drafts_count"><?php echo $status['drafts']; ?></span><br /> 
     1067            <strong><?php _e( 'Pages:' ); ?></strong> <span id="pages_count"><?php echo $status['pages']; ?></span><br /> 
    9751068            <strong><?php _e( 'Comments:' ); ?></strong> <span id="comments_count"><?php echo $status['comments']; ?></span><br /> 
    9761069            <strong><?php _e( 'Attachments:' ); ?></strong> <span id="attachments_count"><?php echo $status['attachments']; ?></span><br /> 
     1070            <strong><?php _e( 'Links:' ); ?></strong> <span id="links_count"><?php echo $status['links']; ?></span><br /> 
    9771071            <p><input type="button" class="button" id="start_poll" name="start_poll" value="<?php _e( 'Check Status' ); ?>" /><input type="button" class="button" id="stop_poll" name="stop_poll" value="<?php _e( 'Stop Checking' ); ?>" /></p> 
    9781072        </div> 
     
    9851079        $this->hostname = $this->sanitize_hostname( $hostname ); 
    9861080        $this->bid = md5( $this->hostname ); 
    987         $this->permalinks = $this->get_imported_posts( 'posterous', $this->bid ); 
     1081        $this->permalinks = $this->get_imported_posts( 'posterous', $this->bid, array( 'post_type' => 'post', 'post_status' => 'publish' ) ); 
     1082        $this->drafts = $this->get_imported_posts( 'posterous', $this->bid, array( 'post_type' => 'post', 'post_status' => 'draft' ) ); 
     1083        $this->pages = $this->get_imported_posts( 'posterous', $this->bid, array( 'post_type' => 'page' ) ); 
    9881084        $this->comments = $this->get_imported_comments(); 
    9891085        $this->attachments = $this->get_imported_attachments( 'posterous', $this->bid ); 
     
    9911087        $status = array(); 
    9921088        $status['posts'] = count( $this->permalinks ); 
     1089        $status['drafts'] = count( $this->drafts ); 
     1090        $status['pages'] = count( $this->pages ); 
    9931091        $status['comments'] = count( $this->comments ); 
    9941092        $status['attachments'] = count( $this->attachments ); 
     1093        $status['links'] = $this->links; 
    9951094 
    9961095        if ( 'json' == $return ) 
     
    11061205            return false; 
    11071206        } 
     1207        $this->links++; 
    11081208        return true; 
    11091209    } 
Note: See TracChangeset for help on using the changeset viewer.