Sam's infrequently-updated cabinet of curiosities
Monday, 25 April 2005

Post Versioning

I spent yesterday hacking the PEAR Text_Diff package into the wp_versioning plugin for WordPress. The idea is basically stolen from the MT-Revision plugin, which you can see in use here.

If I edit a post -- as I just did this one, to remove a misguided rant which I blame on sleep deprivation -- it is marked as a revision, and previous versions are kept available. The changes between them can be viewed as an inline diff. Previous edits were to fix unfortunate formatting mistakes, and the hilariously broken diffs show that, unfortunately, the diff function doesn't play all that well with Markdown.

The latest diff, at time of writing, is available here.

It's an acknowledgement that entries are liable to be updated, and an enforcement of accountability. If I attempt to take back my words -- after posting a misogynistic denunciation of all women, for example, or making a "sure win" bet with an embarrassing penalty -- the original version will still be there. (Not much of an enforcement, to be true, since I can always edit the versioning table in the database, but it's something.)

My changes to the beta version of the plugin are available here. (Note that the contents of that file are liable to change as I tweak things.) You'll also need Diff.php, Renderer.php and inline.php from the Text_Diff package.

It provides eight or so template tags, which I have documented in a half-assed possibly-phpDoc-compatible style.

Post content on single-post pages is replaced by old versions if version=* is requested, and by diffs if from=* is requested. If you ask for a diff from without a version, the diff is to the latest revision. Previous versions of the post title, excerpt etc. currently aren't displayed, though the two versions are saved separately and it's easy enough to add.

Currently I have it set to save at the publish_post plugin hook, with the side effect that I can edit drafts as much as I want without revisions being saved. (It's more to provide transparency than edit-versioning.) It's trivial to modify to run on the save_post and edit_post hooks if that's what you want.

Update: now displaying raw Markdown input, since filtering the diff through it caused too many problems. The copy I uploaded does not reflect this change.