Fixed Possible bug in Post datawriter...

Liam W

in memoriam 1998-2020
I'm not sure if this would be classed as a bug or not, but I've spent ages trying to figure out what is causing the error and I finally found it so...

This is the _getForumInfo() method in the post datawriter:

PHP:
protected function _getForumInfo()
    {
        if (!$forum = $this->getExtraData(self::DATA_FORUM))
        {
            $forum = $this->getModelFromCache('XenForo_Model_Forum')->getForumByThreadId($this->get('thread_id'));

            $this->setExtraData(self::DATA_FORUM, $forum ? $forum : array());
        }

        return $this->getExtraData(self::DATA_FORUM);
    }

If the forum extra isn't set, it attempts to get it based on the thread id (which may not actually be set yet, if this is called during preSave of a thread insert and this is the first post dw). If this fails, it sets the forum extra to an empty array.

The issue arises in the setExtraData method:

PHP:
public function setExtraData($name, $value)
    {
        if ($name == self::DATA_FORUM)
        {
            if (!is_array($value))
            {
                return;
            }
            XenForo_DataWriter_Discussion_Thread::setForumCacheItem($value);
        }

        return parent::setExtraData($name, $value);
    }

The set method calls the setForumCacheItem method in the Thread datawriter if the input value is an array. That method in turns uses the node_id key from that value, however as the array is empty (as it couldn't get the forum data), this causes a server error.

Unlikely to happen, yet it happened to me and has taken me ages to figure out. I'll need to add a check to one of my addons...

Liam
 
I think it makes sense to only set the cache if we get a valid response; that will at least prevent an error.

Though I would also note that the usage generally assumes it's being run post save (or at least where a forum is essentially guaranteed to exist).
 
Top Bottom