Hey,
I’ve been fiddling with this for a bit and am unsure why it is returning the wrong results.
Here’s my dilemma:
I have a script in Velocity that parses all the spaces in my subwiki and returns a list of only the ones that are not hidden and existing. Works fine and provides exactly the result required.
My issue is that while creating the sitemap (dynamically) [see other post here], I figured out that writing to files will require me using Groovy as it seems to not be doable in Velocity (more for templates, etc.).
I currently have this snippet in Velocity working as intended:
{{velocity}}
#set($spaceList = $xwiki.getSpaces())
#set($num = 0)
#foreach($space in $spaceList)
#set($docuSpace = "${space}.WebHome")
#set($docu = $xwiki.getDocument($docuSpace))
#set($docExists = $xwiki.exists($docu))
#if(!$docu.isHidden())
#if($docExists)
#set($num = $num + 1)
* $num) $docu.getTitle() || $docu.getExternalURL()
#end
#end
#end
{{/velocity}}
I tried to use this in my loop in Groovy in the following way:
{{groovy}}
filename = 'sitemap.xml'
File file = new File("sitemap.xml")
content = ""
content += ''
spaces = xwiki.getSpaces()
for(space in spaces)
{
content += "<url>"
content += "<loc>"
//docuSpace = space + ".WebHome"
//docu = xwiki.getDocument(docuSpace)
docu = xwiki.getDocument(space)
//docExists = xwiki.exists(docu)
docHidden = docu.isHidden()
//content += docu.isHidden()
content += docHidden //xwiki.getDocument(space).isHidden()
content += " || "
content += docu
content += " || "
content += docu.isNew() //xwiki.getDocument(space).isNew()
content += "</loc>"
content += "</url>"
}
content += "</urlset>"
file.write content
println file.text
{{/groovy}}
My main issue is that while the loop works and does provide me with the spaces & etc. it cannot seem to report to me properly if something is hidden, existing, etc.
The line “docu.isHidden()” always return false (even for page where that would be true in Velocity) and the line “//docExists = xwiki.exists(docu)” causes an error:
Failed to execute the [groovy] macro. Cause: [No signature of method: com.xpn.xwiki.api.XWiki.exists() is applicable for argument types:
I feel like I am missing a critical (but very basic) piece of knowledge about Groovy to make this work. Any ideas / guidance would be much appreciated.
Sincerely,
P.-S.: I love XWiki and also thank you to the very helpful community!