1 package net.sourceforge.phpeclipse.wiki.actions.mediawiki.post;
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.io.StringWriter;
6 import java.util.HashMap;
8 import net.sourceforge.phpeclipse.wiki.actions.ProblemConsole;
9 import net.sourceforge.phpeclipse.wiki.actions.mediawiki.config.IWikipedia;
10 import net.sourceforge.phpeclipse.wiki.actions.mediawiki.connect.Content;
11 import net.sourceforge.phpeclipse.wiki.actions.mediawiki.connect.MediaWikiConnector;
12 import net.sourceforge.phpeclipse.wiki.actions.mediawiki.exceptions.MethodException;
13 import net.sourceforge.phpeclipse.wiki.actions.mediawiki.exceptions.PageNotEditableException;
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.renderer.StringUtil;
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 editToken, String body, MediaWikiConnector connector, String actionUrl,
87 String wikiName) 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, editToken, 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 String editToken = connector.loadEditToken(actionUrl, wikipedia.getCharSet(), "plog4u.org bot");
125 if (editToken == null) {
126 console.println("Edit token not found: running in unsave update mode");
128 console.println("Using edit token: " + editToken);
130 for (int i = 0; i < files.length; i++) {
133 is = file.getContents();
134 wikiURLTitle = Util.getURLWikiName(file);
135 String body = StoreWikipediaAction.getInputStreamAsString(is, wikipedia.getCharSet());
137 boolean noContent = StringUtil.checkNoContent(body);
138 String srcBasePath = Util.getWikiTextsPath(file);
139 String binBasePath = Util.getProjectsWikiOutputPath(file.getProject(), WikiEditorPlugin.HTML_OUTPUT_PATH);
141 String fileXMLName = Util.getXMLFileName(file, binBasePath, srcBasePath);
142 IPath path = new Path(fileXMLName);
143 IFile xmlFile = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path);
145 String timestamp = "";
146 if (xmlFile.exists()) {
148 Page page = XStreamManager.fromXML(xmlFile.getContents());
150 // we've stored information about the article at download time which
151 // may be more exactly as the standard information
152 if (!page.isEmpty()) {
153 // at least one revision exists:
154 timestamp = page.get(0).getTimestamp();
156 wikiURLTitle = page.getURLTitle();
158 // timestamp = XMLReader.getDateTimestamp(xmlFile.getContents());
159 } catch (Exception e2) {
164 console.println("File: " + file.getLocation().toString() + "\n==>upload not allowed; Wiki text contains no content");
166 monitor.subTask("Upload: " + file.getLocation().toString());
167 uploadWiki(timestamp, editToken, body, connector, actionUrl, wikiURLTitle);
170 } catch (CoreException e1) {
172 console.println("File: " + file.getLocation().toString() + "\n==>CoreException: " + e1.getMessage());
175 if (monitor.isCanceled()) {
176 return Status.CANCEL_STATUS;
179 monitor.worked(work);
184 // The progress dialog is still open show the message
186 setProperty(IProgressConstants.KEEP_PROPERTY, Boolean.TRUE);
187 // setProperty(IProgressConstants.ACTION_PROPERTY, getWikisCompletedAction());
189 return Status.OK_STATUS;
190 // } catch(CoreException e) {
191 // return e.getStatus();
192 } catch (IOException e) {
194 console.println("File: " + file.getLocation().toString());
196 console.println("==>IOException: " + e.getMessage());
197 } catch (UnexpectedAnswerException e) {
198 console.println("==>UnexpectedAnswerException: " + e.getMessage());
199 } catch (InterruptedException e) {
201 console.println("File: " + file.getLocation().toString());
203 console.println("==>InterruptedException: " + e.getMessage());
204 } catch (PageNotEditableException e) {
206 console.println("File: " + file.getLocation().toString());
208 console.println("==>PageNotEditableException: " + e.getMessage());
209 } catch (MethodException e) {
211 console.println("File: " + file.getLocation().toString());
213 console.println("==>HTTP-MethodException: " + e.getMessage());
216 if (success && connector != null) {
218 monitor.subTask("Logout!");
219 connector.logout(wikipedia, actionUrl);
220 } catch (UnexpectedAnswerException e1) {
221 console.println("==>UnexpectedAnswerException: " + e1.getMessage());
222 } catch (MethodException e1) {
223 console.println("==>HTTP-MethodException: " + e1.getMessage());
229 } catch (IOException e1) {
234 // The progress dialog is still open show the message
236 return Status.CANCEL_STATUS;
239 public boolean isModal(Job job) {
240 Boolean isModal = (Boolean) job.getProperty(IProgressConstants.PROPERTY_IN_DIALOG);
241 if (isModal == null) {
244 return isModal.booleanValue();