Added some more console messages
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.wiki / src / net / sourceforge / phpeclipse / wiki / actions / mediawiki / post / PostJob.java
index 5767954..24f807e 100644 (file)
@@ -3,14 +3,15 @@ package net.sourceforge.phpeclipse.wiki.actions.mediawiki.post;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.StringWriter;
-import java.util.Date;
 import java.util.HashMap;
 
 import net.sourceforge.phpeclipse.wiki.actions.ProblemConsole;
 import net.sourceforge.phpeclipse.wiki.actions.mediawiki.config.IWikipedia;
 import net.sourceforge.phpeclipse.wiki.actions.mediawiki.connect.Content;
 import net.sourceforge.phpeclipse.wiki.actions.mediawiki.connect.MediaWikiConnector;
+import net.sourceforge.phpeclipse.wiki.actions.mediawiki.connect.XMLReader;
 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;
 import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin;
 import net.sourceforge.phpeclipse.wiki.internal.Configuration;
@@ -20,10 +21,13 @@ import net.sourceforge.phpeclipse.wiki.velocity.EditorText;
 import org.apache.velocity.VelocityContext;
 import org.apache.velocity.app.Velocity;
 import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.ResourcesPlugin;
 import org.eclipse.core.resources.WorkspaceJob;
 import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IPath;
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Path;
 import org.eclipse.core.runtime.Status;
 import org.eclipse.core.runtime.jobs.Job;
 import org.eclipse.ui.progress.IProgressConstants;
@@ -77,25 +81,20 @@ public class PostJob extends WorkspaceJob {
     return template;
   }
 
-  private void selectWiki(String body, MediaWikiConnector connector, String actionUrl, String wikiName) {
+  private void selectWiki(String timestamp, String body, MediaWikiConnector connector, String actionUrl, String wikiName)
+      throws UnexpectedAnswerException, MethodException, PageNotEditableException, InterruptedException {
 
-    try {
-      String url = generateUrl(configuration.getURL(), wikiName);
-
-      Date d = new Date();
-      Content content = new Content(String.valueOf(d.getTime()), body);
+    String url = generateUrl(configuration.getURL(), wikiName);
+    //      System.out.println(timestamp);
+    Content content = new Content(timestamp, body);
 
-      connector.store(wikipedia, actionUrl, wikiName, content, "", false, false);
+    connector.store(wikipedia, actionUrl, wikiName, content, "", false, false);
 
-    } catch (Exception e) {
-      e.printStackTrace();
-      WikiEditorPlugin.getDefault()
-          .reportError("Exception occured: ", e.getMessage() + "\nSee stacktrace in /.metadata/.log file.");
-    }
   }
 
   public IStatus runInWorkspace(IProgressMonitor monitor) {
     boolean success = false;
+    IFile file = null;
     MediaWikiConnector connector = null;
     InputStream is = null;
     ProblemConsole console = new ProblemConsole();
@@ -105,15 +104,18 @@ public class PostJob extends WorkspaceJob {
       actionUrl = wikipedia.getActionUrl();
     }
     try {
-      monitor.beginTask("Refresh Wikis", 100);
+      if (files.length>0) {
+        // prefetch for error messages
+        file = files[0];
+      }
+      monitor.beginTask("Upload Wiki Articles: ", 100);
       int partWork = 100 / files.length;
       int work = 0;
-      int autoCreateIndex = -1;
       StringBuffer buffer = new StringBuffer();
       HashMap map = new HashMap();
       String wikiTitle;
-      IFile file = null;
 
+      monitor.subTask("Login user:"+user);
       connector = new MediaWikiConnector();
       success = connector.login(wikipedia, actionUrl, user, password, false);
       if (success) {
@@ -123,18 +125,37 @@ public class PostJob extends WorkspaceJob {
             is = file.getContents();
             String wikiName = Util.getFileWikiName(file);
             String body = StoreWikipediaAction.getInputStreamAsString(is, wikipedia.getCharSet());
-            autoCreateIndex = body.indexOf(WikiEditorPlugin.AUTOMATICALLY_CREATED);
-            if (autoCreateIndex < 0) {
-              selectWiki(body, connector, actionUrl, wikiName);
+            char ch;
+            boolean noContent = checkNoContent(body);
+            String srcBasePath = Util.getWikiTextsPath(file);
+            String binBasePath = Util.getProjectsWikiOutputPath(file.getProject(), WikiEditorPlugin.HTML_OUTPUT_PATH);
+
+            String filename = Util.getXMLFileName(file, binBasePath, srcBasePath);
+            IPath path = new Path(filename);
+            IFile xmlFile = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path);
+            String timestamp = null;
+            if (xmlFile.exists()) {
+              try {
+                timestamp = XMLReader.getDateTimestamp(xmlFile.getContents());
+              } catch (Exception e2) {
+              }
+            }
+            if (timestamp == null) {
+              //              Date d = new Date();
+              //              timestamp = String.valueOf(d.getTime());
+              timestamp = "";
+            }
+
+            if (noContent) {
+              console.println("File: " + file.getLocation().toString() + "\n==>upload not allowed; Wiki text contains no content");
             } else {
-              console.println("File: " + file.getLocation().toString() + "\n==>no upload allowed; Wiki text contains string: "
-                  + WikiEditorPlugin.AUTOMATICALLY_CREATED);
+              monitor.subTask("Upload: " + file.getLocation().toString());
+              selectWiki(timestamp, body, connector, actionUrl, wikiName);
             }
 
           } catch (CoreException e1) {
             if (file != null) {
-              console.println("File: " + file.getLocation().toString() + "\n==>CoreException: "
-                  + e1.getMessage());
+              console.println("File: " + file.getLocation().toString() + "\n==>CoreException: " + e1.getMessage());
             }
           }
           if (monitor.isCanceled()) {
@@ -156,15 +177,32 @@ public class PostJob extends WorkspaceJob {
       //        } catch(CoreException e) {
       //          return e.getStatus();
     } catch (IOException e) {
+      if (file != null) {
+        console.println("File: " + file.getLocation().toString());
+      }
       console.println("==>IOException: " + e.getMessage());
     } catch (UnexpectedAnswerException e) {
       console.println("==>UnexpectedAnswerException: " + e.getMessage());
+    } catch (InterruptedException e) {
+      if (file != null) {
+        console.println("File: " + file.getLocation().toString());
+      }
+      console.println("==>InterruptedException: " + e.getMessage());
+    } catch (PageNotEditableException e) {
+      if (file != null) {
+        console.println("File: " + file.getLocation().toString());
+      }
+      console.println("==>PageNotEditableException: " + e.getMessage());
     } catch (MethodException e) {
+      if (file != null) {
+        console.println("File: " + file.getLocation().toString());
+      }
       console.println("==>HTTP-MethodException: " + e.getMessage());
     } finally {
       monitor.done();
       if (success && connector != null) {
         try {
+          monitor.subTask("Logout!");
           connector.logout(wikipedia, actionUrl);
         } catch (UnexpectedAnswerException e1) {
           console.println("==>UnexpectedAnswerException: " + e1.getMessage());
@@ -186,6 +224,43 @@ public class PostJob extends WorkspaceJob {
     return Status.CANCEL_STATUS;
   }
 
+  /**
+   * @param body
+   * @param j
+   * @return
+   */
+  private boolean checkNoContent(String body) {
+    char ch;
+    boolean noContent = true;
+    int j = 0;
+    try {
+      while (true) {
+        ch = body.charAt(j++);
+        if (!Character.isWhitespace(ch)) {
+          if (ch == '<' && body.charAt(j) == '!' && body.charAt(j + 1) == '-' && body.charAt(j + 2) == '-') {
+            //<!-- ... -->
+            j += 3;
+            while (true) {
+              ch = body.charAt(j++);
+              if (ch == '-' && body.charAt(j) == '-' && body.charAt(j + 1) == '>') {
+                j += 2;
+                break;
+              }
+            }
+          } else if (ch == '<' && body.charAt(j) == 'b' && body.charAt(j + 1) == 'r' && body.charAt(j + 2) == '>') {
+            // <br>
+          } else {
+            noContent = false;
+            break;
+          }
+        }
+      }
+    } catch (IndexOutOfBoundsException e) {
+
+    }
+    return noContent;
+  }
+
   public boolean isModal(Job job) {
     Boolean isModal = (Boolean) job.getProperty(IProgressConstants.PROPERTY_IN_DIALOG);
     if (isModal == null) {