0 Votes

Changes for page Script

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

From version 2.1
edited by Ryan C
on 2025/05/12 18:22
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,17 +1,30 @@
1 1  {{groovy}}
2 -def mainGroupDoc = xwiki.getDocument("XWiki.XWikiAllGroup")
3 -def groupClassRef = "XWiki.XWikiGroups"
2 +import org.xwiki.model.reference.DocumentReference
4 4  
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
5 5  def allUsers = xwiki.searchDocuments("where doc.fullName like 'XWiki.%' and doc.fullName != 'XWiki.XWikiAllGroup'")
6 6  def added = []
7 7  
8 8  allUsers.each { userDocName ->
9 - def userDoc = xwiki.getDocument(userDocName)
10 - def existing = mainGroupDoc.getXObjects(groupClassRef)
11 - def alreadyMember = existing?.any { it?.getStringValue("member") == userDocName }
22 + def alreadyMember = groupDoc.getXObjects(groupClassName)?.any {
23 + it?.getStringValue("member") == userDocName
24 + }
12 12  
13 13   if (!alreadyMember) {
14 - def newObj = mainGroupDoc.newXObject(groupClassRef)
27 + def newObj = groupDoc.newXObject(groupClassName)
15 15   newObj.setStringValue("member", userDocName)
16 16   added << userDocName
17 17   }
... ... @@ -18,7 +18,8 @@
18 18  }
19 19  
20 20  if (added) {
21 - xwiki.saveDocument(mainGroupDoc, "Added missing users to XWikiAllGroup: ${added.join(', ')}")
34 + def wrappedDoc = xwiki.getDocument(groupDoc.documentReference)
35 + xwiki.saveDocument(wrappedDoc, "✅ Added users to XWikiAllGroup: ${added.join(', ')}")
22 22   println "✅ Added users to main wiki: ${added.join(', ')}"
23 23  } else {
24 24   println "✅ All users are already members of the main wiki."