Handle the new "wpEditToken" input parameter for upload
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.wiki / src / net / sourceforge / phpeclipse / wiki / actions / mediawiki / post / PostJob.java
index 24f807e..7726ecc 100644 (file)
@@ -9,7 +9,6 @@ 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;
@@ -17,6 +16,8 @@ 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.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 +82,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 +105,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 +114,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);
             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 +181,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,7 +231,6 @@ public class PostJob extends WorkspaceJob {
     }
     if (isModal(this)) {
       // The progress dialog is still open show the message
-      console.reportError();
     }
     return Status.CANCEL_STATUS;
   }