How to count number of active users in wiki

Hi XWiki Community,

I managed to have a counter of users with this velocity code:

$services.query.hql("select count(distinct obj.name) from BaseObject obj where obj.className = 'XWiki.XWikiUsers'").execute()[0]

Or this one:

$services.user.group.getMembers('xwiki:XWiki.XWikiAllGroup').size()

How can I adapt it to count only active users?
How is the “active” field named in the database?

Thank you in advance!
Luc

Hello, I’ve added the info at https://www.xwiki.org/xwiki/bin/view/Documentation/AdminGuide/User%20Management#HUserStorage

Let me know if this helps.

Thx

Hi @vmassol,

Thank you for your answer.
I tried to use this code to get the isActive property:

#foreach ($userWiki in $services.user.group.getMembers('xwiki:XWiki.XWikiAllGroup'))
  #set ($props = $services.user.getProperties($userWiki))
1. $userWiki, $props.getEmail(), $props.isActive()
#end

But it returns this content:

  1. xwiki:XWiki.a=aaa_mycompany=fr, L.DURON@mycompany.fr, true
  2. xwiki:XWiki.b=bbb_mycompany=fr, L.DURON@mycompany.fr, true
  3. xwiki:XWiki.c=ccc_mycompany=fr, L.DURON@mycompany.fr, true

Why are the last two variables concerning my user and not the user of the loop?

Regards,
Luc

I thought you wanted to do a query (see https://extensions.xwiki.org/xwiki/bin/view/Extension/Query%20Module) ?

I wanted to verify if the doc I had created was enough for you to guess that to see all users who are inactive, you had to write:

{{velocity}}
$services.query.xwql('where doc.object(XWiki.XWikiUsers).active = 0').execute()
{{/velocity}}

Now re your script, it’s because you’re using the wrong API:

What you want is to call xwiki-platform/xwiki-platform-core/xwiki-platform-user/xwiki-platform-user-script/src/main/java/org/xwiki/user/script/UserScriptService.java at cb64b6a21c458301468884cabe046437659789d9 · xwiki/xwiki-platform · GitHub or xwiki-platform/xwiki-platform-core/xwiki-platform-user/xwiki-platform-user-script/src/main/java/org/xwiki/user/script/UserScriptService.java at cb64b6a21c458301468884cabe046437659789d9 · xwiki/xwiki-platform · GitHub

You can use for example: #set ($props = $services.user.getProperties($userWiki.toString())).

Hope it helps

1 Like

Thank you very much @vmassol for your explanations!

This code does the trick:

#set ($nbActiveUsers = 0)
#foreach ($userWiki in $services.user.group.getMembers('xwiki:XWiki.XWikiAllGroup'))
  #set ($props = $services.user.getProperties($userWiki.toString()))
  #if ($props.isActive())
    #set ($nbActiveUsers = $nbActiveUsers + 1)
  #end
#end

Or this query:

$services.query.xwql('where doc.object(XWiki.XWikiUsers).active = 1').addFilter('count').execute()[0]

Best regards,
Luc

Note that the query is much faster and lighter on CPU.

1 Like