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
179 console.reportError();
181 setProperty(IProgressConstants.KEEP_PROPERTY, Boolean.TRUE);
182 // setProperty(IProgressConstants.ACTION_PROPERTY, getWikisCompletedAction());
184 return Status.OK_STATUS;
185 // } catch(CoreException e) {
186 // return e.getStatus();
187 } catch (IOException e) {
189 console.println("File: " + file.getLocation().toString());
191 console.println("==>IOException: " + e.getMessage());
192 } catch (UnexpectedAnswerException e) {
193 console.println("==>UnexpectedAnswerException: " + e.getMessage());
194 } catch (InterruptedException e) {
196 console.println("File: " + file.getLocation().toString());
198 console.println("==>InterruptedException: " + e.getMessage());
199 } catch (PageNotEditableException e) {
201 console.println("File: " + file.getLocation().toString());
203 console.println("==>PageNotEditableException: " + e.getMessage());
204 } catch (MethodException e) {
206 console.println("File: " + file.getLocation().toString());
208 console.println("==>HTTP-MethodException: " + e.getMessage());
211 if (success && connector != null) {
213 monitor.subTask("Logout!");
214 connector.logout(wikipedia, actionUrl);
215 } catch (UnexpectedAnswerException e1) {
216 console.println("==>UnexpectedAnswerException: " + e1.getMessage());
217 } catch (MethodException e1) {
218 console.println("==>HTTP-MethodException: " + e1.getMessage());
224 } catch (IOException e1) {
229 // The progress dialog is still open show the message
230 console.reportError();
232 return Status.CANCEL_STATUS;
240 private boolean checkNoContent(String body) {
242 boolean noContent = true;
246 ch = body.charAt(j++);
247 if (!Character.isWhitespace(ch)) {
248 if (ch == '<' && body.charAt(j) == '!' && body.charAt(j + 1) == '-' && body.charAt(j + 2) == '-') {
252 ch = body.charAt(j++);
253 if (ch == '-' && body.charAt(j) == '-' && body.charAt(j + 1) == '>') {
258 } else if (ch == '<' && body.charAt(j) == 'b' && body.charAt(j + 1) == 'r' && body.charAt(j + 2) == '>') {
266 } catch (IndexOutOfBoundsException e) {
272 public boolean isModal(Job job) {
273 Boolean isModal = (Boolean) job.getProperty(IProgressConstants.PROPERTY_IN_DIALOG);
274 if (isModal == null) {
277 return isModal.booleanValue();