-
Notifications
You must be signed in to change notification settings - Fork 20
/
PageInfo.cs
315 lines (267 loc) · 10.4 KB
/
PageInfo.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
305
306
307
308
309
310
311
312
313
314
315
/*
Copyright (C) 2009 Max Semenik
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
using System.Xml;
using System.IO;
using System;
namespace WikiFunctions.API
{
/// <summary>
/// This class represents information about the page currently being edited
/// </summary>
public sealed class PageInfo
{
internal PageInfo()
{
}
// TODO: adopt for retrieval of information for protection, deletion, etc.
internal PageInfo(string xml)
{
XmlReader xr = XmlReader.Create(new StringReader(xml));
string normalisedFrom = null, redirectFrom = null;
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
var redirects = doc.GetElementsByTagName("r");
if (redirects.Count >= 1) //We have redirects
{
var first = redirects[0].Attributes;
var last = redirects[redirects.Count - 1].Attributes;
if (first != null && last != null && (first["from"].Value == last["to"].Value ||
last["from"].Value == last["to"].Value))
{
//Redirect loop
TitleChangedStatus = PageTitleStatus.RedirectLoop;
OriginalTitle = Title = first["from"].Value;
Exists = true;
Text = "";
return; // We're not going to have any page text as there is a redirect loop
}
redirectFrom = first != null ? first["from"].Value : "";
//Valid redirects
TitleChangedStatus = redirects.Count == 1
? PageTitleStatus.Redirected
: PageTitleStatus.MultipleRedirects;
}
else
{
TitleChangedStatus = PageTitleStatus.NoChange;
}
if (!xr.ReadToFollowing("page"))
{
if (redirects.Count > 0)
{
// If there are redirects, but no page element, chances are it's a redirect to IW or something
// similar
return;
}
throw new Exception("Cannot find <page> element");
}
//Normalised before redirect, so would be root. Could still be multiple redirects, or looped
var normalised = doc.GetElementsByTagName("n");
if (normalised.Count > 0 && normalised[0].Attributes != null)
{
normalisedFrom = normalised[0].Attributes["from"].Value;
if (TitleChangedStatus == PageTitleStatus.NoChange)
TitleChangedStatus = PageTitleStatus.Normalised;
else
TitleChangedStatus |= PageTitleStatus.Normalised;
}
//Normalisation occurs before redirection, so if that exists, that is the title passed to the API
if (!string.IsNullOrEmpty(normalisedFrom))
{
OriginalTitle = normalisedFrom;
}
else if (!string.IsNullOrEmpty(redirectFrom))
{
OriginalTitle = redirectFrom;
}
Exists = (xr.GetAttribute("missing") == null); //if null, page exists
IsWatched = (xr.GetAttribute("watched") != null);
var tokens = doc.GetElementsByTagName("tokens");
if (tokens.Count == 0)
{
// Token support for < 1.24
EditToken = xr.GetAttribute("edittoken");
ProtectToken = xr.GetAttribute("protecttoken");
DeleteToken = xr.GetAttribute("deletetoken");
MoveToken = xr.GetAttribute("movetoken");
WatchToken = xr.GetAttribute("watchtoken");
}
else if (tokens[0].Attributes != null)
{
EditToken = tokens[0].Attributes["csrftoken"].Value;
ProtectToken = tokens[0].Attributes["csrftoken"].Value;
DeleteToken = tokens[0].Attributes["csrftoken"].Value;
MoveToken = tokens[0].Attributes["csrftoken"].Value;
WatchToken = tokens[0].Attributes["watchtoken"].Value;
RollbackToken = tokens[0].Attributes["rollbacktoken"].Value;
}
TokenTimestamp = xr.GetAttribute("starttimestamp");
long revId;
RevisionID = long.TryParse(xr.GetAttribute("lastrevid"), out revId) ? revId : -1;
Title = xr.GetAttribute("title");
DisplayTitle = xr.GetAttribute("displaytitle");
var ns = xr.GetAttribute("ns");
NamespaceID = ns != null ? int.Parse(ns) : 0;
if (xr.ReadToDescendant("protection") && !xr.IsEmptyElement)
{
foreach (XmlNode xn in doc.GetElementsByTagName("pr"))
{
switch (xn.Attributes["type"].Value)
{
case "edit":
EditProtection = xn.Attributes["level"].Value;
break;
case "move":
MoveProtection = xn.Attributes["level"].Value;
break;
case "create":
CreateProtection = xn.Attributes["level"].Value;
break;
}
}
}
xr.ReadToFollowing("revisions");
xr.ReadToDescendant("rev");
Timestamp = xr.GetAttribute("timestamp");
Text = Tools.ConvertToLocalLineEndings(xr.ReadString());
}
/// <summary>
/// Display title of the Page in HTML format, used e.g. if page has some italics (using {{italic title}} etc.)
/// </summary>
public string DisplayTitle
{ get; private set; }
/// <summary>
/// Title of the Page
/// </summary>
public string Title
{ get; private set; }
/// <summary>
/// Original title (before redirects/normalisation) of the Page
/// </summary>
public string OriginalTitle
{ get; private set; }
/// <summary>
/// Why OriginalTitle differs from Title
/// </summary>
public PageTitleStatus TitleChangedStatus
{ get; private set; }
/// <summary>
/// Text of the Page
/// </summary>
public string Text
{ get; private set; }
/// <summary>
/// Whether the page exists or not
/// </summary>
public bool Exists
{ get; private set; }
/// <summary>
/// Revision ID, -1 if N/A
/// </summary>
public long RevisionID
{ get; private set; }
/// <summary>
/// Namespace number
/// </summary>
public int NamespaceID
{ get; private set; }
/// <summary>
/// Timestamp of the latest revision of the page
/// </summary>
public string Timestamp
{ get; private set; }
/// <summary>
/// Edit token (https://www.mediawiki.org/wiki/Manual:Edit_token)
/// </summary>
public string EditToken
{ get; internal set; }
/// <summary>
/// Delete Token
/// </summary>
public string DeleteToken
{ get; internal set; }
/// <summary>
/// Protect Token
/// </summary>
public string ProtectToken
{ get; internal set; }
/// <summary>
/// Move Token
/// </summary>
public string MoveToken
{ get; internal set; }
/// <summary>
/// Watch Token
/// </summary>
public string WatchToken
{ get; internal set; }
/// <summary>
/// Rollback Token
/// </summary>
public string RollbackToken
{ get; internal set; }
/// <summary>
/// Time when the token was obtained. Used for deletion detection.
/// </summary>
public string TokenTimestamp
{ get; private set; }
/// <summary>
/// String of any edit protection applied to the page
/// </summary>
public string EditProtection
{ get; private set; }
/// <summary>
/// String of any move protection applied to the page
/// </summary>
public string MoveProtection
{ get; private set; }
/// <summary>
/// String of any create protection applied to the page
/// </summary>
public string CreateProtection
{ get; private set; }
/// <summary>
/// Whether the current user is watching this page
/// </summary>
public bool IsWatched
{ get; set; }
/// <summary>
/// Was the specified PageInfo redirected to get to the final target
/// </summary>
/// <param name="page">PageInfo object</param>
/// <returns>Whether the article was redirected</returns>
public static bool WasRedirected(PageInfo page)
{
PageTitleStatus pts = page.TitleChangedStatus;
if (pts == PageTitleStatus.NoChange)
return false;
return ((pts & PageTitleStatus.Redirected) == PageTitleStatus.Redirected ||
(pts & PageTitleStatus.RedirectLoop) == PageTitleStatus.RedirectLoop ||
(pts & PageTitleStatus.MultipleRedirects) == PageTitleStatus.MultipleRedirects);
}
}
/// <summary>
///
/// </summary>
[Flags]
public enum PageTitleStatus
{
NoChange = 0,
RedirectLoop = 1,
MultipleRedirects = 2,
Redirected = 4,
Normalised = 8,
}
}