How to get total number of posts, threads and members?

nrep

Well-known member
#1
I'm trying to fetch the total number of posts, threads and members outside of XF. I've got a script that loads the XF autoloaders, I've got DB access:

PHP:
    require($fileDir . '/library/XenForo/Autoloader.php');
    XenForo_Autoloader::getInstance()->setupAutoloader($fileDir . '/library');

    XenForo_Application::initialize($fileDir . '/library', $fileDir);

    $db = XenForo_Application::getDb();
I was originally fetching these stats like this, however it is an expensive DB query when running across lots of sites concurrently:

PHP:
        $memberCount = $db->fetchRow("SELECT COUNT(*) FROM xf_user WHERE is_banned = 0 AND user_state = 'valid';");
        $memberCount = $memberCount['COUNT(*)'];
I see that there is serialized data (boardTotals) in xf_data_registry. However, I can't figure out how to easily access these figures.

Is there a way I can get the total number of posts, threads and members naively - or a MySQL query that will unserialize for me?
 

wang

Well-known member
#3
Can you get the total number of those stats by using this code:

PHP:
$boardTotals = XenForo_Model::create('XenForo_Model_DataRegistry')->get('boardTotals');

if (!$boardTotals)
{
       $boardTotals =XenForo_Model::create('XenForo_Model_Counters')>rebuildBoardTotalsCounter();
}

echo 'Discussions: ' . XenForo_Locale::numberFormat( $boardTotals['discussions'] ) . '<br/>';
echo 'Messages: ' . XenForo_Locale::numberFormat( $boardTotals['messages'] ) . '<br />';
echo 'Members: ' . XenForo_Locale::numberFormat( $boardTotals['users'] ) . '<br />';
Or you can try to get the individual stats with a different approach. E.g, to get the total number of members.

PHP:
$userModel = XenForo_Model::create('XenForo_Model_User');
$memberCount = $userModel->countTotalUsers();

echo '<strong>Members</strong>: ' . XenForo_Locale::numberFormat( $memberCount) . '<br />';
 
Last edited:

nrep

Well-known member
#4
Anything that speaks against fetching them serialized and just use php unserialize?
I didn't know if that was a good way of doing things, as I don't know how the data works. I'm quite a novice coder.

Can you get the total number of those stats by using this code:

PHP:
$boardTotals = XenForo_Model::create('XenForo_Model_DataRegistry')->get('boardTotals');

if (!$boardTotals)
{
       $boardTotals =XenForo_Model::create('XenForo_Model_Counters')>rebuildBoardTotalsCounter();
}

echo 'Discussions: ' . XenForo_Locale::numberFormat( $boardTotals['discussions'] ) . '<br/>';
echo 'Messages: ' . XenForo_Locale::numberFormat( $boardTotals['messages'] ) . '<br />';
echo 'Members: ' . XenForo_Locale::numberFormat( $boardTotals['users'] ) . '<br />';
That works perfectly, thank you! I really appreciated it.

Instead of my script taking almost a minute per site, it completes near instantly. :D
 
Top