From: axelcl Date: Sat, 5 Feb 2005 18:44:12 +0000 (+0000) Subject: Handle the new "wpEditToken" input parameter for upload X-Git-Url: http://git.phpeclipse.com Handle the new "wpEditToken" input parameter for upload --- 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 index d701056..6d2334b 100644 --- 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 @@ -7,12 +7,10 @@ package net.sourceforge.phpeclipse.wiki.actions.mediawiki.connect; public class Content { String timestamp; - String body; /** * the Content of a Page - * * @param timestamp * @param body */ 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 index a9eba4a..9225b17 100644 --- 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 @@ -54,6 +54,10 @@ public class MediaWikiConnector { ".*]*\\sid=\"editform\"[^>]*title=(.*?)&[^>]*>" + ".*]*\\sname=\"wpTextbox1\"[^>]*>(.*?)" + ".*]*\\svalue=\"(\\d*)\"[^>]*\\sname=\"wpEdittime\"[^>]*>" + ".*", Pattern.DOTALL); + // + private static final Pattern EDIT_TOKEN = Pattern.compile(".*.*", + Pattern.DOTALL); + //setup default user agent final static public String userAgent = "plog4u.org/0.0"; @@ -180,7 +184,7 @@ public class MediaWikiConnector { // log(method); if (responseCode == 302 && responseBody.length() == 0 || responseCode == 200 - && responseBody.matches(config.getLoginSuccess())) { + && responseBody.matches(config.getLogoutSuccess())) { // config.getloggedIn = false; result = true; } else if (responseCode == 200) { @@ -205,6 +209,58 @@ public class MediaWikiConnector { return result; } + /** parses a returned editform for the sessions wpEditToken */ + private String parseEditToken(String charSet, String responseBody) throws PageNotEditableException { + Matcher matcher = EDIT_TOKEN.matcher(responseBody); + if (!matcher.matches()) { + return null; + } + + return matcher.group(1); + } + + /** + * returns the edit token or null if no token is available + * + * @param actionURL + * @param charSet + * @param title + * @return + * @throws UnexpectedAnswerException + * @throws MethodException + * @throws PageNotEditableException + */ + public String loadEditToken(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)); + + try { + int responseCode = client.executeMethod(method); + String responseBody = method.getResponseBodyAsString(); + // log(method); + + if (responseCode == 200) { + String parsed = parseEditToken(charSet, responseBody); + if (parsed != null && parsed.length() == 0) { + return null; + } + return parsed; + } 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(); + } + } + /** 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); @@ -214,12 +270,13 @@ public class MediaWikiConnector { String title = matcher.group(1); String body = matcher.group(2); String timestamp = matcher.group(3); - + String tokenEdit = null; + // String tokenEdit = matcher.group(4); 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); + return new Parsed(timestamp, title, body, tokenEdit); } /** load a Page Version - returns a Loaded Object */ @@ -305,16 +362,21 @@ public class MediaWikiConnector { * @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 { + public Stored store(IWikipedia config, String editToken, 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()); + if (editToken == null) { + // in some versions editToken isn't supported + editToken = " "; + } NameValuePair[] params = new NameValuePair[] { // new NameValuePair("wpSection", ""), // new NameValuePair("wpPreview", "Vorschau zeigen"), @@ -323,6 +385,7 @@ public class MediaWikiConnector { new NameValuePair("wpTextbox1", content.body), new NameValuePair("wpEdittime", content.timestamp), new NameValuePair("wpSummary", summary), + new NameValuePair("wpEditToken", editToken), new NameValuePair("wpSave", "yes"), new NameValuePair("action", "submit") }; method.addParameters(params); @@ -500,7 +563,7 @@ public class MediaWikiConnector { try { // timeout after xx seconds connection.setConnectionTimeout(Integer.parseInt(timeout)); - + if (proxyHost.length() > 0) { String proxyPort = prefs.getString(WikiEditorPlugin.HTTP_PROXYPORT); connection.setProxyHost(proxyHost); @@ -512,7 +575,7 @@ public class MediaWikiConnector { state.setProxyCredentials(null, null, new UsernamePasswordCredentials(proxyUserName, proxyPassWord)); } } - + } catch (Exception e) { } @@ -520,7 +583,7 @@ public class MediaWikiConnector { } private void setHTTPClientParameters(HttpClient client) { - + Preferences prefs = WikiEditorPlugin.getDefault().getPluginPreferences(); String timeout = prefs.getString(WikiEditorPlugin.HTTP_TIMEOUT); String proxyHost = prefs.getString(WikiEditorPlugin.HTTP_PROXYHOST); @@ -528,11 +591,11 @@ public class MediaWikiConnector { try { // timeout after xx seconds client.setConnectionTimeout(Integer.parseInt(timeout)); - + if (proxyHost.length() > 0) { String proxyPort = prefs.getString(WikiEditorPlugin.HTTP_PROXYPORT); HostConfiguration conf = new HostConfiguration(); - client.setHostConfiguration(conf); + client.setHostConfiguration(conf); conf.setProxy(proxyHost, Integer.parseInt(proxyPort)); String proxyUserName = prefs.getString(WikiEditorPlugin.HTTP_PROXYUSERNAME); @@ -543,12 +606,13 @@ public class MediaWikiConnector { client.setState(state); } } - + } catch (Exception e) { } - + } + public static void main(String[] args) { MediaWikiConnector mwc = new MediaWikiConnector(); try { 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 index 8adf257..153572e 100644 --- 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 @@ -10,85 +10,101 @@ import java.io.InputStream; public class Parsed { /** - * 6lt;page6gt; XML data from Wikipedia Special:Export pages - * may be null - * + * 6lt;page6gt; XML data from Wikipedia Special:Export pages may be null + * */ - /*package private*/ String xmlData=null; + /* package private */String xmlData = null; + /** * timeStamp represented in XML format from Wikipedia Special:Export pages */ - /*package private*/ String timestamp=null; + /* package private */String timestamp = null; - /*package private*/ String title=null; + /* package private */String editToken = null; - /*package private*/ String body=null; + /* package private */String title = null; - /*package private*/ Parsed() { + /* package private */String body = null; + + /* package private */Parsed() { } - public Parsed(String timeStamp, String title, String body) { + public Parsed(String timeStamp, String title, String body, String editToken) { this.xmlData = ""; this.timestamp = timeStamp; this.title = title; this.body = body; + this.editToken = editToken; } - /* (non-Javadoc) + + /* + * (non-Javadoc) + * * @see java.lang.Object#toString() */ - public String toString() { + public String toString() { StringBuffer buffer = new StringBuffer(); buffer.append("==>Title:\n"); - if (title!=null) { + if (title != null) { buffer.append(title); } buffer.append("==>Timestamp:\n"); - if (timestamp!=null) { + if (timestamp != null) { buffer.append(timestamp); } buffer.append("==>Body:\n"); - if (body!=null) { + if (body != null) { buffer.append(body); } return buffer.toString(); } + /** * @return Returns the body. */ public String getBody() { return body; } + /** * @return Returns the timestamp. */ public String getTimestamp() { return timestamp; } - + public String getDateTimestamp() { - if (timestamp!=null) { + if (timestamp != null) { StringBuffer buffer = new StringBuffer(); // 2004-11-22T12:41:10Z - buffer.append(timestamp.substring(0,4)); //year - buffer.append(timestamp.substring(5,7)); //month - buffer.append(timestamp.substring(8,10)); //day - buffer.append(timestamp.substring(11,13));//hour - buffer.append(timestamp.substring(14,16));//minute - buffer.append(timestamp.substring(17,19));//second + buffer.append(timestamp.substring(0, 4)); //year + buffer.append(timestamp.substring(5, 7)); //month + buffer.append(timestamp.substring(8, 10)); //day + buffer.append(timestamp.substring(11, 13));//hour + buffer.append(timestamp.substring(14, 16));//minute + buffer.append(timestamp.substring(17, 19));//second return buffer.toString(); - } + } return ""; } + /** * @return Returns the title. */ public String getTitle() { return title; } + /** * @return Returns the xmlData. */ public String getXmlData() { return xmlData; } + /** + * @return Returns the editToken. + */ + public String getEditToken() { + return editToken; + } } \ No newline at end of file diff --git a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/post/PostJob.java b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/post/PostJob.java index 4ee5c35..7726ecc 100644 --- a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/post/PostJob.java +++ b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/post/PostJob.java @@ -2,7 +2,6 @@ package net.sourceforge.phpeclipse.wiki.actions.mediawiki.post; import java.io.IOException; import java.io.InputStream; -import java.io.InputStreamReader; import java.io.StringWriter; import java.util.HashMap; @@ -83,14 +82,14 @@ public class PostJob extends WorkspaceJob { return template; } - private void uploadWiki(String timestamp, String body, MediaWikiConnector connector, String actionUrl, String wikiName) - throws UnexpectedAnswerException, MethodException, PageNotEditableException, InterruptedException { + 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, actionUrl, wikiName, content, "", false, false); + connector.store(wikipedia, editToken, actionUrl, wikiName, content, "", false, false); } @@ -121,6 +120,12 @@ public class PostJob extends WorkspaceJob { 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]; @@ -158,7 +163,7 @@ public class PostJob extends WorkspaceJob { console.println("File: " + file.getLocation().toString() + "\n==>upload not allowed; Wiki text contains no content"); } else { monitor.subTask("Upload: " + file.getLocation().toString()); - uploadWiki(timestamp, body, connector, actionUrl, wikiURLTitle); + uploadWiki(timestamp, editToken, body, connector, actionUrl, wikiURLTitle); } } catch (CoreException e1) { 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 85dc17d..3c9f7bb 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 @@ -247,7 +247,7 @@ public class StoreWikipediaAction implements IEditorActionDelegate { boolean success = connector.login(wikipediaProperties, actionUrl, user, password, false); if (success) { - connector.store(wikipediaProperties, actionUrl, wikiName, content, "", false, false); + connector.store(wikipediaProperties, null, actionUrl, wikiName, content, "", false, false); connector.logout(wikipediaProperties, actionUrl); } } catch (Exception e) { diff --git a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/editor/WikiEditor.java b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/editor/WikiEditor.java index 8339692..dc9c7a4 100644 --- a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/editor/WikiEditor.java +++ b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/editor/WikiEditor.java @@ -8,8 +8,6 @@ package net.sourceforge.phpeclipse.wiki.editor; import net.sourceforge.phpeclipse.wiki.editor.model.WikipediaSection; -import net.sourceforge.phpeclipse.wiki.editor.model.WikipediaText; -import net.sourceforge.phpeclipse.wiki.preferences.Util; import org.eclipse.jface.action.IAction; import org.eclipse.jface.text.IDocument;