MediawikiConnection.java

From Knot Atlas
Revision as of 00:17, 16 August 2005 by Scott (talk | contribs)
Jump to navigationJump to search
package mediawiki;

import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

/**
 * @author Scott Morrison
 * copyright 2005, available under either the MIT or GPL license.
 */
public class MediawikiConnection {
	
	private String baseURL;
	private HttpClient client = new HttpClient();
	
	public MediawikiConnection(String baseURL) {
		this.baseURL = baseURL;
	}
	
	public MediawikiConnection(String baseURL, String username, String password) {
		this.baseURL = baseURL;
		doLogin(username, password);
	}
	
	private boolean doLogin(String username, String password) {
		PostMethod post = new PostMethod(baseURL + "?title=Special:Userlogin&action=submit");
		
		// enable automatic retrying
		post.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, 
	    		new DefaultHttpMethodRetryHandler(3, false));
		
		NameValuePair[] data = {
			new NameValuePair("wpName", username),
		    new NameValuePair("wpPassword", password),
		    new NameValuePair("wpRemember", "1"),
		    new NameValuePair("wpLoginAttempt", "1")
		};
		
		post.setRequestBody(data);
		
		int status;
		try {
			status = client.executeMethod(post);
		} catch (HttpException e) {
			return false;
		} catch (IOException e) {
			return false;
		} finally {
			post.releaseConnection();
		}
		return (status == 302);
	}

	public String getPageText(String title) throws HttpException, IOException, JDOMException {
		String URL = baseURL + "?title=Special:Export/" + title;
		GetMethod get = new GetMethod(URL);
		
		// enable automatic retrying
		get.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, 
	    		new DefaultHttpMethodRetryHandler(3, false));
		
		client.executeMethod(get);
		InputStream is = get.getResponseBodyAsStream();
		
		SAXBuilder builder = new SAXBuilder();
		Document doc = builder.build(is);
		Element text = doc.getRootElement().getChild("page").getChild("revision").getChild("text");

		// finally, make sure we release the connection!
		get.releaseConnection();
		
		return text.getText();
	}
	
	public boolean setPageText(String title, String text) throws IOException {
		return setPageText(title, text, "");		
	}
	
	public boolean setPageText(String title, String text, String summary) throws IOException {
		String URL = baseURL + "?title=" + title + "&action=edit";
		
		// first, load the edit page, to get an 'edittime'
		String editTime = "";
		GetMethod get = new GetMethod(URL);
		// enable automatic retrying
		get.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, 
	    		new DefaultHttpMethodRetryHandler(3, false));
		client.executeMethod(get);
		
		// now sift through the response looking for the editime
		String response = get.getResponseBodyAsString();
		get.releaseConnection();
		java.util.regex.Pattern regex = java.util.regex.Pattern.compile(
				"value=\"([0-9]+)\" name=\"wpEdittime\"");
		java.util.regex.Matcher matcher = regex.matcher(response);
		if(matcher.find()) editTime = matcher.group(1);
		
		PostMethod post = new PostMethod(URL);
			
		NameValuePair[] data = {
			new NameValuePair("wpTextbox1", text),
			new NameValuePair("wpEdittime", editTime),
		    new NameValuePair("wpSummary", summary)
		};
			
		post.setRequestBody(data);
		
		int status;
		try {
			status = client.executeMethod(post);
		} catch (HttpException e) {
			return false;
		} catch (IOException e) {
			return false;
		} finally {
			post.releaseConnection();
		}
	
		return (status == 302);
	}
	
	public static class Test {
		public static void main(String[] args) throws HttpException, IOException, JDOMException {
			MediawikiConnection conn = new MediawikiConnection(
					"http://math.berkeley.edu/~scott/w/index.php", "TestRobot", "foobar");
			System.out.println(conn.getPageText("Main_Page"));
			System.out.println(conn.setPageText("foo", "crel", "auto!"));
		}
	}
}