Ticket #5456: import_backfill_parents-r6367.patch

File import_backfill_parents-r6367.patch, 4.5 KB (added by tellyworth, 4 years ago)
  • wordpress/wp-admin/import/wordpress.php

     
    22 
    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; 
    109        var $mtnames = array (); 
     
    8887        function get_entries($process_post_func=NULL) { 
    8988                set_magic_quotes_runtime(0); 
    9089 
    91 #               $this->posts = array(); 
    92 #               $this->categories = array(); 
    93 #               $this->tags = array(); 
    94 #               $num = 0; 
     90                $doing_entry = false; 
    9591 
    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                                         } 
     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; 
    130101                                } 
    131          
    132                                 fclose($fp); 
     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                                } 
    133122                        } 
     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        } 
    141129         
     
    270258        } 
    271259 
    272260        function process_posts() { 
    273                 return; //FIXME 
    274261                $i = -1; 
    275262                echo '<ol>'; 
    276263 
     
    335322                        // If it has parent, process parent first. 
    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                        } 
    347334 
     
    434421                        add_post_meta( $post_id, $key, $value ); 
    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->process_posts(); 
     450                $this->backfill_parents(); 
    444451                $this->process_categories(); 
    445452                $this->process_tags(); 
    446                 $result = $this->process_posts(); 
    447453                if ( is_wp_error( $result ) ) 
    448454                        return $result; 
    449455        }