cogCleanup slashes and backslashes from document names.
Developed by

Anca Luca, Paul Panțiru

Rating
Rate!
3 Votes
LicenseGNU Lesser General Public License 2.1

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).

Example for terminal pages (pre-nested pages)

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:

Also keep in mind that the example script below only works for terminal pages.
{{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.

Example for nested pages

See below an updated script that also works for nested documents.
{{velocity}}
#if ("$!request.documentsToFix" != "")
 #set($documentsList = $request.getParameterValues('documentsToFix'))
 #foreach($page in $documentsList)
   #calculateNewPages()
    * renaming $page : $xwiki.getDocument($pageReference).rename($newPageReference)
 #end
#else
 #set($pagesWithSlash = $services.query.xwql("from XWikiSpace as space where doc.space = space.reference and (((doc.name <> 'WebHome' and lower(doc.name) like '%/%') or(doc.name <> 'WebHome' and lower(doc.name) like '%\\%')) or ((doc.name = 'WebHome' and lower(space.name) like '%/%') or (doc.name = 'WebHome' and lower(space.name) like '%\\%')))").execute())

 {{html}}
  <form action="" method="post">
  <ul>
 #foreach($page in $pagesWithSlash)
   #calculateNewPages()
    <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

#macro(calculateNewPages)
 #set($pageReference = $services.model.resolveDocument($page))
 #set($spaceArray = [])
 #foreach($sp in $pageReference.getSpaceReferences())
   #set($auxSpace = $sp.name.replaceAll('/', ' ').replaceAll('\\', ' '))
   #set($discard = $spaceArray.add($auxSpace))
 #end
 #set($newPageReference = $services.model.createDocumentReference($pageReference.wikiReference.name, $spaceArray, $pageReference.name.replaceAll('/', ' ').replaceAll('\\', ' ')))
#end
{{/velocity}}

Prerequisites & Installation Instructions

Copy the code snippet to a page and save it.

Tags: cleanup slash
Created by Anca Luca on 2013/03/05 15:42
     

Get Connected