mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-14 10:54:50 +08:00
Enabling pre-dating of content from Live Writer
--HG-- branch : dev
This commit is contained in:
91
src/Orchard.Web/Core/Common/Services/XmlRpcHandler.cs
Normal file
91
src/Orchard.Web/Core/Common/Services/XmlRpcHandler.cs
Normal file
@@ -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<IXmlRpcDriver> drivers) {
|
||||
if (!publish)
|
||||
return;
|
||||
|
||||
var publishedUtc = content.Optional<DateTime?>("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<CommonPart>())
|
||||
return;
|
||||
|
||||
contentItem.As<CommonPart>().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<object> _process;
|
||||
|
||||
public XmlRpcDriver(Action<object> process) {
|
||||
_process = process;
|
||||
}
|
||||
|
||||
public void Process(object item) {
|
||||
_process(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -62,6 +62,7 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Common\Models\CommonPartVersionRecord.cs" />
|
||||
<Compile Include="Common\Services\XmlRpcHandler.cs" />
|
||||
<Compile Include="Containers\Controllers\ItemController.cs" />
|
||||
<Compile Include="Containers\Drivers\ContainablePartDriver.cs" />
|
||||
<Compile Include="Containers\Drivers\ContainerPartDriver.cs" />
|
||||
|
Reference in New Issue
Block a user