Parsing Post for BbCode on XenForo_DataWriter_DiscussionMessage_Post?


I have a requirement to parse a post for BbCode and have access to the threadid and post id.

The goal is to extract some meta data from the text and links in the message using a text / link extraction service to figure out what the post is about (sentiment analysis / tag extraction).

Extracted tags / keywords / location information would then added to the XenForo thread tags.

Sentiments will be stored and perhaps used for user / message highlighting (most angry user - negative post / positive post etc).

The best place I've figured this could be done is in XenForo_DataWriter_DiscussionMessage_Post

I have this code which gives me the postid / threadid and the post message in $post['message']

       protected function _postSaveAfterTransaction() {
                $postid = $this->get('post_id');
                $threadid = $this->get('thread_id');
                $post = $this->getMergedData();
                XenForo_Error::debug("postSaveAfterTransaction: postid = $postid , threadid = $threadid");
                XenForo_Error::debug("Post Data: " . print_r($post, TRUE));

Before I go further can someone help me identify if I'm on the right track.

1) Am I on the right track where this might be the best place to extract the meta data from the post?
2) How do I parse the bbcode
cleanly to give me the urls in that post? I can use a regex, but want to make sure there isnt a clean XenForo way of doing this.

Seems like a reasonable approach.

With regards to parsing the BB code, I probably wouldn't bother and I would just go down the regex route; there isn't really anything in XF that would help you with this, so regex is probably the best approach.
Hi Chris,

So, I've managed to plug the postSaveAfterTransaction into Alchemy API and have a sanitised list of keywords. Say they're in an array $keywords.

Can you give me a pointer on how to access the tagging API so that I can add or append these keywords to the thread's tag list?

I can see references in ControllerPublic/Thread.php and DataWriter/Discussion.php - not sure where to start looking at this.

Look at XenForo_ControllerPublic_Thread::actionTags()

This gets the Tagger object, sets the content, sets the tags and saves. You might be able to ignore some of the editable/uneditable and permission stuff as if you're adding this programmatically those permissions may not apply.
FYI, this is part of the code that accomplished it, incase anyone else has a similar issue.

    $frontController = XenForo_Application::get('fc');
    $fakeController = new XenForo_ControllerPublic_Index($frontController->getRequest(), $frontController->getResponse(), $frontController->route());
    $ftpHelper = $fakeController->getHelper('ForumThreadPost');
    list($thread, $forum) = $ftpHelper->assertThreadValidAndViewable($threadid);
    $tagModel = $this->getModelFromCache('XenForo_Model_Tag');
    $tagger = $tagModel->getTagger('thread');
    $tagger->setContent($thread['thread_id'])->setPermissionsFromContext($thread, $forum);
    // XenForo_Error::debug("Tag Errors: " . print_r($tagger->getErrors(),TRUE));
Top Bottom