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.
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()
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
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.