--- /dev/null
+package net.sourceforge.phpeclipse.wiki.actions.mediawiki;
+
+import java.io.ByteArrayInputStream;
+import java.io.UnsupportedEncodingException;
+import java.util.ArrayList;
+import java.util.HashMap;
+
+import net.sourceforge.phpeclipse.wiki.actions.ProblemConsole;
+import net.sourceforge.phpeclipse.wiki.actions.mediawiki.config.IWikipedia;
+import net.sourceforge.phpeclipse.wiki.actions.mediawiki.connect.MediaWikiConnector;
+import net.sourceforge.phpeclipse.wiki.actions.mediawiki.connect.Parsed;
+import net.sourceforge.phpeclipse.wiki.actions.mediawiki.exceptions.MethodException;
+import net.sourceforge.phpeclipse.wiki.actions.mediawiki.exceptions.UnexpectedAnswerException;
+import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin;
+import net.sourceforge.phpeclipse.wiki.preferences.Util;
+
+import org.eclipse.core.resources.IContainer;
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IFolder;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.resources.WorkspaceJob;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Path;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.core.runtime.jobs.Job;
+import org.eclipse.ui.progress.IProgressConstants;
+
+public class RefreshJob extends WorkspaceJob {
+ IFile[] files;
+
+ IWikipedia configuration;
+
+ String actionURL;
+
+ public RefreshJob(IWikipedia configuration, IFile[] files, String actionURL) {
+ super("Refresh Job");
+ this.files = files;
+ this.configuration = configuration;
+ this.actionURL = actionURL;
+ }
+
+ public IStatus runInWorkspace(IProgressMonitor monitor) {
+ ProblemConsole console = new ProblemConsole();
+ IFile file = null;
+ try {
+ monitor.beginTask("Refresh Wikis", 100);
+ // ArrayList wikiTitles = new ArrayList();
+ // for (int i = 0; i < files.length; i++) {
+ // wikiTitles.add( Util.getReadableWikiName(files[i]) );
+ // }
+ StringBuffer buffer = new StringBuffer();
+ HashMap map = new HashMap();
+ String wikiTitle;
+ for (int i = 0; i < files.length; i++) {
+ wikiTitle = Util.getReadableWikiName(files[i]);
+ buffer.append(wikiTitle);
+ map.put(wikiTitle, files[i]);
+ if (i != files.length - 1) {
+ buffer.append("\n");
+ }
+ }
+ MediaWikiConnector mwc = new MediaWikiConnector();
+ String url = actionURL;
+ if (url == null) {
+ url = configuration.getActionUrl() + "/" + configuration.getSpecialNs() + ":Export";
+ }
+ // get a list of Parsed elements
+ ArrayList list = mwc.loadXML(configuration, url, buffer.toString());
+ String body;
+
+ for (int i = 0; i < list.size(); i++) {
+ Parsed parsed = (Parsed) list.get(i);
+ wikiTitle = parsed.getTitle();
+ if (wikiTitle != null) {
+ body = parsed.getBody();
+ if (body != null) {
+ file = (IFile) map.get(wikiTitle);
+ if (file != null) {
+ updateFileContent(console, file, parsed, body, configuration, monitor);
+ }
+ }
+ }
+ if (monitor.isCanceled()) {
+ return Status.CANCEL_STATUS;
+ }
+ }
+ if (isModal(this)) {
+ // The progress dialog is still open show the message
+ console.reportError();
+ } else {
+ // setProperty(IProgressConstants.KEEP_PROPERTY, Boolean.TRUE);
+ // setProperty(IProgressConstants.ACTION_PROPERTY, getWikisCompletedAction());
+ }
+ return Status.OK_STATUS;
+ // } catch(CoreException e) {
+ // return e.getStatus();
+ } catch (UnexpectedAnswerException e) {
+ if (file != null) {
+ console.println("File: " + file.getLocation().toString() + "\n==>UnexpectedAnswerException: " + e.getMessage());
+ } else {
+ console.println("UnexpectedAnswerException: " + e.getMessage());
+ }
+ } catch (MethodException e) {
+ if (file != null) {
+ console.println("File: " + file.getLocation().toString() + "\n==>HTTP-MethodException: " + e.getMessage());
+ } else {
+ console.println("HTTP-MethodException: " + e.getMessage());
+ }
+ } finally {
+ monitor.done();
+ }
+ if (isModal(this)) {
+ // The progress dialog is still open show the message
+ console.reportError();
+ }
+ return Status.OK_STATUS;
+ }
+
+ public boolean isModal(Job job) {
+ Boolean isModal = (Boolean) job.getProperty(IProgressConstants.PROPERTY_IN_DIALOG);
+ if (isModal == null) {
+ return false;
+ }
+ return isModal.booleanValue();
+ }
+
+ private static void updateFileContent(ProblemConsole console, IFile file, Parsed parsed, String body, IWikipedia wp,
+ IProgressMonitor monitor) {
+ try {
+ if (file.exists()) {
+ if (wp == null) {
+ file.setContents(new ByteArrayInputStream(body.getBytes()), true, false, null);
+ } else {
+ file.setContents(new ByteArrayInputStream(body.getBytes(wp.getCharSet())), true, false, null);
+ file.setCharset(wp.getCharSet(), monitor);
+ }
+ } else {
+ if (wp == null) {
+ file.create(new ByteArrayInputStream(body.getBytes()), false, null);
+ } else {
+ file.create(new ByteArrayInputStream(body.getBytes(wp.getCharSet())), false, null);
+ file.setCharset(wp.getCharSet(), monitor);
+ }
+ }
+ String srcBasePath = Util.getWikiTextsPath(file);
+ String binBasePath = Util.getProjectsWikiOutputPath(file.getProject(), WikiEditorPlugin.HTML_OUTPUT_PATH);
+
+ String filename = Util.getXMLFileName(file, binBasePath, srcBasePath);
+ IPath path = new Path(filename);
+ IFile xmlFile = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path);
+ IContainer parent = xmlFile.getParent();
+ if (parent instanceof IFolder && (!((IFolder) parent).exists())) {
+ Util.createFolder((IFolder) parent, monitor);
+ }
+ try {
+ String xmlData = parsed.getXmlData();
+ String charSet = "UTF-8";
+ StringBuffer buf = new StringBuffer();
+ int index = xmlData.indexOf("<page>");
+ if (index<0) {
+ console.println("File: " + xmlFile.getLocation().toString() + "\n==>Couldn't create xml file - <page> tag not found");
+ return;
+ }
+ xmlData = xmlData.substring(index);
+// buf.setLength(xmlData.length() + WikiEditorPlugin.XML_START_1.length() + WikiEditorPlugin.XML_START_2.length()
+// + WikiEditorPlugin.XML_END.length() + charSet.length());
+//
+ buf.append(WikiEditorPlugin.XML_START_1);
+ buf.append(charSet);
+ buf.append(WikiEditorPlugin.XML_START_2);
+ buf.append(xmlData);
+ buf.append(WikiEditorPlugin.XML_END);
+
+ byte[] buffer = buf.toString().getBytes();
+ ByteArrayInputStream source = new ByteArrayInputStream(buffer);
+ if (!xmlFile.exists()) {
+ xmlFile.create(source, true, monitor);
+ } else {
+ xmlFile.setContents(source, true, true, monitor);
+ }
+ } catch (CoreException e) {
+ if (file != null) {
+ console.println("File: " + xmlFile.getLocation().toString() + "\n==>CoreException: " + e.getMessage());
+ }
+ }
+ } catch (UnsupportedEncodingException e) {
+ console.println("File: " + file.getLocation().toString() + "\n==>UnsupportedEncodingException: " + e.getMessage());
+ } catch (CoreException e) {
+ console.println("File: " + file.getLocation().toString() + "\n==>CoreException: " + e.getMessage());
+ }
+ }
+}
\ No newline at end of file