0 Votes

Changes for page Script

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

From version 10.1
edited by Ryan C
on 2025/05/13 16:03
Change comment: There is no comment for this version
To version 13.1
edited by Ryan C
on 2025/05/13 16:08
Change comment: Changed the author of the page to fix programming rights

Summary

Details

Page properties
Content
... ... @@ -1,42 +1,62 @@
1 1  {{groovy}}
2 2  import org.xwiki.model.reference.DocumentReference
3 3  
4 -// References
5 -def groupDocRef = new DocumentReference("xwiki", "XWiki", "XWikiAllGroup")
6 -def groupClassName = "XWiki.XWikiGroups" // ⚠️ Must be a STRING
4 +// ------------------------------------------------------------------
5 +// 1. References
6 +// ------------------------------------------------------------------
7 +def groupDocRef = new DocumentReference('xwiki', 'XWiki', 'XWikiAllGroup')
8 +def groupClassRef = new DocumentReference('xwiki', 'XWiki', 'XWikiGroups')
7 7  
8 -// Load or create the group page
9 -def groupDocAPI = xwiki.getDocument(groupDocRef)
10 -def groupDoc = groupDocAPI.getDocument()
10 +// ------------------------------------------------------------------
11 +// 2. Load (or create) the XWikiAllGroup page
12 +// ------------------------------------------------------------------
13 +def groupDocAPI = xwiki.getDocument(groupDocRef) // Document API wrapper
14 +def groupDoc = groupDocAPI.getDocument() // Raw XWikiDocument
11 11  
12 12  if (groupDoc.isNew()) {
13 - xwiki.saveDocument(groupDocAPI, "Created XWikiAllGroup")
14 - println "Created XWiki.XWikiAllGroup"
17 + xwiki.saveDocument(groupDocAPI, 'Created XWikiAllGroup')
18 + println " Created XWiki.XWikiAllGroup"
15 15  }
16 16  
17 -// Add missing users to group
18 -def allUsers = xwiki.searchDocuments("where doc.fullName like 'XWiki.%' and doc.fullName != 'XWiki.XWikiAllGroup'")
21 +// ------------------------------------------------------------------
22 +// 3. Collect all users except the AllGroup page itself
23 +// ------------------------------------------------------------------
24 +def allUsers = xwiki.searchDocuments(
25 + "where doc.fullName like 'XWiki.%' and doc.fullName <> 'XWiki.XWikiAllGroup'"
26 +)
27 +
28 +// ------------------------------------------------------------------
29 +// 4. Add the missing users
30 +// ------------------------------------------------------------------
19 19  def added = []
20 20  
21 21  allUsers.each { userDocName ->
22 - def alreadyMember = groupDoc.getXObjects(groupClassName)?.any {
23 - it?.getStringValue("member") == userDocName
34 + // already in group?
35 + def alreadyMember = groupDoc.getXObjects(groupClassRef)?.any {
36 + it?.getStringValue('member') == userDocName
24 24   }
25 25  
26 26   if (!alreadyMember) {
27 - def obj = groupDoc.newXObject(groupClassName) // ✅ String only
28 - obj.setStringValue("member", userDocName)
29 - added << userDocName
40 + // Try to create XObject using the simplest method first
41 + try {
42 + // Use the string class name instead of the DocumentReference
43 + def obj = groupDoc.newXObject("XWiki.XWikiGroups", xcontext)
44 + obj.setStringValue('member', userDocName)
45 + added << userDocName
46 + } catch (Exception e) {
47 + println "Could not add user ${userDocName}: ${e.message}"
48 + }
30 30   }
31 31  }
32 32  
33 -// Save the document
52 +// ------------------------------------------------------------------
53 +// 5. Persist if we changed anything
54 +// ------------------------------------------------------------------
34 34  if (added) {
35 - def wrappedDoc = xwiki.getDocument(groupDoc.documentReference)
36 - xwiki.saveDocument(wrappedDoc, "Added users to group: ${added.join(', ')}")
37 - println "Added users: ${added.join(', ')}"
56 + xwiki.saveDocument(groupDocAPI, "Added users: ${added.join(', ')}")
57 + println " Added users to main wiki group: ${added.join(', ')}"
38 38  } else {
39 - println "All users are already members of the main wiki."
59 + println " All users are already members of XWikiAllGroup"
40 40  }
41 41  {{/groovy}}
42 42