Skip to content

Commit 03eaece

Browse files
authored
Merge pull request #20 from Choco31415/Development
Development
2 parents 38d2e5c + f81c586 commit 03eaece

34 files changed

Lines changed: 1231 additions & 436 deletions

build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ repositories {
2424
}
2525

2626
dependencies {
27+
compile 'com.fasterxml.jackson.core:jackson-core:2.8.0'
28+
compile 'com.fasterxml.jackson.core:jackson-databind:2.8.0'
2729
compile 'org.apache.commons:commons-lang3:3.4'
2830
compile 'commons-logging:commons-logging:1.2'
2931
compile 'org.apache.httpcomponents:httpclient:4.5.2'

src/main/java/WikiBot/APIcommands/APIcommand.java

Lines changed: 54 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,18 @@
77
import WikiBot.Errors.UnsupportedError;
88
import WikiBot.MediawikiData.MediawikiDataManager;
99
import WikiBot.MediawikiData.VersionNumber;
10+
import WikiBot.Utils.ArrayUtils;
1011

11-
//TODO: Update edit token and family files
12+
/**
13+
* This class is a base for making APIcommands.
14+
*
15+
* APIcommands are commands to a wiki's API.
16+
*/
1217
public class APIcommand extends PageLocationContainer {
1318

14-
protected String commandSummary;//One or two words to summarize what this edit does.
19+
protected String commandName;//One or two words to summarize what this edit does.
1520

16-
protected boolean requiresGET = false;
21+
protected boolean requiresPOST = false;
1722
protected boolean unescapeText = false;
1823
protected boolean unescapeHTML = true;
1924

@@ -23,78 +28,41 @@ public class APIcommand extends PageLocationContainer {
2328
protected ArrayList<String> keys = new ArrayList<String>();
2429
protected ArrayList<String> values = new ArrayList<String>();
2530

26-
27-
public APIcommand(String commandSummary_, PageLocation pl_, ArrayList<String> keys_, ArrayList<String> values_, boolean requiresGET_, String oldTokenType_, String newTokenType_) {
31+
public APIcommand(String commandName_, PageLocation pl_, boolean requiresPOST_, String oldTokenType_, String newTokenType_) {
2832
super(pl_);
29-
commandSummary = commandSummary_;
30-
keys.addAll(keys_);
31-
values.addAll(values_);
32-
requiresGET = requiresGET_;
33+
commandName = commandName_;
34+
requiresPOST = requiresPOST_;
3335
oldTokenType = oldTokenType_;
3436
newTokenType = newTokenType_;
3537
}
3638

37-
38-
public APIcommand(String commandSummary_, PageLocation pl_, ArrayList<String> keys_, ArrayList<String> values_) {
39+
public APIcommand(String commandName_, PageLocation pl_) {
3940
super(pl_);
40-
commandSummary = commandSummary_;
41-
keys.addAll(keys_);
42-
values.addAll(values_);
41+
commandName = commandName_;
4342
}
4443

45-
public APIcommand(String commandSummary_, PageLocation pl_, boolean requiresGET_, String oldTokenType_, String newTokenType_) {
46-
super(pl_);
47-
commandSummary = commandSummary_;
48-
requiresGET = requiresGET_;
49-
oldTokenType = oldTokenType_;
50-
newTokenType = newTokenType_;
51-
}
52-
53-
public APIcommand(String commandSummary_, PageLocation pl_) {
54-
super(pl_);
55-
commandSummary = commandSummary_;
56-
}
57-
58-
public APIcommand(String commandSummary_, String language, ArrayList<String> keys_, ArrayList<String> values_, boolean requiresGET_, String oldTokenType_, String newTokenType_) {
44+
public APIcommand(String commandName_, String language, boolean requiresPOST_, String oldTokenType_, String newTokenType_) {
5945
super(new PageLocation("null", language));
60-
commandSummary = commandSummary_;
61-
keys.addAll(keys_);
62-
values.addAll(values_);
63-
requiresGET = requiresGET_;
46+
commandName = commandName_;
47+
requiresPOST = requiresPOST_;
6448
oldTokenType = oldTokenType_;
6549
newTokenType = newTokenType_;
6650
}
6751

68-
69-
public APIcommand(String commandSummary_, String language, ArrayList<String> keys_, ArrayList<String> values_) {
70-
super(new PageLocation("null", language));
71-
commandSummary = commandSummary_;
72-
keys.addAll(keys_);
73-
values.addAll(values_);
74-
}
75-
76-
public APIcommand(String commandSummary_, String language, boolean requiresGET_, String oldTokenType_, String newTokenType_) {
52+
public APIcommand(String commandName_, String language) {
7753
super(new PageLocation("null", language));
78-
commandSummary = commandSummary_;
79-
requiresGET = requiresGET_;
80-
oldTokenType = oldTokenType_;
81-
newTokenType = newTokenType_;
54+
commandName = commandName_;
8255
}
8356

84-
public APIcommand(String commandSummary_, String language) {
85-
super(new PageLocation("null", language));
86-
commandSummary = commandSummary_;
87-
}
88-
89-
protected void enforceMWVersion(VersionNumber introduced) {
57+
protected void enforceMWVersion(String introduced) {
9058
enforceMWVersion(introduced, null);
9159
}
9260

93-
protected void enforceMWVersion(VersionNumber introduced, VersionNumber removed) {
61+
protected void enforceMWVersion(String introduced, String removed) {
9462
VersionNumber myVersion = getMWVersion();
9563

9664
if ((myVersion.compareTo(introduced) < 0 || introduced == null) && (myVersion.compareTo(removed) > 0 || removed == null)) {
97-
throw new UnsupportedError("The " + getLanguage() + " wiki does not support this API command.");
65+
throw new UnsupportedError("The " + getLanguage() + " wiki does not support this API command. (command name: " + commandName + ")");
9866
}
9967
}
10068

@@ -117,6 +85,16 @@ public void addParameter(String key, String value) {
11785
values.add(value);
11886
}
11987

88+
public boolean setParameter(String key, String value) {
89+
if (keys.contains(key)) {
90+
values.set(keys.indexOf(key), value);
91+
return true;
92+
} else {
93+
addParameter(key, value);
94+
return false;
95+
}
96+
}
97+
12098
public boolean removeParameter(String key) {
12199
if (keys.contains(key)) {
122100
values.remove(keys.indexOf(key));
@@ -163,16 +141,16 @@ public String getValue(String key) {
163141
return values.get(keys.indexOf(key));
164142
}
165143

166-
public void setRequiresGET(boolean bool) { requiresGET = bool; }
167-
public boolean requiresGET() { return requiresGET; }
144+
public void setRequiresPOST(boolean bool) { requiresPOST = bool; }
145+
public boolean requiresPOST() { return requiresPOST; }
168146
public void setUnescapeText(boolean bool) { unescapeText = bool; }
169147
public boolean shouldUnescapeText() { return unescapeText; }
170148
public void setUnescapeHTML(boolean bool) { unescapeHTML = bool; }
171149
public boolean shouldUnescapeHTML() { return unescapeHTML; }
172150

173151
//A simple one or two words to summarize what this edit does.
174-
public String getShortCommandSummary() {
175-
return commandSummary;
152+
public String getCommandName() {
153+
return commandName;
176154
}
177155

178156
public String getSummary() {
@@ -190,6 +168,25 @@ public String getSummary() {
190168
}
191169
return temp;
192170
}
171+
172+
protected String compactPLArray(ArrayList<PageLocation> array, String delimitor) {
173+
//This takes an array of strings and compacts it into one string.
174+
String output = "";
175+
176+
for (int i = 0; i < array.size(); i++) {
177+
output += array.get(i).getTitle();
178+
if (i != array.size()-1) {
179+
output += delimitor;
180+
}
181+
}
182+
183+
return output;
184+
}
185+
186+
protected String compactArray(ArrayList<String> array, String delimitor) {
187+
//This takes an array of strings and compacts it into one string.
188+
return ArrayUtils.compactArray(array, delimitor);
189+
}
193190

194191
@Override
195192
public boolean equals(Object obj) {

src/main/java/WikiBot/APIcommands/AppendText.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22

33
import WikiBot.ContentRep.PageLocation;
44

5+
/**
6+
* This command appends text to a page.
7+
*
8+
* Rights required:
9+
* edit
10+
*
11+
* MW version required:
12+
* 1.13+
13+
*/
514
public class AppendText extends APIcommand {
615
public AppendText(PageLocation pl_, String appendText_, String editSummary_) {
716
super("Append text", pl_, true, "edit", "csrf");
@@ -15,5 +24,7 @@ public AppendText(PageLocation pl_, String appendText_, String editSummary_) {
1524
values.add(editSummary_);
1625
keys.add("bot");
1726
values.add("true");
27+
28+
enforceMWVersion("1.13");
1829
}
1930
}

src/main/java/WikiBot/APIcommands/DeletePage.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@
33
import WikiBot.ContentRep.PageLocation;
44

55
/**
6-
* This action requires the "delete" right.
6+
* This command deletes a page.
7+
*
8+
* Rights required:
9+
* delete
10+
*
11+
* MW version required:
12+
* 1.12+
713
*/
814
public class DeletePage extends APIcommand {
915

@@ -15,5 +21,7 @@ public DeletePage(PageLocation pl_, String editSummary_) {
1521
values.add(getTitle());
1622
keys.add("reason");
1723
values.add(editSummary_);
24+
25+
enforceMWVersion("1.12");
1826
}
1927
}

src/main/java/WikiBot/APIcommands/EditPage.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22

33
import WikiBot.ContentRep.PageLocation;
44

5+
/**
6+
* This command edits a page, or creates one if the
7+
* page location does not exist.
8+
*
9+
* Rights required:
10+
* edit
11+
*
12+
* MW version required:
13+
* 1.13+
14+
*/
515
public class EditPage extends APIcommand {
616

717
public EditPage(PageLocation pl_, String text_, String editSummary_) {
@@ -16,5 +26,7 @@ public EditPage(PageLocation pl_, String text_, String editSummary_) {
1626
values.add(editSummary_);
1727
keys.add("bot");
1828
values.add("true");
29+
30+
enforceMWVersion("1.13");
1931
}
2032
}

src/main/java/WikiBot/APIcommands/EditSection.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,24 @@
22

33
import WikiBot.ContentRep.PageLocation;
44

5+
/**
6+
* This command edits a page section.
7+
*
8+
* Rights required:
9+
* edit
10+
*
11+
* MW version required:
12+
* 1.13+
13+
*/
514
public class EditSection extends APIcommand {
615

16+
/**
17+
* Edit a section on the page.
18+
* @param pl_ The page.
19+
* @param sectionID_ 0 for the top section. "new" to add a new section.
20+
* @param text_ Section content.
21+
* @param editSummary_
22+
*/
723
public EditSection(PageLocation pl_, int sectionID_, String text_, String editSummary_) {
824
super("Edit section", pl_, true, "edit", "csrf");
925
keys.add("action");
@@ -18,5 +34,7 @@ public EditSection(PageLocation pl_, int sectionID_, String text_, String editSu
1834
values.add(editSummary_);
1935
keys.add("bot");
2036
values.add("true");
37+
38+
enforceMWVersion("1.13");
2139
}
2240
}

src/main/java/WikiBot/APIcommands/MovePage.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,16 @@
33
import WikiBot.ContentRep.PageLocation;
44

55
/**
6-
* To move a page and leave no redirect behind, the suppressredirect right is needed. By default, this is only given to bots and sysops.
6+
* This command moves a page.
7+
*
8+
* Rights required:
9+
* move
10+
* movefile (To move files.)
11+
* move-subpages (To move subpages.)
12+
* suppressredirect (To move a page and leave no redirect behind.)
13+
*
14+
* MW version required:
15+
* 1.12+
716
*/
817
public class MovePage extends APIcommand {
918

@@ -31,6 +40,8 @@ public MovePage(PageLocation from_, PageLocation to_, String editSummary_, boole
3140
keys.add("noredirect");
3241
values.add(null);
3342
}
43+
44+
enforceMWVersion("1.12");
3445
}
3546

3647
/**
@@ -48,5 +59,7 @@ public MovePage(PageLocation from_, PageLocation to_, String editSummary_) {
4859
values.add(editSummary_);
4960
keys.add("format");
5061
values.add("xml");
62+
63+
enforceMWVersion("1.12");
5164
}
5265
}

src/main/java/WikiBot/APIcommands/Query/QueryAllPages.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,30 @@
11
package WikiBot.APIcommands.Query;
22

3-
/*
4-
* APnamespace - The id associated with a namespace in a wiki.
3+
/**
4+
* This command gets a list of all pages on the wiki.
5+
*
6+
* Rights required:
7+
* none
8+
*
9+
* MW version required:
10+
* all
511
*/
6-
712
public class QueryAllPages extends QueryList {
13+
/**
14+
* This constructor assumes the main namespace.
15+
* @param language The wiki language.
16+
* @param depth How many pages to get.
17+
*/
818
public QueryAllPages(String language, int depth) {
919
super("Query all pages", language, "allpages");
1020
keys.add("aplimit");
1121
values.add("" + depth);
1222
unescapeText = true;
1323
}
1424

25+
/*
26+
* Use for continuing large queries.
27+
*/
1528
public QueryAllPages(String language, int depth, String apcontinue) {
1629
super("Query all pages", language, "allpages");
1730
keys.add("aplimit");
@@ -22,6 +35,12 @@ public QueryAllPages(String language, int depth, String apcontinue) {
2235
unescapeHTML = false;
2336
}
2437

38+
/**
39+
*
40+
* @param language The wiki language.
41+
* @param depth How many pages to get.
42+
* @param apcontinue The namespace id to query.
43+
*/
2544
public QueryAllPages(String language, int depth, int apnamespace) {
2645
super("Query all pages", language, "allpages");
2746
keys.add("aplimit");
@@ -32,6 +51,9 @@ public QueryAllPages(String language, int depth, int apnamespace) {
3251
unescapeHTML = false;
3352
}
3453

54+
/*
55+
* Use for continuing large queries.
56+
*/
3557
public QueryAllPages(String language, int depth, String apcontinue, int apnamespace) {
3658
super("Query all pages", language, "allpages");
3759
keys.add("aplimit");

0 commit comments

Comments
 (0)