Skip to content

Commit 5e11bbc

Browse files
authored
Merge pull request #228 from /issues/227
#227 | Wordpress import. Error while importing comments
2 parents edfc378 + 4620e65 commit 5e11bbc

File tree

3 files changed

+112
-88
lines changed

3 files changed

+112
-88
lines changed

src/Sitecore.Modules.WeBlog/Pipelines/CreateComment/CreateCommentItem.cs

Lines changed: 95 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010

1111
namespace Sitecore.Modules.WeBlog.Pipelines.CreateComment
1212
{
13-
public class CreateCommentItem : ICreateCommentProcessor
14-
{
13+
public class CreateCommentItem : ICreateCommentProcessor
14+
{
1515
/// <summary>
1616
/// Gets or sets the <see cref="IBlogManager"/> used to access the structure of the blog and other settings.
1717
/// </summary>
@@ -21,9 +21,9 @@ public class CreateCommentItem : ICreateCommentProcessor
2121
/// Create a new instance.
2222
/// </summary>
2323
public CreateCommentItem()
24-
: this(null)
25-
{
26-
}
24+
: this(null)
25+
{
26+
}
2727

2828
/// <summary>
2929
/// Create a new instance.
@@ -35,92 +35,99 @@ public CreateCommentItem(IBlogManager blogManager)
3535
}
3636

3737
public void Process(CreateCommentArgs args)
38-
{
38+
{
3939
Assert.ArgumentNotNull(args, "args cannot be null");
40-
Assert.IsNotNull(args.Database, "Database cannot be null");
41-
Assert.IsNotNull(args.Comment, "Comment cannot be null");
42-
Assert.IsNotNull(args.EntryID, "Entry ID cannot be null");
40+
Assert.IsNotNull(args.Database, "Database cannot be null");
41+
Assert.IsNotNull(args.Comment, "Comment cannot be null");
42+
Assert.IsNotNull(args.EntryID, "Entry ID cannot be null");
4343
Assert.IsNotNull(args.Language, "Language cannot be null");
4444

45-
var entryItem = args.Database.GetItem(args.EntryID, args.Language);
46-
if (entryItem != null)
47-
{
48-
var blogItem = BlogManager.GetCurrentBlog(entryItem);
49-
if (blogItem != null)
50-
{
51-
var template = args.Database.GetTemplate(blogItem.BlogSettings.CommentTemplateID);
52-
var itemName =
53-
ItemUtil.ProposeValidItemName(string.Format("Comment at {0} by {1}",
54-
GetDateTime().ToString("yyyyMMdd HHmmss"), args.Comment.AuthorName));
55-
if (itemName.Length > 100)
56-
{
57-
itemName = itemName.Substring(0, 100);
58-
}
59-
60-
// verify the comment item name is unique for this entry
61-
var query = "fast:{0}//{1}".FormatWith(ContentHelper.EscapePath(entryItem.Paths.FullPath), itemName);
62-
63-
var num = 1;
64-
var nondupItemName = itemName;
65-
while (entryItem.Database.SelectSingleItem(query) != null)
66-
{
67-
nondupItemName = itemName + " " + num;
68-
num++;
69-
query = "fast:{0}//{1}".FormatWith(entryItem.Paths.FullPath, nondupItemName);
70-
}
71-
72-
// need to create the comment within the shell site to ensure workflow is applied to comment
73-
var shellSite = SiteContextFactory.GetSiteContext(Sitecore.Constants.ShellSiteName);
74-
SiteContextSwitcher siteSwitcher = null;
75-
76-
try
77-
{
78-
if (shellSite != null)
79-
{
80-
siteSwitcher = new SiteContextSwitcher(shellSite);
81-
}
82-
83-
using (new SecurityDisabler())
84-
{
85-
var newItem = entryItem.Add(nondupItemName, template);
86-
87-
var newComment = new CommentItem(newItem);
88-
newComment.BeginEdit();
89-
newComment.Name.Field.Value = args.Comment.AuthorName;
90-
newComment.Email.Field.Value = args.Comment.AuthorEmail;
91-
newComment.Comment.Field.Value = args.Comment.Text;
92-
93-
foreach (var entry in args.Comment.Fields)
94-
{
95-
newComment.InnerItem[entry.Key] = entry.Value;
96-
}
97-
98-
newComment.EndEdit();
99-
100-
args.CommentItem = newComment;
101-
}
102-
}
103-
finally
104-
{
105-
siteSwitcher?.Dispose();
106-
}
107-
}
108-
else
109-
{
110-
var message = "Failed to find blog for entry {0}\r\nIgnoring comment: name='{1}', email='{2}', commentText='{3}'";
45+
var entryItem = args.Database.GetItem(args.EntryID, args.Language);
46+
if (entryItem != null)
47+
{
48+
var blogItem = BlogManager.GetCurrentBlog(entryItem);
49+
if (blogItem != null)
50+
{
51+
var template = args.Database.GetTemplate(blogItem.BlogSettings.CommentTemplateID);
52+
var itemName =
53+
ItemUtil.ProposeValidItemName(string.Format("Comment at {0} by {1}",
54+
GetDateTime().ToString("yyyyMMdd HHmmss"), args.Comment.AuthorName));
55+
if (itemName.Length > 100)
56+
{
57+
itemName = itemName.Substring(0, 100);
58+
}
59+
60+
// verify the comment item name is unique for this entry
61+
var query = BuildFastQuery(entryItem, itemName);
62+
63+
var num = 1;
64+
var nondupItemName = itemName;
65+
while (entryItem.Database.SelectSingleItem(query) != null)
66+
{
67+
nondupItemName = itemName + " " + num;
68+
num++;
69+
query = BuildFastQuery(entryItem, nondupItemName);
70+
}
71+
72+
// need to create the comment within the shell site to ensure workflow is applied to comment
73+
var shellSite = SiteContextFactory.GetSiteContext(Sitecore.Constants.ShellSiteName);
74+
SiteContextSwitcher siteSwitcher = null;
75+
76+
try
77+
{
78+
if (shellSite != null)
79+
{
80+
siteSwitcher = new SiteContextSwitcher(shellSite);
81+
}
82+
83+
using (new SecurityDisabler())
84+
{
85+
var newItem = entryItem.Add(nondupItemName, template);
86+
87+
var newComment = new CommentItem(newItem);
88+
newComment.BeginEdit();
89+
newComment.Name.Field.Value = args.Comment.AuthorName;
90+
newComment.Email.Field.Value = args.Comment.AuthorEmail;
91+
newComment.Comment.Field.Value = args.Comment.Text;
92+
93+
foreach (var entry in args.Comment.Fields)
94+
{
95+
newComment.InnerItem[entry.Key] = entry.Value;
96+
}
97+
98+
newComment.EndEdit();
99+
100+
args.CommentItem = newComment;
101+
}
102+
}
103+
finally
104+
{
105+
siteSwitcher?.Dispose();
106+
}
107+
}
108+
else
109+
{
110+
var message = "Failed to find blog for entry {0}\r\nIgnoring comment: name='{1}', email='{2}', commentText='{3}'";
111111
Logger.Error(string.Format(message, args.EntryID, args.Comment.AuthorName, args.Comment.AuthorEmail, args.Comment.Text), typeof(CreateCommentItem));
112-
}
113-
}
114-
else
115-
{
116-
var message = "Failed to find blog entry {0}\r\nIgnoring comment: name='{1}', email='{2}', commentText='{3}'";
112+
}
113+
}
114+
else
115+
{
116+
var message = "Failed to find blog entry {0}\r\nIgnoring comment: name='{1}', email='{2}', commentText='{3}'";
117117
Logger.Error(string.Format(message, args.EntryID, args.Comment.AuthorName, args.Comment.AuthorEmail, args.Comment.Text), typeof(CreateCommentItem));
118-
}
119-
}
120-
121-
protected virtual DateTime GetDateTime()
122-
{
123-
return DateTime.Now;
124-
}
125-
}
118+
}
119+
}
120+
121+
protected virtual string BuildFastQuery(Item parentItem, string itemName)
122+
{
123+
string path = $"{parentItem.Paths.FullPath}/{itemName}";
124+
string escapePath = ContentHelper.EscapePath(path);
125+
return $"fast:{escapePath}";
126+
}
127+
128+
protected virtual DateTime GetDateTime()
129+
{
130+
return DateTime.Now;
131+
}
132+
}
126133
}

test/UnitTest/ContentHelper.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using NUnit.Framework;
2+
3+
namespace Sitecore.Modules.WeBlog.UnitTest
4+
{
5+
[TestFixture]
6+
public class ContentHelper
7+
{
8+
[TestCase("/sitecore/content/Home/r1/2016/December/Mile Post/Comment at 20161219 174023 by True Religion")]
9+
public void EscapePath(string path)
10+
{
11+
var result = WeBlog.ContentHelper.EscapePath(path);
12+
var expected = "/#sitecore#/#content#/#Home#/#r1#/#2016#/#December#/#Mile Post#/#Comment at 20161219 174023 by True Religion#";
13+
Assert.That(result, Is.EqualTo(expected));
14+
}
15+
}
16+
}

test/UnitTest/UnitTest.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
<Compile Include="..\..\src\SolutionInfo.cs">
9797
<Link>Properties\SolutionInfo.cs</Link>
9898
</Compile>
99+
<Compile Include="ContentHelper.cs" />
99100
<Compile Include="Extensions\MockExtensions.cs" />
100101
<Compile Include="Managers\EntryManagerFixture.cs" />
101102
<Compile Include="Extensions\ItemExtensionsFixture.cs" />

0 commit comments

Comments
 (0)