Textpattern to Serendipity Migration Scripts

Not terribly riveting reading but this is the story of my migration from textpattern (version 4.0.4) to serendipity (version 1.1.3). This includes some PHP scripts I used to fix some problems on import – they’re not the best or the most robust in the world but they worked for me and I wanted to pop them here in case they work for someone else. Feedback on the code isn’t necessary because I won’t be using it again ;)

A running start

Firstly let me say that serendipity has a fabulous import feature which brought in 90% of my content about 30 seconds after I saw the button labelled “Import Data”. This is a real hurdle-remover for new users and definitely clinched the deal for me. Having done this I found that:

  • I had weird entity encodings everywhere
  • my images hadn’t arrived
  • everything seemed much more spaced out than it had been on the old blog


The images hadn’t been transferred and I also realised that the custom tags that textpattern uses, such as:

hadn’t been handled by the import script, so I kind of had two problems to solve. I started by transferring all the images across and renaming them to something more helpful.

This is going to be a really long article, but do read on if its helpful – I’m posting the rest as extended content so the disinterested need not scroll past it!Here’s the code I used to move my images (both databases were on the same server as were both installations – this helped to simplify matters quite a bit. Also I have used an exec() call in this script – maybe not the most secure thing in the world but perfect for this fast-and-dirty one-off import script.

This placed all my images in the relavant folder in the serendipity upload folder, which was great. But I still had those mangled image tags to deal with. I wrote a script which edited each entry (its not pretty but it really did work) and fixed this – here it is:

                foreach($matches[1] as $key=>$match) {
                        $replacement = '';
                        $new_body = preg_replace('/

It took a few attempts (mostly involving getting the same image multiple times on posts where there was more than one image.


The way that textpattern stores character entities gave me a problem when they were then imported into textpattern, and I had ’ and other such “features” all over the place. With a lot of swearing and some help from my friend (thanks Sara!) I eventually got it untangled using this:

                "–" => "-",
                "“" => "\"",
                "”" => "\"",
                "…" => "..."

while($row = mysql_fetch_assoc($results)) {

        $sql = 'update serendipity_comments set body = "'.mysql_real_escape_string(str_replace(array_keys($table), array_values($table),$row['body'])).'"
                where id='.$row['id'];
        echo $sql."\n";
        mysql_query($sql,$db) or die(mysql_error());


There might be a few others that got through but putting these in certainly sorted out the majority of the problems with the encoding. I’m a little unclear why textpattern and serendipity couldn’t communicate on this front but the main thing is that I did get most of my content through.

Vertical Space

The spacing problems that I had early on I “fixed” by running my code through a script to remove a load of whitespace – actually I was seeing this output because I had the nl2br plugin turned on. Sadly now I’ve formatted all my existing posts I can’t turn it off without re-reformatting them so I’m just living I with what I have for the time being!

Import Scripts and One-Shots

I’ve had quite a bit of experience doing data import routines and I applied some of the same principles to solving the problems I experiences with this migration from textpattern to serendipity. I’m really happy with the new platform, I’d like to hang on to some of the stuff that I used for this, and so I’ve stored it here. If it helps you too, then great :)

One thought on “Textpattern to Serendipity Migration Scripts

  1. This site uses a blogging platform called serendipity which is a nice little tool and I’ve been mostly happy since moving across from textpattern (I did write about the experience). Recently however, a few things have been going wrong with the feeds.

Leave a Reply

Please use [code] and [/code] around any source code you wish to share.

This site uses Akismet to reduce spam. Learn how your comment data is processed.