-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
304 lines (260 loc) · 11.8 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Xml.Linq;
using BlogML.Helper.BlogML;
using CsUtilities;
namespace BlogML.Helper
{
class Program
{
readonly static XNamespace blogmlNS = "http://www.blogml.com/2006/09/BlogML";
static void Main(string[] args)
{
ConsoleCopy consoleLog = null;
try
{
//Parse and create settings
Setting setting = ParseInput(args);
//var folderPath ="c:\temp\"
string logPath = String.Format("ConsoleLog-{0}-{1}.log", setting.ToolAction,DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss"));
consoleLog = new CsUtilities.ConsoleCopy(logPath);
//Check if action is present
if (setting.ToolAction == ToolAction.Unknown)
{
Console.WriteLine("/Action: { RemoveComments | ExportToWRX | QATarget | QASource | NewWRXWithOnlyFailedPosts} is Mandatory");
return;
}
//We got Action
switch (setting.ToolAction)
{
case ToolAction.RemoveComments:
if (RequiredParametersPrintUsage(ToolAction.RemoveComments, args))
{
RemoveAllComments(setting.BlogMLFileName);
Console.WriteLine("All comments removed from the file : {0}", setting.BlogMLFileName);
}
break;
case ToolAction.ExportToWRX:
if (RequiredParametersPrintUsage(ToolAction.ExportToWRX, args))
{
string wrxFileName = BlogMLToWRXConverter.GenerateWRXFile(setting.BlogMLFileName);
Console.WriteLine("Created WRX format");
//Generate ReDirect, SourceQA and TargetQA File
BlogMLToWRXConverter.GenerateHelperFiles(setting.SourceBaseUrl, setting.TargetBaseUrl, wrxFileName);
}
break;
case ToolAction.QATarget:
if (RequiredParametersPrintUsage(ToolAction.QATarget, args))
{
QATarget(setting.QATargetFileName);
}
break;
case ToolAction.NewWRXWithOnlyFailedPosts:
if (RequiredParametersPrintUsage(ToolAction.NewWRXWithOnlyFailedPosts, args))
{
string wrxFileName = BlogMLToWRXConverter.GenerateWRXFileWithFailedPosts(setting.WRXFileName, setting.QAReportFileName);
Console.WriteLine("Successfully created WRX file with only error post names");
}
break;
default:
throw new ArgumentOutOfRangeException();
}
Console.WriteLine("ALL Done");
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex);
Console.WriteLine();
PringUsage();
}
if(consoleLog!=null)consoleLog.Dispose();
}
private static void RemoveAllComments(string fileName)
{
try
{
XDocument xd = XDocument.Load(fileName);
var posts = from c in xd.Descendants(blogmlNS + "post")
select c;
foreach (var post in posts)
{
var comments = post.Element(blogmlNS + "comments");
if (comments != null)
{
string postName = post.Element(blogmlNS + "post-name").Value;
var commentsCount = from r in comments.Descendants(blogmlNS + "comment")
select r;
Console.WriteLine("{0}. {1}. REMOVED", commentsCount.Count(), postName);
comments.Remove();
}
}
xd.Save(fileName);
}
catch (Exception ex)
{
throw (new Exception(String.Format("An error occurred. {0}", ex)));
}
}
private static void QATarget(string fileName)
{
//string url = "http://blogbiztalk360.azurewebsites.net/extended-xmlvalidation-pipeline-component";
string qaReportFileName = string.Format("{0}.Report.txt", Path.GetFileNameWithoutExtension(fileName));
StreamWriter swQACheck = File.CreateText(qaReportFileName);
foreach (var url in File.ReadAllLines(fileName))
{
string format;
try
{
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
using (HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse())
{
format = string.Format("{0},{1}", response.StatusCode, url);
}
}
catch (WebException ex)
{
format = string.Format("{0},{1}", ex.Status, url);
}
Console.WriteLine(format);
swQACheck.WriteLine(format);
}
swQACheck.Close();
}
private static Setting ParseInput(string[] args)
{
const string validArguments = "/Action: /BlogMLFile: /WRXFile: /QASourceFile: /QATargetFile: /QAReportFile: /SourceUrl: /TargetUrl: /NewWRXWithOnlyFailedPosts:";
Setting setting = new Setting();
foreach (string s in args)
{
if (s.IndexOf(':') == -1)
throw new Exception("Arguments not in the format /Key:Valye");
string argumentKey = s.Split(':')[0].ToUpper();
string argumentValue = s.Split(':')[1];
if (!validArguments.ToUpper().Contains(argumentKey))
{
throw new Exception("Unknown parameters passed. Valid values are " + validArguments);
}
if (argumentKey == "/ACTION")
setting.ToolAction = (ToolAction)Enum.Parse(typeof(ToolAction), argumentValue, false);
if (argumentKey == "/BLOGMLFILE")
setting.BlogMLFileName = argumentValue;
if (argumentKey == "/WRXFILE")
setting.WRXFileName = argumentValue;
if (argumentKey == "/QASOURCEFILE")
setting.QASourceFileName = argumentValue;
if (argumentKey == "/QATARGETFILE")
setting.QATargetFileName = argumentValue;
if (argumentKey == "/SOURCEURL")
setting.SourceBaseUrl = argumentValue;
if (argumentKey == "/TARGETURL")
setting.TargetBaseUrl = argumentValue;
if (argumentKey == "/TARGETURL")
setting.TargetBaseUrl = argumentValue;
if (argumentKey == "/QAREPORTFILE")
setting.QAReportFileName = argumentValue;
}
return setting;
}
private static void PringUsage()
{
const string exeName = "BlogML.Helper";
Console.WriteLine("BlogML to WRX Converter by Saravana Kumar (http://www.biztalk360.com)");
Console.WriteLine("This tool helps to convert BlogML to WRX and also helps to generate some QA text files for verifications");
Console.WriteLine();
Console.WriteLine("Remove Comments: If you got lot of spam comments, it's worth removing it.");
Console.WriteLine("{0} /Action:RemoveComments /BlogMLFile:yourblogmlfile.xml", exeName);
Console.WriteLine();
Console.WriteLine("Export To WRX: Will create WordPress Export file (WRX) and also supporting redirect, source and target QA files (for verification).");
Console.WriteLine("{0} /Action:ExportToWRX /BlogMLFile:yourblogmlfile.xml /SourceUrl:blogs.digitaldeposit.net /TargetUrl:blogs.biztalk360.com", exeName);
Console.WriteLine();
Console.WriteLine("QA Source. Check whether all the original url are redirection to 301");
Console.WriteLine("{0} /Action:QASource /QASourceFile:QASourceFile.xml ", exeName);
Console.WriteLine();
Console.WriteLine("QA Target. Check whether all the new urls are correct. Will produce a report file with HTTP Status.");
Console.WriteLine("{0} /Action:QATarget /QATargetFile:QATargetFile.xml ", exeName);
}
private static bool RequiredParametersPrintUsage(ToolAction action, string[] args)
{
string validArguments;
switch (action)
{
case ToolAction.RemoveComments:
validArguments = "/Action: /BlogMLFile:";
if (args.Length != 2)
{
Console.WriteLine("/BlogMLFile: is mandatory");
return false;
}
break;
case ToolAction.ExportToWRX:
validArguments = "/Action: /BlogMLFile: /SourceUrl: /TargetUrl:";
if (args.Length != 4)
{
Console.WriteLine("/BlogMLFile: /SourceUrl: /TargetUrl: are mandatory");
return false;
}
break;
case ToolAction.QATarget:
validArguments = "/Action: /QATargetFile:";
if (args.Length != 2)
{
Console.WriteLine("/QATargetFile: is mandatory");
return false;
}
break;
case ToolAction.QASource:
validArguments = "/Action: /QASourceFile:";
if (args.Length != 2)
{
Console.WriteLine("/QASourceFile: is mandatory");
return false;
}
break;
case ToolAction.NewWRXWithOnlyFailedPosts:
validArguments = "/Action: /WRXFile: /QAReportFile:";
if (args.Length != 3)
{
Console.WriteLine("/WRXFile: /QAReportFile are mandatory");
return false;
}
break;
default:
throw new ArgumentOutOfRangeException("action");
}
foreach (string s in args)
{
string argumentKey = s.Split(':')[0].ToUpper();
if (!validArguments.ToUpper().Contains(argumentKey))
{
Console.WriteLine("Unknown parameters passed. Valid values are " + validArguments);
return false;
}
}
return true;
}
}
enum ToolAction
{
Unknown,
RemoveComments,
ExportToWRX,
QATarget,
QASource,
NewWRXWithOnlyFailedPosts
}
class Setting
{
public ToolAction ToolAction { get; set; }
public string BlogMLFileName { get; set; }
public string WRXFileName { get; set; }
public string QASourceFileName { get; set; }
public string QATargetFileName { get; set; }
public string SourceBaseUrl { get; set; }
public string TargetBaseUrl { get; set; }
public string QAReportFileName { get; set; }
}
}