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

XF 1.2 How does XF calculate a user's total number of likes received?

Discussion in 'XenForo Questions and Support' started by Gene, Oct 3, 2013.

  1. Gene

    Gene Member

    What exactly is being counted when XF generates the "likes received" figure for a user's profile or for the notable members list? As many ways as I try and pull this figure out of the DB, it seems I come up with numbers that are a few dozen likes higher for many users than XF is reporting. Note that this isn't true for all users - my own account, for example, has not received any of these "mystery" likes, while my most prolific users (~1500 total likes) have received up to 50 or so likes that XF is ignoring when it spits out its figures.

    So, ultimately, I'm wondering how XF calculates the 'total likes' figure, and where I can find this routine in the code, so that I might do the same using SQL queries.

    For reference: The thread that spawned this question.
     
    Last edited: Oct 3, 2013
  2. Jeremy

    Jeremy XenForo Moderator Staff Member

    Add-ons can add their own likes, which is stored in xf_user with a number and a separate entry in the likes table for display.
     
  3. Gene

    Gene Member

    So how do I find these likes and filter them out of my query?
     
  4. Gene

    Gene Member

    I see the like_count column in xf_user, but at what point (and using what routine) is that being calculated and stored?
     
  5. Gene

    Gene Member

    And another thought: Is it possible the "mystery likes" are from previously deleted users, as described here?
     
  6. Mike

    Mike XenForo Developer Staff Member

    I'm not sure I understand what @Jeremy is saying there. The data is in xf_liked_content, but not every like in there may count towards the total likes received (eg, deleted threads or posts).
     
    Jeremy likes this.
  7. Gene

    Gene Member

    Correct. So in the queries I've tried, I specify that the liked content must be visible to be included in the count. The mystery likes are from something else.
     
  8. Jeremy

    Jeremy XenForo Moderator Staff Member

    What @Mike said. I wasn't in front of my computer to check code but knew that the total number of likes were stored in xf_user table. Sorry for the confusion! If you have mystery likes, I would start looking into what add-ons add the ability to like that you have.
     
  9. Mike

    Mike XenForo Developer Staff Member

    The user version is really just a cache. It's theoretically possible that it's off for various reasons.
     
  10. Gene

    Gene Member

    Yes - this is what I'm thinking. So the question I'm asking is... when/where/how is that column updated?
     
  11. Gene

    Gene Member

    ConvEss is the only addon I've got with any like-related feature.
     
  12. Jeremy

    Jeremy XenForo Moderator Staff Member

    And have you accounted for those likes in your query?
     
  13. Gene

    Gene Member

    Yes. I am only including likes applied where content_type is post or profile_post - the only other content_type is conversation_message, which gets excluded. This post has all the queries I've tried.
     
  14. Gene

    Gene Member

    At this point, the key to answering the entire mystery is how, when and where XF is calculating and caching like_count in xf_user.
     
  15. Mike

    Mike XenForo Developer Staff Member

    It's a count that gets adjusted all over the place when various events happen... As mentioned, it's designed primarily as a cache value. The canonical data should be in xf_liked_content.
     
  16. Gene

    Gene Member

    Unfortunately, my XF installation is relying on like_count as the official number displayed on all profiles and the notable members list, so if that's the case, this whole "most likes in a month' contest being run by a user is based on farcical data and I have no way of verifying it in the DB. :mad:

    Edit: So is this technically a bug? If XF is incorrectly calculating and displaying total likes received, I'd call that a bug.
     
  17. Teapot

    Teapot Well-Known Member

    How so? The likes counter is incremented every time someone hits the Like button, and decremented when they hit the Unlike button, and the information is stored in xf_liked_content if you need to verify it to be accurate. As far as I am aware, the current behaviour is as designed. If I were really adamant that the Likes be calculated based on content that still exists, I'd run the calculation on xf_liked_content.
     
  18. Teapot

    Teapot Well-Known Member

    The most simple query is:

    Code:
    SELECT COUNT(*) FROM `xf_liked_content`
    WHERE `content_user_id` = 1
    
    Replacing 1 with the user whose Likes you want to count, obviously. What exactly are you aiming to do beyond that?
     
  19. Gene

    Gene Member

    Getting that data isn't the issue. The issue is that like_count (in xf_user) is what XF is using as the "official" number displayed publicly on the forum (in a user's profile, on the notable members page, etc.), but the data in like_count is incorrect - it does not match up with the number returned by variations on that query. See above for a link to the queries I've tried.
     
  20. Gene

    Gene Member

    To clarify:

    When I query xf_liked_content, I get one number of total likes.
    When I query xf_post and xf_profile_post, and sum those likes, I get the same number, so it checks out.

    In theory, that number should be the same as what's stored as like_count in xf_user, but it's not, leading me to believe that like_count is not up-to-date or not being calculated correctly.

    XF relies on the like_count column to display total likes all throughout the forum, so this is a serious issue when users are competing for the most likes in a period of time and the forum can't even report that number correctly.

    There is only one question to answer, I'm not sure why this is so difficult:
    Does anyone on the team here have any idea how, when, and where XF generates the like_count column in xf_user?
     

Share This Page