Improved PDF export (every article is a chapter, outline, FileDialog)
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.wiki / src / net / sourceforge / phpeclipse / wiki / actions / mediawiki / post / PostJob.java
index 5767954..e32b44a 100644 (file)
@@ -3,7 +3,6 @@ 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;
@@ -11,19 +10,26 @@ 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.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;
 import net.sourceforge.phpeclipse.wiki.preferences.Util;
+import net.sourceforge.phpeclipse.wiki.renderer.StringUtil;
 import net.sourceforge.phpeclipse.wiki.velocity.EditorText;
+import net.sourceforge.phpeclipse.wiki.xml.Page;
+import net.sourceforge.phpeclipse.wiki.xml.XStreamManager;
 
 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 +83,20 @@ public class PostJob extends WorkspaceJob {
     return template;
   }
 
-  private void selectWiki(String body, MediaWikiConnector connector, String actionUrl, String wikiName) {
+  private void uploadWiki(String timestamp, String editToken, 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, editToken, 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,36 +106,70 @@ 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;
+      String wikiURLTitle;
 
+      monitor.subTask("Login user:" + user);
       connector = new MediaWikiConnector();
       success = connector.login(wikipedia, actionUrl, user, password, false);
       if (success) {
+        String editToken = connector.loadEditToken(actionUrl, wikipedia.getCharSet(), "plog4u.org bot");
+        if (editToken == null) {
+          console.println("Edit token not found: running in unsave update mode");
+        } else {
+          console.println("Using edit token: " + editToken);
+        }
         for (int i = 0; i < files.length; i++) {
           try {
             file = files[i];
             is = file.getContents();
-            String wikiName = Util.getFileWikiName(file);
+            wikiURLTitle = Util.getURLWikiName(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 = StringUtil.checkNoContent(body);
+            String srcBasePath = Util.getWikiTextsPath(file);
+            String binBasePath = Util.getProjectsWikiOutputPath(file.getProject(), WikiEditorPlugin.HTML_OUTPUT_PATH);
+
+            String fileXMLName = Util.getXMLFileName(file, binBasePath, srcBasePath);
+            IPath path = new Path(fileXMLName);
+            IFile xmlFile = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path);
+
+            String timestamp = "";
+            if (xmlFile.exists()) {
+              try {
+                Page page = XStreamManager.fromXML(xmlFile.getContents());
+                if (page != null) {
+                  // we've stored information about the article at download time which
+                  // may be more exactly as the standard information
+                  if (!page.isEmpty()) {
+                    // at least one revision exists:
+                    timestamp = page.get(0).getTimestamp();
+                  }
+                  wikiURLTitle = page.getURLTitle();
+                }
+                //                timestamp = XMLReader.getDateTimestamp(xmlFile.getContents());
+              } catch (Exception e2) {
+              }
+            }
+
+            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());
+              uploadWiki(timestamp, editToken, body, connector, actionUrl, wikiURLTitle);
             }
 
           } 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()) {
@@ -147,7 +182,6 @@ public class PostJob extends WorkspaceJob {
 
       if (isModal(this)) {
         // The progress dialog is still open show the message
-        console.reportError();
       } else {
         setProperty(IProgressConstants.KEEP_PROPERTY, Boolean.TRUE);
         //            setProperty(IProgressConstants.ACTION_PROPERTY, getWikisCompletedAction());
@@ -156,15 +190,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());
@@ -181,7 +232,6 @@ public class PostJob extends WorkspaceJob {
     }
     if (isModal(this)) {
       // The progress dialog is still open show the message
-      console.reportError();
     }
     return Status.CANCEL_STATUS;
   }