From f722f5367d57710bf29491541d0fda66bbfd72dc Mon Sep 17 00:00:00 2001 From: axelcl Date: Sat, 8 Jan 2005 18:57:43 +0000 Subject: [PATCH] Implemeted action for uploading Wikipedia articles (thanks to D.Wunsch) Problems: - timestamp is used wrong at the moment; - wiki locale not configurable by users at the moment --- .../plugin.properties | 3 +- archive/net.sourceforge.phpeclipse.wiki/plugin.xml | 39 ++- .../wiki/actions/OpenWikiLinkEditorAction.java | 33 ++- .../mediawiki/connect/BookmarkletServer.java | 184 ++++++++ .../wiki/actions/mediawiki/connect/Content.java | 23 + .../wiki/actions/mediawiki/connect/Loaded.java | 24 + .../mediawiki/connect/MediaWikiConnector.java | 451 ++++++++++++++++++++ .../wiki/actions/mediawiki/connect/Parsed.java | 20 + .../wiki/actions/mediawiki/connect/SiteState.java | 35 ++ .../wiki/actions/mediawiki/connect/Stored.java | 29 ++ .../mediawiki/post/StoreWikipediaAction.java | 4 +- .../phpeclipse/wiki/editor/WikiEditorPlugin.java | 4 +- .../wiki/internal/ConfigurationManager.java | 8 +- 13 files changed, 831 insertions(+), 26 deletions(-) create mode 100644 archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/connect/BookmarkletServer.java create mode 100644 archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/connect/Content.java create mode 100644 archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/connect/Loaded.java create mode 100644 archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/connect/MediaWikiConnector.java create mode 100644 archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/connect/Parsed.java create mode 100644 archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/connect/SiteState.java create mode 100644 archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/connect/Stored.java diff --git a/archive/net.sourceforge.phpeclipse.wiki/plugin.properties b/archive/net.sourceforge.phpeclipse.wiki/plugin.properties index 11feb4e..f5552a9 100644 --- a/archive/net.sourceforge.phpeclipse.wiki/plugin.properties +++ b/archive/net.sourceforge.phpeclipse.wiki/plugin.properties @@ -1,4 +1,5 @@ -MediaWiki.label=Wikipedia +MediaWiki.download.label=Wikipedia Download +MediaWiki.upload.label=Wikipedia Upload BlogWiki.label=Blog as Wiki Text BlogHTML.label=Blog as HTML Text HTTPQuery.label=HTTP Query diff --git a/archive/net.sourceforge.phpeclipse.wiki/plugin.xml b/archive/net.sourceforge.phpeclipse.wiki/plugin.xml index beafde2..e90fdb7 100644 --- a/archive/net.sourceforge.phpeclipse.wiki/plugin.xml +++ b/archive/net.sourceforge.phpeclipse.wiki/plugin.xml @@ -335,46 +335,61 @@ id="net.sourceforge.phpeclipse.wiki.actions.blogwiki.NewPostBlogHTMLAction"> - - + + + + + + + diff --git a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/OpenWikiLinkEditorAction.java b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/OpenWikiLinkEditorAction.java index de7a91f..d8b4537 100644 --- a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/OpenWikiLinkEditorAction.java +++ b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/OpenWikiLinkEditorAction.java @@ -9,6 +9,7 @@ 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.IProject; import org.eclipse.core.resources.IResource; import org.eclipse.core.resources.IResourceStatus; import org.eclipse.core.resources.ResourcesPlugin; @@ -146,7 +147,27 @@ public final class OpenWikiLinkEditorAction implements IEditorActionDelegate { openWikiFile(ei.getFile(), textRegion); } - void openWikiFile(IFile cfile, String word) { + public static void openWikiUrl(IProject project, String word) { + if (word != null && !word.equals("")) { + IFile cfile = project.getFile("dummy.wp"); + IFile file = getWikiFile(cfile, word); + try { + createNewFileIfNeeded(file, word); + // if (WikiEditorPlugin.getDefault().getPreferenceStore().getBoolean(WikiConstants.REUSE_EDITOR)) { + // saveIfNeeded(); + // getActivePage().reuseEditor(reusableEditor, new FileEditorInput(file)); + // } else { + IDE.openEditor(WikiEditorPlugin.getDefault().getActivePage(), file, true); + // redrawText(); + // } + } catch (Exception e) { + // WikiEditorPlugin.getDefault().logAndReport(WikiEditorPlugin.getResourceString(WikiConstants.RESOURCE_WIKI_ERROR_DIALOGUE_OPEN_WIKI_FILE_TITLE), + // WikiPlugin.getResourceString(WikiConstants.RESOURCE_WIKI_ERROR_DIALOGUE_OPEN_WIKI_FILE_TEXT), e); + } + } + } + + public static void openWikiFile(IFile cfile, String word) { if (word != null && !word.equals("")) { IFile file = getWikiFile(cfile, word); try { @@ -165,13 +186,13 @@ public final class OpenWikiLinkEditorAction implements IEditorActionDelegate { } } - private void createNewFileIfNeeded(IFile file, String word) throws CoreException { + private static void createNewFileIfNeeded(IFile file, String word) throws CoreException { if (!file.exists()) { createWikiFile(file, word); } } - private IFile getWikiFile(IFile file, String word) { + private static IFile getWikiFile(IFile file, String word) { String wikiFileName = Util.getWikiFileName(word, file, WikiEditorPlugin.HTML_OUTPUT_PATH); IPath path = new Path(wikiFileName); return ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path); @@ -186,11 +207,11 @@ public final class OpenWikiLinkEditorAction implements IEditorActionDelegate { * @return the new folder resource handle * @see #createFolder */ - private IFolder createFolderHandle(IPath folderPath) { + private static IFolder createFolderHandle(IPath folderPath) { return IDEWorkbenchPlugin.getPluginWorkspace().getRoot().getFolder(folderPath); } - private void createFolder(IFolder folderHandle, IProgressMonitor monitor) throws CoreException { + private static void createFolder(IFolder folderHandle, IProgressMonitor monitor) throws CoreException { try { // Create the folder resource in the workspace // Recursive to create any folders which do not exist already @@ -213,7 +234,7 @@ public final class OpenWikiLinkEditorAction implements IEditorActionDelegate { } } - private void createWikiFile(IFile file, String word) throws CoreException { + private static void createWikiFile(IFile file, String word) throws CoreException { IContainer parent = file.getParent(); if (parent instanceof IFolder && (!((IFolder) parent).exists())) { createFolder((IFolder) parent, null); diff --git a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/connect/BookmarkletServer.java b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/connect/BookmarkletServer.java new file mode 100644 index 0000000..6b7acff --- /dev/null +++ b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/connect/BookmarkletServer.java @@ -0,0 +1,184 @@ +package net.sourceforge.phpeclipse.wiki.actions.mediawiki.connect; +//Parts of this sources are copied and modified from the jEdit Wikipedia plugin: +//http://www.djini.de/software/wikipedia/index.html +// +//The modified sources are available under the "Common Public License" +//with permission from the original author: Daniel Wunsch + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.io.Writer; +import java.net.InetAddress; +import java.net.ServerSocket; +import java.net.Socket; +import java.net.SocketException; +import java.net.UnknownHostException; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import net.sourceforge.phpeclipse.wiki.actions.OpenWikiLinkEditorAction; + +public class BookmarkletServer implements Runnable { + // configuration + private InetAddress ADDRESS; + + int PORT = 8009; + + ServerSocket serverSocket = null; + + Thread thread = null; + + public BookmarkletServer() { + try { + ADDRESS = InetAddress.getByName("127.0.0.1"); + } catch (UnknownHostException e) { + e.printStackTrace(); + } + } + + /** set the port the server listens on - must be called before start() */ + public void setPort(int port) { + PORT = port; + } + + /** starts the server Thread */ + public void start() { + // Log.log(Log.NOTICE, plugin, "BookmarkletServer: " + "listening on " + ADDRESS.CanonicalHostName + ":" + PORT); + thread = new Thread(this); + thread.start(); + } + + /** stops the server Thread */ + public void stop() throws IOException { + thread.interrupt(); + serverSocket.close(); + // Log.log(Log.NOTICE, plugin, "BookmarkletServer: " + "stopped"); + } + + /** the server loop */ + public void run() { + try { + serverSocket = new ServerSocket(PORT, 2, ADDRESS); + for (;;) { + Socket clientSocket = null; + try { + if (thread.isInterrupted()) + break; + clientSocket = serverSocket.accept(); + clientSocket.setSoTimeout(1000); + if (thread.isInterrupted()) + break; + handleClient(clientSocket); + } catch (SocketException e) { + //Log.Log(Log.NOTICE, plugin, "BookmarkletServerSocket closed"); + System.err.println("WikipediaPlugin: BookmarkletServer: Socket closed"); + } catch (Exception e) { + // Log.log(Log.ERROR, plugin, BshUtil.extractDescription(e)); + } finally { + if (clientSocket != null) + clientSocket.close(); + } + } + serverSocket.close(); + } catch (IOException e) { + + } + } + + /** called when an incoming Connection is accepted */ + private void handleClient(Socket clientSocket) throws IOException { + if (!clientSocket.getInetAddress().equals(ADDRESS)) { + // Log.log(Log.NOTICE, plugin, "BookmarkletServer: " + "illegal remote address: " + clientSocket.InetAddress); + return; + } + + BufferedReader reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream(), "UTF-8")); + String line = reader.readLine(); + if (line == null) { + // Log.log(Log.NOTICE, plugin, "BookmarkletServer: " + "illegal request: " + line); + return; + } + + /* + * javascript:window.location.href='http://127.0.0.1:8009/open' Pattern pattern = Pattern.compile("^GET /(.*?) HTTP/1.[0-9]$", + * Pattern.DOTALL); + */ + + Pattern pattern = Pattern.compile("^GET /(.*?)/\\?(.*) HTTP/1.[0-9]$", Pattern.DOTALL); + Matcher matcher = pattern.matcher(line); + if (!matcher.matches()) { + // Log.log(Log.NOTICE, plugin, "BookmarkletServer: " + "illegal request: " + line); + return; + } + String action = matcher.group(1); + String url = matcher.group(2); + + /* + * BookmarkletServer.bsh: Host: 127.0.0.1:8009 User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.2) Gecko/20040810 + * Debian/1.7.2-2 User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows 98; Win 9x 4.90) Referer: + * http://kamelopedia.mormo.org/index.php/V%F6geln + */ + boolean respond = true; + for (;;) { + line = reader.readLine(); + if (line == null) + break; + if (line.length() == 0) + break; + String lower = line.toLowerCase(); + + /* + * // find out referer if (lower.startsWith("referer: ")) { url = line.substring("referer: ".length()); Log.log(Log.NOTICE, + * plugin, "BookmarkletServer: " + "got URL: " + url); } + */ + + // find out user-agent + if (lower.startsWith("user-agent: ") && lower.indexOf("mozilla") >= 0 && lower.indexOf("gecko") >= 0 + && lower.indexOf("msie") < 0 && lower.indexOf("opera") < 0 && lower.indexOf("safari") < 0) { + respond = false; + // Log.log(Log.DEBUG, plugin, "BookmarkletServer: " + "mozilla detected: skipping response"); + } + } + + /* + * if (url == null) { Log.log(Log.NOTICE, plugin, "BookmarkletServer: " + "missing referer header"); return; } + */ + + handleAction(action, url); + + if (respond) { + Writer writer = new OutputStreamWriter(clientSocket.getOutputStream(), "UTF-8"); + + writer.write("HTTP/1.0 301 Moved Permanently\r\n"); + writer.write("Cache-Control: no-cache\r\n"); + writer.write("Pragma: no-cache\r\n"); + writer.write("Expires: -1\r\n"); + writer.write("Location: " + url + "\r\n"); + writer.write("\r\n"); + + writer.write(""); + writer.write("goto " + url + ""); + writer.write(""); + writer.write("\r\n"); + + writer.flush(); + writer.close(); + } + + reader.close(); + } + + /** called when the Connection sent a Command */ + private void handleAction(String action, String url) { + if (!"open".equals(action)) { + // Log.log(Log.NOTICE, plugin, "BookmarkletServer: " + "unknown action: " + action); + return; + } + // TODO determine the global project and open editor for URL + OpenWikiLinkEditorAction.openWikiUrl(null, url); + // wikipedia.openURL(jEdit.getActiveView(), new URL(url), true); + } + +} \ No newline at end of file diff --git a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/connect/Content.java b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/connect/Content.java new file mode 100644 index 0000000..d701056 --- /dev/null +++ b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/connect/Content.java @@ -0,0 +1,23 @@ +package net.sourceforge.phpeclipse.wiki.actions.mediawiki.connect; +//Parts of this sources are copied and modified from the jEdit Wikipedia plugin: +//http://www.djini.de/software/wikipedia/index.html +// +//The modified sources are available under the "Common Public License" +//with permission from the original author: Daniel Wunsch + +public class Content { + String timestamp; + + String body; + + /** + * the Content of a Page + * + * @param timestamp + * @param body + */ + public Content(String timestamp, String body) { + this.timestamp = timestamp; + this.body = body; + } +} \ No newline at end of file diff --git a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/connect/Loaded.java b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/connect/Loaded.java new file mode 100644 index 0000000..c844cd6 --- /dev/null +++ b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/connect/Loaded.java @@ -0,0 +1,24 @@ +package net.sourceforge.phpeclipse.wiki.actions.mediawiki.connect; +//Parts of this sources are copied and modified from the jEdit Wikipedia plugin: +//http://www.djini.de/software/wikipedia/index.html +// +//The modified sources are available under the "Common Public License" +//with permission from the original author: Daniel Wunsch + +public class Loaded { + String actionURL; + + String charSet; + + String title; + + Content content; + + /** contains loaded remote content */ + Loaded(String actionURL, String charSet, String title, Content content) { + this.actionURL = charSet; + this.title = title; + this.content = content; + } + +} \ No newline at end of file diff --git a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/connect/MediaWikiConnector.java b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/connect/MediaWikiConnector.java new file mode 100644 index 0000000..b963dcc --- /dev/null +++ b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/connect/MediaWikiConnector.java @@ -0,0 +1,451 @@ +package net.sourceforge.phpeclipse.wiki.actions.mediawiki.connect; +//Parts of this sources are copied and modified from the jEdit Wikipedia plugin: +//http://www.djini.de/software/wikipedia/index.html +// +//The modified sources are available under the "Common Public License" +//with permission from the original author: Daniel Wunsch + +import java.io.IOException; +import java.io.InputStream; +import java.io.UnsupportedEncodingException; +import java.net.URLDecoder; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import net.sourceforge.phpeclipse.wiki.actions.mediawiki.config.IWikipedia; +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 org.apache.commons.httpclient.ConnectMethod; +import org.apache.commons.httpclient.HttpClient; +import org.apache.commons.httpclient.HttpConnection; +import org.apache.commons.httpclient.HttpException; +import org.apache.commons.httpclient.HttpMethod; +import org.apache.commons.httpclient.HttpState; +import org.apache.commons.httpclient.HttpStatus; +import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager; +import org.apache.commons.httpclient.NameValuePair; +import org.apache.commons.httpclient.URI; +import org.apache.commons.httpclient.UsernamePasswordCredentials; +import org.apache.commons.httpclient.methods.GetMethod; +import org.apache.commons.httpclient.methods.PostMethod; +import org.apache.commons.httpclient.protocol.Protocol; +import org.apache.commons.httpclient.util.EncodingUtil; + +/** + * This class gets the wikitext from a wikipedia edit page + * + * The basic coding was copied from the commons-httpclient example MediaWikiConnector.java + */ +public class MediaWikiConnector { + //pattern used to scarp an edit page + private static final Pattern BODY_PATTERN = Pattern.compile( + /* + * action=".*?title=(.*?)(&|\")
+ */ + ".*]*\\sid=\"editform\"[^>]*title=(.*?)&[^>]*>" + ".*]*\\sname=\"wpTextbox1\"[^>]*>(.*?)" + + ".*]*\\svalue=\"(\\d*)\"[^>]*\\sname=\"wpEdittime\"[^>]*>" + ".*", Pattern.DOTALL); + + //setup default user agent + final static public String userAgent = "PHPeclipse.de/0.0"; + + // create a ConnectionManager + private MultiThreadedHttpConnectionManager manager; + + private HttpClient client; + + /** + * Delay a new store to 1 second + */ + private Throttle storeThrottle = new Throttle(1000); + + class Throttle { + private long nextTime = 0; + + private final long minimumDelay; + + public Throttle(long minimumDelay) { + this.minimumDelay = minimumDelay; + } + + /** this is called from the client */ + public synchronized void delay() throws InterruptedException { + long delay = nextTime - System.currentTimeMillis(); + if (delay > 0) + Thread.sleep(delay); + nextTime = System.currentTimeMillis() + minimumDelay; + } + } + + public MediaWikiConnector() { + // bookmarklet + manager = new MultiThreadedHttpConnectionManager(); + manager.setMaxConnectionsPerHost(6); + manager.setMaxTotalConnections(18); + manager.setConnectionStaleCheckingEnabled(true); + // open the conversation + client = new HttpClient(manager); + //client.State.CookiePolicy = CookiePolicy.COMPATIBILITY; + //client.HostConfiguration.setHost(LOGON_SITE, LOGON_PORT, "http"); + } + + /** destructor freeing all resources. the Connection is not usable any more after calling this method */ + public void destroy() { + manager.shutdown(); + } + + /** log in - returns success */ + public boolean login(IWikipedia config, String actionUrl, String user, String password, boolean remember) + throws UnexpectedAnswerException, MethodException { + PostMethod method = new PostMethod(actionUrl); + method.setFollowRedirects(false); + method.addRequestHeader("User-Agent", userAgent); + NameValuePair[] params = new NameValuePair[] { new NameValuePair("title", config.getLoginTitle()), new NameValuePair("action", "submit"), + new NameValuePair("wpName", user), new NameValuePair("wpPassword", password), + new NameValuePair("wpRemember", remember ? "1" : "0"), new NameValuePair("wpLoginattempt", "submit") }; + method.addParameters(params); + + boolean result; + try { + int responseCode = client.executeMethod(method); + String responseBody = method.getResponseBodyAsString(); + + //### debugging + //log(responseBody); + // log(method); + + if (responseCode == 302 && responseBody.length() == 0 || responseCode == 200 + && responseBody.matches(config.getLoginSuccess())) { + result = true; + } else if (responseCode == 200 && responseBody.matches(config.getLoginWrongPw()) || responseCode == 200 + && responseBody.matches(config.getLoginNoUser())) { + result = false; + } else { + throw new UnexpectedAnswerException("login not successful: " + method.getStatusLine()); + } + } catch (HttpException e) { + throw new MethodException("method failed", e); + } catch (IOException e) { + throw new MethodException("method failed", e); + } finally { + method.releaseConnection(); + } + /* + * // display cookies System.err.println("login: " + result); for (var cookie : client.State.Cookies) { + * System.err.println("cookie: " + cookie); } + */ + + // remember state + SiteState state = SiteState.siteState(config); + state.loggedIn = result; + state.userName = user; + + return result; + } + + /** log out - return success */ + public boolean logout(IWikipedia config,String actionUrl) throws UnexpectedAnswerException, MethodException { + GetMethod method = new GetMethod(actionUrl); + method.setFollowRedirects(false); + method.addRequestHeader("User-Agent", userAgent); + NameValuePair[] params = new NameValuePair[] { new NameValuePair("title", config.getLogoutTitle()), + new NameValuePair("action", "submit") }; + method.setQueryString(EncodingUtil.formUrlEncode(params, config.getCharSet())); + + boolean result; + try { + int responseCode = client.executeMethod(method); + String responseBody = method.getResponseBodyAsString(); + // log(method); + + if (responseCode == 302 && responseBody.length() == 0 || responseCode == 200 + && responseBody.matches(config.getLoginSuccess())) { + // config.getloggedIn = false; + result = true; + } else if (responseCode == 200) { + //### should check for a failure message + result = false; + } else { + throw new UnexpectedAnswerException("logout not successful: " + method.getStatusLine()); + } + } catch (HttpException e) { + throw new MethodException("method failed", e); + } catch (IOException e) { + throw new MethodException("method failed", e); + } finally { + method.releaseConnection(); + } + + // remember state + SiteState state = SiteState.siteState(config); + state.loggedIn = false; + + return result; + } + + /** parses a returned editform into a Content object with UNIX-EOLs ("\n") */ + private Parsed parseBody(String charSet, String responseBody) throws PageNotEditableException, UnsupportedEncodingException { + Matcher matcher = BODY_PATTERN.matcher(responseBody); + if (!matcher.matches()) + throw new PageNotEditableException("cannot find editform form"); + + String title = matcher.group(1); + String body = matcher.group(2); + String timestamp = matcher.group(3); + + title = URLDecoder.decode(title, charSet); + body = body.replaceAll(""", "\"").replaceAll("'", "'").replaceAll("<", "<").replaceAll(">", ">").replaceAll( + "&", "&").replaceAll("\r\n", "\n").replace('\r', '\n'); + + return new Parsed(timestamp, title, body); + } + + /** load a Page Version - returns a Loaded Object */ + public Loaded load(String actionURL, String charSet, String title) throws UnexpectedAnswerException, MethodException, + PageNotEditableException { + GetMethod method = new GetMethod(actionURL); + method.setFollowRedirects(false); + method.addRequestHeader("User-Agent", userAgent); + NameValuePair[] params = new NameValuePair[] { new NameValuePair("title", title), new NameValuePair("action", "edit") }; + method.setQueryString(EncodingUtil.formUrlEncode(params, charSet)); + + Loaded result; + try { + int responseCode = client.executeMethod(method); + String responseBody = method.getResponseBodyAsString(); + // log(method); + + if (responseCode == 200) { + Parsed parsed = parseBody(charSet, responseBody); + Content content = new Content(parsed.timestamp, parsed.body); + result = new Loaded(actionURL, charSet, parsed.title, content); + } else { + throw new UnexpectedAnswerException("load not successful: expected 200 OK, got " + method.getStatusLine()); + } + } catch (HttpException e) { + throw new MethodException("method failed", e); + } catch (IOException e) { + throw new MethodException("method failed", e); + } finally { + method.releaseConnection(); + } + return result; + } + + /** + * store a Page Version - returns a Stored object + * + * @param config - WiKipedia predefined properties + * @param actionURL + * @param title + * @param content + * @param summary + * @param minorEdit + * @param watchThis + * @return + * @throws UnexpectedAnswerException + * @throws MethodException + * @throws PageNotEditableException + * @throws InterruptedException + */ + public Stored store(IWikipedia config, String actionUrl, String title, Content content, String summary, boolean minorEdit, + boolean watchThis) throws UnexpectedAnswerException, MethodException, PageNotEditableException, InterruptedException { + //### workaround: prevent too many stores at a time + storeThrottle.delay(); + + PostMethod method = new PostMethod(actionUrl); + method.setFollowRedirects(false); + method.addRequestHeader("User-Agent", userAgent); + method.addRequestHeader("Content-Type", PostMethod.FORM_URL_ENCODED_CONTENT_TYPE + "; charset=" + config.getCharSet()); + NameValuePair[] params = new NameValuePair[] { + // new NameValuePair("wpSection", ""), + // new NameValuePair("wpPreview", "Vorschau zeigen"), + // new NameValuePair("wpSave", "Artikel speichern"), + new NameValuePair("title", title), new NameValuePair("wpTextbox1", content.body), + new NameValuePair("wpEdittime", content.timestamp), new NameValuePair("wpSummary", summary), + new NameValuePair("wpSave", "yes"), new NameValuePair("action", "submit") }; + method.addParameters(params); + if (minorEdit) + method.addParameter("wpMinoredit", "1"); + if (watchThis) + method.addParameter("wpWatchthis", "1"); + + Stored result; + try { + int responseCode = client.executeMethod(method); + String responseBody = method.getResponseBodyAsString(); + // log(method); + + // since 11dec04 there is a single linefeed instead of an empty page.. trim() helps. + if (responseCode == 302 && responseBody.trim().length() == 0) { + // log("store successful, reloading"); + Loaded loaded = load(actionUrl, config.getCharSet(), title); + result = new Stored(actionUrl, config.getCharSet(), loaded.title, loaded.content, false); + } else if (responseCode == 200) { + // log("store not successful, conflict detected"); + Parsed parsed = parseBody(config.getCharSet(), responseBody); + Content cont = new Content(parsed.timestamp, parsed.body); + result = new Stored(actionUrl, config.getCharSet(), parsed.title, cont, true); + } else { + throw new UnexpectedAnswerException("store not successful: expected 200 OK, got " + method.getStatusLine()); + } + } catch (HttpException e) { + throw new MethodException("method failed", e); + } catch (IOException e) { + throw new MethodException("method failed", e); + } finally { + method.releaseConnection(); + } + return result; + } + + /** + * Get the text of a wikimedia article + * + */ + public static String getWikiRawText(String wikiname, String urlStr) { + // examples + // http://en.wikipedia.org/w/wiki.phtml?title=Main_Page&action=raw + // http://en.wikibooks.org/w/index.php?title=Programming:PHP:SQL_Injection&action=raw + // http://en.wikipedia.org/w/wiki.phtml?title=Talk:Division_by_zero&action=raw + HttpMethod method = null; + try { + if (urlStr == null) { + WikiEditorPlugin.getDefault().reportError("No Wikipedia URL configured", "URL-String == null"); + // urlStr = "http://en.wikipedia.org/w/wiki.phtml?title=" + wikiname + "&action=raw"; + } + URI uri = new URI(urlStr.toCharArray()); + + String schema = uri.getScheme(); + if ((schema == null) || (schema.equals(""))) { + schema = "http"; + } + Protocol protocol = Protocol.getProtocol(schema); + + HttpState state = new HttpState(); + + method = new GetMethod(uri.toString()); + String host = uri.getHost(); + int port = uri.getPort(); + + HttpConnection connection = new HttpConnection(host, port, protocol); + // timeout after 30 seconds + connection.setConnectionTimeout(30000); + connection.setProxyHost(System.getProperty("http.proxyHost")); + connection.setProxyPort(Integer.parseInt(System.getProperty("http.proxyPort", "80"))); + + if (System.getProperty("http.proxyUserName") != null) { + state.setProxyCredentials(null, null, new UsernamePasswordCredentials(System.getProperty("http.proxyUserName"), System + .getProperty("http.proxyPassword"))); + } + + if (connection.isProxied() && connection.isSecure()) { + method = new ConnectMethod(method); + } + + method.execute(state, connection); + + if (method.getStatusCode() == HttpStatus.SC_OK) { + // get the wiki text now: + String wikiText = method.getResponseBodyAsString(); + return wikiText; + // wrong text not always complete + // InputStream stream = method.getResponseBodyAsStream(); + // int byteLen = stream.available(); + // int count = 1; + // byte[] buffer = new byte[byteLen]; + // int len = 0; + // stream.read(buffer, 0, byteLen); + // String wikiText = new String(buffer); + // return wikiText; + // System.out.println(wikiText); + } + } catch (Throwable e) { + WikiEditorPlugin.log(e); + WikiEditorPlugin.getDefault().reportError("Exception occured", e.getMessage() + "\nSee stacktrace in /.metadata/.log file."); + } finally { + if (method != null) { + method.releaseConnection(); + } + } + return null; // no success in getting wiki text + } + + public static String getWikiEditTextarea(String wikiname, String urlStr) { + // examples + // http://en.wikipedia.org/w/wiki.phtml?title=Main_Page&action=edit + // http://en.wikibooks.org/w/wiki.phtml?title=Programming:PHP:SQL_Injection&action=edit + // http://en.wikipedia.org/w/wiki.phtml?title=Talk:Division_by_zero&action=edit + HttpMethod method = null; + try { + if (urlStr == null) { + urlStr = "http://en.wikipedia.org/w/wiki.phtml?title=" + wikiname + "&action=edit"; + } + // else { + // urlStr = urlStr + "?title=" + wikiname + "&action=edit"; + // } + URI uri = new URI(urlStr.toCharArray()); + + String schema = uri.getScheme(); + if ((schema == null) || (schema.equals(""))) { + schema = "http"; + } + Protocol protocol = Protocol.getProtocol(schema); + + HttpState state = new HttpState(); + + method = new GetMethod(uri.toString()); + String host = uri.getHost(); + int port = uri.getPort(); + + HttpConnection connection = new HttpConnection(host, port, protocol); + + connection.setProxyHost(System.getProperty("http.proxyHost")); + connection.setProxyPort(Integer.parseInt(System.getProperty("http.proxyPort", "80"))); + + if (System.getProperty("http.proxyUserName") != null) { + state.setProxyCredentials(null, null, new UsernamePasswordCredentials(System.getProperty("http.proxyUserName"), System + .getProperty("http.proxyPassword"))); + } + + if (connection.isProxied() && connection.isSecure()) { + method = new ConnectMethod(method); + } + + method.execute(state, connection); + + if (method.getStatusCode() == HttpStatus.SC_OK) { + // get the textareas wiki text now: + InputStream stream = method.getResponseBodyAsStream(); + int byteLen = stream.available(); + int count = 1; + byte[] buffer = new byte[byteLen]; + stream.read(buffer, 0, byteLen); + String wikiText = new String(buffer); + // String wikiText = method.getResponseBodyAsString(); + int start = wikiText.indexOf("", start + 1); + if (start != (-1)) { + int end = wikiText.indexOf(""); + wikiText = wikiText.substring(start + 1, end); + } + } + return wikiText; + // System.out.println(wikiText); + + } + } catch (Exception e) { + e.printStackTrace(); + } finally { + if (method != null) { + method.releaseConnection(); + } + } + return null; // no success in getting wiki text + } +} + diff --git a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/connect/Parsed.java b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/connect/Parsed.java new file mode 100644 index 0000000..d6f282c --- /dev/null +++ b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/connect/Parsed.java @@ -0,0 +1,20 @@ +package net.sourceforge.phpeclipse.wiki.actions.mediawiki.connect; +//Parts of this sources are copied and modified from the jEdit Wikipedia plugin: +//http://www.djini.de/software/wikipedia/index.html +// +//The modified sources are available under the "Common Public License" +//with permission from the original author: Daniel Wunsch + +public class Parsed { + String timestamp; + + String title; + + String body; + + public Parsed(String timeStamp, String title, String body) { + this.timestamp = timeStamp; + this.title = title; + this.body = body; + } +} \ No newline at end of file diff --git a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/connect/SiteState.java b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/connect/SiteState.java new file mode 100644 index 0000000..80ac042 --- /dev/null +++ b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/connect/SiteState.java @@ -0,0 +1,35 @@ +package net.sourceforge.phpeclipse.wiki.actions.mediawiki.connect; +//Parts of this sources are copied and modified from the jEdit Wikipedia plugin: +//http://www.djini.de/software/wikipedia/index.html +// +//The modified sources are available under the "Common Public License" +//with permission from the original author: Daniel Wunsch + +import java.util.IdentityHashMap; +import java.util.Map; + +import net.sourceforge.phpeclipse.wiki.actions.mediawiki.config.IWikipedia; + +public class SiteState { + String userName = null; + + boolean loggedIn = false; + + public SiteState() { + + } + + /** get or create a SiteState for a Site */ + public static SiteState siteState(IWikipedia site) { + SiteState state = (SiteState) siteStates.get(site); + if (state == null) { + state = new SiteState(); + siteStates.put(site, state); + } + return state; + } + + // maps from Site to SiteState + private static Map siteStates = new IdentityHashMap(); + +} \ No newline at end of file diff --git a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/connect/Stored.java b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/connect/Stored.java new file mode 100644 index 0000000..6b97046 --- /dev/null +++ b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/connect/Stored.java @@ -0,0 +1,29 @@ +package net.sourceforge.phpeclipse.wiki.actions.mediawiki.connect; + +//Parts of this sources are copied and modified from the jEdit Wikipedia plugin: +//http://www.djini.de/software/wikipedia/index.html +// +//The modified sources are available under the "Common Public License" +//with permission from the original author: Daniel Wunsch + +public class Stored { + String actionURL; + + String charSet; + + String title; + + Content content; + + boolean conflict; + + /** contains the reloaded remote Content or the remote conflict Content */ + public Stored(String actionURL, String charSet, String title, Content content, boolean conflict) { + this.actionURL = actionURL; + this.charSet = charSet; + this.title = title; + this.content = content; + this.conflict = conflict; + + } +} \ No newline at end of file diff --git a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/post/StoreWikipediaAction.java b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/post/StoreWikipediaAction.java index 3d3ad4f..0723e23 100644 --- a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/post/StoreWikipediaAction.java +++ b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/post/StoreWikipediaAction.java @@ -13,7 +13,7 @@ import java.util.Date; import java.util.List; import net.sourceforge.phpeclipse.wiki.actions.mediawiki.config.IWikipedia; -import net.sourceforge.phpeclipse.wiki.actions.mediawiki.config.WikipediaDE; +import net.sourceforge.phpeclipse.wiki.actions.mediawiki.config.WikipediaEN; import net.sourceforge.phpeclipse.wiki.actions.mediawiki.connect.Content; import net.sourceforge.phpeclipse.wiki.actions.mediawiki.connect.MediaWikiConnector; import net.sourceforge.phpeclipse.wiki.editor.WikiEditor; @@ -172,7 +172,7 @@ public class StoreWikipediaAction implements IEditorActionDelegate { try { IDocument doc = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput()); String url = generateUrl(configuration, configuration.getURL(), wikiName); - IWikipedia w = WikipediaDE.getInstance(); + IWikipedia w = WikipediaEN.getInstance(); MediaWikiConnector connector = new MediaWikiConnector(); Date d = new Date(); diff --git a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/editor/WikiEditorPlugin.java b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/editor/WikiEditorPlugin.java index fb22ed1..ee2c4ab 100644 --- a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/editor/WikiEditorPlugin.java +++ b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/editor/WikiEditorPlugin.java @@ -43,13 +43,15 @@ public class WikiEditorPlugin extends AbstractUIPlugin { public static final String WIKIPEDIA_GET_TEXT = "Wikipedia-Load Text"; + public static final String WIKIPEDIA_SET_TEXT = "Wikipedia-Store Text"; + public static final String WIKIPEDIA_SQL = "Wikipedia SQL access"; public static final String BLOG_A_WIKI = "Blog as Wiki Text"; public static final String BLOG_A_HTML = "Blog as HTML Text"; - public static final String[] CONFIGURATION_TYPES = { HTTP_QUERY, WIKIPEDIA_GET_TEXT, WIKIPEDIA_SQL, BLOG_A_WIKI, BLOG_A_HTML }; + public static final String[] CONFIGURATION_TYPES = { HTTP_QUERY, WIKIPEDIA_GET_TEXT, WIKIPEDIA_SET_TEXT, WIKIPEDIA_SQL, BLOG_A_WIKI, BLOG_A_HTML }; //image paths public static final String ICON_PATH = "icons/full/"; //$NON-NLS-1$ diff --git a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/internal/ConfigurationManager.java b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/internal/ConfigurationManager.java index 60e0b16..f73af5a 100644 --- a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/internal/ConfigurationManager.java +++ b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/internal/ConfigurationManager.java @@ -4,7 +4,7 @@ * are made available under the terms of the Common Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/cpl-v10.html - * +�* * Contributors: * IBM - Initial API and implementation **********************************************************************/ @@ -54,7 +54,7 @@ public class ConfigurationManager { if (ignorePreferenceChanges) return; String property = event.getProperty(); - if (property.equals("configurations")) { + if (property.equals(WikiEditorPlugin.PREF_STRING_CONFIGURATIONS)) { loadConfigurations(); } } @@ -167,7 +167,7 @@ public class ConfigurationManager { protected void saveConfigurations() { try { ignorePreferenceChanges = true; - XMLMemento memento = XMLMemento.createWriteRoot("configurations"); + XMLMemento memento = XMLMemento.createWriteRoot(WikiEditorPlugin.PREF_STRING_CONFIGURATIONS); Iterator iterator = configurations.iterator(); while (iterator.hasNext()) { @@ -178,7 +178,7 @@ public class ConfigurationManager { String xmlString = memento.saveToString(); Preferences prefs = WikiEditorPlugin.getDefault().getPluginPreferences(); - prefs.setValue("configurations", xmlString); + prefs.setValue(WikiEditorPlugin.PREF_STRING_CONFIGURATIONS, xmlString); WikiEditorPlugin.getDefault().savePluginPreferences(); } catch (Exception e) { Trace.trace(Trace.SEVERE, "Could not save Configurations", e); -- 1.7.1