Uploaded image for project: 'Repository'
  1. Repository
  2. REPOSITORY-393

Add a new maintenance button to remove orphean history versions

XMLWordPrintable

    • Icon: Improvement Improvement
    • Resolution: Fixed
    • Icon: Major Major
    • 4.5.0
    • None
    • None
    • None
    • 4.5 M2
    • 1 - Middle term
    • 2 - Normal

      The button must have a big warn (the kind you get when you empty your recylcle bin) since contents could not be retrieved anymore even manually

      This task can be launch when the server is not in safe mode (in contrary to the other existing tasks).. this task should use the new directlog system and not the old jcr maintenance tool log system... maybe it could be inherited to add a % bar and made generic to replace the old exiting jcr maintenant tool
      (e.g. because most interresting stats are in info)

      The hint should sepcify that this task should be forwarded by a garbage collect to be efficient on the disk size.

      Here a script that to the job

      
        var workspaceNames = session.getWorkspace().getAccessibleWorkspaceNames();
        var sessions = {};
        for (var i = 0; i < workspaceNames.length; i++)
        {
          	var workspaceName = workspaceNames[i];
        
             sessions[workspaceName] = repository.login(workspaceName);   
        };
        
        
        
        
        var vhs = [];
      
        var q = session.getWorkspace().getQueryManager().createQuery("//element(*, nt:versionHistory)", javax.jcr.query.Query.XPATH);
        var nodes = q.execute().getNodes();
        
        var count = nodes.getSize();
        
        while (nodes.hasNext())
        {
              var vh = nodes.next();
      		var id = vh.getVersionableIdentifier();
      
          	var foundOne = false;
              for (var i = 0; i < workspaceNames.length; i++)
              {
                    var workspaceName = workspaceNames[i];
      
                    try
                    {
                        sessions[workspaceName].getNodeByUUID(id);
                        foundOne = true;
                        break;
                    }
                    catch (e)
                    {
                        // nothing
                    }
              };
        
        		if (!foundOne)
              {
                	vhs.push(vh);
              }
        };
      
      print("Going to remove " + vhs.length + " history nodes on " + count + " existing nodes")
      print("-------------------------------------------------------")
        
      var errors = 0;
      var done = 0;
      var empty = 0;
      var fixed = 0;
      
      for (var i = 0; i < vhs.length; i++)
      {
      	var vh = vhs[i];
      		
      	var it = vh.getAllVersions();
      
      	var hasError = false;
      	var childNodesSize = 0;
      	
      	while (it.hasNext())
      	{
      		var version = it.next();
      		if (version.getName() != "jcr:rootVersion")
      		{
      			childNodesSize++;
      
      			try
      			{
      				vh.removeVersion(version.getName());
      			}
      			catch (e)
      			{
      				hasError = true;
      				print("Error with version " + version.getName() + " of " + vh.getUUID() + "... " + e);
      			}
      		}
      	}
      	
      	if (childNodesSize == 0)
      	{
      		_removeEmptyHistory(vh);
      		try
      		{
      			session.getNodeByUUID(vh.getUUID());
      			
      			print("Empty history node is " + vh.getUUID())
      			empty++;			
      		}
      		catch (e)
      		{
      			fixed++;
      		}
      	}
      	else if (hasError)
      	{
      		errors++;
      	}
      	else
      	{
      		done++;
      	}
      }
      
      print("-------------------------------------------------------")
      print(done + " normal history nodes removed");
      print(fixed + " empty history nodes fixed and removed");
      print(empty + " empty history nodes that are unfixable");
      print(errors + " unconsistents history nodes that cannot be removed");
      
      function _removeEmptyHistory(vh)
      {
      	var node = session.getNode("/ametys:root").addNodeWithUuid("historyrepair", "nt:unstructured", vh.getProperty("jcr:versionableUuid").getString());
      	node.addMixin("mix:versionable");
      	session.save();
      	node.checkin();
      	node.checkout();	
      	session.removeItem("/ametys:root/historyrepair");
      	session.save();
      	vh.removeVersion("1.0");
      }
      
      

            Unassigned Unassigned
            raphael Raphaël Franchet
            Votes:
            2 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: