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 24f807e..e32b44a 100644 (file)
@@ -9,14 +9,16 @@ 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;
 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;
@@ -81,14 +83,14 @@ public class PostJob extends WorkspaceJob {
     return template;
   }
 
-  private void selectWiki(String timestamp, String body, MediaWikiConnector connector, String actionUrl, String wikiName)
-      throws UnexpectedAnswerException, MethodException, PageNotEditableException, InterruptedException {
+  private void uploadWiki(String timestamp, String editToken, String body, MediaWikiConnector connector, String actionUrl,
+      String wikiName) throws UnexpectedAnswerException, MethodException, PageNotEditableException, InterruptedException {
 
     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);
 
   }
 
@@ -104,7 +106,7 @@ public class PostJob extends WorkspaceJob {
       actionUrl = wikipedia.getActionUrl();
     }
     try {
-      if (files.length>0) {
+      if (files.length > 0) {
         // prefetch for error messages
         file = files[0];
       }
@@ -113,44 +115,56 @@ public class PostJob extends WorkspaceJob {
       int work = 0;
       StringBuffer buffer = new StringBuffer();
       HashMap map = new HashMap();
-      String wikiTitle;
+      String wikiURLTitle;
 
-      monitor.subTask("Login user:"+user);
+      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());
             char ch;
-            boolean noContent = checkNoContent(body);
+            boolean noContent = StringUtil.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);
+            String fileXMLName = Util.getXMLFileName(file, binBasePath, srcBasePath);
+            IPath path = new Path(fileXMLName);
             IFile xmlFile = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path);
-            String timestamp = null;
+
+            String timestamp = "";
             if (xmlFile.exists()) {
               try {
-                timestamp = XMLReader.getDateTimestamp(xmlFile.getContents());
+                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 (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 {
               monitor.subTask("Upload: " + file.getLocation().toString());
-              selectWiki(timestamp, body, connector, actionUrl, wikiName);
+              uploadWiki(timestamp, editToken, body, connector, actionUrl, wikiURLTitle);
             }
 
           } catch (CoreException e1) {
@@ -168,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());
@@ -219,48 +232,10 @@ public class PostJob extends WorkspaceJob {
     }
     if (isModal(this)) {
       // The progress dialog is still open show the message
-      console.reportError();
     }
     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) {