Script works in Velocity but not in Groovy (xwiki.exists() and others)


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:

#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))
      #set($num = $num + 1)
      * $num) $docu.getTitle() || $docu.getExternalURL()

I tried to use this in my loop in Groovy in the following way:

  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

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.


P.-S.: I love XWiki and also thank you to the very helpful community! :slight_smile:

Instead of:

You could write:

#set($docu = $xwiki.getDocument($docuSpace)) 
#if (!$doc.isNew())
  ## The doc exists

The reason it fails is because you’re passing a Document object to exists() but it accepts a String or a DocumentReference…

So if you want to use it, you’d write:

#set($docExists = $xwiki.exists($docuSpace))

But better use what I’ve shown above, it’s more efficient.


Very helpful info.

That being said, what about the part where all my calls in Groovy return the same value (no matter the space being checked) for stuff like .isHidden() ?

My main issue is with using the API in Groovy, not Velocity (everything works as needed in Velocity, but I need it to work in Groovy so I can write the file).

I also tried in my Groovy code calling isNew() from a document and I always get “true” whether that is the case or not (I have both cases in the list of 146+ spaces).

Haha… changed my code slightly to:

    docuSpace = space + ".WebHome"
    docu = xwiki.getDocument(docuSpace)
    docHidden = docu.isHidden()
    docExists = !docu.isNew()

Seems to work now as I expected.
Calling the docuSpace instead of space makes all the difference (has the “.WebHome” addition).

Thanks again for the help @vmassol! :slight_smile:

@scharette a big difference between Velocity and Groovy to keep in mind is that in XWiki Velocity we have a magic parameter converter which take care of trying to call the closest method by converting parameters from one type to another. In Groovy you don’t have that so you can’t make mistake.

1 Like

Ah! Good to know! I was unaware… :smiley: So, I feel what this means (indirectly) is that I should probably have a look at all my other velocity code for such situations and improve it!

Thanks again for the assistance!