Few elementary questions

janslu

Active member
I have just migrated to xenforo from vbulletin and now I need some assistance with integrating it with my other pages. I've searched for solutions but all my tests don't work and most information is in regard to plugins rather than external "bridges".

1. I need to make a super simple counter of users and guests on the forum to display on external pages (and use it for munin graphing). I've found a piece of code that is given as a solution, but I miss an elementary piece of information on how to use it:

Code:
    protected function _getSessionActivityList()
    {
        $visitor = XenForo_Visitor::getInstance();

        $sessionModel = $this->getModelFromCache('XenForo_Model_Session');

        return $sessionModel->getSessionActivityQuickList(
            $visitor->toArray(),
            array('cutOff' => array('>', $sessionModel->getOnlineStatusTimeout())),
            ($visitor['user_id'] ? $visitor->toArray() : null)
        );

    }

If I put it inside my own class like this:
Code:
class bridgeXen
{
    public function __construct() {
        $startTime = microtime(true);
        $fileDir = XF_ROOT;

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

        XenForo_Application::initialize($fileDir . '/library', $fileDir);
        XenForo_Application::set('page_start_time', $startTime);
        XenForo_Session::startPublicSession();
    }

   function onlineUsers()
    {
        $visitor = XenForo_Visitor::getInstance();

        $sessionModel = $this->getModelFromCache('XenForo_Model_Session');

        return $sessionModel->getSessionActivityQuickList(
            $visitor->toArray(),
            array('cutOff' => array('>', $sessionModel->getOnlineStatusTimeout())),
            ($visitor['user_id'] ? $visitor->toArray() : null)
        );
    }
}
of course it doesn't have a clue on what "$this" is. And I have no idea how to make it to work.

2. I need to have a function for my own viewhelpers in a CMS to convert forumIds to URLs. I was trying to use the following code:
Code:
    require($fileDir . '/library/XenForo/Autoloader.php');
    XenForo_Autoloader::getInstance()->setupAutoloader($fileDir . '/library');
    XenForo_Application::initialize($fileDir . '/library', $fileDir);

        // Not required if you are not using any of the preloaded data
        $dependencies = new XenForo_Dependencies_Public();
        $dependencies->preLoadData();

        XenForo_Link::useFriendlyUrls(true);
        XenForo_Link::romanizeTitles(true); 
       
        switch ($numerUid) {
            # ciąża
            case 1267:  
            ourtput = '<li><a href="'.$this->urlXenforoForum(1).'" title="Forum id = 1">Fora id = 1</a></li>';
But I've got an error about logging problems
Code:
PHP Fatal error:  Uncaught exception 'ErrorException' with message 'Undefined index: /var/www/hosts/www.babyboom.pl/typo3temp/logs/typo3.log' in /var/www/typo3_src-6.2.16/typo3/sysext/core/Classes/Log/Writer/FileWriter.php:170

Stack trace:
#0 /var/www/typo3_src-6.2.16/typo3/sysext/core/Classes/Log/Writer/FileWriter.php(170): XenForo_Application::handlePhpError(8, 'Undefined index...', '/var/www/typo3_...', 170, Array)
#1 /var/www/typo3_src-6.2.16/typo3/sysext/core/Classes/Log/Writer/FileWriter.php(74): TYPO3\CMS\Core\Log\Writer\FileWriter->closeLogFile()
#2 [internal function]: TYPO3\CMS\Core\Log\Writer\FileWriter->__destruct()
#3 {main}
  thrown in /var/www/typo3_src-6.2.16/typo3/sysext/core/Classes/Log/Writer/FileWriter.php on line 170
How do I solve such problem? What's at fault here? Is this a problem with configuration, or do I need to do something else, like use namespaces to separate classes?

Thank you
 
Code:
$sessionModel = $this->getModelFromCache('XenForo_Model_Session');

Use $sessionModel = XenForo_Model::create('XenForo_Model_Session').

I don't understand the context of your 2nd problem - I don't understand your file layout either unless I'm missing something. You've snipped bits off not allowing us to see the entire thing.
 
Thank you! I have created a very nice munin monitoring plugin that I will be happy to share once I make sure everything works nice.

As for the question #2... I wish I could be more precise... My problem is that I think that xenforo switches error logging in php at least to notices level. My error log explodes with php notices as soon as I launch the Xenforo App through autoloader in the context of my CMS and what's worse - those notices are being displayed on the screen, even though I have disabled error display in php settings and I log all errors to a file. Those are not Errors or even Warnings - those are Notices. The CMS uses it's own namespaces and I am wondering whether namespaces may be a way to somehow separate those two system - so that the changes to logging done in Xenforo wouldn't catch all undefined indexes from the CMS... What I am going to do with the Xenforo classes and functions is primarily to build proper links to specific forums or threads, and to get last posts or threads in specific categories. Functions themselves seem to work fine. So I think my real question is -
- how can I suppress error messages being displayed after integrating Xenforo classes?
- how should I bridge Xenforo and another complicated PHP system, if the other system uses classes and namespaces and I just need to call few Xenforo functions to for example get a proper forum URL for a given forum_id.

Let's say I have an extension controller
Code:
class Tx_Jsforumnews_Controller_JsForumNewsController extends Tx_Extbase_MVC_Controller_ActionController {
  private function helper($data){
   ....
  }
   public function calloutAction() {

   }
}
And I'm currently working on the following class:
Code:
class XenForoHelperClass {
    public function __construct() {
        $startTime = microtime(true);
        $fileDir = '/var/www/......';

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

        XenForo_Application::initialize($fileDir . '/library', $fileDir);
        XenForo_Application::set('page_start_time', time());
        XenForo_Session::startPublicSession();

        XenForo_Application::disablePhpErrorHandler();
        XenForo_Application::setDebugMode(false);

        //do I need to do this?
        $dependencies = new XenForo_Dependencies_Public();
        $dependencies->preLoadData();

        //do I need to set this here? 
        XenForo_Link::useFriendlyUrls(true);
        XenForo_Link::romanizeTitles(true); 

        //this should return error handling to previous functions
        restore_error_handler();
        restore_exception_handler();
    }  


    }    
    public function link($forumId){
        // Not required if you are not using any of the preloaded data
        $forum = XenForo_Model::create('XenForo_Model_Forum')->getForumById($forumId);
        $title = $forum['title'];
        $url = '/forum/'.XenForo_Link::buildPublicLink('forums',array('node_id' => $forumId, 'title' => $title));
        $result = '<a href="http://www.babyboom.pl/' . $url . '">' . $title . '</a>';  
        return($result);
    }
    public function online(){
        $visitor = XenForo_Visitor::getInstance();
        $sessionModel = XenForo_Model::create('XenForo_Model_Session');
        $onlineUsers = $sessionModel->getSessionActivityQuickList(
            $visitor->toArray(),
            array('cutOff' => array('>', $sessionModel->getOnlineStatusTimeout())),
            ($visitor['user_id'] ? $visitor->toArray() : null)
            );
       
        return array(
            'guests' => $onlineUsers['guests'],
            'robots' => $onlineUsers['robots'],
            'members' => $onlineUsers['members'],
            'total' => $onlineUsers['total']
        );
    }
}
Should I give it a special namespace? Would it help in anything? Is there anything I should do differently?
 
Top Bottom