1 package net.sourceforge.phpeclipse.wiki.actions.mediawiki;
3 import java.io.InputStream;
5 import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin;
7 import org.apache.commons.httpclient.ConnectMethod;
8 import org.apache.commons.httpclient.HttpConnection;
9 import org.apache.commons.httpclient.HttpMethod;
10 import org.apache.commons.httpclient.HttpState;
11 import org.apache.commons.httpclient.HttpStatus;
12 import org.apache.commons.httpclient.URI;
13 import org.apache.commons.httpclient.UsernamePasswordCredentials;
14 import org.apache.commons.httpclient.methods.GetMethod;
15 import org.apache.commons.httpclient.protocol.Protocol;
18 * This class gets the wikitext from a wikipedia edit page
20 * The basic coding was copied from the commons-httpclient example <code>MediaWikiConnector.java</code>
22 public class MediaWikiConnector {
25 * Get the text of a wikimedia Wiki-Description from <code>en.wikipedia.org</code>
28 public static String getWikiRawText(String wikiDescriptor) {
29 return getWikiRawText(wikiDescriptor, null);
33 * Get the text of a wikimedia Wiki-Description
36 public static String getWikiRawText(String wikiname, String urlStr) {
38 // http://en.wikipedia.org/w/wiki.phtml?title=Main_Page&action=raw
39 // http://en.wikibooks.org/w/index.php?title=Programming:PHP:SQL_Injection&action=raw
40 // http://en.wikipedia.org/w/wiki.phtml?title=Talk:Division_by_zero&action=raw
41 HttpMethod method = null;
44 WikiEditorPlugin.getDefault().reportError("No Wikipedia URL configured", "URL-String == null");
45 // urlStr = "http://en.wikipedia.org/w/wiki.phtml?title=" + wikiname + "&action=raw";
47 URI uri = new URI(urlStr.toCharArray());
49 String schema = uri.getScheme();
50 if ((schema == null) || (schema.equals(""))) {
53 Protocol protocol = Protocol.getProtocol(schema);
55 HttpState state = new HttpState();
57 method = new GetMethod(uri.toString());
58 String host = uri.getHost();
59 int port = uri.getPort();
61 HttpConnection connection = new HttpConnection(host, port, protocol);
62 // timeout after 30 seconds
63 connection.setConnectionTimeout(30000);
64 connection.setProxyHost(System.getProperty("http.proxyHost"));
65 connection.setProxyPort(Integer.parseInt(System.getProperty("http.proxyPort", "80")));
67 if (System.getProperty("http.proxyUserName") != null) {
68 state.setProxyCredentials(null, null, new UsernamePasswordCredentials(System.getProperty("http.proxyUserName"), System
69 .getProperty("http.proxyPassword")));
72 if (connection.isProxied() && connection.isSecure()) {
73 method = new ConnectMethod(method);
76 method.execute(state, connection);
78 if (method.getStatusCode() == HttpStatus.SC_OK) {
79 // get the wiki text now:
80 String wikiText = method.getResponseBodyAsString();
82 // wrong text not always complete
83 // InputStream stream = method.getResponseBodyAsStream();
84 // int byteLen = stream.available();
86 // byte[] buffer = new byte[byteLen];
88 // stream.read(buffer, 0, byteLen);
89 // String wikiText = new String(buffer);
91 // System.out.println(wikiText);
93 } catch (Throwable e) {
94 WikiEditorPlugin.log(e);
95 WikiEditorPlugin.getDefault().reportError("Exception occured", e.getMessage() + "\nSee stacktrace in /.metadata/.log file.");
98 method.releaseConnection();
101 return null; // no success in getting wiki text
104 public static String getWikiEditTextarea(String wikiname, String urlStr) {
106 // http://en.wikipedia.org/w/wiki.phtml?title=Main_Page&action=edit
107 // http://en.wikibooks.org/w/wiki.phtml?title=Programming:PHP:SQL_Injection&action=edit
108 // http://en.wikipedia.org/w/wiki.phtml?title=Talk:Division_by_zero&action=edit
109 HttpMethod method = null;
111 if (urlStr == null) {
112 urlStr = "http://en.wikipedia.org/w/wiki.phtml?title=" + wikiname + "&action=edit";
115 // urlStr = urlStr + "?title=" + wikiname + "&action=edit";
117 URI uri = new URI(urlStr.toCharArray());
119 String schema = uri.getScheme();
120 if ((schema == null) || (schema.equals(""))) {
123 Protocol protocol = Protocol.getProtocol(schema);
125 HttpState state = new HttpState();
127 method = new GetMethod(uri.toString());
128 String host = uri.getHost();
129 int port = uri.getPort();
131 HttpConnection connection = new HttpConnection(host, port, protocol);
133 connection.setProxyHost(System.getProperty("http.proxyHost"));
134 connection.setProxyPort(Integer.parseInt(System.getProperty("http.proxyPort", "80")));
136 if (System.getProperty("http.proxyUserName") != null) {
137 state.setProxyCredentials(null, null, new UsernamePasswordCredentials(System.getProperty("http.proxyUserName"), System
138 .getProperty("http.proxyPassword")));
141 if (connection.isProxied() && connection.isSecure()) {
142 method = new ConnectMethod(method);
145 method.execute(state, connection);
147 if (method.getStatusCode() == HttpStatus.SC_OK) {
148 // get the textareas wiki text now:
149 InputStream stream = method.getResponseBodyAsStream();
150 int byteLen = stream.available();
152 byte[] buffer = new byte[byteLen];
153 stream.read(buffer, 0, byteLen);
154 String wikiText = new String(buffer);
155 // String wikiText = method.getResponseBodyAsString();
156 int start = wikiText.indexOf("<textarea");
158 start = wikiText.indexOf(">", start + 1);
160 int end = wikiText.indexOf("</textarea>");
161 wikiText = wikiText.substring(start + 1, end);
165 // System.out.println(wikiText);
168 } catch (Exception e) {
171 if (method != null) {
172 method.releaseConnection();
175 return null; // no success in getting wiki text