From 6bd7e4095883eac54e497a28c321b43bc7f4e211 Mon Sep 17 00:00:00 2001 From: Nathan Heskew Date: Tue, 14 Dec 2010 13:45:58 -0800 Subject: [PATCH] Enabling pre-dating of content from Live Writer --HG-- branch : dev --- .../Core/Common/Services/XmlRpcHandler.cs | 91 +++++++++++++++++++ src/Orchard.Web/Core/Orchard.Core.csproj | 1 + 2 files changed, 92 insertions(+) create mode 100644 src/Orchard.Web/Core/Common/Services/XmlRpcHandler.cs diff --git a/src/Orchard.Web/Core/Common/Services/XmlRpcHandler.cs b/src/Orchard.Web/Core/Common/Services/XmlRpcHandler.cs new file mode 100644 index 000000000..b4a9ff5b9 --- /dev/null +++ b/src/Orchard.Web/Core/Common/Services/XmlRpcHandler.cs @@ -0,0 +1,91 @@ +using System; +using System.Collections.Generic; +using System.Xml.Linq; +using Orchard.ContentManagement; +using Orchard.Core.Common.Models; +using Orchard.Core.XmlRpc; +using Orchard.Core.XmlRpc.Models; + +namespace Orchard.Core.Common.Services { + public class XmlRpcHandler : IXmlRpcHandler { + private readonly IContentManager _contentManager; + + public XmlRpcHandler(IContentManager contentManager) { + _contentManager = contentManager; + } + + public void SetCapabilities(XElement options) { + const string manifestUri = "http://schemas.microsoft.com/wlw/manifest/weblog"; + options.SetElementValue(XName.Get("supportsCustomDate", manifestUri), "Yes"); + } + + public void Process(XmlRpcContext context) { + switch (context.Request.MethodName) { + case "metaWeblog.newPost": + MetaWeblogSetCustomPublishedDate( + GetId(context.Response), + Convert.ToString(context.Request.Params[0].Value), + Convert.ToString(context.Request.Params[1].Value), + Convert.ToString(context.Request.Params[2].Value), + (XRpcStruct) context.Request.Params[3].Value, + Convert.ToBoolean(context.Request.Params[4].Value), + context._drivers); + break; + case "metaWeblog.editPost": + MetaWeblogSetCustomPublishedDate( + GetId(context.Response), + Convert.ToString(context.Request.Params[0].Value), + Convert.ToString(context.Request.Params[1].Value), + Convert.ToString(context.Request.Params[2].Value), + (XRpcStruct) context.Request.Params[3].Value, + Convert.ToBoolean(context.Request.Params[4].Value), + context._drivers); + break; + } + } + + private void MetaWeblogSetCustomPublishedDate(int contentItemId, string appKey, string userName, string password, XRpcStruct content, bool publish, ICollection drivers) { + if (!publish) + return; + + var publishedUtc = content.Optional("dateCreated"); + if (publishedUtc == null || publishedUtc > DateTime.UtcNow) // only pre-dating of content with the CommonPart + return; + + var driver = new XmlRpcDriver(item => { + if (!(item is int)) + return; + + var id = (int)item; + var contentItem = _contentManager.Get(id, VersionOptions.Latest); + if (contentItem == null || !contentItem.Is()) + return; + + contentItem.As().PublishedUtc = publishedUtc; + }); + + if (contentItemId > 0) + driver.Process(contentItemId); + else + drivers.Add(driver); + } + + private static int GetId(XRpcMethodResponse response) { + return response != null && response.Params.Count == 1 && response.Params[0].Value is int + ? Convert.ToInt32(response.Params[0].Value) + : 0; + } + + public class XmlRpcDriver : IXmlRpcDriver { + private readonly Action _process; + + public XmlRpcDriver(Action process) { + _process = process; + } + + public void Process(object item) { + _process(item); + } + } + } +} \ No newline at end of file diff --git a/src/Orchard.Web/Core/Orchard.Core.csproj b/src/Orchard.Web/Core/Orchard.Core.csproj index 67b970439..b59372d85 100644 --- a/src/Orchard.Web/Core/Orchard.Core.csproj +++ b/src/Orchard.Web/Core/Orchard.Core.csproj @@ -62,6 +62,7 @@ +