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.velocity.EditorText;
19 import net.sourceforge.phpeclipse.wiki.xml.Page;
20 import net.sourceforge.phpeclipse.wiki.xml.XStreamManager;
22 import org.apache.velocity.VelocityContext;
23 import org.apache.velocity.app.Velocity;
24 import org.eclipse.core.resources.IFile;
25 import org.eclipse.core.resources.ResourcesPlugin;
26 import org.eclipse.core.resources.WorkspaceJob;
27 import org.eclipse.core.runtime.CoreException;
28 import org.eclipse.core.runtime.IPath;
29 import org.eclipse.core.runtime.IProgressMonitor;
30 import org.eclipse.core.runtime.IStatus;
31 import org.eclipse.core.runtime.Path;
32 import org.eclipse.core.runtime.Status;
33 import org.eclipse.core.runtime.jobs.Job;
34 import org.eclipse.ui.progress.IProgressConstants;
36 public class PostJob extends WorkspaceJob {
41 Configuration configuration;
47 public PostJob(Configuration configuration, IWikipedia wikipedia, String user, String password, IFile[] files) {
50 this.wikipedia = wikipedia;
52 this.password = password;
53 this.configuration = configuration;
56 public String generateUrl(String template, String wikiname) {
57 EditorText text = new EditorText(null);
58 /* first, we init the runtime engine. Defaults are fine. */
63 /* lets make a Context and put data into it */
65 VelocityContext context = new VelocityContext();
67 context.put("config", configuration);
69 text.setWikiname(wikiname);
70 context.put("text", text);
72 /* lets make our own string to render */
73 StringWriter w = new StringWriter();
74 w = new StringWriter();
75 Velocity.evaluate(context, w, "mystring", template);
78 } catch (Exception e) {
79 // TODO Auto-generated catch block
85 private void uploadWiki(String timestamp, String editToken, String body, MediaWikiConnector connector, String actionUrl,
86 String wikiName) throws UnexpectedAnswerException, MethodException, PageNotEditableException, InterruptedException {
88 String url = generateUrl(configuration.getURL(), wikiName);
89 // System.out.println(timestamp);
90 Content content = new Content(timestamp, body);
92 connector.store(wikipedia, editToken, actionUrl, wikiName, content, "", false, false);
96 public IStatus runInWorkspace(IProgressMonitor monitor) {
97 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 if (files.length > 0) {
109 // prefetch for error messages
112 monitor.beginTask("Upload Wiki Articles: ", 100);
113 int partWork = 100 / files.length;
115 StringBuffer buffer = new StringBuffer();
116 HashMap map = new HashMap();
119 monitor.subTask("Login user:" + user);
120 connector = new MediaWikiConnector();
121 success = connector.login(wikipedia, actionUrl, user, password, false);
123 String editToken = connector.loadEditToken(actionUrl, wikipedia.getCharSet(), "plog4u.org bot");
124 if (editToken == null) {
125 console.println("Edit token not found: running in unsave update mode");
127 console.println("Using edit token: " + editToken);
129 for (int i = 0; i < files.length; i++) {
132 is = file.getContents();
133 wikiURLTitle = Util.getURLWikiName(file);
134 String body = StoreWikipediaAction.getInputStreamAsString(is, wikipedia.getCharSet());
136 boolean noContent = checkNoContent(body);
137 String srcBasePath = Util.getWikiTextsPath(file);
138 String binBasePath = Util.getProjectsWikiOutputPath(file.getProject(), WikiEditorPlugin.HTML_OUTPUT_PATH);
140 String fileXMLName = Util.getXMLFileName(file, binBasePath, srcBasePath);
141 IPath path = new Path(fileXMLName);
142 IFile xmlFile = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path);
144 String timestamp = "";
145 if (xmlFile.exists()) {
147 Page page = XStreamManager.fromXML(xmlFile.getContents());
149 // we've stored information about the article at download time which
150 // may be more exactly as the standard information
151 if (!page.isEmpty()) {
152 // at least one revision exists:
153 timestamp = page.get(0).getTimestamp();
155 wikiURLTitle = page.getURLTitle();
157 // timestamp = XMLReader.getDateTimestamp(xmlFile.getContents());
158 } catch (Exception e2) {
163 console.println("File: " + file.getLocation().toString() + "\n==>upload not allowed; Wiki text contains no content");
165 monitor.subTask("Upload: " + file.getLocation().toString());
166 uploadWiki(timestamp, editToken, body, connector, actionUrl, wikiURLTitle);
169 } catch (CoreException e1) {
171 console.println("File: " + file.getLocation().toString() + "\n==>CoreException: " + e1.getMessage());
174 if (monitor.isCanceled()) {
175 return Status.CANCEL_STATUS;
178 monitor.worked(work);
183 // The progress dialog is still open show the message
185 setProperty(IProgressConstants.KEEP_PROPERTY, Boolean.TRUE);
186 // setProperty(IProgressConstants.ACTION_PROPERTY, getWikisCompletedAction());
188 return Status.OK_STATUS;
189 // } catch(CoreException e) {
190 // return e.getStatus();
191 } catch (IOException e) {
193 console.println("File: " + file.getLocation().toString());
195 console.println("==>IOException: " + e.getMessage());
196 } catch (UnexpectedAnswerException e) {
197 console.println("==>UnexpectedAnswerException: " + e.getMessage());
198 } catch (InterruptedException e) {
200 console.println("File: " + file.getLocation().toString());
202 console.println("==>InterruptedException: " + e.getMessage());
203 } catch (PageNotEditableException e) {
205 console.println("File: " + file.getLocation().toString());
207 console.println("==>PageNotEditableException: " + e.getMessage());
208 } catch (MethodException e) {
210 console.println("File: " + file.getLocation().toString());
212 console.println("==>HTTP-MethodException: " + e.getMessage());
215 if (success && connector != null) {
217 monitor.subTask("Logout!");
218 connector.logout(wikipedia, actionUrl);
219 } catch (UnexpectedAnswerException e1) {
220 console.println("==>UnexpectedAnswerException: " + e1.getMessage());
221 } catch (MethodException e1) {
222 console.println("==>HTTP-MethodException: " + e1.getMessage());
228 } catch (IOException e1) {
233 // The progress dialog is still open show the message
235 return Status.CANCEL_STATUS;
243 private boolean checkNoContent(String body) {
245 boolean noContent = true;
249 ch = body.charAt(j++);
250 if (!Character.isWhitespace(ch)) {
251 if (ch == '<' && body.charAt(j) == '!' && body.charAt(j + 1) == '-' && body.charAt(j + 2) == '-') {
255 ch = body.charAt(j++);
256 if (ch == '-' && body.charAt(j) == '-' && body.charAt(j + 1) == '>') {
261 } else if (ch == '<' && body.charAt(j) == 'b' && body.charAt(j + 1) == 'r' && body.charAt(j + 2) == '>') {
269 } catch (IndexOutOfBoundsException e) {
275 public boolean isModal(Job job) {
276 Boolean isModal = (Boolean) job.getProperty(IProgressConstants.PROPERTY_IN_DIALOG);
277 if (isModal == null) {
280 return isModal.booleanValue();