package net.sourceforge.phpeclipse.wiki.actions.mediawiki.post; import java.io.IOException; import java.io.InputStream; import java.io.StringWriter; 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.Content; import net.sourceforge.phpeclipse.wiki.actions.mediawiki.connect.MediaWikiConnector; import net.sourceforge.phpeclipse.wiki.actions.mediawiki.exceptions.MethodException; import net.sourceforge.phpeclipse.wiki.actions.mediawiki.exceptions.PageNotEditableException; import net.sourceforge.phpeclipse.wiki.actions.mediawiki.exceptions.UnexpectedAnswerException; import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin; import net.sourceforge.phpeclipse.wiki.internal.Configuration; import net.sourceforge.phpeclipse.wiki.preferences.Util; import net.sourceforge.phpeclipse.wiki.renderer.StringUtil; import net.sourceforge.phpeclipse.wiki.velocity.EditorText; import net.sourceforge.phpeclipse.wiki.xml.Page; import net.sourceforge.phpeclipse.wiki.xml.XStreamManager; import org.apache.velocity.VelocityContext; import org.apache.velocity.app.Velocity; import org.eclipse.core.resources.IFile; 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 PostJob extends WorkspaceJob { IFile[] files; IWikipedia wikipedia; Configuration configuration; String user; String password; public PostJob(Configuration configuration, IWikipedia wikipedia, String user, String password, IFile[] files) { super("Refresh Job"); this.files = files; this.wikipedia = wikipedia; this.user = user; this.password = password; this.configuration = configuration; } public String generateUrl(String template, String wikiname) { EditorText text = new EditorText(null); /* first, we init the runtime engine. Defaults are fine. */ try { Velocity.init(); /* lets make a Context and put data into it */ VelocityContext context = new VelocityContext(); context.put("config", configuration); text.clear(); text.setWikiname(wikiname); context.put("text", text); /* lets make our own string to render */ StringWriter w = new StringWriter(); w = new StringWriter(); Velocity.evaluate(context, w, "mystring", template); return w.toString(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return template; } private void uploadWiki(String timestamp, String editToken, String body, MediaWikiConnector connector, String actionUrl, String wikiName) throws UnexpectedAnswerException, MethodException, PageNotEditableException, InterruptedException { String url = generateUrl(configuration.getURL(), wikiName); // System.out.println(timestamp); Content content = new Content(timestamp, body); connector.store(wikipedia, editToken, actionUrl, wikiName, content, "", false, false); } public IStatus runInWorkspace(IProgressMonitor monitor) { boolean success = false; IFile file = null; MediaWikiConnector connector = null; InputStream is = null; ProblemConsole console = new ProblemConsole(); String actionUrl = configuration.getURL(); if (actionUrl == null || actionUrl.equals("")) { // fall back to default settings actionUrl = wikipedia.getActionUrl(); } try { if (files.length > 0) { // prefetch for error messages file = files[0]; } monitor.beginTask("Upload Wiki Articles: ", 100); int partWork = 100 / files.length; int work = 0; StringBuffer buffer = new StringBuffer(); HashMap map = new HashMap(); String wikiURLTitle; monitor.subTask("Login user:" + user); connector = new MediaWikiConnector(); success = connector.login(wikipedia, actionUrl, user, password, false); if (success) { String editToken = connector.loadEditToken(actionUrl, wikipedia.getCharSet(), "plog4u.org bot"); if (editToken == null) { console.println("Edit token not found: running in unsave update mode"); } else { console.println("Using edit token: " + editToken); } for (int i = 0; i < files.length; i++) { try { file = files[i]; is = file.getContents(); wikiURLTitle = Util.getURLWikiName(file); String body = StoreWikipediaAction.getInputStreamAsString(is, wikipedia.getCharSet()); char ch; boolean noContent = StringUtil.checkNoContent(body); String srcBasePath = Util.getWikiTextsPath(file); String binBasePath = Util.getProjectsWikiOutputPath(file.getProject(), WikiEditorPlugin.HTML_OUTPUT_PATH); String fileXMLName = Util.getXMLFileName(file, binBasePath, srcBasePath); IPath path = new Path(fileXMLName); IFile xmlFile = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path); String timestamp = ""; if (xmlFile.exists()) { try { Page page = XStreamManager.fromXML(xmlFile.getContents()); if (page != null) { // we've stored information about the article at download time which // may be more exactly as the standard information if (!page.isEmpty()) { // at least one revision exists: timestamp = page.get(0).getTimestamp(); } wikiURLTitle = page.getURLTitle(); } // timestamp = XMLReader.getDateTimestamp(xmlFile.getContents()); } catch (Exception e2) { } } if (noContent) { console.println("File: " + file.getLocation().toString() + "\n==>upload not allowed; Wiki text contains no content"); } else { monitor.subTask("Upload: " + file.getLocation().toString()); uploadWiki(timestamp, editToken, body, connector, actionUrl, wikiURLTitle); } } catch (CoreException e1) { if (file != null) { console.println("File: " + file.getLocation().toString() + "\n==>CoreException: " + e1.getMessage()); } } if (monitor.isCanceled()) { return Status.CANCEL_STATUS; } work += partWork; monitor.worked(work); } } if (isModal(this)) { // The progress dialog is still open show the message } else { setProperty(IProgressConstants.KEEP_PROPERTY, Boolean.TRUE); // setProperty(IProgressConstants.ACTION_PROPERTY, getWikisCompletedAction()); } return Status.OK_STATUS; // } catch(CoreException e) { // return e.getStatus(); } catch (IOException e) { if (file != null) { console.println("File: " + file.getLocation().toString()); } console.println("==>IOException: " + e.getMessage()); } catch (UnexpectedAnswerException e) { console.println("==>UnexpectedAnswerException: " + e.getMessage()); } catch (InterruptedException e) { if (file != null) { console.println("File: " + file.getLocation().toString()); } console.println("==>InterruptedException: " + e.getMessage()); } catch (PageNotEditableException e) { if (file != null) { console.println("File: " + file.getLocation().toString()); } console.println("==>PageNotEditableException: " + e.getMessage()); } catch (MethodException e) { if (file != null) { console.println("File: " + file.getLocation().toString()); } console.println("==>HTTP-MethodException: " + e.getMessage()); } finally { monitor.done(); if (success && connector != null) { try { monitor.subTask("Logout!"); connector.logout(wikipedia, actionUrl); } catch (UnexpectedAnswerException e1) { console.println("==>UnexpectedAnswerException: " + e1.getMessage()); } catch (MethodException e1) { console.println("==>HTTP-MethodException: " + e1.getMessage()); } } if (is != null) { try { is.close(); } catch (IOException e1) { } } } if (isModal(this)) { // The progress dialog is still open show the message } return Status.CANCEL_STATUS; } public boolean isModal(Job job) { Boolean isModal = (Boolean) job.getProperty(IProgressConstants.PROPERTY_IN_DIALOG); if (isModal == null) { return false; } return isModal.booleanValue(); } }