Skip to content

Commit

Permalink
Implement movable type xml-rpc API [Moritz Angermann]
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.techno-weenie.net/projects/mephisto/trunk@2402 567b1171-46fb-0310-a4c9-b4bef9110e78
  • Loading branch information
technoweenie committed Oct 27, 2006
1 parent 2163e57 commit a18ee2e
Show file tree
Hide file tree
Showing 9 changed files with 282 additions and 24 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
@@ -1,5 +1,7 @@
* SVN *

* Implement movable type xml-rpc API [Moritz Angermann]

* Enhance error recovery in the wordpress converter [Chris Petrilli]

* Fix Site#tags query to return distinct tags [DeLynn Berry]
Expand Down
22 changes: 12 additions & 10 deletions app/apis/meta_weblog_service.rb
Expand Up @@ -3,7 +3,7 @@ class MetaWeblogService < XmlRpcService
before_invocation :authenticate

def getCategories(blogid, username, password)
site.sections.find(:all).collect &:name
site.sections.find(:all, :order => 'id ASC').collect &:name
end

def getPost(postid, username, password)
Expand All @@ -19,13 +19,13 @@ def newPost(blogid, username, password, struct, publish)
article = @user.articles.build :site => site
post_it(article, username, password, struct, publish)
end

def editPost(postid, username, password, struct, publish)
article = @user.articles.find(postid)
post_it(article, username, password, struct, publish)
true
end

def deletePost(appkey, postid, username, password, publish)
article = @user.articles.find(postid)
article.destroy
Expand Down Expand Up @@ -65,22 +65,24 @@ def article_dto_from(article)
def article_url(article)
article.published? && article.full_permalink
end

def post_it(article, user, password, struct, publish)
article.attributes = {:updater => @user, :section_ids => Section.find(:all, :conditions => ['name IN (?)', struct['categories']]).collect(&:id),
:body => struct['description'].to_s, :title => struct['title'].to_s, :excerpt => struct['mt_excerpt'].to_s}
utc_date = Time.utc(struct['dateCreated'].year, struct['dateCreated'].month, struct['dateCreated'].day, struct['dateCreated'].hour, struct['dateCreated'].sec, struct['dateCreated'].min) rescue article.published_at

def post_it(article, user, password, struct, publish)
# make sure publish is true if it's 1 if not leave it the way it is.
publish = publish == 1 || publish
# if no categories are supplied do not attempt to set any.
article.section_ids = Section.find(:all, :conditions => ['name IN (?)', struct['categories']]).collect(&:id) if struct['categories']
article.attributes = {:updater => @user, :body => struct['description'].to_s, :title => struct['title'].to_s, :excerpt => struct['mt_excerpt'].to_s}
utc_date = Time.utc(struct['dateCreated'].year, struct['dateCreated'].month, struct['dateCreated'].day, struct['dateCreated'].hour, struct['dateCreated'].sec, struct['dateCreated'].min) rescue article.published_at || Time.now.utc
article.published_at = publish == true ? utc_date : nil
article.save!
article.id
end

def guess_content_type_from(name)
if name =~ /(png|gif|jpe?g)/i
"image/#{$1 == 'jpg' ? 'jpeg' : $1}"
else
'application/octet-stream'
end
end
end
end
84 changes: 83 additions & 1 deletion app/apis/movable_type_api.rb
@@ -1,6 +1,88 @@
class MovableTypeApi < ActionWebService::API::Base
inflect_names false

# Movable Type Programatic interface
# see: <http://www.movabletype.org/mt-static/docs/mtmanual_programmatic.html>
# supportedTextFilters has already been there!
# - Moritz Angermann
#
# mt.getRecentPostTitles
# Description: Returns a bandwidth-friendly list of the most recent posts in the system.
# Parameters: String blogid, String username, String password, int numberOfPosts
# Return value: on success, array of structs containing ISO.8601 dateCreated, String userid, String postid, String title; on failure, fault
# Notes: dateCreated is in the timezone of the weblog blogid
# Reference: <http://www.sixapart.com/developers/xmlrpc/movable_type_api/mtgetrecentposttitles.html>

#api_method :getRecentPostTitles,
# :expects => [ {:blogid => :string}, {:username => :string}, {:password => :string}, {:numberOfPosts => :int} ],
# :returns => [[MovableTypeStructs::Post]]

# mt.getCategoryList
# Description: Returns a list of all categories defined in the weblog.
# Parameters: String blogid, String username, String password
# Return value: on success, an array of structs containing String categoryId and String categoryName; on failure, fault.
# Reference: <http://www.sixapart.com/developers/xmlrpc/movable_type_api/mtgetcategorylist.html>
api_method :getCategoryList,
:expects => [ {:blogid => :string}, {:username => :string}, {:password => :string} ],
:returns => [[MovableTypeStructs::Category]]

# mt.getPostCategories
# Description: Returns a list of all categories to which the post is assigned.
# Parameters: String postid, String username, String password
# Return value: on success, an array of structs containing String categoryName, String categoryId, and boolean isPrimary; on failure, fault.
# Notes: isPrimary denotes whether a category is the post's primary category.
# Reference: <http://www.sixapart.com/developers/xmlrpc/movable_type_api/mtgetpostcategories.html>
api_method :getPostCategories,
:expects => [ {:postid => :string}, {:username => :string}, {:password => :string} ],
:returns => [[MovableTypeStructs::Category]]

# mt.setPostCategories
# Description: Sets the categories for a post.
# Parameters: String postid, String username, String password, array categories
# Return value: on success, boolean true value; on failure, fault
# Notes: the array categories is an array of structs containing String categoryId and boolean isPrimary. Using isPrimary to set the primary category is optional--in the absence of this flag, the first struct in the array will be assigned the primary category for the post.
# Reference: <http://www.sixapart.com/developers/xmlrpc/movable_type_api/mtsetpostcategories.html>
api_method :setPostCategories,
:expects => [ {:postid => :string}, {:username => :string}, {:password => :string}, {:categories => [MovableTypeStructs::PostCategory]} ],
:returns => [:bool]

# mt.supportedMethods
# Description: Retrieve information about the XML-RPC methods supported by the server.
# Parameters: none
# Return value: an array of method names supported by the server.
# Reference: <http://www.sixapart.com/developers/xmlrpc/movable_type_api/mtsupportedMethods.html>

api_method :supportedMethods,
:expects => [],
:returns => [[:string]]

# mt.supportedTextFilters
# Description: Retrieve information about the text formatting plugins supported by the server.
# Parameters: none
# Return value: an array of structs containing String key and String label. key is the unique string identifying a text formatting plugin, and label is the readable description to be displayed to a user. key is the value that should be passed in the mt_convert_breaks parameter to newPost and editPost.
# Reference: <http://www.sixapart.com/developers/xmlrpc/movable_type_api/mtsupportedtextfilters.html>

api_method :supportedTextFilters,
:expects => [],
:returns => [[MovableTypeStructs::TextFilter]]
end

# mt.getTrackbackPings
# Description: Retrieve the list of TrackBack pings posted to a particular entry. This could be used to programmatically retrieve the list of pings for a particular entry, then iterate through each of those pings doing the same, until one has built up a graph of the web of entries referencing one another on a particular topic.
# Parameters: String postid
# Return value: an array of structs containing String pingTitle (the title of the entry sent in the ping), String pingURL (the URL of the entry), and String pingIP (the IP address of the host that sent the ping).
# Reference: <http://www.sixapart.com/developers/xmlrpc/movable_type_api/mtgettrackbackpings.html>

# api_method :getTrackbackPings,
# :expects => [ {:postid => :string} ],
# :returns => [[MovableTypeStructs::Trackback]]

# mt.publishPost
# Description: Publish (rebuild) all of the static files related to an entry from your weblog. Equivalent to saving an entry in the system (but without the ping).
# Parameters: String postid, String username, String password
# Returns value: on success, boolean true value; on failure, fault
# Reference: <http://www.sixapart.com/developers/xmlrpc/movable_type_api/mtpublishpost.html>

#api_method :publishPost,
# :expects => [ {:postid => :string}, {:username => :string}, {:password => :string} ],
# :returns => [:bool]
end
39 changes: 38 additions & 1 deletion app/apis/movable_type_service.rb
@@ -1,7 +1,44 @@
class MovableTypeService < XmlRpcService
web_service_api MovableTypeApi
before_invocation :authenticate, :except => [:supportedMethods, :supportedTextFilters]

#def getRecentPostTitles(blogid, username, password, numberOfPosts)
# # FIXME not implemented
#end

def getCategoryList(blogid, username, password)
site.sections.find(:all, :order => 'id ASC').collect { |c| MovableTypeStructs::Category.new(:categoryId => c.id, :categoryName => c.name) }
end

def getPostCategories(postid, username, password)
article = @user.articles.find(postid)
article.sections.collect { |c| MovableTypeStructs::Category.new(:categoryId => c.id, :categoryName => c.name) }
end

def setPostCategories(postid, username, password, categories)
article = @user.articles.find(postid)
article.section_ids= categories.collect { |c| c.categoryId }
article.save!
true
end

def supportedMethods
MetaWeblogService.public_instance_methods(false).collect { |m| "metaWeblog.{#m}" } \
+ MovableTypeService.public_instance_methods(false).collect { |m| "mt.{#m}" }
end

def supportedTextFilters
FilteredColumn.filters.collect { |(key, filter)| MovableTypeStructs::TextFilter.new(:key => key, :label => filter.filter_name) }
end
end

# def getTrackbackPings,
# ...
# end

#def publishPost(postid, username, password)
# article = @user.articles.find(postid)
# #article. hmmm now what?
# true
#end

end
6 changes: 6 additions & 0 deletions app/apis/movable_type_structs/category.rb
@@ -0,0 +1,6 @@
module MovableTypeStructs
class Category < ActionWebService::Struct
member :categoryId, :string
member :categoryName, :string
end
end
8 changes: 8 additions & 0 deletions app/apis/movable_type_structs/post.rb
@@ -0,0 +1,8 @@
module MovableTypeStructs
class Post < ActionWebService::Struct
member :dateCreated, :time #ISO.8601
member :userid, :string
member :postid, :string
member :title, :string
end
end
7 changes: 7 additions & 0 deletions app/apis/movable_type_structs/post_category.rb
@@ -0,0 +1,7 @@
module MovableTypeStructs
class PostCategory < ActionWebService::Struct
member :categoryId, :string
member :categoryName, :string
member :isPrimary, :bool
end
end
7 changes: 7 additions & 0 deletions app/apis/movable_type_structs/trackback.rb
@@ -0,0 +1,7 @@
module MovableTypeStructs
class Trackback < ActionWebService::Struct
member :pingTitle, :string
member :pingURL, :string
member :pingIP, :string
end
end

0 comments on commit a18ee2e

Please sign in to comment.