Velocity Test: XWiki Document tree

Last modified by XWikiGuest on 2021/05/03 08:19

Background

In XWiki, documents are organized in spaces (the concept is somewhat similar to the directories on a file system; note that at this moment we cannot define spaces inside spaces, each space is top-level and contains only documents). The XWiki documents are identified by their fullName (composed of "<Space>.<PageName>") which is unique in the wiki. Each space should have a "WebHome" document (identified by "<Space>.WebHome").

A document hierarchy is defined in XWiki via the parent field of the document object. The parent field is normally the identifier (fullName) of another document in the wiki; note that it can be undefined (empty), and it may be invalid (pointing to a non-existing document).


Task
Write a velocity macro that displays all the documents in the wiki as a tree, where the hierarchy defined by the parent field. The documents whose parent field is undefined or invalid should be treated as follows:

  • they are direct children of the "WebHome" document in their space if that document exists
  • they are top-level nodes in the tree if they are the space's "WebHome" (their name is "<Space>.WebHome") or if the "WebHome" is missing

Edit code

Resources:

Sample output (please display for each item its fullName and parent for easier verification of the result):

<ul>
 <li>Main.WebHome (parent: )
    <ul>
       <li>Main.Dashboard (parent: Main.WebHome)</li>
       <li>Main.MyNewPage (parent: )
         <ul>
           <li>Main.MyChildPage (parent: Main.MyNewPage)</li>
         </ul>
       </li>
       <li>Main.MyOrphanPage (parent: Main.NonExistingDoc)</li>
        ...
    </ul>
 </li>
 <li>...</li>
</ul>