Exemplo n.º 1
0
        public virtual ISerializedItem[] GetChildItems(ISerializedReference parent)
        {
            Assert.ArgumentNotNull(parent, "parent");

            var path      = SerializationPathUtility.GetReferenceDirectoryPath(parent);
            var shortPath = SerializationPathUtility.GetShortSerializedReferencePath(_rootPath, parent);

            var fileNames = new List <string>();

            bool longPathExists  = Directory.Exists(path);
            bool shortPathExists = Directory.Exists(shortPath);

            if (!longPathExists && !shortPathExists)
            {
                return(new ISerializedItem[0]);
            }

            if (longPathExists)
            {
                fileNames.AddRange(Directory.GetFiles(path, "*" + PathUtils.Extension));
            }
            if (shortPathExists)
            {
                fileNames.AddRange(Directory.GetFiles(shortPath, "*" + PathUtils.Extension));
            }

            return(fileNames.Select(ReadItemFromDisk).ToArray());
        }
Exemplo n.º 2
0
        protected virtual void DeleteItemRecursive(ISerializedReference reference)
        {
            foreach (var child in reference.GetChildReferences(false))
            {
                DeleteItemRecursive(child);
            }

            // kill the serialized file
            var fileItem = reference.GetItem();

            if (fileItem != null && File.Exists(fileItem.ProviderId))
            {
                File.Delete(fileItem.ProviderId);
            }

            // remove any serialized children
            var directory = SerializationPathUtility.GetReferenceDirectoryPath(reference);

            if (Directory.Exists(directory))
            {
                Directory.Delete(directory, true);
            }

            // clean up any hashpaths for this item
            var shortDirectory = SerializationPathUtility.GetShortSerializedReferencePath(_rootPath, reference);

            if (Directory.Exists(shortDirectory))
            {
                Directory.Delete(shortDirectory, true);
            }

            // clean up empty parent folder(s)
            var parentDirectory = Directory.GetParent(directory);

            if (!parentDirectory.Exists)
            {
                return;
            }

            do
            {
                if (parentDirectory.GetFileSystemInfos().Length > 0)
                {
                    break;
                }

                parentDirectory.Delete(true);
                parentDirectory = parentDirectory.Parent;
            } while (parentDirectory != null && parentDirectory.Exists);
        }
Exemplo n.º 3
0
        public virtual void RenameSerializedItem(ISourceItem renamedItem, string oldName)
        {
            if (renamedItem == null || oldName == null)
            {
                return;
            }

            var typed = renamedItem as SitecoreSourceItem;

            if (typed == null)
            {
                throw new ArgumentException("Renamed item must be a SitecoreSourceItem", "renamedItem");
            }

            // write the serialized item under its new name
            var updatedItem = SerializeItem(renamedItem);

            // find the children directory path of the previous item name, if it exists, and move them to the new child path
            var oldItemPath = renamedItem.ItemPath.Substring(0, renamedItem.ItemPath.Length - renamedItem.Name.Length) + oldName;

            var oldSerializedChildrenDirectoryPath = SerializationPathUtility.GetSerializedReferencePath(_rootPath, renamedItem.DatabaseName, oldItemPath);

            var oldSerializedChildrenReference = new SitecoreSerializedReference(oldSerializedChildrenDirectoryPath, this);

            var shortOldSerializedChildrenPath      = SerializationPathUtility.GetShortSerializedReferencePath(_rootPath, oldSerializedChildrenReference);
            var shortOldSerializedChildrenReference = new SitecoreSerializedReference(shortOldSerializedChildrenPath, this);

            if (Directory.Exists(oldSerializedChildrenReference.ProviderId))
            {
                MoveDescendants(oldSerializedChildrenReference, updatedItem, renamedItem, true);
            }

            if (Directory.Exists(shortOldSerializedChildrenPath))
            {
                MoveDescendants(shortOldSerializedChildrenReference, updatedItem, renamedItem, true);
            }

            // delete the original serialized item from pre-rename (unless the names only differ by case, in which case we'd delete the item entirely because NTFS is case insensitive!)
            if (!renamedItem.Name.Equals(oldName, StringComparison.OrdinalIgnoreCase))
            {
                // note that we don't have to worry about short paths here because DeleteSerializedItem() knows how to find them
                DeleteSerializedItem(oldSerializedChildrenReference);
            }
        }
Exemplo n.º 4
0
        public virtual ISerializedReference[] GetChildReferences(ISerializedReference parent, bool recursive)
        {
            Assert.ArgumentNotNull(parent, "parent");

            var longPath  = SerializationPathUtility.GetReferenceDirectoryPath(parent);
            var shortPath = SerializationPathUtility.GetShortSerializedReferencePath(_rootPath, parent);

            Func <string, string[]> parseDirectory = path =>
            {
                if (!Directory.Exists(path))
                {
                    return(new string[0]);
                }

                var resultSet = new HashSet <string>();

                try
                {
                    string[] files = Directory.GetFiles(path, "*" + PathUtils.Extension);

                    foreach (var file in files)
                    {
                        resultSet.Add(file);
                    }

                    string[] directories = SerializationPathUtility.GetDirectories(path, this);

                    // add directories that aren't already ref'd indirectly by a file
                    foreach (var directory in directories)
                    {
                        if (CommonUtils.IsDirectoryHidden(directory))
                        {
                            continue;
                        }

                        if (!resultSet.Contains(directory + PathUtils.Extension))
                        {
                            resultSet.Add(directory);
                        }
                    }

                    string[] resultArray = resultSet.ToArray();

                    // make sure if a "templates" item exists in the current set, it goes first
                    if (resultArray.Length > 1)
                    {
                        for (int i = 1; i < resultArray.Length; i++)
                        {
                            if ("templates".Equals(Path.GetFileName(resultArray[i]), StringComparison.OrdinalIgnoreCase))
                            {
                                string text = resultArray[0];
                                resultArray[0] = resultArray[i];
                                resultArray[i] = text;
                            }
                        }
                    }

                    return(resultArray);
                }
                catch (DirectoryNotFoundException)
                {
                    // it seems like occasionally, even though we use Directory.Exists() to make sure the parent dir exists, that when we actually call Directory.GetFiles()
                    // it throws an error that the directory does not exist during recursive deletes. If the directory does not exist, then we can safely assume no children are present.
                    return(new string[0]);
                }
            };

            var results = Enumerable.Concat(parseDirectory(longPath), parseDirectory(shortPath));

            List <ISerializedReference> referenceResults = results.Select(x => (ISerializedReference) new SitecoreSerializedReference(x, this)).ToList();

            if (recursive)
            {
                var localReferenceResults = referenceResults.ToArray();
                foreach (var child in localReferenceResults)
                {
                    referenceResults.AddRange(GetChildReferences(child, true));
                }
            }

            return(referenceResults.ToArray());
        }