Show Children by Tag
| Displays Child (descendent) pages of the current page that have a given tag |
| Type | |
| Category | Macro |
| Developed by | |
| Rating | |
| License | GNU Lesser General Public License 2.1 |
Table of contents
Description
Displays a page tree showing the children of the current page (including the current page, if appropriate) that have a particular tag. This only iterates through all descendents of the current page (or specified root page).
The root parameter isn't just optional for the user: if you choose not to include a root parameter in your macro definition at all, the script will continue to work: if there isn't a parameter named "root", it'll pick the current page as the ancestor from which to search.
Documents are sorted by title, which unlike doc.fullName does not include "webHome".
See How sort children pages tree by tag? - Help / Discuss - XWiki Forum
Script
Multiple labels
This version supports multiple lables, separated by commas. It will display all descendant pages that have any or all of the specified labels, depending on the value of the requireAll parameter. This is the recommended version of the macro to use.
Virtually all of the heavy lifting is done by the database engine via XWQL, so this all runs pretty quickly, even with a large wiki and many documents and tags.
{{velocity}}
#set ($tags = $wikimacro.parameters.tags)
#set ($tags=$tags.replaceAll("'","''")) ## double any single quotes to mitigate against sql injection
#set ($tagsSplit=$tags.split(","))
#if ($wikimacro.parameters.requireAll)
#set ($operator="and")
#else
#set ($operator="or")
#end ##if requireAll
#if (!$xwiki.getDocument($wikimacro.parameters.root))
#set ($docroot=$doc)
#else
#set ($docroot=$xwiki.getDocument($wikimacro.parameters.root))
#end
#set ($parentRef = $docroot.getSpace())
#set ($descendants=[])
#set ($query = "select doc.fullName from Document doc, doc.object(XWiki.TagClass) as obj where doc.fullName like '$parentRef.%' and (")
#set ($tagsArray=$tags.split(","))
#set ($countTags=0)
#foreach ($tag in $tagsArray)
#set ($tag=$tag.trim())
#if ($countTags > 0 )
#set ($query="$query $operator ") ## we add our "and" or "or" for subsequent tags
#end ## if
#set ($query="$query '$tag' member of obj.tags")
#set ($countTags =$countTags+1)
#end ## foreach tag
#set ($query="$query ) order by doc.title")
#set ($descendants=$services.query.xwql($query).execute())
## #set($dummy=$descendants.addAll($queryResults)) ## here we add the results from the sql query to the descendants var
#foreach ($descendant in $descendants) ## here we eliminate duplicated pages
* [[$descendant]]
#end ## foreach descendant
{{/velocity}}Single label only
This version only supports a single label in the parameter.
{{velocity}}
#set ($tag = $wikimacro.parameters.tag)
#if (!$xwiki.getDocument($wikimacro.parameters.root))
#set ($docroot=$doc)
#else
#set ($docroot=$xwiki.getDocument($wikimacro.parameters.root))
#end
#set ($parentRef = $docroot.getSpace())
#set ($query = "select doc.fullName from Document doc, doc.object(XWiki.TagClass) as obj where doc.fullName like :parent and :tag member of obj.tags order by doc.title")
#set ($descendants = $services.query.xwql($query).bindValue("parent", $parentRef + '\.%').bindValue("tag",$tag).execute())
#foreach ($descendant in $descendants)
* [[$descendant]]
#end
{{/velocity}}Parameters
- Tag(s) = free text string, mandatory.
- Root = org.xwiki.model.reference.DocumentReference, optional. Document reference string for requisite root of the search. Default: current page
- requireAll = java.lang.Boolean, optional. Check if you want to show pages that have all of the tags instead of just one of them. Default: false
Show all pages in the entire wiki with a particular tag
{{velocity}}
#foreach ($docReference in $xwiki.tag.getDocumentsWithTag('tagname'))
* [[$docReference]]
#end
{{/velocity}}