initial contribution
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.wiki / src / net / sourceforge / phpeclipse / wiki / actions / mediawiki / MediaWikiConnector.java
1 package net.sourceforge.phpeclipse.wiki.actions.mediawiki;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.io.StringReader;
6
7 import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin;
8
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;
18
19 /**
20  * This class gets the wikitext from a wikipedia edit page
21  * 
22  * The basic coding was copied from the commons-httpclient example <code>MediaWikiConnector.java</code>
23  */
24 public class MediaWikiConnector {
25
26   /**
27    * Get the text of a wikimedia Wiki-Description from <code>en.wikipedia.org</code>
28    *  
29    */
30   public static String getWikiText(String wikiDescriptor) {
31     return getWikiText(wikiDescriptor, null);
32   }
33
34   /**
35    * Get the text of a wikimedia Wiki-Description
36    *  
37    */
38   public static String getWikiText(String wikiDescriptor, String urlStr) {
39     // examples
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;
44     try {
45       if (urlStr == null) {
46         urlStr = "http://en.wikipedia.org/w/wiki.phtml?title=" + wikiDescriptor + "&action=edit";
47       } else {
48         urlStr = urlStr + "?title=" + wikiDescriptor + "&action=edit";
49       }
50       URI uri = new URI(urlStr.toCharArray());
51
52       String schema = uri.getScheme();
53       if ((schema == null) || (schema.equals(""))) {
54         schema = "http";
55       }
56       Protocol protocol = Protocol.getProtocol(schema);
57
58       HttpState state = new HttpState();
59
60       method = new GetMethod(uri.toString());
61       String host = uri.getHost();
62       int port = uri.getPort();
63
64       HttpConnection connection = new HttpConnection(host, port, protocol);
65
66       connection.setProxyHost(System.getProperty("http.proxyHost"));
67       connection.setProxyPort(Integer.parseInt(System.getProperty("http.proxyPort", "80")));
68
69       if (System.getProperty("http.proxyUserName") != null) {
70         state.setProxyCredentials(null, null, new UsernamePasswordCredentials(System.getProperty("http.proxyUserName"), System
71             .getProperty("http.proxyPassword")));
72       }
73
74       if (connection.isProxied() && connection.isSecure()) {
75         method = new ConnectMethod(method);
76       }
77
78       method.execute(state, connection);
79
80       if (method.getStatusCode() == HttpStatus.SC_OK) {
81         // get the textareas wiki text now:
82         InputStream stream = method.getResponseBodyAsStream();
83         int byteLen = stream.available();
84         int count = 1;
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");
90         if (start != (-1)) {
91           start = wikiText.indexOf(">", start + 1);
92           if (start != (-1)) {
93             int end = wikiText.indexOf("</textarea>");
94             wikiText = wikiText.substring(start + 1, end);
95           }
96         }
97         return wikiText;
98         //        System.out.println(wikiText);
99
100       }
101     } catch (Exception e) {
102       e.printStackTrace();
103     } finally {
104       if (method != null) {
105         method.releaseConnection();
106       }
107     }
108     return null; // no success in getting wiki text
109   }
110 }
111