Fixed XenForo importer, likes problem

Jake Bunce

Well-known member
Affected version
1.5.15a
I had a customer with missing likes after a XF to XF import. I ended up just running a query to manually import the missing likes.

But in looking at the import code it doesn't look correct to my eye:

Rich (BB code):
    /**
     * Limited to likes for post and profile posts at present
     *
     * @param integer $start
     * @param array $options
     */
    public function stepLikes($start, array $options)
    {
        $options = array_merge(array(
            'limit' => 100,
            'max' => false
        ), $options);

        $sDb = $this->_sourceDb;

        /* @var $model XenForo_Model_Import */
        $model = $this->_importModel;

        $likeTypes = $model->getSupportedLikeTypes();

        if ($options['max'] === false)
        {
            $options['max'] = $sDb->fetchOne('
                SELECT MAX(like_id)
                FROM xf_liked_content
                WHERE content_type IN (' . $sDb->quote(array_keys($likeTypes)) . ')
            ');
        }

        $likes = $sDb->fetchAll($sDb->limit(
            '
                SELECT *
                FROM xf_liked_content
                WHERE content_type IN (' . $sDb->quote(array_keys($likeTypes)) . ')
                    AND like_id > ' . $sDb->quote($start) . '
                ORDER BY like_id
            ', $options['limit']
        ));
        if (!$likes)
        {
            return true;
        }

        $next = 0;
        $total = 0;

        $groupedLikes = array();
        $userIds = array();
        foreach ($likes AS $like)
        {
            $groupedLikes[$like['content_type']][$like['content_id']] = $like; // not adding a new record each time?  it looks to me like this would only capture one like per batch per content item
            $userIds[] = $like['like_user_id'];
            $userIds[] = $like['content_user_id'];
        }

        $userIdMap = $model->getImportContentMap('user', $userIds);

        XenForo_Db::beginTransaction();

        foreach ($groupedLikes AS $contentType => $likes)
        {
            $contentIdMap = $model->getImportContentMap($contentType, array_keys($likes));

            foreach ($likes AS $contentId => $like)
            {
                $next = $like['like_id'];

                $newContentId = $this->_mapLookUp($contentIdMap, $contentId);
                if (!$newContentId)
                {
                    continue;
                }

                $model->importLike(
                    $contentType,
                    $newContentId,
                    $this->_mapLookUp($userIdMap, $like['content_user_id'], 0),
                    $this->_mapLookUp($userIdMap, $like['like_user_id'], 0),
                    $like['like_date']
                );

                $total++;
            }
        }

        XenForo_Db::commit();

        $this->_session->incrementStepImportTotal($total);

        return array($next, $options, $this->_getProgressOutput($next, $options['max']));
    }
 
Back
Top Bottom