Search code examples
confluenceconfluence-rest-apiscriptrunner-for-jiraconfluence-macros

Code using the confluence API to display statistics about the pages in 1 given space


I would like to write some code in the scriptrunner console to display some statistics about the pages in a given confluence space. I would like to display the current info

"PageName;UserPageCreatorID;UserPageCreatorFullName;LastModificationDate\n"

How can I do this?


Solution

  • import com.atlassian.confluence.pages.Page
    import com.atlassian.confluence.pages.PageManager
    import com.atlassian.confluence.spaces.Space
    import com.atlassian.confluence.spaces.SpaceManager
    import com.atlassian.sal.api.component.ComponentLocator
    import org.apache.log4j.Logger
    import com.atlassian.confluence.user.UserAccessor
    import com.atlassian.sal.api.user.UserKey
    
    SpaceManager spaceManager = ComponentLocator.getComponent(SpaceManager)
    PageManager pageManager = ComponentLocator.getComponent(PageManager)
    UserAccessor userAccessor = ComponentLocator.getComponent(UserAccessor)
    
    def file = new File('D:/confluence/data/scripts/result.groovy')
    def fileWriter = new FileWriter(file) 
    
    Space space = spaceManager.getSpace("IWIKI")
    String result=""
    result=result+"PageName;UserID;FullName;LastModificationDate\n"
    for (Page page : pageManager.getPages(space, false)) {
        if(page.getCreator()==null){
                result=result+page.toString()+";null"+"\n"
        }
        else{
                String userID=page.getCreator().getName()
                String fullName =userAccessor.getUserByKey(page.getCreator().getKey()).getFullName()
                result=result+page.toString()+";"+userID+";"+fullName+";"+page.getLastModificationDate()+"\n"
    
        }
    
    }
    fileWriter.write(result)
    
    fileWriter.close()