How to find the last time a confluence page was modified/added

I am building a user macro that list all spaces. I would like to highlight those spaces where there has been no user action since X number of days, where user action means page updates or new pages.

I tried: 

#set ( $spaceModificationDate = $space.getLastModificationDate() )

But this doesn't work, it shows an older date even when there has been more recent page updates in a space.

What would be the right way to find the latest date a page was updated or added in a space?

I would also like to know the user that performed that action.

Thanks in advance.

1 answer

1 accepted

0 votes
Accepted answer
Stephen Deutsch Community Champion Apr 12, 2015

Hi Adolfo,

I don't know if it's the right way to do it, but I just hijacked the results from the activity stream macro to get the latest page or blogpost update in a space.  I'm sure you can adapt it for your needs.

## @noparams
#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 ( $contentEntityManager = $containerContext.getComponent('contentEntityManager') )
#set ( $Integer = 0 )
#set ( $count = 0 )
#set ( $spaceKey = $space.key )
<table>
  <tr>
    <th>Page</th>
    <th>Last Modifier</th>
    <th>Modified Date</th>
  </tr>
  #set ( $inString = $action.getHelper().renderConfluenceMacro("{recently-updated:spaces=${spaceKey}|types=page,blogpost|max=1}") )
  #foreach ( $line in $inString.split('<a href="') )
    #set ( $count = $count + 1 )
    #foreach ( $part in $line.split('"') )
      #set ( $string = $part )
      #break
    #end
    #if ( $count == 2 )
      #break
    #end
  #end
  
  #if ( $string.contains("/display/") )
    #set ( $slashCount = $string.length() - $string.replace("/", "").length() )
    #if ( $slashCount == 6 )
      ## Then this must be a blog post!
      #set ( $splitString = $string.split("/") )
      #set ( $index = 0 )
      #foreach ( $section in $splitString )
        #set ( $index = $index + 1 )
        #if ( $index == 3 )
          #set ( $spaceName = $section )
        #end
        #if ( $index == 4 )
          #set ( $year = $Integer.parseInt($section) )
        #end
        #if ( $index == 5 )
          #set ( $month = $Integer.parseInt($section) - 1 )
        #end
        #if ( $index == 6 )
          #set ( $day = $Integer.parseInt($section) )
        #end
        #if ( $index == 7 )
          #set ( $pageNameEncoded = $section )
          #set ( $pageName = $generalUtil.urlDecode($pageNameEncoded) )
        #end
      #end
      #set ( $blogPostCalendarObject = $action.dateFormatter.getCalendar() )
      $blogPostCalendarObject.set($year, $month, $day)
      #set ( $lastUpdatedPage = $pageManager.getBlogPost($spaceName, $pageName, $blogPostCalendarObject) )
    #else
      ## Otherwise it is a regular page
      #set ( $splitString = $string.split("/") )
      #set ( $index = 0 )
      #foreach ( $section in $splitString )
        #set ( $index = $index + 1 )
        #if ( $index == 3 )
          #set ( $spaceName = $section )
        #end
        #if ( $index == 4 )
          #set ( $pageNameEncoded = $section )
          #set ( $pageName = $generalUtil.urlDecode($pageNameEncoded) )
        #end
      #end
      #set ( $lastUpdatedPage = $pageManager.getPage($spaceName, $pageName) )
    #end
  #end
  
  #if ( $string.contains("/pages/") )
    #foreach ( $part in $string.split("=") )
      #set ( $pageId = $part )
    #end
    #set ( $long = $generalUtil.getSystemStartupTime() )
    #set ( $pageId = $long.parseLong($pageId) )
    #set ( $lastUpdatedPage = $contentEntityManager.getById($pageId) )
    #set ( $pageName = $lastUpdatedPage.title )
  #end
  #set ( $lastModifier = $lastUpdatedPage.lastModifier.name )
  #set ( $lastModifiedDate = $lastUpdatedPage.lastModificationDate )
  <tr>
    <td>$pageName</td>
    <td>$lastModifier</td>
    <td>$lastModifiedDate</td>
  </tr>
</table>

Or it is simpler for only pages:

## @noparams
#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 ( $contentEntityManager = $containerContext.getComponent('contentEntityManager') )
#set ( $count = 0 )
#set ( $spaceKey = $space.key )
<table>
  <tr>
    <th>Page</th>
    <th>Last Modifier</th>
    <th>Modified Date</th>
  </tr>
  #set ( $inString = $action.getHelper().renderConfluenceMacro("{recently-updated:spaces=${spaceKey}|types=page|max=1}") )
  #foreach ( $line in $inString.split('<a href="') )
    #set ( $count = $count + 1 )
    #foreach ( $part in $line.split('"') )
      #set ( $string = $part )
      #break
    #end
    #if ( $count == 2 )
      #break
    #end
  #end
  
  #if ( $string.contains("/display/") )
    #set ( $splitString = $string.split("/") )
    #set ( $index = 0 )
    #foreach ( $section in $splitString )
      #set ( $index = $index + 1 )
      #if ( $index == 3 )
        #set ( $spaceName = $section )
      #end
      #if ( $index == 4 )
        #set ( $pageNameEncoded = $section )
        #set ( $pageName = $generalUtil.urlDecode($pageNameEncoded) )
      #end
    #end
    #set ( $lastUpdatedPage = $pageManager.getPage($spaceName, $pageName) )
  #end
  
  #if ( $string.contains("/pages/") )
    #foreach ( $part in $string.split("=") )
      #set ( $pageId = $part )
    #end
    #set ( $long = $generalUtil.getSystemStartupTime() )
    #set ( $pageId = $long.parseLong($pageId) )
    #set ( $lastUpdatedPage = $contentEntityManager.getById($pageId) )
    #set ( $pageName = $lastUpdatedPage.title )
  #end
  #set ( $lastModifier = $lastUpdatedPage.lastModifier.name )
  #set ( $lastModifiedDate = $lastUpdatedPage.lastModificationDate )
  <tr>
    <td>$pageName</td>
    <td>$lastModifier</td>
    <td>$lastModifiedDate</td>
  </tr>
</table>

Thanks. This got me in the right way.

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Oct 24, 2018 in Confluence

Atlassian Research opportunity with Confluence templates

Do you use templates with Confluence? Take part in a remote 1-hr workshop. You'll receive USD $100 for your time!   We're looking for people to participate in a   remote 1-hr workshop...

1,089 views 17 14
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you