integrated velocity engine for URL templates
[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 wikiname, 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=" + wikiname + "&action=edit";
47       } 
48 //      else {
49 //        urlStr = urlStr + "?title=" + wikiname + "&action=edit";
50 //      }
51       URI uri = new URI(urlStr.toCharArray());
52
53       String schema = uri.getScheme();
54       if ((schema == null) || (schema.equals(""))) {
55         schema = "http";
56       }
57       Protocol protocol = Protocol.getProtocol(schema);
58
59       HttpState state = new HttpState();
60
61       method = new GetMethod(uri.toString());
62       String host = uri.getHost();
63       int port = uri.getPort();
64
65       HttpConnection connection = new HttpConnection(host, port, protocol);
66
67       connection.setProxyHost(System.getProperty("http.proxyHost"));
68       connection.setProxyPort(Integer.parseInt(System.getProperty("http.proxyPort", "80")));
69
70       if (System.getProperty("http.proxyUserName") != null) {
71         state.setProxyCredentials(null, null, new UsernamePasswordCredentials(System.getProperty("http.proxyUserName"), System
72             .getProperty("http.proxyPassword")));
73       }
74
75       if (connection.isProxied() && connection.isSecure()) {
76         method = new ConnectMethod(method);
77       }
78
79       method.execute(state, connection);
80
81       if (method.getStatusCode() == HttpStatus.SC_OK) {
82         // get the textareas wiki text now:
83         InputStream stream = method.getResponseBodyAsStream();
84         int byteLen = stream.available();
85         int count = 1;
86         byte[] buffer = new byte[byteLen];
87         stream.read(buffer, 0, byteLen);
88         String wikiText = new String(buffer);
89 //        String wikiText = method.getResponseBodyAsString();
90         int start = wikiText.indexOf("<textarea");
91         if (start != (-1)) {
92           start = wikiText.indexOf(">", start + 1);
93           if (start != (-1)) {
94             int end = wikiText.indexOf("</textarea>");
95             wikiText = wikiText.substring(start + 1, end);
96           }
97         }
98         return wikiText;
99         //        System.out.println(wikiText);
100
101       }
102     } catch (Exception e) {
103       e.printStackTrace();
104     } finally {
105       if (method != null) {
106         method.releaseConnection();
107       }
108     }
109     return null; // no success in getting wiki text
110   }
111 }
112