MyBB Community Forums

Full Version: Rebuild and Recount
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello, I've been developing a small script to work with my boards and have come across a small issue. Basically my script grabs items from multiple rss feeds and posts them to my forums. My script successfully creates the thread, then the post, and everything is formatted and looks great. The only issue I am having is with the post count/thread count of the forum I'm posting to. What I want to know is the easiest way to rebuild the stats without going to the admincp and recounting manually. As you can imagine taking items from an rss feed means I'll have threads/posts added on an hourly basis... sometimes 50-150 at a time. I would like the stats to update as each thread/post is added but at the same time I don't want to strain the board/server. I know there must be a way to do it since after you post something through mybb it automatically updates the stats. anyone have any ideas?
How are you posting it? You should use the post class (inc/datahandlers/post.php). Look through newreply or newthread.php to see how it works and it should do it automatically. If you really need to recount it take a look at admin/modules/tools/recount_rebuild.php
You should use the PostDataHandler rather than inserting it in the database.

$postHandler = new postDataHandler();
if($postHandler->validate_post($post))
{
	$postHandler->insert_post($post);
}

Look at the code in newthread.php or newpost.php to see what that $post array looks like.
Thanks guys! I'd rather do it through mybb then straight to the database anyways... just didnt know where to start. I'll have a look around and let you know if I can get something working. Thanks again!
Alright. I've looked around a bit and still don't really know whats going on. But I'm still searching through the code trying to figure out whats what. Right now my script doesn't have to be logged into the forums in order to post the feed items... if I was to use the post handlers I would have to rewrite everything so it logs into the forums wouldn't I?
(2011-03-25, 01:41 PM)Onloac Wrote: [ -> ]if I was to use the post handlers I would have to rewrite everything so it logs into the forums wouldn't I?

Not necessarily. You can use the post handler outside of MyBB by including global.php into your project. I created a little example for you:
<?php

define('IN_MYBB', true);

define('MYBB_ROOT', realpath("1602")."/");
include "./1602/global.php"; // needs to be a relative path to the global.php file

// include the post handler
require_once MYBB_ROOT."inc/datahandlers/post.php";

// our new thread in an array
$uid = 1;
$username = "Admin";
$thread = array(
	"fid" => 12,
	"prefix" => 0,
	"subject" => "Our new thread",
	"icon" => 0,
	"uid" => $uid,
	"username" => $username,
	"dateline" => TIME_NOW,
	"message" => "Our new message",
	"ipaddress" => get_ip(),
	"posthash" => md5($uid.random_str())
);

// create the PostDataHandler
$posthandler = new PostDataHandler("insert");
$posthandler->action = "thread";
$posthandler->set_data($thread);

// validate the thread
if($posthandler->validate_thread())
{
    // insert it into the database
    $posthandler->insert_thread();
}
else
{
	print_r($posthandler->get_friendly_errors());
}

The above will insert a thread into forum with fid 12 and with user id 1 (usually the Admin) but you can create a thread with any user. Of course you need to change the paths of the includes to make it work on your site. All the counters that need to be updated will be updated.
Thanks... I think that might be just what I need! I'll play around with it and let you know!
@Aries-Belgium: You are the man! With that bit of code I was able to do exactly what I wanted!!! Thanks!!! One more quick question! I use addslashes cause the array won't accept quotes, how can I escape the quotes without having the slashes appear within the post?
You can just put raw data into the array for the datahandler, the datahandler sorts out the escaping for you.
Awesome! I swear MyBB never stops amazing me! Thanks for all your help guys everything works PERFECT now!