0 Votes

Wiki source code of Script

Version 11.1 by Ryan C on 2025/05/13 16:04

Hide last authors
Ryan C 7.1 1 {{groovy}}
2 import org.xwiki.model.reference.DocumentReference
Ryan C 1.1 3
Ryan C 8.1 4 // References
Ryan C 7.1 5 def groupDocRef = new DocumentReference("xwiki", "XWiki", "XWikiAllGroup")
Ryan C 11.1 6 def groupClassName = "XWiki.XWikiGroups" as String // Force string type!
Ryan C 8.1 7
8 // Load or create the group page
9 def groupDocAPI = xwiki.getDocument(groupDocRef)
10 def groupDoc = groupDocAPI.getDocument()
11
12 if (groupDoc.isNew()) {
Ryan C 9.1 13 xwiki.saveDocument(groupDocAPI, "Created XWikiAllGroup")
14 println "Created XWiki.XWikiAllGroup"
Ryan C 8.1 15 }
16
17 // Add missing users to group
18 def allUsers = xwiki.searchDocuments("where doc.fullName like 'XWiki.%' and doc.fullName != 'XWiki.XWikiAllGroup'")
19 def added = []
20
21 allUsers.each { userDocName ->
Ryan C 10.1 22 def alreadyMember = groupDoc.getXObjects(groupClassName)?.any {
Ryan C 8.1 23 it?.getStringValue("member") == userDocName
24 }
25
26 if (!alreadyMember) {
Ryan C 11.1 27 def obj = groupDoc.newXObject(groupClassName)
Ryan C 8.1 28 obj.setStringValue("member", userDocName)
29 added << userDocName
30 }
31 }
32
Ryan C 11.1 33 // Save if needed
Ryan C 8.1 34 if (added) {
Ryan C 10.1 35 def wrappedDoc = xwiki.getDocument(groupDoc.documentReference)
36 xwiki.saveDocument(wrappedDoc, "Added users to group: ${added.join(', ')}")
Ryan C 9.1 37 println "Added users: ${added.join(', ')}"
Ryan C 8.1 38 } else {
Ryan C 9.1 39 println "All users are already members of the main wiki."
Ryan C 7.1 40 }
41 {{/groovy}}
42