Import data uploading. wp_import_handle_upload(), wp_import_cleanup(), wp_import_upload_form().

git-svn-id: http://svn.automattic.com/wordpress/trunk@3093 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2005-11-15 23:39:32 +00:00
parent f83326f125
commit a90b0d297d
2 changed files with 67 additions and 17 deletions

View File

@ -1730,4 +1730,54 @@ function wp_shrink_dimensions($width, $height, $wmax = 128, $hmax = 96) {
return array((int) ($width / $height * $hmax), $hmax); return array((int) ($width / $height * $hmax), $hmax);
} }
function wp_import_cleanup($file) {
wp_delete_attachment($file['id']);
}
function wp_import_upload_form($action) {
?>
<script type="text/javascript">
function cancelUpload() {
o = document.getElementById('uploadForm');
o.method = 'GET';
o.action.value = 'view';
o.submit();
}
</script>
<form enctype="multipart/form-data" id="uploadForm" method="POST" action="<?php echo $action ?>">
<label for="upload"><?php _e('File:'); ?></label><input type="file" id="upload" name="import" />
<input type="hidden" name="action" value="save" />
<div id="buttons">
<input type="submit" value="<?php _e('Import'); ?>" />
<input type="button" value="<?php _e('Cancel'); ?>" onclick="cancelUpload()" />
</div>
</form>
<?php
}
function wp_import_handle_upload() {
$overrides = array('test_form' => false, 'test_type' => false);
$file = wp_handle_upload($_FILES['import'], $overrides);
if ( isset($file['error']) )
return $file;
$url = $file['url'];
$file = $file['file'];
$filename = basename($file);
// Construct the object array
$object = array(
'post_title' => $filename,
'post_content' => $url,
'post_mime_type' => 'import',
'guid' => $url
);
// Save the data
$id = wp_insert_attachment($object, $file);
return array('file' => $file, 'id' => $id);
}
?> ?>

View File

@ -7,6 +7,7 @@ define('RSSFILE', 'rss.xml');
class RSS_Import { class RSS_Import {
var $posts = array (); var $posts = array ();
var $file;
function header() { function header() {
echo '<div class="wrap">'; echo '<div class="wrap">';
@ -24,20 +25,15 @@ class RSS_Import {
} }
function greet() { function greet() {
_e("<p>Howdy! This importer allows you to extract posts from any RSS 2.0 file into your blog. This is useful if you want to import your posts from a system that is not handled by a custom import tool. To get started you must edit the following line in this file (<code>import/rss.php</code>) </p> _e("<p>Howdy! This importer allows you to extract posts from any RSS 2.0 file into your blog. This is useful if you want to import your posts from a system that is not handled by a custom import tool. Pick an RSS file to upload and click Import.</p>");
<p><code>define('RSSFILE', '');</code></p> wp_import_upload_form("admin.php?import=rss&amp;step=1");
<p>You want to define where the RSS file we'll be working with is, for example: </p>
<p><code>define('RSSFILE', 'rss.xml');</code></p>
<p>You have to do this manually for security reasons. When you're done reload this page and we'll take you to the next step.</p>");
if ('' != RSSFILE)
echo '<a href="admin.php?import=rss&amp;step=1">' . __('Begin RSS Import &raquo;') . '</a>';
} }
function get_posts() { function get_posts() {
global $wpdb; global $wpdb;
set_magic_quotes_runtime(0); set_magic_quotes_runtime(0);
$datalines = file(RSSFILE); // Read the file into an array $datalines = file($this->file); // Read the file into an array
$importdata = implode('', $datalines); // squish it $importdata = implode('', $datalines); // squish it
$importdata = str_replace(array ("\r\n", "\r"), "\n", $importdata); $importdata = str_replace(array ("\r\n", "\r"), "\n", $importdata);
@ -115,9 +111,11 @@ class RSS_Import {
echo __('Post already imported'); echo __('Post already imported');
} else { } else {
$post_id = wp_insert_post($post); $post_id = wp_insert_post($post);
if (!$post_id) if (!$post_id) {
die(__("Couldn't get post ID")); echo(__("Couldn't get post ID"));
return;
}
if (0 != count($categories)) if (0 != count($categories))
wp_create_categories($categories, $post_id); wp_create_categories($categories, $post_id);
echo __('Done !'); echo __('Done !');
@ -130,15 +128,17 @@ class RSS_Import {
} }
function import() { function import() {
// FIXME: Don't die $file = wp_import_handle_upload();
if ('' == RSSFILE) if ( isset($file['error']) ) {
die("You must edit the RSSFILE line as described on the <a href='import-mt.php'>previous page</a> to continue."); echo $file['error'];
return;
if (!file_exists(RSSFILE)) }
die("The file you specified does not seem to exist. Please check the path you've given.");
$this->file = $file['file'];
$this->get_posts(); $this->get_posts();
$this->import_posts(); $this->import_posts();
wp_import_cleanup($file);
echo '<h3>All done. <a href="' . get_option('home') . '">Have fun!</a></h3>'; echo '<h3>All done. <a href="' . get_option('home') . '">Have fun!</a></h3>';
} }