janslu
Active member
Can you take a look at my code below and check whether this is the best way? I need this for a super small API, to retrieve number of users online and latest posts (you can see it in action at https://www.babyboom.pl . I am polling this script quite often and I wonder if this is the best way to query...
PHP:
<?php
class XenForoHelperClass {
/*
* Initialise the XenForom Controllers:
*/
public function __construct(){
if (!class_exists('\XF')) {
$this->init();
}
}
public static function init() {
$fileDir = '/var/www/html/forum';
if ( ! file_exists( $fileDir . '/src/XF.php' ) ) {
return false;
}
require( $fileDir . '/src/XF.php' );
\XF::start($fileDir);
$app = \XF::setupApp('XF\Pub\App');
$app->start();
}
//currently online
public static function online(){
/** @var \XF\Repository\SessionActivity $activityRepo */
$activityRepo = \XF::app()->repository('XF:SessionActivity');
$onlineCount = $activityRepo->getOnlineCounts();
return array(
'guests' => $onlineCount['guests'],
'members' => $onlineCount['members'],
'total' => $onlineCount['total']
);
}
public function latestPosts($nodeIds=null, $limit=5) {
$visitor = \XF::visitor();
$filter = 'latest';
$router = \XF::app()->router('public');
/** @var \XF\Repository\Thread $threadRepo */
$threadRepo = \XF::app()->repository('XF:Thread');
$postRepo = \XF::app()->repository('XF:Post');
$parser = \XF::app()->bbCode()->parser();
$rules = \XF::app()->bbCode()->rules('post:rss');
$formatter = \XF::app()->stringFormatter();
$bbCodeCleaner = \XF::app()->bbCode()->renderer('bbCodeClean');
$bbCodeRenderer = \XF::app()->bbCode()->renderer('html');
$threadFinder = $threadRepo->findThreadsWithLatestPosts();
$threadFinder
->with('Forum.Node.Permissions|' . $visitor->permission_combination_id)
->limit(max($limit * 2, 10));
if ($nodeIds && !in_array(0, $nodeIds))
{
$threadFinder->where('node_id', $nodeIds);
}
//$threadFinder->forFullView(true);
$threadFinder
->with('LastPoster')
->withReadData();
$latestposts = array();
$maxLength = 120;
/** @var \XF\Entity\Thread $thread */
foreach ($threads = $threadFinder->fetch() AS $threadId => $thread)
{
$post = $postRepo->finder('XF:Post')
->where('Thread.thread_id', $threadId)
->where('message_state', 'visible')
->order('post_id', 'DESC')
//->indexHint('FORCE', 'post_date')
->fetchOne();
$message = $bbCodeCleaner->render($formatter->wholeWordTrim($post->message, $maxLength),$parser, $rules);
$message = preg_replace('/\[(attach|media|img|spoiler)\]/siU', '', $message);
$message = preg_replace('/^[\t\s]*(\r?\n){2,}/m', '', $message);
$latestposts[$threadId] = array(
'post_id' => $post['post_id'],
'thread_id' => $thread['thread_id'],
'title' => $thread['title'],
'message' => substr(strip_tags(preg_replace('/\[.*\]/', '', $message)),0,90),
'node' => $thread['node_id'],
'username' => $post['username'],
'forumtitle' => $thread['node_title'],
'forumurl' => $router->buildLink('canonical:forums',array('node_id' => $thread['node_id'], 'title' => $thread['node_title'])),
'url' => $router->buildLink('canonical:posts', $post),
'replycount' => $thread['reply_count'],
'dateline' => $post['post_date'],
'date' => $this->localDatetime($post['post_date'])
);
}
//$latestthreads = $latestthreads->slice(0, $limit, true);
return $latestposts;
}
}