Skip to content

Commit 860dca8

Browse files
authored
Merge pull request #61 from Choco31415/development
General Fixes
2 parents 09eaa00 + 290a0ed commit 860dca8

6 files changed

Lines changed: 132 additions & 82 deletions

File tree

src/main/java/WikiBot/ContentRep/PageLocation.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,19 @@ public boolean equals(Object obj) {
5454
}
5555
if (obj.getClass().equals(PageLocation.class)) {
5656
PageLocation pg = (PageLocation)obj;
57-
return pg.getTitle().equalsIgnoreCase(titleObject.getTitle()) && lan.equals(pg.getLanguage());
57+
boolean equal = true;
58+
equal = equal & lan.equals(pg.getLanguage());
59+
equal = equal & pg.getTitleObject().equals(titleObject);
60+
return equal;
5861
}
5962
return false;
6063
}
6164

65+
@Override
66+
public int hashCode() {
67+
return lan.hashCode() + getTitle().hashCode();
68+
}
69+
6270
@Override
6371
public String toString() {
6472
return "PageLocation: " + lan + ": " + titleObject.getTitle();

src/main/java/WikiBot/ContentRep/PageTitle.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,23 @@ public PageTitle(String title_) {
2828
public String getNameSpace() { return nameSpace; }
2929
public String getTitleWithoutNameSpace() { return titleWithoutNameSpace; }
3030

31+
@Override
32+
public boolean equals(Object obj) {
33+
if (obj == null) {
34+
return false;
35+
}
36+
if (obj.getClass().equals(PageLocation.class)) {
37+
PageTitle pt = (PageTitle)obj;
38+
return getTitle().substring(0).equalsIgnoreCase(pt.getTitle().substring(0));
39+
}
40+
return false;
41+
}
42+
43+
@Override
44+
public int hashCode() {
45+
return title.hashCode();
46+
}
47+
3148
@Override
3249
public String toString() {
3350
return "\nPageTitle: " + title;

src/main/java/WikiBot/Core/GenericBot.java

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public SimplePage getWikiSimplePage(PageLocation loc) {
164164
public boolean doesPageExist(PageLocation loc) {
165165
JsonNode serverOutput = getWikiPageJsonCode(loc);
166166

167-
return serverOutput.findValue("missing") == null; // If contains missing tag, the page is missing.
167+
return serverOutput.findValue("missing") == null && serverOutput.findValue("invalid") == null; // If contains missing tag, the page is missing.
168168
}
169169

170170
private JsonNode getWikiPageJsonCode(PageLocation loc) {
@@ -351,7 +351,8 @@ private void getPageRevisions(Page page) {
351351
public ArrayList<Revision> getPastRevisions(PageLocation loc, int localRevisionDepth, boolean getContent) {
352352
ArrayList<Revision> toReturn = new ArrayList<>();
353353

354-
String serverOutput = APIcommand(new QueryPageRevisions(loc, Math.min(localRevisionDepth, APIlimit), getContent));
354+
int revDepth = Math.min(localRevisionDepth, APIlimit);
355+
String serverOutput = APIcommand(new QueryPageRevisions(loc, revDepth, getContent));
355356

356357

357358
// Read in the Json!!!
@@ -416,7 +417,8 @@ public ArrayList<Revision> getRecentChanges(String language, int depth) {
416417

417418
int revisionsNeeded = depth;
418419
do {
419-
int batchSize = Math.min(Math.min(30, APIlimit), revisionsNeeded);
420+
revisionsNeeded = depth - toReturn.size();
421+
int batchSize = Math.min(APIlimit, revisionsNeeded);
420422

421423
//Make a query call.
422424
String serverOutput;
@@ -465,8 +467,6 @@ public ArrayList<Revision> getRecentChanges(String language, int depth) {
465467
toReturn.add(revision);
466468
}
467469

468-
revisionsNeeded -= batchSize;
469-
470470
//Try continuing the query.
471471
if (rootNode.findValue("rccontinue") != null) {
472472
rccontinue = rootNode.findValue("rccontinue").asText();
@@ -475,7 +475,7 @@ public ArrayList<Revision> getRecentChanges(String language, int depth) {
475475
} else {
476476
rccontinue = null;
477477
}
478-
} while (rccontinue != null && revisionsNeeded != 0);
478+
} while (rccontinue != null && toReturn.size() < depth);
479479

480480
return toReturn;
481481
}
@@ -618,9 +618,10 @@ public ArrayList<PageLocation> getPagesThatLinkTo(PageLocation loc, int depth) {
618618

619619
logFine("Getting pages that link to: " + loc.getTitle());
620620

621-
int backlinksNeeded = depth;
621+
int backlinksNeeded;
622622
do {
623623
//Make a query call.
624+
backlinksNeeded = depth - toReturn.size();
624625
int batchSize = Math.min(Math.min(30, APIlimit), backlinksNeeded);
625626

626627
String serverOutput;
@@ -651,8 +652,6 @@ public ArrayList<PageLocation> getPagesThatLinkTo(PageLocation loc, int depth) {
651652
toReturn.add(backlinkLoc);
652653
}
653654

654-
backlinksNeeded -= batchSize;
655-
656655
//Try continuing the query.
657656
if (rootNode.findValue("blcontinue") != null) {
658657
blcontinue = rootNode.findValue("blcontinue").asText();
@@ -661,7 +660,7 @@ public ArrayList<PageLocation> getPagesThatLinkTo(PageLocation loc, int depth) {
661660
} else {
662661
blcontinue = null;
663662
}
664-
} while (blcontinue != null && backlinksNeeded != 0);
663+
} while (blcontinue != null && toReturn.size() < depth);
665664

666665
return toReturn;
667666
}
@@ -705,26 +704,28 @@ public ArrayList<PageLocation> getAllPages(String language, int depth, String fr
705704

706705
do {
707706
//Make query call.
707+
int pagesNeeded = toReturn.size() - depth;
708+
int batchSize = Math.min(APIlimit, pagesNeeded);
708709
String serverOutput;
709710
if (apnamespace == null) {
710711
if (apcontinue == null) {
711712
if (from != null) {
712-
serverOutput = APIcommand(new QueryAllPages(language, APIlimit, from));
713+
serverOutput = APIcommand(new QueryAllPages(language, batchSize, from));
713714
} else {
714-
serverOutput = APIcommand(new QueryAllPages(language, APIlimit));
715+
serverOutput = APIcommand(new QueryAllPages(language, batchSize));
715716
}
716717
} else {
717-
serverOutput = APIcommand(new QueryAllPages(language, APIlimit, apcontinue));
718+
serverOutput = APIcommand(new QueryAllPages(language, batchSize, apcontinue));
718719
}
719720
} else {
720721
if (apcontinue == null) {
721722
if (from != null) {
722-
serverOutput = APIcommand(new QueryAllPages(language, APIlimit, from, apnamespace));
723+
serverOutput = APIcommand(new QueryAllPages(language, batchSize, from, apnamespace));
723724
} else {
724-
serverOutput = APIcommand(new QueryAllPages(language, APIlimit, apnamespace));
725+
serverOutput = APIcommand(new QueryAllPages(language, batchSize, apnamespace));
725726
}
726727
} else {
727-
serverOutput = APIcommand(new QueryAllPages(language, APIlimit, apcontinue, apnamespace));
728+
serverOutput = APIcommand(new QueryAllPages(language, batchSize, apcontinue, apnamespace));
728729
}
729730
}
730731

@@ -757,7 +758,7 @@ public ArrayList<PageLocation> getAllPages(String language, int depth, String fr
757758
} else {
758759
apcontinue = null;
759760
}
760-
} while (apcontinue != null && toReturn.size() != depth);
761+
} while (apcontinue != null && toReturn.size() < depth);
761762

762763
return toReturn;
763764
}
@@ -1188,7 +1189,7 @@ public ArrayList<ArrayList<Revision>> getUserContribs(ArrayList<User> users, int
11881189
logFine(userLogMessage);
11891190

11901191
//Method code below
1191-
ArrayList<ArrayList<Revision>> multiContribs = new ArrayList<ArrayList<Revision>>();
1192+
ArrayList<ArrayList<Revision>> multiContribs = new ArrayList<>();
11921193

11931194
String language = users.get(0).getLanguage();
11941195

@@ -1206,17 +1207,13 @@ public ArrayList<ArrayList<Revision>> getUserContribs(ArrayList<User> users, int
12061207
User user = users.get(u);
12071208

12081209
//Query user's contributions.
1209-
int rev = 0;
1210+
multiContribs.add(new ArrayList<Revision>());
12101211
boolean moreRevisionsExist = true;
12111212
String queryContinue = null; // User for continuing queries.
1212-
while (rev < depth && moreRevisionsExist) {
1213+
while (multiContribs.get(u).size() < depth && moreRevisionsExist) {
12131214
//Query the server.
1214-
int querySize = -1;
1215-
if (rev + maxQuerySize < depth) {
1216-
querySize = maxQuerySize;
1217-
} else {
1218-
querySize = depth - rev;
1219-
}
1215+
int querySize = Math.min(maxQuerySize, depth - multiContribs.get(u).size());
1216+
12201217
APIcommand queryUserContribs = new QueryUserContribs(user, properties, querySize);
12211218
if (queryContinue != null) {
12221219
queryUserContribs.addParameter("ucstart", queryContinue);
@@ -1236,8 +1233,6 @@ public ArrayList<ArrayList<Revision>> getUserContribs(ArrayList<User> users, int
12361233
}
12371234

12381235
//Parse Json for contribs
1239-
ArrayList<Revision> contribs = new ArrayList<Revision>();
1240-
12411236
JsonNode queryNode = rootNode.findValue("query");
12421237
JsonNode userContribs = queryNode.findValue("usercontribs");
12431238

@@ -1265,13 +1260,9 @@ public ArrayList<ArrayList<Revision>> getUserContribs(ArrayList<User> users, int
12651260
}
12661261

12671262
//Package and ship the revision! Then have it sink due to a bunyip and cucumber sandwiches.
1268-
contribs.add(new Revision(loc, userName, comment, date, flags));
1263+
multiContribs.get(u).add(new Revision(loc, userName, comment, date, flags));
12691264
}
12701265

1271-
multiContribs.add(contribs);
1272-
1273-
rev += querySize;
1274-
12751266
//Parse for query continue
12761267
JsonNode queryContinueNode = rootNode.findValue("query-continue");
12771268
if (queryContinueNode == null) {
@@ -1658,13 +1649,13 @@ private String APIcommandHTTP(APIcommand command) {
16581649
}
16591650
}
16601651

1661-
//Follor the url!
1652+
//Follow the url!
16621653
try {
1663-
String[] output = getURL(url, command.shouldUnescapeHTML());
1654+
String output = removeBOM(EntityUtils.toString(getURL(url)));
16641655
if (output == null) {
16651656
throw new NetworkError("Cannot connect to server at: " + baseURL);
16661657
} else {
1667-
return ArrayUtils.compactArray(output, "\n");
1658+
return output;
16681659
}
16691660
} catch (IOException e) {
16701661
throw new Error(e);

src/main/java/WikiBot/Core/NetworkingBase.java

Lines changed: 68 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
package WikiBot.Core;
22

3-
import org.apache.commons.lang3.StringEscapeUtils;
4-
5-
import java.io.BufferedReader;
63
import java.io.IOException;
7-
import java.io.InputStreamReader;
84
import java.io.UnsupportedEncodingException;
95
import java.net.HttpURLConnection;
106
import java.net.MalformedURLException;
@@ -14,21 +10,31 @@
1410
import java.net.URLEncoder;
1511
import java.net.UnknownHostException;
1612
import java.nio.charset.StandardCharsets;
13+
import java.security.KeyManagementException;
14+
import java.security.NoSuchAlgorithmException;
15+
import java.security.SecureRandom;
16+
import java.security.cert.CertificateException;
17+
import java.security.cert.X509Certificate;
1718
import java.util.ArrayList;
1819
import java.util.List;
1920
import java.util.logging.Level;
2021

22+
import javax.net.ssl.SSLContext;
2123
import javax.net.ssl.SSLHandshakeException;
24+
import javax.net.ssl.X509TrustManager;
2225

23-
import org.apache.http.Header;
2426
import org.apache.http.HttpEntity;
2527
import org.apache.http.HttpResponse;
2628
import org.apache.http.NameValuePair;
2729
import org.apache.http.NoHttpResponseException;
2830
import org.apache.http.client.HttpClient;
2931
import org.apache.http.client.entity.UrlEncodedFormEntity;
32+
import org.apache.http.client.methods.HttpGet;
3033
import org.apache.http.client.methods.HttpPost;
3134
import org.apache.http.client.protocol.HttpClientContext;
35+
import org.apache.http.conn.ssl.NoopHostnameVerifier;
36+
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
37+
import org.apache.http.ssl.SSLContexts;
3238
import org.apache.http.cookie.Cookie;
3339
import org.apache.http.impl.client.HttpClientBuilder;
3440
import org.apache.http.message.BasicNameValuePair;
@@ -65,9 +71,53 @@ public class NetworkingBase extends javax.swing.JPanel {
6571
//Special characters.
6672
private static final String UTF8_BOM = "\uFEFF";
6773

74+
// One-off SSL trust manager class.
75+
public class HttpsTrustManager implements X509TrustManager {
76+
@Override
77+
public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
78+
// TODO Auto-generated method stub
79+
80+
}
81+
82+
@Override
83+
public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
84+
// TODO Auto-generated method stub
85+
86+
}
87+
88+
@Override
89+
public X509Certificate[] getAcceptedIssuers() {
90+
// TODO Auto-generated method stub
91+
return new X509Certificate[]{};
92+
}
93+
94+
}
95+
96+
private void setupSSLclient() {
97+
// Handle SSL
98+
SSLContext sslcontext = null;
99+
SSLConnectionSocketFactory factory = null;
100+
101+
102+
try {
103+
sslcontext = SSLContexts.custom().useProtocol("SSL").build();
104+
sslcontext.init(null, new X509TrustManager[]{new HttpsTrustManager()}, new SecureRandom());
105+
factory = new SSLConnectionSocketFactory(sslcontext, new NoopHostnameVerifier());
106+
} catch (KeyManagementException | NoSuchAlgorithmException e) {
107+
// TODO Auto-generated catch block
108+
logError("Could not create SSL context. Entering fail state.");
109+
throw new Error(e.getMessage());
110+
}
111+
112+
113+
114+
// Create client and context for web stuff.
115+
httpclient = HttpClientBuilder.create().setSSLSocketFactory(factory).build();
116+
}
117+
68118
//Instantiation.
69119
public NetworkingBase() {
70-
httpclient = HttpClientBuilder.create().build();
120+
setupSSLclient();
71121
context = HttpClientContext.create();
72122
}
73123

@@ -187,38 +237,22 @@ public void setLogPropagation(boolean set) {
187237
* @param ur The url you want to get.
188238
* @param unescapeHTML4 Unescapes HTML4 text. Ex: & #039;
189239
*/
190-
protected String[] getURL(String ur, boolean unescapeHTML4) throws IOException {
240+
protected HttpEntity getURL(String ur) throws IOException {
191241
logFiner("Loading: " + ur);
242+
243+
//This method actual fetches a web page, and turns it into a more easily use-able format. //This method actual fetches a web page, and turns it into a more easily use-able format.
244+
HttpResponse response = null;
245+
HttpGet httpost = new HttpGet(ur);
192246

193-
//This method actual fetches a web page, and turns it into a more easily use-able format.
194-
URL oracle = null;
247+
// Fetch the url.
195248
try {
196-
oracle = new URL(ur);
197-
} catch (MalformedURLException e) {
198-
System.err.println(e.getMessage());
249+
response = httpclient.execute(httpost, context);
250+
} catch (SocketException|NoHttpResponseException e) {
251+
throw new NetworkError("Cannot connect to server at: " + ur);
252+
} catch (IOException e) {
253+
e.printStackTrace();
199254
}
200-
201-
BufferedReader in = null;
202-
try {
203-
in = new BufferedReader(new InputStreamReader(oracle.openStream(), StandardCharsets.UTF_8));
204-
} catch (IOException e) {
205-
logError("Connection cannot be opened.");
206-
return null;
207-
}
208-
209-
ArrayList<String> page = new ArrayList<String>();
210-
String inputLine;
211-
while ((inputLine = in.readLine()) != null) {
212-
if (unescapeHTML4) {
213-
inputLine = StringEscapeUtils.unescapeHtml4(StringEscapeUtils.unescapeHtml4(inputLine));
214-
}
215-
inputLine = removeBOM(inputLine);
216-
217-
page.add(inputLine);
218-
}
219-
in.close();
220-
221-
return page.toArray(new String[page.size()]);
255+
return response.getEntity();
222256
}
223257

224258
/*

0 commit comments

Comments
 (0)