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 wikiDescriptor, 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=" + wikiDescriptor + "&action=edit";
48 urlStr = urlStr + "?title=" + wikiDescriptor + "&action=edit";
50 URI uri = new URI(urlStr.toCharArray());
52 String schema = uri.getScheme();
53 if ((schema == null) || (schema.equals(""))) {
56 Protocol protocol = Protocol.getProtocol(schema);
58 HttpState state = new HttpState();
60 method = new GetMethod(uri.toString());
61 String host = uri.getHost();
62 int port = uri.getPort();
64 HttpConnection connection = new HttpConnection(host, port, protocol);
66 connection.setProxyHost(System.getProperty("http.proxyHost"));
67 connection.setProxyPort(Integer.parseInt(System.getProperty("http.proxyPort", "80")));
69 if (System.getProperty("http.proxyUserName") != null) {
70 state.setProxyCredentials(null, null, new UsernamePasswordCredentials(System.getProperty("http.proxyUserName"), System
71 .getProperty("http.proxyPassword")));
74 if (connection.isProxied() && connection.isSecure()) {
75 method = new ConnectMethod(method);
78 method.execute(state, connection);
80 if (method.getStatusCode() == HttpStatus.SC_OK) {
81 // get the textareas wiki text now:
82 InputStream stream = method.getResponseBodyAsStream();
83 int byteLen = stream.available();
85 byte[] buffer = new byte[byteLen];
86 stream.read(buffer, 0, byteLen);
87 String wikiText = new String(buffer);
88 // String wikiText = method.getResponseBodyAsString();
89 int start = wikiText.indexOf("<textarea");
91 start = wikiText.indexOf(">", start + 1);
93 int end = wikiText.indexOf("</textarea>");
94 wikiText = wikiText.substring(start + 1, end);
98 // System.out.println(wikiText);
101 } catch (Exception e) {
104 if (method != null) {
105 method.releaseConnection();
108 return null; // no success in getting wiki text