Is it possible to add a specific tag to all children of a certain page without doing it page by page?
Yes, it is possible to write a script that iterates over all children pages, add an XWiki.TagClass
object and save the page.
Hope it helps,
Alex
And how would that script look like?
I tried several options to code a script for tagging page children. The problem is, that these methods do not find any page children.
I tried:
{{velocity}}
#set($pageChildren = $xwiki.getDocument($doc.fullName).getChildren())
#foreach($item in $pageChildren)
#set($childDoc = $xwiki.getDocument($item))
#if(!$childDoc.isNew())
#set($tags = $childDoc.getTags())
#if(!$tags.contains("Example"))
#set($discard = $tags.add("Example"))
$childDoc.setTags($tags)
$childDoc.save("Added tag 'Example'")
#end
#end
#end
{{/velocity}}
The result was 0 child pages instead of 10 (listet as children in the breadcrumbs menu).
Using pageManager did not work either:
#set($pageManager = $services.page)
#set($pageChildren = $pageManager.getChildren($currentDocument.documentReference, true))
I also tried to use document references with the same result:
#set($pageChildren = $currentDocument.getChildrenReferences())
Any ideas?
$xwiki.getDocument($doc.fullName)
is actually $doc
So you need to iterate over $doc.children
/ $doc.getChildren()
.
Then try check at each like of code what is the data you are working with., by printing the variable like $item
or $childDoc
.
$doc.getTags()
returns a String
adn you cannot perform add
operations on it.
You need something like this:
{{velocity}}
#foreach($item in $doc.children)
#if ($foreach.index < 2)
#set($childDoc = $xwiki.getDocument($item))
#if(!$childDoc.isNew())
#set ($tagsObj = $childDoc.getObject('XWiki.TagClass'))
#set ($tags = $tagsObj.tags)
#if(!$tags.contains("Example"))
#set ($tags = "${tags}|Example")
#set ($discard = $tagsObj.set('tags', $tags))
#set ($discard = $childDoc.save("Added tag 'Example'"))
#end
#end
#end
#end
{{/velocity}}
Thanks @acotiuga for your help. The script only worked for pages that have or already had at least one tag. For all other pages the initial object of type TagClass was missing. I changed that to:
{{velocity}}
#foreach($item in $doc.children)
## Workaround for XWIKI-22358: #foreach($item in $services.query.xwql("where doc.space like '${doc.space}.%'").execute())
#if ($foreach.index < 2)
#set($childDoc = $xwiki.getDocument($item))
#if(!$childDoc.isNew())
#set ($tagsObj = $childDoc.getObject('XWiki.TagClass'))
#if(!$tagsObj) ## create a TagClass object if there is none
#set ($tagsObj = $childDoc.newObject('XWiki.TagClass'))
$childDoc.save()
#end
#set ($tags = $tagsObj.tags)
#if(!$tags.contains("Example"))
#set ($tags = "${tags}|Example")
#set ($discard = $tagsObj.set('tags', $tags))
#set ($discard = $childDoc.save("Added tag 'Example'"))
#end
#end
#end
#end
{{/velocity}}
But this doesn’t work for child pages that have been created with the Office importer. Somehow these sub pages are no child pages.
Indeed, the documents imported with the Office importer are considered children.
You can still get all the pages, including the ones imported with office by using $services.query.xwql("where doc.space like '${doc.space}.%'").execute()
The same issue occurs for pages imported via Confluence XML and when creating a page through the dialog that shows up for a non-existent page:
The requested page could not be found.
You can edit this page to create it.
As a workaround, this issue can be fixed by renaming a page (tree) and then renaming it back using the Move/Rename dialog.