/// <summary> /// Performs cleanup for shorten folders which referenced link items were already deleted. /// </summary> protected virtual void CleanupObsoleteShortens() { foreach (string directory in Directory.GetDirectories(_rootPath)) { string path = FileUtil.MakePath(directory, "link"); if (File.Exists(path)) { string linkContents = File.ReadAllText(path); ItemReference itemReference = ItemReference.Parse(linkContents.Replace('\\', '/')); if (itemReference != null && itemReference.GetItem() == null) { FileUtil.DeleteDirectory(directory, true); } else { // clean directories with only the link file, even if the link is valid (no items exist under the link) // (if the children length is 1 and we got here we know the link file exists and thus it MUST be the 1 item) if (Directory.GetFileSystemEntries(directory).Length == 1) { Directory.Delete(directory, true); } } } else { // clean empty directories if (Directory.GetFileSystemEntries(directory).Length == 0) { Directory.Delete(directory); } } } }
private static string GetTargetDatabase(string path, LoadOptions options) { if (options.Database != null) { return(options.Database.Name); } string path2 = PathUtils.UnmapItemPath(path, PathUtils.Root); ItemReference itemReference = ItemReference.Parse(path2); return(itemReference.Database); }
private void LoadItemPath() { var itemPath = PathUtils.MakeItemPath(ProviderId, _sourceProvider.SerializationRoot); // this will result in a path that includes the db as its first node, e.g. // /master/sitecore/content/Home // we can use an ItemReference to extract this info. var reference = ItemReference.Parse(itemPath); _itemPath = reference.Path.TrimEnd('/'); _databaseName = reference.Database; }
/// <summary> /// Loads a set of children from a serialized path /// </summary> private void LoadOneLevel(string path, AdvancedLoadOptions options, List <Failure> retryList) { Assert.ArgumentNotNullOrEmpty(path, "path"); Assert.ArgumentNotNull(options, "options"); Assert.ArgumentNotNull(retryList, "retryList"); var deleteCandidates = new Dictionary <ID, Item>(); // look for a serialized file for the root item if (File.Exists(path + PathUtils.Extension)) { var itemReference = ItemReference.Parse(PathUtils.MakeItemPath(path, options.Root)); if (itemReference != null) { // get the corresponding item from Sitecore Item rootItem = options.Database != null ? itemReference.GetItemInDatabase(options.Database) : itemReference.GetItem(); // if we're reverting, we add all of the root item's direct children to the "to-delete" list (we'll remove them as we find matching serialized children) if (rootItem != null && (options.ForceUpdate || options.DeleteOrphans)) { foreach (Item child in rootItem.Children) { // if the preset includes the child add it to the delete-candidate list (if we don't deserialize it below, it will be deleted if the right options are present) if (options.Preset.Includes(child)) { deleteCandidates[child.ID] = child; } else { options.Progress.ReportStatus(string.Format("[SKIPPED] {0}:{1} (and children) because it was excluded by the preset.", child.Database.Name, child.Paths.FullPath), MessageType.Debug); } } } } } // check for a directory containing children of the target path if (Directory.Exists(path)) { string[] files = Directory.GetFiles(path, "*" + PathUtils.Extension); foreach (string fileName in files) { try { ID itemId; if (IsStandardValuesItem(fileName, out itemId)) { deleteCandidates.Remove(itemId); // avoid deleting standard values items when forcing an update retryList.Add(new Failure(fileName, new StandardValuesException(fileName))); } else { // load a child item ItemLoadResult result; Item loadedItem = DoLoadItem(fileName, options, out result); if (loadedItem != null) { deleteCandidates.Remove(loadedItem.ID); // check if we have any child directories under this loaded child item (existing children) - // if we do not, we can nuke any children of the loaded item as well if ((options.ForceUpdate || options.DeleteOrphans) && !Directory.Exists(PathUtils.StripPath(fileName))) { foreach (Item child in loadedItem.Children) { deleteCandidates.Add(child.ID, child); } } } else if (result == ItemLoadResult.Skipped) // if the item got skipped we'll prevent it from being deleted { deleteCandidates.Remove(itemId); } } } catch (Exception ex) { // if a problem occurs we attempt to retry later retryList.Add(new Failure(path, ex)); } } } // if we're forcing an update (ie deleting stuff not on disk) we send the items that we found that weren't on disk off to get deleted from Sitecore if ((options.ForceUpdate || options.DeleteOrphans) && deleteCandidates.Count > 0) { Database db = deleteCandidates.Values.First().Database; bool reset = DeleteItems(deleteCandidates.Values, options.Progress); if (reset) { db.Engines.TemplateEngine.Reset(); } } }
private void OnFileChanged(string path, WatcherChangeTypes changeType) { if (changeType == WatcherChangeTypes.Created || changeType == WatcherChangeTypes.Changed || changeType == WatcherChangeTypes.Renamed) { Log.Info(string.Format("Serialized item {0} changed ({1}), reloading caches.", path, changeType), this); const int retries = 5; for (int i = 0; i < retries; i++) { try { var syncItem = LoadItem(path); if (syncItem != null) { _index.UpdateIndexes(syncItem); // TODO: nuclear bomb when all we need is a nerf gun Sitecore.Caching.CacheManager.ClearAllCaches(); } } catch (IOException iex) { // this is here because FSW can tell us the file has changed // BEFORE it's done with writing. So if we get access denied, // we wait 500ms and retry up to 5x before rethrowing if (i < retries - 1) { Thread.Sleep(500); continue; } Log.Error("Failed to read serialization file", iex, this); } break; } } if (changeType == WatcherChangeTypes.Deleted) { Log.Info(string.Format("Serialized item {0} deleted, reloading caches.", path), this); var root = _serializationPath; // if the path does not end with a backslash (\) MakeItemPath won't generate a proper ItemReference (//master vs /master) if (!root.EndsWith(@"\")) { root += @"\"; } var itemPath = ItemReference.Parse(PathUtils.MakeItemPath(path, root)); var item = _index.GetItem(itemPath.Path); if (item != null) { _index.ClearIndexes(item.GetSitecoreId()); // TODO: nuclear bomb when all we need is a nerf gun Sitecore.Caching.CacheManager.ClearAllCaches(); } } }