Clean deprecated properties

Last modified by Valdis Vitolins on 2021/03/18 11:28

cogClean deprecated properties from objects
TypeSnippet
Category
Developed by

Anca Luca

Rating
0 Votes
LicenseGNU Lesser General Public License 2.1

Table of contents

Description

When deleting a property in a class, all the objects of that class that already exist will declare the existence of a deprecated property when edited in object mode, which can be cleaned up by clicking a button in the object editor.

I wrote this script that helps bulk remove those deprecated properties for a given class. To use, paste the code under in a document with syntax 2.1. However, you need programming rights for this script to work, so make sure that you're connected with a user that has such rights when performing this operation.

Protect properly the page where this script is stored, untrained users can do hazardous things with it! Ideally even remove it once you've done your job with it. 

{{velocity}}
#if ("$!request.documentsToFix" != "" && "$!request.className" != "")
 #set($classNameToRemove = "$!request.className")
 #set($documentsList = $request.getParameterValues('documentsToFix'))
 #foreach($documentName in $documentsList)
   ## * received: $documentName $xwiki.getURL($documentName)
   #set($documentToClean = $xwiki.getXWiki().getDocument($documentName, $xcontext.context))
   #set($deprecatedBaseObject = $documentToClean.getObject($classNameToRemove))
   #set($needsSave = false)
   #foreach ($baseProp in $deprecatedBaseObject.getXClass($xcontext.context).getDeprecatedObjectProperties($deprecatedBaseObject))
     #set($discard = $deprecatedBaseObject.removeField($baseProp.name))
     #set($needsSave = true)
   #end
   #if ($needsSave)
     ## Set the new author
     #set($discard = $documentToClean.setAuthorReference($xcontext.context.getUserReference()))
     #set($discard = $xwiki.getXWiki().saveDocument($documentToClean, $msg.get("core.model.xobject.synchronizeObjects.versionSummary"),true, $xcontext.context))
      1. cleaned document [[$documentToClean.prefixedFullName]]
   #else
      1. NO deprecated property found in [[$documentToClean.prefixedFullName]]
   #end
 #end
#elseif ("$!request.className" != "" && "$!request.property" != "" && "$!request.propname" != "")
 #set($classNameToRemove = "$!request.className")
 #if("$!request.start" != "")
   #set($startOffset = $mathtool.toInteger("$!request.start"))
 #else
   #set($startOffset = 0)
 #end
 #if("$!request.limit" != "")
   #set($limitOffset = $mathtool.toInteger("$!request.limit"))
 #else
   #set($limitOffset = 0)
 #end
 #set($propType = "$!request.property")
 #set($propName = "$!request.propname")
 #set($documentsWithClass = $xwiki.searchDocuments(", BaseObject obj, $propType as prop where obj.name = doc.fullName and obj.className = ? and obj.id=prop.id.id and prop.id.name = ? order by doc.name", $limitOffset, $startOffset, [$classNameToRemove, $propName]))
 {{html clean="false"}}
  <a class="update-selectAll" href="#">Select All</a>
  <script type="text/javascript">
  document.observe('xwiki:dom:loaded', function() {
    var selectAllLink = $$('a.update-selectAll')[0];
    if (!selectAllLink) {
      return;
    }
   
    selectAllLink.observe('click', function(evt){
      evt.stop();
     
      $$('form.selectorsForm input[type=checkbox]').each(function(checkbox) {
       checkbox.writeAttribute('checked', 'checked');
      });
    });
  });
  </script>
  <form action="" method="post" class="xform full selectorsForm">
  <ol>
 #foreach($page in $documentsWithClass)
   #set($deprecatedDocument = $xwiki.getDocument($page))
   #set($deprecatedObject = $deprecatedDocument.getObject($classNameToRemove))    
   #set($deprecatedProps = $deprecatedObject.getxWikiClass().getDeprecatedObjectPropertyNames($deprecatedObject))
   #if($deprecatedProps.size() > 0)
      <li> <input type="checkbox" name="documentsToFix" value="$escapetool.xml($page)">$deprecatedProps</input><a href="$deprecatedDocument.getURL()">$page</a> $deprecatedDocument.getURL() </li>
   #end
 #end
  </ol>
  <input type="hidden" name="className" value="$escapetool.xml($classNameToRemove)"/>
  <input type="submit" class="button" value="Remove deprecated properties" />
  </form>
 {{/html}}
#else
 {{html}}
  <form action="" method="post" class="xform half">
    <dl>
      <dt><label>Class name:</label></dt>
      <dd><input type="text" name="className" value="$!request.className" /></dd>
      <dt><label>Property type:</label>
          <div class="xHint">Note that this property is just to help finding the documents which have deprecated properties, the script will still delete all deprecated properties from the found documents, not only the one mentioned here. E.g. LargeStringProperty, StringProperty, etc</div>
      </dt>
      <dd><input type="text" name="property" value="$!request.property"/></dd>
      <dt><label>Property name:</label>
          <div class="xHint">Note that this property is just to help finding the documents which have deprecated properties, the script will still delete all deprecated properties from the found documents, not only the one mentioned here.</div>
      </dt>
      <dd><input type="text" name="propname" value="$!request.propname"/></dd>
      <dt><label>Start:</label>
          <div class="xHint">The index on which to start, for paginated cleanup (leave emtpy for all documents).</div>
      </dt>
      <dd><input type="text" name="start" value="$!request.start" /></dd>
      <dt><label>Limit:</label>
          <div class="xHint">The number of documents to cleanup, for paginated cleanup (leave empty to get all documents).</div>
      </dt>
      <dd><input type="text" name="limit" value="$!request.limit" /></dd>
    </dl>
    <input type="submit" class="button" value="Search documents" />
  </form>
 {{/html}}
#end
{{/velocity}}
     

Get Connected