-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathWikipediaIO.cs
More file actions
396 lines (346 loc) · 13.6 KB
/
WikipediaIO.cs
File metadata and controls
396 lines (346 loc) · 13.6 KB
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Xml;
namespace WikiAccess
{
/// <summary>
/// Class to retrieve a Wikipedia page
/// </summary>
public class WikipediaIO : WikimediaApi
{
protected override string APIurl { get { return @"http://en.wikipedia.org/w/api.php?"; } }
protected override string Parameters
{
get
{
string Param = "action=" + Action;
if (Titles != "") Param += "&titles=" + HttpUtility.UrlEncode(Titles);
if (Format != "") Param += "&format=" + Format;
if (Redirects != "") Param += "&redirects";
if (Export != "") Param += "&export";
if (ExportNoWrap != "") Param += "&exportnowrap";
return Param;
}
}
public List<string[]> TemplatesUsed;
public List<string> CategoriesUsed;
public string Article { get; set; }
public string Action { get; set; }
public string Titles { get; set; }
public string Format { get; set; }
public string Redirects { get; set; }
public string Export { get; set; }
public string ExportNoWrap { get; set; }
public string PageTitle { get; set; }
private WikipediaIOErrorLog WikipediaErrors { get; set; }
public WikipediaIO()
: base()
{
WikipediaErrors = new WikipediaIOErrorLog();
TemplatesUsed = new List<string[]>();
CategoriesUsed = new List<string>();
}
/// <summary>
/// Download a page from Wikipedia and process
/// </summary>
/// <returns>True = success</returns>
public bool GetData()
{
if (GrabPage())
{
if (ExtractXML())
{
ExtractCategories();
ExtractTemplates();
return true;
}
else
{
WikipediaErrors.UnableToParseXML();
return false;
}
}
else
{
WikipediaErrors.UnableToRetrieveData();
return false;
}
}
/// <summary>
/// Extract the article from the downloaded XML content
/// </summary>
/// <returns></returns>
private bool ExtractXML()
{
bool WikipediaArticleExists = false;
using (XmlReader Reader = XmlReader.Create(new StringReader(Content)))
{
string[] thisElementName = new string[5];
while (Reader.Read())
{
switch (Reader.NodeType)
{
case XmlNodeType.Element:
thisElementName[Reader.Depth] = Reader.Name;
break;
case XmlNodeType.Text:
if (thisElementName[0] == "mediawiki")
{
if (thisElementName[1] == "page")
{
if (thisElementName[2] == "title")
{
PageTitle = Reader.Value;
}
if (thisElementName[2] == "revision")
{
if (thisElementName[3] == "text")
{
WikipediaArticleExists = true;
Article = RemoveHTMLcomments(ReplaceDash(RemoveTerminators(Reader.Value)));
}
}
}
}
break;
}
}
if (!WikipediaArticleExists)
{
WikipediaErrors.ArticleNotExists();
return false;
}
else
return true;
}
}
private string RemoveHTMLcomments(string OriginalText)
{
string ThisText = OriginalText;
int CommentStart = 0;
do
{
CommentStart = ThisText.IndexOf("<!--", StringComparison.Ordinal);
if (CommentStart != -1)
{
int CommentEnd = ThisText.IndexOf("-->", CommentStart + 3, StringComparison.Ordinal);
if (CommentEnd == -1 || CommentEnd < CommentStart)
{
WikipediaErrors.UnbalancedHTMLcomment();
return ThisText;
}
else
{
string Comment = ThisText.Substring(CommentStart, CommentEnd - CommentStart + 3);
ThisText = ThisText.Replace(Comment, "");
}
}
} while (CommentStart != -1);
return ThisText;
}
private string RemoveTerminators(string originalText)
{
string newText = originalText;
newText = newText.Replace("\u000a", string.Empty);
newText = newText.Replace("\u000b", string.Empty);
newText = newText.Replace("\u000c", string.Empty);
newText = newText.Replace("\u000d", string.Empty);
newText = newText.Replace("\u0085", string.Empty);
newText = newText.Replace("\u2028", string.Empty);
newText = newText.Replace("\u2029", string.Empty);
return newText;
}
private string ReplaceDash(string original)
{
string Output = original;
Output = Output.Replace("\u058A", "-");
Output = Output.Replace("\u05BE", "-");
Output = Output.Replace("\u1400", "-");
Output = Output.Replace("\u1806", "-");
Output = Output.Replace("\u2010", "-");
Output = Output.Replace("\u2011", "-");
Output = Output.Replace("\u2012", "-");
Output = Output.Replace("\u2013", "-");
Output = Output.Replace("\u2014", "-");
Output = Output.Replace("\u2015", "-");
Output = Output.Replace("\u2E17", "-");
Output = Output.Replace("\u2E1A", "-");
Output = Output.Replace("\u2E3A", "-");
Output = Output.Replace("\u2E3B", "-");
Output = Output.Replace("\u301C", "-");
Output = Output.Replace("\u3030", "-");
Output = Output.Replace("\u30A0", "-");
Output = Output.Replace("\uFE31", "-");
Output = Output.Replace("\uFE32", "-");
Output = Output.Replace("\uFE58", "-");
Output = Output.Replace("\uFE63", "-");
Output = Output.Replace("\uFF0D", "-");
return Output;
}
private void ExtractCategories()
{
int catStart = Article.IndexOf("[[Category:", StringComparison.Ordinal);
while (catStart > 0)
{
int catNextPipe = Article.IndexOf("|", catStart, StringComparison.Ordinal);
int catNextClose = Article.IndexOf("]]", catStart, StringComparison.Ordinal);
int catFinish = 0;
if (catNextPipe < catNextClose && catNextPipe > 0)
{
catFinish = catNextPipe;
}
else
{
catFinish = catNextClose;
}
if (catStart != -1 && catFinish != -1 && catFinish > catStart)
{
CategoriesUsed.Add(Article.Substring(catStart + 11, catFinish - catStart - 11).ToLower().Trim());
catStart = Article.IndexOf("[[Category:", catFinish, StringComparison.Ordinal);
}
else
{
WikipediaErrors.UnbalancedCategoryBrackets();
catStart = -99;
}
}
}
private void ExtractTemplates()
{
int TplStart = Article.IndexOf("{{", StringComparison.Ordinal);
while (TplStart >= 0)
{
int TplNextPipe = Article.IndexOf("|", TplStart, StringComparison.Ordinal);
int TplNextClose = Article.IndexOf("}}", TplStart, StringComparison.Ordinal);
int TplFinish = 0;
if (TplNextPipe < TplNextClose && TplNextPipe > 0)
{
TplFinish = TplNextPipe;
}
else
{
TplFinish = TplNextClose;
}
if (TplStart != -1 && TplFinish != -1 && TplFinish > TplStart)
{
string[] thisTemplate = new string[2];
thisTemplate[0] = Article.Substring(TplStart + 2, TplFinish - TplStart - 2).ToLower().Trim();
thisTemplate[1] = getFullTemplate(TplStart);
TemplatesUsed.Add(thisTemplate);
TplStart = Article.IndexOf("{{", TplFinish, StringComparison.Ordinal);
}
else
{
WikipediaErrors.UnbalancedTemplateBrackets();
TplStart = -99;
}
}
}
/// <summary>
/// Function to remove Wikipedia style [[links]]
/// </summary>
/// <param name="OriginalText"></param>
/// <param name="RevisedText"></param>
/// <returns></returns>
public static bool DelinkText(string OriginalText, out string RevisedText)
{
string thisText = OriginalText;
int linkStart = 0;
// Look for first [[ and ]], replace them with text
// repeat until no more [[ or ]]
do
{
linkStart = thisText.IndexOf("[[", StringComparison.Ordinal);
if (linkStart != -1)
{
//Found start
int linkEnd = thisText.IndexOf("]]", StringComparison.Ordinal);
if (linkEnd == -1 || linkEnd < linkStart)
{
//Did not find close
RevisedText = OriginalText;
return false;
}
else
{
// Found link, extract text
string link = thisText.Substring(linkStart, linkEnd - linkStart + 2);
string newlink = link.Substring(2, link.Length - 4);
// If its piped, remove left side
int pipe = newlink.IndexOf("|", StringComparison.Ordinal);
if (pipe != -1)
{
newlink = newlink.Substring(pipe + 1);
}
// Replace [[link]] with newlink
thisText = thisText.Replace(link, newlink);
}
}
else
{
//Did not find a [[, probably finished
//but first check there are no closing ]]
int linkClose = thisText.IndexOf("]]", StringComparison.Ordinal);
if (linkClose != -1)
{
RevisedText = OriginalText;
return false;
}
}
} while (linkStart != -1);
RevisedText = thisText;
return true;
}
public List<ErrorLog> GetErrors()
{
List<ErrorLog> Logs = new List<ErrorLog>();
Logs.Add(APIErrors);
Logs.Add(WikipediaErrors);
return Logs;
}
private string getFullTemplate(int startpoint)
{
int Endpoint = 0;
int LeftBraceCount = 0;
int RightBraceCount = 0;
/* There might be a template within the template, so we just cant search for next }}
* instead I count all further occurances of {{ or }}, when they balance I have the full original template.
* In case the template is {{{{Hello}}wave}} I do an i++ to avoid matching 2+3 bracket. */
for (int i = startpoint; i < Article.Length - 1; i++)
{
if (Article.Substring(i, 2) == "{{")
{
LeftBraceCount++;
i++;
}
if (Article.Substring(i, 2) == "}}")
{
RightBraceCount++;
i++;
}
if (LeftBraceCount == RightBraceCount)
{
Endpoint = i;
break;
}
}
string TemplateText = "";
if (LeftBraceCount != RightBraceCount)
{
int PipePos = Article.IndexOf("|",startpoint);
WikipediaErrors.UnableToExtractTemplate(Article.Substring(startpoint+2,PipePos-startpoint-2));
return null;
}
if (DelinkText(Article.Substring(startpoint + 2, Endpoint - startpoint - 3), out TemplateText))
{
return TemplateText;
}
else
{
return null;
}
}
}
}