Update or Export WordPress with Ruby and Thor

I may be a devout rubyist, but I still have a soft spot for WordPress – and maybe even a little for PHP too. One of the things I think is lacking is a good way to manage WordPress updates since, as we all know, WordPress updates constantly. I wrote this quick thor script to help me update to the latest version from the command line so I can do quick version testing. As an added bonus, there is also an export task as well which will simulate doing a wordpress XML export from the dashboard.

So, for all you WordPress guys out there who hate doing manual updates, check this out. It may make life a little bit easier.

#!/usr/bin/env ruby
# wordpress.rb
require 'rubygems'
require 'thor'
require 'open-uri'

class Wordpress < Thor
  include Thor::Actions
  
  WORDPRESS_URL = %{http://wordpress.org}
  FILENAME = %{latest.tar.gz}
  TMP_PATH = %{/tmp}
  
  desc 'update', 'Update wordpress to the latest version'
  def update
    download_url = URI.join(WORDPRESS_URL, FILENAME)
    tmp_path = File.join(TMP_PATH, FILENAME)
    install_path = File.expand_path(File.dirname(__FILE__))
    
    say %{Downloading latest version of wordpress from #{download_url}}, :yellow
    run %{`which curl` #{download_url} -o #{tmp_path}}
    
    say %{Installing to #{install_path}}, :yellow
    if yes?(%{Continue? [yes/no]: }, :green)
      say %{Updating...}, :yellow
      run %{tar -C #{install_path} -xzf #{tmp_path} --strip 1}
    else
      say %{Update Aborted}, :red
    end
  end
  
  # See: http://drincruz.blogspot.com/2009/11/wordpress-cli-export.html
  desc 'export', 'Export wordpress to STDOUT (same as using the website)'
  def export
    code = %{
      require_once('wp-config.php');
      require_once('wp-admin/includes/export.php');
      export_wp();
    }.gsub(/\n|\s*/,'')

    run %{`which php` -r "#{code}"}, :verbose => false
  end 
end

Wordpress.start

Usage

  1. install thor gem
    • gem install thor
  2. add wordpress script to your wordpress install root and chmod it
    • chmod a+x wordpress
  3. update wordpress
    • ./wordpress update
  4. export wordpress (php must have CLI support)
    • ./wordpress export | gzip -c > ~/Desktop/wp_export.xml.gz

View Gist