From version 21.1
edited by Clemens Robbenhaar
on 2018/11/11 21:38
To version 22.1
edited by Clemens Robbenhaar
on 2018/11/11 21:41
Change comment: Add notes for minor update to loop over subwikis, if present

Summary

Details

ExtensionCode.ExtensionClass[0]
Description
... ... @@ -1,4 +1,4 @@
1 -Replace all occurences of the old user with the new user for the selected fields of existing documents inside the current wiki.
1 +Replace all occurences of the old user with the new user for the selected fields of existing documents inside the current wiki. If this is used in the main wiki, one can optionally do the replacement in all subwikis, too.
2 2  
3 3  Based on the affected fields, this will also grant the affected documents the rights of the new user, instead of the old one's:
4 4  
... ... @@ -29,20 +29,30 @@
29 29  #set ($newUser = "$!request.newUser")
30 30  #set ($fields = $request.getParameterValues('field'))
31 31  #set ($fields = $fields.subList(0, $fields.size()))
32 +#set ($wantSubWikis = ("$!request.subwikis" != ""))
33 +#set($mainWikiId = $services.wiki.getMainWikiId())
34 +#set($currentWikiId = $services.wiki.getCurrentWikiId())
35 +#set($hasSubWikis = ($currentWikiId == $mainWikiId) && $services.wiki.getAllIds().size() > 1)
32 32  ##
33 33  #if ($oldUser != '' && $newUser != '' && "$!fields" != '')
34 - ## Build and run the query
35 - #set ($documentsToUpdateQuery = "SELECT doc.fullName, doc.author, doc.contentAuthor, doc.creator FROM Document doc WHERE ")
36 - #foreach ($field in $fields)
37 - #if ($foreach.count > 1)
38 - #set ($documentsToUpdateQuery = "${documentsToUpdateQuery} OR ")
38 + #set($documentsPerWikiToUpdate = {})
39 + #set($documentsToUpdate = [])
40 + #macro (getDocumentToUpdateFromWiki $oldUser $wikiId)
41 + ## Build and run the query
42 + #set ($documentsToUpdateQuery = "SELECT doc.fullName, doc.author, doc.contentAuthor, doc.creator FROM Document doc WHERE ")
43 + #foreach ($field in $fields)
44 + #if ($foreach.count > 1)
45 + #set ($documentsToUpdateQuery = "${documentsToUpdateQuery} OR ")
46 + #end
47 + #set ($documentsToUpdateQuery = "${documentsToUpdateQuery} doc.${field}=:oldUser")
39 39   #end
40 - #set ($documentsToUpdateQuery = "${documentsToUpdateQuery} doc.${field}=:oldUser")
49 + #set ($documentsToUpdateQuery = "${documentsToUpdateQuery} ORDER BY doc.fullName ASC")
50 + #set($documentsToUpdate = $services.query.xwql($documentsToUpdateQuery).setWiki($wikiId).bindValue('oldUser', $oldUser).execute())
51 + #set($discard = $documentsPerWikiToUpdate.put($wikiId, $documentsToUpdate))
52 + #set($docCount = $docCount + $documentsToUpdate.size())
41 41   #end
42 - #set ($documentsToUpdateQuery = "${documentsToUpdateQuery} ORDER BY doc.fullName ASC")
43 - #set ($documentsToUpdate = $services.query.xwql($documentsToUpdateQuery).bindValue('oldUser', $oldUser).execute())
44 44   ##
45 - #macro (getMatchedFields $queryResultRow)
55 + #macro (getMatchedFields $queryResultRow $oldUser)
46 46   #set ($matchedFields = [])
47 47   #if ($queryResultRow[1] == $oldUser && $fields.contains('author'))
48 48   #set ($discard = $matchedFields.add('author'))
... ... @@ -55,29 +55,54 @@
55 55   #end
56 56   #end
57 57   ##
68 + #set($docCount = 0)
69 + #if($hasSubWikis && $wantSubWikis)
70 + #foreach($wikiId in $services.wiki.getAllIds())
71 + #if($wikiId == $mainWikiId)
72 + #getDocumentToUpdateFromWiki($oldUser $wikiId)
73 + #else
74 + #getDocumentToUpdateFromWiki("${mainWikiId}:$oldUser" $wikiId)
75 + #end
76 + #set($docCount = $docCount + $documentsToUpdate.size())
77 + #end
78 + #else
79 + #getDocumentToUpdateFromWiki($oldUser $currentWikiId)
80 + #end
58 58   #set ($newUserReference = $services.model.resolveDocument($newUser))
59 59   ##
60 60   #if ($request.confirm == 'true')
61 - #foreach ($documentToUpdate in $documentsToUpdate)
62 - #getMatchedFields($documentToUpdate)
63 - ##
64 - #set ($document = $xwiki.getDocument($documentToUpdate[0]))
65 - #set ($protectedDocument = $document.document)
66 - ## Update the requested document user fields with the new user reference.
67 - #if ($fields.contains('author'))
68 - #set ($discard = $protectedDocument.setAuthorReference($newUserReference))
84 + #foreach($wikiId in $documentsPerWikiToUpdate.keySet())
85 + #set($documentsToUpdate = $documentsPerWikiToUpdate.get($wikiId))
86 + #set($prefixUser = "")
87 + #set($prefixPage = "")
88 + #if($hasSubWikis && $wikiId != $mainWikiId)
89 + #set($prefixUser = "${mainWikiId}:")
90 + #set($prefixPage = "${wikiId}:")
91 + #set($discard = $xcontext.setDatabase($wikiId))
69 69   #end
70 - #if ($fields.contains('contentAuthor'))
71 - #set ($discard = $protectedDocument.setContentAuthorReference($newUserReference))
93 + #foreach ($documentToUpdate in $documentsToUpdate)
94 + #getMatchedFields($documentToUpdate "${prefixUser}$oldUser")
95 + ##
96 + #set ($document = $xwiki.getDocument($documentToUpdate[0]))
97 + #set ($protectedDocument = $document.document)
98 + ## Update the requested document user fields with the new user reference.
99 + #if ($fields.contains('author'))
100 + #set ($discard = $protectedDocument.setAuthorReference($newUserReference))
101 + #end
102 + #if ($fields.contains('contentAuthor'))
103 + #set ($discard = $protectedDocument.setContentAuthorReference($newUserReference))
104 + #end
105 + #if ($fields.contains('creator'))
106 + #set ($discard = $protectedDocument.setCreatorReference($newUserReference))
107 + #end
108 + ## Save the changes
109 + #set ($discard = $xwiki.xWiki.saveDocument($protectedDocument, "Replaced user [$oldUser] in fields $matchedFields to user [$newUser]", $xcontext.context))
110 + * [[doc:${prefixPage}$documentToUpdate[0]]] (${prefixPage}$documentToUpdate[0]) fields $matchedFields - (/)
72 72   #end
73 - #if ($fields.contains('creator'))
74 - #set ($discard = $protectedDocument.setCreatorReference($newUserReference))
112 + #if($hasSubWikis && $wikiId != $mainWikiId)
113 + #set($discard = $xcontext.setDatabase($currentWikiId))
75 75   #end
76 - ## Save the changes
77 - #set ($discard = $xwiki.xWiki.saveDocument($protectedDocument, "Replaced user [$oldUser] in fields $matchedFields to user [$newUser]", $xcontext.context))
78 - * [[$documentToUpdate[0]]] ($documentToUpdate[0]) fields $matchedFields - (/)
79 79   #end
80 -
81 81   Done.
82 82  
83 83   {{html}}
... ... @@ -84,10 +84,19 @@
84 84   <a class='button' href="$doc.getURL()">Back to main screen</a>
85 85   {{/html}}
86 86   #else
87 - Documents to update ($documentsToUpdate.size() total) from [[doc:$oldUser]] ($oldUser) to [[doc:$newUserReference]] ($newUser) for fields $fields:
88 - #foreach ($documentToUpdate in $documentsToUpdate)
89 - #getMatchedFields($documentToUpdate)
90 - * [[$documentToUpdate[0]]] ($documentToUpdate[0]) fields $matchedFields
122 + Documents to update ($docCount total) from [[doc:$oldUser]] ($oldUser) to [[doc:$newUserReference]] ($newUser) for fields $fields:
123 + #foreach($wikiId in $documentsPerWikiToUpdate.keySet())
124 + #set($documentsToUpdate = $documentsPerWikiToUpdate.get($wikiId))
125 + #set($prefix = "")
126 + #set($prefixUser = "")
127 + #if($hasSubWikis && $wikiId != $mainWikiId)
128 + #set($prefix = "${wikiId}:")
129 + #set($prefixUser = "${mainWikiId}:")
130 + #end
131 + #foreach ($documentToUpdate in $documentsToUpdate)
132 + #getMatchedFields($documentToUpdate "$prefixUser$oldUser")
133 + * [[${prefix}$documentToUpdate[0]]] (${prefix}$documentToUpdate[0]) fields $matchedFields
134 + #end
91 91   #end
92 92  
93 93   {{html}}
... ... @@ -99,8 +99,9 @@
99 99   #foreach ($field in $fields)
100 100   <input type='hidden' name='field' value="$escapetool.xml($field)" />
101 101   #end
146 + #if($wantSubWikis)<input type='hidden' name='subwikis' value='y' />#end
102 102   <input type='hidden' name='confirm' value='true' />
103 - #if ($documentsToUpdate.size() > 0)
148 + #if ($docCount > 0)
104 104   <input class='button' type='submit' value='Confirm' />
105 105   #end
106 106   <a class='button secondary' href="$doc.getURL()">Cancel</a>
... ... @@ -135,6 +135,12 @@
135 135   <label><input type='checkbox' name='field' value='creator' #if ($fields.contains('creator'))checked="checked"#end />creator</label>
136 136   </dd>
137 137  
183 +#if($hasSubWikis)
184 + <dt><label>Include Subwikis</label></dt>
185 + <dd>
186 + <label for='subwikis'><input type='checkbox' name='subwikis' id='subwikis' value='yes' #if ($wantSubWikis)checked="checked"#end />with subwikis</label>
187 + </dd>
188 +#end
138 138   <dt><label>New user to replace with</label></dt>
139 139   <dd><input class='suggestUsers' type='text' name='newUser' #if ($newUser != '')value="$escapetool.xml($newUser)"#{else}value="$escapetool.xml(${xcontext.user})"#end /></dd>
140 140   </dl>
Summary
... ... @@ -1,1 +1,1 @@
1 -Replace all occurences of the old user with the new user for the selected fields of existing documents inside the current wiki.
1 +Replace all occurences of the old user with the new user for the selected fields of existing documents.
ExtensionCode.ExtensionVersionClass[4]
Release Notes
... ... @@ -1,1 +1,1 @@
1 -* Allow to change the user in subwikis, too
1 +* Allow to change the user in subwikis, too.

Get Connected