2017-07-20 11:21:28 +02:00
using System ;
2016-06-13 17:42:05 +02:00
using System.Collections.Generic ;
2017-05-30 15:56:27 +02:00
using Umbraco.Core.Composing ;
2016-06-13 17:42:05 +02:00
using Umbraco.Core.Services ;
2016-07-20 12:44:15 +02:00
using Umbraco.Web.PublishedCache ;
2016-06-13 17:42:05 +02:00
namespace Umbraco.Web.HealthCheck.Checks.DataIntegrity
{
/// <summary>
/// This moves the functionality from the XmlIntegrity check dashboard into a health check
/// </summary>
[ HealthCheck (
"D999EB2B-64C2-400F-B50C-334D41F8589A" ,
"XML Data Integrity" ,
2016-08-05 14:42:29 +02:00
Description = "This checks the data integrity for the xml structures for content, media and members that are stored in the cmsContentXml table. This does not check the data integrity of the xml cache file, only the xml structures stored in the database used to create the xml cache file." ,
2016-06-13 17:42:05 +02:00
Group = "Data Integrity" ) ]
2016-09-08 18:43:58 +02:00
[HideFromTypeFinder] // only if running the Xml cache! added by XmlCacheComponent!
2016-06-13 17:42:05 +02:00
public class XmlDataIntegrityHealthCheck : HealthCheck
{
private readonly ILocalizedTextService _textService ;
2017-10-31 12:48:24 +01:00
private readonly PublishedCache . XmlPublishedCache . PublishedSnapshotService _publishedSnapshotService ;
2016-06-13 17:42:05 +02:00
private const string CheckContentXmlTableAction = "checkContentXmlTable" ;
private const string CheckMediaXmlTableAction = "checkMediaXmlTable" ;
private const string CheckMembersXmlTableAction = "checkMembersXmlTable" ;
2017-10-31 12:48:24 +01:00
public XmlDataIntegrityHealthCheck ( ILocalizedTextService textService , IPublishedSnapshotService publishedSnapshotService )
2016-06-13 17:42:05 +02:00
{
2016-09-01 19:06:08 +02:00
_textService = textService ;
2016-06-13 17:42:05 +02:00
2017-10-31 12:48:24 +01:00
_publishedSnapshotService = publishedSnapshotService as PublishedCache . XmlPublishedCache . PublishedSnapshotService ;
if ( _publishedSnapshotService = = null )
throw new NotSupportedException ( "Unsupported IPublishedSnapshotService, only the Xml one is supported." ) ;
2016-07-20 12:44:15 +02:00
}
2016-06-13 17:42:05 +02:00
/// <summary>
/// Get the status for this health check
/// </summary>
/// <returns></returns>
public override IEnumerable < HealthCheckStatus > GetStatus ( )
{
//return the statuses
return new [ ] { CheckContent ( ) , CheckMedia ( ) , CheckMembers ( ) } ;
}
/// <summary>
/// Executes the action and returns it's status
/// </summary>
/// <param name="action"></param>
/// <returns></returns>
public override HealthCheckStatus ExecuteAction ( HealthCheckAction action )
{
switch ( action . Alias )
{
case CheckContentXmlTableAction :
2017-10-31 12:48:24 +01:00
_publishedSnapshotService . RebuildContentAndPreviewXml ( ) ;
2016-06-13 17:42:05 +02:00
return CheckContent ( ) ;
case CheckMediaXmlTableAction :
2017-10-31 12:48:24 +01:00
_publishedSnapshotService . RebuildMediaXml ( ) ;
2016-06-13 17:42:05 +02:00
return CheckMedia ( ) ;
case CheckMembersXmlTableAction :
2017-10-31 12:48:24 +01:00
_publishedSnapshotService . RebuildMemberXml ( ) ;
2016-06-13 17:42:05 +02:00
return CheckMembers ( ) ;
default :
throw new ArgumentOutOfRangeException ( ) ;
}
}
private HealthCheckStatus CheckMembers ( )
{
2017-10-31 12:48:24 +01:00
return Check ( _publishedSnapshotService . VerifyMemberXml ( ) , CheckMembersXmlTableAction , "healthcheck/xmlDataIntegrityCheckMembers" ) ;
2016-06-13 17:42:05 +02:00
}
private HealthCheckStatus CheckMedia ( )
{
2017-10-31 12:48:24 +01:00
return Check ( _publishedSnapshotService . VerifyMediaXml ( ) , CheckMediaXmlTableAction , "healthcheck/xmlDataIntegrityCheckMedia" ) ;
2016-06-13 17:42:05 +02:00
}
private HealthCheckStatus CheckContent ( )
{
2017-10-31 12:48:24 +01:00
return Check ( _publishedSnapshotService . VerifyContentAndPreviewXml ( ) , CheckContentXmlTableAction , "healthcheck/xmlDataIntegrityCheckContent" ) ;
2016-07-20 12:44:15 +02:00
}
2016-06-13 17:42:05 +02:00
2016-07-20 12:44:15 +02:00
private HealthCheckStatus Check ( bool ok , string action , string text )
{
2016-06-13 17:42:05 +02:00
var actions = new List < HealthCheckAction > ( ) ;
2016-07-20 12:44:15 +02:00
if ( ok = = false )
actions . Add ( new HealthCheckAction ( action , Id ) ) ;
2016-06-13 17:42:05 +02:00
2016-07-20 12:44:15 +02:00
return new HealthCheckStatus ( _textService . Localize ( text , new [ ] { ok ? "ok" : "not ok" } ) )
2016-06-13 17:42:05 +02:00
{
2016-07-20 12:44:15 +02:00
ResultType = ok ? StatusResultType . Success : StatusResultType . Error ,
2016-06-13 17:42:05 +02:00
Actions = actions
} ;
}
}
2017-07-20 11:21:28 +02:00
}