Forums

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

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

Mouna Hammoudi
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.
July 3, 2023

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.

Capture.PNG

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

1 answer

1 accepted

1 vote
Answer accepted
Markus Fredén
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.
July 3, 2023

Hi @Mouna Hammoudi ,

The issue in your code lies in the placement of the fileWriter.write(result) statement.

You are currently writing the result to the file inside the loop for each page, which accumulates the result string with each iteration.

To fix this, move the fileWriter.write(result) statement outside the loop so that it writes the final result after all the pages have been processed.

Here's the modified code:

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

Now, the fileWriter.write(result) statement will only be executed once, after processing all the pages in the loop. This will ensure that the result is written to the file correctly, and you should get the expected output with 403 rows instead of 100,000.

/Markus

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events