Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

User macro to get a label count for each space in a specific category

Vincent Poiré
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
November 25, 2019

Hello,

Here is the thing, I got a lot of spaces that use the same labels and want to count some of the labels per spaces. The catch is that I don't want to count everything in the confluence instance, I just want what's in 1 or 2 specific space categories.

Not a dev myself and been fishing around for this (to list the spaces in the category I want):

## Macro title: List Spaces by Category
## Macro has a body: N

## @param Label1:title=Category|type=string|required=true|desc=The category you want to list spaces from. Only non-archived spaces will be listed.
## @param Label2:title=Category2|type=string|required=false|desc=Optional second category. Only spaces that have BOTH categories will be listed.

## try to get space categories
#set ( $spaceLabel1 = $action.labelManager.getLabel( "team:${paramLabel1}" ) )
#set ( $spaceLabel2 = $action.labelManager.getLabel( "team:${paramLabel2}" ) )

## check if space category exists
#if ( $!spaceLabel1 )
## try to get spaces by category
#set ( $spaces1 = $action.labelManager.getSpacesWithLabel( $spaceLabel1 ) )

## sort spaces alphabetically
#set($size=$spaces1.size())
#foreach($junk in $spaces1) ##Bubble sort takes n^2 passes
#set($actual=-1)##Having trouble with math on $velocityCount -- keeping my own count
#foreach($line in $spaces1)
#set($actual=$actual+1)
#if($velocityCount<$size) ##Preventing bad array access
## Compare this and previous
## If this is smaller, swap
#if ($line.name.compareToIgnoreCase($spaces1.get($velocityCount).name) > 0 )
#set ($tmp=$spaces1.get($velocityCount))
#set ($junk=$spaces1.set($velocityCount,$line))
#set ($junk=$spaces1.set($actual,$tmp))
#end
#end
#end
#end

<ul>
#foreach ( $space in $spaces1 )
## check if current user can view space and space is not archived
#if ( $permissionHelper.canView( $action.remoteUser, $space ) && !$space.isArchived() )
## check if there is a second category that the space should have to be listed
#if ($!spaceLabel2)
#set ( $spaces2 = $action.labelManager.getSpacesWithLabel( $spaceLabel2 ) )
#if($spaces2.contains($space))
<li><a href="$!space.urlPath">$!space.name</a> - $!space.key</li>
#end
#else
<li><a href="$!space.urlPath">$!space.name</a> - $!space.key</li>
#end
#end
#end
</ul>
#else
<div class="aui-message warning"> <p class="title"> <span class="aui-icon icon-warning">Warning</span> <p>Couldn't find space category <strong>$paramLabel1</strong>!</p> </div>
#end

 

Adding on that I tried to get a count by defining a new param at the top:

## @param Label3:title=Label|type=string|required=true|desc=Please enter a label to count

and using an existing (Confluence?) function :

#set ( $containerManagerClass=$content.class.forName('com.atlassian.spring.container.ContainerManager') )
#set ( $getInstanceMethod=$containerManagerClass.getDeclaredMethod('getInstance',null) )
#set ( $containerManager=$getInstanceMethod.invoke(null,null) )
#set ( $containerContext=$containerManager.containerContext )
#set ( $labelManager = $containerContext.getComponent('labelManager') )
#set ( $requestedLabel = $labelManager.getLabel($paramLabel) )
<li><a href="$!space.urlPath">$!space.name</a> - $!space.key - $!labelManager.getContentCount($requestedLabel)</li> 

But none of my efforts worked. Moreover I have no idea how to count only the current pages.

Any help from here ? 

0 answers

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events