version 1.1.2b
[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.InputStream;
4
5 import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin;
6
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;
16
17 /**
18  * This class gets the wikitext from a wikipedia edit page
19  * 
20  * The basic coding was copied from the commons-httpclient example <code>MediaWikiConnector.java</code>
21  */
22 public class MediaWikiConnector {
23
24   /**
25    * Get the text of a wikimedia Wiki-Description from <code>en.wikipedia.org</code>
26    *  
27    */
28   public static String getWikiRawText(String wikiDescriptor) {
29     return getWikiRawText(wikiDescriptor, null);
30   }
31
32   /**
33    * Get the text of a wikimedia Wiki-Description
34    *  
35    */
36   public static String getWikiRawText(String wikiname, String urlStr) {
37     // examples
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;
42     try {
43       if (urlStr == 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";
46       }
47       URI uri = new URI(urlStr.toCharArray());
48
49       String schema = uri.getScheme();
50       if ((schema == null) || (schema.equals(""))) {
51         schema = "http";
52       }
53       Protocol protocol = Protocol.getProtocol(schema);
54
55       HttpState state = new HttpState();
56
57       method = new GetMethod(uri.toString());
58       String host = uri.getHost();
59       int port = uri.getPort();
60
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")));
66
67       if (System.getProperty("http.proxyUserName") != null) {
68         state.setProxyCredentials(null, null, new UsernamePasswordCredentials(System.getProperty("http.proxyUserName"), System
69             .getProperty("http.proxyPassword")));
70       }
71
72       if (connection.isProxied() && connection.isSecure()) {
73         method = new ConnectMethod(method);
74       }
75
76       method.execute(state, connection);
77
78       if (method.getStatusCode() == HttpStatus.SC_OK) {
79         // get the wiki text now:
80         String wikiText = method.getResponseBodyAsString();
81         return wikiText;
82         // wrong text not always complete
83         //        InputStream stream = method.getResponseBodyAsStream();
84         //        int byteLen = stream.available();
85         //        int count = 1;
86         //        byte[] buffer = new byte[byteLen];
87         //        int len = 0;
88         //        stream.read(buffer, 0, byteLen);
89         //        String wikiText = new String(buffer);
90         //        return wikiText;
91         //        System.out.println(wikiText);
92       }
93     } catch (Throwable e) {
94       WikiEditorPlugin.log(e);
95       WikiEditorPlugin.getDefault().reportError("Exception occured", e.getMessage() + "\nSee stacktrace in /.metadata/.log file.");
96     } finally {
97       if (method != null) {
98         method.releaseConnection();
99       }
100     }
101     return null; // no success in getting wiki text
102   }
103
104   public static String getWikiEditTextarea(String wikiname, String urlStr) {
105     // examples
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;
110     try {
111       if (urlStr == null) {
112         urlStr = "http://en.wikipedia.org/w/wiki.phtml?title=" + wikiname + "&action=edit";
113       }
114       //      else {
115       //        urlStr = urlStr + "?title=" + wikiname + "&action=edit";
116       //      }
117       URI uri = new URI(urlStr.toCharArray());
118
119       String schema = uri.getScheme();
120       if ((schema == null) || (schema.equals(""))) {
121         schema = "http";
122       }
123       Protocol protocol = Protocol.getProtocol(schema);
124
125       HttpState state = new HttpState();
126
127       method = new GetMethod(uri.toString());
128       String host = uri.getHost();
129       int port = uri.getPort();
130
131       HttpConnection connection = new HttpConnection(host, port, protocol);
132
133       connection.setProxyHost(System.getProperty("http.proxyHost"));
134       connection.setProxyPort(Integer.parseInt(System.getProperty("http.proxyPort", "80")));
135
136       if (System.getProperty("http.proxyUserName") != null) {
137         state.setProxyCredentials(null, null, new UsernamePasswordCredentials(System.getProperty("http.proxyUserName"), System
138             .getProperty("http.proxyPassword")));
139       }
140
141       if (connection.isProxied() && connection.isSecure()) {
142         method = new ConnectMethod(method);
143       }
144
145       method.execute(state, connection);
146
147       if (method.getStatusCode() == HttpStatus.SC_OK) {
148         // get the textareas wiki text now:
149         InputStream stream = method.getResponseBodyAsStream();
150         int byteLen = stream.available();
151         int count = 1;
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");
157         if (start != (-1)) {
158           start = wikiText.indexOf(">", start + 1);
159           if (start != (-1)) {
160             int end = wikiText.indexOf("</textarea>");
161             wikiText = wikiText.substring(start + 1, end);
162           }
163         }
164         return wikiText;
165         //        System.out.println(wikiText);
166
167       }
168     } catch (Exception e) {
169       e.printStackTrace();
170     } finally {
171       if (method != null) {
172         method.releaseConnection();
173       }
174     }
175     return null; // no success in getting wiki text
176   }
177 }
178