Improved PDF export (every article is a chapter, outline, FileDialog)
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.wiki / src / net / sourceforge / phpeclipse / wiki / actions / mediawiki / post / PostJob.java
1 package net.sourceforge.phpeclipse.wiki.actions.mediawiki.post;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.io.StringWriter;
6 import java.util.HashMap;
7
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;
22
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;
36
37 public class PostJob extends WorkspaceJob {
38   IFile[] files;
39
40   IWikipedia wikipedia;
41
42   Configuration configuration;
43
44   String user;
45
46   String password;
47
48   public PostJob(Configuration configuration, IWikipedia wikipedia, String user, String password, IFile[] files) {
49     super("Refresh Job");
50     this.files = files;
51     this.wikipedia = wikipedia;
52     this.user = user;
53     this.password = password;
54     this.configuration = configuration;
55   }
56
57   public String generateUrl(String template, String wikiname) {
58     EditorText text = new EditorText(null);
59     /* first, we init the runtime engine. Defaults are fine. */
60
61     try {
62       Velocity.init();
63
64       /* lets make a Context and put data into it */
65
66       VelocityContext context = new VelocityContext();
67
68       context.put("config", configuration);
69       text.clear();
70       text.setWikiname(wikiname);
71       context.put("text", text);
72
73       /* lets make our own string to render */
74       StringWriter w = new StringWriter();
75       w = new StringWriter();
76       Velocity.evaluate(context, w, "mystring", template);
77       return w.toString();
78
79     } catch (Exception e) {
80       // TODO Auto-generated catch block
81       e.printStackTrace();
82     }
83     return template;
84   }
85
86   private void uploadWiki(String timestamp, String editToken, String body, MediaWikiConnector connector, String actionUrl,
87       String wikiName) throws UnexpectedAnswerException, MethodException, PageNotEditableException, InterruptedException {
88
89     String url = generateUrl(configuration.getURL(), wikiName);
90     //      System.out.println(timestamp);
91     Content content = new Content(timestamp, body);
92
93     connector.store(wikipedia, editToken, actionUrl, wikiName, content, "", false, false);
94
95   }
96
97   public IStatus runInWorkspace(IProgressMonitor monitor) {
98     boolean success = false;
99     IFile file = null;
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();
107     }
108     try {
109       if (files.length > 0) {
110         // prefetch for error messages
111         file = files[0];
112       }
113       monitor.beginTask("Upload Wiki Articles: ", 100);
114       int partWork = 100 / files.length;
115       int work = 0;
116       StringBuffer buffer = new StringBuffer();
117       HashMap map = new HashMap();
118       String wikiURLTitle;
119
120       monitor.subTask("Login user:" + user);
121       connector = new MediaWikiConnector();
122       success = connector.login(wikipedia, actionUrl, user, password, false);
123       if (success) {
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");
127         } else {
128           console.println("Using edit token: " + editToken);
129         }
130         for (int i = 0; i < files.length; i++) {
131           try {
132             file = files[i];
133             is = file.getContents();
134             wikiURLTitle = Util.getURLWikiName(file);
135             String body = StoreWikipediaAction.getInputStreamAsString(is, wikipedia.getCharSet());
136             char ch;
137             boolean noContent = StringUtil.checkNoContent(body);
138             String srcBasePath = Util.getWikiTextsPath(file);
139             String binBasePath = Util.getProjectsWikiOutputPath(file.getProject(), WikiEditorPlugin.HTML_OUTPUT_PATH);
140
141             String fileXMLName = Util.getXMLFileName(file, binBasePath, srcBasePath);
142             IPath path = new Path(fileXMLName);
143             IFile xmlFile = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path);
144
145             String timestamp = "";
146             if (xmlFile.exists()) {
147               try {
148                 Page page = XStreamManager.fromXML(xmlFile.getContents());
149                 if (page != null) {
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();
155                   }
156                   wikiURLTitle = page.getURLTitle();
157                 }
158                 //                timestamp = XMLReader.getDateTimestamp(xmlFile.getContents());
159               } catch (Exception e2) {
160               }
161             }
162
163             if (noContent) {
164               console.println("File: " + file.getLocation().toString() + "\n==>upload not allowed; Wiki text contains no content");
165             } else {
166               monitor.subTask("Upload: " + file.getLocation().toString());
167               uploadWiki(timestamp, editToken, body, connector, actionUrl, wikiURLTitle);
168             }
169
170           } catch (CoreException e1) {
171             if (file != null) {
172               console.println("File: " + file.getLocation().toString() + "\n==>CoreException: " + e1.getMessage());
173             }
174           }
175           if (monitor.isCanceled()) {
176             return Status.CANCEL_STATUS;
177           }
178           work += partWork;
179           monitor.worked(work);
180         }
181       }
182
183       if (isModal(this)) {
184         // The progress dialog is still open show the message
185       } else {
186         setProperty(IProgressConstants.KEEP_PROPERTY, Boolean.TRUE);
187         //            setProperty(IProgressConstants.ACTION_PROPERTY, getWikisCompletedAction());
188       }
189       return Status.OK_STATUS;
190       //        } catch(CoreException e) {
191       //          return e.getStatus();
192     } catch (IOException e) {
193       if (file != null) {
194         console.println("File: " + file.getLocation().toString());
195       }
196       console.println("==>IOException: " + e.getMessage());
197     } catch (UnexpectedAnswerException e) {
198       console.println("==>UnexpectedAnswerException: " + e.getMessage());
199     } catch (InterruptedException e) {
200       if (file != null) {
201         console.println("File: " + file.getLocation().toString());
202       }
203       console.println("==>InterruptedException: " + e.getMessage());
204     } catch (PageNotEditableException e) {
205       if (file != null) {
206         console.println("File: " + file.getLocation().toString());
207       }
208       console.println("==>PageNotEditableException: " + e.getMessage());
209     } catch (MethodException e) {
210       if (file != null) {
211         console.println("File: " + file.getLocation().toString());
212       }
213       console.println("==>HTTP-MethodException: " + e.getMessage());
214     } finally {
215       monitor.done();
216       if (success && connector != null) {
217         try {
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());
224         }
225       }
226       if (is != null) {
227         try {
228           is.close();
229         } catch (IOException e1) {
230         }
231       }
232     }
233     if (isModal(this)) {
234       // The progress dialog is still open show the message
235     }
236     return Status.CANCEL_STATUS;
237   }
238
239   public boolean isModal(Job job) {
240     Boolean isModal = (Boolean) job.getProperty(IProgressConstants.PROPERTY_IN_DIALOG);
241     if (isModal == null) {
242       return false;
243     }
244     return isModal.booleanValue();
245   }
246
247 }