1 package net.sourceforge.phpeclipse.wiki.actions.mediawiki.post;
 
   3 import java.io.IOException;
 
   4 import java.io.InputStream;
 
   5 import java.io.InputStreamReader;
 
   6 import java.io.StringWriter;
 
   7 import java.util.HashMap;
 
   9 import net.sourceforge.phpeclipse.wiki.actions.ProblemConsole;
 
  10 import net.sourceforge.phpeclipse.wiki.actions.mediawiki.config.IWikipedia;
 
  11 import net.sourceforge.phpeclipse.wiki.actions.mediawiki.connect.Content;
 
  12 import net.sourceforge.phpeclipse.wiki.actions.mediawiki.connect.MediaWikiConnector;
 
  13 import net.sourceforge.phpeclipse.wiki.actions.mediawiki.exceptions.MethodException;
 
  14 import net.sourceforge.phpeclipse.wiki.actions.mediawiki.exceptions.PageNotEditableException;
 
  15 import net.sourceforge.phpeclipse.wiki.actions.mediawiki.exceptions.UnexpectedAnswerException;
 
  16 import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin;
 
  17 import net.sourceforge.phpeclipse.wiki.internal.Configuration;
 
  18 import net.sourceforge.phpeclipse.wiki.preferences.Util;
 
  19 import net.sourceforge.phpeclipse.wiki.velocity.EditorText;
 
  20 import net.sourceforge.phpeclipse.wiki.xml.Page;
 
  21 import net.sourceforge.phpeclipse.wiki.xml.XStreamManager;
 
  23 import org.apache.velocity.VelocityContext;
 
  24 import org.apache.velocity.app.Velocity;
 
  25 import org.eclipse.core.resources.IFile;
 
  26 import org.eclipse.core.resources.ResourcesPlugin;
 
  27 import org.eclipse.core.resources.WorkspaceJob;
 
  28 import org.eclipse.core.runtime.CoreException;
 
  29 import org.eclipse.core.runtime.IPath;
 
  30 import org.eclipse.core.runtime.IProgressMonitor;
 
  31 import org.eclipse.core.runtime.IStatus;
 
  32 import org.eclipse.core.runtime.Path;
 
  33 import org.eclipse.core.runtime.Status;
 
  34 import org.eclipse.core.runtime.jobs.Job;
 
  35 import org.eclipse.ui.progress.IProgressConstants;
 
  37 public class PostJob extends WorkspaceJob {
 
  42   Configuration configuration;
 
  48   public PostJob(Configuration configuration, IWikipedia wikipedia, String user, String password, IFile[] files) {
 
  51     this.wikipedia = wikipedia;
 
  53     this.password = password;
 
  54     this.configuration = configuration;
 
  57   public String generateUrl(String template, String wikiname) {
 
  58     EditorText text = new EditorText(null);
 
  59     /* first, we init the runtime engine. Defaults are fine. */
 
  64       /* lets make a Context and put data into it */
 
  66       VelocityContext context = new VelocityContext();
 
  68       context.put("config", configuration);
 
  70       text.setWikiname(wikiname);
 
  71       context.put("text", text);
 
  73       /* lets make our own string to render */
 
  74       StringWriter w = new StringWriter();
 
  75       w = new StringWriter();
 
  76       Velocity.evaluate(context, w, "mystring", template);
 
  79     } catch (Exception e) {
 
  80       // TODO Auto-generated catch block
 
  86   private void uploadWiki(String timestamp, String body, MediaWikiConnector connector, String actionUrl, String wikiName)
 
  87       throws UnexpectedAnswerException, MethodException, PageNotEditableException, InterruptedException {
 
  89     String url = generateUrl(configuration.getURL(), wikiName);
 
  90     //      System.out.println(timestamp);
 
  91     Content content = new Content(timestamp, body);
 
  93     connector.store(wikipedia, actionUrl, wikiName, content, "", false, false);
 
  97   public IStatus runInWorkspace(IProgressMonitor monitor) {
 
  98     boolean success = false;
 
 100     MediaWikiConnector connector = null;
 
 101     InputStream is = null;
 
 102     ProblemConsole console = new ProblemConsole();
 
 103     String actionUrl = configuration.getURL();
 
 104     if (actionUrl == null || actionUrl.equals("")) {
 
 105       // fall back to default settings
 
 106       actionUrl = wikipedia.getActionUrl();
 
 109       if (files.length > 0) {
 
 110         // prefetch for error messages
 
 113       monitor.beginTask("Upload Wiki Articles: ", 100);
 
 114       int partWork = 100 / files.length;
 
 116       StringBuffer buffer = new StringBuffer();
 
 117       HashMap map = new HashMap();
 
 120       monitor.subTask("Login user:" + user);
 
 121       connector = new MediaWikiConnector();
 
 122       success = connector.login(wikipedia, actionUrl, user, password, false);
 
 124         for (int i = 0; i < files.length; i++) {
 
 127             is = file.getContents();
 
 128             wikiURLTitle = Util.getURLWikiName(file);
 
 129             String body = StoreWikipediaAction.getInputStreamAsString(is, wikipedia.getCharSet());
 
 131             boolean noContent = checkNoContent(body);
 
 132             String srcBasePath = Util.getWikiTextsPath(file);
 
 133             String binBasePath = Util.getProjectsWikiOutputPath(file.getProject(), WikiEditorPlugin.HTML_OUTPUT_PATH);
 
 135             String fileXMLName = Util.getXMLFileName(file, binBasePath, srcBasePath);
 
 136             IPath path = new Path(fileXMLName);
 
 137             IFile xmlFile = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path);
 
 139             String timestamp = "";
 
 140             if (xmlFile.exists()) {
 
 142                 Page page = XStreamManager.fromXML(xmlFile.getContents());
 
 144                   // we've stored information about the article at download time which
 
 145                   // may be more exactly as the standard information
 
 146                   if (!page.isEmpty()) {
 
 147                     // at least one revision exists:
 
 148                     timestamp = page.get(0).getTimestamp();
 
 150                   wikiURLTitle = page.getURLTitle();
 
 152                 //                timestamp = XMLReader.getDateTimestamp(xmlFile.getContents());
 
 153               } catch (Exception e2) {
 
 158               console.println("File: " + file.getLocation().toString() + "\n==>upload not allowed; Wiki text contains no content");
 
 160               monitor.subTask("Upload: " + file.getLocation().toString());
 
 161               uploadWiki(timestamp, body, connector, actionUrl, wikiURLTitle);
 
 164           } catch (CoreException e1) {
 
 166               console.println("File: " + file.getLocation().toString() + "\n==>CoreException: " + e1.getMessage());
 
 169           if (monitor.isCanceled()) {
 
 170             return Status.CANCEL_STATUS;
 
 173           monitor.worked(work);
 
 178         // The progress dialog is still open show the message
 
 180         setProperty(IProgressConstants.KEEP_PROPERTY, Boolean.TRUE);
 
 181         //            setProperty(IProgressConstants.ACTION_PROPERTY, getWikisCompletedAction());
 
 183       return Status.OK_STATUS;
 
 184       //        } catch(CoreException e) {
 
 185       //          return e.getStatus();
 
 186     } catch (IOException e) {
 
 188         console.println("File: " + file.getLocation().toString());
 
 190       console.println("==>IOException: " + e.getMessage());
 
 191     } catch (UnexpectedAnswerException e) {
 
 192       console.println("==>UnexpectedAnswerException: " + e.getMessage());
 
 193     } catch (InterruptedException e) {
 
 195         console.println("File: " + file.getLocation().toString());
 
 197       console.println("==>InterruptedException: " + e.getMessage());
 
 198     } catch (PageNotEditableException e) {
 
 200         console.println("File: " + file.getLocation().toString());
 
 202       console.println("==>PageNotEditableException: " + e.getMessage());
 
 203     } catch (MethodException e) {
 
 205         console.println("File: " + file.getLocation().toString());
 
 207       console.println("==>HTTP-MethodException: " + e.getMessage());
 
 210       if (success && connector != null) {
 
 212           monitor.subTask("Logout!");
 
 213           connector.logout(wikipedia, actionUrl);
 
 214         } catch (UnexpectedAnswerException e1) {
 
 215           console.println("==>UnexpectedAnswerException: " + e1.getMessage());
 
 216         } catch (MethodException e1) {
 
 217           console.println("==>HTTP-MethodException: " + e1.getMessage());
 
 223         } catch (IOException e1) {
 
 228       // The progress dialog is still open show the message
 
 230     return Status.CANCEL_STATUS;
 
 238   private boolean checkNoContent(String body) {
 
 240     boolean noContent = true;
 
 244         ch = body.charAt(j++);
 
 245         if (!Character.isWhitespace(ch)) {
 
 246           if (ch == '<' && body.charAt(j) == '!' && body.charAt(j + 1) == '-' && body.charAt(j + 2) == '-') {
 
 250               ch = body.charAt(j++);
 
 251               if (ch == '-' && body.charAt(j) == '-' && body.charAt(j + 1) == '>') {
 
 256           } else if (ch == '<' && body.charAt(j) == 'b' && body.charAt(j + 1) == 'r' && body.charAt(j + 2) == '>') {
 
 264     } catch (IndexOutOfBoundsException e) {
 
 270   public boolean isModal(Job job) {
 
 271     Boolean isModal = (Boolean) job.getProperty(IProgressConstants.PROPERTY_IN_DIALOG);
 
 272     if (isModal == null) {
 
 275     return isModal.booleanValue();