Example #1
0
        internal static string HtmlWordpressElement(this XElement element, WordpressNamespaces namespaces, string name)
        {
            var wElement = WordpressElement(element, namespaces, name);

            if (wElement == null)
            {
                return(null);
            }

            var data = wElement.FirstNode as XCData;

            if (data == null)
            {
                return(null);
            }

            var value = data.Value.Trim();

            if (string.IsNullOrWhiteSpace(value))
            {
                return(null);
            }

            return(HttpUtility.HtmlDecode(value));
        }
        private void GetCategories(Blog blog, WordpressNamespaces namespaces, XElement channel) {

            // Loop through the elements and build the category list.
            IEnumerable<XElement> categories =
                from cat in channel.Elements(namespaces.WpNamespace + "category")
                select cat;

            foreach (XElement categoryElement in categories) {
                var createdCategory = InternalSchemaAssemblers.AssembleCategory(namespaces, categoryElement);
                if (blog.Categories.CategoryList.All(o => o.ID != createdCategory.ID))
                    blog.Categories.CategoryList.Add(createdCategory);
            }

            // WordPress stores the parent category as the description, but BlogML wants the ID.  Now that we have a
            // complete list, we'll go back through and fix them.
            IEnumerable<Category> children =
                from a in blog.Categories.CategoryList
                where a.ParentCategory != null
                select a;

            foreach (Category child in children) {
                IEnumerable<Category> parent =
                    from a in blog.Categories.CategoryList
                    where a.Title == child.ParentCategory
                    select a;

                if (parent.Any()) {
                    child.ParentCategory = parent.ElementAt(0).ID;
                }
            }
        }
Example #3
0
        private Blog CreateTopLevelBlog(WordpressNamespaces namespaces, XElement channel)
        {
            Blog blog = new Blog(_clock);

            blog.DateCreated = DateTime.Parse(Constants.ParseRssDate(channel.Element("pubDate").Value));

            blog.Title       = new Title();
            blog.Title.Value = channel.Element("title").Value;

            blog.SubTitle       = new Title();
            blog.SubTitle.Value = channel.Element("description").Value;


            // This is the first element we use the WP namespace; make sure it works.
            // (See issue #4 - thanks to stephenway for reporting it.)
            var rootUrlElement = channel.WordpressElement(namespaces, "base_blog_url");

            if (rootUrlElement == null)
            {
                throw new NotSupportedException("Unable to determine the blog base URL.");
            }

            blog.RootURL = rootUrlElement.Value;

            blog.Authors    = new Authors();
            blog.Categories = new Categories();
            blog.Tags       = new Tags();
            blog.Posts      = new Posts();

            return(blog);
        }
        private Blog CreateTopLevelBlog(WordpressNamespaces namespaces, XElement channel) {
            Blog blog = new Blog(_clock);

            blog.DateCreated = DateTime.Parse(Constants.ParseRssDate(channel.Element("pubDate").Value));
			
            blog.Title = new Title();
            blog.Title.Value = channel.Element("title").Value;
			
            blog.SubTitle = new Title();
            blog.SubTitle.Value = channel.Element("description").Value;


            // This is the first element we use the WP namespace; make sure it works.
            // (See issue #4 - thanks to stephenway for reporting it.)
            var rootUrlElement = channel.WordpressElement(namespaces, "base_blog_url");

            if (rootUrlElement == null)
                throw new NotSupportedException("Unable to determine the blog base URL.");

            blog.RootURL = rootUrlElement.Value;

            blog.Authors = new Authors();
            blog.Categories = new Categories();
            blog.Tags = new Tags();
            blog.Posts = new Posts();

            return blog;
        }
Example #5
0
        internal static T WordpressElement <T>(this XElement element, WordpressNamespaces namespaces, string name, Func <XElement, T> afterwork)
        {
            var wElement = WordpressElement(element, namespaces, name);

            if (wElement != null)
            {
                return(afterwork(wElement));
            }

            return(default(T));
        }
Example #6
0
        internal static Trackback AssembleTrackback(WordpressNamespaces namespaces, XElement trackbackElement)
        {
            Trackback trackback = new Trackback();

            trackback.ID          = trackbackElement.WordpressElement(namespaces, "comment_id").Value;
            trackback.Title       = ((XCData)trackbackElement.WordpressElement(namespaces, "comment_author").FirstNode).Value;
            trackback.DateCreated =
                DateTime.Parse(trackbackElement.WordpressElement(namespaces, "comment_date_gmt").Value);

            trackback.Url = trackbackElement.WordpressElement(namespaces, "comment_author_url").Value;

            return(trackback);
        }
        internal static Tag AssembleTag(WordpressNamespaces namespaces, XElement tagElement) {
            var tag = new Tag();

            tag.ID =
                tagElement.WordpressElement(namespaces, "tag_slug", (e) => e.Value.Trim());

            tag.Slug =
                tagElement.WordpressElement(namespaces, "tag_slug", (e) => e.Value.Trim());

            tag.Title = 
                tagElement.HtmlWordpressElement(namespaces, "tag_name");

            return tag;
        }
Example #8
0
        internal static Tag AssembleTag(WordpressNamespaces namespaces, XElement tagElement)
        {
            var tag = new Tag();

            tag.ID =
                tagElement.WordpressElement(namespaces, "tag_slug", (e) => e.Value.Trim());

            tag.Slug =
                tagElement.WordpressElement(namespaces, "tag_slug", (e) => e.Value.Trim());

            tag.Title =
                tagElement.HtmlWordpressElement(namespaces, "tag_name");

            return(tag);
        }
        internal static Category AssembleCategory(WordpressNamespaces namespaces, XElement categoryElement) {
            var category = new Category();

            category.ID =
                categoryElement.WordpressElement(namespaces, "category_nicename", (e) => e.Value.Trim());

            category.Description = 
                categoryElement.WordpressElement(namespaces, "category_description", (e) =>((XCData)e.FirstNode).Value.Trim());

            category.Title =
                categoryElement.HtmlWordpressElement(namespaces, "cat_name");

            category.ParentCategory = 
                categoryElement.WordpressElement(namespaces, "category_parent", (e) => e.Value.Trim());

            return category;
        }
Example #10
0
        internal static Category AssembleCategory(WordpressNamespaces namespaces, XElement categoryElement)
        {
            var category = new Category();

            category.ID =
                categoryElement.WordpressElement(namespaces, "category_nicename", (e) => e.Value.Trim());

            category.Description =
                categoryElement.WordpressElement(namespaces, "category_description", (e) => ((XCData)e.FirstNode).Value.Trim());

            category.Title =
                categoryElement.HtmlWordpressElement(namespaces, "cat_name");

            category.ParentCategory =
                categoryElement.WordpressElement(namespaces, "category_parent", (e) => e.Value.Trim());

            return(category);
        }
Example #11
0
        private void GetPosts(Blog blog, WordpressNamespaces namespaces, XElement channel)
        {
            IEnumerable <XElement> posts =
                from item in channel.Elements("item")
                where item.WordpressElement(namespaces, "status").Value == "publish"
                select item;

            // NGM Might want update urls within content to stop redirects?

            foreach (XElement item in posts)
            {
                Post post = InternalSchemaAssemblers.AssemblePost(namespaces, item);

                // We need to get the author reference separately, as we need the AuthorList from the blog.
                AuthorReference author = new AuthorReference();
                author.ID = GetAuthorReference(blog,
                                               ((XText)item.Element(namespaces.DcNamespace + "creator").FirstNode).Value);
                post.Authors.AuthorReferenceList.Add(author);

                blog.Posts.PostList.Add(post);
            }
        }
        public Blog Assemble(Stream stream) {
            var file = XElement.Load(stream);

            // Get the RSS channel.
            XElement channel = file.Element("channel");

            WordpressNamespaces namespaces = new WordpressNamespaces {
                ExcerptNamespace = file.GetNamespaceOfPrefix("excerpt"),
                ContentNamespace = file.GetNamespaceOfPrefix("content"),
                WfwNamespace = file.GetNamespaceOfPrefix("wfw"),
                DcNamespace = file.GetNamespaceOfPrefix("dc"),
                WpNamespace = file.GetNamespaceOfPrefix("wp")
            };

            Blog blog = CreateTopLevelBlog(namespaces, channel);

            GetCategories(blog, namespaces, channel);
            GetTags(blog, namespaces, channel);
            GetPosts(blog, namespaces, channel);

            return blog;
        }
Example #13
0
        internal static Comment AssembleComment(WordpressNamespaces namespaces, XElement commentElement)
        {
            Comment comment = new Comment();

            // Node (parent) properties.
            comment.ID          = commentElement.WordpressElement(namespaces, "comment_id").Value;
            comment.Title       = comment.ID;
            comment.DateCreated = DateTime.Parse(commentElement.WordpressElement(namespaces, "comment_date_gmt").Value);

            comment.Content       = new Content();
            comment.Content.Type  = Content.TypeHTML;
            comment.Content.Value = ((XCData)commentElement.WordpressElement(namespaces, "comment_content").FirstNode).Value;

            comment.UserName = ((XCData)commentElement.WordpressElement(namespaces, "comment_author").FirstNode).Value;

            string email = commentElement.WordpressElement(namespaces, "comment_author_email").Value;

            if (!string.IsNullOrWhiteSpace(email))
            {
                comment.UserEmail = email;
            }

            string url = commentElement.WordpressElement(namespaces, "comment_author_url").Value;

            if (!string.IsNullOrWhiteSpace(url))
            {
                comment.UserURL = url;
            }

            string approved = commentElement.WordpressElement(namespaces, "comment_approved").Value;

            if (!string.IsNullOrWhiteSpace(approved))
            {
                comment.Approved = approved == "1";
            }

            return(comment);
        }
Example #14
0
        public Blog Assemble(Stream stream)
        {
            var file = XElement.Load(stream);

            // Get the RSS channel.
            XElement channel = file.Element("channel");

            WordpressNamespaces namespaces = new WordpressNamespaces {
                ExcerptNamespace = file.GetNamespaceOfPrefix("excerpt"),
                ContentNamespace = file.GetNamespaceOfPrefix("content"),
                WfwNamespace     = file.GetNamespaceOfPrefix("wfw"),
                DcNamespace      = file.GetNamespaceOfPrefix("dc"),
                WpNamespace      = file.GetNamespaceOfPrefix("wp")
            };

            Blog blog = CreateTopLevelBlog(namespaces, channel);

            GetCategories(blog, namespaces, channel);
            GetTags(blog, namespaces, channel);
            GetPosts(blog, namespaces, channel);

            return(blog);
        }
Example #15
0
        private void GetTags(Blog blog, WordpressNamespaces namespaces, XElement channel)
        {
            // Loop through the elements and build the category list.
            IEnumerable <XElement> tags =
                from tag in channel.Elements(namespaces.WpNamespace + "tag")
                select tag;

            foreach (XElement tagElement in tags)
            {
                var createdTag = InternalSchemaAssemblers.AssembleTag(namespaces, tagElement);
                if (blog.Tags.TagList.All(o => o.ID != createdTag.ID))
                {
                    blog.Tags.TagList.Add(createdTag);
                }
            }

            // WordPress stores the parent category as the description, but BlogML wants the ID.  Now that we have a
            // complete list, we'll go back through and fix them.
            IEnumerable <Tag> children =
                from a in blog.Tags.TagList
                where a.Slug != null
                select a;

            foreach (Tag child in children)
            {
                IEnumerable <Tag> parent =
                    from a in blog.Tags.TagList
                    where a.Title == child.Slug
                    select a;

                if (0 < parent.Count())
                {
                    child.Slug = parent.ElementAt(0).ID;
                }
            }
        }
Example #16
0
        private void GetCategories(Blog blog, WordpressNamespaces namespaces, XElement channel)
        {
            // Loop through the elements and build the category list.
            IEnumerable <XElement> categories =
                from cat in channel.Elements(namespaces.WpNamespace + "category")
                select cat;

            foreach (XElement categoryElement in categories)
            {
                var createdCategory = InternalSchemaAssemblers.AssembleCategory(namespaces, categoryElement);
                if (blog.Categories.CategoryList.All(o => o.ID != createdCategory.ID))
                {
                    blog.Categories.CategoryList.Add(createdCategory);
                }
            }

            // WordPress stores the parent category as the description, but BlogML wants the ID.  Now that we have a
            // complete list, we'll go back through and fix them.
            IEnumerable <Category> children =
                from a in blog.Categories.CategoryList
                where a.ParentCategory != null
                select a;

            foreach (Category child in children)
            {
                IEnumerable <Category> parent =
                    from a in blog.Categories.CategoryList
                    where a.Title == child.ParentCategory
                    select a;

                if (parent.Any())
                {
                    child.ParentCategory = parent.ElementAt(0).ID;
                }
            }
        }
Example #17
0
        internal static Post AssemblePost(WordpressNamespaces namespaces, XElement postElement)
        {
            Post post = new Post();

            // Node (parent) properties.
            post.ID           = postElement.WordpressElement(namespaces, "post_id").Value;
            post.Title        = postElement.Element("title").Value;
            post.DateCreated  = DateTime.Parse(Constants.ParseRssDate(postElement.Element("pubDate").Value));
            post.DateModified = DateTime.Parse(Constants.ParseRssDate(postElement.Element("pubDate").Value));
            post.PostUrl      = (new Uri(postElement.Element("link").Value).AbsolutePath).TrimStart('/'); // NGM
            post.Approved     = true;

            // Object properties.
            post.Content       = new Content();
            post.Content.Type  = Content.TypeHTML;
            post.Content.Value = ((XCData)postElement.ContentElement(namespaces, "encoded").FirstNode).Value;

            post.PostName       = new Title();
            post.PostName.Type  = Content.TypeHTML;
            post.PostName.Value = postElement.Element("title").Value;

            string excerpt = ((XCData)postElement.Element(namespaces.ExcerptNamespace + "encoded").FirstNode).Value;

            if (String.Empty == excerpt)
            {
                post.HasExcerpt = false;
            }
            else
            {
                post.Excerpt       = new Content();
                post.Excerpt.Type  = Content.TypeHTML;
                post.Excerpt.Value = excerpt;
                post.HasExcerpt    = true;
            }

            // Category references.
            IEnumerable <XElement> categories =
                from cat in postElement.Elements("category")
                where cat.Attribute("domain") != null && cat.Attribute("domain").Value == "category"
                select cat;

            if (categories.Any())
            {
                post.Categories = new CategoryReferences();
                foreach (XElement reference in categories)
                {
                    post.Categories.CategoryReferenceList.Add(AssembleCategoryReference(reference));
                }
            }

            // Tag references.
            IEnumerable <XElement> tags =
                from tag in postElement.Elements("category")
                where tag.Attribute("domain") != null && (tag.Attribute("domain").Value == "post_tag" || tag.Attribute("domain").Value == "tag") && tag.Attribute("nicename") != null
                select tag;

            if (tags.Any())
            {
                post.Tags = new TagReferences();
                foreach (XElement reference in tags)
                {
                    post.Tags.TagReferenceList.Add(new TagReference(reference));
                }
            }

            // Comments on this post.
            IEnumerable <XElement> comments =
                from comment in postElement.Elements(namespaces.WpNamespace + "comment")
                where comment.WordpressElement(namespaces, "comment_approved").Value == "1" &&
                string.IsNullOrEmpty(comment.WordpressElement(namespaces, "comment_type").Value)
                select comment;

            post.Comments = new Comments();

            if (comments.Any())
            {
                foreach (XElement comment in comments)
                {
                    post.Comments.CommentList.Add(AssembleComment(namespaces, comment));
                }
            }

            if (postElement.WordpressElement(namespaces, "comment_status").Value == "open")
            {
                post.Comments.Enabled = true;
            }
            else
            {
                post.Comments.Enabled = false;
            }

            // Trackbacks for this post.
            IEnumerable <XElement> trackbax =
                from tb in postElement.Elements(namespaces.WpNamespace + "comment")
                where tb.WordpressElement(namespaces, "comment_approved").Value == "1" &&
                ((tb.WordpressElement(namespaces, "comment_type").Value == "trackback") ||
                 (tb.WordpressElement(namespaces, "comment_type").Value == "pingback"))
                select tb;

            if (trackbax.Any())
            {
                post.Trackbacks = new Trackbacks();
                foreach (XElement trackback in trackbax)
                {
                    post.Trackbacks.TrackbackList.Add(AssembleTrackback(namespaces, trackback));
                }
            }

            post.Authors = new AuthorReferences();
            post.Authors.AuthorReferenceList.Add(new AuthorReference {
                ID = postElement.Element(namespaces.DcNamespace + "creator").Value
            });

            return(post);
        }
Example #18
0
 internal static XElement WordpressElement(this XElement element, WordpressNamespaces namespaces, string name)
 {
     return(element.Element(namespaces.WpNamespace + name));
 }
        private void GetPosts(Blog blog, WordpressNamespaces namespaces, XElement channel) {

            IEnumerable<XElement> posts =
                from item in channel.Elements("item")
                where item.WordpressElement(namespaces, "status").Value == "publish"
                select item;

            // NGM Might want update urls within content to stop redirects?

            foreach (XElement item in posts) {

                Post post = InternalSchemaAssemblers.AssemblePost(namespaces, item);

                // We need to get the author reference separately, as we need the AuthorList from the blog.
                AuthorReference author = new AuthorReference();
                author.ID = GetAuthorReference(blog,
                                               ((XText)item.Element(namespaces.DcNamespace + "creator").FirstNode).Value);
                post.Authors.AuthorReferenceList.Add(author);

                blog.Posts.PostList.Add(post);
            }
        }
        internal static Trackback AssembleTrackback(WordpressNamespaces namespaces, XElement trackbackElement) {
            Trackback trackback = new Trackback();

            trackback.ID = trackbackElement.WordpressElement(namespaces, "comment_id").Value;
            trackback.Title = ((XCData)trackbackElement.WordpressElement(namespaces, "comment_author").FirstNode).Value;
            trackback.DateCreated =
                DateTime.Parse(trackbackElement.WordpressElement(namespaces, "comment_date_gmt").Value);

            trackback.Url = trackbackElement.WordpressElement(namespaces, "comment_author_url").Value;

            return trackback;
        }
        internal static Post AssemblePost(WordpressNamespaces namespaces, XElement postElement) {
			Post post = new Post();

			// Node (parent) properties.
            post.ID = postElement.WordpressElement(namespaces, "post_id").Value;
            post.Title = postElement.Element("title").Value;
            post.DateCreated = DateTime.Parse(Constants.ParseRssDate(postElement.Element("pubDate").Value));
            post.DateModified = DateTime.Parse(Constants.ParseRssDate(postElement.Element("pubDate").Value));
            post.PostUrl = (new Uri(postElement.Element("link").Value).AbsolutePath).TrimStart('/'); // NGM
            post.Approved = true;

			// Object properties.
            post.Content = new Content();
            post.Content.Type = Content.TypeHTML;
            post.Content.Value = ((XCData)postElement.ContentElement(namespaces, "encoded").FirstNode).Value;

            post.PostName = new Title();
            post.PostName.Type = Content.TypeHTML;
            post.PostName.Value = postElement.Element("title").Value;

            string excerpt = ((XCData)postElement.Element(namespaces.ExcerptNamespace + "encoded").FirstNode).Value;
			if (String.Empty == excerpt) {
                post.HasExcerpt = false;
			}
			else {
                post.Excerpt = new Content();
                post.Excerpt.Type = Content.TypeHTML;
                post.Excerpt.Value = excerpt;
                post.HasExcerpt = true;
			}
			
			// Category references.
			IEnumerable<XElement> categories =
				from cat in postElement.Elements("category")
				where cat.Attribute("domain") != null && cat.Attribute("domain").Value == "category"
				select cat;
			
			if (categories.Any()) {
                post.Categories = new CategoryReferences();
				foreach(XElement reference in categories) {
                    post.Categories.CategoryReferenceList.Add(AssembleCategoryReference(reference));
				}
            }

            // Tag references.
            IEnumerable<XElement> tags =
                from tag in postElement.Elements("category")
                where tag.Attribute("domain") != null && (tag.Attribute("domain").Value == "post_tag" || tag.Attribute("domain").Value == "tag") && tag.Attribute("nicename") != null
                select tag;

            if (tags.Any()) {
                post.Tags = new TagReferences();
                foreach (XElement reference in tags) {
                    post.Tags.TagReferenceList.Add(new TagReference(reference));
                }
            }
			
			// Comments on this post.
            IEnumerable<XElement> comments =
                from comment in postElement.Elements(namespaces.WpNamespace + "comment")
                where comment.WordpressElement(namespaces, "comment_approved").Value == "1"
                      && string.IsNullOrEmpty(comment.WordpressElement(namespaces, "comment_type").Value)
				select comment;

            post.Comments = new Comments();

			if (comments.Any()) {
				foreach(XElement comment in comments) {
                    post.Comments.CommentList.Add(AssembleComment(namespaces, comment));
				}
			}

            if (postElement.WordpressElement(namespaces, "comment_status").Value == "open")
                post.Comments.Enabled = true;
            else {
                post.Comments.Enabled = false;
            }

			// Trackbacks for this post.
			IEnumerable<XElement> trackbax =
				from tb in postElement.Elements(namespaces.WpNamespace + "comment")
				where tb.WordpressElement(namespaces, "comment_approved").Value == "1"
					&& ((tb.WordpressElement(namespaces, "comment_type").Value == "trackback")
					    || (tb.WordpressElement(namespaces, "comment_type").Value == "pingback"))
				select tb;
			
			if (trackbax.Any()) {
				post.Trackbacks = new Trackbacks();
				foreach(XElement trackback in trackbax) {
                    post.Trackbacks.TrackbackList.Add(AssembleTrackback(namespaces, trackback));
				}
			}

            post.Authors = new AuthorReferences();
            post.Authors.AuthorReferenceList.Add(new AuthorReference { ID = postElement.Element(namespaces.DcNamespace + "creator").Value });

		    return post;
		}
        internal static Comment AssembleComment(WordpressNamespaces namespaces, XElement commentElement) {
            Comment comment = new Comment();

            // Node (parent) properties.
            comment.ID = commentElement.WordpressElement(namespaces, "comment_id").Value;
            comment.Title = comment.ID;
            comment.DateCreated = DateTime.Parse(commentElement.WordpressElement(namespaces, "comment_date_gmt").Value);

            comment.Content = new Content();
            comment.Content.Type = Content.TypeHTML;
            comment.Content.Value = ((XCData)commentElement.WordpressElement(namespaces, "comment_content").FirstNode).Value;

            comment.UserName = ((XCData)commentElement.WordpressElement(namespaces, "comment_author").FirstNode).Value;

            string email = commentElement.WordpressElement(namespaces, "comment_author_email").Value;
            if (!string.IsNullOrWhiteSpace(email)) {
                comment.UserEmail = email;
            }

            string url = commentElement.WordpressElement(namespaces, "comment_author_url").Value;
            if (!string.IsNullOrWhiteSpace(url)) {
                comment.UserURL = url;
            }

            string approved = commentElement.WordpressElement(namespaces, "comment_approved").Value;
            if (!string.IsNullOrWhiteSpace(approved)) {
                comment.Approved = approved == "1";
            }

            return comment;
        }
        private void GetTags(Blog blog, WordpressNamespaces namespaces, XElement channel) {
            // Loop through the elements and build the category list.
            IEnumerable<XElement> tags =
                from tag in channel.Elements(namespaces.WpNamespace + "tag")
                select tag;

            foreach (XElement tagElement in tags) {
                var createdTag = InternalSchemaAssemblers.AssembleTag(namespaces, tagElement);
                if (blog.Tags.TagList.All(o => o.ID != createdTag.ID))
                    blog.Tags.TagList.Add(createdTag);
            }

            // WordPress stores the parent category as the description, but BlogML wants the ID.  Now that we have a
            // complete list, we'll go back through and fix them.
            IEnumerable<Tag> children =
                from a in blog.Tags.TagList
                where a.Slug != null
                select a;

            foreach (Tag child in children) {
                IEnumerable<Tag> parent =
                    from a in blog.Tags.TagList
                    where a.Title == child.Slug
                    select a;

                if (0 < parent.Count()) {
                    child.Slug = parent.ElementAt(0).ID;
                }
            }
        }