move importers to plugin repo, see #13307

git-svn-id: http://svn.automattic.com/wordpress/trunk@14764 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
wpmuguru 2010-05-20 20:54:28 +00:00
parent e5430249fb
commit 4976188b2f
13 changed files with 0 additions and 6745 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1,211 +0,0 @@
<?php
/**
* Blogware XML Importer
*
* @package WordPress
* @subpackage Importer
*/
/**
* Blogware XML Importer class
*
* Extract posts from Blogware XML export file into your blog.
*
* @since unknown
*/
class BW_Import {
var $file;
function header() {
echo '<div class="wrap">';
screen_icon();
echo '<h2>'.__('Import Blogware').'</h2>';
}
function footer() {
echo '</div>';
}
function greet() {
echo '<div class="narrow">';
echo '<p>'.__('Howdy! This importer allows you to extract posts from Blogware XML export file into your site. Pick a Blogware file to upload and click Import.').'</p>';
wp_import_upload_form("admin.php?import=blogware&amp;step=1");
echo '</div>';
}
function _normalize_tag( $matches ) {
return '<' . strtolower( $matches[1] );
}
function import_posts() {
global $wpdb, $current_user;
set_magic_quotes_runtime(0);
$importdata = file($this->file); // Read the file into an array
$importdata = implode('', $importdata); // squish it
$importdata = str_replace(array ("\r\n", "\r"), "\n", $importdata);
preg_match_all('|(<item[^>]+>(.*?)</item>)|is', $importdata, $posts);
$posts = $posts[1];
unset($importdata);
echo '<ol>';
foreach ($posts as $post) {
flush();
preg_match('|<item type=\"(.*?)\">|is', $post, $post_type);
$post_type = $post_type[1];
if ($post_type == "photo") {
preg_match('|<photoFilename>(.*?)</photoFilename>|is', $post, $post_title);
} else {
preg_match('|<title>(.*?)</title>|is', $post, $post_title);
}
$post_title = $wpdb->escape(trim($post_title[1]));
preg_match('|<pubDate>(.*?)</pubDate>|is', $post, $post_date);
$post_date = strtotime($post_date[1]);
$post_date = gmdate('Y-m-d H:i:s', $post_date);
preg_match_all('|<category>(.*?)</category>|is', $post, $categories);
$categories = $categories[1];
$cat_index = 0;
foreach ($categories as $category) {
$categories[$cat_index] = $wpdb->escape( html_entity_decode($category) );
$cat_index++;
}
if ( strcasecmp($post_type, "photo") === 0 ) {
preg_match('|<sizedPhotoUrl>(.*?)</sizedPhotoUrl>|is', $post, $post_content);
$post_content = '<img src="'.trim($post_content[1]).'" />';
$post_content = html_entity_decode( $post_content );
} else {
preg_match('|<body>(.*?)</body>|is', $post, $post_content);
$post_content = str_replace(array ('<![CDATA[', ']]>'), '', trim($post_content[1]));
$post_content = html_entity_decode( $post_content );
}
// Clean up content
$post_content = preg_replace_callback('|<(/?[A-Z]+)|', array( &$this, '_normalize_tag' ), $post_content);
$post_content = str_replace('<br>', '<br />', $post_content);
$post_content = str_replace('<hr>', '<hr />', $post_content);
$post_content = $wpdb->escape($post_content);
$post_author = $current_user->ID;
preg_match('|<postStatus>(.*?)</postStatus>|is', $post, $post_status);
$post_status = trim($post_status[1]);
echo '<li>';
if ($post_id = post_exists($post_title, $post_content, $post_date)) {
printf(__('Post <em>%s</em> already exists.'), stripslashes($post_title));
} else {
printf(__('Importing post <em>%s</em>...'), stripslashes($post_title));
$postdata = compact('post_author', 'post_date', 'post_content', 'post_title', 'post_status');
$post_id = wp_insert_post($postdata);
if ( is_wp_error( $post_id ) ) {
return $post_id;
}
if (!$post_id) {
_e('Couldn&#8217;t get post ID');
echo '</li>';
break;
}
if ( 0 != count($categories) )
wp_create_categories($categories, $post_id);
}
preg_match_all('|<comment>(.*?)</comment>|is', $post, $comments);
$comments = $comments[1];
if ( $comments ) {
$comment_post_ID = (int) $post_id;
$num_comments = 0;
foreach ($comments as $comment) {
preg_match('|<body>(.*?)</body>|is', $comment, $comment_content);
$comment_content = str_replace(array ('<![CDATA[', ']]>'), '', trim($comment_content[1]));
$comment_content = html_entity_decode( $comment_content );
// Clean up content
$comment_content = preg_replace_callback('|<(/?[A-Z]+)|', array( &$this, '_normalize_tag' ), $comment_content);
$comment_content = str_replace('<br>', '<br />', $comment_content);
$comment_content = str_replace('<hr>', '<hr />', $comment_content);
$comment_content = $wpdb->escape($comment_content);
preg_match('|<pubDate>(.*?)</pubDate>|is', $comment, $comment_date);
$comment_date = trim($comment_date[1]);
$comment_date = date('Y-m-d H:i:s', strtotime($comment_date));
preg_match('|<author>(.*?)</author>|is', $comment, $comment_author);
$comment_author = $wpdb->escape(trim($comment_author[1]));
$comment_author_email = NULL;
$comment_approved = 1;
// Check if it's already there
if (!comment_exists($comment_author, $comment_date)) {
$commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_email', 'comment_date', 'comment_content', 'comment_approved');
$commentdata = wp_filter_comment($commentdata);
wp_insert_comment($commentdata);
$num_comments++;
}
}
}
if ( $num_comments ) {
echo ' ';
printf( _n('%s comment', '%s comments', $num_comments), $num_comments );
}
echo '</li>';
flush();
ob_flush();
}
echo '</ol>';
}
function import() {
$file = wp_import_handle_upload();
if ( isset($file['error']) ) {
echo $file['error'];
return;
}
$this->file = $file['file'];
$result = $this->import_posts();
if ( is_wp_error( $result ) )
return $result;
wp_import_cleanup($file['id']);
do_action('import_done', 'blogware');
echo '<h3>';
printf(__('All done. <a href="%s">Have fun!</a>'), get_option('home'));
echo '</h3>';
}
function dispatch() {
if (empty ($_GET['step']))
$step = 0;
else
$step = (int) $_GET['step'];
$this->header();
switch ($step) {
case 0 :
$this->greet();
break;
case 1 :
$result = $this->import();
if ( is_wp_error( $result ) )
$result->get_error_message();
break;
}
$this->footer();
}
function BW_Import() {
// Nothing.
}
}
$blogware_import = new BW_Import();
register_importer('blogware', __('Blogware'), __('Import posts from Blogware.'), array ($blogware_import, 'dispatch'));
?>

View File

@ -1,675 +0,0 @@
<?php
/**
* DotClear Importer
*
* @package WordPress
* @subpackage Importer
*/
/**
Add These Functions to make our lives easier
**/
if (!function_exists('get_comment_count')) {
/**
* Get the comment count for posts.
*
* @package WordPress
* @subpackage Dotclear_Import
*
* @param int $post_ID Post ID
* @return int
*/
function get_comment_count($post_ID)
{
global $wpdb;
return $wpdb->get_var( $wpdb->prepare("SELECT count(*) FROM $wpdb->comments WHERE comment_post_ID = %d", $post_ID) );
}
}
if (!function_exists('link_exists')) {
/**
* Check whether link already exists.
*
* @package WordPress
* @subpackage Dotclear_Import
*
* @param string $linkname
* @return int
*/
function link_exists($linkname)
{
global $wpdb;
return $wpdb->get_var( $wpdb->prepare("SELECT link_id FROM $wpdb->links WHERE link_name = %s", $linkname) );
}
}
/**
* Convert from dotclear charset to utf8 if required
*
* @package WordPress
* @subpackage Dotclear_Import
*
* @param string $s
* @return string
*/
function csc ($s) {
if (seems_utf8 ($s)) {
return $s;
} else {
return iconv(get_option ("dccharset"),"UTF-8",$s);
}
}
/**
* @package WordPress
* @subpackage Dotclear_Import
*
* @param string $s
* @return string
*/
function textconv ($s) {
return csc (preg_replace ('|(?<!<br />)\s*\n|', ' ', $s));
}
/**
* Dotclear Importer class
*
* Will process the WordPress eXtended RSS files that you upload from the export
* file.
*
* @package WordPress
* @subpackage Importer
*
* @since unknown
*/
class Dotclear_Import {
function header()
{
echo '<div class="wrap">';
screen_icon();
echo '<h2>'.__('Import DotClear').'</h2>';
echo '<p>'.__('Steps may take a few minutes depending on the size of your database. Please be patient.').'</p>';
}
function footer()
{
echo '</div>';
}
function greet()
{
echo '<div class="narrow"><p>'.__('Howdy! This importer allows you to extract posts from a DotClear database into your WordPress site. Mileage may vary.').'</p>';
echo '<p>'.__('Your DotClear Configuration settings are as follows:').'</p>';
echo '<form action="admin.php?import=dotclear&amp;step=1" method="post">';
wp_nonce_field('import-dotclear');
$this->db_form();
echo '<p class="submit"><input type="submit" name="submit" class="button" value="'.esc_attr__('Import Categories').'" /></p>';
echo '</form></div>';
}
function get_dc_cats()
{
global $wpdb;
// General Housekeeping
$dcdb = new wpdb(get_option('dcuser'), get_option('dcpass'), get_option('dcname'), get_option('dchost'));
set_magic_quotes_runtime(0);
$dbprefix = get_option('dcdbprefix');
// Get Categories
return $dcdb->get_results('SELECT * FROM '.$dbprefix.'categorie', ARRAY_A);
}
function get_dc_users()
{
global $wpdb;
// General Housekeeping
$dcdb = new wpdb(get_option('dcuser'), get_option('dcpass'), get_option('dcname'), get_option('dchost'));
set_magic_quotes_runtime(0);
$dbprefix = get_option('dcdbprefix');
// Get Users
return $dcdb->get_results('SELECT * FROM '.$dbprefix.'user', ARRAY_A);
}
function get_dc_posts()
{
// General Housekeeping
$dcdb = new wpdb(get_option('dcuser'), get_option('dcpass'), get_option('dcname'), get_option('dchost'));
set_magic_quotes_runtime(0);
$dbprefix = get_option('dcdbprefix');
// Get Posts
return $dcdb->get_results('SELECT '.$dbprefix.'post.*, '.$dbprefix.'categorie.cat_libelle_url AS post_cat_name
FROM '.$dbprefix.'post INNER JOIN '.$dbprefix.'categorie
ON '.$dbprefix.'post.cat_id = '.$dbprefix.'categorie.cat_id', ARRAY_A);
}
function get_dc_comments()
{
global $wpdb;
// General Housekeeping
$dcdb = new wpdb(get_option('dcuser'), get_option('dcpass'), get_option('dcname'), get_option('dchost'));
set_magic_quotes_runtime(0);
$dbprefix = get_option('dcdbprefix');
// Get Comments
return $dcdb->get_results('SELECT * FROM '.$dbprefix.'comment', ARRAY_A);
}
function get_dc_links()
{
//General Housekeeping
$dcdb = new wpdb(get_option('dcuser'), get_option('dcpass'), get_option('dcname'), get_option('dchost'));
set_magic_quotes_runtime(0);
$dbprefix = get_option('dcdbprefix');
return $dcdb->get_results('SELECT * FROM '.$dbprefix.'link ORDER BY position', ARRAY_A);
}
function cat2wp($categories='')
{
// General Housekeeping
global $wpdb;
$count = 0;
$dccat2wpcat = array();
// Do the Magic
if (is_array($categories)) {
echo '<p>'.__('Importing Categories...').'<br /><br /></p>';
foreach ($categories as $category) {
$count++;
extract($category);
// Make Nice Variables
$name = $wpdb->escape($cat_libelle_url);
$title = $wpdb->escape(csc ($cat_libelle));
$desc = $wpdb->escape(csc ($cat_desc));
if ($cinfo = category_exists($name)) {
$ret_id = wp_insert_category(array('cat_ID' => $cinfo, 'category_nicename' => $name, 'cat_name' => $title, 'category_description' => $desc));
} else {
$ret_id = wp_insert_category(array('category_nicename' => $name, 'cat_name' => $title, 'category_description' => $desc));
}
$dccat2wpcat[$id] = $ret_id;
}
// Store category translation for future use
add_option('dccat2wpcat',$dccat2wpcat);
echo '<p>'.sprintf(_n('Done! <strong>%1$s</strong> category imported.', 'Done! <strong>%1$s</strong> categories imported.', $count), $count).'<br /><br /></p>';
return true;
}
echo __('No Categories to Import!');
return false;
}
function users2wp($users='') {
// General Housekeeping
global $wpdb;
$count = 0;
$dcid2wpid = array();
// Midnight Mojo
if (is_array($users)) {
echo '<p>'.__('Importing Users...').'<br /><br /></p>';
foreach ($users as $user) {
$count++;
extract($user);
// Make Nice Variables
$name = $wpdb->escape(csc ($name));
$RealName = $wpdb->escape(csc ($user_pseudo));
if ($uinfo = get_userdatabylogin($name)) {
$ret_id = wp_insert_user(array(
'ID' => $uinfo->ID,
'user_login' => $user_id,
'user_nicename' => $Realname,
'user_email' => $user_email,
'user_url' => 'http://',
'display_name' => $Realname)
);
} else {
$ret_id = wp_insert_user(array(
'user_login' => $user_id,
'user_nicename' => csc ($user_pseudo),
'user_email' => $user_email,
'user_url' => 'http://',
'display_name' => $Realname)
);
}
$dcid2wpid[$user_id] = $ret_id;
// Set DotClear-to-WordPress permissions translation
// Update Usermeta Data
$user = new WP_User($ret_id);
$wp_perms = $user_level + 1;
if (10 == $wp_perms) { $user->set_role('administrator'); }
else if (9 == $wp_perms) { $user->set_role('editor'); }
else if (5 <= $wp_perms) { $user->set_role('editor'); }
else if (4 <= $wp_perms) { $user->set_role('author'); }
else if (3 <= $wp_perms) { $user->set_role('contributor'); }
else if (2 <= $wp_perms) { $user->set_role('contributor'); }
else { $user->set_role('subscriber'); }
update_user_meta( $ret_id, 'wp_user_level', $wp_perms);
update_user_meta( $ret_id, 'rich_editing', 'false');
update_user_meta( $ret_id, 'first_name', csc ($user_prenom));
update_user_meta( $ret_id, 'last_name', csc ($user_nom));
}// End foreach($users as $user)
// Store id translation array for future use
add_option('dcid2wpid',$dcid2wpid);
echo '<p>'.sprintf(__('Done! <strong>%1$s</strong> users imported.'), $count).'<br /><br /></p>';
return true;
}// End if(is_array($users)
echo __('No Users to Import!');
return false;
}// End function user2wp()
function posts2wp($posts='') {
// General Housekeeping
global $wpdb;
$count = 0;
$dcposts2wpposts = array();
$cats = array();
// Do the Magic
if (is_array($posts)) {
echo '<p>'.__('Importing Posts...').'<br /><br /></p>';
foreach($posts as $post)
{
$count++;
extract($post);
// Set DotClear-to-WordPress status translation
$stattrans = array(0 => 'draft', 1 => 'publish');
$comment_status_map = array (0 => 'closed', 1 => 'open');
//Can we do this more efficiently?
$uinfo = ( get_userdatabylogin( $user_id ) ) ? get_userdatabylogin( $user_id ) : 1;
$authorid = ( is_object( $uinfo ) ) ? $uinfo->ID : $uinfo ;
$Title = $wpdb->escape(csc ($post_titre));
$post_content = textconv ($post_content);
$post_excerpt = "";
if ($post_chapo != "") {
$post_excerpt = textconv ($post_chapo);
$post_content = $post_excerpt ."\n<!--more-->\n".$post_content;
}
$post_excerpt = $wpdb->escape ($post_excerpt);
$post_content = $wpdb->escape ($post_content);
$post_status = $stattrans[$post_pub];
// Import Post data into WordPress
if ($pinfo = post_exists($Title,$post_content)) {
$ret_id = wp_insert_post(array(
'ID' => $pinfo,
'post_author' => $authorid,
'post_date' => $post_dt,
'post_date_gmt' => $post_dt,
'post_modified' => $post_upddt,
'post_modified_gmt' => $post_upddt,
'post_title' => $Title,
'post_content' => $post_content,
'post_excerpt' => $post_excerpt,
'post_status' => $post_status,
'post_name' => $post_titre_url,
'comment_status' => $comment_status_map[$post_open_comment],
'ping_status' => $comment_status_map[$post_open_tb],
'comment_count' => $post_nb_comment + $post_nb_trackback)
);
if ( is_wp_error( $ret_id ) )
return $ret_id;
} else {
$ret_id = wp_insert_post(array(
'post_author' => $authorid,
'post_date' => $post_dt,
'post_date_gmt' => $post_dt,
'post_modified' => $post_modified_gmt,
'post_modified_gmt' => $post_modified_gmt,
'post_title' => $Title,
'post_content' => $post_content,
'post_excerpt' => $post_excerpt,
'post_status' => $post_status,
'post_name' => $post_titre_url,
'comment_status' => $comment_status_map[$post_open_comment],
'ping_status' => $comment_status_map[$post_open_tb],
'comment_count' => $post_nb_comment + $post_nb_trackback)
);
if ( is_wp_error( $ret_id ) )
return $ret_id;
}
$dcposts2wpposts[$post_id] = $ret_id;
// Make Post-to-Category associations
$cats = array();
$category1 = get_category_by_slug($post_cat_name);
$category1 = $category1->term_id;
if ($cat1 = $category1) { $cats[1] = $cat1; }
if (!empty($cats)) { wp_set_post_categories($ret_id, $cats); }
}
}
// Store ID translation for later use
add_option('dcposts2wpposts',$dcposts2wpposts);
echo '<p>'.sprintf(__('Done! <strong>%1$s</strong> posts imported.'), $count).'<br /><br /></p>';
return true;
}
function comments2wp($comments='') {
// General Housekeeping
global $wpdb;
$count = 0;
$dccm2wpcm = array();
$postarr = get_option('dcposts2wpposts');
// Magic Mojo
if (is_array($comments)) {
echo '<p>'.__('Importing Comments...').'<br /><br /></p>';
foreach ($comments as $comment) {
$count++;
extract($comment);
// WordPressify Data
$comment_ID = (int) ltrim($comment_id, '0');
$comment_post_ID = (int) $postarr[$post_id];
$comment_approved = $comment_pub;
$name = $wpdb->escape(csc ($comment_auteur));
$email = $wpdb->escape($comment_email);
$web = "http://".$wpdb->escape($comment_site);
$message = $wpdb->escape(textconv ($comment_content));
$comment = array(
'comment_post_ID' => $comment_post_ID,
'comment_author' => $name,
'comment_author_email' => $email,
'comment_author_url' => $web,
'comment_author_IP' => $comment_ip,
'comment_date' => $comment_dt,
'comment_date_gmt' => $comment_dt,
'comment_content' => $message,
'comment_approved' => $comment_approved);
$comment = wp_filter_comment($comment);
if ( $cinfo = comment_exists($name, $comment_dt) ) {
// Update comments
$comment['comment_ID'] = $cinfo;
$ret_id = wp_update_comment($comment);
} else {
// Insert comments
$ret_id = wp_insert_comment($comment);
}
$dccm2wpcm[$comment_ID] = $ret_id;
}
// Store Comment ID translation for future use
add_option('dccm2wpcm', $dccm2wpcm);
// Associate newly formed categories with posts
get_comment_count($ret_id);
echo '<p>'.sprintf(__('Done! <strong>%1$s</strong> comments imported.'), $count).'<br /><br /></p>';
return true;
}
echo __('No Comments to Import!');
return false;
}
function links2wp($links='') {
// General Housekeeping
global $wpdb;
$count = 0;
// Deal with the links
if (is_array($links)) {
echo '<p>'.__('Importing Links...').'<br /><br /></p>';
foreach ($links as $link) {
$count++;
extract($link);
if ($title != "") {
if ($cinfo = is_term(csc ($title), 'link_category')) {
$category = $cinfo['term_id'];
} else {
$category = wp_insert_term($wpdb->escape (csc ($title)), 'link_category');
$category = $category['term_id'];
}
} else {
$linkname = $wpdb->escape(csc ($label));
$description = $wpdb->escape(csc ($title));
if ($linfo = link_exists($linkname)) {
$ret_id = wp_insert_link(array(
'link_id' => $linfo,
'link_url' => $href,
'link_name' => $linkname,
'link_category' => $category,
'link_description' => $description)
);
} else {
$ret_id = wp_insert_link(array(
'link_url' => $url,
'link_name' => $linkname,
'link_category' => $category,
'link_description' => $description)
);
}
$dclinks2wplinks[$link_id] = $ret_id;
}
}
add_option('dclinks2wplinks',$dclinks2wplinks);
echo '<p>';
printf(_n('Done! <strong>%s</strong> link or link category imported.', 'Done! <strong>%s</strong> links or link categories imported.', $count), $count);
echo '<br /><br /></p>';
return true;
}
echo __('No Links to Import!');
return false;
}
function import_categories() {
// Category Import
$cats = $this->get_dc_cats();
$this->cat2wp($cats);
add_option('dc_cats', $cats);
echo '<form action="admin.php?import=dotclear&amp;step=2" method="post">';
wp_nonce_field('import-dotclear');
printf('<p class="submit"><input type="submit" name="submit" class="button" value="%s" /></p>', esc_attr__('Import Users'));
echo '</form>';
}
function import_users() {
// User Import
$users = $this->get_dc_users();
$this->users2wp($users);
echo '<form action="admin.php?import=dotclear&amp;step=3" method="post">';
wp_nonce_field('import-dotclear');
printf('<p class="submit"><input type="submit" name="submit" class="button" value="%s" /></p>', esc_attr__('Import Posts'));
echo '</form>';
}
function import_posts() {
// Post Import
$posts = $this->get_dc_posts();
$result = $this->posts2wp($posts);
if ( is_wp_error( $result ) )
return $result;
echo '<form action="admin.php?import=dotclear&amp;step=4" method="post">';
wp_nonce_field('import-dotclear');
printf('<p class="submit"><input type="submit" name="submit" class="button" value="%s" /></p>', esc_attr__('Import Comments'));
echo '</form>';
}
function import_comments() {
// Comment Import
$comments = $this->get_dc_comments();
$this->comments2wp($comments);
echo '<form action="admin.php?import=dotclear&amp;step=5" method="post">';
wp_nonce_field('import-dotclear');
printf('<p class="submit"><input type="submit" name="submit" class="button" value="%s" /></p>', esc_attr__('Import Links'));
echo '</form>';
}
function import_links()
{
//Link Import
$links = $this->get_dc_links();
$this->links2wp($links);
add_option('dc_links', $links);
echo '<form action="admin.php?import=dotclear&amp;step=6" method="post">';
wp_nonce_field('import-dotclear');
printf('<p class="submit"><input type="submit" name="submit" class="button" value="%s" /></p>', esc_attr__('Finish'));
echo '</form>';
}
function cleanup_dcimport() {
delete_option('dcdbprefix');
delete_option('dc_cats');
delete_option('dcid2wpid');
delete_option('dccat2wpcat');
delete_option('dcposts2wpposts');
delete_option('dccm2wpcm');
delete_option('dclinks2wplinks');
delete_option('dcuser');
delete_option('dcpass');
delete_option('dcname');
delete_option('dchost');
delete_option('dccharset');
do_action('import_done', 'dotclear');
$this->tips();
}
function tips() {
echo '<p>'.__('Welcome to WordPress. We hope (and expect!) that you will find this platform incredibly rewarding! As a new WordPress user coming from DotClear, there are some things that we would like to point out. Hopefully, they will help your transition go as smoothly as possible.').'</p>';
echo '<h3>'.__('Users').'</h3>';
echo '<p>'.sprintf(__('You have already setup WordPress and have been assigned an administrative login and password. Forget it. You didn&#8217;t have that login in DotClear, why should you have it here? Instead we have taken care to import all of your users into our system. Unfortunately there is one downside. Because both WordPress and DotClear uses a strong encryption hash with passwords, it is impossible to decrypt it and we are forced to assign temporary passwords to all your users. <strong>Every user has the same username, but their passwords are reset to password123.</strong> So <a href="%1$s">Log in</a> and change it.'), '/wp-login.php').'</p>';
echo '<h3>'.__('Preserving Authors').'</h3>';
echo '<p>'.__('Secondly, we have attempted to preserve post authors. If you are the only author or contributor to your blog, then you are safe. In most cases, we are successful in this preservation endeavor. However, if we cannot ascertain the name of the writer due to discrepancies between database tables, we assign it to you, the administrative user.').'</p>';
echo '<h3>'.__('Textile').'</h3>';
echo '<p>'.__('Also, since you&#8217;re coming from DotClear, you probably have been using Textile to format your comments and posts. If this is the case, we recommend downloading and installing <a href="http://www.huddledmasses.org/category/development/wordpress/textile/">Textile for WordPress</a>. Trust me&#8230; You&#8217;ll want it.').'</p>';
echo '<h3>'.__('WordPress Resources').'</h3>';
echo '<p>'.__('Finally, there are numerous WordPress resources around the internet. Some of them are:').'</p>';
echo '<ul>';
echo '<li>'.__('<a href="http://wordpress.org/">The official WordPress site</a>').'</li>';
echo '<li>'.__('<a href="http://wordpress.org/support/">The WordPress support forums</a>').'</li>';
echo '<li>'.__('<a href="http://codex.wordpress.org/">The Codex (In other words, the WordPress Bible)</a>').'</li>';
echo '</ul>';
echo '<p>'.sprintf(__('That&#8217;s it! What are you waiting for? Go <a href="%1$s">log in</a>!'), '../wp-login.php').'</p>';
}
function db_form() {
echo '<table class="form-table">';
printf('<tr><th><label for="dbuser">%s</label></th><td><input type="text" name="dbuser" id="dbuser" /></td></tr>', __('DotClear Database User:'));
printf('<tr><th><label for="dbpass">%s</label></th><td><input type="password" name="dbpass" id="dbpass" /></td></tr>', __('DotClear Database Password:'));
printf('<tr><th><label for="dbname">%s</label></th><td><input type="text" name="dbname" id="dbname" /></td></tr>', __('DotClear Database Name:'));
printf('<tr><th><label for="dbhost">%s</label></th><td><input type="text" name="dbhost" id="dbhost" value="localhost" /></td></tr>', __('DotClear Database Host:'));
printf('<tr><th><label for="dbprefix">%s</label></th><td><input type="text" name="dbprefix" id="dbprefix" value="dc_"/></td></tr>', __('DotClear Table prefix:'));
printf('<tr><th><label for="dccharset">%s</label></th><td><input type="text" name="dccharset" id="dccharset" value="ISO-8859-15"/></td></tr>', __('Originating character set:'));
echo '</table>';
}
function dispatch() {
if (empty ($_GET['step']))
$step = 0;
else
$step = (int) $_GET['step'];
$this->header();
if ( $step > 0 ) {
check_admin_referer('import-dotclear');
if ($_POST['dbuser']) {
if(get_option('dcuser'))
delete_option('dcuser');
add_option('dcuser', sanitize_user($_POST['dbuser'], true));
}
if ($_POST['dbpass']) {
if(get_option('dcpass'))
delete_option('dcpass');
add_option('dcpass', sanitize_user($_POST['dbpass'], true));
}
if ($_POST['dbname']) {
if (get_option('dcname'))
delete_option('dcname');
add_option('dcname', sanitize_user($_POST['dbname'], true));
}
if ($_POST['dbhost']) {
if(get_option('dchost'))
delete_option('dchost');
add_option('dchost', sanitize_user($_POST['dbhost'], true));
}
if ($_POST['dccharset']) {
if (get_option('dccharset'))
delete_option('dccharset');
add_option('dccharset', sanitize_user($_POST['dccharset'], true));
}
if ($_POST['dbprefix']) {
if (get_option('dcdbprefix'))
delete_option('dcdbprefix');
add_option('dcdbprefix', sanitize_user($_POST['dbprefix'], true));
}
}
switch ($step) {
default:
case 0 :
$this->greet();
break;
case 1 :
$this->import_categories();
break;
case 2 :
$this->import_users();
break;
case 3 :
$result = $this->import_posts();
if ( is_wp_error( $result ) )
echo $result->get_error_message();
break;
case 4 :
$this->import_comments();
break;
case 5 :
$this->import_links();
break;
case 6 :
$this->cleanup_dcimport();
break;
}
$this->footer();
}
function Dotclear_Import() {
// Nothing.
}
}
$dc_import = new Dotclear_Import();
register_importer('dotclear', __('DotClear'), __('Import categories, users, posts, comments, and links from a DotClear blog.'), array ($dc_import, 'dispatch'));
?>

View File

@ -1,334 +0,0 @@
<?php
/**
* GreyMatter Importer
*
* @package WordPress
* @subpackage Importer
*/
/**
* GreyMatter Importer class
*
* Basic GreyMatter to WordPress importer, will import posts, comments, and
* posts karma.
*
* @since unknown
*/
class GM_Import {
var $gmnames = array ();
function header() {
echo '<div class="wrap">';
screen_icon();
echo '<h2>'.__('Import GreyMatter').'</h2>';
}
function footer() {
echo '</div>';
}
function greet() {
$this->header();
?>
<p><?php _e('This is a basic GreyMatter to WordPress import script.') ?></p>
<p><?php _e('What it does:') ?></p>
<ul>
<li><?php _e('Parses gm-authors.cgi to import (new) authors. Everyone is imported at level 1.') ?></li>
<li><?php _e('Parses the entries cgi files to import posts, comments, and karma on posts (although karma is not used on WordPress yet).<br />If authors are found not to be in gm-authors.cgi, imports them at level 0.') ?></li>
<li><?php _e("Detects duplicate entries or comments. If you don't import everything the first time, or this import should fail in the middle, duplicate entries will not be made when you try again.") ?></li>
</ul>
<p><?php _e('What it does not:') ?></p>
<ul>
<li><?php _e('Parse gm-counter.cgi, gm-banlist.cgi, gm-cplog.cgi (you can make a CP log hack if you really feel like it, but I question the need of a CP log).') ?></li>
<li><?php _e('Import gm-templates.') ?></li>
<li><?php _e("Doesn&#8217;t keep entries on top.")?></li>
</ul>
<p>&nbsp;</p>
<form name="stepOne" method="get" action="">
<input type="hidden" name="import" value="greymatter" />
<input type="hidden" name="step" value="1" />
<?php wp_nonce_field('import-greymatter'); ?>
<h3><?php _e('Second step: GreyMatter details:') ?></h3>
<table class="form-table">
<tr>
<td><label for="gmpath"><?php _e('Path to GM files:') ?></label></td>
<td><input type="text" style="width:300px" name="gmpath" id="gmpath" value="/home/my/site/cgi-bin/greymatter/" /></td>
</tr>
<tr>
<td><label for="archivespath"><?php _e('Path to GM entries:') ?></label></td>
<td><input type="text" style="width:300px" name="archivespath" id="archivespath" value="/home/my/site/cgi-bin/greymatter/archives/" /></td>
</tr>
<tr>
<td><label for="lastentry"><?php _e('Last entry&#8217;s number:') ?></label></td>
<td><input type="text" name="lastentry" id="lastentry" value="00000001" /><br />
<?php _e('This importer will search for files 00000001.cgi to 000-whatever.cgi,<br />so you need to enter the number of the last GM post here.<br />(if you don&#8217;t know that number, just log in to your FTP and look it out<br />in the entries&#8217; folder)') ?></td>
</tr>
</table>
<p class="submit"><input type="submit" name="submit" class="button" value="<?php esc_attr_e('Start Importing') ?>" /></p>
</form>
<?php
$this->footer();
}
function gm2autobr($string) { // transforms GM's |*| into b2's <br />\n
$string = str_replace("|*|","<br />\n",$string);
return($string);
}
function import() {
global $wpdb;
$wpvarstoreset = array('gmpath', 'archivespath', 'lastentry');
for ($i=0; $i<count($wpvarstoreset); $i += 1) {
$wpvar = $wpvarstoreset[$i];
if (!isset($$wpvar)) {
if (empty($_POST[$wpvar])) {
if (empty($_GET[$wpvar])) {
$$wpvar = '';
} else {
$$wpvar = $_GET[$wpvar];
}
} else {
$$wpvar = $_POST[$wpvar];
}
}
}
if (!chdir($archivespath))
wp_die(__("Wrong path, the path to the GM entries does not exist on the server"));
if (!chdir($gmpath))
wp_die(__("Wrong path, the path to the GM files does not exist on the server"));
$lastentry = (int) $lastentry;
$this->header();
?>
<p><?php _e('The importer is running...') ?></p>
<ul>
<li><?php _e('importing users...') ?><ul><?php
chdir($gmpath);
$userbase = file("gm-authors.cgi");
foreach($userbase as $user) {
$userdata=explode("|", $user);
$user_ip="127.0.0.1";
$user_domain="localhost";
$user_browser="server";
$s=$userdata[4];
$user_joindate=substr($s,6,4)."-".substr($s,0,2)."-".substr($s,3,2)." 00:00:00";
$user_login=$wpdb->escape($userdata[0]);
$pass1=$wpdb->escape($userdata[1]);
$user_nickname=$wpdb->escape($userdata[0]);
$user_email=$wpdb->escape($userdata[2]);
$user_url=$wpdb->escape($userdata[3]);
$user_joindate=$wpdb->escape($user_joindate);
$user_id = username_exists($user_login);
if ($user_id) {
printf('<li>'.__('user %s').'<strong>'.__('Already exists').'</strong></li>', "<em>$user_login</em>");
$this->gmnames[$userdata[0]] = $user_id;
continue;
}
$user_info = array("user_login"=>$user_login, "user_pass"=>$pass1, "user_nickname"=>$user_nickname, "user_email"=>$user_email, "user_url"=>$user_url, "user_ip"=>$user_ip, "user_domain"=>$user_domain, "user_browser"=>$user_browser, "dateYMDhour"=>$user_joindate, "user_level"=>"1", "user_idmode"=>"nickname");
$user_id = wp_insert_user($user_info);
$this->gmnames[$userdata[0]] = $user_id;
printf('<li>'.__('user %s...').' <strong>'.__('Done').'</strong></li>', "<em>$user_login</em>");
}
?></ul><strong><?php _e('Done') ?></strong></li>
<li><?php _e('importing posts, comments, and karma...') ?><br /><ul><?php
chdir($archivespath);
for($i = 0; $i <= $lastentry; $i = $i + 1) {
$entryfile = "";
if ($i<10000000) {
$entryfile .= "0";
if ($i<1000000) {
$entryfile .= "0";
if ($i<100000) {
$entryfile .= "0";
if ($i<10000) {
$entryfile .= "0";
if ($i<1000) {
$entryfile .= "0";
if ($i<100) {
$entryfile .= "0";
if ($i<10) {
$entryfile .= "0";
}}}}}}}
$entryfile .= $i;
if (is_file($entryfile.".cgi")) {
$entry=file($entryfile.".cgi");
$postinfo=explode("|",$entry[0]);
$postmaincontent=$this->gm2autobr($entry[2]);
$postmorecontent=$this->gm2autobr($entry[3]);
$post_author=trim($wpdb->escape($postinfo[1]));
$post_title=$this->gm2autobr($postinfo[2]);
printf('<li>'.__('entry # %s : %s : by %s'), $entryfile, $post_title, $postinfo[1]);
$post_title=$wpdb->escape($post_title);
$postyear=$postinfo[6];
$postmonth=zeroise($postinfo[4],2);
$postday=zeroise($postinfo[5],2);
$posthour=zeroise($postinfo[7],2);
$postminute=zeroise($postinfo[8],2);
$postsecond=zeroise($postinfo[9],2);
if (($postinfo[10]=="PM") && ($posthour!="12"))
$posthour=$posthour+12;
$post_date="$postyear-$postmonth-$postday $posthour:$postminute:$postsecond";
$post_content=$postmaincontent;
if (strlen($postmorecontent)>3)
$post_content .= "<!--more--><br /><br />".$postmorecontent;
$post_content=$wpdb->escape($post_content);
$post_karma=$postinfo[12];
$post_status = 'publish'; //in greymatter, there are no drafts
$comment_status = 'open';
$ping_status = 'closed';
if ($post_ID = post_exists($post_title, '', $post_date)) {
echo ' ';
_e('(already exists)');
} else {
//just so that if a post already exists, new users are not created by checkauthor
// we'll check the author is registered, or if it's a deleted author
$user_id = username_exists($post_author);
if (!$user_id) { // if deleted from GM, we register the author as a level 0 user
$user_ip="127.0.0.1";
$user_domain="localhost";
$user_browser="server";
$user_joindate="1979-06-06 00:41:00";
$user_login=$wpdb->escape($post_author);
$pass1=$wpdb->escape("password");
$user_nickname=$wpdb->escape($post_author);
$user_email=$wpdb->escape("user@deleted.com");
$user_url=$wpdb->escape("");
$user_joindate=$wpdb->escape($user_joindate);
$user_info = array("user_login"=>$user_login, "user_pass"=>$pass1, "user_nickname"=>$user_nickname, "user_email"=>$user_email, "user_url"=>$user_url, "user_ip"=>$user_ip, "user_domain"=>$user_domain, "user_browser"=>$user_browser, "dateYMDhour"=>$user_joindate, "user_level"=>0, "user_idmode"=>"nickname");
$user_id = wp_insert_user($user_info);
$this->gmnames[$postinfo[1]] = $user_id;
echo ': ';
printf(__('registered deleted user %s at level 0 '), "<em>$user_login</em>");
}
if (array_key_exists($postinfo[1], $this->gmnames)) {
$post_author = $this->gmnames[$postinfo[1]];
} else {
$post_author = $user_id;
}
$postdata = compact('post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_excerpt', 'post_status', 'comment_status', 'ping_status', 'post_modified', 'post_modified_gmt');
$post_ID = wp_insert_post($postdata);
if ( is_wp_error( $post_ID ) )
return $post_ID;
}
$c=count($entry);
if ($c>4) {
$numAddedComments = 0;
$numComments = 0;
for ($j=4;$j<$c;$j++) {
$entry[$j]=$this->gm2autobr($entry[$j]);
$commentinfo=explode("|",$entry[$j]);
$comment_post_ID=$post_ID;
$comment_author=$wpdb->escape($commentinfo[0]);
$comment_author_email=$wpdb->escape($commentinfo[2]);
$comment_author_url=$wpdb->escape($commentinfo[3]);
$comment_author_IP=$wpdb->escape($commentinfo[1]);
$commentyear=$commentinfo[7];
$commentmonth=zeroise($commentinfo[5],2);
$commentday=zeroise($commentinfo[6],2);
$commenthour=zeroise($commentinfo[8],2);
$commentminute=zeroise($commentinfo[9],2);
$commentsecond=zeroise($commentinfo[10],2);
if (($commentinfo[11]=="PM") && ($commenthour!="12"))
$commenthour=$commenthour+12;
$comment_date="$commentyear-$commentmonth-$commentday $commenthour:$commentminute:$commentsecond";
$comment_content=$wpdb->escape($commentinfo[12]);
if (!comment_exists($comment_author, $comment_date)) {
$commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_url', 'comment_author_email', 'comment_author_IP', 'comment_date', 'comment_content', 'comment_approved');
$commentdata = wp_filter_comment($commentdata);
wp_insert_comment($commentdata);
$numAddedComments++;
}
$numComments++;
}
if ($numAddedComments > 0) {
echo ': ';
printf( _n('imported %s comment', 'imported %s comments', $numAddedComments) , $numAddedComments);
}
$preExisting = $numComments - numAddedComments;
if ($preExisting > 0) {
echo ' ';
printf( _n( 'ignored %s pre-existing comment', 'ignored %s pre-existing comments', $preExisting ) , $preExisting);
}
}
echo '... <strong>'.__('Done').'</strong></li>';
}
}
do_action('import_done', 'greymatter');
?>
</ul><strong><?php _e('Done') ?></strong></li></ul>
<p>&nbsp;</p>
<p><?php _e('Completed GreyMatter import!') ?></p>
<?php
$this->footer();
return;
}
function dispatch() {
if (empty ($_GET['step']))
$step = 0;
else
$step = (int) $_GET['step'];
switch ($step) {
case 0 :
$this->greet();
break;
case 1:
check_admin_referer('import-greymatter');
$result = $this->import();
if ( is_wp_error( $result ) )
echo $result->get_error_message();
break;
}
}
function GM_Import() {
// Nothing.
}
}
$gm_import = new GM_Import();
register_importer('greymatter', __('GreyMatter'), __('Import users, posts, and comments from a Greymatter blog.'), array ($gm_import, 'dispatch'));
?>

File diff suppressed because it is too large Load Diff

View File

@ -1,508 +0,0 @@
<?php
/**
* Movable Type and TypePad Importer
*
* @package WordPress
* @subpackage Importer
*/
/**
* Moveable Type and TypePad Importer class
*
* Upload your exported Movable Type or TypePad entries into WordPress.
*
* @since unknown
*/
class MT_Import {
var $posts = array ();
var $file;
var $id;
var $mtnames = array ();
var $newauthornames = array ();
var $j = -1;
function header() {
echo '<div class="wrap">';
screen_icon();
echo '<h2>'.__('Import Movable Type or TypePad').'</h2>';
}
function footer() {
echo '</div>';
}
function greet() {
$this->header();
?>
<div class="narrow">
<p><?php _e( 'Howdy! We are about to begin importing all of your Movable Type or TypePad entries into WordPress. To begin, either choose a file to upload and click &#8220;Upload file and import&#8221;, or use FTP to upload your MT export file as <code>mt-export.txt</code> in your <code>/wp-content/</code> directory and then click &#8220;Import mt-export.txt&#8221;.' ); ?></p>
<?php wp_import_upload_form( add_query_arg('step', 1) ); ?>
<form method="post" action="<?php echo esc_attr(add_query_arg('step', 1)); ?>" class="import-upload-form">
<?php wp_nonce_field('import-upload'); ?>
<p>
<input type="hidden" name="upload_type" value="ftp" />
<?php _e('Or use <code>mt-export.txt</code> in your <code>/wp-content/</code> directory'); ?></p>
<p class="submit">
<input type="submit" class="button" value="<?php esc_attr_e('Import mt-export.txt'); ?>" />
</p>
</form>
<p><?php _e('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&#8217;t finish. If you get an <strong>out of memory</strong> error try splitting up the import file into pieces.'); ?> </p>
</div>
<?php
$this->footer();
}
function users_form($n) {
$users = get_users_of_blog();
?><select name="userselect[<?php echo $n; ?>]">
<option value="#NONE#"><?php _e('&mdash; Select &mdash;') ?></option>
<?php
foreach ( $users as $user )
echo '<option value="' . $user->user_login . '">' . $user->user_login . '</option>';
?>
</select>
<?php
}
function has_gzip() {
return is_callable('gzopen');
}
function fopen($filename, $mode='r') {
if ( $this->has_gzip() )
return gzopen($filename, $mode);
return fopen($filename, $mode);
}
function feof($fp) {
if ( $this->has_gzip() )
return gzeof($fp);
return feof($fp);
}
function fgets($fp, $len=8192) {
if ( $this->has_gzip() )
return gzgets($fp, $len);
return fgets($fp, $len);
}
function fclose($fp) {
if ( $this->has_gzip() )
return gzclose($fp);
return fclose($fp);
}
//function to check the authorname and do the mapping
function checkauthor($author) {
//mtnames is an array with the names in the mt import file
$pass = wp_generate_password();
if (!(in_array($author, $this->mtnames))) { //a new mt author name is found
++ $this->j;
$this->mtnames[$this->j] = $author; //add that new mt author name to an array
$user_id = username_exists($this->newauthornames[$this->j]); //check if the new author name defined by the user is a pre-existing wp user
if (!$user_id) { //banging my head against the desk now.
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.
} else {
$user_id = wp_create_user($this->newauthornames[$this->j], $pass);
}
} else {
return $user_id; // return pre-existing wp username if it exists
}
} else {
$key = array_search($author, $this->mtnames); //find the array key for $author in the $mtnames array
$user_id = username_exists($this->newauthornames[$key]); //use that key to get the value of the author's name from $newauthornames
}
return $user_id;
}
function get_mt_authors() {
$temp = array();
$authors = array();
$handle = $this->fopen($this->file, 'r');
if ( $handle == null )
return false;
$in_comment = false;
while ( $line = $this->fgets($handle) ) {
$line = trim($line);
if ( 'COMMENT:' == $line )
$in_comment = true;
else if ( '-----' == $line )
$in_comment = false;
if ( $in_comment || 0 !== strpos($line,"AUTHOR:") )
continue;
$temp[] = trim( substr($line, strlen("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.
$authors[0] = array_shift($temp);
$y = count($temp) + 1;
for ($x = 1; $x < $y; $x ++) {
$next = array_shift($temp);
if (!(in_array($next, $authors)))
array_push($authors, $next);
}
$this->fclose($handle);
return $authors;
}
function get_authors_from_post() {
$formnames = array ();
$selectnames = array ();
foreach ($_POST['user'] as $key => $line) {
$newname = trim(stripslashes($line));
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.
array_push($formnames, $newname);
} // $formnames is the array with the form entered names
foreach ($_POST['userselect'] as $user => $key) {
$selected = trim(stripslashes($key));
array_push($selectnames, $selected);
}
$count = count($formnames);
for ($i = 0; $i < $count; $i ++) {
if ($selectnames[$i] != '#NONE#') { //if no name was selected from the select menu, use the name entered in the form
array_push($this->newauthornames, "$selectnames[$i]");
} else {
array_push($this->newauthornames, "$formnames[$i]");
}
}
}
function mt_authors_form() {
?>
<div class="wrap">
<?php screen_icon(); ?>
<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 admin&#8217;s entries.'); ?></p>
<p><?php _e('Below, you can see the names of the authors of the MovableType posts in <em>italics</em>. For each of these names, you can either pick an author in your WordPress installation from the menu, or enter a name for the author in the textbox.'); ?></p>
<p><?php _e('If a new user is created by WordPress, a password will be randomly generated. Manually change the user&#8217;s details if necessary.'); ?></p>
<?php
$authors = $this->get_mt_authors();
echo '<ol id="authors">';
echo '<form action="?import=mt&amp;step=2&amp;id=' . $this->id . '" method="post">';
wp_nonce_field('import-mt');
$j = -1;
foreach ($authors as $author) {
++ $j;
echo '<li><label>'.__('Current author:').' <strong>'.$author.'</strong><br />'.sprintf(__('Create user %1$s or map to existing'), ' <input type="text" value="'. esc_attr($author) .'" name="'.'user[]'.'" maxlength="30"> <br />');
$this->users_form($j);
echo '</label></li>';
}
echo '<p class="submit"><input type="submit" class="button" value="'.esc_attr__('Submit').'"></p>'.'<br />';
echo '</form>';
echo '</ol></div>';
}
function select_authors() {
if ( $_POST['upload_type'] === 'ftp' ) {
$file['file'] = WP_CONTENT_DIR . '/mt-export.txt';
if ( !file_exists($file['file']) )
$file['error'] = __('<code>mt-export.txt</code> does not exist');
} else {
$file = wp_import_handle_upload();
}
if ( isset($file['error']) ) {
$this->header();
echo '<p>'.__('Sorry, there has been an error').'.</p>';
echo '<p><strong>' . $file['error'] . '</strong></p>';
$this->footer();
return;
}
$this->file = $file['file'];
$this->id = (int) $file['id'];
$this->mt_authors_form();
}
function save_post(&$post, &$comments, &$pings) {
$post = get_object_vars($post);
$post = add_magic_quotes($post);
$post = (object) $post;
if ( $post_id = post_exists($post->post_title, '', $post->post_date) ) {
echo '<li>';
printf(__('Post <em>%s</em> already exists.'), stripslashes($post->post_title));
} else {
echo '<li>';
printf(__('Importing post <em>%s</em>...'), stripslashes($post->post_title));
if ( '' != trim( $post->extended ) )
$post->post_content .= "\n<!--more-->\n$post->extended";
$post->post_author = $this->checkauthor($post->post_author); //just so that if a post already exists, new users are not created by checkauthor
$post_id = wp_insert_post($post);
if ( is_wp_error( $post_id ) )
return $post_id;
// Add categories.
if ( 0 != count($post->categories) ) {
wp_create_categories($post->categories, $post_id);
}
// Add tags or keywords
if ( 1 < strlen($post->post_keywords) ) {
// Keywords exist.
printf('<br />'.__('Adding tags <em>%s</em>...'), stripslashes($post->post_keywords));
wp_add_post_tags($post_id, $post->post_keywords);
}
}
$num_comments = 0;
foreach ( $comments as $comment ) {
$comment = get_object_vars($comment);
$comment = add_magic_quotes($comment);
if ( !comment_exists($comment['comment_author'], $comment['comment_date'])) {
$comment['comment_post_ID'] = $post_id;
$comment = wp_filter_comment($comment);
wp_insert_comment($comment);
$num_comments++;
}
}
if ( $num_comments )
printf(' '._n('(%s comment)', '(%s comments)', $num_comments), $num_comments);
$num_pings = 0;
foreach ( $pings as $ping ) {
$ping = get_object_vars($ping);
$ping = add_magic_quotes($ping);
if ( !comment_exists($ping['comment_author'], $ping['comment_date'])) {
$ping['comment_content'] = "<strong>{$ping['title']}</strong>\n\n{$ping['comment_content']}";
$ping['comment_post_ID'] = $post_id;
$ping = wp_filter_comment($ping);
wp_insert_comment($ping);
$num_pings++;
}
}
if ( $num_pings )
printf(' '._n('(%s ping)', '(%s pings)', $num_pings), $num_pings);
echo "</li>";
//ob_flush();flush();
}
function process_posts() {
global $wpdb;
$handle = $this->fopen($this->file, 'r');
if ( $handle == null )
return false;
$context = '';
$post = new StdClass();
$comment = new StdClass();
$comments = array();
$ping = new StdClass();
$pings = array();
echo "<div class='wrap'><ol>";
while ( $line = $this->fgets($handle) ) {
$line = trim($line);
if ( '-----' == $line ) {
// Finishing a multi-line field
if ( 'comment' == $context ) {
$comments[] = $comment;
$comment = new StdClass();
} else if ( 'ping' == $context ) {
$pings[] = $ping;
$ping = new StdClass();
}
$context = '';
} else if ( '--------' == $line ) {
// Finishing a post.
$context = '';
$result = $this->save_post($post, $comments, $pings);
if ( is_wp_error( $result ) )
return $result;
$post = new StdClass;
$comment = new StdClass();
$ping = new StdClass();
$comments = array();
$pings = array();
} else if ( 'BODY:' == $line ) {
$context = 'body';
} else if ( 'EXTENDED BODY:' == $line ) {
$context = 'extended';
} else if ( 'EXCERPT:' == $line ) {
$context = 'excerpt';
} else if ( 'KEYWORDS:' == $line ) {
$context = 'keywords';
} else if ( 'COMMENT:' == $line ) {
$context = 'comment';
} else if ( 'PING:' == $line ) {
$context = 'ping';
} else if ( 0 === strpos($line, "AUTHOR:") ) {
$author = trim( substr($line, strlen("AUTHOR:")) );
if ( '' == $context )
$post->post_author = $author;
else if ( 'comment' == $context )
$comment->comment_author = $author;
} else if ( 0 === strpos($line, "TITLE:") ) {
$title = trim( substr($line, strlen("TITLE:")) );
if ( '' == $context )
$post->post_title = $title;
else if ( 'ping' == $context )
$ping->title = $title;
} else if ( 0 === strpos($line, "STATUS:") ) {
$status = trim( strtolower( substr($line, strlen("STATUS:")) ) );
if ( empty($status) )
$status = 'publish';
$post->post_status = $status;
} else if ( 0 === strpos($line, "ALLOW COMMENTS:") ) {
$allow = trim( substr($line, strlen("ALLOW COMMENTS:")) );
if ( $allow == 1 )
$post->comment_status = 'open';
else
$post->comment_status = 'closed';
} else if ( 0 === strpos($line, "ALLOW PINGS:") ) {
$allow = trim( substr($line, strlen("ALLOW PINGS:")) );
if ( $allow == 1 )
$post->ping_status = 'open';
else
$post->ping_status = 'closed';
} else if ( 0 === strpos($line, "CATEGORY:") ) {
$category = trim( substr($line, strlen("CATEGORY:")) );
if ( '' != $category )
$post->categories[] = $category;
} else if ( 0 === strpos($line, "PRIMARY CATEGORY:") ) {
$category = trim( substr($line, strlen("PRIMARY CATEGORY:")) );
if ( '' != $category )
$post->categories[] = $category;
} else if ( 0 === strpos($line, "DATE:") ) {
$date = trim( substr($line, strlen("DATE:")) );
$date = strtotime($date);
$date = date('Y-m-d H:i:s', $date);
$date_gmt = get_gmt_from_date($date);
if ( '' == $context ) {
$post->post_modified = $date;
$post->post_modified_gmt = $date_gmt;
$post->post_date = $date;
$post->post_date_gmt = $date_gmt;
} else if ( 'comment' == $context ) {
$comment->comment_date = $date;
} else if ( 'ping' == $context ) {
$ping->comment_date = $date;
}
} else if ( 0 === strpos($line, "EMAIL:") ) {
$email = trim( substr($line, strlen("EMAIL:")) );
if ( 'comment' == $context )
$comment->comment_author_email = $email;
else
$ping->comment_author_email = '';
} else if ( 0 === strpos($line, "IP:") ) {
$ip = trim( substr($line, strlen("IP:")) );
if ( 'comment' == $context )
$comment->comment_author_IP = $ip;
else
$ping->comment_author_IP = $ip;
} else if ( 0 === strpos($line, "URL:") ) {
$url = trim( substr($line, strlen("URL:")) );
if ( 'comment' == $context )
$comment->comment_author_url = $url;
else
$ping->comment_author_url = $url;
} else if ( 0 === strpos($line, "BLOG NAME:") ) {
$blog = trim( substr($line, strlen("BLOG NAME:")) );
$ping->comment_author = $blog;
} else {
// Processing multi-line field, check context.
if( !empty($line) )
$line .= "\n";
if ( 'body' == $context ) {
$post->post_content .= $line;
} else if ( 'extended' == $context ) {
$post->extended .= $line;
} else if ( 'excerpt' == $context ) {
$post->post_excerpt .= $line;
} else if ( 'keywords' == $context ) {
$post->post_keywords .= $line;
} else if ( 'comment' == $context ) {
$comment->comment_content .= $line;
} else if ( 'ping' == $context ) {
$ping->comment_content .= $line;
}
}
}
$this->fclose($handle);
echo '</ol>';
wp_import_cleanup($this->id);
do_action('import_done', 'mt');
echo '<h3>'.sprintf(__('All done. <a href="%s">Have fun!</a>'), get_option('home')).'</h3></div>';
}
function import() {
$this->id = (int) $_GET['id'];
if ( $this->id == 0 )
$this->file = WP_CONTENT_DIR . '/mt-export.txt';
else
$this->file = get_attached_file($this->id);
$this->get_authors_from_post();
$result = $this->process_posts();
if ( is_wp_error( $result ) )
return $result;
}
function dispatch() {
if (empty ($_GET['step']))
$step = 0;
else
$step = (int) $_GET['step'];
switch ($step) {
case 0 :
$this->greet();
break;
case 1 :
check_admin_referer('import-upload');
$this->select_authors();
break;
case 2:
check_admin_referer('import-mt');
set_time_limit(0);
$result = $this->import();
if ( is_wp_error( $result ) )
echo $result->get_error_message();
break;
}
}
function MT_Import() {
// Nothing.
}
}
$mt_import = new MT_Import();
register_importer('mt', __('Movable Type and TypePad'), __('Import posts and comments from a Movable Type or TypePad blog.'), array ($mt_import, 'dispatch'));
?>

View File

@ -1,152 +0,0 @@
<?php
/**
* Links Import Administration Panel.
*
* @package WordPress
* @subpackage Administration
*/
/** Load WordPress Administration Bootstrap */
$parent_file = 'tools.php';
$submenu_file = 'import.php';
$title = __('Import Blogroll');
class OPML_Import {
function dispatch() {
global $wpdb, $user_ID;
$step = isset( $_POST['step'] ) ? $_POST['step'] : 0;
switch ($step) {
case 0: {
include_once( ABSPATH . 'wp-admin/admin-header.php' );
if ( !current_user_can('manage_links') )
wp_die(__('Cheatin&#8217; uh?'));
$opmltype = 'blogrolling'; // default.
?>
<div class="wrap">
<?php screen_icon(); ?>
<h2><?php _e('Import your blogroll from another system') ?> </h2>
<form enctype="multipart/form-data" action="admin.php?import=opml" method="post" name="blogroll">
<?php wp_nonce_field('import-bookmarks') ?>
<p><?php _e('If a program or website you use allows you to export your links or subscriptions as OPML you may import them here.'); ?></p>
<div style="width: 70%; margin: auto; height: 8em;">
<input type="hidden" name="step" value="1" />
<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo wp_max_upload_size(); ?>" />
<div style="width: 48%;" class="alignleft">
<h3><label for="opml_url"><?php _e('Specify an OPML URL:'); ?></label></h3>
<input type="text" name="opml_url" id="opml_url" size="50" class="code" style="width: 90%;" value="http://" />
</div>
<div style="width: 48%;" class="alignleft">
<h3><label for="userfile"><?php _e('Or choose from your local disk:'); ?></label></h3>
<input id="userfile" name="userfile" type="file" size="30" />
</div>
</div>
<p style="clear: both; margin-top: 1em;"><label for="cat_id"><?php _e('Now select a category you want to put these links in.') ?></label><br />
<?php _e('Category:') ?> <select name="cat_id" id="cat_id">
<?php
$categories = get_terms('link_category', array('get' => 'all'));
foreach ($categories as $category) {
?>
<option value="<?php echo $category->term_id; ?>"><?php echo esc_html(apply_filters('link_category', $category->name)); ?></option>
<?php
} // end foreach
?>
</select></p>
<p class="submit"><input type="submit" name="submit" value="<?php esc_attr_e('Import OPML File') ?>" /></p>
</form>
</div>
<?php
break;
} // end case 0
case 1: {
check_admin_referer('import-bookmarks');
include_once( ABSPATH . 'wp-admin/admin-header.php' );
if ( !current_user_can('manage_links') )
wp_die(__('Cheatin&#8217; uh?'));
?>
<div class="wrap">
<h2><?php _e('Importing...') ?></h2>
<?php
$cat_id = abs( (int) $_POST['cat_id'] );
if ( $cat_id < 1 )
$cat_id = 1;
$opml_url = $_POST['opml_url'];
if ( isset($opml_url) && $opml_url != '' && $opml_url != 'http://' ) {
$blogrolling = true;
} else { // try to get the upload file.
$overrides = array('test_form' => false, 'test_type' => false);
$_FILES['userfile']['name'] .= '.txt';
$file = wp_handle_upload($_FILES['userfile'], $overrides);
if ( isset($file['error']) )
wp_die($file['error']);
$url = $file['url'];
$opml_url = $file['file'];
$blogrolling = false;
}
global $opml, $updated_timestamp, $all_links, $map, $names, $urls, $targets, $descriptions, $feeds;
if ( isset($opml_url) && $opml_url != '' ) {
if ( $blogrolling === true ) {
$opml = wp_remote_fopen($opml_url);
} else {
$opml = file_get_contents($opml_url);
}
/** Load OPML Parser */
include_once( ABSPATH . 'wp-admin/link-parse-opml.php' );
$link_count = count($names);
for ( $i = 0; $i < $link_count; $i++ ) {
if ('Last' == substr($titles[$i], 0, 4))
$titles[$i] = '';
if ( 'http' == substr($titles[$i], 0, 4) )
$titles[$i] = '';
$link = array( 'link_url' => $urls[$i], 'link_name' => $wpdb->escape($names[$i]), 'link_category' => array($cat_id), 'link_description' => $wpdb->escape($descriptions[$i]), 'link_owner' => $user_ID, 'link_rss' => $feeds[$i]);
wp_insert_link($link);
echo sprintf('<p>'.__('Inserted <strong>%s</strong>').'</p>', $names[$i]);
}
?>
<p><?php printf(__('Inserted %1$d links into category %2$s. All done! Go <a href="%3$s">manage those links</a>.'), $link_count, $cat_id, 'link-manager.php') ?></p>
<?php
} // end if got url
else
{
echo "<p>" . __("You need to supply your OPML url. Press back on your browser and try again") . "</p>\n";
} // end else
if ( ! $blogrolling )
do_action( 'wp_delete_file', $opml_url);
@unlink($opml_url);
?>
</div>
<?php
break;
} // end case 1
} // end switch
}
function OPML_Import() {}
}
$opml_importer = new OPML_Import();
register_importer('opml', __('Blogroll'), __('Import links in OPML format.'), array(&$opml_importer, 'dispatch'));
?>

View File

@ -1,196 +0,0 @@
<?php
/**
* RSS Importer
*
* @package WordPress
* @subpackage Importer
*/
/**
* RSS Importer
*
* Will process a RSS feed for importing posts into WordPress. This is a very
* limited importer and should only be used as the last resort, when no other
* importer is available.
*
* @since unknown
*/
class RSS_Import {
var $posts = array ();
var $file;
function header() {
echo '<div class="wrap">';
screen_icon();
echo '<h2>'.__('Import RSS').'</h2>';
}
function footer() {
echo '</div>';
}
function greet() {
echo '<div class="narrow">';
echo '<p>'.__('Howdy! This importer allows you to extract posts from an RSS 2.0 file into your WordPress site. 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>';
wp_import_upload_form("admin.php?import=rss&amp;step=1");
echo '</div>';
}
function _normalize_tag( $matches ) {
return '<' . strtolower( $matches[1] );
}
function get_posts() {
global $wpdb;
set_magic_quotes_runtime(0);
$datalines = file($this->file); // 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, $this->posts);
$this->posts = $this->posts[1];
$index = 0;
foreach ($this->posts as $post) {
preg_match('|<title>(.*?)</title>|is', $post, $post_title);
$post_title = str_replace(array('<![CDATA[', ']]>'), '', $wpdb->escape( trim($post_title[1]) ));
preg_match('|<pubdate>(.*?)</pubdate>|is', $post, $post_date_gmt);
if ($post_date_gmt) {
$post_date_gmt = strtotime($post_date_gmt[1]);
} else {
// if we don't already have something from pubDate
preg_match('|<dc:date>(.*?)</dc:date>|is', $post, $post_date_gmt);
$post_date_gmt = preg_replace('|([-+])([0-9]+):([0-9]+)$|', '\1\2\3', $post_date_gmt[1]);
$post_date_gmt = str_replace('T', ' ', $post_date_gmt);
$post_date_gmt = strtotime($post_date_gmt);
}
$post_date_gmt = gmdate('Y-m-d H:i:s', $post_date_gmt);
$post_date = get_date_from_gmt( $post_date_gmt );
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];
}
$cat_index = 0;
foreach ($categories as $category) {
$categories[$cat_index] = $wpdb->escape( html_entity_decode( $category ) );
$cat_index++;
}
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, $post_content);
$post_content = str_replace(array ('<![CDATA[', ']]>'), '', $wpdb->escape(trim($post_content[1])));
if (!$post_content) {
// This is for feeds that put content in description
preg_match('|<description>(.*?)</description>|is', $post, $post_content);
$post_content = $wpdb->escape( html_entity_decode( trim( $post_content[1] ) ) );
}
// Clean up content
$post_content = preg_replace_callback('|<(/?[A-Z]+)|', array( &$this, '_normalize_tag' ), $post_content);
$post_content = str_replace('<br>', '<br />', $post_content);
$post_content = str_replace('<hr>', '<hr />', $post_content);
$post_author = 1;
$post_status = 'publish';
$this->posts[$index] = compact('post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_status', 'guid', 'categories');
$index++;
}
}
function import_posts() {
echo '<ol>';
foreach ($this->posts as $post) {
echo "<li>".__('Importing post...');
extract($post);
if ($post_id = post_exists($post_title, $post_content, $post_date)) {
_e('Post already imported');
} else {
$post_id = wp_insert_post($post);
if ( is_wp_error( $post_id ) )
return $post_id;
if (!$post_id) {
_e('Couldn&#8217;t get post ID');
return;
}
if (0 != count($categories))
wp_create_categories($categories, $post_id);
_e('Done!');
}
echo '</li>';
}
echo '</ol>';
}
function import() {
$file = wp_import_handle_upload();
if ( isset($file['error']) ) {
echo $file['error'];
return;
}
$this->file = $file['file'];
$this->get_posts();
$result = $this->import_posts();
if ( is_wp_error( $result ) )
return $result;
wp_import_cleanup($file['id']);
do_action('import_done', 'rss');
echo '<h3>';
printf(__('All done. <a href="%s">Have fun!</a>'), get_option('home'));
echo '</h3>';
}
function dispatch() {
if (empty ($_GET['step']))
$step = 0;
else
$step = (int) $_GET['step'];
$this->header();
switch ($step) {
case 0 :
$this->greet();
break;
case 1 :
check_admin_referer('import-upload');
$result = $this->import();
if ( is_wp_error( $result ) )
echo $result->get_error_message();
break;
}
$this->footer();
}
function RSS_Import() {
// Nothing.
}
}
$rss_import = new RSS_Import();
register_importer('rss', __('RSS'), __('Import posts from an RSS feed.'), array ($rss_import, 'dispatch'));
?>

View File

@ -1,170 +0,0 @@
<?php
/**
* Simple Tags Plugin Importer
*
* @package WordPress
* @subpackage Importer
*/
/**
* Simple Tags Plugin Tags converter class.
*
* Will convert Simple Tags Plugin tags over to the WordPress 2.3 taxonomy.
*
* @since unknown
*/
class STP_Import {
function header() {
echo '<div class="wrap">';
screen_icon();
echo '<h2>'.__('Import Simple Tagging').'</h2>';
echo '<p>'.__('Steps may take a few minutes depending on the size of your database. Please be patient.').'<br /><br /></p>';
}
function footer() {
echo '</div>';
}
function greet() {
echo '<div class="narrow">';
echo '<p>'.__('Howdy! This imports tags from Simple Tagging 1.6.2 into WordPress tags.').'</p>';
echo '<p>'.__('This has not been tested on any other versions of Simple Tagging. Mileage may vary.').'</p>';
echo '<p>'.__('To accommodate larger databases for those tag-crazy authors out there, we have made this into an easy 4-step program to help you kick that nasty Simple Tagging habit. Just keep clicking along and we will let you know when you are in the clear!').'</p>';
echo '<p><strong>'.__('Don&#8217;t be stupid - backup your database before proceeding!').'</strong></p>';
echo '<form action="admin.php?import=stp&amp;step=1" method="post">';
wp_nonce_field('import-stp');
echo '<p class="submit"><input type="submit" name="submit" class="button" value="'.esc_attr__('Step 1').'" /></p>';
echo '</form>';
echo '</div>';
}
function dispatch () {
if ( empty( $_GET['step'] ) ) {
$step = 0;
} else {
$step = (int) $_GET['step'];
}
// load the header
$this->header();
switch ( $step ) {
case 0 :
$this->greet();
break;
case 1 :
check_admin_referer('import-stp');
$this->import_posts();
break;
case 2:
check_admin_referer('import-stp');
$this->import_t2p();
break;
case 3:
check_admin_referer('import-stp');
$this->cleanup_import();
break;
}
// load the footer
$this->footer();
}
function import_posts ( ) {
echo '<div class="narrow">';
echo '<p><h3>'.__('Reading STP Post Tags&#8230;').'</h3></p>';
// read in all the STP tag -> post settings
$posts = $this->get_stp_posts();
// if we didn't get any tags back, that's all there is folks!
if ( !is_array($posts) ) {
echo '<p>' . __('No posts were found to have tags!') . '</p>';
return false;
}
else {
// if there's an existing entry, delete it
if ( get_option('stpimp_posts') ) {
delete_option('stpimp_posts');
}
add_option('stpimp_posts', $posts);
$count = count($posts);
echo '<p>' . sprintf( _n('Done! <strong>%s</strong> tag to post relationships were read.', 'Done! <strong>%s</strong> tags to post relationships were read.', $count), $count ) . '<br /></p>';
}
echo '<form action="admin.php?import=stp&amp;step=2" method="post">';
wp_nonce_field('import-stp');
echo '<p class="submit"><input type="submit" name="submit" class="button" value="'.esc_attr__('Step 2').'" /></p>';
echo '</form>';
echo '</div>';
}
function import_t2p ( ) {
echo '<div class="narrow">';
echo '<p><h3>'.__('Adding Tags to Posts&#8230;').'</h3></p>';
// run that funky magic!
$tags_added = $this->tag2post();
echo '<p>' . sprintf( _n('Done! <strong>%s</strong> tag was added!', 'Done! <strong>%s</strong> tags were added!', $tags_added), $tags_added ) . '<br /></p>';
echo '<form action="admin.php?import=stp&amp;step=3" method="post">';
wp_nonce_field('import-stp');
echo '<p class="submit"><input type="submit" name="submit" class="button" value="'.esc_attr__('Step 3').'" /></p>';
echo '</form>';
echo '</div>';
}
function get_stp_posts ( ) {
global $wpdb;
// read in all the posts from the STP post->tag table: should be wp_post2tag
$posts_query = "SELECT post_id, tag_name FROM " . $wpdb->prefix . "stp_tags";
$posts = $wpdb->get_results($posts_query);
return $posts;
}
function tag2post ( ) {
global $wpdb;
// get the tags and posts we imported in the last 2 steps
$posts = get_option('stpimp_posts');
// null out our results
$tags_added = 0;
// loop through each post and add its tags to the db
foreach ( $posts as $this_post ) {
$the_post = (int) $this_post->post_id;
$the_tag = $wpdb->escape($this_post->tag_name);
// try to add the tag
wp_add_post_tags($the_post, $the_tag);
$tags_added++;
}
// that's it, all posts should be linked to their tags properly, pending any errors we just spit out!
return $tags_added;
}
function cleanup_import ( ) {
delete_option('stpimp_posts');
$this->done();
}
function done ( ) {
echo '<div class="narrow">';
echo '<p><h3>'.__('Import Complete!').'</h3></p>';
echo '<p>' . __('OK, so we lied about this being a 4-step program! You&#8217;re done!') . '</p>';
echo '<p>' . __('Now wasn&#8217;t that easy?') . '</p>';
echo '</div>';
}
function STP_Import ( ) {
// Nothing.
}
}
// create the import object
$stp_import = new STP_Import();
// add it to the import page!
register_importer('stp', 'Simple Tagging', __('Import Simple Tagging tags into WordPress tags.'), array($stp_import, 'dispatch'));
?>

View File

@ -1,691 +0,0 @@
<?php
/**
* TextPattern Importer
*
* @package WordPress
* @subpackage Importer
*/
if(!function_exists('get_comment_count'))
{
/**
* Get the comment count for posts.
*
* @package WordPress
* @subpackage Textpattern_Import
*
* @param int $post_ID Post ID
* @return int
*/
function get_comment_count($post_ID)
{
global $wpdb;
return $wpdb->get_var( $wpdb->prepare("SELECT count(*) FROM $wpdb->comments WHERE comment_post_ID = %d", $post_ID) );
}
}
if(!function_exists('link_exists'))
{
/**
* Check whether link already exists.
*
* @package WordPress
* @subpackage Textpattern_Import
*
* @param string $linkname
* @return int
*/
function link_exists($linkname)
{
global $wpdb;
return $wpdb->get_var( $wpdb->prepare("SELECT link_id FROM $wpdb->links WHERE link_name = %s", $linkname) );
}
}
/**
* TextPattern Importer Class
*
* @since unknown
*/
class Textpattern_Import {
function header()
{
echo '<div class="wrap">';
screen_icon();
echo '<h2>'.__('Import Textpattern').'</h2>';
echo '<p>'.__('Steps may take a few minutes depending on the size of your database. Please be patient.').'</p>';
}
function footer()
{
echo '</div>';
}
function greet() {
echo '<div class="narrow">';
echo '<p>'.__('Howdy! This imports categories, users, posts, comments, and links from any Textpattern 4.0.2+ into this site.').'</p>';
echo '<p>'.__('This has not been tested on previous versions of Textpattern. Mileage may vary.').'</p>';
echo '<p>'.__('Your Textpattern Configuration settings are as follows:').'</p>';
echo '<form action="admin.php?import=textpattern&amp;step=1" method="post">';
wp_nonce_field('import-textpattern');
$this->db_form();
echo '<p class="submit"><input type="submit" name="submit" class="button" value="'.esc_attr__('Import').'" /></p>';
echo '</form>';
echo '</div>';
}
function get_txp_cats()
{
global $wpdb;
// General Housekeeping
$txpdb = new wpdb(get_option('txpuser'), get_option('txppass'), get_option('txpname'), get_option('txphost'));
set_magic_quotes_runtime(0);
$prefix = get_option('tpre');
// Get Categories
return $txpdb->get_results('SELECT
id,
name,
title
FROM '.$prefix.'txp_category
WHERE type = "article"',
ARRAY_A);
}
function get_txp_users()
{
global $wpdb;
// General Housekeeping
$txpdb = new wpdb(get_option('txpuser'), get_option('txppass'), get_option('txpname'), get_option('txphost'));
set_magic_quotes_runtime(0);
$prefix = get_option('tpre');
// Get Users
return $txpdb->get_results('SELECT
user_id,
name,
RealName,
email,
privs
FROM '.$prefix.'txp_users', ARRAY_A);
}
function get_txp_posts()
{
// General Housekeeping
$txpdb = new wpdb(get_option('txpuser'), get_option('txppass'), get_option('txpname'), get_option('txphost'));
set_magic_quotes_runtime(0);
$prefix = get_option('tpre');
// Get Posts
return $txpdb->get_results('SELECT
ID,
Posted,
AuthorID,
LastMod,
Title,
Body,
Excerpt,
Category1,
Category2,
Status,
Keywords,
url_title,
comments_count
FROM '.$prefix.'textpattern
', ARRAY_A);
}
function get_txp_comments()
{
global $wpdb;
// General Housekeeping
$txpdb = new wpdb(get_option('txpuser'), get_option('txppass'), get_option('txpname'), get_option('txphost'));
set_magic_quotes_runtime(0);
$prefix = get_option('tpre');
// Get Comments
return $txpdb->get_results('SELECT * FROM '.$prefix.'txp_discuss', ARRAY_A);
}
function get_txp_links()
{
//General Housekeeping
$txpdb = new wpdb(get_option('txpuser'), get_option('txppass'), get_option('txpname'), get_option('txphost'));
set_magic_quotes_runtime(0);
$prefix = get_option('tpre');
return $txpdb->get_results('SELECT
id,
date,
category,
url,
linkname,
description
FROM '.$prefix.'txp_link',
ARRAY_A);
}
function cat2wp($categories='')
{
// General Housekeeping
global $wpdb;
$count = 0;
$txpcat2wpcat = array();
// Do the Magic
if(is_array($categories))
{
echo '<p>'.__('Importing Categories...').'<br /><br /></p>';
foreach ($categories as $category)
{
$count++;
extract($category);
// Make Nice Variables
$name = $wpdb->escape($name);
$title = $wpdb->escape($title);
if($cinfo = category_exists($name))
{
$ret_id = wp_insert_category(array('cat_ID' => $cinfo, 'category_nicename' => $name, 'cat_name' => $title));
}
else
{
$ret_id = wp_insert_category(array('category_nicename' => $name, 'cat_name' => $title));
}
$txpcat2wpcat[$id] = $ret_id;
}
// Store category translation for future use
add_option('txpcat2wpcat',$txpcat2wpcat);
echo '<p>'.sprintf(_n('Done! <strong>%1$s</strong> category imported.', 'Done! <strong>%1$s</strong> categories imported.', $count), $count).'<br /><br /></p>';
return true;
}
echo __('No Categories to Import!');
return false;
}
function users2wp($users='')
{
// General Housekeeping
global $wpdb;
$count = 0;
$txpid2wpid = array();
// Midnight Mojo
if(is_array($users))
{
echo '<p>'.__('Importing Users...').'<br /><br /></p>';
foreach($users as $user)
{
$count++;
extract($user);
// Make Nice Variables
$name = $wpdb->escape($name);
$RealName = $wpdb->escape($RealName);
if($uinfo = get_userdatabylogin($name))
{
$ret_id = wp_insert_user(array(
'ID' => $uinfo->ID,
'user_login' => $name,
'user_nicename' => $RealName,
'user_email' => $email,
'user_url' => 'http://',
'display_name' => $name)
);
}
else
{
$ret_id = wp_insert_user(array(
'user_login' => $name,
'user_nicename' => $RealName,
'user_email' => $email,
'user_url' => 'http://',
'display_name' => $name)
);
}
$txpid2wpid[$user_id] = $ret_id;
// Set Textpattern-to-WordPress permissions translation
$transperms = array(1 => '10', 2 => '9', 3 => '5', 4 => '4', 5 => '3', 6 => '2', 7 => '0');
// Update Usermeta Data
$user = new WP_User($ret_id);
if('10' == $transperms[$privs]) { $user->set_role('administrator'); }
if('9' == $transperms[$privs]) { $user->set_role('editor'); }
if('5' == $transperms[$privs]) { $user->set_role('editor'); }
if('4' == $transperms[$privs]) { $user->set_role('author'); }
if('3' == $transperms[$privs]) { $user->set_role('contributor'); }
if('2' == $transperms[$privs]) { $user->set_role('contributor'); }
if('0' == $transperms[$privs]) { $user->set_role('subscriber'); }
update_user_meta( $ret_id, 'wp_user_level', $transperms[$privs] );
update_user_meta( $ret_id, 'rich_editing', 'false');
}// End foreach($users as $user)
// Store id translation array for future use
add_option('txpid2wpid',$txpid2wpid);
echo '<p>'.sprintf(__('Done! <strong>%1$s</strong> users imported.'), $count).'<br /><br /></p>';
return true;
}// End if(is_array($users)
echo __('No Users to Import!');
return false;
}// End function user2wp()
function posts2wp($posts='')
{
// General Housekeeping
global $wpdb;
$count = 0;
$txpposts2wpposts = array();
$cats = array();
// Do the Magic
if(is_array($posts))
{
echo '<p>'.__('Importing Posts...').'<br /><br /></p>';
foreach($posts as $post)
{
$count++;
extract($post);
// Set Textpattern-to-WordPress status translation
$stattrans = array(1 => 'draft', 2 => 'private', 3 => 'draft', 4 => 'publish', 5 => 'publish');
//Can we do this more efficiently?
$uinfo = ( get_userdatabylogin( $AuthorID ) ) ? get_userdatabylogin( $AuthorID ) : 1;
$authorid = ( is_object( $uinfo ) ) ? $uinfo->ID : $uinfo ;
$Title = $wpdb->escape($Title);
$Body = $wpdb->escape($Body);
$Excerpt = $wpdb->escape($Excerpt);
$post_status = $stattrans[$Status];
// Import Post data into WordPress
if($pinfo = post_exists($Title,$Body))
{
$ret_id = wp_insert_post(array(
'ID' => $pinfo,
'post_date' => $Posted,
'post_date_gmt' => $post_date_gmt,
'post_author' => $authorid,
'post_modified' => $LastMod,
'post_modified_gmt' => $post_modified_gmt,
'post_title' => $Title,
'post_content' => $Body,
'post_excerpt' => $Excerpt,
'post_status' => $post_status,
'post_name' => $url_title,
'comment_count' => $comments_count)
);
if ( is_wp_error( $ret_id ) )
return $ret_id;
}
else
{
$ret_id = wp_insert_post(array(
'post_date' => $Posted,
'post_date_gmt' => $post_date_gmt,
'post_author' => $authorid,
'post_modified' => $LastMod,
'post_modified_gmt' => $post_modified_gmt,
'post_title' => $Title,
'post_content' => $Body,
'post_excerpt' => $Excerpt,
'post_status' => $post_status,
'post_name' => $url_title,
'comment_count' => $comments_count)
);
if ( is_wp_error( $ret_id ) )
return $ret_id;
}
$txpposts2wpposts[$ID] = $ret_id;
// Make Post-to-Category associations
$cats = array();
$category1 = get_category_by_slug($Category1);
$category1 = $category1->term_id;
$category2 = get_category_by_slug($Category2);
$category2 = $category2->term_id;
if($cat1 = $category1) { $cats[1] = $cat1; }
if($cat2 = $category2) { $cats[2] = $cat2; }
if(!empty($cats)) { wp_set_post_categories($ret_id, $cats); }
}
}
// Store ID translation for later use
add_option('txpposts2wpposts',$txpposts2wpposts);
echo '<p>'.sprintf(__('Done! <strong>%1$s</strong> posts imported.'), $count).'<br /><br /></p>';
return true;
}
function comments2wp($comments='')
{
// General Housekeeping
global $wpdb;
$count = 0;
$txpcm2wpcm = array();
$postarr = get_option('txpposts2wpposts');
// Magic Mojo
if(is_array($comments))
{
echo '<p>'.__('Importing Comments...').'<br /><br /></p>';
foreach($comments as $comment)
{
$count++;
extract($comment);
// WordPressify Data
$comment_ID = ltrim($discussid, '0');
$comment_post_ID = $postarr[$parentid];
$comment_approved = (1 == $visible) ? 1 : 0;
$name = $wpdb->escape($name);
$email = $wpdb->escape($email);
$web = $wpdb->escape($web);
$message = $wpdb->escape($message);
$comment = array(
'comment_post_ID' => $comment_post_ID,
'comment_author' => $name,
'comment_author_IP' => $ip,
'comment_author_email' => $email,
'comment_author_url' => $web,
'comment_date' => $posted,
'comment_content' => $message,
'comment_approved' => $comment_approved);
$comment = wp_filter_comment($comment);
if ( $cinfo = comment_exists($name, $posted) ) {
// Update comments
$comment['comment_ID'] = $cinfo;
$ret_id = wp_update_comment($comment);
} else {
// Insert comments
$ret_id = wp_insert_comment($comment);
}
$txpcm2wpcm[$comment_ID] = $ret_id;
}
// Store Comment ID translation for future use
add_option('txpcm2wpcm', $txpcm2wpcm);
// Associate newly formed categories with posts
get_comment_count($ret_id);
echo '<p>'.sprintf(__('Done! <strong>%1$s</strong> comments imported.'), $count).'<br /><br /></p>';
return true;
}
echo __('No Comments to Import!');
return false;
}
function links2wp($links='')
{
// General Housekeeping
global $wpdb;
$count = 0;
// Deal with the links
if(is_array($links))
{
echo '<p>'.__('Importing Links...').'<br /><br /></p>';
foreach($links as $link)
{
$count++;
extract($link);
// Make nice vars
$category = $wpdb->escape($category);
$linkname = $wpdb->escape($linkname);
$description = $wpdb->escape($description);
if($linfo = link_exists($linkname))
{
$ret_id = wp_insert_link(array(
'link_id' => $linfo,
'link_url' => $url,
'link_name' => $linkname,
'link_category' => $category,
'link_description' => $description,
'link_updated' => $date)
);
}
else
{
$ret_id = wp_insert_link(array(
'link_url' => $url,
'link_name' => $linkname,
'link_category' => $category,
'link_description' => $description,
'link_updated' => $date)
);
}
$txplinks2wplinks[$link_id] = $ret_id;
}
add_option('txplinks2wplinks',$txplinks2wplinks);
echo '<p>';
printf(_n('Done! <strong>%s</strong> link imported', 'Done! <strong>%s</strong> links imported', $count), $count);
echo '<br /><br /></p>';
return true;
}
echo __('No Links to Import!');
return false;
}
function import_categories()
{
// Category Import
$cats = $this->get_txp_cats();
$this->cat2wp($cats);
add_option('txp_cats', $cats);
echo '<form action="admin.php?import=textpattern&amp;step=2" method="post">';
wp_nonce_field('import-textpattern');
printf('<p class="submit"><input type="submit" name="submit" class="button" value="%s" /></p>', esc_attr__('Import Users'));
echo '</form>';
}
function import_users()
{
// User Import
$users = $this->get_txp_users();
$this->users2wp($users);
echo '<form action="admin.php?import=textpattern&amp;step=3" method="post">';
wp_nonce_field('import-textpattern');
printf('<p class="submit"><input type="submit" name="submit" class="button" value="%s" /></p>', esc_attr__('Import Posts'));
echo '</form>';
}
function import_posts()
{
// Post Import
$posts = $this->get_txp_posts();
$result = $this->posts2wp($posts);
if ( is_wp_error( $result ) )
return $result;
echo '<form action="admin.php?import=textpattern&amp;step=4" method="post">';
wp_nonce_field('import-textpattern');
printf('<p class="submit"><input type="submit" name="submit" class="button" value="%s" /></p>', esc_attr__('Import Comments'));
echo '</form>';
}
function import_comments()
{
// Comment Import
$comments = $this->get_txp_comments();
$this->comments2wp($comments);
echo '<form action="admin.php?import=textpattern&amp;step=5" method="post">';
wp_nonce_field('import-textpattern');
printf('<p class="submit"><input type="submit" name="submit" class="button" value="%s" /></p>', esc_attr__('Import Links'));
echo '</form>';
}
function import_links()
{
//Link Import
$links = $this->get_txp_links();
$this->links2wp($links);
add_option('txp_links', $links);
echo '<form action="admin.php?import=textpattern&amp;step=6" method="post">';
wp_nonce_field('import-textpattern');
printf('<p class="submit"><input type="submit" name="submit" class="button" value="%s" /></p>', esc_attr__('Finish'));
echo '</form>';
}
function cleanup_txpimport()
{
delete_option('tpre');
delete_option('txp_cats');
delete_option('txpid2wpid');
delete_option('txpcat2wpcat');
delete_option('txpposts2wpposts');
delete_option('txpcm2wpcm');
delete_option('txplinks2wplinks');
delete_option('txpuser');
delete_option('txppass');
delete_option('txpname');
delete_option('txphost');
do_action('import_done', 'textpattern');
$this->tips();
}
function tips()
{
echo '<p>'.__('Welcome to WordPress. We hope (and expect!) that you will find this platform incredibly rewarding! As a new WordPress user coming from Textpattern, there are some things that we would like to point out. Hopefully, they will help your transition go as smoothly as possible.').'</p>';
echo '<h3>'.__('Users').'</h3>';
echo '<p>'.sprintf(__('You have already setup WordPress and have been assigned an administrative login and password. Forget it. You didn&#8217;t have that login in Textpattern, why should you have it here? Instead we have taken care to import all of your users into our system. Unfortunately there is one downside. Because both WordPress and Textpattern uses a strong encryption hash with passwords, it is impossible to decrypt it and we are forced to assign temporary passwords to all your users. <strong>Every user has the same username, but their passwords are reset to password123.</strong> So <a href="%1$s">log in</a> and change it.'), get_bloginfo( 'wpurl' ) . '/wp-login.php').'</p>';
echo '<h3>'.__('Preserving Authors').'</h3>';
echo '<p>'.__('Secondly, we have attempted to preserve post authors. If you are the only author or contributor to your blog, then you are safe. In most cases, we are successful in this preservation endeavor. However, if we cannot ascertain the name of the writer due to discrepancies between database tables, we assign it to you, the administrative user.').'</p>';
echo '<h3>'.__('Textile').'</h3>';
echo '<p>'.__('Also, since you&#8217;re coming from Textpattern, you probably have been using Textile to format your comments and posts. If this is the case, we recommend downloading and installing <a href="http://www.huddledmasses.org/category/development/wordpress/textile/">Textile for WordPress</a>. Trust me... You&#8217;ll want it.').'</p>';
echo '<h3>'.__('WordPress Resources').'</h3>';
echo '<p>'.__('Finally, there are numerous WordPress resources around the internet. Some of them are:').'</p>';
echo '<ul>';
echo '<li>'.__('<a href="http://wordpress.org/">The official WordPress site</a>').'</li>';
echo '<li>'.__('<a href="http://wordpress.org/support/">The WordPress support forums</a>').'</li>';
echo '<li>'.__('<a href="http://codex.wordpress.org/">The Codex (In other words, the WordPress Bible)</a>').'</li>';
echo '</ul>';
echo '<p>'.sprintf(__('That&#8217;s it! What are you waiting for? Go <a href="%1$s">log in</a>!'), get_bloginfo( 'wpurl' ) . '/wp-login.php').'</p>';
}
function db_form()
{
echo '<table class="form-table">';
printf('<tr><th scope="row"><label for="dbuser">%s</label></th><td><input type="text" name="dbuser" id="dbuser" /></td></tr>', __('Textpattern Database User:'));
printf('<tr><th scope="row"><label for="dbpass">%s</label></th><td><input type="password" name="dbpass" id="dbpass" /></td></tr>', __('Textpattern Database Password:'));
printf('<tr><th scope="row"><label for="dbname">%s</label></th><td><input type="text" id="dbname" name="dbname" /></td></tr>', __('Textpattern Database Name:'));
printf('<tr><th scope="row"><label for="dbhost">%s</label></th><td><input type="text" id="dbhost" name="dbhost" value="localhost" /></td></tr>', __('Textpattern Database Host:'));
printf('<tr><th scope="row"><label for="dbprefix">%s</label></th><td><input type="text" name="dbprefix" id="dbprefix" /></td></tr>', __('Textpattern Table prefix (if any):'));
echo '</table>';
}
function dispatch()
{
if (empty ($_GET['step']))
$step = 0;
else
$step = (int) $_GET['step'];
$this->header();
if ( $step > 0 )
{
check_admin_referer('import-textpattern');
if($_POST['dbuser'])
{
if(get_option('txpuser'))
delete_option('txpuser');
add_option('txpuser', sanitize_user($_POST['dbuser'], true));
}
if($_POST['dbpass'])
{
if(get_option('txppass'))
delete_option('txppass');
add_option('txppass', sanitize_user($_POST['dbpass'], true));
}
if($_POST['dbname'])
{
if(get_option('txpname'))
delete_option('txpname');
add_option('txpname', sanitize_user($_POST['dbname'], true));
}
if($_POST['dbhost'])
{
if(get_option('txphost'))
delete_option('txphost');
add_option('txphost', sanitize_user($_POST['dbhost'], true));
}
if($_POST['dbprefix'])
{
if(get_option('tpre'))
delete_option('tpre');
add_option('tpre', sanitize_user($_POST['dbprefix']));
}
}
switch ($step)
{
default:
case 0 :
$this->greet();
break;
case 1 :
$this->import_categories();
break;
case 2 :
$this->import_users();
break;
case 3 :
$result = $this->import_posts();
if ( is_wp_error( $result ) )
echo $result->get_error_message();
break;
case 4 :
$this->import_comments();
break;
case 5 :
$this->import_links();
break;
case 6 :
$this->cleanup_txpimport();
break;
}
$this->footer();
}
function Textpattern_Import()
{
// Nothing.
}
}
$txp_import = new Textpattern_Import();
register_importer('textpattern', __('Textpattern'), __('Import categories, users, posts, comments, and links from a Textpattern blog.'), array ($txp_import, 'dispatch'));
?>

View File

@ -1,290 +0,0 @@
<?php
/**
* The Ultimate Tag Warrior Importer.
*
* @package WordPress
* @subpackage Importer
*/
/**
* Ultimate Tag Warrior Converter to 2.3 taxonomy.
*
* This converts the Ultimate Tag Warrior tags to the 2.3 WordPress taxonomy.
*
* @since 2.3.0
*/
class UTW_Import {
function header() {
echo '<div class="wrap">';
screen_icon();
echo '<h2>'.__('Import Ultimate Tag Warrior').'</h2>';
echo '<p>'.__('Steps may take a few minutes depending on the size of your database. Please be patient.').'<br /><br /></p>';
}
function footer() {
echo '</div>';
}
function greet() {
echo '<div class="narrow">';
echo '<p>'.__('Howdy! This imports tags from Ultimate Tag Warrior 3 into WordPress tags.').'</p>';
echo '<p>'.__('This has not been tested on any other versions of Ultimate Tag Warrior. Mileage may vary.').'</p>';
echo '<p>'.__('To accommodate larger databases for those tag-crazy authors out there, we have made this into an easy 5-step program to help you kick that nasty UTW habit. Just keep clicking along and we will let you know when you are in the clear!').'</p>';
echo '<p><strong>'.__('Don&#8217;t be stupid - backup your database before proceeding!').'</strong></p>';
echo '<form action="admin.php?import=utw&amp;step=1" method="post">';
echo '<p class="submit"><input type="submit" name="submit" class="button" value="'.esc_attr__('Step 1').'" /></p>';
echo '</form>';
echo '</div>';
}
function dispatch () {
if ( empty( $_GET['step'] ) ) {
$step = 0;
} else {
$step = (int) $_GET['step'];
}
if ( $step > 1 )
check_admin_referer('import-utw');
// load the header
$this->header();
switch ( $step ) {
case 0 :
$this->greet();
break;
case 1 :
$this->import_tags();
break;
case 2 :
$this->import_posts();
break;
case 3:
$this->import_t2p();
break;
case 4:
$this->cleanup_import();
break;
}
// load the footer
$this->footer();
}
function import_tags ( ) {
echo '<div class="narrow">';
echo '<p><h3>'.__('Reading UTW Tags&#8230;').'</h3></p>';
$tags = $this->get_utw_tags();
// if we didn't get any tags back, that's all there is folks!
if ( !is_array($tags) ) {
echo '<p>' . __('No Tags Found!') . '</p>';
return false;
}
else {
// if there's an existing entry, delete it
if ( get_option('utwimp_tags') ) {
delete_option('utwimp_tags');
}
add_option('utwimp_tags', $tags);
$count = count($tags);
echo '<p>' . sprintf( _n('Done! <strong>%s</strong> tag were read.', 'Done! <strong>%s</strong> tags were read.', $count), $count ) . '<br /></p>';
echo '<p>' . __('The following tags were found:') . '</p>';
echo '<ul>';
foreach ( $tags as $tag_id => $tag_name ) {
echo '<li>' . $tag_name . '</li>';
}
echo '</ul>';
echo '<br />';
echo '<p>' . __('If you don&#8217;t want to import any of these tags, you should delete them from the UTW tag management page and then re-run this import.') . '</p>';
}
echo '<form action="admin.php?import=utw&amp;step=2" method="post">';
wp_nonce_field('import-utw');
echo '<p class="submit"><input type="submit" name="submit" class="button" value="'.esc_attr__('Step 2').'" /></p>';
echo '</form>';
echo '</div>';
}
function import_posts ( ) {
echo '<div class="narrow">';
echo '<p><h3>'.__('Reading UTW Post Tags&#8230;').'</h3></p>';
// read in all the UTW tag -> post settings
$posts = $this->get_utw_posts();
// if we didn't get any tags back, that's all there is folks!
if ( !is_array($posts) ) {
echo '<p>' . __('No posts were found to have tags!') . '</p>';
return false;
}
else {
// if there's an existing entry, delete it
if ( get_option('utwimp_posts') ) {
delete_option('utwimp_posts');
}
add_option('utwimp_posts', $posts);
$count = count($posts);
echo '<p>' . sprintf( _n('Done! <strong>%s</strong> tag to post relationships were read.', 'Done! <strong>%s</strong> tags to post relationships were read.', $count), $count ) . '<br /></p>';
}
echo '<form action="admin.php?import=utw&amp;step=3" method="post">';
wp_nonce_field('import-utw');
echo '<p class="submit"><input type="submit" name="submit" class="button" value="'.esc_attr__('Step 3').'" /></p>';
echo '</form>';
echo '</div>';
}
function import_t2p ( ) {
echo '<div class="narrow">';
echo '<p><h3>'.__('Adding Tags to Posts&#8230;').'</h3></p>';
// run that funky magic!
$tags_added = $this->tag2post();
echo '<p>' . sprintf( _n( 'Done! <strong>%s</strong> tag were added!', 'Done! <strong>%s</strong> tags were added!', $tags_added ), $tags_added ) . '<br /></p>';
echo '<form action="admin.php?import=utw&amp;step=4" method="post">';
wp_nonce_field('import-utw');
echo '<p class="submit"><input type="submit" name="submit" class="button" value="'.esc_attr__('Step 4').'" /></p>';
echo '</form>';
echo '</div>';
}
function get_utw_tags ( ) {
global $wpdb;
// read in all the tags from the UTW tags table: should be wp_tags
$tags_query = "SELECT tag_id, tag FROM " . $wpdb->prefix . "tags";
$tags = $wpdb->get_results($tags_query);
// rearrange these tags into something we can actually use
foreach ( $tags as $tag ) {
$new_tags[$tag->tag_id] = $tag->tag;
}
return $new_tags;
}
function get_utw_posts ( ) {
global $wpdb;
// read in all the posts from the UTW post->tag table: should be wp_post2tag
$posts_query = "SELECT tag_id, post_id FROM " . $wpdb->prefix . "post2tag";
$posts = $wpdb->get_results($posts_query);
return $posts;
}
function tag2post ( ) {
// get the tags and posts we imported in the last 2 steps
$tags = get_option('utwimp_tags');
$posts = get_option('utwimp_posts');
// null out our results
$tags_added = 0;
// loop through each post and add its tags to the db
foreach ( $posts as $this_post ) {
$the_post = (int) $this_post->post_id;
$the_tag = (int) $this_post->tag_id;
// what's the tag name for that id?
$the_tag = $tags[$the_tag];
// screw it, just try to add the tag
wp_add_post_tags($the_post, $the_tag);
$tags_added++;
}
// that's it, all posts should be linked to their tags properly, pending any errors we just spit out!
return $tags_added;
}
function cleanup_import ( ) {
delete_option('utwimp_tags');
delete_option('utwimp_posts');
$this->done();
}
function done ( ) {
echo '<div class="narrow">';
echo '<p><h3>'.__('Import Complete!').'</h3></p>';
echo '<p>' . __('OK, so we lied about this being a 5-step program! You&#8217;re done!') . '</p>';
echo '<p>' . __('Now wasn&#8217;t that easy?') . '</p>';
echo '</div>';
}
function UTW_Import ( ) {
// Nothing.
}
}
// create the import object
$utw_import = new UTW_Import();
// add it to the import page!
register_importer('utw', 'Ultimate Tag Warrior', __('Import Ultimate Tag Warrior tags into WordPress tags.'), array($utw_import, 'dispatch'));
?>

View File

@ -1,898 +0,0 @@
<?php
/**
* WordPress Importer
*
* @package WordPress
* @subpackage Importer
*/
/**
* WordPress Importer
*
* Will process the WordPress eXtended RSS files that you upload from the export
* file.
*
* @since unknown
*/
class WP_Import {
var $post_ids_processed = array ();
var $orphans = array ();
var $file;
var $id;
var $mtnames = array ();
var $newauthornames = array ();
var $allauthornames = array ();
var $author_ids = array ();
var $tags = array ();
var $categories = array ();
var $terms = array ();
var $j = -1;
var $fetch_attachments = false;
var $url_remap = array ();
function header() {
echo '<div class="wrap">';
screen_icon();
echo '<h2>'.__('Import WordPress').'</h2>';
}
function footer() {
echo '</div>';
}
function greet() {
echo '<div class="narrow">';
echo '<p>'.__('Howdy! Upload your WordPress eXtended RSS (WXR) file and we&#8217;ll import the posts, pages, comments, custom fields, categories, and tags into this site.').'</p>';
echo '<p>'.__('Choose a WordPress WXR file to upload, then click Upload file and import.').'</p>';
wp_import_upload_form("admin.php?import=wordpress&amp;step=1");
echo '</div>';
}
function get_tag( $string, $tag ) {
global $wpdb;
preg_match("|<$tag.*?>(.*?)</$tag>|is", $string, $return);
if ( isset($return[1]) ) {
$return = preg_replace('|^<!\[CDATA\[(.*)\]\]>$|s', '$1', $return[1]);
$return = $wpdb->escape( trim( $return ) );
} else {
$return = '';
}
return $return;
}
function has_gzip() {
return is_callable('gzopen');
}
function fopen($filename, $mode='r') {
if ( $this->has_gzip() )
return gzopen($filename, $mode);
return fopen($filename, $mode);
}
function feof($fp) {
if ( $this->has_gzip() )
return gzeof($fp);
return feof($fp);
}
function fgets($fp, $len=8192) {
if ( $this->has_gzip() )
return gzgets($fp, $len);
return fgets($fp, $len);
}
function fclose($fp) {
if ( $this->has_gzip() )
return gzclose($fp);
return fclose($fp);
}
function get_entries($process_post_func=NULL) {
set_magic_quotes_runtime(0);
$doing_entry = false;
$is_wxr_file = false;
$fp = $this->fopen($this->file, 'r');
if ($fp) {
while ( !$this->feof($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:base_site_url>') ) {
preg_match('|<wp:base_site_url>(.*?)</wp:base_site_url>|is', $importline, $url);
$this->base_url = $url[1];
continue;
}
if ( false !== strpos($importline, '<wp:category>') ) {
preg_match('|<wp:category>(.*?)</wp:category>|is', $importline, $category);
$this->categories[] = $category[1];
continue;
}
if ( false !== strpos($importline, '<wp:tag>') ) {
preg_match('|<wp:tag>(.*?)</wp:tag>|is', $importline, $tag);
$this->tags[] = $tag[1];
continue;
}
if ( false !== strpos($importline, '<wp:term>') ) {
preg_match('|<wp:term>(.*?)</wp:term>|is', $importline, $term);
$this->terms[] = $term[1];
continue;
}
if ( false !== strpos($importline, '<item>') ) {
$this->post = '';
$doing_entry = true;
continue;
}
if ( false !== strpos($importline, '</item>') ) {
$doing_entry = false;
if ($process_post_func)
call_user_func($process_post_func, $this->post);
continue;
}
if ( $doing_entry ) {
$this->post .= $importline . "\n";
}
}
$this->fclose($fp);
}
return $is_wxr_file;
}
function get_wp_authors() {
// 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;
$authors[0] = array_shift($temp);
$y = count($temp) + 1;
for ($x = 1; $x < $y; $x ++) {
$next = array_shift($temp);
if (!(in_array($next, $authors)))
array_push($authors, $next);
}
return $authors;
}
function get_authors_from_post() {
global $current_user;
// this will populate $this->author_ids with a list of author_names => user_ids
foreach ( (array) $_POST['author_in'] as $i => $in_author_name ) {
if ( !empty($_POST['user_select'][$i]) ) {
// an existing user was selected in the dropdown list
$user = get_userdata( intval($_POST['user_select'][$i]) );
if ( isset($user->ID) )
$this->author_ids[$in_author_name] = $user->ID;
}
elseif ( $this->allow_create_users() ) {
// nothing was selected in the dropdown list, so we'll use the name in the text field
$new_author_name = trim($_POST['user_create'][$i]);
// if the user didn't enter a name, assume they want to use the same name as in the import file
if ( empty($new_author_name) )
$new_author_name = $in_author_name;
$user_id = username_exists($new_author_name);
if ( !$user_id ) {
$user_id = wp_create_user($new_author_name, wp_generate_password());
}
if ( !is_wp_error( $user_id ) ) {
$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);
}
}
}
function wp_authors_form() {
?>
<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>
<?php
if ( $this->allow_create_users() ) {
echo '<p>'.__('If a new user is created by WordPress, a password will be randomly generated. Manually change the user&#8217;s details if necessary.')."</p>\n";
}
$authors = $this->get_wp_authors();
echo '<form action="?import=wordpress&amp;step=2&amp;id=' . $this->id . '" method="post">';
wp_nonce_field('import-wordpress');
?>
<ol id="authors">
<?php
$j = -1;
foreach ($authors as $author) {
++ $j;
echo '<li>'.__('Import author:').' <strong>'.$author.'</strong><br />';
$this->users_form($j, $author);
echo '</li>';
}
if ( $this->allow_fetch_attachments() ) {
?>
</ol>
<h2><?php _e('Import Attachments'); ?></h2>
<p>
<input type="checkbox" value="1" name="attachments" id="import-attachments" />
<label for="import-attachments"><?php _e('Download and import file attachments') ?></label>
</p>
<?php
}
echo '<p class="submit">';
echo '<input type="submit" class="button" value="'. esc_attr__('Submit') .'" />'.'<br />';
echo '</p>';
echo '</form>';
}
function users_form($n, $author) {
if ( $this->allow_create_users() ) {
printf('<label>'.__('Create user %1$s or map to existing'), ' <input type="text" value="'. esc_attr($author) .'" name="'.'user_create['.intval($n).']'.'" maxlength="30" /></label> <br />');
}
else {
echo __('Map to existing').'<br />';
}
// keep track of $n => $author name
echo '<input type="hidden" name="author_in['.intval($n).']" value="' . esc_attr($author).'" />';
$users = get_users_of_blog();
?><select name="user_select[<?php echo $n; ?>]">
<option value="0"><?php _e('- Select -'); ?></option>
<?php
foreach ($users as $user) {
echo '<option value="'.$user->user_id.'">'.$user->user_login.'</option>';
}
?>
</select>
<?php
}
function select_authors() {
$is_wxr_file = $this->get_entries(array(&$this, 'process_author'));
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() {
global $wpdb;
$cat_names = (array) get_terms('category', array('fields' => 'names'));
while ( $c = array_shift($this->categories) ) {
$cat_name = trim($this->get_tag( $c, 'wp:cat_name' ));
// If the category exists we leave it alone
if ( in_array($cat_name, $cat_names) )
continue;
$category_nicename = $this->get_tag( $c, 'wp:category_nicename' );
$category_description = $this->get_tag( $c, 'wp:category_description' );
$posts_private = (int) $this->get_tag( $c, 'wp:posts_private' );
$links_private = (int) $this->get_tag( $c, 'wp:links_private' );
$parent = $this->get_tag( $c, 'wp:category_parent' );
if ( empty($parent) )
$category_parent = '0';
else
$category_parent = category_exists($parent);
$catarr = compact('category_nicename', 'category_parent', 'posts_private', 'links_private', 'posts_private', 'cat_name', 'category_description');
print '<em>' . sprintf( __( 'Importing category <em>%s</em>&#8230;' ), esc_html($cat_name) ) . '</em><br />' . "\n";
$cat_ID = wp_insert_category($catarr);
}
}
function process_tags() {
global $wpdb;
$tag_names = (array) get_terms('post_tag', array('fields' => 'names'));
while ( $c = array_shift($this->tags) ) {
$tag_name = trim($this->get_tag( $c, 'wp:tag_name' ));
// If the category exists we leave it alone
if ( in_array($tag_name, $tag_names) )
continue;
$slug = $this->get_tag( $c, 'wp:tag_slug' );
$description = $this->get_tag( $c, 'wp:tag_description' );
$tagarr = compact('slug', 'description');
print '<em>' . sprintf( __( 'Importing tag <em>%s</em>&#8230;' ), esc_html($tag_name) ) . '</em><br />' . "\n";
$tag_ID = wp_insert_term($tag_name, 'post_tag', $tagarr);
}
}
function process_terms() {
global $wpdb, $wp_taxonomies;
$custom_taxonomies = $wp_taxonomies;
// get rid of the standard taxonomies
unset( $custom_taxonomies['category'] );
unset( $custom_taxonomies['post_tag'] );
unset( $custom_taxonomies['link_category'] );
$custom_taxonomies = array_keys( $custom_taxonomies );
$current_terms = (array) get_terms( $custom_taxonomies, array('get' => 'all') );
$taxonomies = array();
foreach ( $current_terms as $term ) {
if ( isset( $_terms[$term->taxonomy] ) ) {
$taxonomies[$term->taxonomy] = array_merge( $taxonomies[$term->taxonomy], array($term->name) );
} else {
$taxonomies[$term->taxonomy] = array($term->name);
}
}
while ( $c = array_shift($this->terms) ) {
$term_name = trim($this->get_tag( $c, 'wp:term_name' ));
$term_taxonomy = trim($this->get_tag( $c, 'wp:term_taxonomy' ));
// If the term exists in the taxonomy we leave it alone
if ( isset($taxonomies[$term_taxonomy] ) && in_array( $term_name, $taxonomies[$term_taxonomy] ) )
continue;
$slug = $this->get_tag( $c, 'wp:term_slug' );
$description = $this->get_tag( $c, 'wp:term_description' );
$termarr = compact('slug', 'description');
print '<em>' . sprintf( __( 'Importing <em>%s</em>&#8230;' ), esc_html($term_name) ) . '</em><br />' . "\n";
$term_ID = wp_insert_term($term_name, $this->get_tag( $c, 'wp:term_taxonomy' ), $termarr);
}
}
function process_author($post) {
$author = $this->get_tag( $post, 'dc:creator' );
if ($author)
$this->allauthornames[] = $author;
}
function process_posts() {
echo '<ol>';
$this->get_entries(array(&$this, 'process_post'));
echo '</ol>';
wp_import_cleanup($this->id);
do_action('import_done', 'wordpress');
echo '<h3>'.sprintf(__('All done.').' <a href="%s">'.__('Have fun!').'</a>', get_option('home')).'</h3>';
}
function _normalize_tag( $matches ) {
return '<' . strtolower( $matches[1] );
}
function process_post($post) {
global $wpdb;
$post_ID = (int) $this->get_tag( $post, 'wp:post_id' );
if ( $post_ID && !empty($this->post_ids_processed[$post_ID]) ) // Processed already
return 0;
set_time_limit( 60 );
// There are only ever one of these
$post_title = $this->get_tag( $post, 'title' );
$post_date = $this->get_tag( $post, 'wp:post_date' );
$post_date_gmt = $this->get_tag( $post, 'wp:post_date_gmt' );
$comment_status = $this->get_tag( $post, 'wp:comment_status' );
$ping_status = $this->get_tag( $post, 'wp:ping_status' );
$post_status = $this->get_tag( $post, 'wp:status' );
$post_name = $this->get_tag( $post, 'wp:post_name' );
$post_parent = $this->get_tag( $post, 'wp:post_parent' );
$menu_order = $this->get_tag( $post, 'wp:menu_order' );
$post_type = $this->get_tag( $post, 'wp:post_type' );
$post_password = $this->get_tag( $post, 'wp:post_password' );
$is_sticky = $this->get_tag( $post, 'wp:is_sticky' );
$guid = $this->get_tag( $post, 'guid' );
$post_author = $this->get_tag( $post, 'dc:creator' );
$post_excerpt = $this->get_tag( $post, 'excerpt:encoded' );
$post_excerpt = preg_replace_callback('|<(/?[A-Z]+)|', array( &$this, '_normalize_tag' ), $post_excerpt);
$post_excerpt = str_replace('<br>', '<br />', $post_excerpt);
$post_excerpt = str_replace('<hr>', '<hr />', $post_excerpt);
$post_content = $this->get_tag( $post, 'content:encoded' );
$post_content = preg_replace_callback('|<(/?[A-Z]+)|', array( &$this, '_normalize_tag' ), $post_content);
$post_content = str_replace('<br>', '<br />', $post_content);
$post_content = str_replace('<hr>', '<hr />', $post_content);
preg_match_all('|<category domain="tag">(.*?)</category>|is', $post, $tags);
$tags = $tags[1];
$tag_index = 0;
foreach ($tags as $tag) {
$tags[$tag_index] = $wpdb->escape( html_entity_decode( str_replace(array( '<![CDATA[', ']]>' ), '', $tag ) ) );
$tag_index++;
}
preg_match_all('|<category>(.*?)</category>|is', $post, $categories);
$categories = $categories[1];
$cat_index = 0;
foreach ($categories as $category) {
$categories[$cat_index] = $wpdb->escape( html_entity_decode( str_replace( array( '<![CDATA[', ']]>' ), '', $category ) ) );
$cat_index++;
}
$post_exists = post_exists($post_title, '', $post_date);
if ( $post_exists ) {
echo '<li>';
printf(__('Post <em>%s</em> already exists.'), stripslashes($post_title));
$comment_post_ID = $post_id = $post_exists;
} else {
// If it has parent, process parent first.
$post_parent = (int) $post_parent;
if ($post_parent) {
// if we already know the parent, map it to the local ID
if ( isset( $this->post_ids_processed[$post_parent] ) ) {
$post_parent = $this->post_ids_processed[$post_parent]; // new ID of the parent
}
else {
// record the parent for later
$this->orphans[intval($post_ID)] = $post_parent;
}
}
echo '<li>';
$post_author = $this->checkauthor($post_author); //just so that if a post already exists, new users are not created by checkauthor
$postdata = compact('post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_excerpt', 'post_title', 'post_status', 'post_name', 'comment_status', 'ping_status', 'guid', 'post_parent', 'menu_order', 'post_type', 'post_password');
$postdata['import_id'] = $post_ID;
if ($post_type == 'attachment') {
$remote_url = $this->get_tag( $post, 'wp:attachment_url' );
if ( !$remote_url )
$remote_url = $guid;
$comment_post_ID = $post_id = $this->process_attachment($postdata, $remote_url);
if ( !$post_id or is_wp_error($post_id) )
return $post_id;
}
else {
printf(__('Importing post <em>%s</em>...') . "\n", stripslashes($post_title));
$comment_post_ID = $post_id = wp_insert_post($postdata);
if ( $post_id && $is_sticky == 1 )
stick_post( $post_id );
}
if ( is_wp_error( $post_id ) )
return $post_id;
// Memorize old and new ID.
if ( $post_id && $post_ID ) {
$this->post_ids_processed[intval($post_ID)] = intval($post_id);
}
// Add categories.
if (count($categories) > 0) {
$post_cats = array();
foreach ($categories as $category) {
if ( '' == $category )
continue;
$slug = sanitize_term_field('slug', $category, 0, 'category', 'db');
$cat = get_term_by('slug', $slug, 'category');
$cat_ID = 0;
if ( ! empty($cat) )
$cat_ID = $cat->term_id;
if ($cat_ID == 0) {
$category = $wpdb->escape($category);
$cat_ID = wp_insert_category(array('cat_name' => $category));
if ( is_wp_error($cat_ID) )
continue;
}
$post_cats[] = $cat_ID;
}
wp_set_post_categories($post_id, $post_cats);
}
// Add tags.
if (count($tags) > 0) {
$post_tags = array();
foreach ($tags as $tag) {
if ( '' == $tag )
continue;
$slug = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
$tag_obj = get_term_by('slug', $slug, 'post_tag');
$tag_id = 0;
if ( ! empty($tag_obj) )
$tag_id = $tag_obj->term_id;
if ( $tag_id == 0 ) {
$tag = $wpdb->escape($tag);
$tag_id = wp_insert_term($tag, 'post_tag');
if ( is_wp_error($tag_id) )
continue;
$tag_id = $tag_id['term_id'];
}
$post_tags[] = intval($tag_id);
}
wp_set_post_tags($post_id, $post_tags);
}
}
// Now for comments
preg_match_all('|<wp:comment>(.*?)</wp:comment>|is', $post, $comments);
$comments = $comments[1];
$num_comments = 0;
$inserted_comments = array();
if ( $comments) {
foreach ($comments as $comment) {
$comment_id = $this->get_tag( $comment, 'wp:comment_id');
$newcomments[$comment_id]['comment_post_ID'] = $comment_post_ID;
$newcomments[$comment_id]['comment_author'] = $this->get_tag( $comment, 'wp:comment_author');
$newcomments[$comment_id]['comment_author_email'] = $this->get_tag( $comment, 'wp:comment_author_email');
$newcomments[$comment_id]['comment_author_IP'] = $this->get_tag( $comment, 'wp:comment_author_IP');
$newcomments[$comment_id]['comment_author_url'] = $this->get_tag( $comment, 'wp:comment_author_url');
$newcomments[$comment_id]['comment_date'] = $this->get_tag( $comment, 'wp:comment_date');
$newcomments[$comment_id]['comment_date_gmt'] = $this->get_tag( $comment, 'wp:comment_date_gmt');
$newcomments[$comment_id]['comment_content'] = $this->get_tag( $comment, 'wp:comment_content');
$newcomments[$comment_id]['comment_approved'] = $this->get_tag( $comment, 'wp:comment_approved');
$newcomments[$comment_id]['comment_type'] = $this->get_tag( $comment, 'wp:comment_type');
$newcomments[$comment_id]['comment_parent'] = $this->get_tag( $comment, 'wp:comment_parent');
}
// Sort by comment ID, to make sure comment parents exist (if there at all)
ksort($newcomments);
foreach ($newcomments as $key => $comment) {
// if this is a new post we can skip the comment_exists() check
if ( !$post_exists || !comment_exists($comment['comment_author'], $comment['comment_date']) ) {
if (isset($inserted_comments[$comment['comment_parent']]))
$comment['comment_parent'] = $inserted_comments[$comment['comment_parent']];
$comment = wp_filter_comment($comment);
$inserted_comments[$key] = wp_insert_comment($comment);
$num_comments++;
}
}
}
if ( $num_comments )
printf(' '._n('(%s comment)', '(%s comments)', $num_comments), $num_comments);
// Now for post meta
preg_match_all('|<wp:postmeta>(.*?)</wp:postmeta>|is', $post, $postmeta);
$postmeta = $postmeta[1];
if ( $postmeta) { foreach ($postmeta as $p) {
$key = $this->get_tag( $p, 'wp:meta_key' );
$value = $this->get_tag( $p, 'wp:meta_value' );
$value = stripslashes($value); // add_post_meta() will escape.
// get_post_meta would have done this but we read straight from the db on export so we could have a serialized string
$value = maybe_unserialize($value);
$this->process_post_meta($post_id, $key, $value);
} }
do_action('import_post_added', $post_id);
print "</li>\n";
}
function process_post_meta($post_id, $key, $value) {
// the filter can return false to skip a particular metadata key
$_key = apply_filters('import_post_meta_key', $key);
if ( $_key ) {
add_post_meta( $post_id, $_key, $value );
do_action('import_post_meta', $post_id, $_key, $value);
}
}
function process_attachment($postdata, $remote_url) {
if ($this->fetch_attachments and $remote_url) {
printf( __('Importing attachment <em>%s</em>... '), htmlspecialchars($remote_url) );
// If the URL is absolute, but does not contain http, upload it assuming the base_site_url variable
if ( preg_match('/^\/[\w\W]+$/', $remote_url) )
$remote_url = rtrim($this->base_url,'/').$remote_url;
$upload = $this->fetch_remote_file($postdata, $remote_url);
if ( is_wp_error($upload) ) {
printf( __('Remote file error: %s'), htmlspecialchars($upload->get_error_message()) );
return $upload;
}
else {
print '('.size_format(filesize($upload['file'])).')';
}
if ( 0 == filesize( $upload['file'] ) ) {
print __( "Zero length file, deleting" ) . "\n";
unlink( $upload['file'] );
return;
}
if ( $info = wp_check_filetype($upload['file']) ) {
$postdata['post_mime_type'] = $info['type'];
}
else {
print __('Invalid file type');
return;
}
$postdata['guid'] = $upload['url'];
// as per wp-admin/includes/upload.php
$post_id = wp_insert_attachment($postdata, $upload['file']);
wp_update_attachment_metadata( $post_id, wp_generate_attachment_metadata( $post_id, $upload['file'] ) );
// remap the thumbnail url. this isn't perfect because we're just guessing the original url.
if ( preg_match('@^image/@', $info['type']) && $thumb_url = wp_get_attachment_thumb_url($post_id) ) {
$parts = pathinfo($remote_url);
$ext = $parts['extension'];
$name = basename($parts['basename'], ".{$ext}");
$this->url_remap[$parts['dirname'] . '/' . $name . '.thumbnail.' . $ext] = $thumb_url;
}
return $post_id;
}
else {
printf( __('Skipping attachment <em>%s</em>'), htmlspecialchars($remote_url) );
}
}
function fetch_remote_file( $post, $url ) {
add_filter( 'http_request_timeout', array( &$this, 'bump_request_timeout' ) );
$upload = wp_upload_dir($post['post_date']);
// extract the file name and extension from the url
$file_name = basename($url);
// get placeholder file in the upload dir with a unique sanitized filename
$upload = wp_upload_bits( $file_name, 0, '', $post['post_date']);
if ( $upload['error'] ) {
echo $upload['error'];
return new WP_Error( 'upload_dir_error', $upload['error'] );
}
// fetch the remote url and write it to the placeholder file
$headers = wp_get_http($url, $upload['file']);
//Request failed
if ( ! $headers ) {
@unlink($upload['file']);
return new WP_Error( 'import_file_error', __('Remote server did not respond') );
}
// make sure the fetch was successful
if ( $headers['response'] != '200' ) {
@unlink($upload['file']);
return new WP_Error( 'import_file_error', sprintf(__('Remote file returned error response %1$d %2$s'), $headers['response'], get_status_header_desc($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') );
}
$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
$this->url_remap[$url] = $upload['url'];
$this->url_remap[$post['guid']] = $upload['url'];
// if the remote url is redirected somewhere else, keep track of the destination too
if ( $headers['x-final-location'] != $url )
$this->url_remap[$headers['x-final-location']] = $upload['url'];
return $upload;
}
/**
* Bump up the request timeout for http requests
*
* @param int $val
* @return int
*/
function bump_request_timeout( $val ) {
return 60;
}
// sort by strlen, longest string first
function cmpr_strlen($a, $b) {
return strlen($b) - strlen($a);
}
// update url references in post bodies to point to the new local files
function backfill_attachment_urls() {
// make sure we do the longest urls first, in case one is a substring of another
uksort($this->url_remap, array(&$this, 'cmpr_strlen'));
global $wpdb;
foreach ($this->url_remap as $from_url => $to_url) {
// remap urls in post_content
$wpdb->query( $wpdb->prepare("UPDATE {$wpdb->posts} SET post_content = REPLACE(post_content, '%s', '%s')", $from_url, $to_url) );
// remap enclosure urls
$result = $wpdb->query( $wpdb->prepare("UPDATE {$wpdb->postmeta} SET meta_value = REPLACE(meta_value, '%s', '%s') WHERE meta_key='enclosure'", $from_url, $to_url) );
}
}
// update the post_parent of orphans now that we know the local id's of all parents
function backfill_parents() {
global $wpdb;
foreach ($this->orphans as $child_id => $parent_id) {
$local_child_id = $local_parent_id = false;
if ( isset( $this->post_ids_processed[$child_id] ) )
$local_child_id = $this->post_ids_processed[$child_id];
if ( isset( $this->post_ids_processed[$parent_id] ) )
$local_parent_id = $this->post_ids_processed[$parent_id];
if ($local_child_id and $local_parent_id) {
$wpdb->update($wpdb->posts, array('post_parent' => $local_parent_id), array('ID' => $local_child_id) );
}
}
}
function is_valid_meta_key($key) {
// skip attachment metadata since we'll regenerate it from scratch
if ( $key == '_wp_attached_file' || $key == '_wp_attachment_metadata' )
return false;
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_start() {
wp_defer_term_counting(true);
wp_defer_comment_counting(true);
do_action('import_start');
}
function import_end() {
do_action('import_end');
// clear the caches after backfilling
foreach ($this->post_ids_processed as $post_id)
clean_post_cache($post_id);
wp_defer_term_counting(false);
wp_defer_comment_counting(false);
}
function import($id, $fetch_attachments = false) {
$this->id = (int) $id;
$this->fetch_attachments = ($this->allow_fetch_attachments() && (bool) $fetch_attachments);
add_filter('import_post_meta_key', array($this, 'is_valid_meta_key'));
$file = get_attached_file($this->id);
$this->import_file($file);
}
function import_file($file) {
$this->file = $file;
$this->import_start();
$this->get_authors_from_post();
wp_suspend_cache_invalidation(true);
$this->get_entries();
$this->process_categories();
$this->process_tags();
$this->process_terms();
$result = $this->process_posts();
wp_suspend_cache_invalidation(false);
$this->backfill_parents();
$this->backfill_attachment_urls();
$this->import_end();
if ( is_wp_error( $result ) )
return $result;
}
function handle_upload() {
$file = wp_import_handle_upload();
if ( isset($file['error']) ) {
echo '<p>'.__('Sorry, there has been an error.').'</p>';
echo '<p><strong>' . $file['error'] . '</strong></p>';
return false;
}
$this->file = $file['file'];
$this->id = (int) $file['id'];
return true;
}
function dispatch() {
if (empty ($_GET['step']))
$step = 0;
else
$step = (int) $_GET['step'];
$this->header();
switch ($step) {
case 0 :
$this->greet();
break;
case 1 :
check_admin_referer('import-upload');
if ( $this->handle_upload() )
$this->select_authors();
break;
case 2:
check_admin_referer('import-wordpress');
$fetch_attachments = ! empty( $_POST['attachments'] );
$result = $this->import( $_GET['id'], $fetch_attachments);
if ( is_wp_error( $result ) )
echo $result->get_error_message();
break;
}
$this->footer();
}
function WP_Import() {
// Nothing.
}
}
/**
* Register WordPress Importer
*
* @since unknown
* @var WP_Import
* @name $wp_import
*/
$wp_import = new WP_Import();
register_importer('wordpress', 'WordPress', __('Import <strong>posts, pages, comments, custom fields, categories, and tags</strong> from a WordPress export file.'), array ($wp_import, 'dispatch'));
?>

View File

@ -1,473 +0,0 @@
<?php
/**
* WordPress Categories to Tags Converter.
*
* @package WordPress
* @subpackage Importer
*/
/**
* WordPress categories to tags converter class.
*
* Will convert WordPress categories to tags, removing the category after the
* process is complete and updating all posts to switch to the tag.
*
* @since unknown
*/
class WP_Categories_to_Tags {
var $categories_to_convert = array();
var $all_categories = array();
var $tags_to_convert = array();
var $all_tags = array();
var $hybrids_ids = array();
function header() {
echo '<div class="wrap">';
if ( ! current_user_can('manage_categories') ) {
echo '<div class="narrow">';
echo '<p>' . __('Cheatin&#8217; uh?') . '</p>';
echo '</div>';
} else { ?>
<div class="tablenav"><p style="margin:4px"><a style="display:inline;" class="button-secondary" href="admin.php?import=wp-cat2tag"><?php _e( "Categories to Tags" ); ?></a>
<a style="display:inline;" class="button-secondary" href="admin.php?import=wp-cat2tag&amp;step=3"><?php _e( "Tags to Categories" ); ?></a></p></div>
<?php }
}
function footer() {
echo '</div>';
}
function populate_cats() {
$categories = get_categories(array('get' => 'all'));
foreach ( $categories as $category ) {
$this->all_categories[] = $category;
if ( is_term( $category->slug, 'post_tag' ) )
$this->hybrids_ids[] = $category->term_id;
}
}
function populate_tags() {
$tags = get_terms( array('post_tag'), array('get' => 'all') );
foreach ( $tags as $tag ) {
$this->all_tags[] = $tag;
if ( is_term( $tag->slug, 'category' ) )
$this->hybrids_ids[] = $tag->term_id;
}
}
function categories_tab() {
$this->populate_cats();
$cat_num = count($this->all_categories);
echo '<br class="clear" />';
if ( $cat_num > 0 ) {
screen_icon();
echo '<h2>' . sprintf( _n( 'Convert Category to Tag.', 'Convert Categories (%d) to Tags.', $cat_num ), $cat_num ) . '</h2>';
echo '<div class="narrow">';
echo '<p>' . __('Hey there. Here you can selectively convert existing categories to tags. To get started, check the categories you wish to be converted, then click the Convert button.') . '</p>';
echo '<p>' . __('Keep in mind that if you convert a category with child categories, the children become top-level orphans.') . '</p></div>';
$this->categories_form();
} else {
echo '<p>'.__('You have no categories to convert!').'</p>';
}
}
function categories_form() { ?>
<script type="text/javascript">
/* <![CDATA[ */
var checkflag = "false";
function check_all_rows() {
field = document.catlist;
if ( 'false' == checkflag ) {
for ( i = 0; i < field.length; i++ ) {
if ( 'cats_to_convert[]' == field[i].name )
field[i].checked = true;
}
checkflag = 'true';
return '<?php _e('Uncheck All') ?>';
} else {
for ( i = 0; i < field.length; i++ ) {
if ( 'cats_to_convert[]' == field[i].name )
field[i].checked = false;
}
checkflag = 'false';
return '<?php _e('Check All') ?>';
}
}
/* ]]> */
</script>
<form name="catlist" id="catlist" action="admin.php?import=wp-cat2tag&amp;step=2" method="post">
<p><input type="button" class="button-secondary" value="<?php esc_attr_e('Check All'); ?>" onclick="this.value=check_all_rows()" />
<?php wp_nonce_field('import-cat2tag'); ?></p>
<ul style="list-style:none">
<?php $hier = _get_term_hierarchy('category');
foreach ($this->all_categories as $category) {
$category = sanitize_term( $category, 'category', 'display' );
if ( (int) $category->parent == 0 ) { ?>
<li><label><input type="checkbox" name="cats_to_convert[]" value="<?php echo intval($category->term_id); ?>" /> <?php echo $category->name . ' (' . $category->count . ')'; ?></label><?php
if ( in_array( intval($category->term_id), $this->hybrids_ids ) )
echo ' <a href="#note"> * </a>';
if ( isset($hier[$category->term_id]) )
$this->_category_children($category, $hier); ?></li>
<?php }
} ?>
</ul>
<?php if ( ! empty($this->hybrids_ids) )
echo '<p><a name="note"></a>' . __('* This category is also a tag. Converting it will add that tag to all posts that are currently in the category.') . '</p>'; ?>
<p class="submit"><input type="submit" name="submit" class="button" value="<?php esc_attr_e('Convert Categories to Tags'); ?>" /></p>
</form>
<?php }
function tags_tab() {
$this->populate_tags();
$tags_num = count($this->all_tags);
echo '<br class="clear" />';
if ( $tags_num > 0 ) {
screen_icon();
echo '<h2>' . sprintf( _n( 'Convert Tag to Category.', 'Convert Tags (%d) to Categories.', $tags_num ), $tags_num ) . '</h2>';
echo '<div class="narrow">';
echo '<p>' . __('Here you can selectively convert existing tags to categories. To get started, check the tags you wish to be converted, then click the Convert button.') . '</p>';
echo '<p>' . __('The newly created categories will still be associated with the same posts.') . '</p></div>';
$this->tags_form();
} else {
echo '<p>'.__('You have no tags to convert!').'</p>';
}
}
function tags_form() { ?>
<script type="text/javascript">
/* <![CDATA[ */
var checktags = "false";
function check_all_tagrows() {
field = document.taglist;
if ( 'false' == checktags ) {
for ( i = 0; i < field.length; i++ ) {
if ( 'tags_to_convert[]' == field[i].name )
field[i].checked = true;
}
checktags = 'true';
return '<?php _e('Uncheck All') ?>';
} else {
for ( i = 0; i < field.length; i++ ) {
if ( 'tags_to_convert[]' == field[i].name )
field[i].checked = false;
}
checktags = 'false';
return '<?php _e('Check All') ?>';
}
}
/* ]]> */
</script>
<form name="taglist" id="taglist" action="admin.php?import=wp-cat2tag&amp;step=4" method="post">
<p><input type="button" class="button-secondary" value="<?php esc_attr_e('Check All'); ?>" onclick="this.value=check_all_tagrows()" />
<?php wp_nonce_field('import-cat2tag'); ?></p>
<ul style="list-style:none">
<?php foreach ( $this->all_tags as $tag ) { ?>
<li><label><input type="checkbox" name="tags_to_convert[]" value="<?php echo intval($tag->term_id); ?>" /> <?php echo esc_attr($tag->name) . ' (' . $tag->count . ')'; ?></label><?php if ( in_array( intval($tag->term_id), $this->hybrids_ids ) ) echo ' <a href="#note"> * </a>'; ?></li>
<?php } ?>
</ul>
<?php if ( ! empty($this->hybrids_ids) )
echo '<p><a name="note"></a>' . __('* This tag is also a category. When converted, all posts associated with the tag will also be in the category.') . '</p>'; ?>
<p class="submit"><input type="submit" name="submit_tags" class="button" value="<?php esc_attr_e('Convert Tags to Categories'); ?>" /></p>
</form>
<?php }
function _category_children($parent, $hier) { ?>
<ul style="list-style:none">
<?php foreach ($hier[$parent->term_id] as $child_id) {
$child =& get_category($child_id); ?>
<li><label><input type="checkbox" name="cats_to_convert[]" value="<?php echo intval($child->term_id); ?>" /> <?php echo $child->name . ' (' . $child->count . ')'; ?></label><?php
if ( in_array( intval($child->term_id), $this->hybrids_ids ) )
echo ' <a href="#note"> * </a>';
if ( isset($hier[$child->term_id]) )
$this->_category_children($child, $hier); ?></li>
<?php } ?>
</ul><?php
}
function _category_exists($cat_id) {
$cat_id = (int) $cat_id;
$maybe_exists = category_exists($cat_id);
if ( $maybe_exists ) {
return true;
} else {
return false;
}
}
function convert_categories() {
global $wpdb;
if ( (!isset($_POST['cats_to_convert']) || !is_array($_POST['cats_to_convert'])) && empty($this->categories_to_convert)) { ?>
<div class="narrow">
<p><?php printf(__('Uh, oh. Something didn&#8217;t work. Please <a href="%s">try again</a>.'), 'admin.php?import=wp-cat2tag'); ?></p>
</div>
<?php return;
}
if ( empty($this->categories_to_convert) )
$this->categories_to_convert = $_POST['cats_to_convert'];
$hier = _get_term_hierarchy('category');
$hybrid_cats = $clear_parents = $parents = false;
$clean_term_cache = $clean_cat_cache = array();
$default_cat = get_option('default_category');
echo '<ul>';
foreach ( (array) $this->categories_to_convert as $cat_id) {
$cat_id = (int) $cat_id;
if ( ! $this->_category_exists($cat_id) ) {
echo '<li>' . sprintf( __('Category %s doesn&#8217;t exist!'), $cat_id ) . "</li>\n";
} else {
$category =& get_category($cat_id);
echo '<li>' . sprintf(__('Converting category <strong>%s</strong> ... '), $category->name);
// If the category is the default, leave category in place and create tag.
if ( $default_cat == $category->term_id ) {
if ( ! ($id = is_term( $category->slug, 'post_tag' ) ) )
$id = wp_insert_term($category->name, 'post_tag', array('slug' => $category->slug));
$id = $id['term_taxonomy_id'];
$posts = get_objects_in_term($category->term_id, 'category');
$term_order = 0;
foreach ( $posts as $post ) {
$values[] = $wpdb->prepare( "(%d, %d, %d)", $post, $id, $term_order);
clean_post_cache($post);
}
if ( $values ) {
$wpdb->query("INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id, term_order) VALUES " . join(',', $values) . " ON DUPLICATE KEY UPDATE term_order = VALUES(term_order)");
$wpdb->update($wpdb->term_taxonomy, array('count' => $category->count), array('term_id' => $category->term_id, 'taxonomy' => 'post_tag') );
}
echo __('Converted successfully.') . "</li>\n";
continue;
}
// if tag already exists, add it to all posts in the category
if ( $tag_ttid = $wpdb->get_var( $wpdb->prepare("SELECT term_taxonomy_id FROM $wpdb->term_taxonomy WHERE term_id = %d AND taxonomy = 'post_tag'", $category->term_id) ) ) {
$objects_ids = get_objects_in_term($category->term_id, 'category');
$tag_ttid = (int) $tag_ttid;
$term_order = 0;
foreach ( $objects_ids as $object_id )
$values[] = $wpdb->prepare( "(%d, %d, %d)", $object_id, $tag_ttid, $term_order);
if ( $values ) {
$wpdb->query("INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id, term_order) VALUES " . join(',', $values) . " ON DUPLICATE KEY UPDATE term_order = VALUES(term_order)");
$count = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $tag_ttid) );
$wpdb->update($wpdb->term_taxonomy, array('count' => $count), array('term_id' => $category->term_id, 'taxonomy' => 'post_tag') );
}
echo __('Tag added to all posts in this category.') . " *</li>\n";
$hybrid_cats = true;
$clean_term_cache[] = $category->term_id;
$clean_cat_cache[] = $category->term_id;
continue;
}
$tt_ids = $wpdb->get_col( $wpdb->prepare("SELECT term_taxonomy_id FROM $wpdb->term_taxonomy WHERE term_id = %d AND taxonomy = 'category'", $category->term_id) );
if ( $tt_ids ) {
$posts = $wpdb->get_col("SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id IN (" . join(',', $tt_ids) . ") GROUP BY object_id");
foreach ( (array) $posts as $post )
clean_post_cache($post);
}
// Change the category to a tag.
$wpdb->update($wpdb->term_taxonomy, array('taxonomy' => 'post_tag'), array('term_id' => $category->term_id, 'taxonomy' => 'category') );
// Set all parents to 0 (root-level) if their parent was the converted tag
$wpdb->update($wpdb->term_taxonomy, array('parent' => 0), array('parent' => $category->term_id, 'taxonomy' => 'category') );
if ( $parents ) $clear_parents = true;
$clean_cat_cache[] = $category->term_id;
echo __('Converted successfully.') . "</li>\n";
}
}
echo '</ul>';
if ( ! empty($clean_term_cache) ) {
$clean_term_cache = array_unique(array_values($clean_term_cache));
clean_term_cache($clean_term_cache, 'post_tag');
}
if ( ! empty($clean_cat_cache) ) {
$clean_cat_cache = array_unique(array_values($clean_cat_cache));
clean_term_cache($clean_cat_cache, 'category');
}
if ( $clear_parents ) delete_option('category_children');
if ( $hybrid_cats )
echo '<p>' . sprintf( __('* This category is also a tag. The converter has added that tag to all posts currently in the category. If you want to remove it, please confirm that all tags were added successfully, then delete it from the <a href="%s">Manage Categories</a> page.'), 'categories.php') . '</p>';
echo '<p>' . sprintf( __('We&#8217;re all done here, but you can always <a href="%s">convert more</a>.'), 'admin.php?import=wp-cat2tag' ) . '</p>';
}
function convert_tags() {
global $wpdb;
if ( (!isset($_POST['tags_to_convert']) || !is_array($_POST['tags_to_convert'])) && empty($this->tags_to_convert)) {
echo '<div class="narrow">';
echo '<p>' . sprintf(__('Uh, oh. Something didn&#8217;t work. Please <a href="%s">try again</a>.'), 'admin.php?import=wp-cat2tag&amp;step=3') . '</p>';
echo '</div>';
return;
}
if ( empty($this->tags_to_convert) )
$this->tags_to_convert = $_POST['tags_to_convert'];
$hybrid_tags = $clear_parents = false;
$clean_cat_cache = $clean_term_cache = array();
$default_cat = get_option('default_category');
echo '<ul>';
foreach ( (array) $this->tags_to_convert as $tag_id) {
$tag_id = (int) $tag_id;
if ( $tag = get_term( $tag_id, 'post_tag' ) ) {
printf('<li>' . __('Converting tag <strong>%s</strong> ... '), $tag->name);
if ( $cat_ttid = $wpdb->get_var( $wpdb->prepare("SELECT term_taxonomy_id FROM $wpdb->term_taxonomy WHERE term_id = %d AND taxonomy = 'category'", $tag->term_id) ) ) {
$objects_ids = get_objects_in_term($tag->term_id, 'post_tag');
$cat_ttid = (int) $cat_ttid;
$term_order = 0;
foreach ( $objects_ids as $object_id ) {
$values[] = $wpdb->prepare( "(%d, %d, %d)", $object_id, $cat_ttid, $term_order);
clean_post_cache($object_id);
}
if ( $values ) {
$wpdb->query("INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id, term_order) VALUES " . join(',', $values) . " ON DUPLICATE KEY UPDATE term_order = VALUES(term_order)");
if ( $default_cat != $tag->term_id ) {
$count = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $tag->term_id) );
$wpdb->update($wpdb->term_taxonomy, array('count' => $count), array('term_id' => $tag->term_id, 'taxonomy' => 'category') );
}
}
$hybrid_tags = true;
$clean_term_cache[] = $tag->term_id;
$clean_cat_cache[] = $tag->term_id;
echo __('All posts were added to the category with the same name.') . " *</li>\n";
continue;
}
// Change the tag to a category.
$parent = $wpdb->get_var( $wpdb->prepare("SELECT parent FROM $wpdb->term_taxonomy WHERE term_id = %d AND taxonomy = 'post_tag'", $tag->term_id) );
if ( 0 == $parent || (0 < (int) $parent && $this->_category_exists($parent)) ) {
$new_fields = array('taxonomy' => 'category');
$clear_parents = true;
} else {
$new_fields = array('taxonomy' => 'category', 'parent' => 0);
}
$wpdb->update($wpdb->term_taxonomy, $new_fields, array('term_id' => $tag->term_id, 'taxonomy' => 'post_tag') );
$clean_term_cache[] = $tag->term_id;
$clean_cat_cache[] = $cat['term_id'];
echo __('Converted successfully.') . "</li>\n";
} else {
printf( '<li>' . __('Tag #%s doesn&#8217;t exist!') . "</li>\n", $tag_id );
}
}
if ( ! empty($clean_term_cache) ) {
$clean_term_cache = array_unique(array_values($clean_term_cache));
clean_term_cache($clean_term_cache, 'post_tag');
}
if ( ! empty($clean_cat_cache) ) {
$clean_cat_cache = array_unique(array_values($clean_cat_cache));
clean_term_cache($clean_term_cache, 'category');
}
if ( $clear_parents ) delete_option('category_children');
echo '</ul>';
if ( $hybrid_tags )
echo '<p>' . sprintf( __('* This tag is also a category. The converter has added all posts from it to the category. If you want to remove it, please confirm that all posts were added successfully, then delete it from the <a href="%s">Manage Tags</a> page.'), 'edit-tags.php') . '</p>';
echo '<p>' . sprintf( __('We&#8217;re all done here, but you can always <a href="%s">convert more</a>.'), 'admin.php?import=wp-cat2tag&amp;step=3' ) . '</p>';
}
function init() {
$step = (isset($_GET['step'])) ? (int) $_GET['step'] : 1;
$this->header();
if ( current_user_can('manage_categories') ) {
switch ($step) {
case 1 :
$this->categories_tab();
break;
case 2 :
check_admin_referer('import-cat2tag');
$this->convert_categories();
break;
case 3 :
$this->tags_tab();
break;
case 4 :
check_admin_referer('import-cat2tag');
$this->convert_tags();
break;
}
}
$this->footer();
}
function WP_Categories_to_Tags() {
// Do nothing.
}
}
$wp_cat2tag_importer = new WP_Categories_to_Tags();
register_importer('wp-cat2tag', __('Categories and Tags Converter'), __('Convert existing categories to tags or tags to categories, selectively.'), array(&$wp_cat2tag_importer, 'init'));
?>