我需要创建一个任务,执行与 MyBB 中的 recount_rebuild.php 文件相同的功能,以每 1 分钟自动重新计数论坛和线程。这是我在MyBB中用作任务的代码,但执行似乎不起作用,主页上呈现的值是相同的:
<?php
require_once 'global.php';
require_once MYBB_ROOT.'inc/functions_rebuild.php';
require_once MYBB_ROOT.'inc/functions.php';
function task_recount_rebuild_forum_counters($task)
{
global $db, $mybb, $lang;
$query = $db->simple_select("forums", "fid", '', array('order_by' => 'fid', 'order_dir' => 'asc'));
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']);
}
}
function task_recount_rebuild_thread_counters($task)
{
global $db, $mybb, $lang;
$query = $db->simple_select("threads", "tid", '', array('order_by' => 'tid', 'order_dir' => 'asc'));
while($thread = $db->fetch_array($query))
{
rebuild_thread_counters($thread['tid']);
}
}
我设法用以下代码解决了这个问题
<?php
function task_rebuildforumcounters($task)
{
try
{
global $db, $mybb, $lang;
require_once MYBB_ROOT."/inc/functions.php";
require_once MYBB_ROOT."/inc/functions_rebuild.php";
require_once MYBB_ROOT.$mybb->config['admin_dir']."/inc/functions.php";
$query = $db->simple_select("forums", "fid", '', array('order_by' => 'fid', 'order_dir' => 'asc'));
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']);
}
add_task_log($task, "Recontagem de foruns realizada com sucesso");
} catch (Exception $e) {
add_task_log($task, "Erro ao realizar recontagem de foruns");
}
}