page_whiteLists all children documents of the currently browsed document
Developed by

Uwe Stöhr, Clemens Fuchslocher

Rating
Rate!
0 Votes
LicenseGNU Lesser General Public License 2.1

Description

Starting with XWiki 7.2 you should use the Document Tree Macro instead.

It is an alternative to the Children Page Tree method that doesn't require to install a plugin. The result is exactly the same as for the Children Page Tree:

  • ChildPage1
    • ChildPage2
    • ChildPage3
      • ChildPage4
    • ChildPage5
      • ChildPage6
      • ChildPage7
  • ChildPage8

The following scripts handle two cases:

  1. the specified root page is not a space home page (not a WebHome page; starting with XWiki 7.2 these pages are also known as terminal pages) -> the script looks for child pages anywhere in the wiki
  2. the specified root page is a space home page (a WebHome page; starting with XWiki 7.2 these pages are also known as nested pages) -> the script looks for child pages only in that space (excluding the subspaces!). If you want to look in subspaces too, then the script needs to be modified, but this wasn't it's initial intent. For this, if you have XWiki 7.2+, we recommend switching to the Document Tree Macro.

For XWiki Syntax 2.0

{{velocity}}
#macro(createDocumentTree $document $depth $level)
 #if ($depth > 0)
   #set ($translated = $document.getTranslatedDocument())
   #set ($name = $translated.getName())
   #set ($fullName = $translated.getFullName())
   #set ($prefixedFullName = $translated.getPrefixedFullName())
   #set ($where = "where doc.parent = ? or doc.parent = ? or doc.parent = ? order by doc.name")
   #set ($parameters = [$name, $fullName, $prefixedFullName])
   #if ($name == 'WebHome')
     #set ($space = $document.getSpace())
     #set ($where = "where doc.space = ? and (doc.parent = '' or doc.parent = ? or doc.parent = ? or doc.parent = ?) and doc.name != 'WebHome' order by doc.name")
     #set ($parameters = [$space, $name, $fullName, $prefixedFullName])
   #end
   #foreach ($name in $xwiki.searchDocuments($where, $parameters))
     #if ($xwiki.getDocument($name))
       #set ($document = $xwiki.getDocument($name))
       #set ($translated = $document.getTranslatedDocument())
       #set ($prefixed = $translated.getPrefixedFullName())
       #set ($title = $translated.getPlainTitle())
        ${stringtool.repeat('*', $level)} [[$title>>$prefixed]]
       #set ($depth = $depth - 1)
       #set ($level = $level + 1)
       #createDocumentTree($document $depth $level)
       #set ($depth = $depth + 1)
       #set ($level = $level - 1)
     #end
   #end
 #end
#end
#createDocumentTree($doc 5 1)
{{/velocity}}

For XWiki Syntax 1.0

#macro(createDocumentTree $document $depth $level)
 #if ($depth > 0)
   #set ($translated = $document.getTranslatedDocument())
   #set ($name = $translated.getName())
   #set ($fullName = $translated.getFullName())
   #set ($prefixedFullName = $translated.getPrefixedFullName())
   #set ($where = "where doc.parent = ? or doc.parent = ? or doc.parent = ? order by doc.name")
   #set ($parameters = [$name, $fullName, $prefixedFullName])
   #if ($name == 'WebHome')
     #set ($space = $document.getSpace())
     #set ($where = "where doc.space = ? and (doc.parent = '' or doc.parent = ? or doc.parent = ? or doc.parent = ?) and doc.name != 'WebHome' order by doc.name")
     #set ($parameters = [$space, $name, $fullName, $prefixedFullName])
   #end
   #foreach ($name in $xwiki.searchDocuments($where, $parameters))
     #if ($xwiki.getDocument($name))
       #set ($document = $xwiki.getDocument($name))
       #set ($translated = $document.getTranslatedDocument())
       #set ($prefixed = $translated.getPrefixedFullName())
       #set ($title = $translated.getPlainTitle())
        ${stringtool.repeat('*', $level)} [$title>$prefixed]
       #set ($depth = $depth - 1)
       #set ($level = $level + 1)
       #createDocumentTree($document $depth $level)
       #set ($depth = $depth + 1)
       #set ($level = $level - 1)
     #end
   #end
 #end
#end
#createDocumentTree($doc 5 1)

Groovy version for XWiki Syntax 2.0

{{groovy}}
def createDocumentTree(document, depth = 5, level = 1) {
if (depth == 0) {
return
}

def translated = document?.getTranslatedDocument()
def name = translated?.getName()
def fullname = translated?.getFullName()
def prefixed = translated?.getPrefixedFullName()
def title = translated?.getDisplayTitle()

def where = "where doc.parent = ? or doc.parent = ? or doc.parent = ? order by doc.name"
def parameters = [name, fullName, prefixedFullName]
if (name == "WebHome") {
def space = document.getSpace()
where = "where doc.space = ? and (doc.parent = '' or doc.parent = ? or doc.parent = ? or doc.parent = ?) and doc.name != 'WebHome' order by doc.name"
parameters = [space, name, fullName, prefixedFullName]
}

def children = xwiki.searchDocuments(where)
children.each { child ->
document = xwiki.getDocument(child)
if (document == null) {
return
}

translated = document.getTranslatedDocument()
prefixed = translated?.getPrefixedFullName()
title = translated?.getDisplayTitle()

println "*" * level + " [[${title}>>${prefixed}]]"

createDocumentTree(document, depth - 1, level + 1)
}
}

createDocumentTree(doc)
{{/groovy}}

Tested with

  • XWiki 4.3
  • XWiki 2.6
  • XWiki 2.2.2
  • XWiki 2.1.1
  • XWiki 2.0.5
  • XWiki 1.9.4

Prerequisites & Installation Instructions

Copy the code snippet to a page and save it.

Created by Uwe Stöhr on 2009/10/20 15:29
     

Get Connected