0 Votes

Changes for page Script

Last modified by Ryan C on 2025/05/13 16:08

From version 11.1
edited by Ryan C
on 2025/05/13 16:04
Change comment: There is no comment for this version
To version 7.1
edited by Ryan C
on 2025/05/13 16:01
Change comment: There is no comment for this version

Summary

Details

Page properties
Content
... ... @@ -1,20 +1,20 @@
1 1  {{groovy}}
2 2  import org.xwiki.model.reference.DocumentReference
3 3  
4 -// References
5 5  def groupDocRef = new DocumentReference("xwiki", "XWiki", "XWikiAllGroup")
6 -def groupClassName = "XWiki.XWikiGroups" as String // Force string type!
5 +def groupClassName = "XWiki.XWikiGroups"
7 7  
8 -// Load or create the group page
7 +// 1. Ensure the group document exists
9 9  def groupDocAPI = xwiki.getDocument(groupDocRef)
10 10  def groupDoc = groupDocAPI.getDocument()
11 11  
12 12  if (groupDoc.isNew()) {
13 - xwiki.saveDocument(groupDocAPI, "Created XWikiAllGroup")
14 - println "Created XWiki.XWikiAllGroup"
12 + def apiDoc = xwiki.getDocument(groupDoc.documentReference)
13 + xwiki.saveDocument(apiDoc, "Created XWikiAllGroup page")
14 + println "🆕 Created XWiki.XWikiAllGroup page"
15 15  }
16 16  
17 -// Add missing users to group
17 +// 2. Proceed as before
18 18  def allUsers = xwiki.searchDocuments("where doc.fullName like 'XWiki.%' and doc.fullName != 'XWiki.XWikiAllGroup'")
19 19  def added = []
20 20  
... ... @@ -24,19 +24,18 @@
24 24   }
25 25  
26 26   if (!alreadyMember) {
27 - def obj = groupDoc.newXObject(groupClassName)
28 - obj.setStringValue("member", userDocName)
27 + def newObj = groupDoc.newXObject(groupClassName)
28 + newObj.setStringValue("member", userDocName)
29 29   added << userDocName
30 30   }
31 31  }
32 32  
33 -// Save if needed
34 34  if (added) {
35 35   def wrappedDoc = xwiki.getDocument(groupDoc.documentReference)
36 - xwiki.saveDocument(wrappedDoc, "Added users to group: ${added.join(', ')}")
37 - println "Added users: ${added.join(', ')}"
35 + xwiki.saveDocument(wrappedDoc, "Added users to XWikiAllGroup: ${added.join(', ')}")
36 + println "Added users to main wiki: ${added.join(', ')}"
38 38  } else {
39 - println "All users are already members of the main wiki."
38 + println "All users are already members of the main wiki."
40 40  }
41 41  {{/groovy}}
42 42