Changeset 6374

Show
Ignore:
Timestamp:
12/12/07 05:08:48 (1 year ago)
Author:
ryan
Message:

Correctly handle post_parent during import. Props tellyworth. fixes #5456

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/wp-admin/import/wordpress.php

    r6336 r6374  
    33class WP_Import { 
    44 
    5     var $posts = array (); 
    65    var $post_ids_processed = array (); 
    7     // Array of arrays. [[0] => XML fragment, [1] => New post ID] 
     6    var $orphans = array (); 
    87    var $file; 
    98    var $id; 
     
    8988        set_magic_quotes_runtime(0); 
    9089 
    91 #       $this->posts = array(); 
    92 #       $this->categories = array(); 
    93 #       $this->tags = array(); 
    94 #       $num = 0; 
    95  
    96         for ($i=0; $i<2; $i++) { 
    97             $this->another_pass = false; 
    98             $doing_entry = false; 
    99      
    100             $fp = fopen($this->file, 'r'); 
    101             if ($fp) { 
    102                 while ( !feof($fp) ) { 
    103                     $importline = rtrim(fgets($fp)); 
    104      
    105                     if ( false !== strpos($importline, '<wp:category>') ) { 
    106                         preg_match('|<wp:category>(.*?)</wp:category>|is', $importline, $category); 
    107                         $this->categories[] = $category[1]; 
    108                         continue; 
    109                     } 
    110                     if ( false !== strpos($importline, '<wp:tag>') ) { 
    111                         preg_match('|<wp:tag>(.*?)</wp:tag>|is', $importline, $tag); 
    112                         $this->tags[] = $tag[1]; 
    113                         continue; 
    114                     } 
    115                     if ( false !== strpos($importline, '<item>') ) { 
    116                         $this->post = ''; 
    117                         $doing_entry = true; 
    118                         continue; 
    119                     } 
    120                     if ( false !== strpos($importline, '</item>') ) { 
    121                         $num++; 
    122                         $doing_entry = false; 
    123                         if ($process_post_func) 
    124                             call_user_func($process_post_func, $this->post); 
    125                         continue; 
    126                     } 
    127                     if ( $doing_entry ) { 
    128                         $this->post .= $importline . "\n"; 
    129                     } 
    130                 } 
    131      
    132                 fclose($fp); 
    133             } 
     90        $doing_entry = false; 
     91 
     92        $fp = fopen($this->file, 'r'); 
     93        if ($fp) { 
     94            while ( !feof($fp) ) { 
     95                $importline = rtrim(fgets($fp)); 
     96 
     97                if ( false !== strpos($importline, '<wp:category>') ) { 
     98                    preg_match('|<wp:category>(.*?)</wp:category>|is', $importline, $category); 
     99                    $this->categories[] = $category[1]; 
     100                    continue; 
     101                } 
     102                if ( false !== strpos($importline, '<wp:tag>') ) { 
     103                    preg_match('|<wp:tag>(.*?)</wp:tag>|is', $importline, $tag); 
     104                    $this->tags[] = $tag[1]; 
     105                    continue; 
     106                } 
     107                if ( false !== strpos($importline, '<item>') ) { 
     108                    $this->post = ''; 
     109                    $doing_entry = true; 
     110                    continue; 
     111                } 
     112                if ( false !== strpos($importline, '</item>') ) { 
     113                    $num++; 
     114                    $doing_entry = false; 
     115                    if ($process_post_func) 
     116                        call_user_func($process_post_func, $this->post); 
     117                    continue; 
     118                } 
     119                if ( $doing_entry ) { 
     120                    $this->post .= $importline . "\n"; 
     121                } 
     122            } 
     123 
     124            fclose($fp); 
     125        } 
    134126             
    135             // skip the second loop iteration unless it's needed 
    136             if ( !$this->another_pass ) 
    137                 break; 
    138         } 
    139127 
    140128    } 
     
    271259 
    272260    function process_posts() { 
    273         return; //FIXME 
    274261        $i = -1; 
    275262        echo '<ol>'; 
     
    336323            $post_parent = (int) $post_parent; 
    337324            if ($post_parent) { 
     325                // if we already know the parent, map it to the local ID 
    338326                if ( $parent = $this->post_ids_processed[$post_parent] ) { 
    339327                    $post_parent = $parent;  // new ID of the parent 
    340328                } 
    341329                else { 
    342                     // wait until the parent has been processed 
    343                     $this->another_pass = true; 
    344                     return; 
     330                    // record the parent for later 
     331                    $this->orphans[intval($post_ID)] = $post_parent; 
    345332                } 
    346333            } 
     
    435422        } } 
    436423    } 
     424     
     425    // update the post_parent of orphans now that we know the local id's of all parents 
     426    function backfill_parents() { 
     427        global $wpdb; 
     428         
     429        foreach ($this->orphans as $child_id => $parent_id) { 
     430            $local_child_id = $this->post_ids_processed[$child_id]; 
     431            $local_parent_id = $this->post_ids_processed[$parent_id]; 
     432            if ($local_child_id and $local_parent_id) { 
     433                $wpdb->query( $wpdb->prepare("UPDATE {$wpdb->posts} SET post_parent = %d WHERE ID = %d", $local_parent_id, $local_child_id)); 
     434            } 
     435        } 
     436    } 
    437437 
    438438    function import($id) { 
    439439        $this->id = (int) $id; 
    440440 
    441         $this->file = get_attached_file($this->id); 
     441        $file = get_attached_file($this->id); 
     442        $this->import_file($file); 
     443    } 
     444         
     445    function import_file($file) { 
     446        $this->file = $file; 
     447         
    442448        $this->get_authors_from_post(); 
    443         $this->get_entries(array(&$this, 'process_post')); 
     449        $this->get_entries(); 
    444450        $this->process_categories(); 
    445451        $this->process_tags(); 
    446452        $result = $this->process_posts(); 
     453        $this->backfill_parents(); 
    447454        if ( is_wp_error( $result ) ) 
    448455            return $result;