# This patch file was generated by NetBeans IDE
# Following Index: paths are relative to: /var/www/GSoC/wordtrunk/wp-includes
# This patch can be applied using context Tools: Patch action on respective folder.
# It uses platform neutral UTF-8 encoding and \n newlines.
# Above lines and this line are ignored by the patching process.
Index: class-wp-xmlrpc-server.php
--- class-wp-xmlrpc-server.php Base (BASE)
+++ class-wp-xmlrpc-server.php Locally Modified (Based On LOCAL)
@@ -64,6 +64,7 @@
 			'wp.getMediaItem'		=> 'this:wp_getMediaItem',
 			'wp.getMediaLibrary'	=> 'this:wp_getMediaLibrary',
 			'wp.getPostFormats'     => 'this:wp_getPostFormats',
+                        'wp.editPost'           => 'this:wp_editPost',
 
 			// Blogger API
 			'blogger.getUsersBlogs' => 'this:blogger_getUsersBlogs',
@@ -1702,6 +1703,399 @@
 		return $formats;
 	}
 
+        /**
+         * Edit a  post
+         *
+         * @uses wp_update_post()
+         * @param array $args Method parameters. Contains:
+         *  - int     $post_id
+         *  - string  $username
+         *  - string  $password
+         *  - array     $content_struct
+         *  - boolean $publish optional. Defaults to true
+         * @return string post_id
+         */
+        function wp_editPost($args) {
+
+		$this->escape( $args );
+
+                $post_ID        = (int) $args[0];
+                $username       = $args[1];
+                $password       = $args[2];
+                $content_struct = $args[3];
+                $publish        = isset( $args[4] ) ? $args[4] : false;
+
+		if ( ! $user = $this->login( $username, $password ) )
+			return $this->error;
+
+                $post = wp_get_single_post( $post_ID, ARRAY_A );
+		if ( empty( $post['ID'] ) )
+			return new IXR_Error( 404, __( 'Invalid post ID.' ) );
+                
+                $post_type = get_post_type_object( $post['post_type'] );
+                if( ! current_user_can( $post_type->cap->edit_posts, $post_ID ) )
+                        return new IXR_Error( 401, __( 'Sorry, you are not allowed to create posts in this post type' ));
+
+                // this holds all the post data needed
+                $post_data = array();
+                $post_data['ID'] = $post_ID;
+                $post_data['post_status'] = $publish ? 'publish' : 'draft';
+
+                if( isset ( $content_struct["{$content_struct['post_type']}_status"] ) )
+                        $post_data['post_status'] = $content_struct["{$post_data['post_type']}_status"];
+
+
+                switch ( $post_data['post_status'] ) {
+
+                        case 'draft':
+                                break;
+                        case 'pending':
+                                break;
+                        case 'private':
+                                if( ! current_user_can( $post_type->cap->publish_posts, $post_ID ) )
+                                        return new IXR_Error( 401, __( 'Sorry, you are not allowed to create private posts in this post type' ));
+                                break;
+                        case 'publish':
+                                if( ! current_user_can( $post_type->cap->publish_posts, $post_ID ) )
+                                        return new IXR_Error( 401, __( 'Sorry, you are not allowed to publish posts in this post type' ));
+                                break;
+                        default:
+                                return new IXR_Error( 401, __( 'The post status specified is not valid' ) );
+                                break;
+
+                }
+
+                // Only use a password if one was given.
+                if ( isset( $content_struct['wp_password'] ) ) {
+
+                        if( ! current_user_can( $post_type->cap->publish_posts ) )
+                                return new IXR_Error( 401, __( 'Sorry, you are not allowed to create password protected posts in this post type' ));
+                        $post_data['post_password'] = $content_struct['wp_password'];
+
+                }
+
+                if ( isset( $content_struct['wp_slug'] ) )
+                        $post_data['post_name'] = $content_struct['wp_slug'];
+
+                if ( isset( $content_struct['wp_page_order'] ) ) {
+
+                        if( ! post_type_supports( $content_struct['post_type'] , 'page-attributes' ) )
+                                return new IXR_Error( 401, __( 'This post type does not support page attributes' ) );
+
+                        $post_data['menu_order'] = $content_struct['wp_page_order'];
+
+                }
+
+                if ( isset( $content_struct['wp_page_parent_id'] ) ) {
+
+                        if( ! $post_type->hierarchical )
+                                return new IXR_Error(401, __('This post type does not support post hierarchy'));
+
+                        // validating parent ID
+                        $parent_ID = (int)$content_struct['wp_page_parent_id'];
+                        if( $parent_ID != 0 ) {
+
+                                $parent_post = (array)wp_get_single_post( $parent_ID );
+                                if ( empty( $parent_post['ID'] ) )
+                                        return new IXR_Error( 401, __( 'Invalid parent ID.' ) );
+
+                                if ( $parent_post['post_type'] != $content_struct['post_type'] )
+                                        return new IXR_Error( 401, __( 'The parent post is of different post type' ) );
+
+                        }
+
+                        $post_data['post_parent'] = $content_struct['wp_page_parent_id'];
+
+                }
+
+                // page template is only supported only by pages
+                if ( isset( $content_struct['wp_page_template'] ) ) {
+
+                        if( $content_struct['post_type'] != 'page'  )
+                                return new IXR_Error(401, __('Page templates are only supported by pages'));
+
+                        // validating page template
+                        $page_templates = get_page_templates( );
+                        $page_templates['Default'] = 'default';
+
+                        if( ! array_key_exists( $content_struct['wp_page_template'], $page_templates ) )
+                                return new IXR_Error( 403, __( 'Invalid page template.' ) );
+
+                        $post_data['page_template'] = $content_struct['wp_page_template'];
+
+                }
+
+
+                // If an author id was provided then use it instead.
+                if( isset( $content_struct['wp_author_id'] ) && ( $user->ID != $content_struct['wp_author_id'] ) ) {
+
+                        if( ! post_type_supports( $content_struct['post_type'] , 'author' ) )
+                                return new IXR_Error( 401, __( 'This post type does not support to set author.' ) );
+
+                        if( ! current_user_can( $post_type->cap->edit_others_posts ) )
+                                return new IXR_Error( 401, __( 'You are not allowed to create posts as this user.' ) );
+
+                        $author_ID = (int)$content_struct['wp_author_id'];
+                        
+                        $author = get_userdata( $author_ID );
+                        if( ! $author )
+                                return new IXR_Error( 404, __( 'Invalid author ID.' ) );
+
+                        $post_data['post_author '] = $author_ID;
+
+                }
+
+
+                if( isset ( $content_struct['title'] ) ) {
+
+                        if( ! post_type_supports( $content_struct['post_type'] , 'title' ) )
+                                return new IXR_Error( 401, __( 'This post type does not support title attribute.' ) );
+                        $post_data['post_title'] = $content_struct['title'];
+
+                }
+
+                if( isset ( $content_struct['post_content'] ) ) {
+
+                        if( ! post_type_supports( $content_struct['post_type'] , 'editor' ) )
+                                return new IXR_Error( 401, __( 'This post type does not support post content.' ) );
+                        $post_data['post_content'] = $content_struct['post_content'];
+
+                }
+
+                if( isset ( $content_struct['mt_excerpt'] ) ) {
+
+                        if( ! post_type_supports( $content_struct['post_type'] , 'excerpt' ) )
+                                return new IXR_Error( 401, __( 'This post type does not support post excerpt.' ) );
+                        $post_data['post_excerpt'] = $content_struct['mt_excerpt'];
+
+                }
+
+                if( isset( $content_struct['mt_allow_comments'] ) ) {
+
+                        if( ! post_type_supports( $content_struct['post_type'], 'comments' ) )
+                                return new IXR_Error( 401, __( 'This post type does not support comments.' ) );
+
+                        if ( ! is_numeric( $content_struct['mt_allow_comments'] ) ) {
+
+                                switch ( $content_struct['mt_allow_comments'] ) {
+                                        case 'closed':
+                                                $post_data['comment_status']= 'closed';
+                                                break;
+                                        case 'open':
+                                                $post_data['comment_status'] = 'open';
+                                                break;
+                                        default:
+                                                return new IXR_Error( 401, __ ( 'Invalid comment option' ) );
+                                }
+
+                        } else {
+
+                                switch ( (int) $content_struct['mt_allow_comments'] ) {
+                                        case 0:
+                                        case 2:
+                                                $post_data['comment_status'] = 'closed';
+                                                break;
+                                        case 1:
+                                                $post_data['comment_status'] = 'open';
+                                                break;
+                                        default:
+                                                return new IXR_Error( 401, __ ( 'Invalid ping option.' ) );
+                                }
+
+                        }
+
+                }
+
+
+                if( isset( $content_struct['mt_allow_pings'] ) ) {
+
+                        if( ! post_type_supports( $content_struct['post_type'], 'trackbacks' ) )
+                                return new IXR_Error(401, __('This post type does not support trackbacks'));
+
+                        if ( ! is_numeric( $content_struct['mt_allow_pings'] ) ) {
+
+                                switch ( $content_struct['mt_allow_pings'] ) {
+                                        case 'closed':
+                                                $post_data['ping_status']= 'closed';
+                                                break;
+                                        case 'open':
+                                                $post_data['ping_status'] = 'open';
+                                                break;
+                                        default:
+                                                break;
+                                }
+
+                        } else {
+
+                                switch ( (int) $content_struct['mt_allow_pings'] ) {
+                                        case 0:
+                                        case 2:
+                                                $post_data['ping_status'] = 'closed';
+                                                break;
+                                        case 1:
+                                                $post_data['ping_status'] = 'open';
+                                                break;
+                                        default:
+                                                break;
+                                }
+
+                        }
+
+                }
+
+                if( isset( $content_struct['mt_text_more'] ) ) {
+
+                        $post_data['post_more'] = $content_struct['mt_text_more'];
+                        $post_data['post_content'] = $post_data['post_content'] . '<!--more-->' . $post_data['post_more'];
+
+                }
+
+                if ( isset( $content_struct['mt_tb_ping_urls'] ) ) {
+
+                        $post_data['to_ping'] = $content_struct['mt_tb_ping_urls'];
+                        if ( is_array($to_ping) )
+                                $post_data['to_ping'] = implode(' ', $to_ping);
+                        
+                }
+
+                // Do some timestamp voodoo
+                if ( ! empty( $content_struct['date_created_gmt'] ) )
+                        $dateCreated = str_replace( 'Z', '', $content_struct['date_created_gmt']->getIso() ) . 'Z'; // We know this is supposed to be GMT, so we're going to slap that Z on there by force
+                elseif ( !empty( $content_struct['dateCreated']) )
+                        $dateCreated = $content_struct['dateCreated']->getIso();
+
+                if ( ! empty( $dateCreated ) ) {
+
+                        $post_data['post_date'] = get_date_from_gmt(iso8601_to_datetime($dateCreated));
+                        $post_data['post_date_gmt'] = iso8601_to_datetime($dateCreated, 'GMT');
+                        
+                } 
+
+                // we got everything we need
+                $post_ID = wp_update_post( $post_data, true );
+
+                if ( is_wp_error( $post_ID ) )
+                        return new IXR_Error(500, $post_ID->get_error_message());
+
+                if ( ! $post_ID )
+                        return new IXR_Error(500, __('Sorry, your entry could not be posted. Something wrong happened.'));
+
+                if( isset ( $content_struct['sticky'] ) ) {
+
+                        $sticky = $content_struct['sticky'] ? true : false;
+                        
+                        if( $sticky ) {
+
+                                if( $post_data['post_status'] != 'publish' )
+                                        return new IXR_Error( 401, __( 'Only published posts can be made sticky.' ));
+
+                                if( ! current_user_can( $post_type->cap->edit_others_posts ) )
+                                        return new IXR_Error( 401, __( 'Sorry, you are not allowed to stick this post.' ) );
+
+                                stick_post( $post_ID );
+
+                        } else {
+
+                                unstick_post( $post_ID );
+
+                        }
+                    
+                }
+
+                if( isset ( $content_struct['custom_fields'] ) ) {
+
+                        if( ! post_type_supports( $content_struct['post_type'], 'custom-fields' ) )
+                                return new IXR_Error(401, __('This post type does not support custom fields'));
+                        $wp_xmlrpc_server->set_custom_fields( $post_ID, $content_struct['custom_fields'] );
+                        
+                }
+
+                $post_type_taxonomies = get_object_taxonomies( $post['post_type'] );
+
+                if( isset( $content_struct['terms'] ) ) {
+
+                        $terms = $content_struct['terms'];
+                        $taxonomies = array_keys( $terms );
+
+                        // validating term ids
+                        foreach( $taxonomies as $taxonomy ) {
+
+                                if( ! in_array( $taxonomy , $post_type_taxonomies ) )
+                                        return new IXR_Error( 401, __( 'Sorry, one of the given taxonomy is not supported by the post type.' ));
+
+                                $term_ids = $terms[ $taxonomy ];
+                                foreach ( $term_ids as $term_id) {
+
+                                        $term = get_term( $term_id, $taxonomy );
+
+                                        if ( is_wp_error( $term ) )
+                                                return new IXR_Error( 500, $term->get_error_message() );
+
+                                        if ( ! $term )
+                                                return new IXR_Error( 401, __( 'Invalid term ID' ) );
+
+                                }
+
+                        }
+
+                        foreach( $taxonomies as $taxonomy ) {
+
+                                $term_ids = $terms[ $taxonomy ];
+                                $term_ids = array_map( 'intval', $term_ids );
+                                $term_ids = array_unique( $term_ids );
+                                wp_set_object_terms( $post_ID , $term_ids, $taxonomy , $append);
+
+                        }
+
+                        return true;
+
+                }
+                
+                // backward compatiblity
+		if ( isset( $content_struct['categories'] ) ) {
+
+                        if( ! in_array( 'category', $post_type_taxonomies ) )
+                                return new IXR_Error( 401, __( 'Sorry, Categories are not supported by the post type' ));
+
+			$category_names = $content_struct['categories'];
+
+                        foreach( $category_names as $category_name ) {
+                                $category_ID = get_cat_ID( $category_name );
+                                if( ! $category_ID )
+                                        return new IXR_Error( 401, __( 'Sorry, one of the given categories does not exist!' ));
+                                $post_categories[] = $category_ID;
+                        }
+
+                        wp_set_post_categories ($post_ID, $post_categories );
+
+		}
+
+                if( isset( $content_struct['mt_keywords'] ) ) {
+
+                        if( ! in_array( 'post_tag' , $post_type_taxonomies ) )
+                                return new IXR_Error( 401, __( 'Sorry, post tags are not supported by the post type' ));
+
+                        wp_set_post_terms( $post_id, $tags, 'post_tag', false); // append is set false here
+                }
+
+                if( isset( $content_struct['wp_post_format'] ) ) {
+
+                        if( ! in_array( 'post_format' , $post_type_taxonomies ) )
+                                return new IXR_Error( 401, __( 'Sorry, post formats are not supported by the post type' ));
+
+                        wp_set_post_terms( $post_ID, array( 'post-format-' . $content_struct['wp_post_format'] ), 'post_format' );
+
+                }
+
+                // Handle enclosures
+                $thisEnclosure = isset($content_struct['enclosure']) ? $content_struct['enclosure'] : null;
+                $wp_xmlrpc_server->add_enclosure_if_new($post_ID, $thisEnclosure);
+                $wp_xmlrpc_server->attach_uploads( $post_ID, $post_data['post_content'] );
+
+                return strval( $post_ID );
+
+        }
+
 	/* Blogger API functions.
 	 * specs on http://plant.blogger.com/api and http://groups.yahoo.com/group/bloggerDev/
 	 */
