Migrating from sfSimpleBlog to WordPress

Sunday, 19 July 2009

The sfSimpleBlogPlugin makes adding a simple blogging platform to any Symfony application very easy. It hasn't really been actively developed for a long while, but it still sees a decent amount of use in the Symfony community simply because it's so easy to setup and integrate with an existing Symfony project. I developed a site around 3 years ago that used sfSimpleBlog. The requirements for the blog were very minimal at the time so it was a good fit. Since then though, their blog has turned into a central part of the site and now they need something a little less simple.

I found it hard to believe that no one's ever done this before, but a quick Google search wasn't much help, so I wrote a pake task to export the sfSimpleBlog articles to an XML format that could be imported using WordPress's built-in RSS importer. It's very basic and you should be able to edit it to your needs. It should be noted that this was written for Symfony 1.0 so the format for pake tasks may have changed for Symfony 1.2.

Basic Usage

From the command line:

./symfony blog-export frontend prod

… where "frontend" is simply the application you want to load the config from and "prod" is the environment that should be used.

By default it makes a file called simple_blog_dump.xml in the root of your Symfony project. (You can change this filepath in the code itself if you need.) You can then import this file from your WordPress administration page.

The Code

Download this file and rename it to "myPakeBlogExport.php" inside of your project_root/data/tasks/. The filename must start with myPake since Symfony won't load the task otherwise. Then run the pake task as mentioned in the section above.

UPDATE: It looks like my WordPress theme is mangling the following code block. Be sure to download and rename it instead. I'll leave the code block here for reference:


// Written by Mark Quezada (mark [at] mirthlab dot com)

pake_desc('Export sfSimpleBlogPosts to wordpress xml.');
pake_task('blog-export', 'project_exists');

function run_blog_export($task, $args)
    throw new Exception('You must specify an application to load configuration info from (e.g. frontend).');

  if(count($args) != 2) {
    throw new Exception('Please specify an environment (e.g. prod or dev).');
  $app = $args[0];
  $env = $args[1];
  $con = _file_storage_get_connection($app, $env);
  $posts = sfSimpleBlogPostPeer::doSelect(new Criteria(), $con);

  $lines = array();
  foreach ($posts as $post)
    $lines[] = '<item>';
    $lines[] = '  <pubDate>'.$post->getCreatedAt().'</pubDate>';
    foreach ($post->getsfSimpleBlogTags() as $tag)
      $lines[] = '  <category>'.$tag->getTag().'</category>';
    $lines[] = '  <title>'.$post->getTitle().'</title>';
    $lines[] = '  <content:encoded>'._blog_export_clean_content($post->getContent()).'</content:encoded>';

    foreach ($post->getsfSimpleBlogComments() as $comment)
      $lines[] = '  <wp:comment>';
      $lines[] = '    <wp:comment_author>'.$comment->getAuthorName().'</wp:comment_author>';
      $lines[] = '    <wp:comment_author_email>'.$comment->getAuthorEmail().'</wp:comment_author_email>';
      $lines[] = '    <wp:comment_author_url>'.$comment->getAuthorUrl().'</wp:comment_author_url>';
      $lines[] = '    <wp:comment_date>'.$comment->getCreatedAt().'</wp:comment_date>';
      $lines[] = '    <wp:comment_content>'._blog_export_clean_content($comment->getContent()).'</wp:comment_content>';
      $lines[] = '    <wp:comment_approved>'.$comment->getIsModerated().'</wp:comment_approved>';
      $lines[] = '  </wp:comment>';
    $lines[] = '</item>';

  $handle = fopen("simple_blog_dump.xml", "w");
  foreach($lines as $line)
    fwrite($handle, $line."\n");

function _blog_export_get_connection($app, $env)
  // define constants
  define('SF_ROOT_DIR',    sfConfig::get('sf_root_dir'));
  define('SF_APP',         $app);
  define('SF_ENVIRONMENT', $env);
  define('SF_DEBUG',       false);
  $connection = sfContext::getInstance()->getDatabaseConnection('propel');

  return $connection;

function _blog_export_clean_content($string)
  // check if the string contains html
  if (strlen($string) != strlen(strip_tags($string)))
    // it does, remove newlines as the wordpress importer will automatically convert them (again) to <p> tags
    $string = str_replace("\r\n",'',$string);
    $string = str_replace("\n",'',$string);
    $string = str_replace("\r",'',$string);
  // Note: I'm not removing newlines from posts that *don't* contain html since we want wordpress to add <p> tags in that case
  return $string;