英文:
Retrieving 100 times more pages than what is actually available in the space
问题
我正在编写一些代码,以打印有关给定 Confluence 空间的一些信息,并且我想打印有关页面的一些信息。问题是该空间仅包含 403 个页面。
当我使用以下代码时,我得到了大约 100,000 行,这是错误的,因为我应该只得到 403 行。以下是我的代码。有人知道问题是什么吗?
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()
<details>
<summary>英文:</summary>
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][1]][1]
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]: https://i.stack.imgur.com/vBHa7.png
</details>
# 答案1
**得分**: 0
以下是您提供的代码的翻译部分:
```groovy
在循环内部的fiewriter导致了问题
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()
请注意,代码中的注释和变量名称保持不变。
英文:
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()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论