Check how many user follow/watch an article

Hi.

Is there a possibility to check how many or which users follow a given page? I only found a snippet with an old approach: https://snippets.xwiki.org/xwiki/bin/view/Extension/List%20users%20with%20watched%20pages%20stored%20in%20WatchListClass%20objects/ that is ofc not working.

All “ofc working solutions” my llm had didn’t work too.

Regards, Simpel

I don’t know this code well but here’s a try with my LLM:

Hi Simpel,

The WatchListClass objects are gone — the WatchList application was removed
and “watching a page” is now part of the Notifications feature. Watching a
location is stored as a notification filter preference (Hibernate entity
DefaultNotificationFilterPreference, table notification_filter_prefs), not
as an XObject anymore.

Unfortunately the public notification APIs are all per-user (e.g.
$services.notification.watch only answers for the current user), so there is
no ready-made “who watches this page” API. But the preferences are stored in a
regular mapped entity, so you can query them directly with HQL. Something like
(needs programming rights):

{{groovy}}
import org.xwiki.notifications.filters.NotificationFilterType
import org.xwiki.query.Query

// The page you want to inspect:
def docRef = services.model.resolveDocument('Sandbox.WebHome')
def ref = services.model.serialize(docRef, 'default')   // e.g. xwiki:Sandbox.WebHome

def hql = '''select distinct nfp.owner
  from DefaultNotificationFilterPreference nfp
  where nfp.enabled = true
    and nfp.filterType = :type
    and (nfp.pageOnly = :ref or nfp.page = :ref)'''

def owners = services.query.createQuery(hql, Query.HQL)
    .bindValue('type', NotificationFilterType.INCLUSIVE)
    .bindValue('ref', ref)
    .execute()

println "${owners.size()} user(s) watch ${ref}:"
owners.each { println "* ${it}" }
{{/groovy}}

Notes:

  • nfp.pageOnly matches users watching just this page; nfp.page matches
    users watching this page and its children. Adjust the condition to whatever
    you mean by “follow”.
  • This reads internal storage, so treat it as best-effort, not a stable API.
  • It does not count inherited watches (someone watching a parent space or
    the whole wiki) nor users on automatic-watch mode — add nfp.wiki / ancestor
    references to the query if you need those too.

Hope that helps!

Note: I haven’t tried it so I don’t know if it works or not. Ofc you also need Programming Rights.

Thanks for the answer. That’s a good starting point. I will give it a try in two weeks.