-
Notifications
You must be signed in to change notification settings - Fork 1
/
DotNetWikiBot.cs
4829 lines (4600 loc) · 216 KB
/
DotNetWikiBot.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
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// DotNetWikiBot Framework 2.97 - bot framework based on Microsoft .NET Framework 2.0 for wiki projects
// Distributed under the terms of the MIT (X11) license: http://www.opensource.org/licenses/mit-license.php
// Copyright (c) Iaroslav Vassiliev (2006-2011) [email protected]
using System;
using System.IO;
using System.IO.Compression;
using System.Globalization;
using System.Threading;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Net;
using System.Xml;
using System.Xml.XPath;
using System.Web;
namespace DotNetWikiBot
{
/// <summary>Class defines wiki site object.</summary>
[ClassInterface(ClassInterfaceType.AutoDispatch)]
[Serializable]
public class Site
{
/// <summary>Wiki site URL.</summary>
public string site;
/// <summary>User's account to login with.</summary>
public string userName;
/// <summary>User's password to login with.</summary>
private string userPass;
/// <summary>Default domain for LDAP authentication. Additional information can
/// be found at http://www.mediawiki.org/wiki/Extension:LDAP_Authentication.</summary>
public string userDomain = "";
/// <summary>Site title.</summary>
public string name;
/// <summary>MediaWiki version as string.</summary>
public string generator;
/// <summary>MediaWiki version as number.</summary>
public float version;
/// <summary>MediaWiki version as Version object.</summary>
public Version ver;
/// <summary>Rule of page title capitalization.</summary>
public string capitalization;
/// <summary>Short relative path to wiki pages (if such alias is set on the server).
/// See "http://www.mediawiki.org/wiki/Manual:Short URL" for details.</summary>
public string wikiPath; // = "/wiki/";
/// <summary>Relative path to "index.php" file on server.</summary>
public string indexPath; // = "/w/";
/// <summary>User's watchlist. Should be loaded manually with FillFromWatchList function,
/// if it is necessary.</summary>
public PageList watchList;
/// <summary>MediaWiki interface messages. Should be loaded manually with
/// GetMediaWikiMessagesEx function, if it is necessary.</summary>
public PageList messages;
/// <summary>Regular expression to find redirection target.</summary>
public Regex redirectRE;
/// <summary>Regular expression to find links to pages in list in HTML source.</summary>
public static Regex linkToPageRE1 =
new Regex("<li><a href=\"[^\"]*?\" (?:class=\"mw-redirect\" )?title=\"([^\"]+?)\">");
/// <summary>Alternative regular expression to find links to pages in HTML source.</summary>
public static Regex linkToPageRE2 =
new Regex("<a href=\"[^\"]*?\" title=\"([^\"]+?)\">\\1</a>");
/// <summary>Alternative regular expression to find links to pages (mostly image and file
/// pages) in HTML source.</summary>
public Regex linkToPageRE3;
/// <summary>Regular expression to find links to subcategories in HTML source
/// of category page on sites that use "CategoryTree" MediaWiki extension.</summary>
public static Regex linkToSubCategoryRE =
new Regex(">([^<]+)</a></div>\\s*<div class=\"CategoryTreeChildren\"");
/// <summary>Regular expression to find links to image pages in galleries
/// in HTML source.</summary>
public static Regex linkToImageRE =
new Regex("<div class=\"gallerytext\">\n<a href=\"[^\"]*?\" title=\"([^\"]+?)\">");
/// <summary>Regular expression to find titles in markup.</summary>
public static Regex pageTitleTagRE = new Regex("<title>(.+?)</title>");
/// <summary>Regular expression to find internal wiki links in markup.</summary>
public static Regex wikiLinkRE = new Regex(@"\[\[(.+?)(\|.+?)?]]");
/// <summary>Regular expression to find wiki category links.</summary>
public Regex wikiCategoryRE;
/// <summary>Regular expression to find wiki templates in markup.</summary>
public static Regex wikiTemplateRE = new Regex(@"(?s)\{\{(.+?)((\|.*?)*?)}}");
/// <summary>Regular expression to find embedded images and files in wiki markup.</summary>
public Regex wikiImageRE;
/// <summary>Regular expression to find links to sister wiki projects in markup.</summary>
public static Regex sisterWikiLinkRE;
/// <summary>Regular expression to find interwiki links in wiki markup.</summary>
public static Regex iwikiLinkRE;
/// <summary>Regular expression to find displayed interwiki links in wiki markup,
/// like "[[:de:...]]".</summary>
public static Regex iwikiDispLinkRE;
/// <summary>Regular expression to find external web links in wiki markup.</summary>
public static Regex webLinkRE =
new Regex("(https?|t?ftp|news|nntp|telnet|irc|gopher)://([^\\s'\"<>]+)");
/// <summary>Regular expression to find sections of text, that are explicitly
/// marked as non-wiki with special tag.</summary>
public static Regex noWikiMarkupRE = new Regex("(?is)<nowiki>(.*?)</nowiki>");
/// <summary>A template for disambiguation page. If some unusual template is used in your
/// wiki for disambiguation, then it must be set in this variable. Use "|" as a delimiter
/// when enumerating several templates here.</summary>
public string disambigStr;
/// <summary>Regular expression to extract language code from site URL.</summary>
public static Regex siteLangRE = new Regex(@"http://(.*?)\.(.+?\..+)");
/// <summary>Regular expression to extract edit session time attribute.</summary>
public static Regex editSessionTimeRE1 =
new Regex("value=\"([^\"]*?)\" name=['\"]wpEdittime['\"]");
/// <summary>Regular expression to extract edit session time attribute.</summary>
public static Regex editSessionTimeRE3 = new Regex(" touched=\"(.+?)\"");
/// <summary>Regular expression to extract edit session token attribute.</summary>
public static Regex editSessionTokenRE1 =
new Regex("value=\"([^\"]*?)\" name=['\"]wpEditToken['\"]");
/// <summary>Regular expression to extract edit session token attribute.</summary>
public static Regex editSessionTokenRE2 =
new Regex("name=['\"]wpEditToken['\"](?: type=\"hidden\")? value=\"([^\"]*?)\"");
/// <summary>Regular expression to extract edit session token attribute.</summary>
public static Regex editSessionTokenRE3 = new Regex(" edittoken=\"(.+?)\"");
/// <summary>Site cookies.</summary>
public CookieContainer cookies = new CookieContainer();
/// <summary>XML name table for parsing XHTML documents from wiki site.</summary>
public NameTable xhtmlNameTable = new NameTable();
/// <summary>XML namespace URI of wiki site's XHTML version.</summary>
public string xhtmlNSUri = "http://www.w3.org/1999/xhtml";
/// <summary>XML namespace manager for parsing XHTML documents from wiki site.</summary>
public XmlNamespaceManager xmlNS;
/// <summary>Local namespaces.</summary>
public Hashtable namespaces = new Hashtable();
/// <summary>Default namespaces.</summary>
public static Hashtable wikiNSpaces = new Hashtable();
/// <summary>List of Wikimedia Foundation sites and according prefixes.</summary>
public static Hashtable WMSites = new Hashtable();
/// <summary>Built-in variables of MediaWiki software, used in brackets {{...}}.
/// To be distinguished from templates.
/// (see http://meta.wikimedia.org/wiki/Help:Magic_words).</summary>
public static string[] mediaWikiVars;
/// <summary>Built-in parser functions (and similar prefixes) of MediaWiki software, used
/// like first ... in {{...:...}}. To be distinguished from templates.
/// (see http://meta.wikimedia.org/wiki/Help:Magic_words).</summary>
public static string[] parserFunctions;
/// <summary>Built-in template modifiers of MediaWiki software
/// (see http://meta.wikimedia.org/wiki/Help:Magic_words).</summary>
public static string[] templateModifiers;
/// <summary>Interwiki links sorting order, based on local language by first word.
/// See http://meta.wikimedia.org/wiki/Interwiki_sorting_order for details.</summary>
public static string[] iwikiLinksOrderByLocalFW;
/// <summary>Interwiki links sorting order, based on local language.
/// See http://meta.wikimedia.org/wiki/Interwiki_sorting_order for details.</summary>
public static string[] iwikiLinksOrderByLocal;
/// <summary>Interwiki links sorting order, based on latin alphabet by first word.
/// See http://meta.wikimedia.org/wiki/Interwiki_sorting_order for details.</summary>
public static string[] iwikiLinksOrderByLatinFW;
/// <summary>Wikimedia Foundation sites and prefixes in one regex-escaped string
/// with "|" as separator.</summary>
public static string WMSitesStr;
/// <summary>ISO 639-1 language codes, used as prefixes to identify Wikimedia
/// Foundation sites, gathered in one regex-escaped string with "|" as separator.</summary>
public static string WMLangsStr;
/// <summary>Availability of "api.php" MediaWiki extension (bot interface).</summary>
public bool botQuery;
/// <summary>Versions of "api.php" MediaWiki extension (bot interface) modules.</summary>
public Hashtable botQueryVersions = new Hashtable();
/// <summary>Set of lists of pages, produced by bot interface.</summary>
public static Hashtable botQueryLists = new Hashtable();
/// <summary>Set of lists of parsed data, produced by bot interface.</summary>
public static Hashtable botQueryProps = new Hashtable();
/// <summary>Site language.</summary>
public string language;
/// <summary>Site language text direction.</summary>
public string langDirection;
/// <summary>Site's neutral (language) culture.</summary>
public CultureInfo langCulture;
/// <summary>Randomly chosen regional (non-neutral) culture for site's language.</summary>
public CultureInfo regCulture;
/// <summary>Site encoding.</summary>
public Encoding encoding = Encoding.UTF8;
/// <summary>This constructor is used to generate most Site objects.</summary>
/// <param name="site">Wiki site's URI. It must point to the main page of the wiki, e.g.
/// "http://en.wikipedia.org" or "http://127.0.0.1:80/w/index.php?title=Main_page".</param>
/// <param name="userName">User name to log in.</param>
/// <param name="userPass">Password.</param>
/// <returns>Returns Site object.</returns>
public Site(string site, string userName, string userPass)
: this(site, userName, userPass, "") {}
/// <summary>This constructor is used for LDAP authentication. Additional information can
/// be found at "http://www.mediawiki.org/wiki/Extension:LDAP_Authentication".</summary>
/// <param name="site">Wiki site's URI. It must point to the main page of the wiki, e.g.
/// "http://en.wikipedia.org" or "http://127.0.0.1:80/w/index.php?title=Main_page".</param>
/// <param name="userName">User name to log in.</param>
/// <param name="userPass">Password.</param>
/// <param name="userDomain">Domain for LDAP authentication.</param>
/// <returns>Returns Site object.</returns>
public Site(string site, string userName, string userPass, string userDomain)
{
this.site = site;
this.userName = userName;
this.userPass = userPass;
this.userDomain = userDomain;
Initialize();
}
/// <summary>This constructor uses default site, userName and password. The site URL and
/// account data can be stored in UTF8-encoded "Defaults.dat" file in bot's "Cache"
/// subdirectory.</summary>
/// <returns>Returns Site object.</returns>
public Site()
{
if (File.Exists("Cache" + Path.DirectorySeparatorChar + "Defaults.dat") == true) {
string[] lines = File.ReadAllLines(
"Cache" + Path.DirectorySeparatorChar + "Defaults.dat", Encoding.UTF8);
if (lines.GetUpperBound(0) >= 2) {
this.site = lines[0];
this.userName = lines[1];
this.userPass = lines[2];
if (lines.GetUpperBound(0) >= 3)
this.userDomain = lines[3];
else
this.userDomain = "";
}
else
throw new WikiBotException(
Bot.Msg("\"\\Cache\\Defaults.dat\" file is invalid."));
}
else
throw new WikiBotException(Bot.Msg("\"\\Cache\\Defaults.dat\" file not found."));
Initialize();
}
/// <summary>This internal function establishes connection to site and loads general site
/// info by the use of other functions. Function is called from the constructors.</summary>
public void Initialize()
{
xmlNS = new XmlNamespaceManager(xhtmlNameTable);
if (site.Contains("sourceforge")) {
site = site.Replace("https://", "http://");
GetPaths();
xmlNS.AddNamespace("ns", xhtmlNSUri);
LoadDefaults();
LogInSourceForge();
site = site.Replace("http://", "https://");
}
else {
GetPaths();
xmlNS.AddNamespace("ns", xhtmlNSUri);
LoadDefaults();
LogIn();
}
GetInfo();
}
/// <summary>Gets path to "index.php", short path to pages (if present), and then
/// saves paths to file.</summary>
public void GetPaths()
{
if (!site.StartsWith("http"))
site = "http://" + site;
if (Bot.CountMatches(site, "/", false) == 3 && site.EndsWith("/"))
site = site.Substring(0, site.Length - 1);
string filePathName = "Cache" + Path.DirectorySeparatorChar +
HttpUtility.UrlEncode(site.Replace("://", ".").Replace("/", ".")) + ".dat";
if (File.Exists(filePathName) == true) {
string[] lines = File.ReadAllLines(filePathName, Encoding.UTF8);
if (lines.GetUpperBound(0) >= 4) {
wikiPath = lines[0];
indexPath = lines[1];
xhtmlNSUri = lines[2];
language = lines[3];
langDirection = lines[4];
if (lines.GetUpperBound(0) >= 5)
site = lines[5];
return;
}
}
Console.WriteLine(Bot.Msg("Logging in..."));
HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(site);
webReq.Proxy.Credentials = CredentialCache.DefaultCredentials;
webReq.UseDefaultCredentials = true;
webReq.ContentType = Bot.webContentType;
webReq.UserAgent = Bot.botVer;
if (Bot.unsafeHttpHeaderParsingUsed == 0) {
webReq.ProtocolVersion = HttpVersion.Version10;
webReq.KeepAlive = false;
}
HttpWebResponse webResp = null;
for (int errorCounter = 0; true; errorCounter++) {
try {
webResp = (HttpWebResponse)webReq.GetResponse();
break;
}
catch (WebException e) {
string message = e.Message;
if (Regex.IsMatch(message, ": \\(50[02349]\\) ")) { // Remote problem
if (errorCounter > Bot.retryTimes)
throw;
Console.Error.WriteLine(message + " " + Bot.Msg("Retrying in 60 seconds."));
Thread.Sleep(60000);
}
else if (message.Contains("Section=ResponseStatusLine")) { // Squid problem
Bot.SwitchUnsafeHttpHeaderParsing(true);
GetPaths();
return;
}
else {
Console.Error.WriteLine(Bot.Msg("Can't access the site.") + " " + message);
throw;
}
}
}
site = webResp.ResponseUri.Scheme + "://" + webResp.ResponseUri.Authority;
Regex wikiPathRE = new Regex("(?i)" + Regex.Escape(site) + "(/.+?/).+");
Regex indexPathRE1 = new Regex("(?i)" + Regex.Escape(site) +
"(/.+?/)index\\.php(\\?|/)");
Regex indexPathRE2 = new Regex("(?i)href=\"(/[^\"\\s<>?]*?)index\\.php(\\?|/)");
Regex xhtmlOptionsRE = new Regex("(?i)<html xmlns=\"(?<xmlns>[^\"]+)\" " +
"(xml:lang=\"(?<xmllang>[^\"]+)\" )?lang=\"(?<lang>[^\"]+)\" " +
"dir=\"(?<dir>[^\"]+)\">");
string mainPageUri = webResp.ResponseUri.ToString();
if (mainPageUri.Contains("/index.php?"))
indexPath = indexPathRE1.Match(mainPageUri).Groups[1].ToString();
else
wikiPath = wikiPathRE.Match(mainPageUri).Groups[1].ToString();
if (string.IsNullOrEmpty(indexPath) && string.IsNullOrEmpty(wikiPath) &&
mainPageUri[mainPageUri.Length-1] != '/' &&
Bot.CountMatches(mainPageUri, "/", false) == 3)
wikiPath = "/";
Stream respStream = webResp.GetResponseStream();
if (webResp.ContentEncoding.ToLower().Contains("gzip"))
respStream = new GZipStream(respStream, CompressionMode.Decompress);
else if (webResp.ContentEncoding.ToLower().Contains("deflate"))
respStream = new DeflateStream(respStream, CompressionMode.Decompress);
StreamReader strmReader = new StreamReader(respStream, encoding);
string src = strmReader.ReadToEnd();
webResp.Close();
indexPath = indexPathRE2.Match(src).Groups[1].ToString();
xhtmlNSUri = xhtmlOptionsRE.Match(src).Groups["xmlns"].ToString();
if (string.IsNullOrEmpty(xhtmlNSUri))
xhtmlNSUri = "http://www.w3.org/1999/xhtml";
language = xhtmlOptionsRE.Match(src).Groups["lang"].ToString();
langDirection = xhtmlOptionsRE.Match(src).Groups["dir"].ToString();
if (!Directory.Exists("Cache"))
Directory.CreateDirectory("Cache");
File.WriteAllText(filePathName, wikiPath + "\r\n" + indexPath + "\r\n" + xhtmlNSUri +
"\r\n" + language + "\r\n" + langDirection + "\r\n" + site, Encoding.UTF8);
}
/// <summary>Gets a specified MediaWiki message.</summary>
/// <param name="title">Title of the message.</param>
/// <returns>Returns raw text of the message.
/// If the message doesn't exist, exception is thrown.</returns>
public string GetMediaWikiMessage(string title)
{
if (string.IsNullOrEmpty(title))
throw new ArgumentNullException("title");
title = namespaces["8"].ToString() + ":" + Bot.Capitalize(RemoveNSPrefix(title, 8));
if (messages == null)
messages = new PageList(this);
else if (messages.Contains(title))
return messages[title].text;
string src;
try {
src = GetPageHTM(site + indexPath + "index.php?title=" +
HttpUtility.UrlEncode(title.Replace(" ", "_")) +
"&action=raw&usemsgcache=1&dontcountme=s");
}
catch (WebException e) {
if (e.Message.Contains(": (404) "))
throw new WikiBotException(
string.Format(Bot.Msg("MediaWiki message \"{0}\" was not found."), title));
else
throw;
}
if (string.IsNullOrEmpty(src)) {
throw new WikiBotException(
string.Format(Bot.Msg("MediaWiki message \"{0}\" was not found."), title));
}
messages.Add(new Page(this, title));
messages[messages.Count()-1].text = src;
return src;
}
/// <summary>Gets all modified MediaWiki messages (to be more precise, all messages that are
/// contained in database), loads them into site.messages PageList (both titles and texts)
/// and dumps them to XML file.</summary>
/// <param name="forceLoad">If true, the messages are updated unconditionally. Otherwise
/// the messages are updated only if they are outdated.</param>
public void GetModifiedMediaWikiMessages(bool forceLoad)
{
if (messages == null)
messages = new PageList(this);
string filePathName = "Cache" + Path.DirectorySeparatorChar +
HttpUtility.UrlEncode(site.Replace("://", ".")) + ".mw_db_msg.xml";
if (forceLoad == false && File.Exists(filePathName) &&
(DateTime.Now - File.GetLastWriteTime(filePathName)).Days <= 90) {
messages.FillAndLoadFromXMLDump(filePathName);
return;
}
Console.WriteLine(Bot.Msg("Updating MediaWiki messages dump. Please, wait..."));
PageList pl = new PageList(this);
bool prevBotQueryState = botQuery;
botQuery = false; // backward compatibility requirement
pl.FillFromAllPages("!", 8, false, 100000);
botQuery = prevBotQueryState;
File.Delete(filePathName);
pl.SaveXMLDumpToFile(filePathName);
messages.FillAndLoadFromXMLDump(filePathName);
Console.WriteLine(Bot.Msg("MediaWiki messages dump updated successfully."));
}
/// <summary>Gets all MediaWiki messages from "Special:Allmessages" page and loads them into
/// site.messages PageList. The function is not backward compatible.</summary>
public void GetMediaWikiMessages()
{
if (messages == null)
messages = new PageList(this);
Console.WriteLine(Bot.Msg("Updating MediaWiki messages dump. Please, wait..."));
string res = site + indexPath + "index.php?title=Special:Allmessages";
string src = "";
Page p = null;
Regex nextPortionRE = new Regex("offset=([^\"]+)\" title=\"[^\"]+\" rel=\"next\"");
do {
src = GetPageHTM(res + (src != ""
? "&offset=" + HttpUtility.HtmlDecode(nextPortionRE.Match(src).Groups[1].Value)
: "&limit=5000"));
using (XmlReader reader = GetXMLReader(src)) {
reader.ReadToFollowing("tbody");
while (reader.Read()) {
if (reader.Name == "tr" && reader.NodeType == XmlNodeType.Element &&
reader["id"] != null)
p = new Page(this, namespaces["8"].ToString() + ":" +
Bot.Capitalize(reader["id"].Replace("msg_", "")));
else if (reader.Name == "td" &&
(reader["class"] == "am_default" || reader["class"] == "am_actual"))
p.text = reader.ReadString();
else if (reader.Name == "tr" && reader.NodeType == XmlNodeType.EndElement)
messages.Add(p);
else if (reader.Name == "tbody" &&
reader.NodeType == XmlNodeType.EndElement)
break;
}
}
} while (nextPortionRE.IsMatch(src));
if (p != null)
messages.Add(p);
Console.WriteLine(Bot.Msg("MediaWiki messages dump updated successfully."));
}
/// <summary>Retrieves metadata and local namespace names from site.</summary>
public void GetInfo()
{
try {
langCulture = new CultureInfo(language, false);
}
catch (Exception) {
langCulture = new CultureInfo("");
}
if (langCulture.Equals(CultureInfo.CurrentUICulture.Parent))
regCulture = CultureInfo.CurrentUICulture;
else {
try {
regCulture = CultureInfo.CreateSpecificCulture(language);
}
catch (Exception) {
foreach (CultureInfo ci in
CultureInfo.GetCultures(CultureTypes.SpecificCultures)) {
if (langCulture.Equals(ci.Parent)) {
regCulture = ci;
break;
}
}
if (regCulture == null)
regCulture = CultureInfo.InvariantCulture;
}
}
string src = GetPageHTM(site + indexPath + "index.php?title=Special:Export/" +
DateTime.Now.Ticks.ToString("x"));
XmlTextReader reader = new XmlTextReader(new StringReader(src));
reader.WhitespaceHandling = WhitespaceHandling.None;
reader.ReadToFollowing("sitename");
name = reader.ReadString();
reader.ReadToFollowing("generator");
generator = reader.ReadString();
ver = new Version(Regex.Replace(generator, @"[^\d\.]", ""));
float.TryParse(ver.ToString(), NumberStyles.AllowDecimalPoint,
new CultureInfo("en-US"), out version);
reader.ReadToFollowing("case");
capitalization = reader.ReadString();
namespaces.Clear();
while (reader.ReadToFollowing("namespace"))
namespaces.Add(reader.GetAttribute("key"),
HttpUtility.HtmlDecode(reader.ReadString()));
reader.Close();
namespaces.Remove("0");
foreach (DictionaryEntry ns in namespaces) {
if (!wikiNSpaces.ContainsKey(ns.Key) ||
ns.Key.ToString() == "4" || ns.Key.ToString() == "5")
wikiNSpaces[ns.Key] = ns.Value;
}
if (ver >= new Version(1,14)) {
wikiNSpaces["6"] = "File";
wikiNSpaces["7"] = "File talk";
}
wikiCategoryRE = new Regex(@"\[\[(?i)(((" + Regex.Escape(wikiNSpaces["14"].ToString()) +
"|" + Regex.Escape(namespaces["14"].ToString()) + @"):(.+?))(\|(.+?))?)]]");
wikiImageRE = new Regex(@"\[\[(?i)((File|Image" +
"|" + Regex.Escape(namespaces["6"].ToString()) + @"):(.+?))(\|(.+?))*?]]");
string namespacesStr = "";
foreach (DictionaryEntry ns in namespaces)
namespacesStr += Regex.Escape(ns.Value.ToString()) + "|";
namespacesStr = namespacesStr.Replace("||", "|").Trim("|".ToCharArray());
linkToPageRE3 = new Regex("<a href=\"[^\"]*?\" title=\"(" +
Regex.Escape(namespaces["6"].ToString()) + ":[^\"]+?)\">");
string redirectTag = "REDIRECT";
switch(language) { // Revised 2010-07-02 (MediaWiki 1.15.4)
case "af": redirectTag += "|aanstuur"; break;
case "ar": redirectTag += "|تحويل"; break;
case "arz": redirectTag += "|تحويل|تحويل#"; break;
case "be": redirectTag += "|перанакіраваньне"; break;
case "be-x-old": redirectTag += "|перанакіраваньне"; break;
case "bg": redirectTag += "|пренасочване|виж"; break;
case "br": redirectTag += "|adkas"; break;
case "bs": redirectTag += "|preusmjeri"; break;
case "cs": redirectTag += "|přesměruj"; break;
case "cu": redirectTag += "|прѣнаправлєниѥ"; break;
case "cy": redirectTag += "|ail-cyfeirio|ailgyfeirio"; break;
case "de": redirectTag += "|weiterleitung"; break;
case "el": redirectTag += "|ανακατευθυνση"; break;
case "eo": redirectTag += "|alidirektu"; break;
case "es": redirectTag += "|redireccíon"; break;
case "et": redirectTag += "|suuna"; break;
case "eu": redirectTag += "|birzuzendu"; break;
case "fa": redirectTag += "|تغییرمسیر"; break;
case "fi": redirectTag += "|uudelleenohjaus|ohjaus"; break;
case "fr": redirectTag += "|redirection"; break;
case "ga": redirectTag += "|athsheoladh"; break;
case "gl": redirectTag += "|redirección"; break;
case "he": redirectTag += "|הפניה"; break;
case "hr": redirectTag += "|preusmjeri"; break;
case "hu": redirectTag += "|átirányítás"; break;
case "hy": redirectTag += "|վերահղում"; break;
case "id": redirectTag += "|alih"; break;
case "is": redirectTag += "|tilvísun"; break;
case "it": redirectTag += "|redirezione"; break;
case "ja": redirectTag += "|転送|リダイレクト|転送|リダイレクト"; break;
case "ka": redirectTag += "|გადამისამართება"; break;
case "kk": redirectTag += "|ايداۋ|айдау|aýdaw"; break;
case "km": redirectTag += "|បញ្ជូនបន្ត|ប្ដូរទីតាំងទៅ #ប្តូរទីតាំងទៅ"
+ "|ប្ដូរទីតាំង|ប្តូរទីតាំង|ប្ដូរចំណងជើង"; break;
case "ko": redirectTag += "|넘겨주기"; break;
case "ksh": redirectTag += "|ömleidung"; break;
case "lt": redirectTag += "|peradresavimas"; break;
case "mk": redirectTag += "|пренасочување|види"; break;
case "ml": redirectTag += "|തിരിച്ചുവിടുക" +
"|തിരിച്ചുവിടല്‍"; break;
case "mr": redirectTag += "|पुनर्निर्देशन"; break;
case "mt": redirectTag += "|rindirizza"; break;
case "mwl": redirectTag += "|ancaminar"; break;
case "nds": redirectTag += "|wiederleiden"; break;
case "nds-nl": redirectTag += "|deurverwiezing|doorverwijzing"; break;
case "nl": redirectTag += "|doorverwijzing"; break;
case "nn": redirectTag += "|omdiriger"; break;
case "oc": redirectTag += "|redireccion"; break;
case "pl": redirectTag += "|patrz|przekieruj|tam"; break;
case "pt": redirectTag += "|redirecionamento"; break;
case "ro": redirectTag += "|redirecteaza"; break;
case "ru": redirectTag += "|перенаправление|перенапр"; break;
case "sa": redirectTag += "|पुनर्निदेशन"; break;
case "sd": redirectTag += "|چوريو"; break;
case "si": redirectTag += "|а¶єа·…а·’а¶єа·ња¶ёа·”а·Ђ"; break;
case "sk": redirectTag += "|presmeruj"; break;
case "sl": redirectTag += "|preusmeritev"; break;
case "sq": redirectTag += "|ridrejto"; break;
case "sr": redirectTag += "|преусмери|preusmeri"; break;
case "srn": redirectTag += "|doorverwijzing"; break;
case "sv": redirectTag += "|omdirigering"; break;
case "ta": redirectTag += "|வழிமாற்று"; break;
case "te": redirectTag += "|а°¦а°ѕа°°а°їа°®а°ѕа°°а±Ќа°Єа±Ѓ"; break;
case "tr": redirectTag += "|yönlendİrme"; break;
case "tt": redirectTag += "перенаправление|перенапр|yünältü"; break;
case "uk": redirectTag += "|перенаправлення|перенаправление|перенапр"; break;
case "vi": redirectTag += "|đổi|đổi"; break;
case "vro": redirectTag += "|saadaq|suuna"; break;
case "yi": redirectTag += "|ווייטערפירן|#הפניה"; break;
default: redirectTag = "REDIRECT"; break;
}
redirectRE = new Regex(@"(?i)^#(?:" + redirectTag + @")\s*:?\s*\[\[(.+?)(\|.+)?]]",
RegexOptions.Compiled);
Console.WriteLine(Bot.Msg("Site: {0} ({1})"), name, generator);
string botQueryUriStr = site + indexPath + "api.php?version";
string respStr;
try {
respStr = GetPageHTM(botQueryUriStr);
if (respStr.Contains("<title>MediaWiki API</title>")) {
botQuery = true;
Regex botQueryVersionsRE = new Regex(@"(?i)<b><i>\$" +
@"Id: (\S+) (\d+) (.+?) \$</i></b>");
foreach (Match m in botQueryVersionsRE.Matches(respStr))
botQueryVersions[m.Groups[1].ToString()] = m.Groups[2].ToString();
}
}
catch (WebException) {
botQuery = false;
}
if (botQuery == false || !botQueryVersions.ContainsKey("ApiQueryCategoryMembers.php")) {
botQueryUriStr = site + indexPath + "query.php";
try {
respStr = GetPageHTM(botQueryUriStr);
if (respStr.Contains("<title>MediaWiki Query Interface</title>")) {
botQuery = true;
botQueryVersions["query.php"] = "Unknown";
}
}
catch (WebException) {
return;
}
}
}
/// <summary>Loads default English namespace names for site.</summary>
public void LoadDefaults()
{
if (wikiNSpaces.Count != 0 && WMSites.Count != 0)
return;
string[] wikiNSNames = { "Media", "Special", "", "Talk", "User", "User talk", name,
name + " talk", "Image", "Image talk", "MediaWiki", "MediaWiki talk", "Template",
"Template talk", "Help", "Help talk", "Category", "Category talk" };
for (int i=-2, j=0; i < 16; i++, j++)
wikiNSpaces.Add(i.ToString(), wikiNSNames[j]);
wikiNSpaces.Remove("0");
WMSites.Add("w", "wikipedia"); WMSites.Add("wikt", "wiktionary");
WMSites.Add("b", "wikibooks"); WMSites.Add("n", "wikinews");
WMSites.Add("q", "wikiquote"); WMSites.Add("s", "wikisource");
foreach (DictionaryEntry s in WMSites)
WMSitesStr += s.Key + "|" + s.Value + "|";
// Revised 2010-07-02
mediaWikiVars = new string[] { "currentmonth","currentmonthname","currentmonthnamegen",
"currentmonthabbrev","currentday2","currentdayname","currentyear","currenttime",
"currenthour","localmonth","localmonthname","localmonthnamegen","localmonthabbrev",
"localday","localday2","localdayname","localyear","localtime","localhour",
"numberofarticles","numberoffiles","sitename","server","servername","scriptpath",
"pagename","pagenamee","fullpagename","fullpagenamee","namespace","namespacee",
"currentweek","currentdow","localweek","localdow","revisionid","revisionday",
"revisionday2","revisionmonth","revisionyear","revisiontimestamp","subpagename",
"subpagenamee","talkspace","talkspacee","subjectspace","dirmark","directionmark",
"subjectspacee","talkpagename","talkpagenamee","subjectpagename","subjectpagenamee",
"numberofusers","rawsuffix","newsectionlink","numberofpages","currentversion",
"basepagename","basepagenamee","urlencode","currenttimestamp","localtimestamp",
"directionmark","language","contentlanguage","pagesinnamespace","numberofadmins",
"currentday","numberofarticles:r","numberofpages:r","magicnumber",
"numberoffiles:r", "numberofusers:r", "numberofadmins:r", "numberofactiveusers",
"numberofactiveusers:r" };
parserFunctions = new string[] { "ns:", "localurl:", "localurle:", "urlencode:",
"anchorencode:", "fullurl:", "fullurle:", "grammar:", "plural:", "lc:", "lcfirst:",
"uc:", "ucfirst:", "formatnum:", "padleft:", "padright:", "#language:",
"displaytitle:", "defaultsort:", "#if:", "#if:", "#switch:", "#ifexpr:",
"numberingroup:", "pagesinns:", "pagesincat:", "pagesincategory:", "pagesize:",
"gender:", "filepath:", "#special:", "#tag:" };
templateModifiers = new string[] { ":", "int:", "msg:", "msgnw:", "raw:", "subst:" };
// Revised 2010-07-02
iwikiLinksOrderByLocalFW = new string[] {
"ace", "af", "ak", "als", "am", "ang", "ab", "ar", "an", "arc",
"roa-rup", "frp", "as", "ast", "gn", "av", "ay", "az", "id", "ms",
"bm", "bn", "zh-min-nan", "nan", "map-bms", "jv", "su", "ba", "be",
"be-x-old", "bh", "bcl", "bi", "bar", "bo", "bs", "br", "bug", "bg",
"bxr", "ca", "ceb", "cv", "cs", "ch", "cbk-zam", "ny", "sn", "tum",
"cho", "co", "cy", "da", "dk", "pdc", "de", "dv", "nv", "dsb", "na",
"dz", "mh", "et", "el", "eml", "en", "myv", "es", "eo", "ext", "eu",
"ee", "fa", "hif", "fo", "fr", "fy", "ff", "fur", "ga", "gv", "sm",
"gd", "gl", "gan", "ki", "glk", "gu", "got", "hak", "xal", "ko",
"ha", "haw", "hy", "hi", "ho", "hsb", "hr", "io", "ig", "ilo",
"bpy", "ia", "ie", "iu", "ik", "os", "xh", "zu", "is", "it", "he",
"kl", "kn", "kr", "pam", "ka", "ks", "csb", "kk", "kw", "rw", "ky",
"rn", "sw", "kv", "kg", "ht", "ku", "kj", "lad", "lbe", "lo", "la",
"lv", "to", "lb", "lt", "lij", "li", "ln", "jbo", "lg", "lmo", "hu",
"mk", "mg", "ml", "krc", "mt", "mi", "mr", "arz", "mzn", "cdo",
"mwl", "mdf", "mo", "mn", "mus", "my", "nah", "fj", "nl", "nds-nl",
"cr", "ne", "new", "ja", "nap", "ce", "pih", "no", "nb", "nn",
"nrm", "nov", "ii", "oc", "mhr", "or", "om", "ng", "hz", "uz", "pa",
"pi", "pag", "pnb", "pap", "ps", "km", "pcd", "pms", "nds", "pl",
"pnt", "pt", "aa", "kaa", "crh", "ty", "ksh", "ro", "rmy", "rm",
"qu", "ru", "sah", "se", "sa", "sg", "sc", "sco", "stq", "st", "tn",
"sq", "scn", "si", "simple", "sd", "ss", "sk", "sl", "cu", "szl",
"so", "ckb", "srn", "sr", "sh", "fi", "sv", "tl", "ta", "kab",
"roa-tara", "tt", "te", "tet", "th", "vi", "ti", "tg", "tpi",
"tokipona", "tp", "chr", "chy", "ve", "tr", "tk", "tw", "udm", "uk",
"ur", "ug", "za", "vec", "vo", "fiu-vro", "wa", "zh-classical",
"vls", "war", "wo", "wuu", "ts", "yi", "yo", "zh-yue", "diq", "zea",
"bat-smg", "zh", "zh-tw", "zh-cn"
};
iwikiLinksOrderByLocal = new string[] {
"ace", "af", "ak", "als", "am", "ang", "ab", "ar", "an", "arc",
"roa-rup", "frp", "as", "ast", "gn", "av", "ay", "az", "bm", "bn",
"zh-min-nan", "nan", "map-bms", "ba", "be", "be-x-old", "bh", "bcl",
"bi", "bar", "bo", "bs", "br", "bg", "bxr", "ca", "cv", "ceb", "cs",
"ch", "cbk-zam", "ny", "sn", "tum", "cho", "co", "cy", "da", "dk",
"pdc", "de", "dv", "nv", "dsb", "dz", "mh", "et", "el", "eml", "en",
"myv", "es", "eo", "ext", "eu", "ee", "fa", "hif", "fo", "fr", "fy",
"ff", "fur", "ga", "gv", "gd", "gl", "gan", "ki", "glk", "gu",
"got", "hak", "xal", "ko", "ha", "haw", "hy", "hi", "ho", "hsb",
"hr", "io", "ig", "ilo", "bpy", "id", "ia", "ie", "iu", "ik", "os",
"xh", "zu", "is", "it", "he", "jv", "kl", "kn", "kr", "pam", "krc",
"ka", "ks", "csb", "kk", "kw", "rw", "ky", "rn", "sw", "kv", "kg",
"ht", "ku", "kj", "lad", "lbe", "lo", "la", "lv", "lb", "lt", "lij",
"li", "ln", "jbo", "lg", "lmo", "hu", "mk", "mg", "ml", "mt", "mi",
"mr", "arz", "mzn", "ms", "cdo", "mwl", "mdf", "mo", "mn", "mus",
"my", "nah", "na", "fj", "nl", "nds-nl", "cr", "ne", "new", "ja",
"nap", "ce", "pih", "no", "nb", "nn", "nrm", "nov", "ii", "oc",
"mhr", "or", "om", "ng", "hz", "uz", "pa", "pi", "pag", "pnb",
"pap", "ps", "km", "pcd", "pms", "tpi", "nds", "pl", "tokipona",
"tp", "pnt", "pt", "aa", "kaa", "crh", "ty", "ksh", "ro", "rmy",
"rm", "qu", "ru", "sah", "se", "sm", "sa", "sg", "sc", "sco", "stq",
"st", "tn", "sq", "scn", "si", "simple", "sd", "ss", "sk", "cu",
"sl", "szl", "so", "ckb", "srn", "sr", "sh", "su", "fi", "sv", "tl",
"ta", "kab", "roa-tara", "tt", "te", "tet", "th", "ti", "tg", "to",
"chr", "chy", "ve", "tr", "tk", "tw", "udm", "bug", "uk", "ur",
"ug", "za", "vec", "vi", "vo", "fiu-vro", "wa", "zh-classical",
"vls", "war", "wo", "wuu", "ts", "yi", "yo", "zh-yue", "diq", "zea",
"bat-smg", "zh", "zh-tw", "zh-cn"
};
iwikiLinksOrderByLatinFW = new string[] {
"ace", "af", "ak", "als", "am", "ang", "ab", "ar", "an", "arc",
"roa-rup", "frp", "arz", "as", "ast", "gn", "av", "ay", "az", "id",
"ms", "bg", "bm", "zh-min-nan", "nan", "map-bms", "jv", "su", "ba",
"be", "be-x-old", "bh", "bcl", "bi", "bn", "bo", "bar", "bs", "bpy",
"br", "bug", "bxr", "ca", "ceb", "ch", "cbk-zam", "sn", "tum", "ny",
"cho", "chr", "co", "cy", "cv", "cs", "da", "dk", "pdc", "de", "nv",
"dsb", "na", "dv", "dz", "mh", "et", "el", "eml", "en", "myv", "es",
"eo", "ext", "eu", "ee", "fa", "hif", "fo", "fr", "fy", "ff", "fur",
"ga", "gv", "sm", "gd", "gl", "gan", "ki", "glk", "got", "gu", "ha",
"hak", "xal", "haw", "he", "hi", "ho", "hsb", "hr", "hy", "io",
"ig", "ii", "ilo", "ia", "ie", "iu", "ik", "os", "xh", "zu", "is",
"it", "ja", "ka", "kl", "kr", "pam", "krc", "csb", "kk", "kw", "rw",
"ky", "rn", "sw", "km", "kn", "ko", "kv", "kg", "ht", "ks", "ku",
"kj", "lad", "lbe", "la", "lv", "to", "lb", "lt", "lij", "li", "ln",
"lo", "jbo", "lg", "lmo", "hu", "mk", "mg", "mt", "mi", "cdo",
"mwl", "ml", "mdf", "mo", "mn", "mr", "mus", "my", "mzn", "nah",
"fj", "ne", "nl", "nds-nl", "cr", "new", "nap", "ce", "pih", "no",
"nb", "nn", "nrm", "nov", "oc", "mhr", "or", "om", "ng", "hz", "uz",
"pa", "pag", "pap", "pi", "pcd", "pms", "nds", "pnb", "pl", "pt",
"pnt", "ps", "aa", "kaa", "crh", "ty", "ksh", "ro", "rmy", "rm",
"qu", "ru", "sa", "sah", "se", "sg", "sc", "sco", "sd", "stq", "st",
"tn", "sq", "si", "scn", "simple", "ss", "sk", "sl", "cu", "szl",
"so", "ckb", "srn", "sr", "sh", "fi", "sv", "ta", "tl", "kab",
"roa-tara", "tt", "te", "tet", "th", "ti", "vi", "tg", "tokipona",
"tp", "tpi", "chy", "ve", "tr", "tk", "tw", "udm", "uk", "ur", "ug",
"za", "vec", "vo", "fiu-vro", "wa", "vls", "war", "wo", "wuu", "ts",
"yi", "yo", "diq", "zea", "zh", "zh-tw", "zh-cn", "zh-classical",
"zh-yue", "bat-smg"
};
botQueryLists.Add("allpages", "ap"); botQueryLists.Add("alllinks", "al");
botQueryLists.Add("allusers", "au"); botQueryLists.Add("backlinks", "bl");
botQueryLists.Add("categorymembers", "cm"); botQueryLists.Add("embeddedin", "ei");
botQueryLists.Add("imageusage", "iu"); botQueryLists.Add("logevents", "le");
botQueryLists.Add("recentchanges", "rc"); botQueryLists.Add("usercontribs", "uc");
botQueryLists.Add("watchlist", "wl"); botQueryLists.Add("exturlusage", "eu");
botQueryProps.Add("info", "in"); botQueryProps.Add("revisions", "rv");
botQueryProps.Add("links", "pl"); botQueryProps.Add("langlinks", "ll");
botQueryProps.Add("images", "im"); botQueryProps.Add("imageinfo", "ii");
botQueryProps.Add("templates", "tl"); botQueryProps.Add("categories", "cl");
botQueryProps.Add("extlinks", "el"); botQueryLists.Add("search", "sr");
}
/// <summary>Logs in and retrieves cookies.</summary>
public void LogIn()
{
string loginPageSrc = GetPageHTM(site + indexPath +
"index.php?title=Special:Userlogin");
string loginToken = "";
int loginTokenPos = loginPageSrc.IndexOf(
"<input type=\"hidden\" name=\"wpLoginToken\" value=\"");
if( loginTokenPos != -1)
loginToken = loginPageSrc.Substring(loginTokenPos + 48, 32);
HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(site + indexPath +
"index.php?title=Special:Userlogin&action=submitlogin&type=login");
string postData = string.Format("wpName={0}&wpPassword={1}&wpDomain={2}" +
"&wpLoginToken={3}&wpRemember=1&wpLoginattempt=Log+in",
HttpUtility.UrlEncode(userName), HttpUtility.UrlEncode(userPass),
HttpUtility.UrlEncode(userDomain), HttpUtility.UrlEncode(loginToken));
webReq.Method = "POST";
webReq.ContentType = Bot.webContentType;
webReq.UserAgent = Bot.botVer;
webReq.Proxy.Credentials = CredentialCache.DefaultCredentials;
webReq.UseDefaultCredentials = true;
webReq.CookieContainer = cookies;
webReq.AllowAutoRedirect = false;
if (Bot.unsafeHttpHeaderParsingUsed == 0) {
webReq.ProtocolVersion = HttpVersion.Version10;
webReq.KeepAlive = false;
}
byte[] postBytes = encoding.GetBytes(postData);
webReq.ContentLength = postBytes.Length;
Stream reqStrm = webReq.GetRequestStream();
reqStrm.Write(postBytes, 0, postBytes.Length);
reqStrm.Close();
HttpWebResponse webResp = null;
for (int errorCounter = 0; true; errorCounter++) {
try {
webResp = (HttpWebResponse)webReq.GetResponse();
break;
}
catch (WebException e) {
string message = e.Message;
if (Regex.IsMatch(message, ": \\(50[02349]\\) ")) { // Remote problem
if (errorCounter > Bot.retryTimes)
throw;
Console.Error.WriteLine(message + " " + Bot.Msg("Retrying in 60 seconds."));
Thread.Sleep(60000);
}
else if (message.Contains("Section=ResponseStatusLine")) { // Squid problem
Bot.SwitchUnsafeHttpHeaderParsing(true);
LogIn();
return;
}
else
throw;
}
}
foreach (Cookie cookie in webResp.Cookies)
cookies.Add(cookie);
StreamReader strmReader = new StreamReader(webResp.GetResponseStream());
string respStr = strmReader.ReadToEnd();
if (respStr.Contains("<div class=\"errorbox\">"))
throw new WikiBotException(
"\n\n" + Bot.Msg("Login failed. Check your username and password.") + "\n");
strmReader.Close();
webResp.Close();
Console.WriteLine(Bot.Msg("Logged in as {0}."), userName);
}
/// <summary>Logs in SourceForge.net and retrieves cookies for work with
/// SourceForge-hosted wikis. That's a special version of LogIn() function.</summary>
public void LogInSourceForge()
{
HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(
"https://sourceforge.net/account/login.php");
string postData = string.Format("form_loginname={0}&form_pw={1}" +
"&ssl_status=&form_rememberme=yes&login=Log+in",
HttpUtility.UrlEncode(userName.ToLower()), HttpUtility.UrlEncode(userPass));
webReq.Method = "POST";
webReq.ContentType = Bot.webContentType;
webReq.UserAgent = Bot.botVer;
webReq.Proxy.Credentials = CredentialCache.DefaultCredentials;
webReq.UseDefaultCredentials = true;
webReq.CookieContainer = new CookieContainer();
webReq.AllowAutoRedirect = false;
if (Bot.unsafeHttpHeaderParsingUsed == 0) {
webReq.ProtocolVersion = HttpVersion.Version10;
webReq.KeepAlive = false;
}
byte[] postBytes = encoding.GetBytes(postData);
webReq.ContentLength = postBytes.Length;
Stream reqStrm = webReq.GetRequestStream();
reqStrm.Write(postBytes, 0, postBytes.Length);
reqStrm.Close();
HttpWebResponse webResp = null;
for (int errorCounter = 0; true; errorCounter++) {
try {
webResp = (HttpWebResponse)webReq.GetResponse();
break;
}
catch (WebException e) {
string message = e.Message;
if (Regex.IsMatch(message, ": \\(50[02349]\\) ")) { // Remote problem
if (errorCounter > Bot.retryTimes)
throw;
Console.Error.WriteLine(message + " " + Bot.Msg("Retrying in 60 seconds."));
Thread.Sleep(60000);
}
else if (message.Contains("Section=ResponseStatusLine")) { // Squid problem
Bot.SwitchUnsafeHttpHeaderParsing(true);
LogIn();
return;
}
else
throw;
}
}
foreach (Cookie cookie in webResp.Cookies)
cookies.Add(cookie);
StreamReader strmReader = new StreamReader(webResp.GetResponseStream());
string respStr = strmReader.ReadToEnd();
if (respStr.Contains(" class=\"error\""))
throw new WikiBotException(
"\n\n" + Bot.Msg("Login failed. Check your username and password.") + "\n");
strmReader.Close();
webResp.Close();
Console.WriteLine(Bot.Msg("Logged in SourceForge as {0}."), userName);
}
/// <summary>Gets the list of Wikimedia Foundation wiki sites and ISO 639-1
/// language codes, used as prefixes.</summary>
public void GetWikimediaWikisList()
{
Uri wikimediaMeta = new Uri("http://meta.wikimedia.org/wiki/Special:SiteMatrix");
string respStr = Bot.GetWebResource(wikimediaMeta, "");
Regex langCodeRE = new Regex("<a id=\"([^\"]+?)\"");
Regex siteCodeRE = new Regex("<li><a href=\"[^\"]+?\">([^\\s]+?)<");
MatchCollection langMatches = langCodeRE.Matches(respStr);
MatchCollection siteMatches = siteCodeRE.Matches(respStr);
foreach(Match m in langMatches)
WMLangsStr += Regex.Escape(HttpUtility.HtmlDecode(m.Groups[1].ToString())) + "|";
WMLangsStr = WMLangsStr.Remove(WMLangsStr.Length - 1);
foreach(Match m in siteMatches)
WMSitesStr += Regex.Escape(HttpUtility.HtmlDecode(m.Groups[1].ToString())) + "|";
WMSitesStr += "m";
Site.iwikiLinkRE = new Regex(@"(?i)\[\[((" + WMLangsStr + "):(.+?))]]\r?\n?");
Site.iwikiDispLinkRE = new Regex(@"(?i)\[\[:((" + WMLangsStr + "):(.+?))]]");
Site.sisterWikiLinkRE = new Regex(@"(?i)\[\[((" + WMSitesStr + "):(.+?))]]");
}
/// <summary>This internal function gets the hypertext markup (HTM) of wiki-page.</summary>
/// <param name="pageURL">Absolute or relative URL of page to get.</param>
/// <returns>Returns HTM source code.</returns>
public string GetPageHTM(string pageURL)
{
return PostDataAndGetResultHTM(pageURL, "");
}
/// <summary>This internal function posts specified string to requested resource
/// and gets the result hypertext markup (HTM).</summary>
/// <param name="pageURL">Absolute or relative URL of page to get.</param>
/// <param name="postData">String to post to site with web request.</param>
/// <returns>Returns code of hypertext markup (HTM).</returns>
public string PostDataAndGetResultHTM(string pageURL, string postData)
{
if (string.IsNullOrEmpty(pageURL))
throw new WikiBotException(Bot.Msg("No URL specified."));
if (!pageURL.StartsWith(site))
pageURL = site + pageURL;
HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(pageURL);
webReq.Proxy.Credentials = CredentialCache.DefaultCredentials;
webReq.UseDefaultCredentials = true;
webReq.ContentType = Bot.webContentType;
webReq.UserAgent = Bot.botVer;
if (cookies.Count == 0)
webReq.CookieContainer = new CookieContainer();
else
webReq.CookieContainer = cookies;
if (Bot.unsafeHttpHeaderParsingUsed == 0) {
webReq.ProtocolVersion = HttpVersion.Version10;
webReq.KeepAlive = false;
}
webReq.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");
if (!string.IsNullOrEmpty(postData)) {
if (Bot.isRunningOnMono) // Mono bug 636219 evasion
webReq.AllowAutoRedirect = false;
// https://bugzilla.novell.com/show_bug.cgi?id=636219
webReq.Method = "POST";
//webReq.Timeout = 180000;
byte[] postBytes = Encoding.UTF8.GetBytes(postData);
webReq.ContentLength = postBytes.Length;
Stream reqStrm = webReq.GetRequestStream();
reqStrm.Write(postBytes, 0, postBytes.Length);
reqStrm.Close();
}
HttpWebResponse webResp = null;
for (int errorCounter = 0; true; errorCounter++) {
try {
webResp = (HttpWebResponse)webReq.GetResponse();
break;
}
catch (WebException e) {
string message = e.Message;
if (webReq.AllowAutoRedirect == false &&
webResp.StatusCode == HttpStatusCode.Redirect) // Mono bug 636219 evasion
return "";
if (Regex.IsMatch(message, ": \\(50[02349]\\) ")) { // Remote problem
if (errorCounter > Bot.retryTimes)
throw;
Console.Error.WriteLine(message + " " + Bot.Msg("Retrying in 60 seconds."));
Thread.Sleep(60000);
}
else if (message.Contains("Section=ResponseStatusLine")) { // Squid problem
Bot.SwitchUnsafeHttpHeaderParsing(true);
//Console.Write("|");
return PostDataAndGetResultHTM(pageURL, postData);
}
else
throw;
}
}
Stream respStream = webResp.GetResponseStream();
if (webResp.ContentEncoding.ToLower().Contains("gzip"))
respStream = new GZipStream(respStream, CompressionMode.Decompress);
else if (webResp.ContentEncoding.ToLower().Contains("deflate"))
respStream = new DeflateStream(respStream, CompressionMode.Decompress);
if (cookies.Count == 0) {
foreach (Cookie cookie in webResp.Cookies) {
cookie.Domain = cookie.Domain.TrimStart(new char[] {'.'});
cookies.Add(cookie);
}
}
StreamReader strmReader = new StreamReader(respStream, encoding);