mirror of
https://github.com/WordPress/WordPress.git
synced 2025-02-16 19:46:21 +00:00
Importer refactoring from tellyworth. fixes #5522
git-svn-id: http://svn.automattic.com/wordpress/trunk@6477 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
1d2147c456
commit
b3fa9f2840
@ -9,6 +9,9 @@ class WP_Import {
|
|||||||
var $mtnames = array ();
|
var $mtnames = array ();
|
||||||
var $newauthornames = array ();
|
var $newauthornames = array ();
|
||||||
var $allauthornames = array ();
|
var $allauthornames = array ();
|
||||||
|
|
||||||
|
var $author_ids = array ();
|
||||||
|
|
||||||
var $j = -1;
|
var $j = -1;
|
||||||
var $fetch_attachments = false;
|
var $fetch_attachments = false;
|
||||||
var $url_remap = array ();
|
var $url_remap = array ();
|
||||||
@ -44,56 +47,48 @@ class WP_Import {
|
|||||||
return $return;
|
return $return;
|
||||||
}
|
}
|
||||||
|
|
||||||
function users_form($n) {
|
function has_gzip() {
|
||||||
global $wpdb, $testing;
|
return is_callable('gzopen');
|
||||||
$users = $wpdb->get_results("SELECT user_login FROM $wpdb->users ORDER BY user_login");
|
|
||||||
?><select name="userselect[<?php echo $n; ?>]">
|
|
||||||
<option value="#NONE#">- Select -</option>
|
|
||||||
<?php
|
|
||||||
foreach ($users as $user) {
|
|
||||||
echo '<option value="'.$user->user_login.'">'.$user->user_login.'</option>';
|
|
||||||
}
|
|
||||||
?>
|
|
||||||
</select>
|
|
||||||
<?php
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//function to check the authorname and do the mapping
|
function fopen($filename, $mode='r') {
|
||||||
function checkauthor($author) {
|
if ( $this->has_gzip() )
|
||||||
global $wpdb;
|
return gzopen($filename, $mode);
|
||||||
//mtnames is an array with the names in the mt import file
|
return fopen($filename, $mode);
|
||||||
$pass = 'changeme';
|
}
|
||||||
if (!(in_array($author, $this->mtnames))) { //a new mt author name is found
|
|
||||||
++ $this->j;
|
function feof($fp) {
|
||||||
$this->mtnames[$this->j] = $author; //add that new mt author name to an array
|
if ( $this->has_gzip() )
|
||||||
$user_id = username_exists($this->newauthornames[$this->j]); //check if the new author name defined by the user is a pre-existing wp user
|
return gzeof($fp);
|
||||||
if (!$user_id) { //banging my head against the desk now.
|
return feof($fp);
|
||||||
if ($this->newauthornames[$this->j] == 'left_blank') { //check if the user does not want to change the authorname
|
}
|
||||||
$user_id = wp_create_user($author, $pass);
|
|
||||||
$this->newauthornames[$this->j] = $author; //now we have a name, in the place of left_blank.
|
function fgets($fp, $len=8192) {
|
||||||
} else {
|
if ( $this->has_gzip() )
|
||||||
$user_id = wp_create_user($this->newauthornames[$this->j], $pass);
|
return gzgets($fp, $len);
|
||||||
}
|
return fgets($fp, $len);
|
||||||
} else {
|
}
|
||||||
return $user_id; // return pre-existing wp username if it exists
|
|
||||||
}
|
function fclose($fp) {
|
||||||
} else {
|
if ( $this->has_gzip() )
|
||||||
$key = array_search($author, $this->mtnames); //find the array key for $author in the $mtnames array
|
return gzclose($fp);
|
||||||
$user_id = username_exists($this->newauthornames[$key]); //use that key to get the value of the author's name from $newauthornames
|
return fclose($fp);
|
||||||
}
|
|
||||||
|
|
||||||
return $user_id;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function get_entries($process_post_func=NULL) {
|
function get_entries($process_post_func=NULL) {
|
||||||
set_magic_quotes_runtime(0);
|
set_magic_quotes_runtime(0);
|
||||||
|
|
||||||
$doing_entry = false;
|
$doing_entry = false;
|
||||||
|
$is_wxr_file = false;
|
||||||
|
|
||||||
$fp = fopen($this->file, 'r');
|
$fp = $this->fopen($this->file, 'r');
|
||||||
if ($fp) {
|
if ($fp) {
|
||||||
while ( !feof($fp) ) {
|
while ( !$this->feof($fp) ) {
|
||||||
$importline = rtrim(fgets($fp));
|
$importline = rtrim($this->fgets($fp));
|
||||||
|
|
||||||
|
// this doesn't check that the file is perfectly valid but will at least confirm that it's not the wrong format altogether
|
||||||
|
if ( !$is_wxr_file && preg_match('|xmlns:wp="http://wordpress[.]org/export/\d+[.]\d+/"|', $importline) )
|
||||||
|
$is_wxr_file = true;
|
||||||
|
|
||||||
if ( false !== strpos($importline, '<wp:category>') ) {
|
if ( false !== strpos($importline, '<wp:category>') ) {
|
||||||
preg_match('|<wp:category>(.*?)</wp:category>|is', $importline, $category);
|
preg_match('|<wp:category>(.*?)</wp:category>|is', $importline, $category);
|
||||||
@ -122,15 +117,14 @@ class WP_Import {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fclose($fp);
|
$this->fclose($fp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return $is_wxr_file;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function get_wp_authors() {
|
function get_wp_authors() {
|
||||||
$this->get_entries(array(&$this, 'process_author'));
|
|
||||||
|
|
||||||
// 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.
|
// 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.
|
||||||
$temp = $this->allauthornames;
|
$temp = $this->allauthornames;
|
||||||
$authors[0] = array_shift($temp);
|
$authors[0] = array_shift($temp);
|
||||||
@ -145,27 +139,37 @@ class WP_Import {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function get_authors_from_post() {
|
function get_authors_from_post() {
|
||||||
$formnames = array ();
|
global $current_user;
|
||||||
$selectnames = array ();
|
|
||||||
|
// this will populate $this->author_ids with a list of author_names => user_ids
|
||||||
foreach ($_POST['user'] as $key => $line) {
|
|
||||||
$newname = trim(stripslashes($line));
|
foreach ( $_POST['author_in'] as $i => $in_author_name ) {
|
||||||
if ($newname == '')
|
|
||||||
$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.
|
if ( !empty($_POST['user_select'][$i]) ) {
|
||||||
array_push($formnames, "$newname");
|
// an existing user was selected in the dropdown list
|
||||||
} // $formnames is the array with the form entered names
|
$user = get_userdata( intval($_POST['user_select'][$i]) );
|
||||||
|
if ( isset($user->ID) )
|
||||||
foreach ($_POST['userselect'] as $user => $key) {
|
$this->author_ids[$in_author_name] = $user->ID;
|
||||||
$selected = trim(stripslashes($key));
|
}
|
||||||
array_push($selectnames, "$selected");
|
elseif ( $this->allow_create_users() ) {
|
||||||
}
|
// nothing was selected in the dropdown list, so we'll use the name in the text field
|
||||||
|
|
||||||
$count = count($formnames);
|
$new_author_name = trim($_POST['user_create'][$i]);
|
||||||
for ($i = 0; $i < $count; $i ++) {
|
// if the user didn't enter a name, assume they want to use the same name as in the import file
|
||||||
if ($selectnames[$i] != '#NONE#') { //if no name was selected from the select menu, use the name entered in the form
|
if ( empty($new_author_name) )
|
||||||
array_push($this->newauthornames, "$selectnames[$i]");
|
$new_author_name = $in_author_name;
|
||||||
} else {
|
|
||||||
array_push($this->newauthornames, "$formnames[$i]");
|
$user_id = username_exists($new_author_name);
|
||||||
|
if ( !$user_id ) {
|
||||||
|
$user_id = wp_create_user($new_author_name, 'changeme');
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->author_ids[$in_author_name] = $user_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
// failsafe: if the user_id was invalid, default to the current user
|
||||||
|
if ( empty($this->author_ids[$in_author_name]) ) {
|
||||||
|
$this->author_ids[$in_author_name] = intval($current_user->ID);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -175,8 +179,10 @@ class WP_Import {
|
|||||||
?>
|
?>
|
||||||
<h2><?php _e('Assign Authors'); ?></h2>
|
<h2><?php _e('Assign Authors'); ?></h2>
|
||||||
<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>
|
<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>
|
||||||
<p><?php _e('If a new user is created by WordPress, the password will be set, by default, to "changeme". Quite suggestive, eh? ;)'); ?></p>
|
<?php
|
||||||
<?php
|
if ( $this->allow_create_users() ) {
|
||||||
|
echo '<p>'.__('If a new user is created by WordPress, the password will be set, by default, to "changeme". Quite suggestive, eh? ;)')."</p>\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
$authors = $this->get_wp_authors();
|
$authors = $this->get_wp_authors();
|
||||||
@ -186,10 +192,12 @@ class WP_Import {
|
|||||||
$j = -1;
|
$j = -1;
|
||||||
foreach ($authors as $author) {
|
foreach ($authors as $author) {
|
||||||
++ $j;
|
++ $j;
|
||||||
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 />');
|
echo '<li>'.__('Import author:').' <strong>'.$author.'</strong><br />';
|
||||||
$this->users_form($j);
|
$this->users_form($j, $author);
|
||||||
echo '</li>';
|
echo '</li>';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ( $this->allow_fetch_attachments() ) {
|
||||||
?>
|
?>
|
||||||
</ol>
|
</ol>
|
||||||
<h2><?php _e('Import Attachments'); ?></h2>
|
<h2><?php _e('Import Attachments'); ?></h2>
|
||||||
@ -199,16 +207,61 @@ class WP_Import {
|
|||||||
</p>
|
</p>
|
||||||
|
|
||||||
<?php
|
<?php
|
||||||
|
}
|
||||||
|
|
||||||
echo '<input type="submit" value="Submit">'.'<br />';
|
echo '<input type="submit" value="Submit">'.'<br />';
|
||||||
echo '</form>';
|
echo '</form>';
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function users_form($n, $author) {
|
||||||
|
|
||||||
|
if ( $this->allow_create_users() ) {
|
||||||
|
printf(__('Create user %1$s or map to existing'), ' <input type="text" value="'.$author.'" name="'.'user_create['.intval($n).']'.'" maxlength="30"> <br />');
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
echo __('Map to existing').'<br />';
|
||||||
|
}
|
||||||
|
|
||||||
|
// keep track of $n => $author name
|
||||||
|
echo '<input type="hidden" name="author_in['.intval($n).']" value="'.htmlspecialchars($author).'" />';
|
||||||
|
|
||||||
|
$users = get_users_of_blog();
|
||||||
|
?><select name="user_select[<?php echo $n; ?>]">
|
||||||
|
<option value="0">- Select -</option>
|
||||||
|
<?php
|
||||||
|
foreach ($users as $user) {
|
||||||
|
echo '<option value="'.$user->user_id.'">'.$user->user_login.'</option>';
|
||||||
|
}
|
||||||
|
?>
|
||||||
|
</select>
|
||||||
|
<?php
|
||||||
|
}
|
||||||
|
|
||||||
function select_authors() {
|
function select_authors() {
|
||||||
$this->get_entries(array(&$this, 'process_author'));
|
$is_wxr_file = $this->get_entries(array(&$this, 'process_author'));
|
||||||
$this->wp_authors_form();
|
if ( $is_wxr_file ) {
|
||||||
|
$this->wp_authors_form();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
echo '<h2>'.__('Invalid file').'</h2>';
|
||||||
|
echo '<p>'.__('Please upload a valid WXR (WordPress eXtended RSS) export file.').'</p>';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// fetch the user ID for a given author name, respecting the mapping preferences
|
||||||
|
function checkauthor($author) {
|
||||||
|
global $current_user;
|
||||||
|
|
||||||
|
if ( !empty($this->author_ids[$author]) )
|
||||||
|
return $this->author_ids[$author];
|
||||||
|
|
||||||
|
// failsafe: map to the current user
|
||||||
|
return $current_user->ID;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function process_categories() {
|
function process_categories() {
|
||||||
global $wpdb;
|
global $wpdb;
|
||||||
|
|
||||||
@ -515,10 +568,20 @@ class WP_Import {
|
|||||||
$headers = wp_get_http($url, $upload['file']);
|
$headers = wp_get_http($url, $upload['file']);
|
||||||
|
|
||||||
// make sure the fetch was successful
|
// make sure the fetch was successful
|
||||||
if ( $headers['response'] != '200' )
|
if ( $headers['response'] != '200' ) {
|
||||||
return new WP_Error( 'import_file_error', __(sprintf('Remote file returned error response %d', intval($headers['response']))) );
|
@unlink($upload['file']);
|
||||||
elseif ( isset($headers['content-length']) && filesize($upload['file']) != $headers['content-length'] )
|
return new WP_Error( 'import_file_error', sprintf(__('Remote file returned error response %d'), intval($headers['response'])) );
|
||||||
|
}
|
||||||
|
elseif ( isset($headers['content-length']) && filesize($upload['file']) != $headers['content-length'] ) {
|
||||||
|
@unlink($upload['file']);
|
||||||
return new WP_Error( 'import_file_error', __('Remote file is incorrect size') );
|
return new WP_Error( 'import_file_error', __('Remote file is incorrect size') );
|
||||||
|
}
|
||||||
|
|
||||||
|
$max_size = $this->max_attachment_size();
|
||||||
|
if ( !empty($max_size) and filesize($upload['file']) > $max_size ) {
|
||||||
|
@unlink($upload['file']);
|
||||||
|
return new WP_Error( 'import_file_error', sprintf(__('Remote file is too large, limit is %s', size_format($max_size))) );
|
||||||
|
}
|
||||||
|
|
||||||
// keep track of the old and new urls so we can substitute them later
|
// keep track of the old and new urls so we can substitute them later
|
||||||
$this->url_remap[$url] = $upload['url'];
|
$this->url_remap[$url] = $upload['url'];
|
||||||
@ -570,13 +633,31 @@ class WP_Import {
|
|||||||
return $key;
|
return $key;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// give the user the option of creating new users to represent authors in the import file?
|
||||||
|
function allow_create_users() {
|
||||||
|
return apply_filters('import_allow_create_users', true);
|
||||||
|
}
|
||||||
|
|
||||||
|
// give the user the option of downloading and importing attached files
|
||||||
|
function allow_fetch_attachments() {
|
||||||
|
return apply_filters('import_allow_fetch_attachments', true);
|
||||||
|
}
|
||||||
|
|
||||||
|
function max_attachment_size() {
|
||||||
|
// can be overridden with a filter - 0 means no limit
|
||||||
|
return apply_filters('import_attachment_size_limit', 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
function import($id, $fetch_attachments = false) {
|
function import($id, $fetch_attachments = false) {
|
||||||
$this->id = (int) $id;
|
$this->id = (int) $id;
|
||||||
$this->fetch_attachments = (bool) $fetch_attachments;
|
$this->fetch_attachments = ($this->allow_fetch_attachments() && (bool) $fetch_attachments);
|
||||||
|
|
||||||
add_filter('import_post_meta_key', array($this, 'is_valid_meta_key'));
|
add_filter('import_post_meta_key', array($this, 'is_valid_meta_key'));
|
||||||
|
do_action('import_start');
|
||||||
$file = get_attached_file($this->id);
|
$file = get_attached_file($this->id);
|
||||||
$this->import_file($file);
|
$this->import_file($file);
|
||||||
|
do_action('import_end');
|
||||||
}
|
}
|
||||||
|
|
||||||
function import_file($file) {
|
function import_file($file) {
|
||||||
@ -647,4 +728,4 @@ $wp_import = new WP_Import();
|
|||||||
|
|
||||||
register_importer('wordpress', 'WordPress', __('Import <strong>posts, comments, custom fields, pages, and categories</strong> from a WordPress export file'), array ($wp_import, 'dispatch'));
|
register_importer('wordpress', 'WordPress', __('Import <strong>posts, comments, custom fields, pages, and categories</strong> from a WordPress export file'), array ($wp_import, 'dispatch'));
|
||||||
|
|
||||||
?>
|
?>
|
@ -20,6 +20,7 @@ function wp_import_cleanup( $id ) {
|
|||||||
|
|
||||||
function wp_import_handle_upload() {
|
function wp_import_handle_upload() {
|
||||||
$overrides = array( 'test_form' => false, 'test_type' => false );
|
$overrides = array( 'test_form' => false, 'test_type' => false );
|
||||||
|
$_FILES['import']['name'] .= '.import';
|
||||||
$file = wp_handle_upload( $_FILES['import'], $overrides );
|
$file = wp_handle_upload( $_FILES['import'], $overrides );
|
||||||
|
|
||||||
if ( isset( $file['error'] ) )
|
if ( isset( $file['error'] ) )
|
||||||
|
Loading…
x
Reference in New Issue
Block a user