77import WikiBot .Errors .UnsupportedError ;
88import WikiBot .MediawikiData .MediawikiDataManager ;
99import 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+ */
1217public 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 ) {
0 commit comments