Created
February 10, 2012 23:49
-
-
Save gabesumner/1794193 to your computer and use it in GitHub Desktop.
Importing WordPress Export files into Sitefinity CMS (VERY unfinished)
This file contains 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
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WPimport.ascx.cs" Inherits="SitefinityWebApp.Widgets.WPimport" %> | |
<div> | |
<div style="color: red;"> | |
<asp:Literal ID="Error" Visible="false" runat="server" /> | |
</div> | |
<div> | |
<asp:Label AssociatedControlID="WordPressFile" runat="server">WordPress Export File: </asp:Label> | |
<asp:FileUpload ID="WordPressFile" runat="server" /> | |
</div> | |
<div> | |
<asp:Label AssociatedControlID="BlogDestination" runat="server">Import into this blog: </asp:Label> | |
<asp:DropDownList ID="BlogDestination" runat="server"> | |
<asp:ListItem Value="">No blogs found!</asp:ListItem> | |
</asp:DropDownList> | |
</div> | |
<div> | |
<asp:Label AssociatedControlID="CreateTags" runat="server">Create the tags? </asp:Label> | |
<asp:DropDownList ID="CreateTags" runat="server"> | |
<asp:ListItem>Yes</asp:ListItem> | |
<asp:ListItem>No</asp:ListItem> | |
</asp:DropDownList> | |
</div> | |
<div> | |
<asp:Label AssociatedControlID="CreateCategories" runat="server">Create the categories? </asp:Label> | |
<asp:DropDownList ID="CreateCategories" runat="server"> | |
<asp:ListItem>Yes</asp:ListItem> | |
<asp:ListItem>No</asp:ListItem> | |
</asp:DropDownList> | |
</div> | |
<div> | |
<asp:Button ID="Submit" Text="Let's do this!" runat="server" | |
onclick="Submit_Click" /> | |
</div> | |
</div> | |
<asp:PlaceHolder ID="ImportResults" Visible="false" runat="server"> | |
<hr /> | |
<asp:Repeater ID="ImportMessagesRepeater" runat="server"> | |
<HeaderTemplate> | |
<ul> | |
</HeaderTemplate> | |
<ItemTemplate> | |
<li><%# Container.DataItem %></li> | |
</ItemTemplate> | |
<FooterTemplate> | |
</ul> | |
</FooterTemplate> | |
</asp:Repeater> | |
</asp:PlaceHolder> |
This file contains 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 System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using Telerik.Sitefinity; | |
using System.Xml.Linq; | |
namespace SitefinityWebApp.Widgets | |
{ | |
public partial class WPimport : System.Web.UI.UserControl | |
{ | |
protected List<string> ImportMessages = new List<string>(); | |
protected void Page_Load(object sender, EventArgs e) | |
{ | |
var blogs = App.WorkWith().Blogs().Get().ToList(); | |
BlogDestination.DataValueField = "Id"; | |
BlogDestination.DataTextField = "Title"; | |
BlogDestination.DataSource = blogs; | |
BlogDestination.DataBind(); | |
} | |
protected void Submit_Click(object sender, EventArgs e) | |
{ | |
if (BlogDestination.SelectedValue == "") | |
{ | |
ShowError("No blog was selected."); | |
return; | |
} | |
if (WordPressFile.HasFile == false) | |
{ | |
ShowError("No WordPress import file was included."); | |
return; | |
} | |
XNamespace wp = "http://wordpress.org/export/1.1/"; | |
XNamespace content = "http://purl.org/rss/1.0/modules/content/"; | |
var xml = XDocument.Load(WordPressFile.FileContent); | |
// Tags | |
var tags = xml.Descendants(wp + "tag") | |
.Select(t => new | |
{ | |
Slug = t.Element(wp + "tag_slug").Value | |
}) | |
.ToList(); | |
// Posts | |
var posts = xml.Descendants("item") | |
.Where(t => t.Element(wp + "post_type").Value == "post") | |
.Where(t => t.Element(wp + "status").Value == "publish") | |
.Select(t => new { | |
Title = t.Element("title").Value, | |
Link = t.Element("link").Value, | |
PubDate = t.Element("pubDate").Value, | |
Encoded = t.Element(content + "encoded"), | |
Tags = t.Elements("category").Where(b => (string)b.Attribute("domain") == "post_tag").Select(c => c.Value).ToList() | |
}) | |
.ToList(); | |
// Tons of things to do: | |
// -------------------------------------- | |
// - Import Categories | |
// - Import Images | |
// - Import Comments | |
// - Wrap <p> tags around the paragraphs in WordPress posts | |
// - Create Sitefinity tags, categories, images & blog posts using the Sitefinity API | |
// - Preserve URL's and/or establish 301 redirects | |
foreach (var tag in tags) | |
{ | |
ShowImportMessage("Tag: " + tag.Slug); | |
} | |
foreach (var post in posts) | |
{ | |
ShowImportMessage("Post: " + post.Title); | |
} | |
ImportMessagesRepeater.DataSource = ImportMessages; | |
ImportMessagesRepeater.DataBind(); | |
} | |
private void ShowImportMessage(string Message) | |
{ | |
ImportResults.Visible = true; | |
ImportMessages.Add(Message); | |
} | |
private void ShowError(string ErrorMessage) | |
{ | |
Error.Visible = true; | |
Error.Text = ErrorMessage; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Any chance you finished coding this? I need to do a WP->SF migration and have only found one tool available.