package net.sourceforge.phpeclipse.wiki.actions.mediawiki; import java.io.InputStream; import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin; import org.apache.commons.httpclient.ConnectMethod; import org.apache.commons.httpclient.HttpConnection; import org.apache.commons.httpclient.HttpMethod; import org.apache.commons.httpclient.HttpState; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.URI; import org.apache.commons.httpclient.UsernamePasswordCredentials; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.protocol.Protocol; /** * This class gets the wikitext from a wikipedia edit page * * The basic coding was copied from the commons-httpclient example MediaWikiConnector.java */ public class MediaWikiConnector { /** * Get the text of a wikimedia Wiki-Description from en.wikipedia.org * */ public static String getWikiRawText(String wikiDescriptor) { return getWikiRawText(wikiDescriptor, null); } /** * Get the text of a wikimedia Wiki-Description * */ public static String getWikiRawText(String wikiname, String urlStr) { // examples // http://en.wikipedia.org/w/wiki.phtml?title=Main_Page&action=raw // http://en.wikibooks.org/w/index.php?title=Programming:PHP:SQL_Injection&action=raw // http://en.wikipedia.org/w/wiki.phtml?title=Talk:Division_by_zero&action=raw HttpMethod method = null; try { if (urlStr == null) { WikiEditorPlugin.getDefault().reportError("No Wikipedia URL configured", "URL-String == null"); // urlStr = "http://en.wikipedia.org/w/wiki.phtml?title=" + wikiname + "&action=raw"; } URI uri = new URI(urlStr.toCharArray()); String schema = uri.getScheme(); if ((schema == null) || (schema.equals(""))) { schema = "http"; } Protocol protocol = Protocol.getProtocol(schema); HttpState state = new HttpState(); method = new GetMethod(uri.toString()); String host = uri.getHost(); int port = uri.getPort(); HttpConnection connection = new HttpConnection(host, port, protocol); // timeout after 30 seconds connection.setConnectionTimeout(30000); connection.setProxyHost(System.getProperty("http.proxyHost")); connection.setProxyPort(Integer.parseInt(System.getProperty("http.proxyPort", "80"))); if (System.getProperty("http.proxyUserName") != null) { state.setProxyCredentials(null, null, new UsernamePasswordCredentials(System.getProperty("http.proxyUserName"), System .getProperty("http.proxyPassword"))); } if (connection.isProxied() && connection.isSecure()) { method = new ConnectMethod(method); } method.execute(state, connection); if (method.getStatusCode() == HttpStatus.SC_OK) { // get the wiki text now: String wikiText = method.getResponseBodyAsString(); return wikiText; // wrong text not always complete // InputStream stream = method.getResponseBodyAsStream(); // int byteLen = stream.available(); // int count = 1; // byte[] buffer = new byte[byteLen]; // int len = 0; // stream.read(buffer, 0, byteLen); // String wikiText = new String(buffer); // return wikiText; // System.out.println(wikiText); } } catch (Throwable e) { WikiEditorPlugin.log(e); WikiEditorPlugin.getDefault().reportError("Exception occured", e.getMessage() + "\nSee stacktrace in /.metadata/.log file."); } finally { if (method != null) { method.releaseConnection(); } } return null; // no success in getting wiki text } public static String getWikiEditTextarea(String wikiname, String urlStr) { // examples // http://en.wikipedia.org/w/wiki.phtml?title=Main_Page&action=edit // http://en.wikibooks.org/w/wiki.phtml?title=Programming:PHP:SQL_Injection&action=edit // http://en.wikipedia.org/w/wiki.phtml?title=Talk:Division_by_zero&action=edit HttpMethod method = null; try { if (urlStr == null) { urlStr = "http://en.wikipedia.org/w/wiki.phtml?title=" + wikiname + "&action=edit"; } // else { // urlStr = urlStr + "?title=" + wikiname + "&action=edit"; // } URI uri = new URI(urlStr.toCharArray()); String schema = uri.getScheme(); if ((schema == null) || (schema.equals(""))) { schema = "http"; } Protocol protocol = Protocol.getProtocol(schema); HttpState state = new HttpState(); method = new GetMethod(uri.toString()); String host = uri.getHost(); int port = uri.getPort(); HttpConnection connection = new HttpConnection(host, port, protocol); connection.setProxyHost(System.getProperty("http.proxyHost")); connection.setProxyPort(Integer.parseInt(System.getProperty("http.proxyPort", "80"))); if (System.getProperty("http.proxyUserName") != null) { state.setProxyCredentials(null, null, new UsernamePasswordCredentials(System.getProperty("http.proxyUserName"), System .getProperty("http.proxyPassword"))); } if (connection.isProxied() && connection.isSecure()) { method = new ConnectMethod(method); } method.execute(state, connection); if (method.getStatusCode() == HttpStatus.SC_OK) { // get the textareas wiki text now: InputStream stream = method.getResponseBodyAsStream(); int byteLen = stream.available(); int count = 1; byte[] buffer = new byte[byteLen]; stream.read(buffer, 0, byteLen); String wikiText = new String(buffer); // String wikiText = method.getResponseBodyAsString(); int start = wikiText.indexOf("", start + 1); if (start != (-1)) { int end = wikiText.indexOf(""); wikiText = wikiText.substring(start + 1, end); } } return wikiText; // System.out.println(wikiText); } } catch (Exception e) { e.printStackTrace(); } finally { if (method != null) { method.releaseConnection(); } } return null; // no success in getting wiki text } }