public void CleanUpExistingJobs()
        {
            string   contextDbName = Settings.GetSetting(SitecronConstants.SettingsNames.SiteCronContextDB, "master");
            Database contextDb     = Factory.GetDatabase(contextDbName);

            var folder = contextDb?.GetItem(SitecronConstants.ItemIds.AutoFolderID);

            if (folder == null)
            {
                return;
            }

            var autoFolderItems = folder.Axes.GetDescendants().Where(i =>
                                                                     TemplateManager.IsFieldPartOfTemplate(SitecronConstants.SiteCronFieldIds.CronExpression, i));

            if (autoFolderItems == null || (autoFolderItems != null && !autoFolderItems.Any()))
            {
                return;
            }

            Archive archive = ArchiveManager.GetArchive("archive", contextDb);

            foreach (var jobItem in autoFolderItems)
            {
                using (new SecurityDisabler())
                {
                    using (new EditContext(jobItem))
                    {
                        jobItem[SitecronConstants.FieldNames.LastRunLog] += "Archived at startup.";
                    }
                    archive.ArchiveItem(jobItem);
                    Log.Info(string.Format("SiteCron - Item Archived during startup. (ItemID: {0} DB: {1} Name:{2})", jobItem.ID.ToString(), contextDbName, jobItem.Name), this);
                }
            }
        }
Ejemplo n.º 2
0
        public string Execute([NotNull] string databaseName, [NotNull] string archiveName, [NotNull] string items)
        {
            Assert.ArgumentNotNull(databaseName, nameof(databaseName));
            Assert.ArgumentNotNull(archiveName, nameof(archiveName));
            Assert.ArgumentNotNull(items, nameof(items));

            var database = Factory.GetDatabase(databaseName);

            if (database == null)
            {
                throw new Exception("Database not found");
            }

            var archive = ArchiveManager.GetArchive(archiveName, database);

            var result = string.Empty;

            foreach (var id in items.Split('|'))
            {
                var guid = new Guid(id);

                if (!archive.RestoreItem(guid))
                {
                    result = "Failed to restore one or more items";
                }
            }

            return(result);
        }
Ejemplo n.º 3
0
        public IEnumerable <ArchiveEntry> GetEntries(string archiveName, Database database, ID itemId)
        {
            var archive   = ArchiveManager.GetArchive(archiveName, database);
            var archiveId = archive.GetArchivalId(itemId);

            return(archive.GetEntries(ID.Parse(archiveId)));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Archives items to the archive of the current database, if an archive is configured and available.
        /// </summary>
        /// <remarks>
        /// This applies to all versions and all languages of these items.
        /// </remarks>
        /// <param name="itemIDs">A list of Sitecore item ID strings of the items to archive.</param>
        /// <param name="archiveChildren">If set to true, child items will be archived as well; if set to false, items with children will be left untouched.</param>
        public void Archive(List <string> itemIDs, bool archiveChildren)
        {
            var archive = ArchiveManager.GetArchive("archive", database);

            if (Context.Job != null)
            {
                Context.Job.Status.Total = itemIDs.Count;
            }

            if (archive != null)
            {
                using (new SecurityDisabler())
                {
                    foreach (var itemId in itemIDs)
                    {
                        var scItem = this.database.GetItem(new ID(itemId));
                        if (scItem != null && !scItem.TemplateID.ToString().Equals(MediaConstants.MediaFolderTemplateID))
                        {
                            if (Context.Job != null)
                            {
                                Context.Job.Status.Messages.Add($"Archiving item {scItem.Paths.FullPath}");
                                Context.Job.Status.Processed++;
                            }

                            if (!scItem.HasChildren || archiveChildren)
                            {
                                archive.ArchiveItem(scItem);
                            }
                        }
                    }
                }
            }

            this.UpdateStorageAfterCleanUp(itemIDs, ActionType.DeleteItem);
        }
        /// <summary>
        /// Archive specific versions.
        ///
        /// </summary>
        /// <param name="item">The item.
        ///             </param>
        private void ArchiveItemVersion(Item item, ListString list)
        {
            string   databaseName = item.Database.Name;
            Database database     = Database.GetDatabase(databaseName);

            Assert.IsNotNull((object)database, "Database \"{0}\" not found", (object)databaseName);
            Language language = Language.Parse(item.Language.Name);

            foreach (var v in list)
            {
                Sitecore.Data.Version version = Sitecore.Data.Version.Parse(item.Versions.GetVersionNumbers().Where(n => n.Number.ToString() == v.ToString()).FirstOrDefault());

                Item obj = database.GetItem(item.ID, language, version);
                if (obj == null)
                {
                    return;
                }

                Sitecore.Data.Archiving.Archive archive = ArchiveManager.GetArchive("archive", database);
                Log.Audit(new object(), "Archive version: {0}", new string[1]
                {
                    AuditFormatter.FormatItem(obj)
                });
                archive.ArchiveVersion(obj);
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        ///     Moves page to recycle bin on remove-item
        /// </summary>
        /// <param name="path"></param>
        /// <param name="recurse"></param>
        protected override void RemoveItem(string path, bool recurse)
        {
            try
            {
                LogInfo("Executing RemoveItem(string path='{0}', string recurse='{1}')", path, recurse);
                if (!TryGetDynamicParam(ItemParam, out Item item))
                {
                    item = GetItemForPath(path);
                }

                if (item == null)
                {
                    return;
                }

                CheckOperationAllowed("remove", item.Access.CanDelete(), item.Uri.ToString());
                if (!ShouldProcess(item.Paths.Path))
                {
                    return;
                }

                var hasArchive     = IsDynamicParamSet(ArchiveParam);
                var hasPermanently = IsDynamicParamSet(PermanentlyParam);

                if (hasArchive && hasPermanently)
                {
                    var error = new ErrorRecord(new ParameterBindingException("Parameter set cannot be resolved using the specified named parameters. Detected Archive and Permanently parameters provided."), ErrorIds.AmbiguousParameterSet.ToString(), ErrorCategory.InvalidOperation, null);
                    WriteError(error);
                    return;
                }

                if (hasArchive)
                {
                    var archive = ArchiveManager.GetArchive("archive", item.Database);
                    WriteVerbose($"Removing item {item.ID} and moving to the archive {archive.Name} in database {item.Database}");
                    archive.ArchiveItem(item);
                }
                else if (hasPermanently)
                {
                    WriteVerbose($"Removing item {item.ID} permanently");
                    item.Delete();
                }
                else
                {
                    WriteVerbose($"Removing item {item.ID} and moving to the recycle bin in database {item.Database}");
                    item.Recycle();
                }
            }
            catch (Exception ex)
            {
                LogError(ex,
                         "Error while executing RemoveItem(string path='{0}', string recurse='{1}')",
                         path, recurse);
                throw;
            }
        }
        protected override void ProcessRecord()
        {
            if (!ID.IsNullOrEmpty(ItemId))
            {
                var archivalId = Archive.GetArchivalId(ItemId);
                if (!ShouldProcess(ItemId.ToString(), "Restore items by Item Id"))
                {
                    return;
                }

                WriteVerbose($"Restoring item {ItemId} from the archive {Archive.Name}");
                Archive.RestoreItem(archivalId);
            }
            else if (Identity != null)
            {
                var user = User.FromName(Identity.Name, false);
                if (user == null)
                {
                    return;
                }
                if (!ShouldProcess(Identity.ToString(), "Restore items by User"))
                {
                    return;
                }

                var entryCount = Archive.GetEntryCountForUser(user);
                var entries    = Archive.GetEntriesForUser(user, 0, entryCount);
                foreach (var entry in entries)
                {
                    WriteVerbose($"Restoring item {entry.ItemId} from the archive {entry.ArchiveName} in database {entry.Database.Name}");
                    Archive.RestoreItem(entry.ArchivalId);
                }
            }
            else if (ArchiveItem != null)
            {
                foreach (var entry in ArchiveItem)
                {
                    var archivalId = entry.ArchivalId;
                    if (!ShouldProcess(entry.ItemId.ToString(), "Restore items by ArchiveItem"))
                    {
                        return;
                    }

                    var archive = ArchiveManager.GetArchive(entry.ArchiveName, entry.Database);
                    if (archive == null)
                    {
                        return;
                    }
                    WriteVerbose($"Restoring item {entry.ItemId} from the archive {entry.ArchiveName} in database {entry.Database.Name}");
                    archive.RestoreItem(archivalId);
                }
            }
        }
Ejemplo n.º 8
0
        private void ArchiveItem(Database db, string itemID)
        {
            Item jobItem = db.GetItem(new ID(itemID));

            if (jobItem != null)
            {
                using (new Sitecore.SecurityModel.SecurityDisabler())
                {
                    Archive archive = ArchiveManager.GetArchive("archive", jobItem.Database);
                    archive.ArchiveItem(jobItem);
                    Log.Info(string.Format("Sitecron - Item Archived. (ItemID: {0} Archive:{1} DB: {2})", itemID, db.Name), this);
                }
            }
        }
        private void RemoveVersion(Item item)
        {
            var itemSig = $"{item.Database}:{item.ID}/{item.Language}#{item.Version}";

            if (ProcessedList.Contains(itemSig))
            {
                return;
            }

            ProcessedList.Add(itemSig);
            if (!ShouldProcess(item.GetProviderPath() + ", Lang:" + item.Language.Name + ", Ver:" + item.Version.Number, confirmMessage))
            {
                return;
            }

            var hasArchive     = IsParameterSpecified(nameof(Archive));
            var hasPermanently = IsParameterSpecified(nameof(Permanently));

            if (hasArchive && hasPermanently)
            {
                WriteError(typeof(ParameterBindingException), "Parameter set cannot be resolved using the specified named parameters. Detected Archive and Permanently parameters provided.", ErrorIds.AmbiguousParameterSet, ErrorCategory.InvalidOperation, null);
                return;
            }

            if (IsParameterSpecified("Archive"))
            {
                var archive = ArchiveManager.GetArchive("archive", item.Database);
                if (archive == null)
                {
                    return;
                }
                WriteVerbose($"Removing item {itemSig} and moving to the archive {archive.Name} in database {item.Database}");
                archive.ArchiveVersion(item);
            }
            else if (IsParameterSpecified("Permanently"))
            {
                WriteVerbose($"Removing item {itemSig} permanently.");
                item.Versions.RemoveVersion();
            }
            else
            {
                var archive = ArchiveManager.GetArchive("recyclebin", item.Database);
                if (archive == null)
                {
                    return;
                }
                WriteVerbose($"Removing item {itemSig} and moving to the archive {archive.Name} in database {item.Database}");
                archive.ArchiveVersion(item);
            }
        }
Ejemplo n.º 10
0
        public string Execute([NotNull] string databaseName, [NotNull] string archiveName)
        {
            Assert.ArgumentNotNull(databaseName, nameof(databaseName));
            Assert.ArgumentNotNull(archiveName, nameof(archiveName));

            var database = Factory.GetDatabase(databaseName);

            if (database == null)
            {
                throw new Exception("Database not found");
            }

            var archive = ArchiveManager.GetArchive(archiveName, database);

            archive.RemoveEntries();

            return(string.Empty);
        }
Ejemplo n.º 11
0
        public string Execute([NotNull] string databaseName, [NotNull] string archiveName, int pageIndex)
        {
            Assert.ArgumentNotNull(databaseName, nameof(databaseName));
            Assert.ArgumentNotNull(archiveName, nameof(archiveName));

            var database = Factory.GetDatabase(databaseName);

            if (database == null)
            {
                throw new Exception("Database not found");
            }

            var archive = ArchiveManager.GetArchive(archiveName, database);

            var entries = archive.GetEntriesForUser(Context.User, pageIndex, 250);

            var writer = new StringWriter();
            var output = new XmlTextWriter(writer);

            output.WriteStartElement("archive");

            foreach (var entry in entries)
            {
                output.WriteStartElement("entry");

                output.WriteAttributeString("id", entry.ArchivalId.ToString("B").ToUpperInvariant());
                output.WriteAttributeString("datetime", DateUtil.ToIsoDate(entry.ArchiveLocalDate));
                output.WriteAttributeString("archivedby", entry.ArchivedBy);
                output.WriteAttributeString("name", entry.Name);
                output.WriteAttributeString("path", entry.OriginalLocation);

                output.WriteEndElement();
            }

            output.WriteEndElement();

            return(writer.ToString());
        }
Ejemplo n.º 12
0
    /// <summary>
    /// 加载通用标签
    /// </summary>
    protected void LoadDefault()
    {
        #region 全局

        th.Put(TagFields.SITE_NAME, SettingManager.GetSetting().SiteName);
        th.Put(TagFields.SITE_DESCRIPTION, SettingManager.GetSetting().SiteDescription);
        th.Put(TagFields.META_KEYWORDS, SettingManager.GetSetting().MetaKeywords);
        th.Put(TagFields.META_DESCRIPTION, SettingManager.GetSetting().MetaDescription);

        th.Put(TagFields.FOOTER_HTML, SettingManager.GetSetting().FooterHtml);

        th.Put(TagFields.VERSION, SettingManager.GetSetting().Version);

        th.Put(TagFields.PAGE_TITLE, "首页");

        th.Put(TagFields.SITE_PATH, ConfigHelper.SitePath);
        th.Put(TagFields.SITE_URL, ConfigHelper.SiteUrl);

        th.Put(TagFields.THEME_PATH, ConfigHelper.SitePath + "themes/" + themeName + "/");
        th.Put(TagFields.THEME_URL, ConfigHelper.SiteUrl + "themes/" + themeName + "/");

        th.Put(TagFields.IS_DEFAULT, "0");
        th.Put(TagFields.IS_POST, "0");

        //th.Put(TagFields.FEED_URL, ConfigHelper.SiteUrl + "feed/post" + SettingManager.GetSetting().RewriteExtension);
        //th.Put(TagFields.FEED_COMMENT_URL, ConfigHelper.SiteUrl + "feed/comment" + SettingManager.GetSetting().RewriteExtension);

        th.Put(TagFields.FEED_URL, ConfigHelper.SiteUrl + "feed/post.aspx");
        th.Put(TagFields.FEED_COMMENT_URL, ConfigHelper.SiteUrl + "feed/comment.aspx");

        th.Put(TagFields.PAGER, string.Empty);
        th.Put(TagFields.PAGER_INDEX, RequestHelper.QueryInt("page", 1));

        th.Put(TagFields.URL, RequestHelper.CurrentUrl);
        th.Put(TagFields.DATE, DateTime.Now);

        th.Put(TagFields.ARCHIVES, ArchiveManager.GetArchive());

        th.Put(TagFields.SEARCH_KEYWORD, string.Empty);

        th.Put(TagFields.QUERY_COUNT, 0);
        th.Put(TagFields.PROCESS_TIME, 0);

        th.Put(TagFields.ENABLE_VERIFYCODE, SettingManager.GetSetting().EnableVerifyCode);

        string headhtml = string.Empty;

        headhtml += string.Format("<meta name=\"generator\" content=\"Loachs {0}\" />\n", SettingManager.GetSetting().Version);
        headhtml += "<meta name=\"author\" content=\"Loachs Team\" />\n";
        headhtml += string.Format("<meta name=\"copyright\" content=\"2008-{0} Loachs Team.\" />\n", DateTime.Now.Year);
        headhtml += string.Format("<link rel=\"alternate\" type=\"application/rss+xml\" title=\"{0}\"  href=\"{1}\"  />\n", SettingManager.GetSetting().SiteName, ConfigHelper.SiteUrl + "feed/post" + SettingManager.GetSetting().RewriteExtension);
        headhtml += string.Format("<link rel=\"EditURI\" type=\"application/rsd+xml\" title=\"RSD\" href=\"{0}xmlrpc/rsd.aspx\" />\n", ConfigHelper.SiteUrl);
        headhtml += string.Format("<link rel=\"wlwmanifest\" type=\"application/wlwmanifest+xml\" href=\"{0}xmlrpc/wlwmanifest.aspx\" />", ConfigHelper.SiteUrl);

        th.Put(TagFields.HEAD, headhtml);

        //if (Utils.IsSupportUrlRewriter == false)
        //{
        //    th.Put(TagFields.SEARCH_URL, ConfigHelper.SiteUrl + "default.aspx?type=search&keyword=");
        //}
        //else
        //{
        //      th.Put(TagFields.SEARCH_URL, ConfigHelper.SiteUrl + "search");
        //   }

        #endregion

        #region 文章

        //th.Put(TagFields.POST, null);
        //th.Put(TagFields.POST_MESSAGE, null);
        //th.Put(TagFields.POSTS, null);

        th.Put(TagFields.RECENT_POSTS, PostManager.GetPostList(SettingManager.GetSetting().SidebarPostCount, -1, -1, -1, 1, -1, 0));
        th.Put(TagFields.RECOMMEND_POSTS, PostManager.GetPostList(SettingManager.GetSetting().SidebarPostCount, -1, -1, 1, 1, -1, 0));
        th.Put(TagFields.TOP_POSTS, PostManager.GetPostList(Int32.MaxValue, -1, -1, -1, 1, 1, 0));

        //th.Put(TagFields.FEED_POSTS, null);

        #endregion

        #region 评论

        //th.Put(TagFields.COMMENTS, null);

        th.Put(TagFields.RECENT_COMMENTS, CommentManager.GetCommentListByRecent(SettingManager.GetSetting().SidebarCommentCount));

        if (PageUtils.IsLogin)
        {
            UserInfo user = UserManager.GetUser(PageUtils.CurrentUserId);
            if (user != null)
            {
                th.Put(TagFields.COMMENT_AUTHOR, user.Name);
                th.Put(TagFields.COMMENT_EMAIL, user.Email);
                th.Put(TagFields.COMMENT_SITEURL, user.SiteUrl);
            }
        }
        else
        {
            th.Put(TagFields.COMMENT_AUTHOR, Server.UrlDecode(PageUtils.GetCookie(TagFields.COMMENT_AUTHOR)));
            th.Put(TagFields.COMMENT_EMAIL, Server.UrlDecode(PageUtils.GetCookie(TagFields.COMMENT_EMAIL)));
            th.Put(TagFields.COMMENT_SITEURL, Server.UrlDecode(PageUtils.GetCookie(TagFields.COMMENT_SITEURL)));
        }
        th.Put(TagFields.COMMENT_CONTENT, string.Empty);
        th.Put(TagFields.COMMENT_MESSAGE, string.Empty);

        #endregion

        #region 作者,分类,标签

        th.Put(TagFields.AUTHORS, UserManager.GetUserList().FindAll(delegate(UserInfo user) { return(user.Status == 1); }));
        th.Put(TagFields.CATEGORIES, CategoryManager.GetCategoryList());
        th.Put(TagFields.RECENT_TAGS, TagManager.GetTagList(SettingManager.GetSetting().SidebarTagCount));

        #endregion

        #region 连接

        th.Put(TagFields.LINKS, LinkManager.GetLinkList(-1, 1));
        th.Put(TagFields.NAV_LINKS, LinkManager.GetLinkList((int)LinkPosition.Navigation, 1));
        th.Put(TagFields.GENERAL_LINKS, LinkManager.GetLinkList((int)LinkPosition.General, 1));

        #endregion

        #region 统计

        th.Put(TagFields.POST_COUNT, StatisticsManager.GetStatistics().PostCount);
        th.Put(TagFields.COMMENT_COUNT, StatisticsManager.GetStatistics().CommentCount);
        th.Put(TagFields.VIEW_COUNT, StatisticsManager.GetStatistics().VisitCount);
        th.Put(TagFields.AUTHOR_COUNT, UserManager.GetUserList().FindAll(delegate(UserInfo user) { return(user.Status == 1); }).Count);

        #endregion
    }
 public ArchiveVersionsAgentTask()
 {
     _master  = Factory.GetDatabase("master");
     _archive = ArchiveManager.GetArchive("archive", _master);
 }