confluenceconfluence-rest-apiscriptrunner-for-jira

Retrieving 100 times more pages than what is actually available in the space


I am writing some code to print some information about a given confluence space and I would like to print some information about the pages. The problem is that the space only contains 403 pages.

enter image description here

When I use the following code, I get around 100 000 rows which is wrong since I should only be getting 403 rows. Here is my code. Anyone knows the problem? 

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=""

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()

Solution

  • The fiewriter was inside the loop which was causing the problem

         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()