Example #1
0
        private void ImportBlog(WordPressImportData data)
        {
            var    db            = Factory.GetDatabase(data.DatabaseName);
            var    options       = BuildWpImportOptions(data);
            var    postProvider  = new MediaItemBasedProvider(data.DataSourceId, db);
            var    importManager = new WpImportManager(db, postProvider, options);
            string jobHandle     = Context.Job.Handle.ToString();

            LogMessage("Reading import item", jobHandle);

            var templateMappingItem = db.GetItem(data.TemplateMappingItemId);
            var templatesMapping    = new TemplatesMapping(templateMappingItem);

            var blogParent = db.GetItem(data.ParentId);

            if (blogParent != null)
            {
                LogMessage("Creating blog", jobHandle);
                var blogRoot = importManager.CreateBlogRoot(blogParent, data.BlogName, data.BlogEmail, templatesMapping.BlogRootTemplate);

                LogTotal(importManager.Posts.Count, jobHandle);
                LogMessage("Importing posts", jobHandle);
                importManager.ImportPosts(blogRoot, templatesMapping, (itemName, count) =>
                {
                    LogMessage("Importing entry " + itemName, jobHandle);
                    LogProgress(count, jobHandle);
                });
            }
            else
            {
                LogMessage(String.Format("Parent item for blog root could not be found ({0})", data.ParentId), jobHandle);
            }
        }
Example #2
0
        private ImportSummary ImportBlog()
        {
            var options = new WpImportOptions
            {
                IncludeComments   = ImportComments.Checked,
                IncludeCategories = ImportCategories.Checked,
                IncludeTags       = ImportTags.Checked
            };

            string fileLocation = String.Format("{0}\\{1}", ApplicationContext.PackagePath, WordpressXmlFile.Value);

            LogMessage("Reading import file");
            var importManager = new WpImportManager(db, new FileBasedProvider(fileLocation), options);

            LogMessage("Creating blog");
            Item root = db.GetItem(litSummaryPath.Text);

            if (root != null)
            {
                var templateMappingItem = root.Database.GetItem(new ID(TemplatesMapping.Value));
                var templatesMapping    = new TemplatesMapping(templateMappingItem);
                var blogItem            = importManager.CreateBlogRoot(root, BlogName.Value, BlogEmail.Value, templatesMapping.BlogRootTemplate);

                LogMessage("Importing posts");
                LogTotal(importManager.Posts.Count);

                return(importManager.ImportPosts(blogItem, templatesMapping, (itemName, count) =>
                {
                    LogMessage("Importing entry " + itemName);
                    LogProgress(count);
                }));
            }
            else
            {
                LogMessage(String.Format("Parent item for blog root could not be found ({0})", litSummaryPath.Text));
            }

            return(null);
        }
Example #3
0
        internal ImportSummary ImportPosts(Item blogItem, TemplatesMapping mapping, Action <string, int> logger = null)
        {
            var summary = new ImportSummary();

            var entryTemplate = TemplateManager.GetTemplate(mapping.BlogEntryTemplate, _db);

            foreach (WpPost post in Posts)
            {
                summary.PostCount++;

                if (!string.IsNullOrEmpty(post.Content))
                {
                    var title = post.Title;
                    title = String.IsNullOrEmpty(title) ? $"Post {Posts.IndexOf(post)}" : title;
                    var name = ItemUtil.ProposeValidItemName(title);

                    if (logger != null)
                    {
                        logger(name, summary.PostCount);
                    }

                    EntryItem entry = ItemManager.AddFromTemplate(name, entryTemplate.ID, blogItem);

                    entry.BeginEdit();
                    entry.Title.Field.Value        = post.Title;
                    entry.Introduction.Field.Value = string.Empty;
                    entry.Content.Field.Value      = post.Content;
                    entry.Tags.Field.Value         = string.Join(", ", post.Tags.ToArray());

                    var categorieItems = new List <string>();

                    foreach (string categoryName in post.Categories)
                    {
                        var categoryItem = ManagerFactory.CategoryManagerInstance.Add(categoryName, blogItem);
                        categoryItem.InnerItem.ChangeTemplate(categoryItem.Database.GetItem(mapping.BlogCategoryTemplate));
                        categorieItems.Add(categoryItem.ID.ToString());
                        summary.CategoryCount++;
                    }

                    if (categorieItems.Count > 0)
                    {
                        entry.Category.Field.Value = string.Join("|", categorieItems.ToArray());
                    }

                    foreach (WpComment wpComment in post.Comments)
                    {
                        var commentId = ManagerFactory.CommentManagerInstance.AddCommentToEntry(entry.ID, wpComment, blogItem.Language);
                        var comment   = entry.Database.GetItem(commentId, blogItem.Language);
                        comment?.ChangeTemplate(comment.Database.GetItem(mapping.BlogCommentTemplate));

                        summary.CommentCount++;
                    }

                    var publicationDate = DateUtil.ToIsoDate(post.PublicationDate);
                    entry.InnerItem.Fields[FieldIDs.Created].Value = publicationDate;
                    entry.InnerItem.Fields["Entry Date"].Value     = publicationDate;
                    entry.EndEdit();
                }
            }

            return(summary);
        }