--- - 2007-01-17 23:21:54.000000000 -0500 +++ /dev/fd/63 2007-01-17 23:21:54.000000000 -0500 @@ -47,14 +47,16 @@ endpoint_list = IO.readlines(BLOG_ACCOUNTS_FILE) else endpoint_list = [ <<-TEXT ] -# Blogging Account List +# List of Blogs +# # Enter a blog name followed by the endpoint URL (see Help for proxy config) -# Blog Name URL -# example http://user@example.com/xmlrpc +# +# Blog Name URL mode +# example http://user@example.com/xmlrpc wp TEXT end endpoint_list.push(self.endpoints[self.endpoint] + " " + self.endpoint + - "\n") + self.mode + "\n") File.open(BLOG_ACCOUNTS_FILE, "w") do | file | file.write(endpoint_list.join) end @@ -91,12 +93,16 @@ def read_endpoints @endpoints = {} + @modes = {} if File.exist?(BLOG_ACCOUNTS_FILE) IO.readlines(BLOG_ACCOUNTS_FILE).each do | line | next if line =~ /^\s*#/ - if line =~ /^(.+?)\s+(https?:\/\/.+)/ - @endpoints[$1] = $2 - @endpoints[$2] = $1 + if line =~ /^(.+?)\s+(https?:\/\/.+?)\s(\w+)?/ # Match Blog URL Mode(optional) + @endpoints[$1] = $2 # endpoints[Blog]=URL + @endpoints[$2] = $1 # endpoints[URL]=Blog + if ! $3.nil? + @modes[$2] = $3 # modes[URL]=Mode + end end end end @@ -138,20 +144,6 @@ end end - # guess the mode based on the endpoint path - @mode = ENV['TM_BLOG_MODE'] - if @mode == nil - case @endpoint - when %r{/mt-xmlrpc\.cgi}, %r{/backend/xmlrpc} - @mode = 'mt' - when %r{/xmlrpc(\.php)?} - @mode = 'wp' - else - # our default - @mode = 'mt' - end - end - if @endpoint =~ /^https?:\/\/([^\/]+?)(\/.+)$/ @host = $1 @path = $2 @@ -257,7 +249,7 @@ end date_created = DateTime.parse(@headers['date']) if @headers['date'] - if date_created && self.mode != 'mt' && (self.mode != 'wp' || ENV['TM_SEND_DATE_TO_WP']) then + if date_created && self.mode != 'mt' && (self.mode != 'wp' || ENV['TM_SEND_DATE_TO_WP']) && self.mode != 'drupal' then # Convert to GMT and then to an XMLRPC:DateTime object to # workaround xmlrpc/create.rb’s poor handling of DateTime. d = date_created.new_offset(0) @@ -270,7 +262,7 @@ @post['mt_allow_pings'] = @headers['pings'] =~ /\b(on|1|y(es)?)\b/i ? '1' : '0' if @headers['pings'] @post['mt_tags'] = @headers['tags'] if @headers['tags'] @post['mt_basename'] = @headers['basename'] if @headers['basename'] - elsif self.mode == 'wp' + elsif self.mode == 'wp' || self.mode == 'drupal' @post['mt_allow_comments'] = @headers['comments'] =~ /\b(on|1|y(es)?)\b/i ? 'open' : 'closed' if @headers['comments'] @post['mt_allow_pings'] = @headers['pings'] =~ /\b(on|1|y(es)?)\b/i ? 'open' : 'closed' if @headers['pings'] end @@ -288,6 +280,29 @@ fetch_credentials_from_keychain() unless @password @password end + + def mode + # The mode can be determined via the endpoint in the BLOG_ACCOUNTS_FILE + self.endpoint + + # If not specified, guess the mode based on the endpoint path + @mode = ENV['TM_BLOG_MODE'] + if (@mode == nil) && (@modes == nil) + case @endpoint + when %r{/mt-xmlrpc\.cgi}, %r{/backend/xmlrpc} + @mode = 'mt' + when %r{/xmlrpc(\.php)?} + @mode = 'wp' + else + # our default + @mode = 'mt' + end + else + # Mode was specified in BLOG_ACCOUNTS_FILE + @mode = self.modes[@endpoint] + end + @mode + end def post parse_post() if @post == nil @@ -343,6 +358,11 @@ @endpoints end + def modes + read_endpoints() unless @modes + @modes + end + def client current_endpoint = endpoint.dup current_endpoint.sub!(/#.+/, '') if current_endpoint =~ /#.+/ @@ -400,6 +420,11 @@ if (self.mode == 'wp') && self.post['category'] cats = self.post['category'].split(/,/) cats.each { | cat | doc += "Category: #{cat}\n" } + # Drupal's categories are not kept in the post + # but can be fetched with mt.getPostCategories + elsif (self.mode == 'drupal') + cats = self.client.call("mt.getPostCategories", self.post_id, self.username, self.password) + cats.each { | cat | doc += "Category: #{cat['categoryName']}\n" } end doc += "Format: #{self.post['mt_convert_breaks']}\n" if self.post['mt_convert_breaks'] @@ -526,6 +551,27 @@ else self.post_id = client.newPost(self.blog_id, self.username, current_password, self.post, self.publish) end + + # Setting post categories with drupal blogs + if (self.mode == 'drupal') && (! @post['categories'].nil?) + # Get the blog's categories: Category => Id + blog_cats = Hash.new + client.call("metaWeblog.getCategories", "", self.username, current_password).map do |cat| + blog_cats[cat['categoryId']] = cat['categoryName'] + end + # Convert the category strings to an array of categoryIds + post_cats = @post['categories'] + post_cats.each_index do |cat| + post_id = blog_cats.index(post_cats[cat]) + # Don't try to add categories that the blog doesn't know about! + if ! post_id.nil? + post_cats[cat] = Hash['categoryId' => post_id] + end + end + # Set the post categories. + client.call("mt.setPostCategories", "#{self.post_id}", self.username, current_password, post_cats) + end + show_post_page() end @mw_success = true