Changeset 6477

Show
Ignore:
Timestamp:
12/24/07 06:20:34 (8 months ago)
Author:
ryan
Message:

Importer refactoring from tellyworth. fixes #5522

Files:

Legend:

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

    r6472 r6477  
    1010    var $newauthornames = array (); 
    1111    var $allauthornames = array (); 
     12     
     13    var $author_ids = array (); 
     14     
    1215    var $j = -1; 
    1316    var $fetch_attachments = false; 
     
    4548    } 
    4649 
    47     function users_form($n) { 
    48         global $wpdb, $testing; 
    49         $users = $wpdb->get_results("SELECT user_login FROM $wpdb->users ORDER BY user_login"); 
    50 ?><select name="userselect[<?php echo $n; ?>]"> 
    51     <option value="#NONE#">- Select -</option> 
    52     <?php 
    53         foreach ($users as $user) { 
    54             echo '<option value="'.$user->user_login.'">'.$user->user_login.'</option>'; 
    55         } 
    56 ?> 
    57     </select> 
    58     <?php 
    59     } 
    60  
    61     //function to check the authorname and do the mapping 
    62     function checkauthor($author) { 
    63         global $wpdb; 
    64         //mtnames is an array with the names in the mt import file 
    65         $pass = 'changeme'; 
    66         if (!(in_array($author, $this->mtnames))) { //a new mt author name is found 
    67             ++ $this->j; 
    68             $this->mtnames[$this->j] = $author; //add that new mt author name to an array 
    69             $user_id = username_exists($this->newauthornames[$this->j]); //check if the new author name defined by the user is a pre-existing wp user 
    70             if (!$user_id) { //banging my head against the desk now. 
    71                 if ($this->newauthornames[$this->j] == 'left_blank') { //check if the user does not want to change the authorname 
    72                     $user_id = wp_create_user($author, $pass); 
    73                     $this->newauthornames[$this->j] = $author; //now we have a name, in the place of left_blank. 
    74                 } else { 
    75                     $user_id = wp_create_user($this->newauthornames[$this->j], $pass); 
    76                 } 
    77             } else { 
    78                 return $user_id; // return pre-existing wp username if it exists 
    79             } 
    80         } else { 
    81             $key = array_search($author, $this->mtnames); //find the array key for $author in the $mtnames array 
    82             $user_id = username_exists($this->newauthornames[$key]); //use that key to get the value of the author's name from $newauthornames 
    83         } 
    84  
    85         return $user_id; 
     50    function has_gzip() { 
     51        return is_callable('gzopen'); 
     52    } 
     53     
     54    function fopen($filename, $mode='r') { 
     55        if ( $this->has_gzip() ) 
     56            return gzopen($filename, $mode); 
     57        return fopen($filename, $mode); 
     58    } 
     59     
     60    function feof($fp) { 
     61        if ( $this->has_gzip() ) 
     62            return gzeof($fp); 
     63        return feof($fp); 
     64    } 
     65     
     66    function fgets($fp, $len=8192) { 
     67        if ( $this->has_gzip() ) 
     68            return gzgets($fp, $len); 
     69        return fgets($fp, $len); 
     70    } 
     71     
     72    function fclose($fp) { 
     73        if ( $this->has_gzip() ) 
     74            return gzclose($fp); 
     75        return fclose($fp); 
    8676    } 
    8777 
     
    9080 
    9181        $doing_entry = false; 
    92  
    93         $fp = fopen($this->file, 'r'); 
     82        $is_wxr_file = false; 
     83 
     84        $fp = $this->fopen($this->file, 'r'); 
    9485        if ($fp) { 
    95             while ( !feof($fp) ) { 
    96                 $importline = rtrim(fgets($fp)); 
     86            while ( !$this->feof($fp) ) { 
     87                $importline = rtrim($this->fgets($fp)); 
     88                 
     89                // this doesn't check that the file is perfectly valid but will at least confirm that it's not the wrong format altogether 
     90                if ( !$is_wxr_file && preg_match('|xmlns:wp="http://wordpress[.]org/export/\d+[.]\d+/"|', $importline) ) 
     91                    $is_wxr_file = true; 
    9792 
    9893                if ( false !== strpos($importline, '<wp:category>') ) { 
     
    123118            } 
    124119 
    125             fclose($fp); 
    126         } 
    127              
     120            $this->fclose($fp); 
     121        } 
     122 
     123        return $is_wxr_file; 
    128124 
    129125    } 
    130126     
    131127    function get_wp_authors() { 
    132         $this->get_entries(array(&$this, 'process_author')); 
    133  
    134128        // We need to find unique values of author names, while preserving the order, so this function emulates the unique_value(); php function, without the sorting. 
    135129        $temp = $this->allauthornames; 
     
    146140 
    147141    function get_authors_from_post() { 
    148         $formnames = array (); 
    149         $selectnames = array (); 
    150  
    151         foreach ($_POST['user'] as $key => $line) { 
    152             $newname = trim(stripslashes($line)); 
    153             if ($newname == '') 
    154                 $newname = 'left_blank'; //passing author names from step 1 to step 2 is accomplished by using POST. left_blank denotes an empty entry in the form. 
    155             array_push($formnames, "$newname"); 
    156         } // $formnames is the array with the form entered names 
    157  
    158         foreach ($_POST['userselect'] as $user => $key) { 
    159             $selected = trim(stripslashes($key)); 
    160             array_push($selectnames, "$selected"); 
    161         } 
    162  
    163         $count = count($formnames); 
    164         for ($i = 0; $i < $count; $i ++) { 
    165             if ($selectnames[$i] != '#NONE#') { //if no name was selected from the select menu, use the name entered in the form 
    166                 array_push($this->newauthornames, "$selectnames[$i]"); 
    167             } else { 
    168                 array_push($this->newauthornames, "$formnames[$i]"); 
     142        global $current_user; 
     143         
     144        // this will populate $this->author_ids with a list of author_names => user_ids 
     145         
     146        foreach ( $_POST['author_in'] as $i => $in_author_name ) { 
     147             
     148            if ( !empty($_POST['user_select'][$i]) ) { 
     149                // an existing user was selected in the dropdown list 
     150                $user = get_userdata( intval($_POST['user_select'][$i]) ); 
     151                if ( isset($user->ID) ) 
     152                    $this->author_ids[$in_author_name] = $user->ID; 
     153            } 
     154            elseif ( $this->allow_create_users() ) { 
     155                // nothing was selected in the dropdown list, so we'll use the name in the text field 
     156                 
     157                $new_author_name = trim($_POST['user_create'][$i]); 
     158                // if the user didn't enter a name, assume they want to use the same name as in the import file 
     159                if ( empty($new_author_name) ) 
     160                    $new_author_name = $in_author_name; 
     161                 
     162                $user_id = username_exists($new_author_name); 
     163                if ( !$user_id ) {  
     164                    $user_id = wp_create_user($new_author_name, 'changeme'); 
     165                } 
     166                 
     167                $this->author_ids[$in_author_name] = $user_id; 
     168            } 
     169             
     170            // failsafe: if the user_id was invalid, default to the current user 
     171            if ( empty($this->author_ids[$in_author_name]) ) { 
     172                $this->author_ids[$in_author_name] = intval($current_user->ID); 
    169173            } 
    170174        } 
     
    176180<h2><?php _e('Assign Authors'); ?></h2> 
    177181<p><?php _e('To make it easier for you to edit and save the imported posts and drafts, you may want to change the name of the author of the posts. For example, you may want to import all the entries as <code>admin</code>s entries.'); ?></p> 
    178 <p><?php _e('If a new user is created by WordPress, the password will be set, by default, to "changeme". Quite suggestive, eh? ;)'); ?></p> 
    179     <?php 
     182<?php 
     183    if ( $this->allow_create_users() ) { 
     184        echo '<p>'.__('If a new user is created by WordPress, the password will be set, by default, to "changeme". Quite suggestive, eh? ;)')."</p>\n"; 
     185    } 
    180186 
    181187 
     
    187193        foreach ($authors as $author) { 
    188194            ++ $j; 
    189             echo '<li>'.__('Current author:').' <strong>'.$author.'</strong><br />'.sprintf(__('Create user %1$s or map to existing'), ' <input type="text" value="'.$author.'" name="'.'user[]'.'" maxlength="30"> <br />')
    190             $this->users_form($j); 
     195            echo '<li>'.__('Import author:').' <strong>'.$author.'</strong><br />'
     196            $this->users_form($j, $author); 
    191197            echo '</li>'; 
    192198        } 
     199         
     200        if ( $this->allow_fetch_attachments() ) { 
    193201?>   
    194202</ol>    
     
    200208 
    201209<?php 
     210        } 
     211     
    202212        echo '<input type="submit" value="Submit">'.'<br />'; 
    203213        echo '</form>'; 
    204214 
    205215    } 
     216     
     217    function users_form($n, $author) { 
     218         
     219        if ( $this->allow_create_users() ) { 
     220            printf(__('Create user %1$s or map to existing'), ' <input type="text" value="'.$author.'" name="'.'user_create['.intval($n).']'.'" maxlength="30"> <br />'); 
     221        } 
     222        else { 
     223            echo __('Map to existing').'<br />'; 
     224        } 
     225 
     226        // keep track of $n => $author name 
     227        echo '<input type="hidden" name="author_in['.intval($n).']" value="'.htmlspecialchars($author).'" />'; 
     228         
     229        $users = get_users_of_blog(); 
     230?><select name="user_select[<?php echo $n; ?>]"> 
     231    <option value="0">- Select -</option> 
     232    <?php 
     233        foreach ($users as $user) { 
     234            echo '<option value="'.$user->user_id.'">'.$user->user_login.'</option>'; 
     235        } 
     236?> 
     237    </select> 
     238    <?php 
     239    } 
    206240 
    207241    function select_authors() { 
    208         $this->get_entries(array(&$this, 'process_author')); 
    209         $this->wp_authors_form(); 
    210     } 
     242        $is_wxr_file = $this->get_entries(array(&$this, 'process_author')); 
     243        if ( $is_wxr_file ) { 
     244            $this->wp_authors_form(); 
     245        } 
     246        else { 
     247            echo '<h2>'.__('Invalid file').'</h2>'; 
     248            echo '<p>'.__('Please upload a valid WXR (WordPress eXtended RSS) export file.').'</p>'; 
     249        } 
     250    } 
     251 
     252    // fetch the user ID for a given author name, respecting the mapping preferences 
     253    function checkauthor($author) { 
     254        global $current_user; 
     255         
     256        if ( !empty($this->author_ids[$author]) ) 
     257            return $this->author_ids[$author]; 
     258             
     259        // failsafe: map to the current user 
     260        return $current_user->ID; 
     261    } 
     262 
     263 
    211264 
    212265    function process_categories() { 
     
    516569         
    517570        // make sure the fetch was successful 
    518         if ( $headers['response'] != '200' ) 
    519             return new WP_Error( 'import_file_error', __(sprintf('Remote file returned error response %d', intval($headers['response']))) ); 
    520         elseif ( isset($headers['content-length']) && filesize($upload['file']) != $headers['content-length'] ) 
     571        if ( $headers['response'] != '200' ) { 
     572            @unlink($upload['file']); 
     573            return new WP_Error( 'import_file_error', sprintf(__('Remote file returned error response %d'), intval($headers['response'])) ); 
     574        } 
     575        elseif ( isset($headers['content-length']) && filesize($upload['file']) != $headers['content-length'] ) { 
     576            @unlink($upload['file']); 
    521577            return new WP_Error( 'import_file_error', __('Remote file is incorrect size') ); 
     578        } 
     579             
     580        $max_size = $this->max_attachment_size(); 
     581        if ( !empty($max_size) and filesize($upload['file']) > $max_size ) { 
     582            @unlink($upload['file']); 
     583            return new WP_Error( 'import_file_error', sprintf(__('Remote file is too large, limit is %s', size_format($max_size))) ); 
     584        } 
    522585             
    523586        // keep track of the old and new urls so we can substitute them later 
     
    571634    } 
    572635 
     636    // give the user the option of creating new users to represent authors in the import file? 
     637    function allow_create_users() { 
     638        return apply_filters('import_allow_create_users', true); 
     639    } 
     640     
     641    // give the user the option of downloading and importing attached files 
     642    function allow_fetch_attachments() { 
     643        return apply_filters('import_allow_fetch_attachments', true); 
     644    } 
     645 
     646    function max_attachment_size() { 
     647        // can be overridden with a filter - 0 means no limit 
     648        return apply_filters('import_attachment_size_limit', 0); 
     649    } 
     650     
     651 
    573652    function import($id, $fetch_attachments = false) { 
    574653        $this->id = (int) $id; 
    575         $this->fetch_attachments = (bool) $fetch_attachments
     654        $this->fetch_attachments = ($this->allow_fetch_attachments() && (bool) $fetch_attachments)
    576655 
    577656        add_filter('import_post_meta_key', array($this, 'is_valid_meta_key')); 
     657        do_action('import_start'); 
    578658        $file = get_attached_file($this->id); 
    579659        $this->import_file($file); 
     660        do_action('import_end'); 
    580661    } 
    581662         
  • trunk/wp-admin/includes/import.php

    r6411 r6477  
    2121function wp_import_handle_upload() { 
    2222    $overrides = array( 'test_form' => false, 'test_type' => false ); 
     23    $_FILES['import']['name'] .= '.import'; 
    2324    $file = wp_handle_upload( $_FILES['import'], $overrides ); 
    2425