Esempio n. 1
0
        /**
         * Import Post
         *
         * Imports a given post into the database, creating the necessary dependencies,
         * including BlogMonth (if it doesn't already exist), TreeNode, and Document.
         * Returns the newly-created document that represents the post.
         *
         * @param entities context
         * @param CMS_Tree blog
         * @param CONTENT_BlogPost post
         * @return CMS_Document
         */
        protected static CMS_Document ImportPost(kenticofreeEntities context, CMS_Tree blog, CONTENT_BlogPost post)
        {
            Regex forbidden         = new Regex(forbiddenChars);
            Regex consolidateDashes = new Regex("[-]{2}");

            /* We want to preserve the IDs of the Posts for linking, but EF won't let us turn on IDENTITY_INSERT
             * with its available methods (ExecuteStoreCommand and SaveChanges are different connections, it seems).
             * So we have to do it the old fashioned way.
             */
            object[] values = new object[] {
                post.BlogPostID.ToString(),
                     post.BlogPostTitle,
                     post.BlogPostDate.Date.ToString("yyyy-MM-dd HH:mm:ss"),
                post.BlogPostSummary,
                post.BlogPostAllowComments,
                post.BlogPostBody,
                post.BlogLogActivity
            };

            /* We'll use MERGE here, so that we can handle existing entries.
             * The "xmlTrumpsDb" config switch will allow a choice between nuking what's in the DB
             * or preserving it.
             */
            string cmd = "SET IDENTITY_INSERT CONTENT_BlogPost ON; ";

            cmd += "MERGE CONTENT_BlogPost ";
            cmd += "USING (VALUES ({0},{1},{2},{3},{4},{5},{6})) as temp(BlogPostID, BlogPostTitle, BlogPostDate, BlogPostSummary, BlogPostAllowComments, BlogPostBody, BlogLogActivity) ";
            cmd += "ON CONTENT_BlogPost.BlogPostID = temp.BlogPostID ";
            // To nuke or not to nuke, that is the question...
            if (config.Get("xmlTrumpsDb") == "true")
            {
                cmd += "WHEN MATCHED THEN ";
                cmd += "UPDATE SET CONTENT_BlogPost.BlogPostTitle = temp.BlogPostTitle, CONTENT_BlogPost.BlogPostDate = temp.BlogPostDate, CONTENT_BlogPost.BlogPostSummary = temp.BlogPostSummary, CONTENT_BlogPost.BlogPostAllowComments = temp.BlogPostAllowComments, CONTENT_BlogPost.BlogPostBody = temp.BlogPostBody, CONTENT_BlogPost.BlogLogActivity = temp.BlogLogActivity ";
            }
            cmd += "WHEN NOT MATCHED THEN ";
            cmd += "INSERT (BlogPostId, BlogPostTitle, BlogPostDate, BlogPostSummary, BlogPostAllowComments, BlogPostBody, BlogLogActivity) VALUES ({0},{1},{2},{3},{4},{5},{6}); ";
            cmd += "SET IDENTITY_INSERT CONTENT_BlogPost OFF;";
            context.ExecuteStoreCommand(cmd, values);

            // See if there's a BlogMonth entry for the month this post is for
            CMS_Tree month = GetBlogMonth(context, post, blog);

            CMS_Class blogClass = context.CMS_Class.Where(x => x.ClassName == "CMS.BlogPost").First();

            CMS_Tree treeNode = (from t in context.CMS_Tree
                                 join d in context.CMS_Document on t.NodeID equals d.DocumentNodeID
                                 where d.DocumentForeignKeyValue == post.BlogPostID && t.NodeClassID == 3423
                                 select t).FirstOrDefault();

            // Add a new node only if one doesn't already exist
            if (treeNode == null)
            {
                string nodeAlias = consolidateDashes.Replace(forbidden.Replace(post.BlogPostTitle, "-"), "-");
                nodeAlias = (nodeAlias.Length > 50 ? nodeAlias.Substring(0, 50) : nodeAlias);                 // Truncate the alias to avoid SQL Server errors

                // Create the Tree Node for the post and add it in
                treeNode = new CMS_Tree()
                {
                    NodeAliasPath              = string.Format("{0}/{1}", month.NodeAliasPath, forbidden.Replace(post.BlogPostTitle, "-")),
                    NodeName                   = post.BlogPostTitle,
                    NodeAlias                  = nodeAlias,
                    NodeClassID                = blogClass.ClassID,
                    NodeParentID               = month.NodeID,
                    NodeLevel                  = Int32.Parse(config.Get("kenticoBlogLevel")) + 2,
                    NodeACLID                  = 1,    // Default ACL ID
                    NodeSiteID                 = siteId,
                    NodeGUID                   = Guid.NewGuid(),
                    NodeInheritPageLevels      = "",
                    NodeTemplateForAllCultures = true,
                    NodeChildNodesCount        = 0
                };

                CMS_User author = GetAuthor(context, post);
                treeNode.NodeOwner = author.UserID;

                context.CMS_Tree.AddObject(treeNode);
                treeNode.NodeOrder = GetNodeOrder(context, treeNode);
                month.NodeChildNodesCount++;                // Increment the child nodes count, so the new post will display in the CMS
                blog.NodeChildNodesCount++;                 // Increment the blog's child nodes count, too.
                context.SaveChanges();
            }

            CMS_TagGroup tagGroup = GetTagGroup(context);

            // Create the document and add it into the database
            CMS_Document postDoc = AddDocument(context, treeNode, post.BlogPostID, tagGroup.TagGroupID);

            return(postDoc);
        }
Esempio n. 2
0
        /**
         * Find or create the "BlogMonth" entry for the month a given post is in.
         *
         * Returns a TreeNode of the requested month, either by finding an existing one or creating a new one.
         *
         * @param entity context
         * @param CONTNET_BlogPost post
         * @param CMS_Tree blog
         * @return CMS_Tree
         */
        protected static CMS_Tree GetBlogMonth(kenticofreeEntities context, CONTENT_BlogPost post, CMS_Tree blog)
        {
            Regex forbidden = new Regex(forbiddenChars);

            // Does one exist?
            var monthQuery = (from m in context.CONTENT_BlogMonth
                              where m.BlogMonthStartingDate.Year == post.BlogPostDate.Year && m.BlogMonthStartingDate.Month == post.BlogPostDate.Month
                              select m);

            CMS_Tree treeNode = null;
            // Find out the classID of the CMS.BlogMonth document type
            CMS_Class monthClass = context.CMS_Class.Where(x => x.ClassName == "CMS.BlogMonth").First();

            // If not, make a new one
            if (monthQuery.Any() == false)
            {
                CONTENT_BlogMonth month = new CONTENT_BlogMonth()
                {
                    BlogMonthName         = post.BlogPostDate.ToString("MMMM yyyy"),
                    BlogMonthStartingDate = new DateTime(post.BlogPostDate.Year, post.BlogPostDate.Month, 01)
                };

                context.CONTENT_BlogMonth.AddObject(month);

                // Add the corresponding tree node
                treeNode = new CMS_Tree()
                {
                    NodeAliasPath = string.Format("{0}/{1}/{2}", config.Get("kenticoBlogPath"), forbidden.Replace(blog.NodeName, "-"), forbidden.Replace(month.BlogMonthName, "-")),
                    NodeTemplateForAllCultures = true,
                    NodeName              = month.BlogMonthName,
                    NodeAlias             = forbidden.Replace(month.BlogMonthName, "-"),
                    NodeClassID           = monthClass.ClassID,
                    NodeParentID          = blog.NodeID,
                    NodeLevel             = Int32.Parse(config.Get("kenticoBlogLevel")) + 1,
                    NodeACLID             = 1,         // Default ACL ID
                    NodeSiteID            = Int32.Parse(config.Get("siteId")),
                    NodeGUID              = Guid.NewGuid(),
                    NodeOwner             = Int32.Parse(config.Get("nodeOwnerId")),
                    NodeInheritPageLevels = "",
                    NodeChildNodesCount   = 0                   // Start out with a number, instead of NULL, so we can easily increment it
                };

                treeNode.NodeOrder = GetNodeOrder(context, treeNode);

                context.CMS_Tree.AddObject(treeNode);
                context.SaveChanges();

                AddDocument(context, treeNode, month.BlogMonthID);
            }
            // If so, use it
            else
            {
                CONTENT_BlogMonth month = monthQuery.First();
                treeNode = (from t in context.CMS_Tree
                            join d in context.CMS_Document on t.NodeID equals d.DocumentNodeID
                            join b in context.CONTENT_BlogMonth on d.DocumentForeignKeyValue equals b.BlogMonthID
                            where b.BlogMonthID == month.BlogMonthID && t.NodeClassID == monthClass.ClassID
                            select t).Single();
            }

            return(treeNode);
        }
Esempio n. 3
0
        /**
         * The main function for importing the posts.
         *
         * @param entity context
         */
        protected static void ProcessPosts(kenticofreeEntities context)
        {
            Console.WriteLine("Adding posts to database.");
            // Gather list of Posts and make them each Entities
            var posts = (from i in wpxml.Descendants("item")
                         where i.Element(wpns + "post_type").Value == "post" && i.Element(wpns + "status").Value == "publish"
                         select new CONTENT_BlogPost {
                BlogPostID = Int32.Parse(i.Element(wpns + "post_id").Value),
                BlogPostTitle = i.Element("title").Value,
                BlogPostDate = DateTime.Parse(i.Element(wpns + "post_date").Value),
                BlogPostSummary = (String.IsNullOrEmpty(i.Element("description").Value) ? i.Element("title").Value : i.Element("description").Value),
                BlogPostAllowComments = true,
                BlogPostBody = i.Element(encoded + "encoded").Value.Replace("\n", "<br/>"),                                                 // CDATA element
                BlogLogActivity = true
            });

            // Get the blog by the ID set in the settings
            CMS_Tree blog = (from b in context.CONTENT_Blog
                             join d in context.CMS_Document on b.BlogID equals d.DocumentForeignKeyValue
                             join t in context.CMS_Tree on d.DocumentNodeID equals t.NodeID
                             where b.BlogID == blogId && t.NodeClassID == 3423                                    //Blog class ID, otherwise we end up with a bunch of stuff that aren't blogs
                             select t).FirstOrDefault();

            if (blog == null)
            {
                Console.WriteLine("No blog with the ID of " + blogId + " detected. Adding new blog.");
                CONTENT_Blog b = new CONTENT_Blog()
                {
                    BlogName             = "BlogImport",
                    BlogDescription      = "The Imported Blog",
                    BlogOpenCommentsFor  = "-1",
                    BlogEnableTrackbacks = true
                };

                CMS_Class blogClass = context.CMS_Class.Where(x => x.ClassName == "CMS.BlogPost").First();
                CMS_Tree  root      = context.CMS_Tree.Where(x => x.NodeClassID == 1095).Where(x => x.NodeSiteID == siteId).First();

                blog = new CMS_Tree()
                {
                    NodeAliasPath              = "/" + b.BlogName,
                    NodeName                   = b.BlogName,
                    NodeAlias                  = b.BlogName,
                    NodeClassID                = blogClass.ClassID,
                    NodeLevel                  = Int32.Parse(config.Get("kenticoBlogLevel")),
                    NodeACLID                  = 1,    // Default ACL ID
                    NodeSiteID                 = siteId,
                    NodeOwner                  = nodeOwnerId,
                    NodeOrder                  = 100,
                    NodeGUID                   = Guid.NewGuid(),
                    NodeInheritPageLevels      = "",
                    NodeTemplateForAllCultures = true,
                    NodeChildNodesCount        = 0,
                    NodeParentID               = (root != null ? root.NodeID : 0)
                };

                context.CONTENT_Blog.AddObject(b);
                context.CMS_Tree.AddObject(blog);
                context.SaveChanges();

                AddDocument(context, blog, b.BlogID);
            }

            Console.WriteLine("Connecting categories, tags, and comments to posts. This may take a while.");
            foreach (CONTENT_BlogPost post in posts)
            {
                CMS_Document postDoc = ImportPost(context, blog, post);
                LinkCategoriesAndTags(context, postDoc, post);
                ImportComments(context, postDoc);
            }

            OrderPosts(context, blog);
        }