MyBB Community Forums

Full Version: How can I set the "rebuild forum counters" option to run every hour as task?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
How can I set the "rebuild forum counters" option to run every hour as task?

The reason I need to do this is because when a new thread is created by the "form manager" plugin, that thread isn't displayed in the Last Post section of the forum index.  When I go to Recount & Rebuild and I run the "Rebuild Forum Counters" option, it fixes the issue. I'm not sure how to have this happen automatically as a task, though...
Does anybody here know if this is possible to do or not?
Is there a way to add ANY new task that isn't already available in the dropdown menu of task files when you go to the add new task? I'm not sure why this is such a hard question to answer...
(2015-12-14, 08:03 AM)Der Meister Wrote: [ -> ]Is there a way to add ANY new task that isn't already available in the dropdown menu of task files when you go to the add new task?

Yes - the task files are located in inc/tasks/ and you can create new ones basing on these already present.
Here's the function for synchronizing the forums cache: https://github.com/mybb/mybb/blob/featur....php#L2589

That being said, I would recommend fixing the plugin causing the problem - including the file above and running the marked function in the correct place (after content is inserted) should do the trick.
I believe I followed those steps, and when I run the created task, this is the error:

Fatal error: Cannot redeclare update_forum_lastpost() (previously declared in /home/xzxcom/public_html/inc/functions.php:2589) in /home/xzxcom/public_html/inc/tasks/lastpostercountfix.php on line 26

How do I fix this?

Here is the php file I created in inc/tasks:

<?php
function update_forum_lastpost($fid)
{
global $db;
// Fetch the last post for this forum
$query = $db->query("
SELECT tid, lastpost, lastposter, lastposteruid, subject
FROM ".TABLE_PREFIX."threads
WHERE fid='{$fid}' AND visible='1' AND closed NOT LIKE 'moved|%'
ORDER BY lastpost DESC
LIMIT 0, 1
");
$lastpost = $db->fetch_array($query);
$updated_forum = array(
"lastpost" => (int)$lastpost['lastpost'],
"lastposter" => $db->escape_string($lastpost['lastposter']),
"lastposteruid" => (int)$lastpost['lastposteruid'],
"lastposttid" => (int)$lastpost['tid'],
"lastpostsubject" => $db->escape_string($lastpost['subject'])
);
$db->update_query("forums", $updated_forum, "fid='{$fid}'");
}


?>
From what I gather, I'm supposed to include the require_once line in here to fix the error? Any variation I try of that, doesn't work. Can somebody please take my example php code above and write in the line that will fix this error, so I know exactly how it's supposed to be written and where? No matter what variation of require_once I try, it still gives the "cannot redeclare" error. Or if there's something else wrong or missing in my php file above, can you tell me exactly what it is?
Sorry if I go back this topic and for my English 'google' ...

I think for the task, it's simple, I try the task it works (then it will remain to do the plugin, which will be more practical, examples of lacking).

<?php


/**
 * Rebuild forum counters
 */
function task_acprebuild($task)
{
function acp_rebuild_forum_counters()
{
	global $db, $mybb, $lang;

	$query = $db->simple_select("forums", "COUNT(*) as num_forums");
	$num_forums = $db->fetch_field($query, 'num_forums');

	$page = $mybb->get_input('page', MyBB::INPUT_INT);
	$per_page = $mybb->get_input('forumcounters', MyBB::INPUT_INT);
	if($per_page <= 0)
	{
		$per_page = 50;
	}
	$start = ($page-1) * $per_page;
	$end = $start + $per_page;

	$query = $db->simple_select("forums", "fid", '', array('order_by' => 'fid', 'order_dir' => 'asc', 'limit_start' => $start, 'limit' => $per_page));
	while($forum = $db->fetch_array($query))
	{
		$update['parentlist'] = make_parent_list($forum['fid']);
		$db->update_query("forums", $update, "fid='{$forum['fid']}'");
		rebuild_forum_counters($forum['fid']);
	}

	//check_proceed($num_forums, $end, ++$page, $per_page, "forumcounters", "do_rebuildforumcounters", $lang->success_rebuilt_forum_counters);
}

/**
 * Rebuild thread counters
 */
function acp_rebuild_thread_counters()
{
	global $db, $mybb, $lang;

	$query = $db->simple_select("threads", "COUNT(*) as num_threads");
	$num_threads = $db->fetch_field($query, 'num_threads');

	$page = $mybb->get_input('page', MyBB::INPUT_INT);
	$per_page = $mybb->get_input('threadcounters', MyBB::INPUT_INT);
	if($per_page <= 0)
	{
		$per_page = 500;
	}
	$start = ($page-1) * $per_page;
	$end = $start + $per_page;

	$query = $db->simple_select("threads", "tid", '', array('order_by' => 'tid', 'order_dir' => 'asc', 'limit_start' => $start, 'limit' => $per_page));
	while($thread = $db->fetch_array($query))
	{
		rebuild_thread_counters($thread['tid']);
	}

	//check_proceed($num_threads, $end, ++$page, $per_page, "threadcounters", "do_rebuildthreadcounters", $lang->success_rebuilt_thread_counters);
}
	    add_task_log($task, "success_rebuilt_thread_counters and success_rebuilt_forum_counters." );
}
?>

Thanks in advance for correcting me if this is not good to do!

Best regards,