检索的页面数量比实际可用的空间多100倍。

huangapple go评论58阅读模式
英文:

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?&#160;


    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(&#39;D:/confluence/data/scripts/result.groovy&#39;)
    
    def fileWriter = new FileWriter(file) 
    
    Space space = spaceManager.getSpace(&quot;IWIKI&quot;)
    
    String result=&quot;&quot;
    
    for (Page page : pageManager.getPages(space, false)) {
    
    &#160; &#160; if(page.getCreator()==null){
    
    &#160; &#160; &#160; &#160; &#160; &#160; result=result+page.toString()+&quot;,null&quot;+&quot;\n&quot;
    
    &#160; &#160; }
    
    &#160; &#160; else{
    
    &#160; &#160; &#160; &#160; &#160; &#160; String userID=page.getCreator().getName()
    
    &#160; &#160; &#160; &#160; &#160; &#160; String fullName =userAccessor.getUserByKey(page.getCreator().getKey()).getFullName()
    
    &#160; &#160; &#160; &#160; &#160; &#160; result=result+page.toString()+&quot;,&quot;+userID+&quot;,&quot;+fullName+&quot;,&quot;+page.getLastModificationDate()+&quot;\n&quot;
    
    &#160; &#160; }
    
    &#160; &#160;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(&#39;D:/confluence/data/scripts/result.groovy&#39;)
def fileWriter = new FileWriter(file) 

Space space = spaceManager.getSpace(&quot;IWIKI&quot;)
String result=&quot;&quot;
result=result+&quot;PageName;UserID;FullName;LastModificationDate\n&quot;
for (Page page : pageManager.getPages(space, false)) {
    if(page.getCreator()==null){
            result=result+page.toString()+&quot;;null&quot;+&quot;\n&quot;
    }
    else{
            String userID=page.getCreator().getName()
            String fullName =userAccessor.getUserByKey(page.getCreator().getKey()).getFullName()
            result=result+page.toString()+&quot;;&quot;+userID+&quot;;&quot;+fullName+&quot;;&quot;+page.getLastModificationDate()+&quot;\n&quot;

    }

}
fileWriter.write(result)

fileWriter.close()

huangapple
  • 本文由 发表于 2023年7月3日 21:19:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76605152.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定