Last active
November 21, 2018 08:38
-
-
Save LucGosso/b4292ee43285a82ece17b75d4c464e89 to your computer and use it in GitHub Desktop.
ILM: Scheduled job and server to get all pages that is not updated from a past date [https://devblog.gosso.se/?p=953]
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
using EPiServer; | |
using EPiServer.Core; | |
using EPiServer.PlugIn; | |
using EPiServer.Security; | |
using EPiServer.ServiceLocation; | |
using EPiServer.Web; | |
using EPiServer.Web.Routing; | |
using Gosso.Mvc.Business.Initialization; | |
using Gosso.Mvc.Helpers; | |
using log4net; | |
using System; | |
using System.Configuration; | |
using System.Diagnostics; | |
using System.Linq; | |
using System.Reflection; | |
using System.Text; | |
namespace Gosso.Mvc.Business | |
{ | |
[ScheduledPlugIn(DisplayName = "ILM: Job to evaluate updated pages", SortIndex = 100)] | |
public class ILMPageCheckJob : EPiServer.Scheduler.ScheduledJobBase | |
{ | |
private readonly ILog _log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | |
private static bool _stop; | |
private string _returnMessage = string.Empty; | |
private readonly IContentRepository _contentRepository; | |
private readonly IUserImpersonation _userImpersonation; | |
private readonly PageUpdateService _pageUpdateService; | |
private readonly ISiteDefinitionRepository _siteDefinitionRepository; | |
private readonly UrlResolver _urlResolver; | |
public PageUpdateJob() | |
: base() | |
{ | |
// Make the job interruptable | |
this.IsStoppable = true; | |
_contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>(); | |
_userImpersonation = ServiceLocator.Current.GetInstance<IUserImpersonation>(); | |
_pageUpdateService = ServiceLocator.Current.GetInstance<PageUpdateService>(); | |
_siteDefinitionRepository = ServiceLocator.Current.GetInstance<ISiteDefinitionRepository>(); | |
_urlResolver = ServiceLocator.Current.GetInstance<UrlResolver>(); | |
} | |
public override string Execute() | |
{ | |
Stopwatch sw = Stopwatch.StartNew(); | |
_log.Info("PageUpdate Job started at: " + DateTime.Now); | |
_stop = false; | |
try | |
{ | |
string RunBy = "Job executed manually"; | |
if (PrincipalInfo.CurrentPrincipal.Identity.Name == string.Empty) | |
{ | |
string userName = ConfigurationManager.AppSettings["JobExecutorUserName"] as string ?? string.Empty; | |
PrincipalInfo.CurrentPrincipal = _userImpersonation.CreatePrincipal(userName); | |
RunBy = "Job executed by Scheduler"; | |
} | |
_returnMessage += "<br/>" + RunBy + "<br/>"; | |
int count = _siteDefinitionRepository.List().Count(); | |
int cc = 1; | |
foreach (SiteDefinition site in _siteDefinitionRepository.List()) | |
{ | |
if (!ContentReference.IsNullOrEmpty(site.StartPage)) | |
{ | |
this.OnStatusChanged("Evaluating " + site.Name + " - site " + cc++ + " of total " + count); | |
HandleSite(site.Name, site.SiteUrl, site.StartPage); | |
} | |
} | |
} | |
catch (Exception ex) | |
{ | |
_log.Debug("Error in Page Update job", ex); | |
throw new Exception(_returnMessage); | |
} | |
_log.Info("Page Update Job ended at: " + DateTime.Now); | |
sw.Stop(); | |
_returnMessage += "Time for execution: " + sw.ElapsedMilliseconds + " ms<br/>"; | |
// Return message indicating finished status | |
return string.Format(cctotal + "pages not updated<br/>" + _returnMessage).Replace("<br/>", " | "); | |
} | |
private const string htmlTemplate = "<tr><td>{2}</td><td>{1}</td><td>{3}</td><td><a href=\"{0}\">{0}</a></td><td><a href=\"{5}\">{5}</a></td><td>{4}</td></tr>"; | |
private void HandleSite(string siteName, Uri siteUri, ContentReference startPage) | |
{ | |
if (_stop) | |
return; | |
StringBuilder outputHtml = new StringBuilder(); | |
outputHtml.Append("<table width='100%' cellspacing='0' cellpadding='2'border='0'>"); | |
outputHtml.Append("<tr><th><b>changed</b></th><th><b>PageTypeName</b></th>" + | |
" <th><b>Changed By</b></th><th><b>Link for content</b></th><th><b>Web Resource</b></th></tr>"); | |
var pageList = _pageUpdateService.GetPagesNotUpdated(DateTime.Now.AddMonths(-13), startPage); | |
var siteUrl = SiteDefinition.Current.SiteUrl; | |
EditUrlResolver editUrlResolver = ServiceLocator.Current.GetInstance<EditUrlResolver>(); | |
if (pageList.Count > 0) | |
{ | |
pageList = pageList.OrderBy(x => x.Created).ToList(); | |
foreach (var page in pageList) | |
{ | |
if (_stop) | |
return; | |
string pageUrl = editUrlResolver.GetEditViewUrl(page.ContentLink).ToString(); | |
outputHtml.AppendLine(string.Format(htmlTemplate, siteUrl + pageUrl, page.PageTypeName, page.Changed.ToString("yyyy-MM-dd"), page.ChangedBy, page.PageName, _urlResolver.GetUrl(page.ContentLink))); | |
} | |
cctotal = pageList.Count; | |
outputHtml.Append("</table>"); | |
//implement your own emailing | |
MailHelper.SendMail("ILM: Page granting found "+ pageList.Count+ " pages", | |
outputHtml.ToString(), "[email protected]", | |
"[email protected]", | |
IsBodyHtml:true); | |
} | |
} | |
public int cctotal | |
{ get; set; } | |
public override void Stop() | |
{ | |
_stop = true; | |
} | |
} | |
} |
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
using EPiServer; | |
using EPiServer.Core; | |
using EPiServer.Security; | |
using EPiServer.ServiceLocation; | |
using Gosso.Mvc.Models.Pages; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace Gosso.Mvc.Business.Initialization | |
{ | |
[ServiceConfiguration(ServiceType = typeof(PageUpdateService),Lifecycle = ServiceInstanceScope.Singleton)] | |
public class PageUpdateService | |
{ | |
private readonly IContentRepository _contentRepository; | |
public PageUpdateService(IContentRepository contentRepository) | |
{ | |
_contentRepository = contentRepository; | |
} | |
/// <summary> | |
/// Service to get all pages that is not updated from a past date | |
/// </summary> | |
/// <param name="date"></param> | |
/// <param name="startPage"></param> | |
/// <returns></returns> | |
public List<PageData> GetPagesNotUpdated(DateTime date, ContentReference startPage) | |
{ | |
var pageList = new List<PageData>(); | |
foreach (var pr in _contentRepository.GetDescendents(startPage)) | |
{ | |
BasePageData pd = null; | |
try | |
{ | |
pd = _contentRepository.Get<BasePageData>(pr); | |
} | |
catch | |
{ | |
pd = null; | |
} | |
if (checkAccess("Everyone", pd)) | |
{ | |
if (pd != null) | |
{ | |
foreach (var language in pd.ExistingLanguages) | |
{ | |
if (_contentRepository.Get<PageData>(pd.ContentGuid, language) is BasePageData pdLang && pdLang.CheckPublishedStatus(PagePublishedStatus.Published) && pdLang.Changed < date) | |
{ | |
if (!pageList.Contains(pdLang)) | |
{ | |
if (pdLang.LinkType != PageShortcutType.External && CheckPageType(pdLang)) | |
{ | |
pageList.Add(pdLang); | |
} | |
} | |
} | |
} // foreach lang | |
} | |
} | |
} | |
return pageList; | |
} | |
private bool CheckPageType(BasePageData page) | |
{ | |
//if you rather dont want to evaluate som pagetypes, put them here | |
if (page is NewsArticlePage | |
|| page is NewsArticleListPage | |
) | |
return false; | |
return true; | |
} | |
private bool checkAccess(string role, PageData pd) | |
{ | |
if (pd == null) | |
return false; | |
try | |
{ | |
var list = pd.ACL.Entries.FirstOrDefault(x => x.Name == "Everyone"); | |
if (list != null && list.Access == AccessLevel.Read) | |
{ | |
return true; | |
} | |
} | |
catch (Exception) | |
{ | |
return false; | |
} | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment