Clean slashes from document names

Version 3.1 by Manuel Smeria on 2018/04/02 09:44

cogCleanup slashes and backslashes from document names.
TypeSnippet
Category
Developed by

Anca Luca

Rating
3 Votes
LicenseGNU Lesser General Public License 2.1

Table of contents

Description

This script is useful if for any reason issue XE-688 is hit and it cannot be fixed at the level of Tomcat or other application server in front (for example, Apache httpd proxy-ing will have the same behaviour and Tomcat changes cannot fix anything).

The following script looks up all documents containing '/' or '\' in the document name and allows replacing the character with a space. To use it, copy the following code in a page with syntax 2.1:

{{velocity}}
#if ("$!request.documentsToFix" != "")
 #set($documentsList = $request.getParameterValues('documentsToFix'))
 #foreach($documentName in $documentsList)
   #set($pageReference = $services.model.resolveDocument($documentName))
   #set($newPageReference = $services.model.createDocumentReference($pageReference.wikiReference.name, $pageReference.lastSpaceReference.name, $pageReference.name.replaceAll('/', ' ').replaceAll('\\', ' ')))    
    * renaming $documentName : $xwiki.getDocument($pageReference).rename($newPageReference)
 #end
#else
 #set($pagesWithSlash = $xwiki.searchDocuments("where doc.name like '%/%' or doc.name like '%\\%'"))
 {{html}}
  <form action="" method="post">
  <ul>
 #foreach($page in $pagesWithSlash)
   #set($pageReference = $services.model.resolveDocument($page))
   #set($newPageReference = $services.model.createDocumentReference($pageReference.wikiReference.name, $pageReference.lastSpaceReference.name, $pageReference.name.replaceAll('/', ' ').replaceAll('\\', ' ')))
    <li> <input type="checkbox" name="documentsToFix" value="$escapetool.xml($services.model.serialize($pageReference, 'default'))">$page -> $services.model.serialize($newPageReference, 'default')</input></li>
 #end
  </ul>
  <input type="submit" class="button" value="Rename" />
  </form>
 {{/html}}
#end
{{/velocity}}

Keep in mind that this script does not prevent the issue to reappear (i.e. a document with the said characters in the name to be re-created) so, if you find yourself in the situation of not being able to fix XE-688, you need to make custom verifications and make sure that the code that created those documents does not do it again.

Also keep in mind that the script only works for terminal pages.

Get Connected