1 package net.sourceforge.phpeclipse.wiki.actions.mediawiki.post;
3 import java.io.IOException;
4 import java.io.InputStream;
5 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.UnexpectedAnswerException;
15 import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin;
16 import net.sourceforge.phpeclipse.wiki.internal.Configuration;
17 import net.sourceforge.phpeclipse.wiki.preferences.Util;
18 import net.sourceforge.phpeclipse.wiki.velocity.EditorText;
20 import org.apache.velocity.VelocityContext;
21 import org.apache.velocity.app.Velocity;
22 import org.eclipse.core.resources.IFile;
23 import org.eclipse.core.resources.WorkspaceJob;
24 import org.eclipse.core.runtime.CoreException;
25 import org.eclipse.core.runtime.IProgressMonitor;
26 import org.eclipse.core.runtime.IStatus;
27 import org.eclipse.core.runtime.Status;
28 import org.eclipse.core.runtime.jobs.Job;
29 import org.eclipse.ui.progress.IProgressConstants;
31 public class PostJob extends WorkspaceJob {
36 Configuration configuration;
42 public PostJob(Configuration configuration, IWikipedia wikipedia, String user, String password, IFile[] files) {
45 this.wikipedia = wikipedia;
47 this.password = password;
48 this.configuration = configuration;
51 public String generateUrl(String template, String wikiname) {
52 EditorText text = new EditorText(null);
53 /* first, we init the runtime engine. Defaults are fine. */
58 /* lets make a Context and put data into it */
60 VelocityContext context = new VelocityContext();
62 context.put("config", configuration);
64 text.setWikiname(wikiname);
65 context.put("text", text);
67 /* lets make our own string to render */
68 StringWriter w = new StringWriter();
69 w = new StringWriter();
70 Velocity.evaluate(context, w, "mystring", template);
73 } catch (Exception e) {
74 // TODO Auto-generated catch block
80 private void selectWiki(String body, MediaWikiConnector connector, String actionUrl, String wikiName) {
83 String url = generateUrl(configuration.getURL(), wikiName);
86 Content content = new Content(String.valueOf(d.getTime()), body);
88 connector.store(wikipedia, actionUrl, wikiName, content, "", false, false);
90 } catch (Exception e) {
92 WikiEditorPlugin.getDefault()
93 .reportError("Exception occured: ", e.getMessage() + "\nSee stacktrace in /.metadata/.log file.");
97 public IStatus runInWorkspace(IProgressMonitor monitor) {
98 boolean success = false;
99 MediaWikiConnector connector = null;
100 InputStream is = null;
101 ProblemConsole console = new ProblemConsole();
102 String actionUrl = configuration.getURL();
103 if (actionUrl == null || actionUrl.equals("")) {
104 // fall back to default settings
105 actionUrl = wikipedia.getActionUrl();
108 monitor.beginTask("Refresh Wikis", 100);
109 int partWork = 100 / files.length;
111 int autoCreateIndex = -1;
112 StringBuffer buffer = new StringBuffer();
113 HashMap map = new HashMap();
117 connector = new MediaWikiConnector();
118 success = connector.login(wikipedia, actionUrl, user, password, false);
120 for (int i = 0; i < files.length; i++) {
123 is = file.getContents();
124 String wikiName = Util.getFileWikiName(file);
125 String body = StoreWikipediaAction.getInputStreamAsString(is, wikipedia.getCharSet());
126 autoCreateIndex = body.indexOf(WikiEditorPlugin.AUTOMATICALLY_CREATED);
127 if (autoCreateIndex < 0) {
128 selectWiki(body, connector, actionUrl, wikiName);
130 console.println("File: " + file.getLocation().toString() + "\n==>no upload allowed; Wiki text contains string: "
131 + WikiEditorPlugin.AUTOMATICALLY_CREATED);
134 } catch (CoreException e1) {
136 console.println("File: " + file.getLocation().toString() + "\n==>CoreException: "
140 if (monitor.isCanceled()) {
141 return Status.CANCEL_STATUS;
144 monitor.worked(work);
149 // The progress dialog is still open show the message
150 console.reportError();
152 setProperty(IProgressConstants.KEEP_PROPERTY, Boolean.TRUE);
153 // setProperty(IProgressConstants.ACTION_PROPERTY, getWikisCompletedAction());
155 return Status.OK_STATUS;
156 // } catch(CoreException e) {
157 // return e.getStatus();
158 } catch (IOException e) {
159 console.println("==>IOException: " + e.getMessage());
160 } catch (UnexpectedAnswerException e) {
161 console.println("==>UnexpectedAnswerException: " + e.getMessage());
162 } catch (MethodException e) {
163 console.println("==>HTTP-MethodException: " + e.getMessage());
166 if (success && connector != null) {
168 connector.logout(wikipedia, actionUrl);
169 } catch (UnexpectedAnswerException e1) {
170 console.println("==>UnexpectedAnswerException: " + e1.getMessage());
171 } catch (MethodException e1) {
172 console.println("==>HTTP-MethodException: " + e1.getMessage());
178 } catch (IOException e1) {
183 // The progress dialog is still open show the message
184 console.reportError();
186 return Status.CANCEL_STATUS;
189 public boolean isModal(Job job) {
190 Boolean isModal = (Boolean) job.getProperty(IProgressConstants.PROPERTY_IN_DIALOG);
191 if (isModal == null) {
194 return isModal.booleanValue();