Replace authors by a user with Admin rights

Last modified by Vincent Massol on 2026/06/02 17:54

cogResave documents by replacing last author with user who has admin rights
TypeSnippet
CategoryOther
Developed by

Nikita Petrenko

Rating
0 Votes
LicenseGNU Lesser General Public License 2.1

Description

This code is developed to perform a specific task and can only be executed by a user with admin rights. The code includes input fields with placeholders, prompting the user to enter the username of the last author who has modified certain documents and the last one - the name of wiki(subwiki) on which documents were modified by the user. After entering these data, the code will search for documents in the main wiki or subwiki based on the last author's username. The search results will be displayed in a table, which will include only documents modified (not created by a user), its titles with corresponding links, as well as the relative date of the last modification. Finally, users with admin rights can resave the found documents. This action involves replacing the last author and content author with the admin user's own credentials. This allows the admin to take ownership of the identified documents.

{{velocity}}

{{info}}
Please note that to use this script, it is necessary to be logged in as a user with admin rights.
{{/info}}

#if($hasAdmin)

{{html wiki="true"}}
<form action="" method="post">
  <label for="lastAuthor">Username:</label>
  <input type="text" id="lastAuthor" name="lastAuthor" placeholder="xwiki:XWiki.{username}">
  <label for="wikiName">Wikiname:</label>
  <input type="text" id="wikiName" name="wikiName" placeholder="xwiki or any name of subwiki">
  <input type="submit" value="Find documents">
</form>
{{/html}}

  #set ($lastAuthor = $!request.getParameter("lastAuthor"))
  #set ($wikiName = $!request.getParameter("wikiName"))
  #if ($lastAuthor || $wikiName)
    #set ($documentsQuery = "SELECT doc.fullName, doc.author, doc.date FROM Document doc WHERE doc.space <> 'XWiki' AND doc.author = :lastAuthor")
    #set ($listOfWikis = $services.wiki.getAllIds())
    #set ($wikiExists = $listOfWikis.contains($wikiName))
    #if ($wikiExists)
      #set ($documents =  $services.query.xwql($documentsQuery).setWiki($wikiName).bindValue("lastAuthor", $lastAuthor).execute())

    {{html wiki="true"}}
      #if ($documents.size() == 0)
        {{warning}}
          No documents were found with the last author as "$lastAuthor".
        {{/warning}}
      #else
        <table>
          <tr>
            <th>Document Name</th>
            <th>Modification Date</th>
          </tr>
        #foreach ($document in $documents)
          #set ($documentReference = $services.model.resolveDocument($wikiName + ":" + $document[0]))
          #set ($userDocument = $xwiki.getDocument($documentReference))
          #if (!$userDocument.isCreator($lastAuthor))
            <tr>
              <td>[[$document[0]]]</td>
              <td>$xwiki.formatDate($document[2], 'dd/MM/yyyy HH:mm:ss')</td>
            </tr>
          #end
        #end
        </table>
          <form method="post">
            <input type="hidden" name="lastAuthor" value="$!{lastAuthor}">
            <input type="hidden" name="wikiName" value="$!{wikiName}"
            <label for="resaveAsAdmin">Would you like to resave these documents as Admin, where $lastAuthor is last modifier?</label>
            <input type="checkbox" id="resaveAsAdmin" name="resaveAsAdmin" value="Yes">
            <br>
            <input type="submit" value="Submit">
          </form>
      #end
    {{/html}}
    
    #else
      {{warning}}
        The wiki/subwiki $wikiName isn't existed
      {{/warning}}
    #end

  #else
    {{warning}}
      Either the username or name of the wiki/subwiki wasn't specified.
    {{/warning}}
  #end

  #set ($resaveAsAdmin = $request.getParameter("resaveAsAdmin"))
  #if ($resaveAsAdmin == "Yes")
    #foreach ($document in $documents)
      #set ($documentReference = $services.model.resolveDocument($wikiName + ":" + $document[0]))
      #set ($userDocument = $xwiki.getDocument($documentReference))
      #set ($internalDocument = $userDocument.document)
      #if ($userDocument.isCreator($lastAuthor))
        {{info}}
          This document $userDocument was created by $lastAuthor.
        {{/info}}
      #else
        #set ($discard = $internalDocument.setAuthorReference($xcontext.user))
        #set ($discard = $internalDocument.setContentAuthorReference($xcontext.user))
        #set ($discard = $userDocument.save("Resaved by user with admin rights", true))
     #end
    #end
    {{success}}
      Found documents were successfully resaved.
    {{/success}}
  #end

#else
    {{warning}}
      You're not allowed to perform this operation. Only users with admin rights can do this.
    {{/warning}}
#end

{{/velocity}}

How to use

To use this code, please follow the guide below:

  1. Ensure that you are logged in as an advanced user with admin rights. The code can only be executed by users with admin privileges, so this step is mandatory.
  2. Copy and paste the provided code onto any dedicated or Sandbox page. You can do this by either editing the page in Wiki mode or using the CKEditor to access the source code of the page.
  3. In the code, locate the input fields with the placeholders. Enter the username of the user who was the last author of the documents you want to find. Enter the name of wiki or subwiki. For instance main wiki - "XWiki".
  4. After filling input fields, click on the "Find documents" button. This will trigger the search process.
  5. If you wish to resave the found documents with your admin credentials, check the box that says, "Would you like to resave these documents as Admin, where {{username}} is last modifier?" This step is optional.
  6. Finally, click the "Submit" button to resave found documents.

Purpose

This code snippet might be useful in situations where you have encountered issues with scripts or code in your XWiki due to documents modified by a user who had programming rights. If such a user is no longer available or has been removed from your XWiki, it can be quite tedious and time-consuming to manually edit and resave each modified document.

Get Connected