-
Notifications
You must be signed in to change notification settings - Fork 1
/
FormMain.cs
984 lines (873 loc) · 40.3 KB
/
FormMain.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Web;
using System.Windows.Forms;
using Microsoft.Win32;
using DotNetWikiBot;
namespace Persondata_o_matic
{
public partial class FormMain : Form
{
Site site;
Thread loadAheadThread, savePendingThread;
string currentPageText;
string currentTemplate;
int currentTemplateIndex;
PageListSource pageListSource;
string pageListSourceValue;
string origPageText;
string origName = "", origAlternativeNames = "", origShortDescription = "", origDateOfBirth = "", origPlaceOfBirth = "", origDateOfDeath = "", origPlaceOfDeath = "";
bool manualEditSummary;
PageList pageList;
// Lock covers both lastLoadedPageIndex and lastSavedPageIndex
object lastLoadedPageIndexLock = new object();
int lastLoadedPageIndex = -1;
int lastSavedPageIndex = -1;
int numLoadAheadPages;
int numCategoryPagesToRetrieve;
bool randomizePageOrder;
bool loadPageListOnDemand;
Queue<SaveInfo> saveQueue = new Queue<SaveInfo>();
private static string GetRegexForField(string field)
{
// make sure to look ahead for "|" or "}" since lookup is non-greedy and would otherwise halt immediatelly
// | wikilink | | template | \|/
return field + @"\s*=((?:(?:\[\[[^\]]*\]\])*(?:\{\{[^\}]*\}\})*[^\|}]*?)*)(?=[\|\}])";
}
Regex regexPersondataTemplate = new Regex(@"\{\{\s*persondata(?:(?:\{\{[^\}]*\}\})*[^}]*?)*\}\}", RegexOptions.IgnoreCase | RegexOptions.Multiline);
Regex regexName = new Regex(GetRegexForField("NAME"), RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.Compiled);
Regex regexAlternativeNames = new Regex(GetRegexForField("ALTERNATIVE NAMES"), RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.Compiled);
Regex regexShortDescription = new Regex(GetRegexForField("SHORT DESCRIPTION"), RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.Compiled);
Regex regexPersondataDateOfBirth = new Regex(GetRegexForField("DATE OF BIRTH"), RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.Compiled);
Regex regexPlaceOfBirth = new Regex(GetRegexForField("PLACE OF BIRTH"), RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.Compiled);
Regex regexDateOfDeath = new Regex(GetRegexForField("DATE OF DEATH"), RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.Compiled);
Regex regexPlaceOfDeath = new Regex(GetRegexForField("PLACE OF DEATH"), RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.Compiled);
Regex regexInfoboxTemplate = new Regex(@"\{\{\s*[a-zA-Z0-9 ]*infobox(?:(?:\{\{[^\}]*\}\})*[^}]*?)*\}\}", RegexOptions.IgnoreCase | RegexOptions.Multiline);
Regex regexInfoboxDateOfBirth = new Regex(GetRegexForField("birth_date"), RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.Compiled);
Regex regexInfoboxDateOfDeath = new Regex(GetRegexForField("death_date"), RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.Compiled);
Regex regexSortKey = new Regex(@"\[\[\s*Category\s*:\s*[^\|\]]*\|\s*([^\]]*)", RegexOptions.IgnoreCase | RegexOptions.Compiled);
public FormMain()
{
InitializeComponent();
}
private void FormMain_Load(object sender, EventArgs e)
{
Login();
}
private void Login()
{
this.Hide();
loginForm = new FormLogin();
if (loginForm.ShowDialog(this) != System.Windows.Forms.DialogResult.OK)
{
Application.Exit();
return;
}
loadingDialog = new LoadingDialog();
loadingDialog.Message = "Logging in...";
loadingDialog.Show(this);
this.Enabled = false;
Thread loginThread = new Thread(new ThreadStart(delegate()
{
try
{
errorMessage = null;
site = new Site("http://en.wikipedia.org", loginForm.Username, loginForm.Password);
}
catch (WikiBotException ex)
{
if (ex.Message.Contains("Login failed"))
{
errorMessage = "Login failed. Check your username and password and try again.";
}
else
{
errorMessage = "Encountered error: " + ex.Message;
}
}
this.Invoke(new MethodInvoker(CompleteLogin));
}));
loginThread.Start();
}
FormLogin loginForm;
string errorMessage;
LoadingDialog loadingDialog;
string pageListNext = ""; // Used when loadPageListOnDemand == true to stream page list
void CompleteLogin()
{
if (errorMessage != null)
{
MessageBox.Show(errorMessage);
loadingDialog.Hide();
Login();
return;
}
numLoadAheadPages = loginForm.MaxLoadAheadPages;
numCategoryPagesToRetrieve = loginForm.MaxCategoryPages;
randomizePageOrder = loginForm.RandomizeOrder;
loadPageListOnDemand = !randomizePageOrder;
pageList = new PageList(site);
pageListSource = loginForm.PageListSource;
pageListSourceValue = loginForm.PageListSourceValue;
if (loadPageListOnDemand)
{
// Actually just loading some of the list of pages, more will be loaded on-demand later
loadingDialog.Message = "Loading list of pages...";
}
else
{
loadingDialog.Message = "Loading list of pages (up to " + numCategoryPagesToRetrieve + ")...";
}
Thread fillFromCategoryThread = new Thread(new ThreadStart(delegate()
{
switch (pageListSource)
{
case PageListSource.category:
if (loadPageListOnDemand)
{
pageList.FillSomeFromCategoryEx(pageListSourceValue, ref pageListNext);
}
else
{
pageList.FillAllFromCategoryEx(pageListSourceValue, numCategoryPagesToRetrieve);
}
Invoke(new MethodInvoker(CompletePageListLoad));
break;
case PageListSource.file:
pageList.FillFromFile(pageListSourceValue);
Invoke(new MethodInvoker(CompletePageListLoad));
break;
default:
throw new ArgumentOutOfRangeException();
}
//pageList.FillFromFile("pages.txt"); // just for debugging to load up some pages
}));
fillFromCategoryThread.Start();
}
void CompletePageListLoad()
{
loadingDialog.Hide();
if (randomizePageOrder)
{
Random r = new Random();
ShufflePageList(r, pageList);
}
loadAheadThread = new Thread(new ThreadStart(LoadAhead));
loadAheadThread.Start();
savePendingThread = new Thread(new ThreadStart(SavePending));
savePendingThread.Start();
ShowNextPage();
}
private static void ShufflePageList(Random r, PageList list)
{
for (int i = list.Count() - 1; i >= 1; i--)
{
int j = r.Next(0, i + 1);
Page temp = list[j];
list[j] = list[i];
list[i] = temp;
}
}
void SavePending()
{
while (true)
{
SaveInfo saveInfo = null;
lock (saveQueue)
{
if (saveQueue.Count > 0)
{
saveInfo = saveQueue.Dequeue();
}
}
if (saveInfo != null)
{
try
{
saveInfo.Page.Save(saveInfo.EditSummary, /*isMinorEdit*/false);
}
catch (Exception e)
{
this.Invoke(new MethodInvoker(delegate()
{
MessageBox.Show("Could not save page " + saveInfo.Page.title + " due to error:\n" + e.Message);
}));
}
}
else
{
Thread.Sleep(100);
}
}
}
void ShowNextPage()
{
SetControlsEnabled(false);
webBrowser.Navigate("about:blank"); // clear the contents
int nextPage = lastSavedPageIndex + 1;
if (nextPage >= pageList.Count())
{
MessageBox.Show("No more pages.");
return;
}
bool pageAvailable;
lock (lastLoadedPageIndexLock)
{
pageAvailable = (lastLoadedPageIndex >= nextPage);
}
if (pageAvailable)
{
CompleteWaitForPage();
}
else
{
loadingDialog.Message = "Loading pages...";
loadingDialog.Show();
Thread waitForPageThread = new Thread(new ThreadStart(delegate()
{
while (true)
{
lock (lastLoadedPageIndexLock)
{
if (lastLoadedPageIndex >= nextPage)
{
break;
}
}
Thread.Sleep(100);
}
this.Invoke(new MethodInvoker(CompleteWaitForPage));
}));
waitForPageThread.Start();
}
}
private void CompleteWaitForPage()
{
SetControlsEnabled(true);
this.Enabled = true;
int nextPage = lastSavedPageIndex + 1;
Page page = pageList[nextPage];
UpdateControlsFromPage(page);
loadingDialog.Hide();
}
private void SetControlsEnabled(bool value)
{
buttonSaveAndNext.Enabled = value;
buttonSkip.Enabled = value;
buttonRemove.Enabled = value;
textBoxName.Enabled = value;
textBoxAlternativeNames.Enabled = value;
textBoxShortDescription.Enabled = value;
textBoxDateOfBirth.Enabled = value;
textBoxPlaceOfBirth.Enabled = value;
textBoxDateOfDeath.Enabled = value;
textBoxPlaceOfDeath.Enabled = value;
textBoxEditSummary.Enabled = value;
}
private void UpdateControlsFromPage(Page page)
{
textBoxWikitextOriginal.Text = Regex.Replace(page.text, @"(?!=\r)\n", "\r\n");
// Removing comment per consensus at [[Wikipedia_talk:Persondata#Can_we_stop_adding_the_annoying.2C_useless_comment_now.3F]]
currentPageText = page.text.Replace(" <!-- Metadata: see [[Wikipedia:Persondata]]. -->", "");
origPageText = page.text;
textBoxTitle.Text = page.title;
textBoxWikitext.Text = Regex.Replace(currentPageText, @"(?!=\r)\n", "\r\n");
if (tabControlPage.SelectedTab == tabPageBrowser)
{
NavigateWebBrowserToCurrentPage();
}
// Fill in the textboxes with values from the existing template
Match templateMatch = regexPersondataTemplate.Match(currentPageText);
currentTemplate = regexPersondataTemplate.Match(currentPageText).Value;
currentTemplateIndex = templateMatch.Index;
origName = regexName.Match(currentTemplate).Groups[1].Value.Trim(); // NOTE: set origXXX values before the textbox value, which would trigger recoloring, but it would screw up if origXXX wouldn't be set yet
textBoxName.Text = origName;
SetToolTipForFieldTextbox(textBoxName);
origAlternativeNames = regexAlternativeNames.Match(currentTemplate).Groups[1].Value.Trim();
textBoxAlternativeNames.Text = origAlternativeNames;
SetToolTipForFieldTextbox(textBoxAlternativeNames);
origShortDescription = regexShortDescription.Match(currentTemplate).Groups[1].Value.Trim();
textBoxShortDescription.Text = origShortDescription;
SetToolTipForFieldTextbox(textBoxShortDescription);
origDateOfBirth = regexPersondataDateOfBirth.Match(currentTemplate).Groups[1].Value.Trim();
textBoxDateOfBirth.Text = origDateOfBirth;
SetToolTipForFieldTextbox(textBoxDateOfBirth);
origPlaceOfBirth = regexPlaceOfBirth.Match(currentTemplate).Groups[1].Value.Trim();
textBoxPlaceOfBirth.Text = origPlaceOfBirth;
SetToolTipForFieldTextbox(textBoxPlaceOfBirth);
origDateOfDeath = regexDateOfDeath.Match(currentTemplate).Groups[1].Value.Trim();
textBoxDateOfDeath.Text = origDateOfDeath;
SetToolTipForFieldTextbox(textBoxDateOfDeath);
origPlaceOfDeath = regexPlaceOfDeath.Match(currentTemplate).Groups[1].Value.Trim();
textBoxPlaceOfDeath.Text = origPlaceOfDeath;
SetToolTipForFieldTextbox(textBoxPlaceOfDeath);
// Time to guess values
if (textBoxName.Text == "") textBoxName.Text = GuessName(page);
textBoxDateOfBirth.Text = GuessDate(textBoxDateOfBirth.Text, GuessDateType.birth);
textBoxDateOfDeath.Text = GuessDate(textBoxDateOfDeath.Text, GuessDateType.death);
manualEditSummary = false;
UpdateEditSummary();
string warningText = "";
if (regexPersondataTemplate.Matches(currentPageText).Count > 1)
{
warningText += "* Multiple {{persondata}} templates found!";
}
if (warningText != "")
{
labelWarnings.Text = "Warning:\n" + warningText;
labelWarnings.Show();
}
else
{
labelWarnings.Hide();
}
UpdateFocus();
}
/// <summary>
/// Add a tooltip for a given textbox based on its current value as formatted for field value textboxes
/// </summary>
private void SetToolTipForFieldTextbox(TextBox textBox)
{
toolTipForTextboxes.SetToolTip(textBox, textBox.Text != "" ? "Original value: " + "\"" + textBox.Text + "\"" : "No original value");
}
private void NavigateWebBrowserToCurrentPage()
{
// Don't try loading when no more pages left
if (pageList.Count() <= lastSavedPageIndex + 1)
return;
// Syntax: http://en.wikipedia.org/w/index.php?title=Elephant&action=render
string url = site.site + "/w/index.php?action=render&title=" + HttpUtility.UrlEncode(pageList[lastSavedPageIndex + 1].title);
if (webBrowser.Url == null || webBrowser.Url.ToString() != url)
webBrowser.Navigate(url);
}
private string GuessName(Page page)
{
Match sortKeyMatch = regexSortKey.Match(currentPageText);
if (sortKeyMatch.Success)
{
return sortKeyMatch.Groups[1].Value.Trim();
}
string[] titleWords = page.title.Split(new char[] { ' ' });
if (!page.title.Contains(":") && !page.title.Contains("("))
{
if (titleWords.Length == 2)
{
return titleWords[1] + ", " + titleWords[0];
}
else
{
return page.title;
}
}
return "";
}
private const string monthNames = @"(Jan(?:uary)?|Feb(?:ruary)?|Mar(c?:h)?|Apr(?:il)?|May|Jun[ey]?|Aug(?:ust)?|Sep(?:tember)?|Oct(?:ober)?|Nov(?:embr)?|Dec(?:ember)?)";
private enum GuessDateType { birth, death }
private const string birthDateTemplates = @"(?:Birth date|Birth year|Birthdate|Date of birth|Dob|Birth date and age|BDA|Birthdate and age|Birthdateandage)\s*\|\s*";
private const string deathDateTemplates = @"(?:Death date|Date of death|Date of disappearance with age|Dda|Deathdateandage|Event date and age)\s*\|\s*";
private string GuessDate(string currentValue, GuessDateType guessDateType)
{
// First, let's see if we can recognise the existing date
// Basically, we'd want to fill in more info if possible in case the original data is lacking,
// so we don't skip the field right away if it is non-empty
// This variable will signify how complex the existing value is:
// > 0 -- no value
// > 1 -- just 1 data point: year
// > 2 -- just 2 data points: year and month
// > 3 -- 3 data points: year, month, day
// > 4 -- could not parse the value, so shouldn't mess with this
// This implies if we discover day, month, year, but the current value is just year, we can update it
// Real example: http://en.wikipedia.org/w/index.php?title=Fuad_Abdurahmanov&diff=480997918&oldid=480997834
int currentComplexity = 0;
string currentValueTrimmed = currentValue.Trim();
if (currentValueTrimmed != "")
{
currentComplexity = 4; // by default, we cannot parse it
// YEAR
if (Regex.Match(currentValueTrimmed, @"^(\[\[)?[0-9]{4}(\]\])?$").Success)
currentComplexity = 1;
// MONTH YEAR
else if (Regex.Match(currentValueTrimmed, @"^" + monthNames + @"\s*[0-9]{4}$", RegexOptions.IgnoreCase).Success)
currentComplexity = 2;
// DAY MONTH YEAR
else if (Regex.Match(currentValueTrimmed, @"^[0-9]{2}(?:th|nd|rd)?\s*" + monthNames + @"\s*[0-9]{4}$", RegexOptions.IgnoreCase).Success)
currentComplexity = 3;
// MONTH DAY YEAR
else if (Regex.Match(currentValueTrimmed, @"^" + monthNames + @"^[0-9]{2}(?:th|nd|rd)?,?\s*[0-9]{4}$", RegexOptions.IgnoreCase).Success)
currentComplexity = 3;
// DAY MONTH YEAR (probably)
else if (Regex.Match(currentValueTrimmed, @"^[0-9]{2}[\s\-\.]*[0-9]{2}[\s\-\.]*[0-9]{4}$", RegexOptions.IgnoreCase).Success)
currentComplexity = 3;
// YEAR MONTH DAY
else if (Regex.Match(currentValueTrimmed, @"^[0-9]{4}[\s\-\.]*[0-9]{2}[\s\-\.]*[0-9]{2}$", RegexOptions.IgnoreCase).Success)
currentComplexity = 3;
}
// We will skip guessing if we already have 3 data points (day, month, year) because we cannot guess anything more accurate anyway
// Potentially, we can verify that our guess is the same as the value and alert the user if it isn't
if (currentComplexity >= 3)
return currentValue;
Match infoboxMatch = regexInfoboxTemplate.Match(currentPageText);
if (infoboxMatch.Success)
{
string infoboxText = infoboxMatch.Value;
Match fieldMatch = guessDateType == GuessDateType.birth ? regexInfoboxDateOfBirth.Match(infoboxText) : regexInfoboxDateOfDeath.Match(infoboxText);
if (fieldMatch.Success)
{
// Get the value of the field
string value = fieldMatch.Groups[1].Value.Trim();
if (value == "") return currentValue;
// Trim any comments from the field
value = Regex.Replace(value, "<!--.*?-->", "").Trim();
if (value == "") return currentValue;
if (currentComplexity < 3) // if current value is less than day+month+year
{
// Try to find birth date template
// {{Birth date|1993|2|24|df=yes}} returns "24 February 1993"
// {{Birth date|1993|2|24|mf=yes}} returns "February 24, 1993"
Match match = Regex.Match(
value,
@"\{\{\s*" +
(guessDateType == GuessDateType.birth ? birthDateTemplates : deathDateTemplates) +
@"([0-9]+)\s*\|\s*([0-9]+)\s*\|\s*([0-9]+)\s*\|?(?:\s*([md])f\s*=\s*yes)?", // don't care about the ending
RegexOptions.IgnoreCase
);
if (match.Success)
{
if (match.Groups[4].Value == "m")
return match.Groups[3] + " " + MonthName(int.Parse(match.Groups[2].Value)) + " " + match.Groups[1];
else
return MonthName(int.Parse(match.Groups[2].Value)) + " " + match.Groups[3] + ", " + match.Groups[1];
}
}
// Birth-date death-date -- plain text
// {{start-date|7 December 1941}}
// {{start-date|5:43PM HST, December 7th, 1941|tz=y}}
// {{start-date| December 8, 1941 12:50PM Australia/Adelaide|tz=y}}
if (currentComplexity == 0) // if there was no value before
return value; // try the field without any parsing, user can fix it up
}
}
return currentValue;
}
private string MonthName(int monthNumber)
{
switch (monthNumber)
{
case 01: return "January";
case 02: return "February";
case 03: return "March";
case 04: return "April";
case 05: return "May";
case 06: return "June";
case 07: return "July";
case 08: return "August";
case 09: return "September";
case 10: return "October";
case 11: return "November";
case 12: return "December";
default: return monthNumber < 10 ? "0" + monthNumber : monthNumber.ToString();
}
}
private void UpdateFocus()
{
if (pageListSource == PageListSource.category)
{
if (pageListSourceValue == "Category:Persondata templates without name parameter")
{
textBoxName.Focus();
}
else if (pageListSourceValue == "Category:Persondata templates without short description parameter")
{
textBoxShortDescription.Focus();
}
else
{
textBoxName.Focus();
}
}
else
{
textBoxName.Focus();
}
}
void LoadAhead()
{
bool pageListDone = false;
while (true)
{
bool shouldLoadNext;
lock (lastLoadedPageIndexLock)
{
shouldLoadNext = (lastLoadedPageIndex - (lastSavedPageIndex) - 1 < numLoadAheadPages);
}
if (shouldLoadNext)
{
int nextPage = lastLoadedPageIndex + 1;
if (!pageListDone && nextPage >= pageList.Count() - numLoadAheadPages && loadPageListOnDemand)
{
int prevCount = pageList.Count();
pageList.FillSomeFromCategoryEx(pageListSourceValue, ref pageListNext);
pageListDone = (pageList.Count() == prevCount);
}
if (nextPage >= pageList.Count())
{
break;
}
try
{
pageList[nextPage].Load();
}
catch (Exception e)
{
this.Invoke(new MethodInvoker(delegate()
{
MessageBox.Show("Could not load page " + pageList[nextPage].title + " due to error:\n" + e.Message);
}));
break;
}
lock (lastLoadedPageIndexLock)
{
lastLoadedPageIndex++;
}
}
Thread.Sleep(100);
}
}
private void FormMain_Resize(object sender, EventArgs e)
{
textBoxWikitext.Width = this.ClientSize.Width - panelPersondata.Width;
textBoxWikitext.Height = this.ClientSize.Height - (textBoxTitle.Top + textBoxTitle.Height + 8);
textBoxWikitext.Top = this.ClientSize.Height - textBoxWikitext.Height;
textBoxTitle.Width = this.ClientSize.Width - panelPersondata.Width - textBoxTitle.Left - buttonOpenInBrowser.Width - 16;
buttonOpenInBrowser.Left = textBoxTitle.Left + textBoxTitle.Width + 8;
}
private void FormMain_FormClosed(object sender, FormClosedEventArgs e)
{
if (loadingDialog != null)
{
loadingDialog.Close();
}
if (loadAheadThread != null)
{
loadAheadThread.Abort();
}
if (savePendingThread != null)
{
for (int i=0; i < 300; i++)
{
lock (saveQueue)
{
if (saveQueue.Count == 0)
{
break;
}
}
Thread.Sleep(100);
}
savePendingThread.Abort();
}
Application.Exit();
}
private delegate string UpdateEditSummaryHelper(string name, string origValue, string value);
private string UpdateEditSummaryHelperExtraLong(string name, string origValue, string value)
{
if (origValue.Trim() == "" && value.Trim() != "")
{
return "added " + name + " \"" + value.Trim() + "\"";
}
else if (origValue.Trim() != "" && value.Trim() == "")
{
return "removed " + name + " (was \"" + origValue.Trim() + "\")";
}
else if (origValue.Trim() != value.Trim() && origValue.Trim() != "" && value.Trim() != "")
{
return "updated " + name + " from \"" + origValue.Trim() + "\" to \"" + value.Trim() + "\"";
}
return "";
}
private string UpdateEditSummaryHelperLong(string name, string origValue, string value)
{
if (origValue.Trim() == "" && value.Trim() != "")
{
return "added " + name + " \"" + value.Trim() + "\"";
}
else if (origValue.Trim() != "" && value.Trim() == "")
{
return "removed " + name;
}
else if (origValue.Trim() != value.Trim() && origValue.Trim() != "" && value.Trim() != "")
{
return "updated " + name + " to \"" + value.Trim() + "\"";
}
return "";
}
private string UpdateEditSummaryHelperShort(string name, string origValue, string value)
{
if (origValue.Trim() == "" && value.Trim() != "")
{
return "added " + name;
}
else if (origValue.Trim() != "" && value.Trim() == "")
{
return "removed " + name;
}
else if (origValue.Trim() != value.Trim() && origValue.Trim() != "" && value.Trim() != "")
{
return "updated " + name;
}
return "";
}
private void CreateSummaryList(UpdateEditSummaryHelper helper, List<string> list)
{
list.Add(helper("name", origName, textBoxName.Text));
list.Add(helper("alternative names", origAlternativeNames, textBoxAlternativeNames.Text));
list.Add(helper("short description", origShortDescription, textBoxShortDescription.Text));
list.Add(helper("date of birth", origDateOfBirth, textBoxDateOfBirth.Text));
list.Add(helper("place of birth", origPlaceOfBirth, textBoxPlaceOfBirth.Text));
list.Add(helper("date of death", origDateOfDeath, textBoxDateOfDeath.Text));
list.Add(helper("place of death", origPlaceOfDeath, textBoxPlaceOfDeath.Text));
list.RemoveAll(delegate(string s) { return s.Length == 0; });
}
private void UpdateEditSummary()
{
if (manualEditSummary)
{
return;
}
List<string> listExtraLong = new List<string>();
List<string> listLong = new List<string>();
List<string> listShort = new List<string>();
CreateSummaryList(UpdateEditSummaryHelperExtraLong, listExtraLong);
CreateSummaryList(UpdateEditSummaryHelperLong, listLong);
CreateSummaryList(UpdateEditSummaryHelperShort, listShort);
if (listLong.Count == 0)
{
textBoxEditSummary.Text = "";
}
else
{
textBoxEditSummary.Text = "Persondata: " + string.Join(", ", listExtraLong.ToArray()) + " using [[WP:POM|Persondata-o-matic]]";
if (textBoxEditSummary.Text.Length > 250)
textBoxEditSummary.Text = "Persondata: " + string.Join(", ", listLong.ToArray()) + " using [[WP:POM|Persondata-o-matic]]";
if (textBoxEditSummary.Text.Length > 250)
textBoxEditSummary.Text = "Persondata: " + string.Join(", ", listShort.ToArray()) + " using [[WP:POM|Persondata-o-matic]]";
if (textBoxEditSummary.Text.Length > 250)
textBoxEditSummary.Text = "Modifying Persondata using [[WP:POM|Persondata-o-matic]]";
}
manualEditSummary = false;
}
private void UpdateField(string value, Regex regex)
{
value = value.Trim();
Match m = regex.Match(currentTemplate);
int startIndexTemplate = m.Groups[1].Index;
int startIndexPage = currentTemplateIndex + startIndexTemplate;
int length = m.Groups[1].Length;
if (m.Groups[2].Value.Trim() == value.Trim())
{
// No change except for whitespace, don't mess with it
return;
}
// Keep enter at the end if there is one already
string suffix = "";
if (m.Groups[1].Value.EndsWith("\n"))
{
suffix = "\n";
}
currentPageText = currentPageText.Remove(startIndexPage, length);
currentPageText = currentPageText.Insert(startIndexPage, " " + value + suffix);
currentTemplate = currentTemplate.Remove(startIndexTemplate, length);
currentTemplate = currentTemplate.Insert(startIndexTemplate, " " + value + suffix);
int savePos = TextBoxHelpers.GetVScrollPos(textBoxWikitext);
textBoxWikitext.Text = Regex.Replace(currentPageText, @"(?!=\r)\n", "\r\n");
TextBoxHelpers.SetVScrollPos(textBoxWikitext, savePos);
UpdateEditSummary();
}
private void textBoxName_TextChanged(object sender, EventArgs e)
{
UpdateField(textBoxName.Text, regexName);
textBoxName.BackColor = ColorFromChange(origName, textBoxName.Text);
}
private void textBoxAlternativeNames_TextChanged(object sender, EventArgs e)
{
UpdateField(textBoxAlternativeNames.Text, regexAlternativeNames);
textBoxAlternativeNames.BackColor = ColorFromChange(origAlternativeNames, textBoxAlternativeNames.Text);
}
private void textBoxShortDescription_TextChanged(object sender, EventArgs e)
{
UpdateField(textBoxShortDescription.Text, regexShortDescription);
textBoxShortDescription.BackColor = ColorFromChange(origShortDescription, textBoxShortDescription.Text);
}
private void textBoxDateOfBirth_TextChanged(object sender, EventArgs e)
{
UpdateField(textBoxDateOfBirth.Text, regexPersondataDateOfBirth);
textBoxDateOfBirth.BackColor = ColorFromChange(origDateOfBirth, textBoxDateOfBirth.Text);
}
private void textBoxPlaceOfBirth_TextChanged(object sender, EventArgs e)
{
UpdateField(textBoxPlaceOfBirth.Text, regexPlaceOfBirth);
textBoxPlaceOfBirth.BackColor = ColorFromChange(origPlaceOfBirth, textBoxPlaceOfBirth.Text);
}
private void textBoxDateOfDeath_TextChanged(object sender, EventArgs e)
{
UpdateField(textBoxDateOfDeath.Text, regexDateOfDeath);
textBoxDateOfDeath.BackColor = ColorFromChange(origDateOfDeath, textBoxDateOfDeath.Text);
}
private void textBoxPlaceOfDeath_TextChanged(object sender, EventArgs e)
{
UpdateField(textBoxPlaceOfDeath.Text, regexPlaceOfDeath);
textBoxPlaceOfDeath.BackColor = ColorFromChange(origPlaceOfDeath, textBoxPlaceOfDeath.Text);
}
private void textBoxEditSummary_TextChanged(object sender, EventArgs e)
{
manualEditSummary = true;
}
/// <summary>
/// This will return a background color for a textbox based on what the original value was and what the new value is
/// </summary>
private Color ColorFromChange(string original, string current)
{
// No change
if (original == current) return Color.White;
// We removed the field
if (original != "" && current == "") return Color.Tomato;
// We added the field
if (original == "" && current != "") return Color.Cyan;
// We modified the field
if (original != "" && current != "") return Color.Gold;
return Color.HotPink; // becuse we are awesome and this should never happen
}
private void FormMain_Shown(object sender, EventArgs e)
{
UpdateFocus();
tabControlPage.SelectTab(tabPageMarkup);
}
private void buttonSkip_Click(object sender, EventArgs e)
{
if (lastSavedPageIndex < pageList.Count())
{
lock (lastLoadedPageIndexLock)
{
lastSavedPageIndex++;
}
ShowNextPage();
}
}
private void buttonSaveAndNext_Click(object sender, EventArgs e)
{
if (lastSavedPageIndex < pageList.Count())
{
lock (lastLoadedPageIndexLock)
{
lastSavedPageIndex++;
}
if (currentPageText != origPageText)
{
pageList[lastSavedPageIndex].text = currentPageText;
SaveInfo info = new SaveInfo();
info.Page = pageList[lastSavedPageIndex];
info.EditSummary = textBoxEditSummary.Text;
lock (saveQueue)
{
saveQueue.Enqueue(info);
}
}
ShowNextPage();
}
}
private void buttonRemove_Click(object sender, EventArgs e)
{
if (lastSavedPageIndex < pageList.Count())
{
lock (lastLoadedPageIndexLock)
{
lastSavedPageIndex++;
}
currentPageText = regexPersondataTemplate.Replace(currentPageText, "");
if (currentPageText != origPageText)
{
pageList[lastSavedPageIndex].text = currentPageText;
SaveInfo info = new SaveInfo();
info.Page = pageList[lastSavedPageIndex];
info.EditSummary = "Removing persondata (not a biographical article) using [[WP:POM|Persondata-o-matic]]";
lock (saveQueue)
{
saveQueue.Enqueue(info);
}
}
ShowNextPage();
}
}
private void buttonOpenInBrowser_Click(object sender, EventArgs e)
{
// Based on http://dotnetpulse.blogspot.com/2006/04/opening-url-from-within-c-program.html
string key = @"http\shell\open\command";
RegistryKey registryKey = Registry.ClassesRoot.OpenSubKey(key, false);
// get default browser path
string defaultBrowserPath = ((string)registryKey.GetValue(null, null)).Split('"')[1];
try
{
Process.Start(defaultBrowserPath, "http://en.wikipedia.org/wiki/" + pageList[lastSavedPageIndex + 1].title.Replace(' ', '_'));
}
catch (Exception ex)
{
MessageBox.Show("Error occurred starting default browser: " + ex.Message);
}
}
private void tabControlPage_SelectedIndexChanged(object sender, EventArgs e)
{
if (tabControlPage.SelectedTab == tabPageBrowser)
{
NavigateWebBrowserToCurrentPage();
}
}
private void restoreValueButton_Click(object sender, EventArgs e)
{
textBoxName.Text = origName;
textBoxAlternativeNames.Text = origAlternativeNames;
textBoxShortDescription.Text = origShortDescription;
textBoxDateOfBirth.Text = origDateOfBirth;
textBoxPlaceOfBirth.Text = origPlaceOfBirth;
textBoxDateOfDeath.Text = origDateOfDeath;
textBoxPlaceOfDeath.Text = origPlaceOfDeath;
}
}
public class SaveInfo
{
public Page Page;
public string EditSummary;
}
public static class TextBoxHelpers
{
// From http://www.pinvoke.net/default.aspx/user32.getscrollpos , http://pinvoke.net/default.aspx/user32/SetScrollPos.html
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int GetScrollPos(IntPtr hWnd, System.Windows.Forms.Orientation nBar);
[DllImport("user32.dll")]
public static extern int SetScrollPos(IntPtr hWnd, System.Windows.Forms.Orientation nBar, int nPos, bool bRedraw);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
// Adapted from http://www.vbforums.com/showthread.php?p=3801101
private const uint EM_LINESCROLL = 0xb6;
private const uint WM_VSCROLL = 0x115;
private const int SB_TOP = 6;
// Adapted from http://www.pinvoke.net/default.aspx/user32.getscrollpos
public static int GetVScrollPos(TextBox textBox)
{
return GetScrollPos((IntPtr)textBox.Handle, System.Windows.Forms.Orientation.Vertical);
}
public static void SetVScrollPos(TextBox textBox, int position)
{
SetScrollPos((IntPtr)textBox.Handle, System.Windows.Forms.Orientation.Vertical, position, true);
// Adapted from http://stackoverflow.com/questions/1069497/how-to-scroll-down-in-a-textbox-by-code-in-c-sharp
SendMessage(textBox.Handle, WM_VSCROLL, new IntPtr(SB_TOP), IntPtr.Zero);
SendMessage(textBox.Handle, EM_LINESCROLL, IntPtr.Zero, new IntPtr(position));
}
}
}