Esempio n. 1
0
        /// <summary>
        /// Recursively deletes items from the media item report object that are present in the given list of item IDs.
        /// </summary>
        /// <param name="mediaItemReport">The media item report object to delete the children of.</param>
        /// <param name="itemIDs">A list of Sitecore item ID strings of the items to remove.</param>
        /// <param name="actionType">An ActionType enum object to indicate whether to delete the items in the list, or to remove the old versions from them.</param>
        private void RemoveChildren(MediaItemReport mediaItemReport, List <string> itemIDs, ActionType actionType)
        {
            var itemsToDelete = new List <MediaItemReport>();

            foreach (var item in mediaItemReport.Children)
            {
                // collect item references to delete
                var sitecoreItemIdString = $"{{{item.ID.ToString().ToUpper()}}}";
                if (item.IsMediaFolder.HasValue && !item.IsMediaFolder.Value && itemIDs.Contains(sitecoreItemIdString))
                {
                    // either stage the item for removal or just mark the old versions flag false, depending on the action type
                    if (actionType == ActionType.DeleteItem)
                    {
                        itemsToDelete.Add(item);
                    }
                    else if (actionType == ActionType.DeleteVersions)
                    {
                        item.HasOldVersions = false;
                    }
                }

                // check out the children of this item (recursively)
                if (item.Children != null)
                {
                    this.RemoveChildren(item, itemIDs, actionType);
                }
            }

            // remove the items to delete from the children object list
            foreach (var item in itemsToDelete)
            {
                mediaItemReport.Children.Remove(item);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// To avoid completely re-scanning the whole media library after a change, we could simply update the current JSON storage
        /// with the items that were cleaned up by our module. But take in mind that external changes are still being ignored by this module.
        /// So for an accurate rendition of the media library data usage, a re-scan is necessary once in a while.
        /// </summary>
        /// <remarks>
        /// The backlog of this module contains an idea to create a pipeline processor for the onSave handler of all items,
        /// so the JSON storage could be updated continuously, never requiring a re-scan after the initial media library scan!
        /// </remarks>
        /// <param name="itemIDs">A list of Sitecore item ID strings of the items to update in the JSON storage.</param>
        /// <param name="actionType">An ActionType enum object to indicate whether to delete the items in the list, or to remove the old versions from them.</param>
        private void UpdateStorageAfterCleanUp(List <string> itemIDs, ActionType actionType)
        {
            MediaItemReport mediaItemRoot          = null;
            JsonStorage     mediaItemReportStorage = null;

            // read the media item report object from the JSON storage
            var mediaItemPath = Settings.GetSetting("Shrink.MediaItemReportPath");

            if (!string.IsNullOrEmpty(mediaItemPath))
            {
                mediaItemReportStorage = new JsonStorage(mediaItemPath);
                mediaItemRoot          = mediaItemReportStorage.Deserialize <MediaItemReport>();
            }

            if (mediaItemRoot != null)
            {
                // remove any children as supplied in the list of Sitecore item IDs
                this.RemoveChildren(mediaItemRoot, itemIDs, actionType);

                // write the updated JSON file to disk
                mediaItemReportStorage.Serialize(mediaItemRoot);

                // update the corresponding media library report JSON storage file with the updated info
                var libraryReportPath = Settings.GetSetting("Shrink.MediaLibraryReportPath");
                if (!string.IsNullOrEmpty(libraryReportPath))
                {
                    var json = new JsonStorage(libraryReportPath);
                    json.Serialize(new MediaLibraryReport(mediaItemRoot));
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Scans the media library and all of its media items recursively.
        /// </summary>
        public void ScanMediaLibrary()
        {
            if (database != null)
            {
                var root = database.Items["/sitecore/media library"];
                this.MediaItemRoot = new MediaItemReport(root);

                this.ScanItemsOf(root, this.MediaItemRoot);
                this.WriteReportsToDataStorage();
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Analyzes a Sitecore media item and stores the result in the corresponding media item report object.
        /// </summary>
        /// <param name="sitecoreItem">The Sitecore item to analyze.</param>
        /// <param name="reportItem">The media item report object to store the results in.</param>
        private void Analyze(Item sitecoreItem, MediaItemReport reportItem)
        {
            if (sitecoreItem != null && reportItem.IsMediaFolder.HasValue && !reportItem.IsMediaFolder.Value)
            {
                // update and get referrers
                Globals.LinkDatabase.UpdateReferences(sitecoreItem);

                try
                {
                    // start with the assumption that the item isn't referenced
                    reportItem.IsReferenced = false;

                    var itemReferrers = Globals.LinkDatabase.GetReferrers(sitecoreItem);

                    // check validity of all referrers
                    foreach (var itemLink in itemReferrers)
                    {
                        if (itemLink != null)
                        {
                            var referencedItem = itemLink.GetSourceItem();
                            if (referencedItem != null)
                            {
                                // if at least one valid referrer is found, report and break out
                                reportItem.IsReferenced = true;
                                break;
                            }
                        }
                    }
                }
                catch (Exception exception)
                {
                    // exception handling because getting the referrers doesn't always seem to go flawless
                    Log.Error($"Shrink: exception during getting the referrers for item {{{reportItem.ID}}}", exception, this);

                    // if getting the referrers fails, the nullable IsReferenced boolean of the report object is set to null
                    // to indicate that the reference state couldn't be determined
                    reportItem.IsReferenced = null;
                }

                // if the item is referenced from code, always mark it as being referenced
                if (this.codeReferencedItemList.Contains(sitecoreItem.ID.ToString()))
                {
                    reportItem.IsReferenced = true;
                }

                // add other meta data as well
                reportItem.IsPublished    = new PublishingHelper().ListPublishedTargets(sitecoreItem).Count > 0;
                reportItem.HasOldVersions = this.HasMultipleVersions(sitecoreItem);
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Recursive method to analyze a Sitecore media item and its child items.
        /// </summary>
        /// <param name="sitecoreItem">The Sitecore item to analyze.</param>
        /// <param name="reportItem">The media item report item to store the results and its children in.</param>
        private void ScanItemsOf(Item sitecoreItem, MediaItemReport reportItem)
        {
            this.Analyze(sitecoreItem, reportItem);

            if (Context.Job != null)
            {
                Context.Job.Status.Processed++;
            }

            if (sitecoreItem.HasChildren)
            {
                foreach (Item child in sitecoreItem.Children)
                {
                    var childItem = new MediaItemReport(child);
                    reportItem.Children.Add(childItem);

                    this.ScanItemsOf(child, childItem);
                }
            }
        }