Skip to content

Commit

Permalink
Item14414: some optimizations of USERLIST
Browse files Browse the repository at this point in the history
- faster sorting
- don't return a header or footer if all results have been excluded / limitted
- added $username in addition to $wikiname token to make it usable as an input for %USERINFO
- docu foxies
  • Loading branch information
MichaelDaum committed Dec 12, 2017
1 parent 276b8fe commit 5ea6f51
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 22 deletions.
11 changes: 8 additions & 3 deletions core/data/System/VarUSERLIST.txt
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
%META:TOPICINFO{author="ProjectContributor" date="1497053450" format="1.1" version="1"}%
%META:TOPICINFO{author="ProjectContributor" date="1513080633" format="1.1" version="1"}%
%META:TOPICPARENT{name="Macros"}%
---+ USERLIST -- retrieve a list of registered users.
---++ Parameters
| *Parameter* | *Description* |

| *Parameter* | *Description* | *Default* |
| ="filter"= | A regular expression matching the users to be returned. | (no filter) |
| =limit= | Count of groups to be returned. | 0 (no limit) |
| =exclude= | List of group names to be excluded. Can use * wildcards | (none) |
| =casesensitive= | Should filter and exclude lists be a case sensitive comparison. | =false= |
| =header= | | |
| =format= | Format string; see below for supported formatting tokens. | =$wikiname= |
| =footer= | | |

Format tokens that can be used in =format=:

| *Token* | *Description* |
| =$wikiname= | The user's =WikiName= | |
| =$wikiname= | The user's =WikiName= |
| =$username= | The user's =LoginName= |

The [[FormatTokens][standard format tokens]] are also supported.
---++ Examples
* =%<nop>USERLIST{"^Admin"}%= expands to <code><literal>%USERLIST{"^Admin"}%</literal></code>
Expand Down
45 changes: 26 additions & 19 deletions core/lib/Foswiki/Macros/USERLIST.pm
Original file line number Diff line number Diff line change
Expand Up @@ -49,48 +49,55 @@ sub USERLIST {
Foswiki::convertTopicPatternToRegex( $params->{exclude} )
if ( $params->{exclude} );

my $it = $Foswiki::Plugins::SESSION->{users}->eachUser();
$it->{process} = sub {
return $Foswiki::Plugins::SESSION->{users}->getWikiName( $_[0] );
};
my $it = $session->{users}->eachUser();

my @users;

while ( $it->hasNext() ) {
my $user = $it->next();
my $cUID = $it->next();
my $wikiName = $session->{users}->getWikiName($cUID);
if ( length($filter) ) {
next unless ( $user =~ m/$casesensitive$filter/ );
next unless ( $wikiName =~ m/$casesensitive$filter/ );
}
if ( defined $excludeTopics ) {
next if $user =~ m/$casesensitive$excludeTopics/;
next if $wikiName =~ m/$casesensitive$excludeTopics/;
}
push @users, $user;
push @users,
{
wikiname => $wikiName,
username => $session->{users}->getLoginName($cUID),
sorting => NFKD($wikiName),
};
}

return '' unless scalar @users;

my $count = 0;
my $results = $header;
foreach my $user ( sort { NFKD($a) cmp NFKD($b) } @users ) {
my @results = ();
foreach my $user ( sort { $a->{sorting} cmp $b->{sorting} } @users ) {
$count++;
last if ( $limit && $count > $limit );
if ($checkaccess) {
if ( $session->topicExists( $Foswiki::cfg{UsersWebName}, $user ) ) {
if (
$session->topicExists(
$Foswiki::cfg{UsersWebName},
$user->{wikiname}
)
)
{
my $userto =
Foswiki::Meta->load( $session, $Foswiki::cfg{UsersWebName},
$user );
$user->{wikiname} );
next unless $userto->haveAccess('VIEW');
}
}

my $temp = $format;
$temp =~ s/\$wikiname/$user/g;
$results .= $temp;
$results .= $separator if ($separator);
$temp =~ s/\$wikiname/$user->{wikiname}/g;
$temp =~ s/\$username/$user->{username}/g;
push @results, $temp;
}
$results = substr( $results, 0, -length($separator) ) if length($separator);
$results .= $footer;
return '' unless scalar @results;

my $results = $header . join( $separator, @results ) . $footer;
return Foswiki::expandStandardEscapes($results);
}

Expand Down

0 comments on commit 5ea6f51

Please sign in to comment.