New import system. Very rough draft.

git-svn-id: http://svn.automattic.com/wordpress/trunk@2800 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2005-08-21 07:03:23 +00:00
parent d878c57a30
commit 16aa6aee11
13 changed files with 371 additions and 3 deletions

View File

@ -1382,4 +1382,16 @@ function documentation_link( $for ) {
return;
}
function register_importer($id, $name, $description, $callback) {
global $wp_importers;
$wp_importers[$id] = array($name, $description, $callback);
}
function get_importers() {
global $wp_importers;
return $wp_importers;
}
?>

View File

@ -63,7 +63,33 @@ if (isset($_GET['page'])) {
include(ABSPATH . 'wp-admin/admin-footer.php');
exit();
} else if (isset($_GET['import'])) {
$importer = $_GET['import'];
if ( validate_file($importer) ) {
die(__('Invalid importer.'));
}
if (! file_exists(ABSPATH . "wp-admin/import/$importer.php"))
die(__('Cannot load importer.'));
include(ABSPATH . "wp-admin/import/$importer.php");
$parent_file = 'import.php';
$title = __('Import');
if (! isset($_GET['noheader']))
require_once(ABSPATH . 'wp-admin/admin-header.php');
require_once(ABSPATH . 'wp-admin/upgrade-functions.php');
call_user_func($wp_importers[$importer][2]);
include(ABSPATH . 'wp-admin/admin-footer.php');
exit();
}
?>
?>

62
wp-admin/import.php Normal file
View File

@ -0,0 +1,62 @@
<?php
require_once ('admin.php');
$title = __('Import');
$parent_file = 'import.php';
require_once ('admin-header.php');
?>
<div class="wrap">
<h2><?php _e('Import'); ?></h2>
<p><?php _e('If you have posts or comments in another system WordPress can import them into your current blog. To get started, choose a system to import from below:'); ?></p>
<?php
// Load all importers so that they can register.
$import_loc = 'wp-admin/import';
$import_root = ABSPATH.$import_loc;
$imports_dir = @ dir($import_root);
if ($imports_dir) {
while (($file = $imports_dir->read()) !== false) {
if (preg_match('|^\.+$|', $file))
continue;
if (preg_match('|\.php$|', $file))
require_once("$import_root/$file");
}
}
$importers = get_importers();
if (empty ($importers)) {
_e("<p>No importers are available.</p>"); // TODO: make more helpful
} else {
?>
<table width="100%" cellpadding="3" cellspacing="3">
<?php
$style = '';
foreach ($importers as $id => $data) {
$style = ('class="alternate"' == $style || 'class="alternate active"' == $style) ? '' : 'alternate';
$action = "<a href='admin.php?import=$id' title='{$data[1]}'>{$data[0]}</a>";
if ($style != '')
$style = 'class="'.$style.'"';
echo "
<tr $style>
<td class=\"togl\">$action</td>
<td class=\"desc\">{$data[1]}</td>
</tr>";
}
?>
</table>
<?php
}
?>
</div>
<?php
include ('admin-footer.php');
?>

0
wp-admin/import/b2.php Normal file
View File

View File

View File

@ -0,0 +1,4 @@
<?php
register_importer('greymatter', 'GreyMatter', 'Import posts from your Greymatter blog', array($greymatter_import, 'dispatch'));
?>

View File

72
wp-admin/import/mt.php Normal file
View File

@ -0,0 +1,72 @@
<?php
class MT_Import {
var $authors = array();
var $posts = array();
function header() {
echo '<div class="wrap">';
echo '<h2>' . __('Import Movable Type') . '</h2>';
}
function footer() {
echo '</div>';
}
function greet() {
$this->header();
?>
<p>Howdy! We&#8217;re about to begin the process to import all of your Movable Type entries into WordPress. Before we get started, you need to edit this file (<code>import/mt.php</code>) and change one line so we know where to find your MT export file. To make this easy put the import file into the <code>wp-admin/import</code> directory. Look for the line that says:</p>
<p><code>define('MTEXPORT', '');</code></p>
<p>and change it to</p>
<p><code>define('MTEXPORT', 'import.txt');</code></p>
<p>You have to do this manually for security reasons.</p>
<p>If you've done that and you&#8217;re all ready, <a href="<?php echo add_query_arg('step', 1) ?>">let's go</a>! Remember that the import process may take a minute or so if you have a large number of entries and comments. Think of all the rebuilding time you'll be saving once it's done. :)</p>
<p>The importer is smart enough not to import duplicates, so you can run this multiple times without worry if&#8212;for whatever reason&#8212;it doesn't finish. If you get an <strong>out of memory</strong> error try splitting up the import file into pieces. </p>
<?php
$this->footer();
}
function get_entries() {
set_magic_quotes_runtime(0);
$importdata = file(MTEXPORT); // Read the file into an array
$importdata = implode('', $importdata); // squish it
$importdata = preg_replace("/(\r\n|\n|\r)/", "\n", $importdata);
$importdata = preg_replace("/\n--------\n/", "--MT-ENTRY--\n", $importdata);
$this->posts = explode("--MT-ENTRY--", $importdata);
}
function import() {
if ('' != MTEXPORT && !file_exists(MTEXPORT)) die("The file you specified does not seem to exist. Please check the path you've given.");
if ('' == MTEXPORT) die("You must edit the MTEXPORT line as described on the <a href='import-mt.php'>previous page</a> to continue.");
$this->get_entries();
}
function dispatch() {
if (empty($_GET['step']))
$step = 0;
else
$step = (int) $_GET['step'];
switch ($step) {
case 0:
$this->greet();
break;
case 1:
$this->import();
break;
}
}
function MT_Import() {
// Nothing.
}
}
$mt_import = new MT_Import();
register_importer('mt', 'Movable Type', 'Import posts and comments from your Movable Type blog', array($mt_import, 'dispatch'));
?>

186
wp-admin/import/rss.php Normal file
View File

@ -0,0 +1,186 @@
<?php
class RSS_Import {
var $authors = array ();
var $posts = array ();
function header() {
echo '<div class="wrap">';
echo '<h2>'.__('Import RSS').'</h2>';
}
function footer() {
echo '</div>';
}
function greet() {
$this->header();
?>
<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>
<p><code>define('RSSFILE', '');</code></p>
<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>
<?php if ('' != RSSFILE) : ?>
<a href="admin.php?import=rss&amp;step=1">Begin RSS Import &raquo;</a>
<?php
endif;
$this->footer();
}
function get_posts() {
set_magic_quotes_runtime(0);
$datalines = file(RSSFILE); // Read the file into an array
$importdata = implode('', $datalines); // squish it
$importdata = str_replace(array ("\r\n", "\r"), "\n", $importdata);
preg_match_all('|<item>(.*?)</item>|is', $importdata, $posts);
$this->posts = $posts[1];
}
function import_posts() {
echo '<ol>';
foreach ($this->posts as $post)
: $title = $date = $categories = $content = $post_id = '';
echo "<li>Importing post... ";
preg_match('|<title>(.*?)</title>|is', $post, $title);
$title = $wpdb->escape(trim($title[1]));
$post_name = sanitize_title($title);
preg_match('|<pubdate>(.*?)</pubdate>|is', $post, $date);
if ($date)
: $date = strtotime($date[1]);
else
: // if we don't already have something from pubDate
preg_match('|<dc:date>(.*?)</dc:date>|is', $post, $date);
$date = preg_replace('|([-+])([0-9]+):([0-9]+)$|', '\1\2\3', $date[1]);
$date = str_replace('T', ' ', $date);
$date = strtotime($date);
endif;
$post_date = gmdate('Y-m-d H:i:s', $date);
preg_match_all('|<category>(.*?)</category>|is', $post, $categories);
$categories = $categories[1];
if (!$categories)
: preg_match_all('|<dc:subject>(.*?)</dc:subject>|is', $post, $categories);
$categories = $categories[1];
endif;
preg_match('|<guid.+?>(.*?)</guid>|is', $post, $guid);
if ($guid)
$guid = $wpdb->escape(trim($guid[1]));
else
$guid = '';
preg_match('|<content:encoded>(.*?)</content:encoded>|is', $post, $content);
$content = str_replace(array ('<![CDATA[', ']]>'), '', $wpdb->escape(trim($content[1])));
if (!$content)
: // This is for feeds that put content in description
preg_match('|<description>(.*?)</description>|is', $post, $content);
$content = $wpdb->escape(unhtmlentities(trim($content[1])));
endif;
// Clean up content
$content = preg_replace('|<(/?[A-Z]+)|e', "'<' . strtolower('$1')", $content);
$content = str_replace('<br>', '<br />', $content);
$content = str_replace('<hr>', '<hr />', $content);
// This can mess up on posts with no titles, but checking content is much slower
// So we do it as a last resort
if ('' == $title)
: $dupe = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_content = '$content' AND post_date = '$post_date'");
else
: $dupe = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_title = '$title' AND post_date = '$post_date'");
endif;
// Now lets put it in the DB
if ($dupe)
: echo 'Post already imported';
else
: $wpdb->query("INSERT INTO $wpdb->posts
(post_author, post_date, post_date_gmt, post_content, post_title,post_status, comment_status, ping_status, post_name, guid)
VALUES
('$post_author', '$post_date', DATE_ADD('$post_date', INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE), '$content', '$title', 'publish', '$comment_status', '$ping_status', '$post_name', '$guid')");
$post_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_title = '$title' AND post_date = '$post_date'");
if (!$post_id)
die("couldn't get post ID");
if (0 != count($categories))
: foreach ($categories as $post_category)
: $post_category = unhtmlentities($post_category);
// See if the category exists yet
$cat_id = $wpdb->get_var("SELECT cat_ID from $wpdb->categories WHERE cat_name = '$post_category'");
if (!$cat_id && '' != trim($post_category)) {
$cat_nicename = sanitize_title($post_category);
$wpdb->query("INSERT INTO $wpdb->categories (cat_name, category_nicename) VALUES ('$post_category', '$cat_nicename')");
$cat_id = $wpdb->get_var("SELECT cat_ID from $wpdb->categories WHERE cat_name = '$post_category'");
}
if ('' == trim($post_category))
$cat_id = 1;
// Double check it's not there already
$exists = $wpdb->get_row("SELECT * FROM $wpdb->post2cat WHERE post_id = $post_id AND category_id = $cat_id");
if (!$exists) {
$wpdb->query("
INSERT INTO $wpdb->post2cat
(post_id, category_id)
VALUES
($post_id, $cat_id)
");
}
endforeach;
else
: $exists = $wpdb->get_row("SELECT * FROM $wpdb->post2cat WHERE post_id = $post_id AND category_id = 1");
if (!$exists)
$wpdb->query("INSERT INTO $wpdb->post2cat (post_id, category_id) VALUES ($post_id, 1) ");
endif;
echo 'Done!</li>';
endif;
endforeach;
echo '</ol>';
}
function import() {
// FIXME: Don't die.
if ('' != RSSFILE && !file_exists(RSSFILE)) die("The file you specified does not seem to exist. Please check the path you've given.");
if ('' == RSSFILE) die("You must edit the RSSFILE line as described on the <a href='import-mt.php'>previous page</a> to continue.");
$this->get_posts();
$this->import_posts();
echo '<h3>All done. <a href="../">Have fun!</a></h3>';
}
function dispatch() {
if (empty($_GET['step']))
$step = 0;
else
$step = (int) $_GET['step'];
switch ($step) {
case 0:
$this->greet();
break;
case 1:
$this->import();
break;
}
}
function RSS_Import() {
// Nothing.
}
}
$rss_import = new RSS_Import();
register_importer('rss', 'RSS', 'Import posts from and RSS feed', array($rss_import, 'dispatch'));
?>

View File

@ -0,0 +1,4 @@
<?php
register_importer('textpattern', 'Textpattern', 'Import posts from your Textpattern blog', array($textpattern_import, 'dispatch'));
?>

View File

@ -15,9 +15,10 @@ if ( current_user_can('edit_users') )
else
$menu[35] = array(__('Profile'), 'read', 'profile.php');
$menu[40] = array(__('Options'), 'read', 'options-personal.php');
$menu[45] = array(__('Import'), 'import', 'import.php');
if ( get_option('use_fileupload') )
$menu[45] = array(__('Upload'), 'upload_files', 'upload.php');
$menu[50] = array(__('Upload'), 'upload_files', 'upload.php');
$submenu['post.php'][5] = array(__('Write Post'), 'edit_posts', 'post.php');
$submenu['post.php'][10] = array(__('Write Page'), 'edit_pages', 'page-new.php');

View File

@ -256,6 +256,7 @@ function populate_roles() {
'edit_plugins' => true,
'edit_users' => true,
'edit_files' => true,
'import' => true,
'read' => true,
'level_10' => true,
'level_9' => true,

View File

@ -118,7 +118,7 @@ require (ABSPATH . WPINC . '/links.php');
require (ABSPATH . WPINC . '/kses.php');
require (ABSPATH . WPINC . '/version.php');
if (!strstr($_SERVER['PHP_SELF'], 'install.php') && !strstr($_SERVER['PHP_SELF'], 'wp-admin/import')) :
if (!strstr($_SERVER['PHP_SELF'], 'install.php')) :
// Used to guarantee unique hash cookies
$cookiehash = md5(get_settings('siteurl')); // Remove in 1.4
define('COOKIEHASH', $cookiehash);