Upload/Download of multiple files now possible
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.wiki / src / net / sourceforge / phpeclipse / wiki / actions / mediawiki / connect / MediaWikiConnector.java
index b963dcc..4c1abb6 100644 (file)
@@ -1,4 +1,5 @@
 package net.sourceforge.phpeclipse.wiki.actions.mediawiki.connect;
+
 //Parts of this sources are copied and modified from the jEdit Wikipedia plugin:
 //http://www.djini.de/software/wikipedia/index.html
 //
@@ -6,13 +7,15 @@ package net.sourceforge.phpeclipse.wiki.actions.mediawiki.connect;
 //with permission from the original author: Daniel Wunsch
 
 import java.io.IOException;
-import java.io.InputStream;
+import java.io.StringReader;
 import java.io.UnsupportedEncodingException;
 import java.net.URLDecoder;
+import java.util.ArrayList;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
 import net.sourceforge.phpeclipse.wiki.actions.mediawiki.config.IWikipedia;
+import net.sourceforge.phpeclipse.wiki.actions.mediawiki.config.WikipediaDE;
 import net.sourceforge.phpeclipse.wiki.actions.mediawiki.exceptions.MethodException;
 import net.sourceforge.phpeclipse.wiki.actions.mediawiki.exceptions.PageNotEditableException;
 import net.sourceforge.phpeclipse.wiki.actions.mediawiki.exceptions.UnexpectedAnswerException;
@@ -33,6 +36,7 @@ import org.apache.commons.httpclient.methods.GetMethod;
 import org.apache.commons.httpclient.methods.PostMethod;
 import org.apache.commons.httpclient.protocol.Protocol;
 import org.apache.commons.httpclient.util.EncodingUtil;
+import org.eclipse.core.runtime.CoreException;
 
 /**
  * This class gets the wikitext from a wikipedia edit page
@@ -50,7 +54,7 @@ public class MediaWikiConnector {
       + ".*<input[^>]*\\svalue=\"(\\d*)\"[^>]*\\sname=\"wpEdittime\"[^>]*>" + ".*", Pattern.DOTALL);
 
   //setup default user agent
-  final static public String userAgent = "PHPeclipse.de/0.0";
+  final static public String userAgent = "plog4u.org/0.0";
 
   // create a ConnectionManager
   private MultiThreadedHttpConnectionManager manager;
@@ -103,9 +107,13 @@ public class MediaWikiConnector {
     PostMethod method = new PostMethod(actionUrl);
     method.setFollowRedirects(false);
     method.addRequestHeader("User-Agent", userAgent);
-    NameValuePair[] params = new NameValuePair[] { new NameValuePair("title", config.getLoginTitle()), new NameValuePair("action", "submit"),
-        new NameValuePair("wpName", user), new NameValuePair("wpPassword", password),
-        new NameValuePair("wpRemember", remember ? "1" : "0"), new NameValuePair("wpLoginattempt", "submit") };
+    NameValuePair[] params = new NameValuePair[] {
+        new NameValuePair("title", config.getLoginTitle()),
+        new NameValuePair("action", "submit"),
+        new NameValuePair("wpName", user),
+        new NameValuePair("wpPassword", password),
+        new NameValuePair("wpRemember", remember ? "1" : "0"),
+        new NameValuePair("wpLoginattempt", "submit") };
     method.addParameters(params);
 
     boolean result;
@@ -147,11 +155,12 @@ public class MediaWikiConnector {
   }
 
   /** log out - return success */
-  public boolean logout(IWikipedia config,String actionUrl) throws UnexpectedAnswerException, MethodException {
+  public boolean logout(IWikipedia config, String actionUrl) throws UnexpectedAnswerException, MethodException {
     GetMethod method = new GetMethod(actionUrl);
     method.setFollowRedirects(false);
     method.addRequestHeader("User-Agent", userAgent);
-    NameValuePair[] params = new NameValuePair[] { new NameValuePair("title", config.getLogoutTitle()),
+    NameValuePair[] params = new NameValuePair[] {
+        new NameValuePair("title", config.getLogoutTitle()),
         new NameValuePair("action", "submit") };
     method.setQueryString(EncodingUtil.formUrlEncode(params, config.getCharSet()));
 
@@ -235,10 +244,43 @@ public class MediaWikiConnector {
     return result;
   }
 
+  public ArrayList loadXML(IWikipedia config, String actionURL, String pages) throws UnexpectedAnswerException, MethodException {
+    PostMethod method = new PostMethod(actionURL);
+    method.setFollowRedirects(false);
+    method.addRequestHeader("User-Agent", userAgent);
+    method.addRequestHeader("Content-Type", PostMethod.FORM_URL_ENCODED_CONTENT_TYPE + "; charset=" + config.getCharSet());
+    
+    NameValuePair[] params = new NameValuePair[] { 
+        new NameValuePair("pages", pages), 
+        new NameValuePair("curonly", "X"),
+        new NameValuePair("action", "submit") };
+    method.addParameters(params);
+    try {
+      int responseCode = client.executeMethod(method);
+      String responseBody = method.getResponseBodyAsString();
+
+      if (responseCode == 200) {
+        StringReader reader = new StringReader(responseBody);
+        return XMLReader.readFromStream(reader);
+      } else {
+        throw new UnexpectedAnswerException("XML load not successful: expected 200 OK, got " + method.getStatusLine());
+      }
+    } catch(CoreException e) {
+      throw new UnexpectedAnswerException("XML load method failed" + e.getMessage());
+    } catch (HttpException e) {
+      throw new MethodException("XML load method failed", e);
+    } catch (IOException e) {
+      throw new MethodException("XML load method failed", e);
+    } finally {
+      method.releaseConnection();
+    }
+  }
+
   /**
-   * store a Page Version - returns a Stored object 
+   * store a Page Version - returns a Stored object
    * 
-   * @param config - WiKipedia predefined properties 
+   * @param config -
+   *          WiKipedia predefined properties
    * @param actionURL
    * @param title
    * @param content
@@ -261,12 +303,15 @@ public class MediaWikiConnector {
     method.addRequestHeader("User-Agent", userAgent);
     method.addRequestHeader("Content-Type", PostMethod.FORM_URL_ENCODED_CONTENT_TYPE + "; charset=" + config.getCharSet());
     NameValuePair[] params = new NameValuePair[] {
-    // new NameValuePair("wpSection", ""),
+        // new NameValuePair("wpSection", ""),
         // new NameValuePair("wpPreview", "Vorschau zeigen"),
         // new NameValuePair("wpSave", "Artikel speichern"),
-        new NameValuePair("title", title), new NameValuePair("wpTextbox1", content.body),
-        new NameValuePair("wpEdittime", content.timestamp), new NameValuePair("wpSummary", summary),
-        new NameValuePair("wpSave", "yes"), new NameValuePair("action", "submit") };
+        new NameValuePair("title", title),
+        new NameValuePair("wpTextbox1", content.body),
+        new NameValuePair("wpEdittime", content.timestamp),
+        new NameValuePair("wpSummary", summary),
+        new NameValuePair("wpSave", "yes"),
+        new NameValuePair("action", "submit") };
     method.addParameters(params);
     if (minorEdit)
       method.addParameter("wpMinoredit", "1");
@@ -306,7 +351,7 @@ public class MediaWikiConnector {
    * Get the text of a wikimedia article
    *  
    */
-  public static String getWikiRawText(String wikiname, String urlStr) {
+  public 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
@@ -347,21 +392,12 @@ public class MediaWikiConnector {
       }
 
       method.execute(state, connection);
+      //      client.executeMethod(method);
 
       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);
@@ -374,78 +410,95 @@ public class MediaWikiConnector {
     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;
+  //  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("<textarea");
+  //        if (start != (-1)) {
+  //          start = wikiText.indexOf(">", start + 1);
+  //          if (start != (-1)) {
+  //            int end = wikiText.indexOf("</textarea>");
+  //            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
+  //  }
+  
+  public static void main(String[] args) {
+    MediaWikiConnector mwc = new MediaWikiConnector();
     try {
-      if (urlStr == null) {
-        urlStr = "http://en.wikipedia.org/w/wiki.phtml?title=" + wikiname + "&action=edit";
+      IWikipedia wp = null; 
+      ArrayList list = mwc.loadXML(wp, "http://www.plog4u.de/wiki/index.php/Spezial:Export", "Mechanisches Fernsehen\nSynästhesie");
+      for (int i = 0; i < list.size(); i++) {
+        System.out.println(list.get(i).toString());
       }
-      //      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("<textarea");
-        if (start != (-1)) {
-          start = wikiText.indexOf(">", start + 1);
-          if (start != (-1)) {
-            int end = wikiText.indexOf("</textarea>");
-            wikiText = wikiText.substring(start + 1, end);
-          }
-        }
-        return wikiText;
-        //        System.out.println(wikiText);
-
-      }
-    } catch (Exception e) {
+    } catch (UnexpectedAnswerException e) {
+      // TODO Auto-generated catch block
       e.printStackTrace();
-    } finally {
-      if (method != null) {
-        method.releaseConnection();
-      }
-    }
-    return null; // no success in getting wiki text
+    } catch (MethodException e) {
+      // TODO Auto-generated catch block
+      e.printStackTrace();
+    } 
   }
 }