Tagging through a lot of pages

Hello,

I have a whole structure representing customer documentation for some 120 customers.
I want to add tags according to customer name to each page and some subpages get subpage-specific tags. (this “list of clients”, “faq”, “list of servers”)
This is to aid the support department in quickly finding documentation.

I have read this thread and started rummaging through various documentation to velocity:

I’d like to have some script that will check the page structure and only add tags if they are missing; it should also not remove any hand-added tags.
I’ve got something that will do something, but it’s getting more complex, as I will need to check a list of “must have” tags against the list of “currently there” tags.

To a point, $str.split() return an array of java.lang.string that I don’t know how to work with and don’t know if I can convert this to a java.util.ArrayList.

Finding documentation for this … is difficult for me as I basically jumped into cold water a few days ago.
I am now questioning my life choices along with the question if I might better look into groovy.

Has anyone some really helpful hints on how to better grasp the basics and maybe on how to fix my above issue?

Hello,

You could use the .getTagList() method on Documents, which returns an ArrayList.

From stackoverflow: https://stackoverflow.com/a/18596568

you can call ArrayList functions (.size(), .get(), use it in #foreach) on java arrays in velocity

There’s also this page for variables/functions available in velocity (important ones include $doc, $xwiki and various tools like $escapetool, $collectiontool …), which might be of help:
https://www.xwiki.org/xwiki/bin/view/ScriptingDocumentation
(you might need to refresh it with Ctrl+F5 a few times to work)

You can also check out the snippet wiki (like this page: https://snippets.xwiki.org/xwiki/bin/view/Extension/Create%20Pages%20With%20Tags).

Here is an example for editing the tags of a single document:

{{velocity}}
## Get the document you want.
#set ($document = $xwiki.getDocument('Sandbox.WebHome'))

## Page structure logic here.
#if ($document.getSections().size() > 1)
  #set ($expectedTags = ['a', 'b', 'd'])
##elseif other cases
#end

## Build new tag list by adding missing expected tags.
#set ($docTags = $document.getTagList())
#foreach ($expectedTag in $expectedTags)
  #if (!$docTags.contains($expectedTag))
    #set ($discard = $docTags.add($expectedTag))
  #end
#end

## Save changes (taken from snippets wiki)
#set ($aTags = $document.getObject("XWiki.TagClass"))
#if (!$aTags)
  #set ($aTags = $document.newObject("XWiki.TagClass"))
#end
#set ($discard = $aTags.set("tags", $docTags))
#set ($discard = $document.save('Minor edit for adding tags', 1))
{{/velocity}}

I know it’s a late response, but if you still need help you can paste the code here.

1 Like

Thanks for your reply.
Yes I got something to work by letting ti go for some time to clear the knot in my head.
My original idea was to split the Asset-String returned from TagsObj.getTags() and try to add to that.
Which will not work as split() return an array of string, not an ArrayList.

So I worked around this by just not splitting.
I wanted to add Tags right now, so I could just foreach over the TagToAdd an check the tag-string with .contains().

And another thank you for your answer, because I did not know about $doc.getTagList()
Which seems to return an ArrayList, if I read your code-example right.