1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

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

Discussion in 'XenForo Development Discussions' started by nrep, Feb 1, 2016.

  1. nrep

    nrep Well-Known Member

    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?
     
  2. katsulynx

    katsulynx Well-Known Member

    Anything that speaks against fetching them serialized and just use php unserialize?
     
  3. wang

    wang Well-Known Member

    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: Feb 1, 2016
    nrep likes this.
  4. nrep

    nrep Well-Known Member

    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.

    That works perfectly, thank you! I really appreciated it.

    Instead of my script taking almost a minute per site, it completes near instantly. :D
     
    wang likes this.
  5. wang

    wang Well-Known Member

    You are most welcome sir.
     

Share This Page