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

Parsing Post for BbCode on XenForo_DataWriter_DiscussionMessage_Post?

Discussion in 'XenForo Development Discussions' started by shri, Aug 4, 2016.

  1. shri

    shri Member

    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']

    Code:
           protected function _postSaveAfterTransaction() {
                    parent::_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
    Code:
     [URL="http://whatever.com"] 
    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.

    Thanks!
     
  2. Chris D

    Chris D XenForo Developer Staff Member

    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.
     
    shri likes this.
  3. shri

    shri Member

    Thanks Chris.
     
  4. shri

    shri Member

    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.

    Thanks.
     
  5. Chris D

    Chris D XenForo Developer Staff Member

    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.
     
    shri likes this.
  6. shri

    shri Member

    FYI, this is part of the code that accomplished it, incase anyone else has a similar issue.

    Code:
        $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);
        $tagger->setTags($tagModel->splitTags($tagstr));
        // XenForo_Error::debug("Tag Errors: " . print_r($tagger->getErrors(),TRUE));
        $tagger->save();
    
     

Share This Page