1 package net.sourceforge.phpeclipse.wiki.actions.mediawiki;
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.io.StringReader;
7 import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin;
9 import org.apache.commons.httpclient.ConnectMethod;
10 import org.apache.commons.httpclient.HttpConnection;
11 import org.apache.commons.httpclient.HttpMethod;
12 import org.apache.commons.httpclient.HttpState;
13 import org.apache.commons.httpclient.HttpStatus;
14 import org.apache.commons.httpclient.URI;
15 import org.apache.commons.httpclient.UsernamePasswordCredentials;
16 import org.apache.commons.httpclient.methods.GetMethod;
17 import org.apache.commons.httpclient.protocol.Protocol;
20 * This class gets the wikitext from a wikipedia edit page
22 * The basic coding was copied from the commons-httpclient example <code>MediaWikiConnector.java</code>
24 public class MediaWikiConnector {
27 * Get the text of a wikimedia Wiki-Description from <code>en.wikipedia.org</code>
30 public static String getWikiText(String wikiDescriptor) {
31 return getWikiText(wikiDescriptor, null);
35 * Get the text of a wikimedia Wiki-Description
38 public static String getWikiText(String wikiname, String urlStr) {
40 // http://en.wikipedia.org/w/wiki.phtml?title=Main_Page&action=edit
41 // http://en.wikibooks.org/w/wiki.phtml?title=Programming:PHP:SQL_Injection&action=edit
42 // http://en.wikipedia.org/w/wiki.phtml?title=Talk:Division_by_zero&action=edit
43 HttpMethod method = null;
46 urlStr = "http://en.wikipedia.org/w/wiki.phtml?title=" + wikiname + "&action=edit";
49 // urlStr = urlStr + "?title=" + wikiname + "&action=edit";
51 URI uri = new URI(urlStr.toCharArray());
53 String schema = uri.getScheme();
54 if ((schema == null) || (schema.equals(""))) {
57 Protocol protocol = Protocol.getProtocol(schema);
59 HttpState state = new HttpState();
61 method = new GetMethod(uri.toString());
62 String host = uri.getHost();
63 int port = uri.getPort();
65 HttpConnection connection = new HttpConnection(host, port, protocol);
67 connection.setProxyHost(System.getProperty("http.proxyHost"));
68 connection.setProxyPort(Integer.parseInt(System.getProperty("http.proxyPort", "80")));
70 if (System.getProperty("http.proxyUserName") != null) {
71 state.setProxyCredentials(null, null, new UsernamePasswordCredentials(System.getProperty("http.proxyUserName"), System
72 .getProperty("http.proxyPassword")));
75 if (connection.isProxied() && connection.isSecure()) {
76 method = new ConnectMethod(method);
79 method.execute(state, connection);
81 if (method.getStatusCode() == HttpStatus.SC_OK) {
82 // get the textareas wiki text now:
83 InputStream stream = method.getResponseBodyAsStream();
84 int byteLen = stream.available();
86 byte[] buffer = new byte[byteLen];
87 stream.read(buffer, 0, byteLen);
88 String wikiText = new String(buffer);
89 // String wikiText = method.getResponseBodyAsString();
90 int start = wikiText.indexOf("<textarea");
92 start = wikiText.indexOf(">", start + 1);
94 int end = wikiText.indexOf("</textarea>");
95 wikiText = wikiText.substring(start + 1, end);
99 // System.out.println(wikiText);
102 } catch (Exception e) {
105 if (method != null) {
106 method.releaseConnection();
109 return null; // no success in getting wiki text