diff --git a/wp-admin/admin-functions.php b/wp-admin/admin-functions.php index 8bccd432c..601488a1e 100644 --- a/wp-admin/admin-functions.php +++ b/wp-admin/admin-functions.php @@ -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; +} + ?> \ No newline at end of file diff --git a/wp-admin/admin.php b/wp-admin/admin.php index 64e602afe..289c8a325 100644 --- a/wp-admin/admin.php +++ b/wp-admin/admin.php @@ -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(); } -?> \ No newline at end of file +?> diff --git a/wp-admin/import.php b/wp-admin/import.php new file mode 100644 index 000000000..93fa7e961 --- /dev/null +++ b/wp-admin/import.php @@ -0,0 +1,62 @@ + + +
+

+

+ +read()) !== false) { + if (preg_match('|^\.+$|', $file)) + continue; + if (preg_match('|\.php$|', $file)) + require_once("$import_root/$file"); + } +} + +$importers = get_importers(); + +if (empty ($importers)) { + _e("

No importers are available.

"); // TODO: make more helpful +} else { +?> + + + $data) { + $style = ('class="alternate"' == $style || 'class="alternate active"' == $style) ? '' : 'alternate'; + $action = "{$data[0]}"; + + if ($style != '') + $style = 'class="'.$style.'"'; + echo " + + + + "; + } +?> + +
$action{$data[1]}
+ + +
+ + + diff --git a/wp-admin/import/b2.php b/wp-admin/import/b2.php new file mode 100644 index 000000000..e69de29bb diff --git a/wp-admin/import/blogger.php b/wp-admin/import/blogger.php new file mode 100644 index 000000000..e69de29bb diff --git a/wp-admin/import/greymatter.php b/wp-admin/import/greymatter.php new file mode 100644 index 000000000..803ae0e32 --- /dev/null +++ b/wp-admin/import/greymatter.php @@ -0,0 +1,4 @@ + + diff --git a/wp-admin/import/livejournal.php b/wp-admin/import/livejournal.php new file mode 100644 index 000000000..e69de29bb diff --git a/wp-admin/import/mt.php b/wp-admin/import/mt.php new file mode 100644 index 000000000..53c7df2b4 --- /dev/null +++ b/wp-admin/import/mt.php @@ -0,0 +1,72 @@ +'; + echo '

' . __('Import Movable Type') . '

'; + } + + function footer() { + echo ''; + } + + function greet() { + $this->header(); +?> +

Howdy! We’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 (import/mt.php) and change one line so we know where to find your MT export file. To make this easy put the import file into the wp-admin/import directory. Look for the line that says:

+

define('MTEXPORT', '');

+

and change it to

+

define('MTEXPORT', 'import.txt');

+

You have to do this manually for security reasons.

+

If you've done that and you’re all ready, let's go! 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. :)

+

The importer is smart enough not to import duplicates, so you can run this multiple times without worry if—for whatever reason—it doesn't finish. If you get an out of memory error try splitting up the import file into pieces.

+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 previous page 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')); + +?> \ No newline at end of file diff --git a/wp-admin/import/rss.php b/wp-admin/import/rss.php new file mode 100644 index 000000000..c43367df7 --- /dev/null +++ b/wp-admin/import/rss.php @@ -0,0 +1,186 @@ +'; + echo '

'.__('Import RSS').'

'; + } + + function footer() { + echo ''; + } + + function greet() { + $this->header(); +?> +

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 (import/rss.php)

+

define('RSSFILE', '');

+

You want to define where the RSS file we'll be working with is, for example:

+

define('RSSFILE', 'rss.xml');

+

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.

+ +Begin RSS Import » +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('|(.*?)|is', $importdata, $posts); + $this->posts = $posts[1]; + } + + function import_posts() { + echo '
    '; + foreach ($this->posts as $post) + : $title = $date = $categories = $content = $post_id = ''; + echo "
  1. Importing post... "; + + preg_match('|(.*?)|is', $post, $title); + $title = $wpdb->escape(trim($title[1])); + $post_name = sanitize_title($title); + + preg_match('|(.*?)|is', $post, $date); + + if ($date) + : $date = strtotime($date[1]); + else + : // if we don't already have something from pubDate + preg_match('|(.*?)|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('|(.*?)|is', $post, $categories); + $categories = $categories[1]; + + if (!$categories) + : preg_match_all('|(.*?)|is', $post, $categories); + $categories = $categories[1]; + endif; + + preg_match('|(.*?)|is', $post, $guid); + if ($guid) + $guid = $wpdb->escape(trim($guid[1])); + else + $guid = ''; + + preg_match('|(.*?)|is', $post, $content); + $content = str_replace(array (''), '', $wpdb->escape(trim($content[1]))); + + if (!$content) + : // This is for feeds that put content in description + preg_match('|(.*?)|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('
    ', '
    ', $content); + $content = str_replace('
    ', '
    ', $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!
  2. '; + endif; + + endforeach; + echo '
'; + + } + + + 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 previous page to continue."); + + $this->get_posts(); + $this->import_posts(); + echo '

All done. Have fun!

'; + } + + 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')); + +?> \ No newline at end of file diff --git a/wp-admin/import/textpattern.php b/wp-admin/import/textpattern.php new file mode 100644 index 000000000..e229e7db3 --- /dev/null +++ b/wp-admin/import/textpattern.php @@ -0,0 +1,4 @@ + + diff --git a/wp-admin/menu.php b/wp-admin/menu.php index 756adb745..410f6fece 100644 --- a/wp-admin/menu.php +++ b/wp-admin/menu.php @@ -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'); diff --git a/wp-admin/upgrade-schema.php b/wp-admin/upgrade-schema.php index 025ebf574..afd603dd6 100644 --- a/wp-admin/upgrade-schema.php +++ b/wp-admin/upgrade-schema.php @@ -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, diff --git a/wp-settings.php b/wp-settings.php index dc14aaf6d..6422bf329 100644 --- a/wp-settings.php +++ b/wp-settings.php @@ -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);