curvemeister (@curvemeister)

Forum Replies Created

Viewing 5 replies - 1 through 5 (of 5 total)
  • In reply to: SMF Import to bbPress

    curvemeister
    Participant

    @curvemeister

    Hi Stephen,

    I’ve made some slight improvements to the attachment importer script – pls check with me when the time comes, and I’ll send you the latest version. I have looked around for how to get write access to the source repository, and still don’t have a clue.

    Looking over the SMF->bbPress import template, I noticed that avatars are being imported from the wrong place – they actually live in the attachments table, not the avatar table, with the associated ID of the user.

    All the best,

    Mike

    In reply to: SMF Import to bbPress

    curvemeister
    Participant

    @curvemeister

    Another script – this one cleans up the messy duplicate attachment references to the same media entry that were left behind, should you happen to re-run the older version of the script. Hopefully
    you will not need this, but I certainly did after developing the script and running it dozens of times! – Mike
    ===================================================================================
    //php_removedupes.php – clean up after running older version of script multiple times.
    //Mike Russell
    //
    <?php
    require( ‘wp-load.php’ );
    require( ‘wp-admin/includes/image.php’ );
    global $wpdb;

    echo “version 1.0\n”;

    $chunk = 100; //posts at a timea

    //utility functions

    function dumpguids($children)
    {
    foreach($children as $child)
    echo “$child->ID $child->guid\n”; flush();
    }

    //remove the %20’s from the supplied guid
    function fixguid($guid)
    {
    return str_replace(“%20”, “”, $guid);
    }

    for($kerchunk = 0; true; $kerchunk++) {

    //Get the next group of posts.
    $args = array( ‘offset’=> $kerchunk * $chunk,
    ‘numberposts’ => $chunk,
    ‘post_type’ => array(‘topic’, ‘reply’)
    );
    $posts = get_posts($args);
    if(empty($posts))
    break;

    foreach($posts as $post) {
    //echo print_r($post); flush();

    //get all attachments for this post.
    $children = get_children( array (
    ‘post_parent’ =>$post->ID,
    ‘post_type’ => ‘attachment’,
    ‘numberposts’ => -1
    ) );
    if(count($children) < 2)
    continue;
    //echo count($children).” attachments for $post->post_title\n”; flush();

    //sort the guid’s, ignoring %20 added by the SMF attachment script
    usort($children, function($a, $b) {
    return strcasecmp(str_replace(“%20”, “”, $a->guid), str_replace(“%20”, “”, $b->guid));
    });
    //reset the parent field of all the extra attachments with matching guid’s, keeping the last one.
    //this leaves orphaned meta rows, which can be cleaned up later by standard WordPress
    $deleted = 0;
    $unique = 0;
    $n = count($children);
    for($i=0; $i<($n-1); $i++) {
    if(fixguid($children[$i]->guid) == fixguid($children[$i+1]->guid)) {
    //echo “delete dupe “.$children[$i]->post_title.” from parent “.$children[$i]->post_parent.”\n”; flush();
    $wpdb->delete($wpdb->posts, array(‘ID’ => $children[$i]->ID));
    $deleted++;
    } else {
    $unique++;
    }
    }
    }
    echo $kerchunk * $chunk.” posts scanned\r”; flush();
    }
    echo “done\n”;
    ?>

    In reply to: SMF Import to bbPress

    curvemeister
    Participant

    @curvemeister

    Hi again,

    Here is an improved version of the SMF to bbPress script. This version supports additional, non-image filetypes, and has more safeguards against inserting attachments multiple times if the script is interrupted and restarted. Since I am not yet savvy in the ways of submitting source code to the forum, I am cutting and pasting, which seems to butcher the syntax horribly…

    Many thanks, again, to Stephen.

    ================================================================
    <?php
    //Standalone script to import smf attachments for the GD bbPress Attachments plugin
    //Mike Russell, April 4, 2015
    //Execute after you have imported smf into bbPress.
    //Run this from the command line, not the web (it will timeout)

    require( ‘wp-load.php’ );
    require( ‘wp-admin/includes/image.php’ );
    global $wpdb;

    $verbose = false;
    $force_attachment_update = false; //normally false. set true to re-write existing attachments
    //$limit = ” LIMIT 0,1″;
    $limit = ” LIMIT 0,99999″;

    // source database connection
    $host=”localhost”;
    $uname=”xxx”;
    $pass=”xxx”;
    $database = “xxx”;
    $site_url = ‘http://www.xxx.com/forum&#8217;;
    $forum_path = ‘forum/attachments/’;

    echo “start smf_attachments-to-bbpress v1.2\n”; flush();

    //get the attachment rows from SMF
    $smf_db = new wpdb($uname, $pass, $database, $host);
    $smf_rows = $smf_db->get_results(”
    SELECT * FROM smf_attachments
    WHERE file_hash != ”
    AND filename NOT LIKE ‘%thumb’
    ORDER BY id_attach”
    .$limit);

    echo “processing “.$smf_db->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 to copy. 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 “.$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 from SMF to the upload dir 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 a reference to the bbPress-imported parent post 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 || $parent_query->post_count > 1) {
    echo $parent_query->post_count.” posts found for smf_post id “.$smf_attachment_row->id_msg.”\n”; flush();
    }

    //add the attachment if there is exactly one parent referencing it
    if($parent_query->have_posts() && $parent_query->post_count == 1) {
    $guid = $uploads[‘url’] . ‘/’ . basename( $new_full_filename );
    $parent_query->the_post();
    $post_id = get_the_ID();

    //if force option is not specified, skip updating an already existing attachment
    if(!$force_attachment_update) {
    $q = $wpdb->prepare(

    SELECT COUNT(*)
    FROM $wpdb->posts
    WHERE guid = %s AND post_parent = %d
    “,
    $guid,
    $post_id);

    $nrefs = $wpdb->get_var($q);
    if($nrefs > 0) {
    if($verbose) { echo “skip existing attachment: “.$guid.”\n”; flush(); }
    wp_reset_postdata(); //reset post info
    goto done;
    }
    }

    $attachment_data = array(
    ‘guid’ => $guid,
    ‘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();
    }
    }
    done:
    $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;
    ?>

    In reply to: SMF Import to bbPress

    curvemeister
    Participant

    @curvemeister

    Beautiful – thanks. Hopefully I can contribute more in the future.

    In reply to: SMF Import to bbPress

    curvemeister
    Participant

    @curvemeister

    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;
    ?>
    
Viewing 5 replies - 1 through 5 (of 5 total)