1 package net.sourceforge.phpeclipse.wiki.actions.mediawiki;
3 import java.io.ByteArrayInputStream;
4 import java.io.UnsupportedEncodingException;
5 import java.util.ArrayList;
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.MediaWikiConnector;
11 import net.sourceforge.phpeclipse.wiki.actions.mediawiki.connect.Parsed;
12 import net.sourceforge.phpeclipse.wiki.actions.mediawiki.exceptions.MethodException;
13 import net.sourceforge.phpeclipse.wiki.actions.mediawiki.exceptions.UnexpectedAnswerException;
14 import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin;
15 import net.sourceforge.phpeclipse.wiki.preferences.Util;
16 import net.sourceforge.phpeclipse.wiki.xml.Page;
17 import net.sourceforge.phpeclipse.wiki.xml.XStreamManager;
19 import org.eclipse.core.resources.IContainer;
20 import org.eclipse.core.resources.IFile;
21 import org.eclipse.core.resources.IFolder;
22 import org.eclipse.core.resources.ResourcesPlugin;
23 import org.eclipse.core.resources.WorkspaceJob;
24 import org.eclipse.core.runtime.CoreException;
25 import org.eclipse.core.runtime.IPath;
26 import org.eclipse.core.runtime.IProgressMonitor;
27 import org.eclipse.core.runtime.IStatus;
28 import org.eclipse.core.runtime.Path;
29 import org.eclipse.core.runtime.Status;
30 import org.eclipse.core.runtime.jobs.Job;
31 import org.eclipse.ui.progress.IProgressConstants;
33 public class RefreshJob extends WorkspaceJob {
36 IWikipedia configuration;
40 public RefreshJob(IWikipedia configuration, IFile[] files, String actionURL) {
43 this.configuration = configuration;
44 this.actionURL = actionURL;
47 public IStatus runInWorkspace(IProgressMonitor monitor) {
48 ProblemConsole console = new ProblemConsole();
52 monitor.beginTask("Download Wiki Articles: ", 100);
53 // ArrayList wikiTitles = new ArrayList();
54 // for (int i = 0; i < files.length; i++) {
55 // wikiTitles.add( Util.getReadableWikiName(files[i]) );
57 StringBuffer buffer = new StringBuffer();
58 HashMap map = new HashMap();
61 for (int i = 0; i < files.length; i++) {
63 wikiTitle = createWikiTitle(file, i);
64 buffer.append(wikiTitle);
66 map.put(wikiTitle, file);
67 if (i != files.length - 1) {
71 // read only 10 files at a time
72 IStatus status = readWikisFromBuffer(buffer, map, monitor, console);
73 if (status.equals(Status.CANCEL_STATUS)) {
74 return Status.CANCEL_STATUS;
76 buffer = new StringBuffer();
80 if (titleCounter > 0) {
81 IStatus status = readWikisFromBuffer(buffer, map, monitor, console);
82 if (status.equals(Status.CANCEL_STATUS)) {
83 return Status.CANCEL_STATUS;
85 buffer = new StringBuffer();
90 // The progress dialog is still open show the message
91 console.reportError();
93 // setProperty(IProgressConstants.KEEP_PROPERTY, Boolean.TRUE);
94 // setProperty(IProgressConstants.ACTION_PROPERTY, getWikisCompletedAction());
96 return Status.OK_STATUS;
97 // } catch(CoreException e) {
98 // return e.getStatus();
99 } catch (UnexpectedAnswerException e) {
101 console.println("File: " + file.getLocation().toString() + "\n==>UnexpectedAnswerException: " + e.getMessage());
103 console.println("UnexpectedAnswerException: " + e.getMessage());
105 } catch (MethodException e) {
107 console.println("File: " + file.getLocation().toString() + "\n==>HTTP-MethodException: " + e.getMessage());
109 console.println("HTTP-MethodException: " + e.getMessage());
111 } catch (InterruptedException e) {
113 console.println("File: " + file.getLocation().toString() + "\n==>InterruptedException: " + e.getMessage());
115 console.println("InterruptedException: " + e.getMessage());
121 // The progress dialog is still open show the message
122 console.reportError();
124 return Status.OK_STATUS;
134 * @throws UnexpectedAnswerException
135 * @throws MethodException
137 private IStatus readWikisFromBuffer(StringBuffer buffer, HashMap map, IProgressMonitor monitor, ProblemConsole console)
138 throws UnexpectedAnswerException, InterruptedException, MethodException {
140 boolean showConsole = WikiEditorPlugin.getDefault().getPreferenceStore().getBoolean(WikiEditorPlugin.CONSOLE_OUTPUT);
141 MediaWikiConnector mwConnector = new MediaWikiConnector();
142 String url = actionURL;
144 url = configuration.getActionUrl() + "/" + configuration.getSpecialNs() + ":Export";
146 // get a list of Parsed elements
147 monitor.subTask("Downloading (XML Import)");
149 console.println("Downloading (XML Import):\n" + buffer.toString());
151 ArrayList list = mwConnector.loadXML(configuration, url, buffer.toString());
154 for (int i = 0; i < list.size(); i++) {
155 Parsed parsed = (Parsed) list.get(i);
156 wikiTitle = parsed.getTitle();
157 if (wikiTitle != null) {
158 body = parsed.getBody();
160 file = (IFile) map.get(wikiTitle);
162 // rearrange parsed data into a page for XStream hamdling:
163 Page page = new Page(parsed.getDateTimestamp(), wikiTitle, body);
164 monitor.subTask("Modify file: " + file.getLocation().toString());
166 console.println("Update file: " + file.getLocation().toString());
168 updateFileContent(console, file, page, body, configuration, monitor);
172 if (monitor.isCanceled()) {
173 return Status.CANCEL_STATUS;
177 return Status.OK_STATUS;
186 private String createWikiTitle(IFile file, int i) {
187 String wikiTitle = null;
188 String srcBasePath = Util.getWikiTextsPath(file);
189 String binBasePath = Util.getProjectsWikiOutputPath(file.getProject(), WikiEditorPlugin.HTML_OUTPUT_PATH);
191 String fileXMLName = Util.getXMLFileName(file, binBasePath, srcBasePath);
192 IPath path = new Path(fileXMLName);
193 IFile xmlFile = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path);
194 if (xmlFile.exists()) {
196 Page page = XStreamManager.fromXML(xmlFile.getContents());
198 wikiTitle = page.getTitle();
200 // timestamp = XMLReader.getDateTimestamp(xmlFile.getContents());
201 } catch (Exception e2) {
204 if (wikiTitle == null) {
205 // if no XML file exists we create the name from the filename
206 wikiTitle = Util.getReadableWikiName(files[i]);
211 public boolean isModal(Job job) {
212 Boolean isModal = (Boolean) job.getProperty(IProgressConstants.PROPERTY_IN_DIALOG);
213 if (isModal == null) {
216 return isModal.booleanValue();
219 private static void updateFileContent(ProblemConsole console, IFile file, Page page, String body, IWikipedia wp,
220 IProgressMonitor monitor) {
224 file.setContents(new ByteArrayInputStream(body.getBytes()), true, false, null);
226 file.setContents(new ByteArrayInputStream(body.getBytes(wp.getCharSet())), true, false, null);
227 file.setCharset(wp.getCharSet(), monitor);
231 file.create(new ByteArrayInputStream(body.getBytes()), false, null);
233 file.create(new ByteArrayInputStream(body.getBytes(wp.getCharSet())), false, null);
234 file.setCharset(wp.getCharSet(), monitor);
237 String srcBasePath = Util.getWikiTextsPath(file);
238 String binBasePath = Util.getProjectsWikiOutputPath(file.getProject(), WikiEditorPlugin.HTML_OUTPUT_PATH);
240 String filename = Util.getXMLFileName(file, binBasePath, srcBasePath);
241 IPath path = new Path(filename);
242 IFile xmlFile = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path);
243 IContainer parent = xmlFile.getParent();
244 if (parent instanceof IFolder && (!((IFolder) parent).exists())) {
245 Util.createFolder((IFolder) parent, monitor);
248 // String xmlData = parsed.getXmlData();
249 // String charSet = "UTF-8";
250 // StringBuffer buf = new StringBuffer();
251 // int index = xmlData.indexOf("<page>");
253 // console.println("File: " + xmlFile.getLocation().toString() + "\n==>Couldn't create xml file - <page> tag not found");
256 // xmlData = xmlData.substring(index);
257 // buf.append(WikiEditorPlugin.XML_START_1);
258 // buf.append(charSet);
259 // buf.append(WikiEditorPlugin.XML_START_2);
260 // buf.append(xmlData);
261 // buf.append(WikiEditorPlugin.XML_END);
263 // byte[] buffer = buf.toString().getBytes();
264 byte[] buffer = XStreamManager.toXML(page).getBytes();
265 ByteArrayInputStream source = new ByteArrayInputStream(buffer);
266 if (!xmlFile.exists()) {
267 xmlFile.create(source, true, monitor);
269 xmlFile.setContents(source, true, true, monitor);
271 } catch (CoreException e) {
273 console.println("File: " + xmlFile.getLocation().toString() + "\n==>CoreException: " + e.getMessage());
276 } catch (UnsupportedEncodingException e) {
277 console.println("File: " + file.getLocation().toString() + "\n==>UnsupportedEncodingException: " + e.getMessage());
278 } catch (Exception e) {
279 console.println("File: " + file.getLocation().toString() + "\n==>Exception: " + e.getMessage());