0 Votes

Changes for page Script

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

From version 7.1
edited by Ryan C
on 2025/05/13 16:01
Change comment: There is no comment for this version
To version 1.1
edited by Ryan C
on 2025/05/12 18:20
Change comment: There is no comment for this version

Summary

Details

Page properties
Content
... ... @@ -1,30 +1,18 @@
1 1  {{groovy}}
2 -import org.xwiki.model.reference.DocumentReference
2 +def mainGroupDoc = xwiki.getDocument("XWiki.XWikiAllGroup")
3 +def groupClassRef = "XWiki.XWikiGroups"
3 3  
4 -def groupDocRef = new DocumentReference("xwiki", "XWiki", "XWikiAllGroup")
5 -def groupClassName = "XWiki.XWikiGroups"
6 -
7 -// 1. Ensure the group document exists
8 -def groupDocAPI = xwiki.getDocument(groupDocRef)
9 -def groupDoc = groupDocAPI.getDocument()
10 -
11 -if (groupDoc.isNew()) {
12 - def apiDoc = xwiki.getDocument(groupDoc.documentReference)
13 - xwiki.saveDocument(apiDoc, "Created XWikiAllGroup page")
14 - println "🆕 Created XWiki.XWikiAllGroup page"
15 -}
16 -
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  
21 21  allUsers.each { userDocName ->
22 - def alreadyMember = groupDoc.getXObjects(groupClassName)?.any {
9 + def userDoc = xwiki.getDocument(userDocName)
10 + def alreadyMember = mainGroupDoc.xObjects(groupClassRef)?.any {
23 23   it?.getStringValue("member") == userDocName
24 24   }
25 25  
26 26   if (!alreadyMember) {
27 - def newObj = groupDoc.newXObject(groupClassName)
15 + def newObj = mainGroupDoc.newObject(groupClassRef)
28 28   newObj.setStringValue("member", userDocName)
29 29   added << userDocName
30 30   }
... ... @@ -31,8 +31,7 @@
31 31  }
32 32  
33 33  if (added) {
34 - def wrappedDoc = xwiki.getDocument(groupDoc.documentReference)
35 - xwiki.saveDocument(wrappedDoc, "✅ Added users to XWikiAllGroup: ${added.join(', ')}")
22 + xwiki.saveDocument(mainGroupDoc, "Added missing users to XWikiAllGroup: ${added.join(', ')}")
36 36   println "✅ Added users to main wiki: ${added.join(', ')}"
37 37  } else {
38 38   println "✅ All users are already members of the main wiki."