From version < 6.1 >
edited by Andreea Chirica
on 2019/12/04 15:13
To version < 7.1
edited by Andreea Chirica
on 2019/12/04 15:13
<
Change comment: There is no comment for this version

Summary

Details

ExtensionCode.ExtensionClass[0]
Description
... ... @@ -1,6 +1,6 @@
1 1  This script is useful if for any reason issue [[XE-688>>http://jira.xwiki.org/browse/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).
2 2  
3 -== Example for terminal pages ==
3 +== Example for terminal pages (pre-nested pages) ==
4 4  
5 5  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:
6 6  
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

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}}
  

  

     #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('\\', ' ')))
        
  • escapetool.xml($services.model.serialize($pageReference, 'default'))">$page -> $services.model.serialize($newPageReference, 'default')

  •  #end
      

  
  

 {{/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}}
  

  

     #foreach($page in $pagesWithSlash)
       #calculateNewPages()
        
  • escapetool.xml($services.model.serialize($pageReference, 'default'))">$page -> $services.model.serialize($newPageReference, 'default')

  •  #end
      

  
  

 {{/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.

Get Connected