I want to offer this php script in exchange for the help Stephen’s script provided. It ran successfully on my installation, going from SMF 2.0.4 to bbPress 2.5.6 / GD bbPress Attachments 2.2. Most of my time is taken up with consulting, and I regret that I will not be providing much in the way of support for this script.
This is a stand-alone php script that you must modify to target your SMF install. I suggest using notepad++ to edit for readability, and something like FileZilla to transfer your file to your host. Install and run the script in the root folder of your WordPress folder. I used Putty configured for an SSHconnection. Do not try to run it via http – the script will time out.
Thanks to Stephen Edgar for providing the original php on which this is based.
Mike Russell – final result at: http://www.curvemeister.com/forums
==============================================================================
<?php
//Standalone script to import smf attachments for the GD bbPress Attachments plugin
//Execute after you have imported smf into bbPress
require( 'wp-load.php' );
require( 'wp-admin/includes/image.php' );
$verbose = false;
//$limit = " LIMIT 0,1";
$limit = " LIMIT 0,99999";
// source database connection
$host="localhost";
$uname="xxx";
$pass="xxx";
$database = "xxx";
$site_url = 'http://xxx/forum';
$forum_path = 'forum/attachments/';
echo "start smf_attachments-to-bbpress\n"; flush();
//get the attachment rows from SMF
$smf_db = new wpdb($uname, $pass, $database, $host);
$smf_rows = $smf_db->get_results("
SELECT * FROM <code>smf_attachments</code>
WHERE file_hash != ''
AND filename NOT LIKE '%thumb'
ORDER BY <code>id_attach</code>"
.$limit);
echo "processing ".$smf_rows->num_rows." rows total\n";
// process each row
$count = 0;
foreach ($smf_rows as $smf_attachment_row) {
if($verbose) { echo 'next row, id_attach = '.$smf_attachment_row->id_attach."\n"; flush(); }
//look for both a new and old style filename. If neither exists, skip this attachment
$smf_filename = $forum_path.$smf_attachment_row->id_attach.'_'.$smf_attachment_row->file_hash;
if(!file_exists($smf_filename))
$smf_filename = $forum_path.$smf_attachment_row->id_attach.'_'.$smf_attachment_row->filename.$smf_attachment_row->file_hash;
if(!file_exists($smf_filename))
{
echo "no file, skipping attachment for ".$smf_attachment_row->id_attach.", missing SMF file: ".$smf_filename."\n"; flush();
continue;
}
$uploads = wp_upload_dir('SMF');
$new_upload_dir = $uploads['path'];
$new_full_filename = $new_upload_dir.'/'.$smf_attachment_row->filename;
if($verbose) { echo('old->new = '.$smf_filename.' -> '.$new_full_filename."\n"); flush(); }
//copy the enclosed file if necessary
if(!file_exists($new_full_filename) && !copy($smf_filename, $new_full_filename) ) {
echo "cannot copy: ".$smf_filename."->".$new_full_filename."\n";
} else {
//look for bbPress's previously imported topic or reply for the current attachment
$parent_args = array(
'post_type' => array('topic', 'reply'),
'meta_key' => '_bbp_post_id',
'meta_value' => $smf_attachment_row->id_msg
);
//echo "$parent_args = ".print_r($parent_args)."\n";
$parent_query = new WP_Query($parent_args);
$parent_query->get_posts();
if($verbose) { echo $parent_query->post_count." posts found for smf_post id ".$smf_attachment_row->id_msg."\n"; flush(); }
//normally only one post references a given enclosure, but handle possible multiples anyway ...
while($parent_query->have_posts()) {
$parent_query->the_post();
$post_id = get_the_ID();
$attachment_data = array(
'guid' => $uploads['url'] . '/' . basename( $new_full_filename ),
'post_mime_type' => 'image/'.$smf_attachment_row->fileext,
'post_title' => $smf_attachment_row->filename,
'post_status' => null,
'post_content' => '',
);
//if($verbose) { echo "attachment_data = ".print_r($attachment_data)."\n"; flush(); }
$attach_id = wp_insert_attachment($attachment_data, $new_full_filename, $post_id);
//echo "attach_id = ".$attach_id."\n"; flush();
if($attach_id) {
//update_post_meta($attach_id, '_bbp_attachment', 1);
if($attach_metadata = wp_generate_attachment_metadata($attach_id, $new_full_filename)) {
//echo 'attach_metadata = '.print_r($attach_metadata)."\n"; flush();
wp_update_attachment_metadata( $attach_id, $attach_metadata );
set_post_thumbnail( $post_id, $attach_id );
} else {
echo 'wp_generate_attachment_metadata failed, fname = '.$new_full_filename."\n"; flush();
}
}
wp_reset_postdata();
}
}
$count++;
if($count%100 == 0)
{
echo $count." attachments processed\r";
flush();
}
}
echo "Done, processed ".$count." records\n\n";
//clean up message body text
//convert <tt> -> <br />
mysql_query("UPDATE wp_posts SET post_content = REPLACE (post_content, \'<tt>\', \'<br />\') WHERE post_content LIKE \'%<tt>%\'");
mysql_close($connection);
exit;
?>