Skip to content

Instantly share code, notes, and snippets.

@ntwb
Created March 17, 2015 23:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ntwb/9ff3f1ed93ae9671a93b to your computer and use it in GitHub Desktop.
Save ntwb/9ff3f1ed93ae9671a93b to your computer and use it in GitHub Desktop.
SMF to bbPress attachments import script (GD Attachments) via Mike Russell https://bbpress.org/forums/topic/smf-import-to-bbpress/page/5/#post-159797
<?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;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment