Last active
April 15, 2017 01:31
-
-
Save robearlam/ad13e1ac78de1be52feaa537ad0e7ba7 to your computer and use it in GitHub Desktop.
Extending Sitecore Revolver to remove old versions.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bind RevolverExtensions.TrimVersions,RevolverExtensions tv |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
tv -r |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public CommandResult Run(string[] args) | |
{ | |
ReadArgs(args); | |
if (_context == null) | |
{ | |
return new CommandResult(CommandStatus.Failure, "No Context"); | |
} | |
if (_context.CurrentItem == null) | |
{ | |
return new CommandResult(CommandStatus.Failure, "No Context Item"); | |
} | |
var currentItem = _context.CurrentItem; | |
if (currentItem.Versions.Count == 1 && !_recursive) | |
{ | |
return new CommandResult(CommandStatus.Success, "Item only has one verison"); | |
} | |
return BuildResponse(ProcessTrimVersionRequest(currentItem)); | |
} | |
private CommandResult BuildResponse(string outputText) | |
{ | |
return new CommandResult(CommandStatus.Success, outputText); | |
} | |
private void ReadArgs(IEnumerable<string> args) | |
{ | |
_recursive = args.Any(x => x == RecursiveSwitch); | |
} | |
private string ProcessTrimVersionRequest(Item currentItem) | |
{ | |
var outputText = TrimVersionsForItem(currentItem); | |
if (_recursive) | |
{ | |
foreach (Item childItem in currentItem.Children) | |
{ | |
outputText += ProcessTrimVersionRequest(childItem); | |
} | |
} | |
return outputText; | |
} | |
private string TrimVersionsForItem(Item currentItem) | |
{ | |
var outputTextLine = String.Empty; | |
foreach (var language in currentItem.Languages) | |
{ | |
var langItem = _context.CurrentDatabase.GetItem(currentItem.ID, language); | |
if (langItem.Versions.Count <= 1) | |
{ | |
continue; | |
} | |
outputTextLine = String.Format("Trimmed:{0}, Lang:{1}, Versions:", currentItem.Paths.FullPath, langItem.Language.Name); | |
foreach (var verItem in GetOldVersionsForLanguage(currentItem, langItem)) | |
{ | |
outputTextLine += verItem.Version.Number.ToString(CultureInfo.InvariantCulture) + ","; | |
verItem.Versions.RemoveVersion(); | |
} | |
} | |
if (outputTextLine.EndsWith(",")) | |
{ | |
outputTextLine = outputTextLine.Substring(0, outputTextLine.Length - 1); | |
} | |
return String.IsNullOrEmpty(outputTextLine) | |
? outputTextLine | |
: outputTextLine + "\r\n"; | |
} | |
private static IEnumerable<Item> GetOldVersionsForLanguage(Item currentItem, Item langItem) | |
{ | |
return langItem.Versions.GetVersions().Where(x => x.Version != currentItem.Versions.GetLatestVersion(langItem.Language).Version); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment