Ejemplo n.º 1
0
        public async Task <bool> Import(
            int userId,
            string fileName,
            int blogRootNode,
            bool overwrite,
            string regexMatch,
            string regexReplace,
            bool publishAll,
            bool exportDisqusXml  = false,
            bool importFirstImage = false)
        {
            try
            {
                if (!File.Exists(fileName))
                {
                    throw new FileNotFoundException("File not found: " + fileName);
                }

                var root = _contentService.GetById(blogRootNode);
                if (root == null)
                {
                    throw new InvalidOperationException("No node found with id " + blogRootNode);
                }
                if (!root.ContentType.Alias.InvariantEquals("Articulate"))
                {
                    throw new InvalidOperationException("The node with id " + blogRootNode + " is not an Articulate root node");
                }

                using (var stream = File.OpenRead(fileName))
                {
                    var document = new BlogMLDocument();
                    document.Load(stream);

                    stream.Position = 0;
                    var xdoc = XDocument.Load(stream);

                    var authorIdsToName = ImportAuthors(userId, root, document.Authors);

                    var imported = await ImportPosts(userId, xdoc, root, document.Posts, document.Authors.ToArray(), document.Categories.ToArray(), authorIdsToName, overwrite, regexMatch, regexReplace, publishAll, importFirstImage);

                    if (exportDisqusXml)
                    {
                        var xDoc = _disqusXmlExporter.Export(imported, document);

                        using (var memStream = new MemoryStream())
                        {
                            xDoc.Save(memStream);
                            _fileSystem.AddFile("DisqusXmlExport.xml", memStream, true);
                        }
                    }
                }

                return(false);
            }
            catch (Exception ex)
            {
                _logger.Error <BlogMlImporter>(ex, "Importing failed with errors");
                return(true);
            }
        }
Ejemplo n.º 2
0
        private void AddBlogPosts(IContent archiveNode, BlogMLDocument blogMlDoc, string tagGroup)
        {
            const int pageSize  = 1000;
            var       pageIndex = 0;

            IContent[] posts;
            do
            {
                long total;
                posts = _applicationContext.Services.ContentService.GetPagedChildren(archiveNode.Id, pageIndex, pageSize, out total, "createDate").ToArray();

                foreach (var child in posts)
                {
                    string content = "";
                    if (child.ContentType.Alias.InvariantEquals("ArticulateRichText"))
                    {
                        //TODO: this would also need to export all macros
                        content = child.GetValue <string>("richText");
                    }
                    else if (child.ContentType.Alias.InvariantEquals("ArticulateMarkdown"))
                    {
                        content = child.GetValue <string>("markdown");
                        var markdown = new MarkdownDeep.Markdown();
                        content = markdown.Transform(content);
                    }

                    var blogMlPost = new BlogMLPost()
                    {
                        Id             = child.Key.ToString(),
                        Name           = new BlogMLTextConstruct(child.Name),
                        Title          = new BlogMLTextConstruct(child.Name),
                        ApprovalStatus = BlogMLApprovalStatus.Approved,
                        PostType       = BlogMLPostType.Normal,
                        CreatedOn      = child.CreateDate,
                        LastModifiedOn = child.UpdateDate,
                        Content        = new BlogMLTextConstruct(content, BlogMLContentType.Html),
                        Excerpt        = new BlogMLTextConstruct(child.GetValue <string>("excerpt")),
                        Url            = new Uri(_umbracoContext.UrlProvider.GetUrl(child.Id), UriKind.RelativeOrAbsolute)
                    };

                    var author = blogMlDoc.Authors.FirstOrDefault(x => x.Title != null && x.Title.Content.InvariantEquals(child.GetValue <string>("author")));
                    if (author != null)
                    {
                        blogMlPost.Authors.Add(author.Id);
                    }

                    var categories = _applicationContext.Services.TagService.GetTagsForEntity(child.Id, tagGroup);
                    foreach (var category in categories)
                    {
                        blogMlPost.Categories.Add(category.Id.ToString());
                    }

                    //TODO: Tags isn't natively supported

                    blogMlDoc.AddPost(blogMlPost);
                }

                pageIndex++;
            } while (posts.Length == pageSize);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Provides example code for the Load(XmlReader) method
        /// </summary>
        public static void LoadXmlReaderExample()
        {
            #region Load(XmlReader reader)
            BlogMLDocument document = new BlogMLDocument();

            using (Stream stream = new FileStream("BlogMLDocument.xml", FileMode.Open, FileAccess.Read))
            {
                XmlReaderSettings settings = new XmlReaderSettings();
                settings.IgnoreComments   = true;
                settings.IgnoreWhitespace = true;

                using (XmlReader reader = XmlReader.Create(stream, settings))
                {
                    document.Load(reader);

                    foreach (BlogMLPost post in document.Posts)
                    {
                        if (post.ApprovalStatus == BlogMLApprovalStatus.Approved)
                        {
                            //  Perform some processing on the blog post
                        }
                    }
                }
            }
            #endregion
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Provides example code for the LoadAsync(Uri, Object) method
        /// </summary>
        public static void LoadAsyncExample()
        {
            BlogMLDocument document = new BlogMLDocument();

            document.Loaded += new EventHandler <SyndicationResourceLoadedEventArgs>(ResourceLoadedCallback);

            document.LoadAsync(new Uri("http://www.example.org/blog/blogML.axd"), null);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Provides example code for the Save(Stream) method
        /// </summary>
        public static void SaveStreamExample()
        {
            BlogMLDocument document = new BlogMLDocument();

            //  Modify document state using public properties and methods

            using (Stream stream = new FileStream("BlogMLDocument.xml", FileMode.Create, FileAccess.Write))
            {
                document.Save(stream);
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Provides example code for the BlogMLDocument.Create(Uri) method
        /// </summary>
        public static void CreateExample()
        {
            BlogMLDocument document = BlogMLDocument.Create(new Uri("http://www.example.org/blog/blogML.axd"));

            foreach (BlogMLPost post in document.Posts)
            {
                if (post.ApprovalStatus == BlogMLApprovalStatus.Approved)
                {
                    //  Perform some processing on the blog post
                }
            }
        }
Ejemplo n.º 7
0
 private void WriteFile(BlogMLDocument blogMlDoc)
 {
     using (var stream = new MemoryStream())
     {
         blogMlDoc.Save(stream, new SyndicationResourceSaveSettings()
         {
             CharacterEncoding = Encoding.UTF8
         });
         stream.Position = 0;
         _fileSystem.AddFile("BlogMlExport.xml", stream, true);
     }
 }
Ejemplo n.º 8
0
 private void AddBlogAuthors(IContent authorsNode, BlogMLDocument blogMlDoc)
 {
     foreach (var author in _contentService.GetPagedChildren(authorsNode.Id, 0, int.MaxValue, out long totalAuthors))
     {
         var blogMlAuthor = new BlogMLAuthor();
         blogMlAuthor.Id             = author.Key.ToString();
         blogMlAuthor.CreatedOn      = author.CreateDate;
         blogMlAuthor.LastModifiedOn = author.UpdateDate;
         blogMlAuthor.ApprovalStatus = BlogMLApprovalStatus.Approved;
         blogMlAuthor.Title          = new BlogMLTextConstruct(author.Name);
         blogMlDoc.Authors.Add(blogMlAuthor);
     }
 }
Ejemplo n.º 9
0
 private void AddBlogAuthors(IContent authorsNode, BlogMLDocument blogMlDoc)
 {
     foreach (var author in _applicationContext.Services.ContentService.GetChildren(authorsNode.Id))
     {
         var blogMlAuthor = new BlogMLAuthor();
         blogMlAuthor.Id             = author.Key.ToString();
         blogMlAuthor.CreatedOn      = author.CreateDate;
         blogMlAuthor.LastModifiedOn = author.UpdateDate;
         blogMlAuthor.ApprovalStatus = BlogMLApprovalStatus.Approved;
         blogMlAuthor.Title          = new BlogMLTextConstruct(author.Name);
         blogMlDoc.Authors.Add(blogMlAuthor);
     }
 }
Ejemplo n.º 10
0
        //============================================================
        //	ASYNC METHODS
        //============================================================
        /// <summary>
        /// Provides example code for the LoadAsync(Uri, Object) method
        /// </summary>
        public static void LoadAsyncExample()
        {
            #region LoadAsync(Uri source, Object userToken)
            //------------------------------------------------------------
            //	Load resource asynchronously using event-based notification
            //------------------------------------------------------------
            BlogMLDocument document = new BlogMLDocument();

            document.Loaded += new EventHandler <SyndicationResourceLoadedEventArgs>(ResourceLoadedCallback);

            document.LoadAsync(new Uri("http://www.example.org/blog/blogML.axd"), null);
            #endregion
        }
        /// <summary>
        /// Modifies the <see cref="ISyndicationResource"/> to match the data source.
        /// </summary>
        /// <param name="resource">The BlogML <see cref="ISyndicationResource"/> to be filled.</param>
        /// <param name="resourceMetadata">A <see cref="SyndicationResourceMetadata"/> object that represents the meta-data describing the <paramref name="resource"/>.</param>
        /// <exception cref="ArgumentNullException">The <paramref name="resource"/> is a null reference (Nothing in Visual Basic).</exception>
        /// <exception cref="ArgumentNullException">The <paramref name="resourceMetadata"/> is a null reference (Nothing in Visual Basic).</exception>
        private void FillBlogMLResource(ISyndicationResource resource, SyndicationResourceMetadata resourceMetadata)
        {
            Guard.ArgumentNotNull(resource, "resource");
            Guard.ArgumentNotNull(resourceMetadata, "resourceMetadata");

            BlogMLDocument blogMLDocument = resource as BlogMLDocument;
            BlogML20SyndicationResourceAdapter blogML20Adapter = new BlogML20SyndicationResourceAdapter(this.Navigator, this.Settings);

            if (resourceMetadata.Version == new Version("2.0"))
            {
                blogML20Adapter.Fill(blogMLDocument);
            }
        }
Ejemplo n.º 12
0
        private BlogMLDocument GetDocument(string fileName)
        {
            if (!File.Exists(fileName))
            {
                throw new FileNotFoundException("File not found: " + fileName);
            }

            using (var stream = File.OpenRead(fileName))
            {
                var document = new BlogMLDocument();
                document.Load(stream);
                return(document);
            }
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Provides example code for the Load(Uri, ICredentials, IWebProxy) method
        /// </summary>
        public static void LoadUriExample()
        {
            BlogMLDocument document = new BlogMLDocument();
            Uri            source   = new Uri("http://www.example.org/blog/blogML.axd");

            document.Load(source, CredentialCache.DefaultNetworkCredentials, null);

            foreach (BlogMLPost post in document.Posts)
            {
                if (post.ApprovalStatus == BlogMLApprovalStatus.Approved)
                {
                    //  Perform some processing on the blog post
                }
            }
        }
Ejemplo n.º 14
0
        /// <summary>
        /// Provides example code for the Load(IXPathNavigable) method
        /// </summary>
        public static void LoadIXPathNavigableExample()
        {
            XPathDocument source = new XPathDocument("http://www.example.org/blog/blogML.axd");

            BlogMLDocument document = new BlogMLDocument();

            document.Load(source);

            foreach (BlogMLPost post in document.Posts)
            {
                if (post.ApprovalStatus == BlogMLApprovalStatus.Approved)
                {
                    //  Perform some processing on the blog post
                }
            }
        }
Ejemplo n.º 15
0
        /// <summary>
        /// Provides example code for the Load(Stream) method
        /// </summary>
        public static void LoadStreamExample()
        {
            BlogMLDocument document = new BlogMLDocument();

            using (Stream stream = new FileStream("BlogMLDocument.xml", FileMode.Open, FileAccess.Read))
            {
                document.Load(stream);

                foreach (BlogMLPost post in document.Posts)
                {
                    if (post.ApprovalStatus == BlogMLApprovalStatus.Approved)
                    {
                        //  Perform some processing on the blog post
                    }
                }
            }
        }
Ejemplo n.º 16
0
        /// <summary>
        /// Provides example code for the Save(XmlWriter) method
        /// </summary>
        public static void SaveXmlWriterExample()
        {
            BlogMLDocument document = new BlogMLDocument();

            //  Modify document state using public properties and methods

            using (Stream stream = new FileStream("BlogMLDocument.xml", FileMode.Create, FileAccess.Write))
            {
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Indent = true;

                using (XmlWriter writer = XmlWriter.Create(stream, settings))
                {
                    document.Save(writer);
                }
            }
        }
        /// <summary>
        /// Modifies the <see cref="ISyndicationResource"/> to match the data source.
        /// </summary>
        /// <param name="resource">The BlogML <see cref="ISyndicationResource"/> to be filled.</param>
        /// <param name="resourceMetadata">A <see cref="SyndicationResourceMetadata"/> object that represents the meta-data describing the <paramref name="resource"/>.</param>
        /// <exception cref="ArgumentNullException">The <paramref name="resource"/> is a null reference (Nothing in Visual Basic).</exception>
        /// <exception cref="ArgumentNullException">The <paramref name="resourceMetadata"/> is a null reference (Nothing in Visual Basic).</exception>
        private void FillBlogMLResource(ISyndicationResource resource, SyndicationResourceMetadata resourceMetadata)
        {
            //------------------------------------------------------------
            //	Validate parameter
            //------------------------------------------------------------
            Guard.ArgumentNotNull(resource, "resource");
            Guard.ArgumentNotNull(resourceMetadata, "resourceMetadata");

            //------------------------------------------------------------
            //	Fill syndication resource using appropriate data adapter
            //------------------------------------------------------------
            BlogMLDocument blogMLDocument = resource as BlogMLDocument;
            BlogML20SyndicationResourceAdapter blogML20Adapter = new BlogML20SyndicationResourceAdapter(this.Navigator, this.Settings);

            if (resourceMetadata.Version == new Version("2.0"))
            {
                blogML20Adapter.Fill(blogMLDocument);
            }
        }
Ejemplo n.º 18
0
        private void AddBlogCategories(BlogMLDocument blogMlDoc, string tagGroup)
        {
            var categories = _applicationContext.Services.TagService.GetAllContentTags(tagGroup);

            foreach (var category in categories)
            {
                if (category.NodeCount == 0)
                {
                    continue;
                }

                var blogMlCategory = new BlogMLCategory();
                blogMlCategory.Id             = category.Id.ToString();
                blogMlCategory.CreatedOn      = category.CreateDate;
                blogMlCategory.LastModifiedOn = category.UpdateDate;
                blogMlCategory.ApprovalStatus = BlogMLApprovalStatus.Approved;
                blogMlCategory.ParentId       = "0";
                blogMlCategory.Title          = new BlogMLTextConstruct(category.Text);
                blogMlDoc.Categories.Add(blogMlCategory);
            }
        }
Ejemplo n.º 19
0
        /// <summary>
        /// Instantiates a <see cref="ISyndicationResource"/> that conforms to the specified <see cref="SyndicationContentFormat"/> using the supplied <see cref="Stream"/>.
        /// </summary>
        /// <param name="stream">The <see cref="Stream"/> used to load the syndication resource.</param>
        /// <param name="format">A <see cref="SyndicationContentFormat"/> enumeration value that indicates the type syndication resource the <paramref name="stream"/> represents.</param>
        /// <returns>
        ///     An <see cref="ISyndicationResource"/> object that conforms to the specified <paramref name="format"/>, initialized using the supplied <paramref name="stream"/>.
        ///     If the <paramref name="format"/> is not supported by the provider, returns a <b>null</b> reference.
        /// </returns>
        /// <exception cref="ArgumentNullException">The <paramref name="stream"/> is a null reference (Nothing in Visual Basic).</exception>
        private static ISyndicationResource BuildResource(SyndicationContentFormat format, Stream stream)
        {
            Guard.ArgumentNotNull(stream, "stream");

            if (format == SyndicationContentFormat.Apml)
            {
                ApmlDocument document = new ApmlDocument();
                document.Load(stream);
                return(document);
            }
            else if (format == SyndicationContentFormat.Atom)
            {
                XPathDocument  document  = new XPathDocument(stream);
                XPathNavigator navigator = document.CreateNavigator();
                navigator.MoveToRoot();
                navigator.MoveToChild(XPathNodeType.Element);

                if (String.Compare(navigator.LocalName, "entry", StringComparison.OrdinalIgnoreCase) == 0)
                {
                    AtomEntry entry = new AtomEntry();
                    entry.Load(navigator);
                    return(entry);
                }
                else if (String.Compare(navigator.LocalName, "feed", StringComparison.OrdinalIgnoreCase) == 0)
                {
                    AtomFeed feed = new AtomFeed();
                    feed.Load(navigator);
                    return(feed);
                }
                else
                {
                    return(null);
                }
            }
            else if (format == SyndicationContentFormat.BlogML)
            {
                BlogMLDocument document = new BlogMLDocument();
                document.Load(stream);
                return(document);
            }
            else if (format == SyndicationContentFormat.Opml)
            {
                OpmlDocument document = new OpmlDocument();
                document.Load(stream);
                return(document);
            }
            else if (format == SyndicationContentFormat.Rsd)
            {
                RsdDocument document = new RsdDocument();
                document.Load(stream);
                return(document);
            }
            else if (format == SyndicationContentFormat.Rss)
            {
                RssFeed feed = new RssFeed();
                feed.Load(stream);
                return(feed);
            }
            else
            {
                return(null);
            }
        }
Ejemplo n.º 20
0
        public XDocument Export(IEnumerable <IContent> posts, BlogMLDocument document)
        {
            var nsContent = XNamespace.Get("http://purl.org/rss/1.0/modules/content/");
            var nsDsq     = XNamespace.Get("http://www.disqus.com/");
            var nsDc      = XNamespace.Get("http://purl.org/dc/elements/1.1/");
            var nsWp      = XNamespace.Get("http://wordpress.org/export/1.0/");

            var xChannel = new XElement("channel");

            var xDoc = new XDocument(
                new XElement("rss",
                             new XAttribute("version", "2.0"),
                             new XAttribute(XNamespace.Xmlns + "content", nsContent),
                             new XAttribute(XNamespace.Xmlns + "dsq", nsDsq),
                             new XAttribute(XNamespace.Xmlns + "dc", nsDc),
                             new XAttribute(XNamespace.Xmlns + "wp", nsWp),
                             xChannel));

            var umbHelper = new UmbracoHelper(UmbracoContext.Current);
            var markDown  = new MarkdownDeep.Markdown();

            foreach (var post in posts)
            {
                var blogMlPost = document.Posts.FirstOrDefault(x => x.Title.Content == post.Name);

                //TODO: Add logging here if we cant find it
                if (blogMlPost == null)
                {
                    continue;
                }

                //no comments to import
                if (blogMlPost.Comments.Any() == false)
                {
                    continue;
                }

                var body = post.GetValue <string>("richText");
                if (body.IsNullOrWhiteSpace())
                {
                    body = markDown.Transform(post.GetValue <string>("markdown"));
                }

                var xItem = new XElement("item",
                                         new XElement("title", post.Name),
                                         new XElement("link", umbHelper.NiceUrlWithDomain(post.Id)),
                                         new XElement(nsContent + "encoded", new XCData(body)),
                                         new XElement(nsDsq + "thread_identifier", post.Key.ToString()),
                                         new XElement(nsWp + "post_date_gmt", post.GetValue <DateTime>("publishedDate").ToUniversalTime().ToIsoString()),
                                         new XElement(nsWp + "comment_status", "open"));

                foreach (var comment in blogMlPost.Comments)
                {
                    string commentText = comment.Content.Content;

                    if (comment.Content.ContentType == BlogMLContentType.Base64)
                    {
                        commentText = Encoding.UTF8.GetString(Convert.FromBase64String(comment.Content.Content));
                    }


                    var xComment = new XElement(nsWp + "comment",
                                                new XElement(nsWp + "comment_id", comment.Id),
                                                new XElement(nsWp + "comment_author", comment.UserName),
                                                new XElement(nsWp + "comment_author_email", comment.UserEmailAddress),
                                                new XElement(nsWp + "comment_author_url", comment.UserUrl == null ? string.Empty : comment.UserUrl.ToString()),
                                                new XElement(nsWp + "comment_date_gmt", comment.CreatedOn.ToUniversalTime().ToIsoString()),
                                                new XElement(nsWp + "comment_content", commentText),
                                                new XElement(nsWp + "comment_approved", comment.ApprovalStatus == BlogMLApprovalStatus.Approved ? 1 : 0));

                    xItem.Add(xComment);
                }

                xChannel.Add(xItem);
            }

            return(xDoc);
        }
Ejemplo n.º 21
0
        public void Export(
            string fileName,
            int blogRootNode)
        {
            var root = _applicationContext.Services.ContentService.GetById(blogRootNode);

            if (root == null)
            {
                throw new InvalidOperationException("No node found with id " + blogRootNode);
            }
            if (!root.ContentType.Alias.InvariantEquals("Articulate"))
            {
                throw new InvalidOperationException("The node with id " + blogRootNode + " is not an Articulate root node");
            }

            var postType = _applicationContext.Services.ContentTypeService.GetContentType("ArticulateRichText");

            if (postType == null)
            {
                throw new InvalidOperationException("Articulate is not installed properly, the ArticulateRichText doc type could not be found");
            }

            var children = root.Children().ToArray();

            var archiveNodes = children.Where(x => x.ContentType.Alias.InvariantEquals("ArticulateArchive")).ToArray();
            var authorsNodes = children.Where(x => x.ContentType.Alias.InvariantEquals("ArticulateAuthors")).ToArray();

            if (archiveNodes.Length == 0)
            {
                throw new InvalidOperationException("No ArticulateArchive found under the blog root node");
            }
            if (authorsNodes.Length == 0)
            {
                throw new InvalidOperationException("No ArticulateAuthors found under the blog root node");
            }
            var categoryDataType = _applicationContext.Services.DataTypeService.GetDataTypeDefinitionByName("Articulate Categories");

            if (categoryDataType == null)
            {
                throw new InvalidOperationException("No Articulate Categories data type found");
            }
            var categoryDtPreVals = _applicationContext.Services.DataTypeService.GetPreValuesCollectionByDataTypeId(categoryDataType.Id);

            if (categoryDtPreVals == null)
            {
                throw new InvalidOperationException("No pre values for Articulate Categories data type found");
            }
            var tagGroup = categoryDtPreVals.PreValuesAsDictionary["group"];

            //TODO: See: http://argotic.codeplex.com/wikipage?title=Generating%20portable%20web%20log%20content&referringTitle=Home

            var blogMlDoc = new BlogMLDocument
            {
                RootUrl     = new Uri(_umbracoContext.UrlProvider.GetUrl(root.Id), UriKind.RelativeOrAbsolute),
                GeneratedOn = DateTime.Now,
                Title       = new BlogMLTextConstruct(root.GetValue <string>("blogTitle")),
                Subtitle    = new BlogMLTextConstruct(root.GetValue <string>("blogDescription"))
            };

            foreach (var authorsNode in authorsNodes)
            {
                AddBlogAuthors(authorsNode, blogMlDoc);
            }
            AddBlogCategories(blogMlDoc, tagGroup.Value);
            foreach (var archiveNode in archiveNodes)
            {
                AddBlogPosts(archiveNode, blogMlDoc, tagGroup.Value);
            }
            WriteFile(blogMlDoc);
        }
        //============================================================
        //	PRIVATE METHODS
        //============================================================
        #region FillDocumentCollections(BlogMLDocument document, XPathNavigator source, XmlNamespaceManager manager, SyndicationResourceLoadSettings settings)
        /// <summary>
        /// Modifies the <see cref="BlogMLDocument"/> collection entities to match the supplied <see cref="XPathNavigator"/> data source.
        /// </summary>
        /// <param name="document">The <see cref="BlogMLDocument"/> to be filled.</param>
        /// <param name="source">The <see cref="XPathNavigator"/> to extract information from.</param>
        /// <param name="manager">The <see cref="XmlNamespaceManager"/> used to resolve XML namespace prefixes.</param>
        /// <param name="settings">The <see cref="SyndicationResourceLoadSettings"/> used to configure the fill operation.</param>
        /// <remarks>
        ///     This method expects the supplied <paramref name="source"/> to be positioned on the XML element that represents a <see cref="BlogMLDocument"/>.
        /// </remarks>
        /// <exception cref="ArgumentNullException">The <paramref name="document"/> is a null reference (Nothing in Visual Basic).</exception>
        /// <exception cref="ArgumentNullException">The <paramref name="source"/> is a null reference (Nothing in Visual Basic).</exception>
        /// <exception cref="ArgumentNullException">The <paramref name="manager"/> is a null reference (Nothing in Visual Basic).</exception>
        /// <exception cref="ArgumentNullException">The <paramref name="settings"/> is a null reference (Nothing in Visual Basic).</exception>
        private static void FillDocumentCollections(BlogMLDocument document, XPathNavigator source, XmlNamespaceManager manager, SyndicationResourceLoadSettings settings)
        {
            //------------------------------------------------------------
            //	Validate parameter
            //------------------------------------------------------------
            Guard.ArgumentNotNull(document, "document");
            Guard.ArgumentNotNull(source, "source");
            Guard.ArgumentNotNull(manager, "manager");
            Guard.ArgumentNotNull(settings, "settings");

            //------------------------------------------------------------
            //	Attempt to extract syndication information
            //------------------------------------------------------------
            XPathNodeIterator authorsIterator            = source.Select("blog:authors/blog:author", manager);
            XPathNodeIterator extendedPropertiesIterator = source.Select("blog:extended-properties/blog:property", manager);
            XPathNodeIterator categoriesIterator         = source.Select("blog:categories/blog:category", manager);
            XPathNodeIterator postsIterator = source.Select("blog:posts/blog:post", manager);

            if (authorsIterator != null && authorsIterator.Count > 0)
            {
                while (authorsIterator.MoveNext())
                {
                    BlogMLAuthor author = new BlogMLAuthor();
                    if (author.Load(authorsIterator.Current, settings))
                    {
                        document.Authors.Add(author);
                    }
                }
            }

            if (extendedPropertiesIterator != null && extendedPropertiesIterator.Count > 0)
            {
                while (extendedPropertiesIterator.MoveNext())
                {
                    if (extendedPropertiesIterator.Current.HasAttributes)
                    {
                        string propertyName  = extendedPropertiesIterator.Current.GetAttribute("name", String.Empty);
                        string propertyValue = extendedPropertiesIterator.Current.GetAttribute("value", String.Empty);

                        if (!String.IsNullOrEmpty(propertyName) && !document.ExtendedProperties.ContainsKey(propertyName))
                        {
                            document.ExtendedProperties.Add(propertyName, propertyValue);
                        }
                    }
                }
            }

            if (categoriesIterator != null && categoriesIterator.Count > 0)
            {
                while (categoriesIterator.MoveNext())
                {
                    BlogMLCategory category = new BlogMLCategory();
                    if (category.Load(categoriesIterator.Current, settings))
                    {
                        document.Categories.Add(category);
                    }
                }
            }

            if (postsIterator != null && postsIterator.Count > 0)
            {
                int counter = 0;
                while (postsIterator.MoveNext())
                {
                    BlogMLPost post = new BlogMLPost();
                    counter++;

                    if (post.Load(postsIterator.Current, settings))
                    {
                        if (settings.RetrievalLimit != 0 && counter > settings.RetrievalLimit)
                        {
                            break;
                        }

                        ((Collection <BlogMLPost>)document.Posts).Add(post);
                    }
                }
            }
        }
        //============================================================
        //	PUBLIC METHODS
        //============================================================
        #region Fill(BlogMLDocument resource)
        /// <summary>
        /// Modifies the <see cref="BlogMLDocument"/> to match the data source.
        /// </summary>
        /// <param name="resource">The <see cref="BlogMLDocument"/> to be filled.</param>
        /// <exception cref="ArgumentNullException">The <paramref name="resource"/> is a null reference (Nothing in Visual Basic).</exception>
        public void Fill(BlogMLDocument resource)
        {
            //------------------------------------------------------------
            //	Validate parameter
            //------------------------------------------------------------
            Guard.ArgumentNotNull(resource, "resource");

            //------------------------------------------------------------
            //	Create namespace resolver
            //------------------------------------------------------------
            XmlNamespaceManager manager = BlogMLUtility.CreateNamespaceManager(this.Navigator.NameTable);

            //------------------------------------------------------------
            //	Attempt to fill syndication resource
            //------------------------------------------------------------
            XPathNavigator blogNavigator = this.Navigator.SelectSingleNode("blog:blog", manager);

            if (blogNavigator != null)
            {
                if (blogNavigator.HasAttributes)
                {
                    string dateCreatedAttribute = blogNavigator.GetAttribute("date-created", String.Empty);
                    string rootUrlAttribute     = blogNavigator.GetAttribute("root-url", String.Empty);

                    if (!String.IsNullOrEmpty(dateCreatedAttribute))
                    {
                        DateTime createdOn;
                        if (SyndicationDateTimeUtility.TryParseRfc3339DateTime(dateCreatedAttribute, out createdOn))
                        {
                            resource.GeneratedOn = createdOn;
                        }
                    }

                    if (!String.IsNullOrEmpty(rootUrlAttribute))
                    {
                        Uri rootUrl;
                        if (Uri.TryCreate(rootUrlAttribute, UriKind.RelativeOrAbsolute, out rootUrl))
                        {
                            resource.RootUrl = rootUrl;
                        }
                    }
                }

                if (blogNavigator.HasChildren)
                {
                    XPathNavigator titleNavigator    = blogNavigator.SelectSingleNode("blog:title", manager);
                    XPathNavigator subtitleNavigator = blogNavigator.SelectSingleNode("blog:sub-title", manager);

                    if (titleNavigator != null)
                    {
                        BlogMLTextConstruct title = new BlogMLTextConstruct();
                        if (title.Load(titleNavigator))
                        {
                            resource.Title = title;
                        }
                    }

                    if (subtitleNavigator != null)
                    {
                        BlogMLTextConstruct subtitle = new BlogMLTextConstruct();
                        if (subtitle.Load(subtitleNavigator))
                        {
                            resource.Subtitle = subtitle;
                        }
                    }

                    BlogML20SyndicationResourceAdapter.FillDocumentCollections(resource, blogNavigator, manager, this.Settings);
                }

                SyndicationExtensionAdapter adapter = new SyndicationExtensionAdapter(blogNavigator, this.Settings);
                adapter.Fill(resource, manager);
            }
        }
        /// <summary>
        /// Returns a date-time indicating the most recent instant in time when the resource was last modified.
        /// </summary>
        /// <param name="resource">The <see cref="ISyndicationResource"/> to determine the modification date for.</param>
        /// <returns>
        ///     A <see cref="DateTime"/> indicating the most recent instant in time when the <see cref="ISyndicationResource"/> was last modified.
        ///     If the <see cref="ISyndicationResource"/> conforms to a format that does not support modification tracking, or if otherwise unable
        ///     to determine the modification date, returns <see cref="DateTime.MinValue"/>.
        /// </returns>
        /// <exception cref="ArgumentNullException">The <paramref name="resource"/> is a null reference (Nothing in Visual Basic).</exception>
        private static DateTime GetLastModificationDate(ISyndicationResource resource)
        {
            //------------------------------------------------------------
            //	Local members
            //------------------------------------------------------------
            DateTime lastModifiedOn = DateTime.MinValue;

            //------------------------------------------------------------
            //	Validate parameter
            //------------------------------------------------------------
            Guard.ArgumentNotNull(resource, "resource");

            //------------------------------------------------------------
            //	Determine modification date based on format specific details
            //------------------------------------------------------------
            switch (resource.Format)
            {
            case SyndicationContentFormat.Apml:
                ApmlDocument apmlDocument = resource as ApmlDocument;
                if (apmlDocument != null && apmlDocument.Head != null)
                {
                    lastModifiedOn = apmlDocument.Head.CreatedOn;
                }
                break;

            case SyndicationContentFormat.Atom:
                XPathNavigator rootNavigator = resource.CreateNavigator();
                if (String.Compare(rootNavigator.LocalName, "entry", StringComparison.OrdinalIgnoreCase) == 0)
                {
                    AtomEntry entry = resource as AtomEntry;
                    if (entry != null)
                    {
                        lastModifiedOn = entry.UpdatedOn;
                    }
                }
                else if (String.Compare(rootNavigator.LocalName, "feed", StringComparison.OrdinalIgnoreCase) == 0)
                {
                    AtomFeed atomFeed = resource as AtomFeed;
                    if (atomFeed != null)
                    {
                        lastModifiedOn = atomFeed.UpdatedOn;
                    }
                }
                break;

            case SyndicationContentFormat.BlogML:
                BlogMLDocument blogDocument = resource as BlogMLDocument;
                if (blogDocument != null)
                {
                    lastModifiedOn = blogDocument.GeneratedOn;
                }
                break;

            case SyndicationContentFormat.Opml:
                OpmlDocument opmlDocument = resource as OpmlDocument;
                if (opmlDocument != null && opmlDocument.Head != null)
                {
                    lastModifiedOn = opmlDocument.Head.ModifiedOn;
                }
                break;

            case SyndicationContentFormat.Rsd:
                //  RSD 1.0 does not provide last modified information
                lastModifiedOn = DateTime.MinValue;
                break;

            case SyndicationContentFormat.Rss:
                RssFeed rssFeed = resource as RssFeed;
                if (rssFeed != null && rssFeed.Channel != null)
                {
                    lastModifiedOn = rssFeed.Channel.LastBuildDate;
                }
                break;

            default:
                lastModifiedOn = DateTime.MinValue;
                break;
            }

            return(lastModifiedOn);
        }
Ejemplo n.º 25
0
        private void AddBlogPosts(IContent archiveNode, BlogMLDocument blogMlDoc, string categoryGroup, string tagGroup)
        {
            const int pageSize  = 1000;
            var       pageIndex = 0;

            IContent[] posts;
            do
            {
                posts = _contentService.GetPagedChildren(archiveNode.Id, pageIndex, pageSize, out long _, ordering: Ordering.By("createDate")).ToArray();

                foreach (var child in posts)
                {
                    string content = "";
                    if (child.ContentType.Alias.InvariantEquals("ArticulateRichText"))
                    {
                        //TODO: this would also need to export all macros
                        content = child.GetValue <string>("richText");
                    }
                    else if (child.ContentType.Alias.InvariantEquals("ArticulateMarkdown"))
                    {
                        var md = new Markdown();
                        content = md.Transform(child.GetValue <string>("markdown"));
                    }

                    var postUrl         = new Uri(_umbracoContextAccessor.UmbracoContext.UrlProvider.GetUrl(child.Id), UriKind.RelativeOrAbsolute);
                    var postAbsoluteUrl = new Uri(_umbracoContextAccessor.UmbracoContext.UrlProvider.GetUrl(child.Id, UrlProviderMode.Absolute), UriKind.Absolute);
                    var blogMlPost      = new BlogMLPost()
                    {
                        Id             = child.Key.ToString(),
                        Name           = new BlogMLTextConstruct(child.Name),
                        Title          = new BlogMLTextConstruct(child.Name),
                        ApprovalStatus = BlogMLApprovalStatus.Approved,
                        PostType       = BlogMLPostType.Normal,
                        CreatedOn      = child.CreateDate,
                        LastModifiedOn = child.UpdateDate,
                        Content        = new BlogMLTextConstruct(content, BlogMLContentType.Html),
                        Excerpt        = new BlogMLTextConstruct(child.GetValue <string>("excerpt")),
                        Url            = postUrl
                    };

                    var author = blogMlDoc.Authors.FirstOrDefault(x => x.Title != null && x.Title.Content.InvariantEquals(child.GetValue <string>("author")));
                    if (author != null)
                    {
                        blogMlPost.Authors.Add(author.Id);
                    }

                    var categories = _tagService.GetTagsForEntity(child.Id, categoryGroup);

                    foreach (var category in categories)
                    {
                        blogMlPost.Categories.Add(category.Id.ToString());
                    }

                    var tags = _tagService.GetTagsForEntity(child.Id, tagGroup).Select(t => t.Text).ToList();
                    if (tags?.Any() == true)
                    {
                        blogMlPost.AddExtension(
                            new Syndication.BlogML.TagsSyndicationExtension()
                        {
                            Context = { Tags = new Collection <string>(tags) }
                        });
                    }

                    //add the image attached if there is one
                    if (child.HasProperty("postImage"))
                    {
                        try
                        {
                            var val  = child.GetValue <string>("postImage");
                            var json = JsonConvert.DeserializeObject <JObject>(val);
                            var src  = json.Value <string>("src");

                            var mime = ImageMimeType(src);

                            if (!mime.IsNullOrWhiteSpace())
                            {
                                var imageUrl = new Uri(postAbsoluteUrl.GetLeftPart(UriPartial.Authority) + src.EnsureStartsWith('/'), UriKind.Absolute);
                                blogMlPost.Attachments.Add(new BlogMLAttachment
                                {
                                    Content     = string.Empty, //this is used for embedded resources
                                    Url         = imageUrl,
                                    ExternalUri = imageUrl,
                                    IsEmbedded  = false,
                                    MimeType    = mime
                                });
                            }
                        }
                        catch (Exception ex)
                        {
                            _logger.Error <BlogMlExporter>(ex, "Could not add the file to the blogML post attachments");
                        }
                    }



                    blogMlDoc.AddPost(blogMlPost);
                }

                pageIndex++;
            } while (posts.Length == pageSize);
        }
Ejemplo n.º 26
0
        //============================================================
        //	CLASS SUMMARY
        //============================================================
        /// <summary>
        /// Provides example code for the BlogMLPost class.
        /// </summary>
        public static void ClassExample()
        {
            #region BlogMLPost
            BlogMLDocument document = new BlogMLDocument();

            document.RootUrl     = new Uri("/blogs/default.aspx");
            document.GeneratedOn = new DateTime(2006, 9, 5, 18, 22, 10);
            document.Title       = new BlogMLTextConstruct("BlogML 2.0 Example");
            document.Subtitle    = new BlogMLTextConstruct("This is some sample blog content for BlogML 2.0");

            BlogMLAuthor administrator = new BlogMLAuthor();
            administrator.Id             = "2100";
            administrator.CreatedOn      = new DateTime(2006, 8, 10, 8, 44, 35);
            administrator.LastModifiedOn = new DateTime(2006, 9, 4, 13, 46, 38);
            administrator.ApprovalStatus = BlogMLApprovalStatus.Approved;
            administrator.EmailAddress   = "*****@*****.**";
            administrator.Title          = new BlogMLTextConstruct("admin");
            document.Authors.Add(administrator);

            document.ExtendedProperties.Add("CommentModeration", "Anonymous");
            document.ExtendedProperties.Add("SendTrackback", "yes");

            BlogMLCategory category1 = new BlogMLCategory();
            category1.Id             = "1018";
            category1.CreatedOn      = new DateTime(2006, 9, 5, 17, 54, 58);
            category1.LastModifiedOn = new DateTime(2006, 9, 5, 17, 54, 58);
            category1.ApprovalStatus = BlogMLApprovalStatus.Approved;
            category1.Description    = "Sample Category 1";
            category1.ParentId       = "0";
            category1.Title          = new BlogMLTextConstruct("Category 1");
            document.Categories.Add(category1);

            BlogMLCategory category2 = new BlogMLCategory();
            category2.Id             = "1019";
            category2.CreatedOn      = new DateTime(2006, 9, 5, 17, 54, 59);
            category2.LastModifiedOn = new DateTime(2006, 9, 5, 17, 54, 59);
            category2.ApprovalStatus = BlogMLApprovalStatus.Approved;
            category2.Description    = "Sample Category 2";
            category2.ParentId       = "0";
            category2.Title          = new BlogMLTextConstruct("Category 2");
            document.Categories.Add(category2);

            BlogMLCategory category3 = new BlogMLCategory();
            category3.Id             = "1020";
            category3.CreatedOn      = new DateTime(2006, 9, 5, 17, 55, 0);
            category3.LastModifiedOn = new DateTime(2006, 9, 5, 17, 55, 0);
            category3.ApprovalStatus = BlogMLApprovalStatus.NotApproved;
            category3.Description    = "Sample Category 3";
            category3.ParentId       = "0";
            category3.Title          = new BlogMLTextConstruct("Category 3");
            document.Categories.Add(category3);

            //  Create a blog entry
            BlogMLPost post = new BlogMLPost();
            post.Id             = "34";
            post.CreatedOn      = new DateTime(2006, 9, 5, 3, 19, 0);
            post.LastModifiedOn = new DateTime(2006, 9, 5, 3, 19, 0);
            post.ApprovalStatus = BlogMLApprovalStatus.Approved;
            post.Url            = new Uri("/blogs/archive/2006/09/05/Sample-Blog-Post.aspx");
            post.PostType       = BlogMLPostType.Normal;
            post.Views          = "0";
            post.Title          = new BlogMLTextConstruct("Sample Blog Post");
            post.Content        = new BlogMLTextConstruct("<p>This is <b>HTML encoded</b> content.&nbsp;</p>", BlogMLContentType.Html);
            post.Name           = new BlogMLTextConstruct("Sample Blog Post");

            post.Categories.Add("1018");
            post.Categories.Add("1020");

            post.Authors.Add("2100");

            BlogMLComment comment = new BlogMLComment();
            comment.Id             = "35";
            comment.CreatedOn      = new DateTime(2006, 9, 5, 11, 36, 50);
            comment.LastModifiedOn = new DateTime(2006, 9, 5, 11, 36, 50);
            comment.Title          = new BlogMLTextConstruct("re: Sample Blog Post");
            comment.Content        = new BlogMLTextConstruct("This is a test comment.");
            post.Comments.Add(comment);
            #endregion
        }
Ejemplo n.º 27
0
        public void Export(
            string fileName,
            int blogRootNode)
        {
            var root = _contentService.GetById(blogRootNode);

            if (root == null)
            {
                throw new InvalidOperationException("No node found with id " + blogRootNode);
            }
            if (!root.ContentType.Alias.InvariantEquals("Articulate"))
            {
                throw new InvalidOperationException("The node with id " + blogRootNode + " is not an Articulate root node");
            }

            var postType = _contentTypeService.Get("ArticulateRichText");

            if (postType == null)
            {
                throw new InvalidOperationException("Articulate is not installed properly, the ArticulateRichText doc type could not be found");
            }

            var archiveContentType = _contentTypeService.Get("ArticulateArchive");
            var archiveNodes       = _contentService.GetPagedOfType(archiveContentType.Id, 0, int.MaxValue, out long totalArchive, null);

            var authorsContentType = _contentTypeService.Get("ArticulateAuthors");
            var authorsNodes       = _contentService.GetPagedOfType(authorsContentType.Id, 0, int.MaxValue, out long totalAuthors, null);

            if (totalArchive == 0)
            {
                throw new InvalidOperationException("No ArticulateArchive found under the blog root node");
            }

            if (totalAuthors == 0)
            {
                throw new InvalidOperationException("No ArticulateAuthors found under the blog root node");
            }

            var categoryDataType = _dataTypeService.GetDataType("Articulate Categories");

            if (categoryDataType == null)
            {
                throw new InvalidOperationException("No Articulate Categories data type found");
            }
            var categoryConfiguration = categoryDataType.ConfigurationAs <TagConfiguration>();
            var categoryGroup         = categoryConfiguration.Group;

            var tagDataType = _dataTypeService.GetDataType("Articulate Tags");

            if (tagDataType == null)
            {
                throw new InvalidOperationException("No Articulate Tags data type found");
            }
            var tagConfiguration = tagDataType.ConfigurationAs <TagConfiguration>();
            var tagGroup         = tagConfiguration.Group;

            //TODO: See: http://argotic.codeplex.com/wikipage?title=Generating%20portable%20web%20log%20content&referringTitle=Home

            var blogMlDoc = new BlogMLDocument
            {
                RootUrl     = new Uri(_umbracoContextAccessor.UmbracoContext.UrlProvider.GetUrl(root.Id), UriKind.RelativeOrAbsolute),
                GeneratedOn = DateTime.Now,
                Title       = new BlogMLTextConstruct(root.GetValue <string>("blogTitle")),
                Subtitle    = new BlogMLTextConstruct(root.GetValue <string>("blogDescription"))
            };

            foreach (var authorsNode in authorsNodes)
            {
                AddBlogAuthors(authorsNode, blogMlDoc);
            }
            AddBlogCategories(blogMlDoc, categoryGroup);
            foreach (var archiveNode in archiveNodes)
            {
                AddBlogPosts(archiveNode, blogMlDoc, categoryGroup, tagGroup);
            }
            WriteFile(blogMlDoc);
        }
Ejemplo n.º 28
0
        public async Task Import(
            int userId,
            string fileName,
            int blogRootNode,
            bool overwrite,
            string regexMatch,
            string regexReplace,
            bool publishAll,
            bool exportDisqusXml = false)
        {
            try
            {
                if (!File.Exists(fileName))
                {
                    throw new FileNotFoundException("File not found: " + fileName);
                }

                var root = _applicationContext.Services.ContentService.GetById(blogRootNode);
                if (root == null)
                {
                    throw new InvalidOperationException("No node found with id " + blogRootNode);
                }
                if (!root.ContentType.Alias.InvariantEquals("Articulate"))
                {
                    throw new InvalidOperationException("The node with id " + blogRootNode + " is not an Articulate root node");
                }

                using (var stream = File.OpenRead(fileName))
                {
                    var document = new BlogMLDocument();
                    document.Load(stream);

                    stream.Position = 0;
                    var xdoc = XDocument.Load(stream);

                    var authorIdsToName = ImportAuthors(userId, root, document.Authors);

                    var imported = await ImportPosts(userId, xdoc, root, document.Posts, document.Authors.ToArray(), document.Categories.ToArray(), authorIdsToName, overwrite, regexMatch, regexReplace, publishAll);

                    if (exportDisqusXml)
                    {
                        var exporter = new DisqusXmlExporter();
                        var xDoc     = exporter.Export(imported, document);

                        using (var memStream = new MemoryStream())
                        {
                            xDoc.Save(memStream);

                            var mediaFs = FileSystemProviderManager.Current.GetFileSystemProvider <MediaFileSystem>();

                            mediaFs.AddFile("Articulate/DisqusXmlExport.xml", memStream, true);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                HasErrors = true;
                LogHelper.Error <BlogMlImporter>("Importing failed with errors", ex);
            }
        }