Forums

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

Confluence user macro exclude pages with certain labels

Erik Ekengren
Contributor
September 3, 2021

Hi!

I am in need of a simple solution to audit and keep relevant information in a Confluence Space. My thought is to have at least one Space admin that are responsible for keeping the information up to date. For this I have created a page that only the space admins can see, and on that page is a user macro that I have found searching far and wide on the web. This user macro presents all pages in a space that haven´t been updated for X days, and by doing this it is easy to go in and have a look at all the pages that needs an update. They are then able to just do an update on the page to keep the the information up to date.

However I have noticed that I would very much would like to exclude some pages in this report. For example all the meeting notes (that could be handy to still keep but doesn´t need an update) would be very good to exclude from the user macro. So I was thinking that maybe we could update this user macro to exclude pages with a specific label, the problem is that I am not a developer so my knowledge around this is very limited.

Therefore I would very much appreciate if someone would be able to help me with this task. I think this would benefit a lot of other Confluence admins as well that have similar requests from management etc.

This is the user macro I have and would like to be able to exclude pages with certain labels:


## Macro title: Old Pages
## Macro has a body: Y or N (N)
## Body processing: Selected body processing option
## Output: Selected output option
##
## Developed by: Andrew Frayling
## Date created: 05/02/2012
## Installed by: <your name>

## Macro to display pages that have not been updated for x days
## @Param numdays:title=Number of Days|type=int|desc=Enter number of days to compare|required=true|multiple=false|default=180

#set ( $allPagesInSpace = $pageManager.getPages($space, true) )

## default value handler, set default to 180 days
#set ( $numdays = $paramnumdays )
#if(!$numdays)
#set ( $numdays = "180" )
#else
#set ( $numdays = $paramnumdays )
#end

## cast the number of days to an Integer
#set ( $Integer = 0 )
#set ( $intnumdays = $Integer.parseInt($numdays) )

## negative sign the number of days
#set ( $negativeDays = (-1 * $intnumdays) )

## get a calendar object so we can calculate the
## date we wish to compare with the page modification date
## e.g. $compareDate.add(6, -7) for pages modified over a week ago
#set ( $compareDate = $action.dateFormatter.getCalendar() )
$compareDate.setTime($content.getCurrentDate())
$compareDate.add(6, $negativeDays)

<p>Pages last updated before <strong>$action.dateFormatter.format($compareDate.getTime())</strong></p>

<table>
<tr>
<th>Page</th><th>Created</th><th>Created By</th><th>Last Updated</th><th>Last Updated By</th>
</tr>

## loop through all the pages in the current space
#foreach($page in $allPagesInSpace)

## get a calendar object and for each page in space set it to last modification date
#set ($pagedate = $action.dateFormatter.getCalendar())
$pagedate.setTime($page.getLastModificationDate())

## only display pages last modified before the compare date
#if ($pagedate.before($compareDate))
<tr>
<td>#contentLink2($page true false)</td>
<td>$action.dateFormatter.format($page.getCreationDate())</td>
<td>#usernameLink($page.creatorName)</td>
<td>$action.dateFormatter.format($page.getLastModificationDate())</td>
<td>#usernameLink($page.lastModifierName)</td>
</tr>
#end
#end
## end looping through pages in the current space

</table>

2 answers

1 accepted

0 votes
Answer accepted
Nic Brough -Adaptavist-
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 3, 2021

I think this chunk of code should get you most of the way there:

 

#foreach ( $label in $content.getLabels() )
     #if ( ( $label == "Penguin" ) )
       Penguins are here!
     #end
  #end
Erik Ekengren
Contributor
September 3, 2021

Thank you för trying to help me Nic but I am not able to figure out how to have 2 separate IF statements in the same Macro. I barely have basic understanding of how this code and are not able to do this at all. I found another macro where I am able to do the other way around (to exclude pages with specific labels) but then I will not be able to sort out the pages that haven´t been modified in a while. 

Is it possible to do 2 different IF statements in the same macro?

This is the other macro:


## @Param ExcludeLabels:title=Labels to exclude|type=string|desc=Enter labels to be excluded, separate multiple labels with comma. This excludes pages with the label that you enter|default=archived
## @Param numdays:title=Number of Days|type=int|desc=Enter number of days to compare|required=true|multiple=false|default=180

#set ( $excludedLabelsString = $paramExcludeLabels + "," )
#set ( $excludedLabelsString = $excludedLabelsString.replace(" ","") )
#set ( $compareDate = $action.dateFormatter.getCalendar() )
#set ( $allPagesInSpace = $pageManager.getPages($space, true) )

## default value handler, set default to 180 days
#set ( $numdays = $paramnumdays )
#if(!$numdays)
#set ( $numdays = "180" )
#else
#set ( $numdays = $paramnumdays )
#end

## cast the number of days to an Integer
#set ( $Integer = 0 )
#set ( $intnumdays = $Integer.parseInt($numdays) )

## negative sign the number of days
#set ( $negativeDays = (-1 * $intnumdays) )

## get a calendar object so we can calculate the
## date we wish to compare with the page modification date
## e.g. $compareDate.add(6, -7) for pages modified over a week ago
#set ( $compareDate = $action.dateFormatter.getCalendar() )
$compareDate.setTime($content.getCurrentDate())
$compareDate.add(6, $negativeDays)

<p>Pages last updated before <strong>$action.dateFormatter.format($compareDate.getTime())</strong></p>

<table>
<tr>
<th>Page</th><th>Created</th><th>Created By</th><th>Last Updated</th><th>Last Updated By</th>
</tr>
#foreach ( $page in $allPagesInSpace )

## get a calendar object and for each page in space set it to last modification date
#set ($pagedate = $action.dateFormatter.getCalendar())
$pagedate.setTime($page.getLastModificationDate())

#set ( $notContainsExcludedLabel = true )
## THIS DO NOT WORK if ($pagedate.before($compareDate))
#if ( $excludedLabelsString != ",")
#foreach ( $labelling in $page.getLabellings() )
#set ( $label = $labelling.getLabel() + "," )
#if ( $excludedLabelsString.contains($label) )
#set ( $notContainsExcludedLabel = false )
#end
#end
#end
#if ( $notContainsExcludedLabel )
<tr>
<td>#contentLink2($page true false)</td>
<td>$action.dateFormatter.format($page.getCreationDate())</td>
<td>#usernameLink($page.creatorName)</td>
<td>$action.dateFormatter.format($page.getLastModificationDate())</td>
<td>#usernameLink($page.lastModifierName)</td>
</tr>
#set ($pageDate = $action.dateFormatter.getCalendar())
#if ( $pageDate.before($compareDate) )
#else
#end
#end
#end

Nic Brough -Adaptavist-
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 3, 2021

Yes, you can do many if statements, or even nest them.

For example:

#foreach ( $label in $content.getLabels() )
     #if ( ( $label == "Penguin" ) )
       Penguins are here!
       #if ($temperature == "warm")
           Galapagos Penguins!
       #else
           Emporer Penguins!
       #end
     #end     
     #if ( ( $label == "Gorilla" ) )
       Smoke me a Gorilla, I'll be back for Breakfast
     #end
#end

The way to think of these macros is very "procedural", the interpreter really does read them one line at a time, top to bottom.  So if you can get the code into the right place inside your #if statements, it should work fine.

Like Erik Ekengren likes this
Erik Ekengren
Contributor
September 5, 2021

Thank you Nic! I will try this and hopefully I will be able to come up with the solution. 

 

BR/

Erik

0 votes
Erik Ekengren
Contributor
September 6, 2021

Got it to work after some (many) failures. This is the end result and will present all pages that hasn´t been updated before X days and also filter out labels that you specify in the macro:

 


## Macro title: Audit outdated pages
## Macro has a body: Y or N (N)
## Body processing: Selected body processing option
## Output: Selected output option
##
## Developed by: Erik Ekengren
## Date created: 06/09/2021
## Installed by: Your name

## Macro to display pages that have not been updated for x days
## @Param numdays:title=Number of Days|type=int|desc=Enter number of days to compare|required=true|multiple=false|default=180
## @Param ExcludeLabels:title=Labels to exclude|type=string|desc=Enter labels to be excluded, separate multiple labels with comma. This excludes pages with the label that you enter|default=archived

#set ( $allPagesInSpace = $pageManager.getPages($space, true) )

#set ( $excludedLabelsString = $paramExcludeLabels + "," )
#set ( $excludedLabelsString = $excludedLabelsString.replace(" ","") )
#set ( $descendantPages = $pageManager.getDescendents($content) )
#set ( $compareDate = $action.dateFormatter.getCalendar() )

## default value handler, set default to 180 days
#set ( $numdays = $paramnumdays )
#if(!$numdays)
#set ( $numdays = "180" )
#else
#set ( $numdays = $paramnumdays )
#end

## cast the number of days to an Integer
#set ( $Integer = 0 )
#set ( $intnumdays = $Integer.parseInt($numdays) )

## negative sign the number of days
#set ( $negativeDays = (-1 * $intnumdays) )

## get a calendar object so we can calculate the
## date we wish to compare with the page modification date
## e.g. $compareDate.add(6, -7) for pages modified over a week ago
#set ( $compareDate = $action.dateFormatter.getCalendar() )
$compareDate.setTime($content.getCurrentDate())
$compareDate.add(6, $negativeDays)

<p>Pages last updated before <strong>$action.dateFormatter.format($compareDate.getTime())</strong></p>

<table>
<tr>
<th>Page</th><th>Created</th><th>Created By</th><th>Last Updated</th><th>Last Updated By</th>
</tr>

## loop through all the pages in the current space
#foreach($page in $allPagesInSpace)

## find pages with the label/s
#set ( $notContainsExcludedLabel = true )
#if ( $excludedLabelsString != ",")
#foreach ( $labelling in $page.getLabellings() )
#set ( $label = $labelling.getLabel() + "," )
#if ( $excludedLabelsString.contains($label) )
#set ( $notContainsExcludedLabel = false )
#end
#end
#end

## get a calendar object and for each page in space set it to last modification date
#set ($pagedate = $action.dateFormatter.getCalendar())
$pagedate.setTime($page.getLastModificationDate())

## only display pages last modified before the compare date
#if ($pagedate.before($compareDate))
#if ( $notContainsExcludedLabel )
<tr>
<td>#contentLink2($page true false)</td>
<td>$action.dateFormatter.format($page.getCreationDate())</td>
<td>#usernameLink($page.creatorName)</td>
<td>$action.dateFormatter.format($page.getLastModificationDate())</td>
<td>#usernameLink($page.lastModifierName)</td>
</tr>
#end
#end
#end
## end looping through pages in the current space

</table>

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
7.13.0
TAGS
AUG Leaders

Atlassian Community Events