-
Notifications
You must be signed in to change notification settings - Fork 0
/
Wiki.java
7779 lines (7216 loc) · 298 KB
/
Wiki.java
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
/**
* @(#)Wiki.java 0.33 10/08/2017
* Copyright (C) 2007 - 2017 MER-C and contributors
*
* 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 3
* of the License, or (at your option) any later version. Additionally
* this file is subject to the "Classpath" exception.
*
* 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 Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.wikipedia;
import java.io.*;
import java.net.*;
import java.nio.*;
import java.nio.file.*;
import java.text.Normalizer;
import java.time.*;
import java.time.format.*;
import java.util.*;
import java.util.function.*;
import java.util.logging.*;
import java.util.zip.GZIPInputStream;
import javax.security.auth.login.*;
/**
* This is a somewhat sketchy bot framework for editing MediaWiki wikis.
* Requires JDK 1.8 or greater. Uses the <a
* href="//www.mediawiki.org/wiki/API:Main_page">MediaWiki API</a> for most
* operations. It is recommended that the server runs the latest version
* of MediaWiki (1.28), otherwise some functions may not work.
* <p>
* Extended documentation is available
* <a href="//github.com/MER-C/wiki-java/wiki/Extended-documentation">here</a>.
* All wikilinks are relative to the English Wikipedia and all timestamps are in
* your wiki's time zone.
* </p>
* Please file bug reports <a href="//en.wikipedia.org/wiki/User_talk:MER-C">here</a>
* or at the <a href="//github.com/MER-C/wiki-java/issues">Github issue tracker</a>.
*
* @author MER-C and contributors
* @version 0.33
*/
public class Wiki implements Serializable
{
// Master TODO list:
// *Admin stuff
// *More multiqueries
// *Generators (hard)
// NAMESPACES
/**
* Denotes the namespace of images and media, such that there is no
* description page. Uses the "Media:" prefix.
* @see #FILE_NAMESPACE
* @since 0.03
*/
public static final int MEDIA_NAMESPACE = -2;
/**
* Denotes the namespace of pages with the "Special:" prefix. Note
* that many methods dealing with special pages may spew due to
* raw content not being available.
* @since 0.03
*/
public static final int SPECIAL_NAMESPACE = -1;
/**
* Denotes the main namespace, with no prefix.
* @since 0.03
*/
public static final int MAIN_NAMESPACE = 0;
/**
* Denotes the namespace for talk pages relating to the main
* namespace, denoted by the prefix "Talk:".
* @since 0.03
*/
public static final int TALK_NAMESPACE = 1;
/**
* Denotes the namespace for user pages, given the prefix "User:".
* @since 0.03
*/
public static final int USER_NAMESPACE = 2;
/**
* Denotes the namespace for user talk pages, given the prefix
* "User talk:".
* @since 0.03
*/
public static final int USER_TALK_NAMESPACE = 3;
/**
* Denotes the namespace for pages relating to the project,
* with prefix "Project:". It also goes by the name of whatever
* the project name was.
* @since 0.03
*/
public static final int PROJECT_NAMESPACE = 4;
/**
* Denotes the namespace for talk pages relating to project
* pages, with prefix "Project talk:". It also goes by the name
* of whatever the project name was, + "talk:".
* @since 0.03
*/
public static final int PROJECT_TALK_NAMESPACE = 5;
/**
* Denotes the namespace for file description pages. Has the prefix
* "File:". Do not create these directly, use upload() instead.
* @see #MEDIA_NAMESPACE
* @since 0.25
*/
public static final int FILE_NAMESPACE = 6;
/**
* Denotes talk pages for file description pages. Has the prefix
* "File talk:".
* @since 0.25
*/
public static final int FILE_TALK_NAMESPACE = 7;
/**
* Denotes the namespace for (wiki) system messages, given the prefix
* "MediaWiki:".
* @since 0.03
*/
public static final int MEDIAWIKI_NAMESPACE = 8;
/**
* Denotes the namespace for talk pages relating to system messages,
* given the prefix "MediaWiki talk:".
* @since 0.03
*/
public static final int MEDIAWIKI_TALK_NAMESPACE = 9;
/**
* Denotes the namespace for templates, given the prefix "Template:".
* @since 0.03
*/
public static final int TEMPLATE_NAMESPACE = 10;
/**
* Denotes the namespace for talk pages regarding templates, given
* the prefix "Template talk:".
* @since 0.03
*/
public static final int TEMPLATE_TALK_NAMESPACE = 11;
/**
* Denotes the namespace for help pages, given the prefix "Help:".
* @since 0.03
*/
public static final int HELP_NAMESPACE = 12;
/**
* Denotes the namespace for talk pages regarding help pages, given
* the prefix "Help talk:".
* @since 0.03
*/
public static final int HELP_TALK_NAMESPACE = 13;
/**
* Denotes the namespace for category description pages. Has the
* prefix "Category:".
* @since 0.03
*/
public static final int CATEGORY_NAMESPACE = 14;
/**
* Denotes the namespace for talk pages regarding categories. Has the
* prefix "Category talk:".
* @since 0.03
*/
public static final int CATEGORY_TALK_NAMESPACE = 15;
/**
* Denotes all namespaces.
* @since 0.03
*/
public static final int ALL_NAMESPACES = 0x09f91102;
// LOG TYPES
/**
* Denotes all logs.
* @since 0.06
*/
public static final String ALL_LOGS = "";
/**
* Denotes the user creation log.
* @since 0.06
*/
public static final String USER_CREATION_LOG = "newusers";
/**
* Denotes the upload log.
* @since 0.06
*/
public static final String UPLOAD_LOG = "upload";
/**
* Denotes the deletion log.
* @since 0.06
*/
public static final String DELETION_LOG = "delete";
/**
* Denotes the move log.
* @since 0.06
*/
public static final String MOVE_LOG = "move";
/**
* Denotes the block log.
* @since 0.06
*/
public static final String BLOCK_LOG = "block";
/**
* Denotes the protection log.
* @since 0.06
*/
public static final String PROTECTION_LOG = "protect";
/**
* Denotes the user rights log.
* @since 0.06
*/
public static final String USER_RIGHTS_LOG = "rights";
/**
* Denotes the user renaming log.
* @since 0.06
*/
public static final String USER_RENAME_LOG = "renameuser";
/**
* Denotes the page importation log.
* @since 0.08
*/
public static final String IMPORT_LOG = "import";
/**
* Denotes the edit patrol log.
* @since 0.08
*/
public static final String PATROL_LOG = "patrol";
// PROTECTION LEVELS
/**
* Denotes a non-protected page.
* @since 0.09
*/
public static final String NO_PROTECTION = "all";
/**
* Denotes semi-protection (i.e. only autoconfirmed users can perform a
* particular action).
* @since 0.09
*/
public static final String SEMI_PROTECTION = "autoconfirmed";
/**
* Denotes full protection (i.e. only admins can perfom a particular action).
* @since 0.09
*/
public static final String FULL_PROTECTION = "sysop";
// ASSERTION MODES
/**
* Use no assertions (i.e. 0).
* @see #setAssertionMode
* @since 0.11
*/
public static final int ASSERT_NONE = 0;
/**
* Assert that we are logged in (i.e. 1). This is checked every action.
* @see #setAssertionMode
* @since 0.30
*/
public static final int ASSERT_USER = 1;
/**
* Assert that we have a bot flag (i.e. 2). This is checked every action.
* @see #setAssertionMode
* @since 0.11
*/
public static final int ASSERT_BOT = 2;
/**
* Assert that we have no new messages. Not defined officially, but
* some bots have this. This is checked intermittently.
* @see #setAssertionMode
* @since 0.11
*/
public static final int ASSERT_NO_MESSAGES = 4;
/**
* Assert that we have a sysop flag (i.e. 8). This is checked intermittently.
* @see #setAssertionMode
* @since 0.30
*/
public static final int ASSERT_SYSOP = 8;
// RC OPTIONS
/**
* In queries against the recent changes table, this would mean we don't
* fetch anonymous edits.
* @since 0.20
*/
public static final int HIDE_ANON = 1;
/**
* In queries against the recent changes table, this would mean we don't
* fetch edits made by bots.
* @since 0.20
*/
public static final int HIDE_BOT = 2;
/**
* In queries against the recent changes table, this would mean we don't
* fetch by the logged in user.
* @since 0.20
*/
public static final int HIDE_SELF = 4;
/**
* In queries against the recent changes table, this would mean we don't
* fetch minor edits.
* @since 0.20
*/
public static final int HIDE_MINOR = 8;
/**
* In queries against the recent changes table, this would mean we don't
* fetch patrolled edits.
* @since 0.20
*/
public static final int HIDE_PATROLLED = 16;
// REVISION OPTIONS
/**
* In <tt>Revision.diff()</tt>, denotes the next revision.
* @see org.wikipedia.Wiki.Revision#diff(org.wikipedia.Wiki.Revision)
* @since 0.21
*/
public static final long NEXT_REVISION = -1L;
/**
* In <tt>Revision.diff()</tt>, denotes the current revision.
* @see org.wikipedia.Wiki.Revision#diff(org.wikipedia.Wiki.Revision)
* @since 0.21
*/
public static final long CURRENT_REVISION = -2L;
/**
* In <tt>Revision.diff()</tt>, denotes the previous revision.
* @see org.wikipedia.Wiki.Revision#diff(org.wikipedia.Wiki.Revision)
* @since 0.21
*/
public static final long PREVIOUS_REVISION = -3L;
/**
* The list of options the user can specify for his/her gender.
* @since 0.24
*/
public enum Gender
{
// These names come from the MW API so we can use valueOf() and
// toString() without any fidgets whatsoever. Java naming conventions
// aren't worth another 20 lines of code.
/**
* The user self-identifies as a male.
* @since 0.24
*/
male,
/**
* The user self-identifies as a female.
* @since 0.24
*/
female,
/**
* The user has not specified a gender in preferences.
* @since 0.24
*/
unknown;
}
private static final String version = "0.33";
// the domain of the wiki
private String domain;
protected String query, base, apiUrl;
protected String scriptPath = "/w";
private boolean wgCapitalLinks = true;
private ZoneId timezone = ZoneId.of("UTC");
// user management
private Map<String, String> cookies = new HashMap<>(12);
private User user;
private int statuscounter = 0;
// various caches
private transient LinkedHashMap<String, Integer> namespaces = null;
private transient ArrayList<Integer> ns_subpages = null;
private transient List<String> watchlist = null;
// preferences
private int max = 500;
private int slowmax = 50;
private int throttle = 10000; // throttle
private int maxlag = 5;
private int assertion = ASSERT_NONE; // assertion mode
private transient int statusinterval = 100; // status check
private int querylimit = Integer.MAX_VALUE;
private String useragent = "Wiki.java/" + version + " (https://github.com/MER-C/wiki-java/)";
private boolean zipped = true;
private boolean markminor = false, markbot = false;
private boolean resolveredirect = false;
private String protocol = "https://";
private Level loglevel = Level.ALL;
private static final Logger logger = Logger.getLogger("wiki");
// Store time when the last throttled action was executed
private long lastThrottleActionTime = 0;
// retry count
private int maxtries = 2;
// serial version
private static final long serialVersionUID = -8745212681497643456L;
// time to open a connection
private static final int CONNECTION_CONNECT_TIMEOUT_MSEC = 30000; // 30 seconds
// time for the read to take place. (needs to be longer, some connections are slow
// and the data volume is large!)
private static final int CONNECTION_READ_TIMEOUT_MSEC = 180000; // 180 seconds
// log2(upload chunk size). Default = 22 => upload size = 4 MB. Disable
// chunked uploads by setting a large value here (50 = 1 PB will do).
private static final int LOG2_CHUNK_SIZE = 22;
// maximum URL length in bytes
protected static final int URL_LENGTH_LIMIT = 8192;
// CONSTRUCTORS AND CONFIGURATION
/**
* Creates a new connection to the English Wikipedia via HTTPS.
* @since 0.02
* @deprecated use Wiki#createInstance instead
*/
@Deprecated
public Wiki()
{
this("en.wikipedia.org", "/w");
}
/**
* Creates a new connection to a wiki via HTTPS. WARNING: if the wiki uses
* a $wgScriptpath other than the default <tt>/w</tt>, you need to call
* <tt>getScriptPath()</tt> to automatically set it. Alternatively, you
* can use the constructor below if you know it in advance.
*
* @param domain the wiki domain name e.g. en.wikipedia.org (defaults to
* en.wikipedia.org)
* @deprecated use Wiki#createInstance instead
*/
@Deprecated
public Wiki(String domain)
{
this(domain, "/w");
}
/**
* Creates a new connection to a wiki with $wgScriptpath set to
* <tt>scriptPath</tt> via HTTPS.
*
* @param domain the wiki domain name
* @param scriptPath the script path
* @since 0.14
* @deprecated use Wiki#createInstance instead
*/
@Deprecated
public Wiki(String domain, String scriptPath)
{
this(domain, scriptPath, "https://");
}
/**
* Creates a new connection to a wiki with $wgScriptpath set to
* <tt>scriptPath</tt> via the specified protocol.
*
* @param domain the wiki domain name
* @param scriptPath the script path
* @param protocol a protocol e.g. "http://", "https://" or "file:///"
* @since 0.31
* @deprecated use Wiki#createInstance instead; this will be made private
* for the reason in the TODO comment below
*/
@Deprecated
public Wiki(String domain, String scriptPath, String protocol)
{
if (domain == null || domain.isEmpty())
domain = "en.wikipedia.org";
this.domain = domain;
this.scriptPath = scriptPath;
this.protocol = protocol;
// init variables
// This is fine as long as you do not have parameters other than domain
// and scriptpath in constructors and do not do anything else than super(x)!
// http://stackoverflow.com/questions/3404301/whats-wrong-with-overridable-method-calls-in-constructors
// TODO: remove this
logger.setLevel(loglevel);
logger.log(Level.CONFIG, "[{0}] Using Wiki.java {1}", new Object[] { domain, version });
initVars();
}
/**
* Creates a new connection to a wiki via HTTPS. Depending on the settings
* of the wiki, you may need to call {@link Wiki#getSiteInfo()} on the
* returned object after this in order for some functionality to work
* correctly.
*
* @param domain the wiki domain name e.g. en.wikipedia.org (defaults to
* en.wikipedia.org)
* @return the created wiki
* @since 0.34
*/
public static Wiki createInstance(String domain)
{
return createInstance(domain, "/w", "https://");
}
/**
* Creates a new connection to a wiki with $wgScriptpath set to
* settings <tt>scriptPath</tt> via the specified protocol. Depending on
* the settings of the wiki, you may need to call {@link Wiki#getSiteInfo()}
* on the returned object after this in order for some functionality to work
* correctly.
*
* @param domain the wiki domain name
* @param scriptPath the script path
* @param protocol a protocol e.g. "http://", "https://" or "file:///"
* @return the constructed Wiki object
* @since 0.34
*/
public static Wiki createInstance(String domain, String scriptPath, String protocol)
{
// Don't put network requests here. Servlets cannot afford to make
// unnecessary network requests in initialization.
Wiki wiki = new Wiki(domain, "/w", protocol);
wiki.initVars(); // construct URL bases
return wiki;
}
/**
* Edit this if you need to change the API and human interface url
* configuration of the wiki. One example use is server-side cache
* management (maxage and smaxage API parameters).
*
* <p>Contributed by Tedder
* @since 0.24
*/
protected void initVars()
{
StringBuilder basegen = new StringBuilder(protocol);
basegen.append(domain);
basegen.append(scriptPath);
StringBuilder apigen = new StringBuilder(basegen);
apigen.append("/api.php?format=xml&");
// MediaWiki has inbuilt maxlag functionality, see [[mw:Manual:Maxlag
// parameter]]. Let's exploit it.
if (maxlag >= 0)
{
apigen.append("maxlag=");
apigen.append(maxlag);
apigen.append("&");
basegen.append("/index.php?maxlag=");
basegen.append(maxlag);
basegen.append("&title=");
}
else
basegen.append("/index.php?title=");
base = basegen.toString();
// the native API supports assertions as of MW 1.23
if ((assertion & ASSERT_BOT) == ASSERT_BOT)
apigen.append("assert=bot&");
else if ((assertion & ASSERT_USER) == ASSERT_USER)
apigen.append("assert=user&");
apiUrl = apigen.toString();
apigen.append("action=query&");
if (resolveredirect)
apigen.append("redirects&");
query = apigen.toString();
}
/**
* Gets the domain of the wiki, as supplied on construction.
* @return the domain of the wiki
* @since 0.06
*/
public String getDomain()
{
return domain;
}
/**
* Gets the editing throttle.
* @return the throttle value in milliseconds
* @see #setThrottle
* @since 0.09
*/
public int getThrottle()
{
return throttle;
}
/**
* Sets the editing throttle. Read requests are not throttled or restricted
* in any way. Default is 10s.
* @param throttle the new throttle value in milliseconds
* @see #getThrottle
* @since 0.09
*/
public void setThrottle(int throttle)
{
this.throttle = throttle;
log(Level.CONFIG, "setThrottle", "Throttle set to " + throttle + " milliseconds");
}
/**
* Gets various properties of the wiki and sets the bot framework up to use
* them. Returns:
* <ul>
* <li><b>usingcapitallinks</b>: (Boolean) whether a wiki forces upper case
* for the title. Example: en.wikipedia = true, en.wiktionary = false.
* Default = true. See [[mw:Manual:$wgCapitalLinks]].
* <li><b>scriptpath</b>: (String) the $wgScriptpath wiki variable. Default
* = <tt>/w</tt>. See [[mw:Manual:$wgScriptpath]].
* <li><b>version</b>: (String) the MediaWiki version used for this wiki
* <li><b>timezone</b>: (String) the timezone the wiki is in, default = UTC
* </ul>
*
* @return (see above)
* @since 0.30
* @throws IOException if a network error occurs
*/
public Map<String, Object> getSiteInfo() throws IOException
{
Map<String, Object> ret = new HashMap<>();
String line = fetch(query + "action=query&meta=siteinfo", "getSiteInfo");
wgCapitalLinks = parseAttribute(line, "case", 0).equals("first-letter");
ret.put("usingcapitallinks", wgCapitalLinks);
scriptPath = parseAttribute(line, "scriptpath", 0);
ret.put("scriptpath", scriptPath);
timezone = ZoneId.of(parseAttribute(line, "timezone", 0));
ret.put("timezone", timezone);
ret.put("version", parseAttribute(line, "generator", 0));
initVars();
return ret;
}
/**
* Sets the user agent HTTP header to be used for requests. Default is
* "Wiki.java " + version.
* @param useragent the new user agent
* @since 0.22
*/
public void setUserAgent(String useragent)
{
this.useragent = useragent;
}
/**
* Gets the user agent HTTP header to be used for requests. Default is
* "Wiki.java " + version.
* @return useragent the user agent
* @since 0.22
*/
public String getUserAgent()
{
return useragent;
}
/**
* Enables/disables GZip compression for GET requests. Default: true.
* @param zipped whether we use GZip compression
* @since 0.23
*/
public void setUsingCompressedRequests(boolean zipped)
{
this.zipped = zipped;
}
/**
* Checks whether we are using GZip compression for GET requests.
* Default: true.
* @return (see above)
* @since 0.23
*/
public boolean isUsingCompressedRequests()
{
return zipped;
}
/**
* Checks whether API action=query dependencies automatically resolve
* redirects (default = false).
* @return (see above)
* @since 0.27
*/
public boolean isResolvingRedirects()
{
return resolveredirect;
}
/**
* Sets whether API action=query dependencies automatically resolve
* redirects (default = false).
* @param b (see above)
* @since 0.27
*/
public void setResolveRedirects(boolean b)
{
resolveredirect = b;
initVars();
}
/**
* Sets whether edits are marked as bot by default (may be overridden
* specifically by edit()). Default = false. Works only if one has the
* required permissions.
* @param markbot (see above)
* @since 0.26
*/
public void setMarkBot(boolean markbot)
{
this.markbot = markbot;
}
/**
* Are edits are marked as bot by default?
* @return whether edits are marked as bot by default
* @since 0.26
*/
public boolean isMarkBot()
{
return markbot;
}
/**
* Sets whether edits are marked as minor by default (may be overridden
* specifically by edit()). Default = false.
* @param minor (see above)
* @since 0.26
*/
public void setMarkMinor(boolean minor)
{
this.markminor = minor;
}
/**
* Are edits are marked as minor by default?
* @return whether edits are marked as minor by default
* @since 0.26
*/
public boolean isMarkMinor()
{
return markminor;
}
/**
* Returns the maximum number of results returned when querying the API.
* Default = Integer.MAX_VALUE
* @return see above
* @since 0.34
*/
public int getQueryLimit()
{
return querylimit;
}
/**
* Sets the maximum number of results returned when querying the API.
* Useful for operating in constrained environments (e.g. web servers)
* or queries for which results are sorted by relevance (e.g. search).
*
* @param limit the desired maximum number of results to retrieve
* @throws IllegalArgumentException if <tt>limit</tt> is not a positive
* integer
* @since 0.34
*/
public void setQueryLimit(int limit)
{
if (limit < 1)
throw new IllegalArgumentException("Query limit must be a positive integer.");
querylimit = limit;
}
/**
* Determines whether this wiki is equal to another object.
* @param obj the object to compare
* @return whether the domains of the wikis are equal
* @since 0.10
*/
@Override
public boolean equals(Object obj)
{
if (!(obj instanceof Wiki))
return false;
return domain.equals(((Wiki)obj).domain);
}
/**
* Returns a hash code of this object.
* @return a hash code
* @since 0.12
*/
@Override
public int hashCode()
{
return domain.hashCode() * maxlag - throttle;
}
/**
* Returns a string representation of this Wiki.
* @return a string representation of this Wiki.
* @since 0.10
*/
@Override
public String toString()
{
// domain
StringBuilder buffer = new StringBuilder("Wiki[domain=");
buffer.append(domain);
// user
buffer.append(",user=");
buffer.append(user != null ? user.toString() : "null");
buffer.append(",");
// throttle mechanisms
buffer.append("throttle=");
buffer.append(throttle);
buffer.append(",maxlag=");
buffer.append(maxlag);
buffer.append(",assertionMode=");
buffer.append(assertion);
buffer.append(",statusCheckInterval=");
buffer.append(statusinterval);
buffer.append(",cookies=");
buffer.append(cookies);
buffer.append("]");
return buffer.toString();
}
/**
* Gets the maxlag parameter. See [[mw:Manual:Maxlag parameter]].
* @return the current maxlag, in seconds
* @see #setMaxLag
* @see #getCurrentDatabaseLag
* @since 0.11
*/
public int getMaxLag()
{
return maxlag;
}
/**
* Sets the maxlag parameter. A value of less than 0s disables this
* mechanism. Default is 5s.
* @param lag the desired maxlag in seconds
* @see #getMaxLag
* @see #getCurrentDatabaseLag
* @since 0.11
*/
public void setMaxLag(int lag)
{
maxlag = lag;
log(Level.CONFIG, "setMaxLag", "Setting maximum allowable database lag to " + lag);
initVars();
}
/**
* Gets the assertion mode. Assertion modes are bitmasks.
* @return the current assertion mode
* @see #setAssertionMode
* @since 0.11
*/
public int getAssertionMode()
{
return assertion;
}
/**
* Sets the assertion mode. Do this AFTER logging in, otherwise the login
* will fail. Assertion modes are bitmasks. Default is <tt>ASSERT_NONE</tt>.
* @param mode an assertion mode
* @see #getAssertionMode
* @since 0.11
*/
public void setAssertionMode(int mode)
{
assertion = mode;
log(Level.CONFIG, "setAssertionMode", "Set assertion mode to " + mode);
initVars();
}
/**
* Gets the number of actions (edit, move, block, delete, etc) between
* status checks. A status check is where we update user rights, block
* status and check for new messages (if the appropriate assertion mode
* is set).
*
* @return the number of edits between status checks
* @see #setStatusCheckInterval
* @since 0.18
*/
public int getStatusCheckInterval()
{
return statusinterval;
}
/**
* Sets the number of actions (edit, move, block, delete, etc) between
* status checks. A status check is where we update user rights, block
* status and check for new messages (if the appropriate assertion mode
* is set). Default is 100.
*
* @param interval the number of edits between status checks
* @see #getStatusCheckInterval
* @since 0.18
*/
public void setStatusCheckInterval(int interval)
{
statusinterval = interval;
log(Level.CONFIG, "setStatusCheckInterval", "Status check interval set to " + interval);
}
/**
* Set the logging level used by the internal logger.
* @param loglevel one of the levels specified in java.util.logging.LEVEL
* @since 0.31
*/
public void setLogLevel(Level loglevel)
{
this.loglevel = loglevel;
logger.setLevel(loglevel);
}
// META STUFF
/**
* Logs in to the wiki. This method is thread-safe.
*
* @param username a username
* @param password a password (as a char[] due to JPasswordField)
* @throws IOException if a network error occurs
* @see #logout
*/
public synchronized void login(String username, char[] password) throws IOException, FailedLoginException
{
StringBuilder buffer = new StringBuilder(500);
buffer.append("lgname=");
buffer.append(encode(username, false));
buffer.append("&lgpassword=");
buffer.append(encode(new String(password), false));
buffer.append("&lgtoken=");
buffer.append(encode(getToken("login"), false));
String line = post(apiUrl + "action=login", buffer.toString(), "login");
buffer.setLength(0);
// check for success
if (line.contains("result=\"Success\""))
{
user = new User(parseAttribute(line, "lgusername", 0));