phptaskscheduled-tasksmybb

Create a task in mybb to automatically recount and rebuild forums and threads every 1 minute


I need to create a task that performs the same function as the recount_rebuild.php file in MyBB to automatically recount the forums and threads every 1 minute. This is the code I use as a task in MyBB, but the execution does not seem to have worked, the values ​​presented on the home page are the same:

    <?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']);
    }
}

Solution

  • I managed to solve the problem with the following code

        <?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");
        }
    }