".*<form[^>]*\\sid=\"editform\"[^>]*title=(.*?)&[^>]*>" + ".*<textarea[^>]*\\sname=\"wpTextbox1\"[^>]*>(.*?)</textarea>"
+ ".*<input[^>]*\\svalue=\"(\\d*)\"[^>]*\\sname=\"wpEdittime\"[^>]*>" + ".*", Pattern.DOTALL);
+ // <input type='hidden' value="53ee6d8b42ff9b7d" name="wpEditToken" />
+ private static final Pattern EDIT_TOKEN = Pattern.compile(
+ ".*<input\\stype='hidden'\\svalue=\"(.*?)\"\\sname=\"wpEditToken\"\\s/>.*", Pattern.DOTALL);
+
//setup default user agent
final static public String userAgent = "plog4u.org/0.0";
// log(method);
if (responseCode == 302 && responseBody.length() == 0 || responseCode == 200
- && responseBody.matches(config.getLoginSuccess())) {
+ && responseBody.matches(config.getLogoutSuccess())) {
// config.getloggedIn = false;
result = true;
} else if (responseCode == 200) {
return result;
}
+ /** parses a returned editform for the sessions wpEditToken */
+ private String parseEditToken(String charSet, String responseBody) throws PageNotEditableException {
+ Matcher matcher = EDIT_TOKEN.matcher(responseBody);
+ if (!matcher.matches()) {
+ return null;
+ }
+
+ return matcher.group(1);
+ }
+
+ /**
+ * returns the edit token or <code>null</code> if no token is available
+ *
+ * @param actionURL
+ * @param charSet
+ * @param title
+ * @return
+ * @throws UnexpectedAnswerException
+ * @throws MethodException
+ * @throws PageNotEditableException
+ */
+ public String loadEditToken(String actionURL, String charSet, String title) throws UnexpectedAnswerException, MethodException,
+ PageNotEditableException {
+ GetMethod method = new GetMethod(actionURL);
+ method.setFollowRedirects(false);
+ method.addRequestHeader("User-Agent", userAgent);
+ NameValuePair[] params = new NameValuePair[] { new NameValuePair("title", title), new NameValuePair("action", "edit") };
+ method.setQueryString(EncodingUtil.formUrlEncode(params, charSet));
+
+ try {
+ int responseCode = client.executeMethod(method);
+ String responseBody = method.getResponseBodyAsString();
+ // log(method);
+
+ if (responseCode == 200) {
+ String parsed = parseEditToken(charSet, responseBody);
+ if (parsed != null && parsed.length() == 0) {
+ return null;
+ }
+ return parsed;
+ } else {
+ throw new UnexpectedAnswerException("load not successful: expected 200 OK, got " + method.getStatusLine());
+ }
+ } catch (HttpException e) {
+ throw new MethodException("method failed", e);
+ } catch (IOException e) {
+ throw new MethodException("method failed", e);
+ } finally {
+ method.releaseConnection();
+ }
+ }
+
/** parses a returned editform into a Content object with UNIX-EOLs ("\n") */
private Parsed parseBody(String charSet, String responseBody) throws PageNotEditableException, UnsupportedEncodingException {
Matcher matcher = BODY_PATTERN.matcher(responseBody);
String title = matcher.group(1);
String body = matcher.group(2);
String timestamp = matcher.group(3);
-
+ String tokenEdit = null;
+ // String tokenEdit = matcher.group(4);
title = URLDecoder.decode(title, charSet);
body = body.replaceAll(""", "\"").replaceAll("'", "'").replaceAll("<", "<").replaceAll(">", ">").replaceAll(
"&", "&").replaceAll("\r\n", "\n").replace('\r', '\n');
- return new Parsed(timestamp, title, body);
+ return new Parsed(timestamp, title, body, tokenEdit);
}
/** load a Page Version - returns a Loaded Object */
return result;
}
+ public Loaded loadCategory(String actionURL, String charSet, String title) throws UnexpectedAnswerException, MethodException,
+ PageNotEditableException {
+ GetMethod method = new GetMethod(actionURL);
+ method.setFollowRedirects(false);
+ method.addRequestHeader("User-Agent", userAgent);
+ NameValuePair[] params = new NameValuePair[] { new NameValuePair("title", title) };
+ method.setQueryString(EncodingUtil.formUrlEncode(params, charSet));
+
+ Loaded result;
+ try {
+ int responseCode = client.executeMethod(method);
+ String responseBody = method.getResponseBodyAsString();
+ // log(method);
+
+ if (responseCode == 200) {
+ Parsed parsed = parseBody(charSet, responseBody);
+ Content content = new Content(parsed.timestamp, parsed.body);
+ result = new Loaded(actionURL, charSet, parsed.title, content);
+ } else {
+ throw new UnexpectedAnswerException("load not successful: expected 200 OK, got " + method.getStatusLine());
+ }
+ } catch (HttpException e) {
+ throw new MethodException("method failed", e);
+ } catch (IOException e) {
+ throw new MethodException("method failed", e);
+ } finally {
+ method.releaseConnection();
+ }
+ return result;
+ }
+
public ArrayList loadXML(IWikipedia config, String actionURL, String pages) throws UnexpectedAnswerException, MethodException,
InterruptedException {
storeThrottle.delay();
* @throws PageNotEditableException
* @throws InterruptedException
*/
- public Stored store(IWikipedia config, String actionUrl, String title, Content content, String summary, boolean minorEdit,
- boolean watchThis) throws UnexpectedAnswerException, MethodException, PageNotEditableException, InterruptedException {
+ public Stored store(IWikipedia config, String editToken, String actionUrl, String title, Content content, String summary,
+ boolean minorEdit, boolean watchThis) throws UnexpectedAnswerException, MethodException, PageNotEditableException,
+ InterruptedException {
//### workaround: prevent too many stores at a time
storeThrottle.delay();
PostMethod method = new PostMethod(actionUrl);
-
+
method.setFollowRedirects(false);
method.addRequestHeader("User-Agent", userAgent);
method.addRequestHeader("Content-Type", PostMethod.FORM_URL_ENCODED_CONTENT_TYPE + "; charset=" + config.getCharSet());
+ if (editToken == null) {
+ // in some versions editToken isn't supported
+ editToken = " ";
+ }
NameValuePair[] params = new NameValuePair[] {
// new NameValuePair("wpSection", ""),
// new NameValuePair("wpPreview", "Vorschau zeigen"),
new NameValuePair("wpTextbox1", content.body),
new NameValuePair("wpEdittime", content.timestamp),
new NameValuePair("wpSummary", summary),
+ new NameValuePair("wpEditToken", editToken),
new NameValuePair("wpSave", "yes"),
new NameValuePair("action", "submit") };
method.addParameters(params);
try {
// timeout after xx seconds
connection.setConnectionTimeout(Integer.parseInt(timeout));
-
+
if (proxyHost.length() > 0) {
String proxyPort = prefs.getString(WikiEditorPlugin.HTTP_PROXYPORT);
connection.setProxyHost(proxyHost);
state.setProxyCredentials(null, null, new UsernamePasswordCredentials(proxyUserName, proxyPassWord));
}
}
-
+
} catch (Exception e) {
}
}
private void setHTTPClientParameters(HttpClient client) {
-
+
Preferences prefs = WikiEditorPlugin.getDefault().getPluginPreferences();
String timeout = prefs.getString(WikiEditorPlugin.HTTP_TIMEOUT);
String proxyHost = prefs.getString(WikiEditorPlugin.HTTP_PROXYHOST);
try {
// timeout after xx seconds
client.setConnectionTimeout(Integer.parseInt(timeout));
-
+
if (proxyHost.length() > 0) {
String proxyPort = prefs.getString(WikiEditorPlugin.HTTP_PROXYPORT);
HostConfiguration conf = new HostConfiguration();
- client.setHostConfiguration(conf);
+ client.setHostConfiguration(conf);
conf.setProxy(proxyHost, Integer.parseInt(proxyPort));
String proxyUserName = prefs.getString(WikiEditorPlugin.HTTP_PROXYUSERNAME);
client.setState(state);
}
}
-
+
} catch (Exception e) {
}
-
+
}
+
public static void main(String[] args) {
MediaWikiConnector mwc = new MediaWikiConnector();
try {