Skip to content
Browse files

Fixes #13

  • Loading branch information...
1 parent 26e04f3 commit 063e4d908e4ba57c4f1143f39fea7ccee66fe988 @adutra committed Feb 13, 2012
View
166 src/main/java/fr/xebia/confluence2wordpress/wp/WordpressClient.java
@@ -43,9 +43,9 @@
*/
public class WordpressClient {
- private static final String CREATE_POST_METHOD_NAME = "metaWeblog.newPost";
+ private static final String CREATE_POST_METHOD_NAME = "c2w.newPost";
- private static final String UPDATE_POST_METHOD_NAME = "metaWeblog.editPost";
+ private static final String UPDATE_POST_METHOD_NAME = "c2w.editPost";
private static final String FIND_POST_BY_ID_METHOD_NAME = "metaWeblog.getPost"; //"blogger.getPost";
@@ -60,6 +60,7 @@
private static final String FIND_PAGE_ID_BY_SLUG_METHOD_NAME = "c2w.findPageIdBySlug";
private static final String PING_METHOD_NAME = "c2w.ping";
+
/**
* Very, VERY old version of the lib bundled with Confluence.
@@ -196,73 +197,10 @@ public WordpressPost findPostById(int postId) throws WordpressXmlRpcException {
params.add(wordpressConnection.getUsername());
params.add(wordpressConnection.getPassword());
Map<String, Object> map = invoke(FIND_POST_BY_ID_METHOD_NAME, params);
-
- /*Sample of the response structure:
- *
- * userid=3,
- * mt_allow_pings=1,
- * postid=40,
- * wp_author_id=3,
- * date_created_gmt=Wed May 04 14:32:57 CEST 2011,
- * wp_password=,
- * link=http://localhost/wordpress/2011/05/04/revue-de-presse-xebia-9/,
- * mt_keywords=tag1, tag2,
- * dateCreated=Wed May 04 16:32:57 CEST 2011,
- * categories=[test],
- * post_status=publish,
- * mt_allow_comments=1,
- * wp_slug=revue-de-presse-xebia-9,
- * permaLink=http://localhost/wordpress/2011/05/04/revue-de-presse-xebia-9/,
- * description=coucou c'est un draft,
- * custom_fields=[],
- * mt_text_more=,
- * mt_excerpt=,
- * sticky=false,
- * title=Revue de Presse Xebia,
- * wp_author_display_name=xebia-france
- */
-
- WordpressPost post = new WordpressPost();
- post.setPostId(postId);
- post.setDraft(false);
-
- Object authorId = map.get("wp_author_id");
- post.setAuthorId(authorId == null ? null : Integer.valueOf(authorId.toString()));
-
- Date dateCreated = (Date) map.get("dateCreated");
- post.setDateCreated(dateCreated);
-
- StringBuilder body = new StringBuilder();
- if(map.get("description") != null){
- body.append((String) map.get("description"));
- }
- if(map.get("mt_text_more") != null){
- body.append("<!--more-->");
- body.append((String) map.get("mt_text_more"));
- }
- post.setBody(body.toString());
-
- String title = (String) map.get("title");
- post.setTitle(title);
-
- post.setDraft( ! "publish".equals(map.get("post_status")));
-
- @SuppressWarnings("unchecked")
- List<String> categoryNames = (List<String>) map.get("categories");
- post.setCategoryNames(categoryNames);
-
- List<String> tagNames = CollectionUtils.split((String) map.get("mt_keywords"), ",");
- post.setTagNames(tagNames);
-
- String slug = (String) map.get("wp_slug");
- post.setPostSlug(slug);
-
- String permaLink = (String) map.get("permaLink");
- post.setLink(permaLink);
-
- return post;
+ return convertToPost(map);
}
+
/**
*
* @param postSlug
@@ -281,7 +219,7 @@ public Integer findPageIdBySlug(String postSlug) throws WordpressXmlRpcException
}
return result;
}
-
+
/**
*
* http://www.xmlrpc.com/metaWeblogApi
@@ -298,13 +236,12 @@ public WordpressPost post(WordpressPost post) throws WordpressXmlRpcException {
Vector<Object> params = new Vector<Object>();
- if(post.getPostId() == null) {
- params.add(wordpressConnection.getBlogId());
- } else {
- params.add(post.getPostId());
- }
+ params.add(wordpressConnection.getBlogId());
params.add(wordpressConnection.getUsername());
params.add(wordpressConnection.getPassword());
+ if(post.getPostId() != null) {
+ params.add(post.getPostId());
+ }
Hashtable<String,Object> map = new Hashtable<String,Object>();
map.put("title", post.getTitle());
@@ -335,18 +272,14 @@ public WordpressPost post(WordpressPost post) throws WordpressXmlRpcException {
//to publish ?
params.add( ! post.isDraft());
+ String methodName;
if(post.getPostId() == null) {
- Object ret = invoke(CREATE_POST_METHOD_NAME, params);
- int postId = Integer.parseInt(ret.toString());
- post.setPostId(postId);
+ methodName = CREATE_POST_METHOD_NAME;
} else {
- Boolean ret = invoke(UPDATE_POST_METHOD_NAME, params);
- if( ! ret) {
- throw new WordpressXmlRpcException("Post edit failed");
- }
+ methodName = UPDATE_POST_METHOD_NAME;
}
-
- return findPostById(post.getPostId());
+ Map<String, Object> ret = invoke(methodName, params);
+ return convertToPost(ret);
}
/**
@@ -417,6 +350,75 @@ private WordpressFile createAlternative(Map<String, ?> value, String mimeType, S
}
return alternative;
}
+
+ private WordpressPost convertToPost(Map<String, Object> map) {
+ /*Sample of the response structure:
+ *
+ * userid=3,
+ * mt_allow_pings=1,
+ * postid=40,
+ * wp_author_id=3,
+ * date_created_gmt=Wed May 04 14:32:57 CEST 2011,
+ * wp_password=,
+ * link=http://localhost/wordpress/2011/05/04/revue-de-presse-xebia-9/,
+ * mt_keywords=tag1, tag2,
+ * dateCreated=Wed May 04 16:32:57 CEST 2011,
+ * categories=[test],
+ * post_status=publish,
+ * mt_allow_comments=1,
+ * wp_slug=revue-de-presse-xebia-9,
+ * permaLink=http://localhost/wordpress/2011/05/04/revue-de-presse-xebia-9/,
+ * description=coucou c'est un draft,
+ * custom_fields=[],
+ * mt_text_more=,
+ * mt_excerpt=,
+ * sticky=false,
+ * title=Revue de Presse Xebia,
+ * wp_author_display_name=xebia-france
+ */
+
+ WordpressPost post = new WordpressPost();
+
+ Object postId = map.get("postid");
+ post.setPostId(Integer.valueOf(postId.toString()));
+ post.setDraft(false);
+
+ Object authorId = map.get("wp_author_id");
+ post.setAuthorId(authorId == null ? null : Integer.valueOf(authorId.toString()));
+
+ Date dateCreated = (Date) map.get("dateCreated");
+ post.setDateCreated(dateCreated);
+
+ StringBuilder body = new StringBuilder();
+ if(map.get("description") != null){
+ body.append((String) map.get("description"));
+ }
+ if(map.get("mt_text_more") != null){
+ body.append("<!--more-->");
+ body.append((String) map.get("mt_text_more"));
+ }
+ post.setBody(body.toString());
+
+ String title = (String) map.get("title");
+ post.setTitle(title);
+
+ post.setDraft( ! "publish".equals(map.get("post_status")));
+
+ @SuppressWarnings("unchecked")
+ List<String> categoryNames = (List<String>) map.get("categories");
+ post.setCategoryNames(categoryNames);
+
+ List<String> tagNames = CollectionUtils.split((String) map.get("mt_keywords"), ",");
+ post.setTagNames(tagNames);
+
+ String slug = (String) map.get("wp_slug");
+ post.setPostSlug(slug);
+
+ String permaLink = (String) map.get("permaLink");
+ post.setLink(permaLink);
+
+ return post;
+ }
@SuppressWarnings("unchecked")
private <T> T invoke(String methodName, Vector<Object> params) throws WordpressXmlRpcException {
View
55 src/main/php/confluence2wordpress.php
@@ -24,22 +24,6 @@
License: Apache License v. 2
*/
-/*
-Copyright 2011 Alexandre Dutra
-
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
-*/
-
require_once(ABSPATH . 'wp-admin/includes/plugin.php');
require_once(ABSPATH . 'wp-includes/post.php');
require_once(ABSPATH . 'wp-includes/link-template.php');
@@ -56,6 +40,8 @@
*/
function c2w_add_xmlrpc_methods( $methods ) {
$methods['c2w.ping'] = 'c2w_ping';
+ $methods['c2w.newPost'] = 'c2w_new_post';
+ $methods['c2w.editPost'] = 'c2w_edit_post';
$methods['c2w.getAuthors'] = 'c2w_get_authors';
$methods['c2w.findPageIdBySlug'] = 'c2w_get_page_id_by_slug';
$methods['c2w.uploadFile'] = 'c2w_upload_file';
@@ -113,6 +99,43 @@ function c2w_ping($args){
return $text;
}
+
+function c2w_new_post ($args) {
+
+ global $wp_xmlrpc_server;
+
+ $username = $args[1];
+ $password = $args[2];
+ $post_ID = $wp_xmlrpc_server->mw_newPost($args);
+
+ if ( is_wp_error( $post_ID ) )
+ return new IXR_Error(500, $post_ID->get_error_message());
+ if ( !$post_ID )
+ return new IXR_Error(500, __('Could not create post.'));
+
+ $post = $wp_xmlrpc_server->mw_getPost(array($post_ID, $username, $password));
+ return $post;
+}
+
+function c2w_edit_post ($args) {
+
+ global $wp_xmlrpc_server;
+
+ $username = $args[1];
+ $password = $args[2];
+ $post_ID = (int) $args[3];
+
+ $result = $wp_xmlrpc_server->mw_editPost($args);
+ if ( is_wp_error( $result ) )
+ return new IXR_Error(500, $result->get_error_message());
+ if ( !$result )
+ return new IXR_Error(500, __('Could not update post.'));
+
+ $post = $wp_xmlrpc_server->mw_getPost(array($post_ID, $username, $password));
+ return $post;
+}
+
+
/**
* Find a page ID by its post slug.
* Return null if post slug does not exist.

0 comments on commit 063e4d9

Please sign in to comment.
Something went wrong with that request. Please try again.